|
Expand Up
|
@@ -360,6 +360,25 @@ const startConversation = Effect.fnUntraced(function* ( |
|
|
return { nativeId: call.commandId!, providerTurn: event.providerTurn }; |
|
|
}); |
|
|
|
|
|
// Returns once the adapter holds a live background Muse run, "wake-1". |
|
|
const makeHeldWakeHarness = Effect.fnUntraced(function* ( |
|
|
fake: Effect.Success<ReturnType<typeof makeFakeMuse>>, |
|
|
) { |
|
|
const offered = yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>(); |
|
|
const harness = yield* makeHarness( |
|
|
fake, |
|
|
INSTANCE_ID, |
|
|
undefined, |
|
|
undefined, |
|
|
undefined, |
|
|
runtimePolicy, |
|
|
{ continuationRequests: { offer: (request) => Deferred.succeed(offered, request) } }, |
|
|
); |
|
|
yield* fake.emit("turn/started", { turnId: "wake-1" }); |
|
|
yield* Deferred.await(offered); |
|
|
return harness; |
|
|
}); |
|
|
|
|
|
const SUBSCRIPTION_USAGE = { |
|
|
window: { usedPercent: 0, windowDurationMins: 300, resetsAtMs: 1791465557000 }, |
|
|
weekly: { usedPercent: 5, resetsAtMs: 1791763200000 }, |
|
Expand Down
Expand Up
|
@@ -1622,19 +1641,253 @@ describe("MuseAdapterV2", () => { |
|
|
}).pipe(Effect.scoped, Effect.provide(testLayer)), |
|
|
); |
|
|
|
|
|
it.effect("reports a no-op compaction as a visible failure and permits another turn", () => |
|
|
it.effect("reports unsuccessful compaction as a visible failure and permits another turn", () => |
|
|
Effect.gen(function* () { |
|
|
const rejected = (reason: string) => |
|
|
new MspError({ |
|
|
code: -32030, |
|
|
message: `session/compact command rejected: ${reason}`, |
|
|
data: { kind: "commandRejected", reason, retryable: false }, |
|
|
}); |
|
|
for (const [response, detail] of [ |
|
|
[{ status: "noop", reason: "no_compactable_history" }, "no_compactable_history"], |
|
|
[rejected("invalid_target"), "invalid_target"], |
|
|
// No live run was observed, so there is nothing to wait for. |
|
|
[rejected("run_active"), "run_active"], |
|
|
] as const) { |
|
|
const fake = yield* makeFakeMuse(); |
|
|
const harness = yield* makeHarness(fake); |
|
|
fake.queueResponse("session/compact", response); |
|
|
yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread)); |
|
|
const terminal = yield* harness.takeEvent("turn.terminal"); |
|
|
assert.strictEqual(terminal.status, "failed"); |
|
|
assert.include(terminal.failure?.message, detail); |
|
|
assert.strictEqual(terminal.threadDisposition, "reusable"); |
|
|
assert.lengthOf( |
|
|
fake.calls.filter((call) => call.method === "session/compact"), |
|
|
1, |
|
|
); |
|
|
yield* startConversation(harness, fake, 2); |
|
|
assert.lengthOf( |
|
|
harness.allEvents.filter((event) => event.type === "turn.terminal"), |
|
|
1, |
|
|
); |
|
|
} |
|
|
}).pipe(Effect.scoped, Effect.provide(testLayer)), |
|
|
); |
|
|
|
|
|
const runActive = () => |
|
|
new MspError({ |
|
|
code: -32030, |
|
|
message: "session/compact command rejected: run_active", |
|
|
data: { kind: "commandRejected", reason: "run_active", retryable: false }, |
|
|
}); |
|
|
const compactionItem = (itemId: string) => ({ |
|
|
item: { |
|
|
itemId, |
|
|
turnId: "wake-1", |
|
|
kind: "compaction", |
|
|
status: "completed", |
|
|
outcome: "compacted", |
|
|
revision: 1, |
|
|
}, |
|
|
}); |
|
|
|
|
|
it.effect("compacts when a held Muse run ends before its run_active rejection arrives", () => |
|
|
Effect.gen(function* () { |
|
|
const fake = yield* makeFakeMuse(); |
|
|
const harness = yield* makeHarness(fake); |
|
|
fake.queueResponse("session/compact", { status: "noop", reason: "no_compactable_history" }); |
|
|
const harness = yield* makeHeldWakeHarness(fake); |
|
|
const rejection = pendingPromise<Record<string, unknown>>(); |
|
|
fake.queueResponse("session/compact", rejection.promise); |
|
|
const compacting = yield* harness.runtime.compactThread!( |
|
|
yield* turnInput(harness.providerThread), |
|
|
).pipe(Effect.forkScoped); |
|
|
yield* fake.takeCall("session/compact"); |
|
|
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" }); |
|
|
rejection.reject(runActive()); |
|
|
yield* Fiber.join(compacting); |
|
|
yield* fake.takeCall("session/compact"); |
|
|
yield* fake.emit("item/completed", compactionItem("compaction-1")); |
|
|
assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "completed"); |
|
|
}).pipe(Effect.scoped, Effect.provide(testLayer)), |
|
|
); |
|
|
|
|
|
it.effect.each([ |
|
|
{ asked: "before compaction", run: "wake-1" }, |
|
|
{ asked: "while compaction waits", run: "wake-1" }, |
|
|
{ asked: "while compaction waits", run: "wake-2" }, |
|
|
] as const)( |
|
|
"shows $run's approval raised $asked so compaction retries once runs end", |
|
|
({ asked, run }) => |
|
|
Effect.gen(function* () { |
|
|
const fake = yield* makeFakeMuse(); |
|
|
const harness = yield* makeHeldWakeHarness(fake); |
|
|
if (asked === "before compaction") { |
|
|
// An earlier automatic compaction must stay with the wake, not end this turn. |
|
|
yield* fake.emit("item/completed", compactionItem("auto-compaction")); |
|
|
yield* fake.emit("approval/requested", approval(run)); |
|
|
// Native events are processed in order; this one marks the two above as buffered. |
|
|
yield* fake.emit("session/contextUsage", { usedTokens: 1 }); |
|
|
yield* harness.takeEvent( |
|
|
"provider_thread.updated", |
|
|
(event) => event.providerThread.contextUsage?.usedTokens === 1, |
|
|
); |
|
|
} |
|
|
fake.queueResponse("session/compact", runActive()); |
|
|
yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread)); |
|
|
const first = yield* fake.takeCall("session/compact"); |
|
|
// A wake's own automatic compaction must not end the waiting manual one. |
|
|
yield* fake.emit("item/completed", compactionItem("auto-compaction-2")); |
|
|
if (run === "wake-2") { |
|
|
yield* fake.emit("turn/started", { turnId: "wake-2" }); |
|
|
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" }); |
|
|
} |
|
|
if (asked === "while compaction waits") |
|
|
yield* fake.emit("approval/requested", approval(run)); |
|
|
const shown = yield* harness.takeEvent( |
|
|
"runtime_request.updated", |
|
|
(event) => event.runtimeRequest.status === "pending", |
|
|
); |
|
|
yield* harness.runtime.respondToRuntimeRequest({ |
|
|
requestId: shown.runtimeRequest.id, |
|
|
decision: "accept", |
|
|
}); |
|
|
yield* fake.takeCall("approval/decide"); |
|
|
yield* fake.emit("approval/resolved", { approvalId: "approval-1", turnId: run }); |
|
|
yield* fake.emit("turn/completed", { turnId: run, terminal: "completed" }); |
|
|
const retry = yield* fake.takeCall("session/compact"); |
|
|
// Command IDs are idempotency keys, so a reused one would replay the rejection. |
|
|
assert.notStrictEqual(retry.commandId, first.commandId); |
|
|
// The compaction runs under its own target run, not the command ID. |
|
|
yield* fake.emit("item/completed", compactionItem("compaction-1")); |
|
|
const terminal = yield* harness.takeEvent("turn.terminal"); |
|
|
assert.strictEqual(terminal.status, "completed"); |
|
|
assert.strictEqual(terminal.threadDisposition, "reusable"); |
|
|
yield* startConversation(harness, fake, 2); |
|
|
}).pipe(Effect.scoped, Effect.provide(testLayer)), |
|
|
); |
|
|
|
|
|
it.effect("keeps a request shown during compaction live after the compaction is stopped", () => |
|
|
Effect.gen(function* () { |
|
|
const fake = yield* makeFakeMuse(); |
|
|
const harness = yield* makeHeldWakeHarness(fake); |
|
|
fake.queueResponse("session/compact", runActive()); |
|
|
yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread)); |
|
|
const terminal = yield* harness.takeEvent("turn.terminal"); |
|
|
assert.strictEqual(terminal.status, "failed"); |
|
|
assert.strictEqual(terminal.threadDisposition, "reusable"); |
|
|
yield* startConversation(harness, fake, 2); |
|
|
yield* fake.takeCall("session/compact"); |
|
|
const running = yield* harness.takeEvent( |
|
|
"provider_turn.updated", |
|
|
(event) => event.providerTurn.status === "running", |
|
|
); |
|
|
yield* fake.emit("turn/started", { turnId: "wake-2" }); |
|
|
yield* fake.emit("approval/requested", approval("wake-2")); |
|
|
const shown = yield* harness.takeEvent( |
|
|
"runtime_request.updated", |
|
|
(event) => event.runtimeRequest.status === "pending", |
|
|
); |
|
|
yield* harness.runtime.interruptTurn({ |
|
|
providerThread: harness.providerThread, |
|
|
providerTurnId: running.providerTurn.id, |
|
|
}); |
|
|
assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "interrupted"); |
|
|
yield* fake.emit("approval/updated", { |
|
|
...approval("wake-2"), |
|
|
currentRequirementId: { approvalId: "approval-1", sourceIndex: 1 }, |
|
|
}); |
|
|
yield* harness.takeEvent( |
|
|
"runtime_request.updated", |
|
|
(event) => |
|
|
event.runtimeRequest.id === shown.runtimeRequest.id && |
|
|
event.runtimeRequest.status === "pending", |
|
|
); |
|
|
yield* harness.runtime.respondToRuntimeRequest({ |
|
|
requestId: shown.runtimeRequest.id, |
|
|
decision: "accept", |
|
|
}); |
|
|
const decision = yield* fake.takeCall("approval/decide"); |
|
|
assert.deepStrictEqual(decision.params.requirementId, { |
|
|
approvalId: "approval-1", |
|
|
sourceIndex: 1, |
|
|
}); |
|
|
yield* fake.emit("approval/resolved", { approvalId: "approval-1", turnId: "wake-2" }); |
|
|
const resolved = yield* harness.takeEvent( |
|
|
"runtime_request.updated", |
|
|
(event) => event.runtimeRequest.status === "resolved", |
|
|
); |
|
|
assert.strictEqual(resolved.runtimeRequest.id, shown.runtimeRequest.id); |
|
|
}).pipe(Effect.scoped, Effect.provide(testLayer)), |
|
|
); |
|
|
|
|
|
it.effect.each(["before compaction", "during admission"] as const)( |
|
|
"keeps a Muse wake that started $0 when stopping a waiting compaction", |
|
|
(wakeStarted) => |
|
|
Effect.gen(function* () { |
|
|
const fake = yield* makeFakeMuse(); |
|
|
const offered = yield* Deferred.make<void>(); |
|
|
const harness = yield* makeHarness( |
|
|
fake, |
|
|
INSTANCE_ID, |
|
|
undefined, |
|
|
undefined, |
|
|
undefined, |
|
|
runtimePolicy, |
|
|
{ continuationRequests: { offer: () => Deferred.succeed(offered, undefined) } }, |
|
|
); |
|
|
if (wakeStarted === "before compaction") { |
|
|
yield* fake.emit("turn/started", { turnId: "wake-1" }); |
|
|
yield* Deferred.await(offered); |
|
|
} |
|
|
const admission = pendingPromise<Record<string, unknown>>(); |
|
|
fake.queueResponse("session/compact", admission.promise); |
|
|
const compacting = yield* harness.runtime.compactThread!( |
|
|
yield* turnInput(harness.providerThread), |
|
|
).pipe(Effect.forkScoped); |
|
|
yield* fake.takeCall("session/compact"); |
|
|
if (wakeStarted === "during admission") { |
|
|
yield* fake.emit("turn/started", { turnId: "wake-1" }); |
|
|
yield* Deferred.await(offered); |
|
|
} |
|
|
admission.reject(runActive()); |
|
|
yield* Fiber.join(compacting); |
|
|
const running = yield* harness.takeEvent( |
|
|
"provider_turn.updated", |
|
|
(event) => event.providerTurn.status === "running", |
|
|
); |
|
|
yield* harness.runtime.interruptTurn({ |
|
|
providerThread: harness.providerThread, |
|
|
providerTurnId: running.providerTurn.id, |
|
|
}); |
|
|
const terminal = yield* harness.takeEvent("turn.terminal"); |
|
|
assert.strictEqual(terminal.status, "interrupted"); |
|
|
assert.strictEqual(terminal.threadDisposition, "reusable"); |
|
|
assert.strictEqual(fake.closeCount(), 0); |
|
|
assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!); |
|
|
yield* fake.emit("item/completed", { |
|
|
item: { |
|
|
itemId: "wake-output", |
|
|
turnId: "wake-1", |
|
|
kind: "agentMessage", |
|
|
revision: 1, |
|
|
status: "completed", |
|
|
text: "Background wake finished.", |
|
|
}, |
|
|
}); |
|
|
yield* fake.emit("turn/completed", { turnId: "wake-1", terminal: "completed" }); |
|
|
const continuation = yield* turnInput(harness.providerThread, 2); |
|
|
yield* harness.runtime.startTurn({ |
|
|
...continuation, |
|
|
message: { ...continuation.message, createdBy: "agent", creationSource: "provider" }, |
|
|
}); |
|
|
const output = yield* harness.takeEvent("message.updated"); |
|
|
assert.strictEqual(output.message.text, "Background wake finished."); |
|
|
assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "completed"); |
|
|
yield* startConversation(harness, fake, 3); |
|
|
assert.lengthOf( |
|
|
fake.calls.filter((call) => call.method === "session/compact"), |
|
|
1, |
|
|
); |
|
|
}).pipe(Effect.scoped, Effect.provide(testLayer)), |
|
|
); |
|
|
|
|
|
it.effect("projects native todos with stable identity and removes cancelled steps", () => |
|
|
Effect.gen(function* () { |
|
|
const fake = yield* makeFakeMuse(); |
|
Expand Down
|
|