@devries-cameron20 / t3code-1 / commits / 0450f854f2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(muse): a declined /compact reports its reason and no longer wedges the thread (#28454)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 7, 2026 at 17:11 UTC 0450f854f2cf54c8cfcfe541cfb056950056172c
2 files changed +202 -12
packages/provider-muse/src/server/adapter.test.ts
+172
@@ -1635,6 +1635,178 @@ describe("MuseAdapterV2", () => {
1635 }).pipe(Effect.scoped, Effect.provide(testLayer)),
1636 );
1637
1638 + it.effect("reports a compaction rejected at admission on its own terminal", () =>
1639 + Effect.gen(function* () {
1640 + const fake = yield* makeFakeMuse();
1641 + const harness = yield* makeHarness(fake);
1642 + fake.queueResponse("session/compact", new Error("command rejected: run_active"));
1643 + yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread));
1644 + const terminal = yield* harness.takeEvent("turn.terminal");
1645 + assert.strictEqual(terminal.status, "failed");
1646 + assert.strictEqual(terminal.threadDisposition, "reusable");
1647 + assert.include(terminal.failure?.message, "run_active");
1648 + yield* startConversation(harness, fake, 2);
1649 + }).pipe(Effect.scoped, Effect.provide(testLayer)),
1650 + );
1651 +
1652 + it.effect("treats recovering an already ended turn as done", () =>
1653 + Effect.gen(function* () {
1654 + const fake = yield* makeFakeMuse();
1655 + const harness = yield* makeHarness(fake);
1656 + const turn = yield* startConversation(harness, fake);
1657 + yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "completed" });
1658 + assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "completed");
1659 + yield* harness.runtime.interruptTurn({
1660 + providerThread: harness.providerThread,
1661 + providerTurnId: turn.providerTurn.id,
1662 + requestRuntimeRestart: true,
1663 + });
1664 + yield* startConversation(harness, fake, 2);
1665 + }).pipe(Effect.scoped, Effect.provide(testLayer)),
1666 + );
1667 +
1668 + it.effect("does not acknowledge Stop on an ended turn while a background subagent runs", () =>
1669 + Effect.gen(function* () {
1670 + const fake = yield* makeFakeMuse();
1671 + const harness = yield* makeHarness(fake);
1672 + const turn = yield* startConversation(harness, fake);
1673 + yield* fake.emit("item/started", {
1674 + turnId: turn.nativeId,
1675 + item: {
1676 + itemId: "background-child",
1677 + kind: "subagent",
1678 + revision: 1,
1679 + status: "inProgress",
1680 + turnId: turn.nativeId,
1681 + subagentId: "subagent-1",
1682 + controlStatus: "running",
1683 + childSessionId: "child-session-1",
1684 + objective: "Finish in the background",
1685 + },
1686 + });
1687 + yield* harness.takeEvent("app_thread.created");
1688 + yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "completed" });
1689 + yield* harness.takeEvent("turn.terminal");
1690 + const stopped = yield* harness.runtime
1691 + .interruptTurn({
1692 + providerThread: harness.providerThread,
1693 + providerTurnId: turn.providerTurn.id,
1694 + requestRuntimeRestart: true,
1695 + })
1696 + .pipe(Effect.exit);
1697 + assert.strictEqual(stopped._tag, "Failure");
1698 + assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!);
1699 + }).pipe(Effect.scoped, Effect.provide(testLayer)),
1700 + );
1701 +
1702 + it.effect("ends a compaction whose item names the held wake as its run", () =>
1703 + Effect.gen(function* () {
1704 + const fake = yield* makeFakeMuse();
1705 + const offered =
1706 + yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>();
1707 + const harness = yield* makeHarness(
1708 + fake,
1709 + INSTANCE_ID,
1710 + undefined,
1711 + undefined,
1712 + undefined,
1713 + runtimePolicy,
1714 + { continuationRequests: { offer: (request) => Deferred.succeed(offered, request) } },
1715 + );
1716 + const previous = yield* startConversation(harness, fake);
1717 + yield* fake.emit("turn/completed", { turnId: previous.nativeId, terminal: "completed" });
1718 + yield* harness.takeEvent("turn.terminal");
1719 + yield* fake.emit("turn/started", { turnId: "background-wake" });
1720 + yield* Deferred.await(offered);
1721 + yield* fake.emit("turn/completed", { turnId: "background-wake", terminal: "completed" });
1722 + yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread, 2));
1723 + yield* fake.emit("item/completed", {
1724 + item: {
1725 + itemId: "manual-compact",
1726 + kind: "compaction",
1727 + revision: 1,
1728 + status: "completed",
1729 + turnId: "background-wake",
1730 + outcome: "compacted",
1731 + },
1732 + });
1733 + const terminal = yield* harness.takeEvent("turn.terminal");
1734 + assert.strictEqual(terminal.runOrdinal, 2);
1735 + assert.strictEqual(terminal.status, "completed");
1736 + assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!);
1737 + }).pipe(Effect.scoped, Effect.provide(testLayer)),
1738 + );
1739 +
1740 + it.effect("drops a held wake when Stop closes the compacting host", () =>
1741 + Effect.gen(function* () {
1742 + const fake = yield* makeFakeMuse();
1743 + const offered =
1744 + yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>();
1745 + const harness = yield* makeHarness(
1746 + fake,
1747 + INSTANCE_ID,
1748 + undefined,
1749 + undefined,
1750 + undefined,
1751 + runtimePolicy,
1752 + { continuationRequests: { offer: (request) => Deferred.succeed(offered, request) } },
1753 + );
1754 + const previous = yield* startConversation(harness, fake);
1755 + yield* fake.emit("turn/completed", { turnId: previous.nativeId, terminal: "completed" });
1756 + yield* harness.takeEvent("turn.terminal");
1757 + yield* fake.emit("turn/started", { turnId: "background-wake" });
1758 + const continuation = yield* Deferred.await(offered);
1759 + yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread, 2));
1760 + const running = yield* harness.takeEvent(
1761 + "provider_turn.updated",
1762 + (event) => event.providerTurn.status === "running",
1763 + );
1764 + yield* harness.runtime.interruptTurn({
1765 + providerThread: harness.providerThread,
1766 + providerTurnId: running.providerTurn.id,
1767 + requestRuntimeRestart: true,
1768 + });
1769 + assert.isFalse(yield* harness.runtime.hasPendingBackgroundWork!);
1770 + const dispatched = yield* continuation.dispatchIfCurrent!(Effect.void);
1771 + assert.isTrue(Option.isNone(dispatched));
1772 + }).pipe(Effect.scoped, Effect.provide(testLayer)),
1773 + );
1774 +
1775 + it.effect("keeps a running wake held when Muse declines compaction", () =>
1776 + Effect.gen(function* () {
1777 + const fake = yield* makeFakeMuse();
1778 + const offered =
1779 + yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>();
1780 + const harness = yield* makeHarness(
1781 + fake,
1782 + INSTANCE_ID,
1783 + undefined,
1784 + undefined,
1785 + undefined,
1786 + runtimePolicy,
1787 + { continuationRequests: { offer: (request) => Deferred.succeed(offered, request) } },
1788 + );
1789 + const previous = yield* startConversation(harness, fake);
1790 + yield* fake.emit("turn/completed", { turnId: previous.nativeId, terminal: "completed" });
1791 + yield* harness.takeEvent("turn.terminal");
1792 + yield* fake.emit("turn/started", { turnId: "background-wake" });
1793 + yield* Deferred.await(offered);
1794 + fake.queueResponse("session/compact", new Error("command rejected: run_active"));
1795 + yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread, 2));
1796 + const terminal = yield* harness.takeEvent("turn.terminal");
1797 + assert.strictEqual(terminal.status, "failed");
1798 + assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!);
1799 + const stopped = yield* harness.runtime
1800 + .interruptTurn({
1801 + providerThread: harness.providerThread,
1802 + providerTurnId: terminal.providerTurnId,
1803 + requestRuntimeRestart: true,
1804 + })
1805 + .pipe(Effect.exit);
1806 + assert.strictEqual(stopped._tag, "Failure");
1807 + }).pipe(Effect.scoped, Effect.provide(testLayer)),
1808 + );
1809 +
1810 it.effect("projects native todos with stable identity and removes cancelled steps", () =>
1811 Effect.gen(function* () {
1812 const fake = yield* makeFakeMuse();
packages/provider-muse/src/server/adapter.ts
+30 -12
@@ -1384,7 +1384,12 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
1384 });
1385 }
1386 }
1387 - if (wake && active?.nativeId !== wake.nativeId) {
1387 + // Muse compacts the latest run, which can be the held wake; its compaction item ends this turn.
1388 + if (
1389 + wake &&
1390 + active?.nativeId !== wake.nativeId &&
1391 + !(active?.compact && itemEvent?.kind === "compaction")
1392 + ) {
1393 const turnId = typeof params.turnId === "string" ? params.turnId : itemEvent?.turnId;
1394 if (turnId === wake.nativeId) {
1395 wake.events.push([method, data]);
@@ -2057,8 +2062,9 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
2062 lastRunOrdinal: turnInput.runOrdinal,
2063 });
2064 yield* updateSession("running");
2060 - const held = wake;
2061 - wake = undefined;
2065 + // Compaction cannot join a running wake, so the wake stays held for its continuation.
2066 + const held = compact ? undefined : wake;
2067 + if (held) wake = undefined;
2068 if (continuation && !held) return yield* finish(turn, "completed");
2069 if (held && !continuation) turn.joined.add(held.nativeId);
2070 for (const [method, data] of held?.events ?? [])
@@ -2067,17 +2073,20 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
2073 if (continuation) return;
2074 yield* Effect.gen(function* () {
2075 if (compact) {
2070 - const result = yield* request("session/compact", {}, true, nativeId).pipe(
2076 + // The turn's terminal reports a declined compaction; only a broken host fails the command.
2077 + const declined = yield* request("session/compact", {}, true, nativeId).pipe(
2078 Effect.flatMap((value) => decode(MuseCompactResult, value)),
2079 + Effect.map((result) =>
2080 + result.status === "accepted"
2081 + ? undefined
2082 + : result.status === "noop"
2083 + ? (result.reason ?? "Muse has no context to compact.")
2084 + : `Muse returned an unsupported compaction status: ${result.status}`,
2085 + ),
2086 + Effect.catch((error) => (broken ? Effect.fail(error) : Effect.succeed(error.detail))),
2087 );
2073 - if (result.status === "noop")
2074 - yield* eventPermit.withPermits(1)(
2075 - finish(turn, "failed", result.reason ?? "Muse has no context to compact."),
2076 - );
2077 - else if (result.status !== "accepted")
2078 - return yield* protocolError(
2079 - `Muse returned an unsupported compaction status: ${result.status}`,
2080 - );
2088 + if (declined !== undefined)
2089 + yield* eventPermit.withPermits(1)(finish(turn, "failed", declined));
2090 } else {
2091 const result = yield* request(
2092 "turn/start",
@@ -2257,6 +2266,14 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
2266 interruptTurn: Effect.fn("MuseAdapter.interruptTurn")(
2267 function* (args) {
2268 const turn = active;
2269 + // Recovering an ended turn is done unless Muse still runs work Stop cannot end.
2270 + if (
2271 + !turn &&
2272 + args.requestRuntimeRestart === true &&
2273 + observedChildren.size === 0 &&
2274 + wake === undefined
2275 + )
2276 + return;
2277 if (
2278 !turn ||
2279 turn.providerTurn.id !== args.providerTurnId ||
@@ -2274,6 +2291,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
2291 catch: (cause) => protocolError("Cannot stop Muse compaction", cause),
2292 }).pipe(Effect.onError(failHost));
2293 broken = true;
2294 + wake = undefined;
2295 yield* finish(turn, "interrupted", undefined, "broken");
2296 yield* settleObservedChildren("interrupted");
2297 yield* updateSession("stopped");