Skip to content
280 changes: 280 additions & 0 deletions packages/provider-muse/src/server/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,286 @@ describe("MuseAdapterV2", () => {
}).pipe(Effect.scoped, Effect.provide(testLayer)),
);

it.effect("lists a running monitor as pending background work and stops it with the thread", () =>
Effect.gen(function* () {
const fake = yield* makeFakeMuse();
const harness = yield* makeHarness(fake);
const turn = yield* startConversation(harness, fake);
const monitor = {
itemId: "monitor-1",
kind: "toolCall",
status: "completed",
turnId: turn.nativeId,
tool: "monitor",
background: true,
};
yield* fake.emit("item/completed", {
turnId: turn.nativeId,
item: { ...monitor, revision: 1 },
});
const listed = yield* harness.takeEvent(
"provider_thread.updated",
(event) =>
event.providerThread.pendingBackgroundTasks?.some(
(task) => task.taskId === "monitor-1",
) === true,
);
assert.deepEqual(listed.providerThread.pendingBackgroundTasks, [
{ taskId: "monitor-1", kind: "monitor", description: "monitor", awaited: true },
]);

// The turn ends while Muse keeps watching; the listing keeps the thread waiting.
fake.queueResponse("task/list", {
tasks: [
{ taskId: "monitor-1", kind: "tool.monitor", status: "inProgress", tool: "monitor" },
],
});
yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "completed" });
yield* harness.takeEvent("turn.terminal");
assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!);

const stop = harness.runtime
.interruptTurn({
providerThread: harness.providerThread,
providerTurnId: turn.providerTurn.id,
requestRuntimeRestart: true,
})
.pipe(Effect.exit);
fake.queueResponse("task/stopAll", new Error("stop rejected"));
assert.strictEqual((yield* stop)._tag, "Failure");
assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!);
yield* fake.takeCall("task/stopAll");

assert.strictEqual((yield* stop)._tag, "Success");
assert.strictEqual((yield* fake.takeCall("task/stopAll")).method, "task/stopAll");
yield* harness.takeEvent(
"provider_thread.updated",
(event) => (event.providerThread.pendingBackgroundTasks?.length ?? 0) === 0,
);
assert.isFalse(yield* harness.runtime.hasPendingBackgroundWork!);
}).pipe(Effect.scoped, Effect.provide(testLayer)),
);

it.effect("lists a backgrounded tool call as a command and a monitor found on attach", () =>
Effect.gen(function* () {
const fake = yield* makeFakeMuse();
const harness = yield* makeHarness(fake);
const turn = yield* startConversation(harness, fake);
yield* fake.emit("item/updated", {
turnId: turn.nativeId,
item: {
itemId: "shell-1",
kind: "toolCall",
revision: 2,
status: "completed",
turnId: turn.nativeId,
tool: "shell",
background: true,
},
});
const listed = yield* harness.takeEvent(
"provider_thread.updated",
(event) =>
event.providerThread.pendingBackgroundTasks?.some((task) => task.taskId === "shell-1") ===
true,
);
assert.deepEqual(listed.providerThread.pendingBackgroundTasks, [
{ taskId: "shell-1", kind: "command", description: "shell" },
]);

// A monitor started before this client attached comes from `task/list` alone.
const resumed = yield* makeFakeMuse("resumed");
resumed.queueResponse("task/list", {
tasks: [{ taskId: "monitor-2", kind: "tool.monitor", status: "inProgress" }],
});
const attached = yield* makeHarness(resumed);
assert.deepEqual(attached.providerThread.pendingBackgroundTasks, [
{ taskId: "monitor-2", kind: "monitor", description: "monitor", awaited: true },
]);
}).pipe(Effect.scoped, Effect.provide(testLayer)),
);

it.effect("drops a task list read that predates Stop", () =>
Effect.gen(function* () {
const fake = yield* makeFakeMuse();
// A notification the adapter handles only after the held refresh returns.
const drained = yield* Deferred.make<void>();
const harness = yield* makeHarness(
fake,
INSTANCE_ID,
undefined,
undefined,
undefined,
runtimePolicy,
{ onUsageLimits: () => Deferred.succeed(drained, undefined).pipe(Effect.asVoid) },
);
const turn = yield* startConversation(harness, fake);
yield* fake.emit("item/completed", {
turnId: turn.nativeId,
item: {
itemId: "monitor-1",
kind: "toolCall",
status: "completed",
turnId: turn.nativeId,
tool: "monitor",
background: true,
revision: 1,
},
});
yield* harness.takeEvent(
"provider_thread.updated",
(event) => (event.providerThread.pendingBackgroundTasks?.length ?? 0) > 0,
);

// The refresh that follows the terminal event is held open while Stop runs.
const gate = pendingPromise<Record<string, unknown>>();
fake.queueResponse("task/list", gate.promise);
yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "completed" });
yield* harness.takeEvent("turn.terminal");
yield* fake.takeCall("task/list");

const stopped = yield* harness.runtime
.interruptTurn({
providerThread: harness.providerThread,
providerTurnId: turn.providerTurn.id,
requestRuntimeRestart: true,
})
.pipe(Effect.exit);
assert.strictEqual(stopped._tag, "Success");
yield* harness.takeEvent(
"provider_thread.updated",
(event) => (event.providerThread.pendingBackgroundTasks?.length ?? 0) === 0,
);

// The held read predates the stop, so its rows must not come back.
gate.resolve({
tasks: [
{ taskId: "monitor-1", kind: "tool.monitor", status: "inProgress", tool: "monitor" },
],
});
// The adapter handles this only after the held refresh has returned.
yield* fake.emitHostWide("usage/changed", SUBSCRIPTION_USAGE);
yield* Deferred.await(drained);
assert.isFalse(yield* harness.runtime.hasPendingBackgroundWork!);
}).pipe(Effect.scoped, Effect.provide(testLayer)),
);

