Skip to content

Commit bcf6056

Browse files
shivamhwpclaude
andcommitted
fix(muse): show every run's requests while compaction waits
Replaying the whole wake buffer let a buffered compaction item end the new compaction turn, and a second run's approval was still dropped because only the held wake's id was routed. Treat any request raised during compaction as runless and pull only requests out of the buffer. Fold the live-run retry case into the approval cases and drop the stale-resume case: the adapter never reads activeTurnId. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 00da4c5 commit bcf6056

2 files changed

Lines changed: 77 additions & 106 deletions

File tree

‎packages/provider-muse/src/server/adapter.test.ts‎

Lines changed: 70 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,73 +1676,72 @@ describe("MuseAdapterV2", () => {
16761676
}).pipe(Effect.scoped, Effect.provide(testLayer)),
16771677
);
16781678

1679+
const runActive = () =>
1680+
new MspError({
1681+
code: -32030,
1682+
message: "session/compact command rejected: run_active",
1683+
data: { kind: "commandRejected", reason: "run_active", retryable: false },
1684+
});
1685+
const compactionItem = (itemId: string) => ({
1686+
item: {
1687+
itemId,
1688+
turnId: "wake-1",
1689+
kind: "compaction",
1690+
status: "completed",
1691+
outcome: "compacted",
1692+
revision: 1,
1693+
},
1694+
});
1695+
1696+
it.effect("compacts when a held Muse run ends before its run_active rejection arrives", () =>
1697+
Effect.gen(function* () {
1698+
const fake = yield* makeFakeMuse();
1699+
const harness = yield* makeHeldWakeHarness(fake);
1700+
const rejection = pendingPromise<Record<string, unknown>>();
1701+
fake.queueResponse("session/compact", rejection.promise);
1702+
const compacting = yield* harness.runtime.compactThread!(
1703+
yield* turnInput(harness.providerThread),
1704+
).pipe(Effect.forkScoped);
1705+
yield* fake.takeCall("session/compact");
1706+
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" });
1707+
rejection.reject(runActive());
1708+
yield* Fiber.join(compacting);
1709+
yield* fake.takeCall("session/compact");
1710+
yield* fake.emit("item/completed", compactionItem("compaction-1"));
1711+
assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "completed");
1712+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1713+
);
1714+
16791715
it.effect.each([
1680-
{ when: "while the run is still going", endBeforeRejection: false },
1681-
{ when: "after the run already ended", endBeforeRejection: true },
1716+
{ asked: "before compaction", run: "wake-1" },
1717+
{ asked: "while compaction waits", run: "wake-1" },
1718+
{ asked: "while compaction waits", run: "wake-2" },
16821719
] as const)(
1683-
"compacts once a held Muse run ends when the rejection arrives $when",
1684-
({ endBeforeRejection }) =>
1720+
"shows $run's approval raised $asked so compaction retries once runs end",
1721+
({ asked, run }) =>
16851722
Effect.gen(function* () {
16861723
const fake = yield* makeFakeMuse();
16871724
const harness = yield* makeHeldWakeHarness(fake);
1688-
const rejection = pendingPromise<Record<string, unknown>>();
1689-
fake.queueResponse("session/compact", rejection.promise);
1690-
const compacting = yield* harness.runtime.compactThread!(
1691-
yield* turnInput(harness.providerThread),
1692-
).pipe(Effect.forkScoped);
1725+
if (asked === "before compaction") {
1726+
// An earlier automatic compaction must stay with the wake, not end this turn.
1727+
yield* fake.emit("item/completed", compactionItem("auto-compaction"));
1728+
yield* fake.emit("approval/requested", approval(run));
1729+
// Native events are processed in order; this one marks the two above as buffered.
1730+
yield* fake.emit("session/contextUsage", { usedTokens: 1 });
1731+
yield* harness.takeEvent(
1732+
"provider_thread.updated",
1733+
(event) => event.providerThread.contextUsage?.usedTokens === 1,
1734+
);
1735+
}
1736+
fake.queueResponse("session/compact", runActive());
1737+
yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread));
16931738
const first = yield* fake.takeCall("session/compact");
1694-
if (endBeforeRejection)
1739+
if (run === "wake-2") {
1740+
yield* fake.emit("turn/started", { turnId: "wake-2" });
16951741
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" });
1696-
rejection.reject(
1697-
new MspError({
1698-
code: -32030,
1699-
message: "session/compact command rejected: run_active",
1700-
data: { kind: "commandRejected", reason: "run_active", retryable: false },
1701-
}),
1702-
);
1703-
yield* Fiber.join(compacting);
1704-
if (!endBeforeRejection)
1705-
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" });
1706-
const retry = yield* fake.takeCall("session/compact");
1707-
assert.notStrictEqual(retry.commandId, first.commandId);
1708-
// The compaction runs under its own target run, not the command ID.
1709-
yield* fake.emit("item/completed", {
1710-
item: {
1711-
itemId: "compaction-1",
1712-
turnId: "wake-1",
1713-
kind: "compaction",
1714-
status: "completed",
1715-
outcome: "compacted",
1716-
revision: 1,
1717-
},
1718-
});
1719-
const terminal = yield* harness.takeEvent("turn.terminal");
1720-
assert.strictEqual(terminal.status, "completed");
1721-
assert.strictEqual(terminal.threadDisposition, "reusable");
1722-
yield* startConversation(harness, fake, 2);
1723-
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1724-
);
1725-
1726-
it.effect.each(["before compaction", "while compaction waits"] as const)(
1727-
"shows a held Muse run's approval raised $0 so compaction can retry",
1728-
(asked) =>
1729-
Effect.gen(function* () {
1730-
const fake = yield* makeFakeMuse();
1731-
const harness = yield* makeHeldWakeHarness(fake);
1732-
if (asked === "before compaction")
1733-
yield* fake.emit("approval/requested", approval("wake-1"));
1734-
fake.queueResponse(
1735-
"session/compact",
1736-
new MspError({
1737-
code: -32030,
1738-
message: "session/compact command rejected: run_active",
1739-
data: { kind: "commandRejected", reason: "run_active", retryable: false },
1740-
}),
1741-
);
1742-
yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread));
1743-
yield* fake.takeCall("session/compact");
1742+
}
17441743
if (asked === "while compaction waits")
1745-
yield* fake.emit("approval/requested", approval("wake-1"));
1744+
yield* fake.emit("approval/requested", approval(run));
17461745
const shown = yield* harness.takeEvent(
17471746
"runtime_request.updated",
17481747
(event) => event.runtimeRequest.status === "pending",
@@ -1752,9 +1751,17 @@ describe("MuseAdapterV2", () => {
17521751
decision: "accept",
17531752
});
17541753
yield* fake.takeCall("approval/decide");
1755-
yield* fake.emit("approval/resolved", { approvalId: "approval-1", turnId: "wake-1" });
1756-
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" });
1757-
yield* fake.takeCall("session/compact");
1754+
yield* fake.emit("approval/resolved", { approvalId: "approval-1", turnId: run });
1755+
yield* fake.emit("turn/completed", { turnId: run, terminal: "completed" });
1756+
const retry = yield* fake.takeCall("session/compact");
1757+
// Command IDs are idempotency keys, so a reused one would replay the rejection.
1758+
assert.notStrictEqual(retry.commandId, first.commandId);
1759+
// The compaction runs under its own target run, not the command ID.
1760+
yield* fake.emit("item/completed", compactionItem("compaction-1"));
1761+
const terminal = yield* harness.takeEvent("turn.terminal");
1762+
assert.strictEqual(terminal.status, "completed");
1763+
assert.strictEqual(terminal.threadDisposition, "reusable");
1764+
yield* startConversation(harness, fake, 2);
17581765
}).pipe(Effect.scoped, Effect.provide(testLayer)),
17591766
);
17601767

