@@ -363,6 +363,8 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( |
363 | 363 | let compactWait: |
364 | 364 | | { readonly turn: ActiveTurn; readonly ready: Deferred.Deferred<void> } |
365 | 365 | | undefined; |
| 366 | + // Before Muse answers session/compact, a compaction item may be ours or a wake's. |
| 367 | + const compactCandidates: Array<[string, unknown]> = []; |
366 | 368 | const emit = (event: ProviderAdapter.ProviderAdapterV2Event) => |
367 | 369 | Queue.offer(events, event).pipe(Effect.asVoid); |
368 | 370 | const decode = <A, I>(schema: Schema.Codec<A, I>, data: unknown) => |
@@ -1116,6 +1118,12 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( |
1116 | 1118 | }); |
1117 | 1119 | const owns = (turn: ActiveTurn, turnId: string) => |
1118 | 1120 | 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)); |
1119 | 1127 | const decideApproval = Effect.fnUntraced(function* ( |
1120 | 1128 | entry: PendingRequest, |
1121 | 1129 | decision: ProviderApprovalDecision | undefined, |
@@ -1160,7 +1168,10 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( |
1160 | 1168 | ); |
1161 | 1169 | // Compaction never asks, so requests during it come from runs it waits on. |
1162 | 1170 | 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); |
1164 | 1175 | const turn = runless ? undefined : active; |
1165 | 1176 | if ( |
1166 | 1177 | !thread || |
@@ -1404,6 +1415,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( |
1404 | 1415 | if ( |
1405 | 1416 | REQUEST_NOTIFICATIONS.has(method) && |
1406 | 1417 | (active?.compact || |
| 1418 | + unownedRun(params.turnId) || |
1407 | 1419 | [...pending.values()].some( |
1408 | 1420 | (entry) => |
1409 | 1421 | entry.request.providerTurnId === null && |
@@ -1439,13 +1451,23 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( |
1439 | 1451 | }); |
1440 | 1452 | } |
1441 | 1453 | } |
| 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 | + } |
1442 | 1464 | if (wake && active?.nativeId !== wake.nativeId) { |
1443 | 1465 | const turnId = typeof params.turnId === "string" ? params.turnId : itemEvent?.turnId; |
1444 | 1466 | if ( |
1445 | 1467 | turnId === wake.nativeId && |
1446 | 1468 | !( |
1447 | 1469 | active?.compact && |
1448 | | - active.compactPhase !== "waiting" && |
| 1470 | + active.compactPhase === "admitted" && |
1449 | 1471 | itemEvent?.kind === "compaction" |
1450 | 1472 | ) |
1451 | 1473 | ) { |
@@ -1465,7 +1487,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( |
1465 | 1487 | if ( |
1466 | 1488 | item.turnId && |
1467 | 1489 | !owns(turn, item.turnId) && |
1468 | | - !(turn.compact && turn.compactPhase !== "waiting" && item.kind === "compaction") |
| 1490 | + !(turn.compact && turn.compactPhase === "admitted" && item.kind === "compaction") |
1469 | 1491 | ) |
1470 | 1492 | return; |
1471 | 1493 | const previous = turn.items.get(item.itemId); |
@@ -2076,11 +2098,13 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( |
2076 | 2098 | while (true) { |
2077 | 2099 | const ended = hostRunsEnded; |
2078 | 2100 | turn.compactPhase = "sending"; |
| 2101 | + compactCandidates.length = 0; |
2079 | 2102 | const admission = yield* request("session/compact", {}, true, commandId).pipe( |
2080 | 2103 | Effect.flatMap((value) => decode(MuseCompactResult, value)), |
2081 | 2104 | Effect.catchIf(isRunActive, (busy) => |
2082 | 2105 | Effect.sync(() => { |
2083 | 2106 | turn.compactPhase = "waiting"; |
| 2107 | + compactCandidates.length = 0; |
2084 | 2108 | return { busy }; |
2085 | 2109 | }), |
2086 | 2110 | ), |
@@ -2096,7 +2120,18 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( |
2096 | 2120 | return yield* protocolError( |
2097 | 2121 | `Muse returned an unsupported compaction status: ${admission.status}`, |
2098 | 2122 | ); |
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 | + ); |
2100 | 2135 | return; |
2101 | 2136 | } |
2102 | 2137 | const next = yield* eventPermit.withPermits(1)( |
|