it.effect("keeps a woken turn on a failed Stop so a retry can end it", () =>
Effect.gen(function* () {
const fake = yield* makeFakeMuse();
// `turn/started` sets the held wake before it offers the continuation.
const woken = yield* Deferred.make<void>();
const harness = yield* makeHarness(
fake,
INSTANCE_ID,
undefined,
undefined,
undefined,
runtimePolicy,
{ continuationRequests: { offer: () => Deferred.succeed(woken, undefined) } },
);
const turn = yield* startConversation(harness, fake);
yield* fake.emit("item/completed", {
turnId: turn.nativeId,
item: {
itemId: "monitor-1",
kind: "toolCall",
status: "completed",
turnId: turn.nativeId,
tool: "monitor",
background: true,
revision: 1,
},
});
yield* harness.takeEvent(
"provider_thread.updated",
(event) => (event.providerThread.pendingBackgroundTasks?.length ?? 0) > 0,
);
fake.queueResponse("task/list", {
tasks: [
{ taskId: "monitor-1", kind: "tool.monitor", status: "inProgress", tool: "monitor" },
],
});
yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "completed" });
yield* harness.takeEvent("turn.terminal");

// The monitor's report already woke a new Muse turn.
yield* fake.emit("turn/started", { turnId: "wake-1" });
yield* Deferred.await(woken);

fake.queueResponse("turn/interrupt", new Error("interrupt rejected"));
const failed = yield* harness.runtime
.interruptTurn({
providerThread: harness.providerThread,
providerTurnId: turn.providerTurn.id,
requestRuntimeRestart: true,
})
.pipe(Effect.exit);
assert.strictEqual(failed._tag, "Failure");
assert.strictEqual((yield* fake.takeCall("task/stopAll")).method, "task/stopAll");
assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!);

const retried = yield* harness.runtime
.interruptTurn({
providerThread: harness.providerThread,
providerTurnId: turn.providerTurn.id,
requestRuntimeRestart: true,
})
.pipe(Effect.exit);
assert.strictEqual(retried._tag, "Success");
assert.strictEqual((yield* fake.takeCall("turn/interrupt")).params.turnId, "wake-1");
assert.isFalse(yield* harness.runtime.hasPendingBackgroundWork!);
}).pipe(Effect.scoped, Effect.provide(testLayer)),
);

it.effect("clears a stopped monitor when the turn ends before Stop's reply", () =>
Effect.gen(function* () {
const fake = yield* makeFakeMuse();
const harness = yield* makeHarness(fake);
const turn = yield* startConversation(harness, fake);
yield* fake.emit("item/completed", {
turnId: turn.nativeId,
item: {
itemId: "monitor-1",
kind: "toolCall",
status: "completed",
turnId: turn.nativeId,
tool: "monitor",
background: true,
revision: 1,
},
});
yield* harness.takeEvent(
"provider_thread.updated",
(event) => (event.providerThread.pendingBackgroundTasks?.length ?? 0) > 0,
);

// Muse reports the turn as ended before it answers the interrupt.
const gate = pendingPromise<Record<string, unknown>>();
fake.queueResponse("turn/interrupt", gate.promise);
const stopping = yield* harness.runtime
.interruptTurn({
providerThread: harness.providerThread,
providerTurnId: turn.providerTurn.id,
requestRuntimeRestart: true,
})
.pipe(Effect.exit, Effect.forkScoped);
yield* fake.takeCall("turn/interrupt");
yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "cancelled" });
yield* harness.takeEvent("turn.terminal");
gate.resolve({});

const stopped = yield* Fiber.join(stopping);
assert.strictEqual(stopped._tag, "Success");
// The clear happens inside Stop, so it is settled as soon as the call returns.
assert.isFalse(yield* harness.runtime.hasPendingBackgroundWork!);
yield* harness.takeEvent(
"provider_thread.updated",
(event) => (event.providerThread.pendingBackgroundTasks?.length ?? 0) === 0,
);
}).pipe(Effect.scoped, Effect.provide(testLayer)),
);

it.effect("ends a compaction whose item names the held wake as its run", () =>
Effect.gen(function* () {
const fake = yield* makeFakeMuse();
Expand Down
Loading
Loading