@@ -1787,13 +1794,7 @@ describe("MuseAdapterV2", () => {
17871794
yield* fake.emit("turn/started", { turnId: "wake-1" });
17881795
yield* Deferred.await(offered);
17891796
}
1790-
admission.reject(
1791-
new MspError({
1792-
code: -32030,
1793-
message: "session/compact command rejected: run_active",
1794-
data: { kind: "commandRejected", reason: "run_active", retryable: false },
1795-
}),
1796-
);
1797+
admission.reject(runActive());
17971798
yield* Fiber.join(compacting);
17981799
const running = yield* harness.takeEvent(
17991800
"provider_turn.updated",
@@ -1835,36 +1836,6 @@ describe("MuseAdapterV2", () => {
18351836
}).pipe(Effect.scoped, Effect.provide(testLayer)),
18361837
);
18371838

1838-
it.effect("lets Muse admit compaction when a resumed session reports a stale active turn", () =>
1839-
Effect.gen(function* () {
1840-
const fake = yield* makeFakeMuse();
1841-
// muse-code-sdk#121: resume after compaction reports a finished turn as running.
1842-
fake.queueResponse("session/resume", {
1843-
session: {
1844-
sessionId: "saved-session",
1845-
modelId: MODEL,
1846-
activeTurnId: "finished-turn",
1847-
status: "running",
1848-
},
1849-
history: { items: [] },
1850-
});
1851-
const harness = yield* makeHarness(fake, INSTANCE_ID, "saved-session");
1852-
yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread));
1853-
yield* fake.takeCall("session/compact");
1854-
yield* fake.emit("item/completed", {
1855-
item: {
1856-
itemId: "compaction-1",
1857-
turnId: "compaction-target-run",
1858-
kind: "compaction",
1859-
status: "completed",
1860-
outcome: "compacted",
1861-
revision: 1,
1862-
},
1863-
});
1864-
assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "completed");
1865-
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1866-
);
1867-
18681839
it.effect("projects native todos with stable identity and removes cancelled steps", () =>
18691840
Effect.gen(function* () {
18701841
const fake = yield* makeFakeMuse();

‎packages/provider-muse/src/server/adapter.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,14 +1146,12 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11461146
/**
11471147
* A workflow child's approval is runless: the turn that started the workflow
11481148
* may end long before the child asks, and T3 keeps a runless request on the
1149-
* thread until Muse settles it. So is a held wake's request while compaction
1150-
* waits for that wake's run to end.
1149+
* thread until Muse settles it.
11511150
*/
11521151
const publishRequest = Effect.fnUntraced(function* (native: PendingRequest["native"]) {
11531152
const child = native.type === "approval" && native.value.subagentOrigin !== undefined;
1154-
const runless =
1155-
child ||
1156-
(active?.compact === true && wake !== undefined && native.value.turnId === wake.nativeId);
1153+
// Compaction never asks, so requests during it come from runs it waits on.
1154+
const runless = child || active?.compact === true;
11571155
const turn = runless ? undefined : active;
11581156
if (
11591157
!thread ||
@@ -2191,10 +2189,12 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
21912189
yield* updateSession("running");
21922190
const held = compact ? undefined : wake;
21932191
if (!compact) wake = undefined;
2194-
// Re-route what the wake buffered so requests it already raised are shown.
2192+
// Compaction waits for the held run, so requests it already raised are shown now.
21952193
else if (wake)
21962194
for (const [method, data] of wake.events.splice(0))
2197-
yield* handleNotification(method, data).pipe(Effect.catch(failHost));
2195+
if (REQUEST_NOTIFICATIONS.has(method))
2196+
yield* handleNotification(method, data).pipe(Effect.catch(failHost));
2197+
else wake.events.push([method, data]);
21982198
if (continuation && !held) return yield* finish(turn, "completed");
21992199
if (held && !continuation) turn.joined.add(held.nativeId);
22002200
for (const [method, data] of held?.events ?? [])

0 commit comments

Comments
 (0)