Skip to content

Commit d87e7be

Browse files
shivamhwpclaude
andcommitted
fix(muse): keep runless requests current and ignore wake compactions while waiting
An approval update for a request shown during a stopped compaction was dropped, leaving stale options and requirement ids. Route every request notification for an existing runless request past turn routing and keep it runless when republished. A held wake's automatic compaction could end a manual compaction that was still waiting to be admitted. Compaction items reach the manual turn only while it is not waiting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2c764e3 commit d87e7be

2 files changed

Lines changed: 64 additions & 36 deletions

File tree

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,8 @@ describe("MuseAdapterV2", () => {
17361736
fake.queueResponse("session/compact", runActive());
17371737
yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread));
17381738
const first = yield* fake.takeCall("session/compact");
1739+
// A wake's own automatic compaction must not end the waiting manual one.
1740+
yield* fake.emit("item/completed", compactionItem("auto-compaction-2"));
17391741
if (run === "wake-2") {
17401742
yield* fake.emit("turn/started", { turnId: "wake-2" });
17411743
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" });
@@ -1765,7 +1767,7 @@ describe("MuseAdapterV2", () => {
17651767
}).pipe(Effect.scoped, Effect.provide(testLayer)),
17661768
);
17671769

1768-
it.effect("settles a request shown during compaction after the compaction is stopped", () =>
1770+
it.effect("keeps a request shown during compaction live after the compaction is stopped", () =>
17691771
Effect.gen(function* () {
17701772
const fake = yield* makeFakeMuse();
17711773
const harness = yield* makeHeldWakeHarness(fake);
@@ -1787,6 +1789,25 @@ describe("MuseAdapterV2", () => {
17871789
providerTurnId: running.providerTurn.id,
17881790
});
17891791
assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "interrupted");
1792+
yield* fake.emit("approval/updated", {
1793+
...approval("wake-2"),
1794+
currentRequirementId: { approvalId: "approval-1", sourceIndex: 1 },
1795+
});
1796+
yield* harness.takeEvent(
1797+
"runtime_request.updated",
1798+
(event) =>
1799+
event.runtimeRequest.id === shown.runtimeRequest.id &&
1800+
event.runtimeRequest.status === "pending",
1801+
);
1802+
yield* harness.runtime.respondToRuntimeRequest({
1803+
requestId: shown.runtimeRequest.id,
1804+
decision: "accept",
1805+
});
1806+
const decision = yield* fake.takeCall("approval/decide");
1807+
assert.deepStrictEqual(decision.params.requirementId, {
1808+
approvalId: "approval-1",
1809+
sourceIndex: 1,
1810+
});
17901811
yield* fake.emit("approval/resolved", { approvalId: "approval-1", turnId: "wake-2" });
17911812
const resolved = yield* harness.takeEvent(
17921813
"runtime_request.updated",

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

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ interface ActiveTurn {
220220
flushScheduled: boolean;
221221
interruptRequested: boolean;
222222
compact: boolean;
223-
compactAdmitted: boolean;
223+
/** Muse admits compaction only when idle; while waiting, its compaction items are not ours. */
224+
compactPhase: "sending" | "waiting" | "admitted";
224225
}
225226

226227
interface PendingRequest {
@@ -1150,8 +1151,16 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11501151
*/
11511152
const publishRequest = Effect.fnUntraced(function* (native: PendingRequest["native"]) {
11521153
const child = native.type === "approval" && native.value.subagentOrigin !== undefined;
1154+
const nativeId =
1155+
native.type === "approval" ? native.value.approvalId : native.value.userInputId;
1156+
const previous = [...pending.values()].find(
1157+
(entry) =>
1158+
entry.native.type === native.type &&
1159+
entry.request.nativeRequestRef?.nativeId === nativeId,
1160+
);
11531161
// Compaction never asks, so requests during it come from runs it waits on.
1154-
const runless = child || active?.compact === true;
1162+
const runless =
1163+
child || active?.compact === true || previous?.request.providerTurnId === null;
11551164
const turn = runless ? undefined : active;
11561165
if (
11571166
!thread ||
@@ -1160,14 +1169,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11601169
(turn && native.value.turnId && !owns(turn, native.value.turnId))
11611170
)
11621171
return;
1163-
const nativeId =
1164-
native.type === "approval" ? native.value.approvalId : native.value.userInputId;
11651172
if (turn?.settledRequests.has(`${native.type}:${nativeId}`)) return;
1166-
const previous = [...pending.values()].find(
1167-
(entry) =>
1168-
entry.native.type === native.type &&
1169-
entry.request.nativeRequestRef?.nativeId === nativeId,
1170-
);
11711173
const providerTurnId = turn?.providerTurn.id ?? null;
11721174
const threadId = turn?.input.threadId ?? thread.appThreadId ?? input.threadId;
11731175
const requestId =
@@ -1397,20 +1399,17 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
13971399
// Child approvals use their own run IDs and can outlive turns; bypass turn routing.
13981400
if (params.subagentOrigin !== undefined && method.startsWith("approval/"))
13991401
return yield* handleRequestEvent(method, params, undefined);
1400-
// Runless requests outlive their turn, so their settlement does too.
1401-
const settled =
1402-
method === "approval/resolved"
1403-
? params.approvalId
1404-
: method === "userInput/settled"
1405-
? params.userInputId
1406-
: undefined;
1402+
// Requests during compaction come from runs it waits on, so they must reach the
1403+
// user now; runless requests outlive their turn.
14071404
if (
1408-
settled !== undefined &&
1409-
[...pending.values()].some(
1410-
(entry) =>
1411-
entry.request.providerTurnId === null &&
1412-
entry.request.nativeRequestRef?.nativeId === settled,
1413-
)
1405+
REQUEST_NOTIFICATIONS.has(method) &&
1406+
(active?.compact ||
1407+
[...pending.values()].some(
1408+
(entry) =>
1409+
entry.request.providerTurnId === null &&
1410+
entry.request.nativeRequestRef?.nativeId ===
1411+
(params.approvalId ?? params.userInputId),
1412+
))
14141413
)
14151414
return yield* handleRequestEvent(method, params, undefined);
14161415
if (
@@ -1442,14 +1441,16 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14421441
}
14431442
if (wake && active?.nativeId !== wake.nativeId) {
14441443
const turnId = typeof params.turnId === "string" ? params.turnId : itemEvent?.turnId;
1445-
if (turnId === wake.nativeId) {
1446-
// Compaction waits for this run, so its requests must reach the user now.
1447-
if (active?.compact && REQUEST_NOTIFICATIONS.has(method))
1448-
return yield* handleRequestEvent(method, params, undefined);
1449-
if (!(active?.compact && itemEvent?.kind === "compaction")) {
1450-
wake.events.push([method, data]);
1451-
return;
1452-
}
1444+
if (
1445+
turnId === wake.nativeId &&
1446+
!(
1447+
active?.compact &&
1448+
active.compactPhase !== "waiting" &&
1449+
itemEvent?.kind === "compaction"
1450+
)
1451+
) {
1452+
wake.events.push([method, data]);
1453+
return;
14531454
}
14541455
}
14551456
const turn = active;
@@ -1464,7 +1465,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14641465
if (
14651466
item.turnId &&
14661467
!owns(turn, item.turnId) &&
1467-
!(turn.compact && item.kind === "compaction")
1468+
!(turn.compact && turn.compactPhase !== "waiting" && item.kind === "compaction")
14681469
)
14691470
return;
14701471
const previous = turn.items.get(item.itemId);
@@ -2074,9 +2075,15 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20742075
let commandId = firstCommandId;
20752076
while (true) {
20762077
const ended = hostRunsEnded;
2078+
turn.compactPhase = "sending";
20772079
const admission = yield* request("session/compact", {}, true, commandId).pipe(
20782080
Effect.flatMap((value) => decode(MuseCompactResult, value)),
2079-
Effect.catchIf(isRunActive, (busy) => Effect.succeed({ busy })),
2081+
Effect.catchIf(isRunActive, (busy) =>
2082+
Effect.sync(() => {
2083+
turn.compactPhase = "waiting";
2084+
return { busy };
2085+
}),
2086+
),
20802087
);
20812088
if (!("busy" in admission)) {
20822089
if (admission.status === "noop") {
@@ -2089,7 +2096,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20892096
return yield* protocolError(
20902097
`Muse returned an unsupported compaction status: ${admission.status}`,
20912098
);
2092-
turn.compactAdmitted = true;
2099+
turn.compactPhase = "admitted";
20932100
return;
20942101
}
20952102
const next = yield* eventPermit.withPermits(1)(
@@ -2186,7 +2193,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
21862193
flushScheduled: false,
21872194
interruptRequested: false,
21882195
compact,
2189-
compactAdmitted: false,
2196+
compactPhase: "sending",
21902197
};
21912198
yield* Effect.gen(function* () {
21922199
thread = turnInput.providerThread;
@@ -2408,7 +2415,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
24082415
Effect.gen(function* () {
24092416
if (active !== turn) return;
24102417
// Nothing runs on the host before admission, so the host can stay.
2411-
if (!turn.compactAdmitted) return yield* finish(turn, "interrupted");
2418+
if (turn.compactPhase !== "admitted") return yield* finish(turn, "interrupted");
24122419
hostEpoch++;
24132420
yield* Effect.tryPromise({
24142421
try: () => host.close(),

0 commit comments

Comments
 (0)