Skip to content

Commit 4239e37

Browse files
shivamhwpclaude
andcommitted
fix(muse): hold pre-ack compaction items and keep unowned runs' requests visible
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d87e7be commit 4239e37

2 files changed

Lines changed: 68 additions & 5 deletions

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,16 +1698,32 @@ describe("MuseAdapterV2", () => {
16981698
const fake = yield* makeFakeMuse();
16991699
const harness = yield* makeHeldWakeHarness(fake);
17001700
const rejection = pendingPromise<Record<string, unknown>>();
1701+
const acceptance = pendingPromise<Record<string, unknown>>();
17011702
fake.queueResponse("session/compact", rejection.promise);
1703+
fake.queueResponse("session/compact", acceptance.promise);
17021704
const compacting = yield* harness.runtime.compactThread!(
17031705
yield* turnInput(harness.providerThread),
17041706
).pipe(Effect.forkScoped);
17051707
yield* fake.takeCall("session/compact");
1708+
// Before the ack, the wake's own automatic compaction must not end this one.
1709+
yield* fake.emit("item/completed", compactionItem("auto-compaction"));
1710+
yield* fake.emit("session/contextUsage", { usedTokens: 1 });
1711+
yield* harness.takeEvent(
1712+
"provider_thread.updated",
1713+
(event) => event.providerThread.contextUsage?.usedTokens === 1,
1714+
);
17061715
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" });
17071716
rejection.reject(runActive());
1708-
yield* Fiber.join(compacting);
17091717
yield* fake.takeCall("session/compact");
1718+
// Muse can finish the compaction before its ack arrives.
17101719
yield* fake.emit("item/completed", compactionItem("compaction-1"));
1720+
yield* fake.emit("session/contextUsage", { usedTokens: 2 });
1721+
yield* harness.takeEvent(
1722+
"provider_thread.updated",
1723+
(event) => event.providerThread.contextUsage?.usedTokens === 2,
1724+
);
1725+
acceptance.resolve({ status: "accepted" });
1726+
yield* Fiber.join(compacting);
17111727
assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "completed");
17121728
}).pipe(Effect.scoped, Effect.provide(testLayer)),
17131729
);
@@ -1814,6 +1830,18 @@ describe("MuseAdapterV2", () => {
18141830
(event) => event.runtimeRequest.status === "resolved",
18151831
);
18161832
assert.strictEqual(resolved.runtimeRequest.id, shown.runtimeRequest.id);
1833+
// wake-2 started while compaction waited, so its later requests stay visible too.
1834+
yield* fake.emit("approval/requested", {
1835+
...approval("wake-2"),
1836+
approvalId: "approval-2",
1837+
currentRequirementId: { approvalId: "approval-2", sourceIndex: 0 },
1838+
});
1839+
yield* harness.takeEvent(
1840+
"runtime_request.updated",
1841+
(event) =>
1842+
event.runtimeRequest.id !== shown.runtimeRequest.id &&
1843+
event.runtimeRequest.status === "pending",
1844+
);
18171845
}).pipe(Effect.scoped, Effect.provide(testLayer)),
18181846
);
18191847

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
363363
let compactWait:
364364
| { readonly turn: ActiveTurn; readonly ready: Deferred.Deferred<void> }
365365
| undefined;
366+
// Before Muse answers session/compact, a compaction item may be ours or a wake's.
367+
const compactCandidates: Array<[string, unknown]> = [];
366368
const emit = (event: ProviderAdapter.ProviderAdapterV2Event) =>
367369
Queue.offer(events, event).pipe(Effect.asVoid);
368370
const decode = <A, I>(schema: Schema.Codec<A, I>, data: unknown) =>
@@ -1116,6 +1118,12 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11161118
});
11171119
const owns = (turn: ActiveTurn, turnId: string) =>
11181120
turnId === turn.nativeId || turn.joined.has(turnId);
1121+
// A live run no turn owns, such as one started while compaction waited, still asks.
1122+
const unownedRun = (turnId: unknown) =>
1123+
typeof turnId === "string" &&
1124+
hostRuns.has(turnId) &&
1125+
turnId !== wake?.nativeId &&
1126+
!(active && owns(active, turnId));
11191127
const decideApproval = Effect.fnUntraced(function* (
11201128
entry: PendingRequest,
11211129
decision: ProviderApprovalDecision | undefined,
@@ -1160,7 +1168,10 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11601168
);
11611169
// Compaction never asks, so requests during it come from runs it waits on.
11621170
const runless =
1163-
child || active?.compact === true || previous?.request.providerTurnId === null;
1171+
child ||
1172+
active?.compact === true ||
1173+
previous?.request.providerTurnId === null ||
1174+
unownedRun(native.value.turnId);
11641175
const turn = runless ? undefined : active;
11651176
if (
11661177
!thread ||
@@ -1404,6 +1415,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14041415
if (
14051416
REQUEST_NOTIFICATIONS.has(method) &&
14061417
(active?.compact ||
1418+
unownedRun(params.turnId) ||
14071419
[...pending.values()].some(
14081420
(entry) =>
14091421
entry.request.providerTurnId === null &&
@@ -1439,13 +1451,23 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14391451
});
14401452
}
14411453
}
1454+
if (
1455+
active?.compact &&
1456+
active.compactPhase === "sending" &&
1457+
itemEvent?.kind === "compaction"
1458+
) {
1459+
const entry: [string, unknown] = [method, data];
1460+
compactCandidates.push(entry);
1461+
if (wake && itemEvent.turnId === wake.nativeId) wake.events.push(entry);
1462+
return;
1463+
}
14421464
if (wake && active?.nativeId !== wake.nativeId) {
14431465
const turnId = typeof params.turnId === "string" ? params.turnId : itemEvent?.turnId;
14441466
if (
14451467
turnId === wake.nativeId &&
14461468
!(
14471469
active?.compact &&
1448-
active.compactPhase !== "waiting" &&
1470+
active.compactPhase === "admitted" &&
14491471
itemEvent?.kind === "compaction"
14501472
)
14511473
) {
@@ -1465,7 +1487,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14651487
if (
14661488
item.turnId &&
14671489
!owns(turn, item.turnId) &&
1468-
!(turn.compact && turn.compactPhase !== "waiting" && item.kind === "compaction")
1490+
!(turn.compact && turn.compactPhase === "admitted" && item.kind === "compaction")
14691491
)
14701492
return;
14711493
const previous = turn.items.get(item.itemId);
@@ -2076,11 +2098,13 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20762098
while (true) {
20772099
const ended = hostRunsEnded;
20782100
turn.compactPhase = "sending";
2101+
compactCandidates.length = 0;
20792102
const admission = yield* request("session/compact", {}, true, commandId).pipe(
20802103
Effect.flatMap((value) => decode(MuseCompactResult, value)),
20812104
Effect.catchIf(isRunActive, (busy) =>
20822105
Effect.sync(() => {
20832106
turn.compactPhase = "waiting";
2107+
compactCandidates.length = 0;
20842108
return { busy };
20852109
}),
20862110
),
@@ -2096,7 +2120,18 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20962120
return yield* protocolError(
20972121
`Muse returned an unsupported compaction status: ${admission.status}`,
20982122
);
2099-
turn.compactPhase = "admitted";
2123+
yield* eventPermit.withPermits(1)(
2124+
Effect.gen(function* () {
2125+
turn.compactPhase = "admitted";
2126+
const candidates = compactCandidates.splice(0);
2127+
if (active !== turn) return;
2128+
for (const entry of candidates) {
2129+
const index = wake ? wake.events.indexOf(entry) : -1;
2130+
if (index >= 0) wake?.events.splice(index, 1);
2131+
yield* handleNotification(...entry).pipe(Effect.catch(failHost));
2132+
}
2133+
}),
2134+
);
21002135
return;
21012136
}
21022137
const next = yield* eventPermit.withPermits(1)(

0 commit comments

Comments
 (0)