@@ -11498,160 +11498,190 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1149811498
1149911499 // Claude 2.1.323 wakes a session per Monitor event with only init, assistant and a
1150011500 // task-notification result; just the Monitor's end sends a task_notification frame.
11501- it.effect("tags a Claude Monitor event wake as a monitor update", () =>
11502- Effect.gen(function* () {
11503- const harness = yield* makeWakeHarness;
11504- const now = yield* DateTime.now;
11505- const taskId = "bi5kfx7xp";
11506- const toolUseId = "toolu_monitor_events";
11507-
11508- yield* harness.runtime.startTurn(
11509- makeClaudeTestTurnInput({
11510- threadId: harness.threadId,
11511- providerThread: harness.providerThread,
11512- now,
11513- attemptId: RunAttemptId.make("attempt-claude-monitor-event"),
11514- text: "Watch the mutation run and report each survivor.",
11515- attachments: [],
11516- }),
11517- );
11518- for (const frame of [
11519- claudeSdkFrame({
11520- type: "assistant",
11521- message: {
11522- model: "claude-sonnet-4-6",
11523- id: "msg_monitor_events",
11524- type: "message",
11525- role: "assistant",
11526- content: [
11527- {
11528- type: "tool_use",
11529- id: toolUseId,
11530- name: "Monitor",
11531- input: { description: "line monitor", command: "./mutate.sh" },
11532- },
11533- ],
11534- stop_reason: null,
11535- stop_sequence: null,
11536- usage: { input_tokens: 1, output_tokens: 1 },
11537- },
11538- parent_tool_use_id: null,
11539- uuid: "00000000-0000-4000-8000-000000000901",
11540- session_id: WAKE_NATIVE_SESSION,
11541- }),
11542- claudeSdkFrame({
11543- type: "system",
11544- subtype: "task_started",
11545- task_id: taskId,
11546- tool_use_id: toolUseId,
11547- description: "line monitor",
11548- is_backgrounded: true,
11549- task_type: "local_bash",
11550- uuid: "00000000-0000-4000-8000-000000000902",
11551- session_id: WAKE_NATIVE_SESSION,
11552- }),
11553- claudeSdkFrame({
11554- type: "user",
11555- message: {
11556- role: "user",
11557- content: [
11558- {
11559- type: "tool_result",
11560- tool_use_id: toolUseId,
11561- content: `Monitor started (task ${taskId}). You will be notified on each event.`,
11562- },
11563- ],
11564- },
11565- parent_tool_use_id: null,
11566- uuid: "00000000-0000-4000-8000-000000000903",
11567- session_id: WAKE_NATIVE_SESSION,
11568- tool_use_result: { taskId, persistent: false },
11569- }),
11570- makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000904", result: "STARTED" }),
11571- ]) {
11572- yield* Queue.offer(harness.sdkMessages, frame);
11573- }
11574- yield* harness.awaitTerminal(1);
11575-
11576- yield* harness.offerAndWait(wakeTurnInit);
11577- yield* harness.offerAndWait(
11578- makeAssistantTextFrame({
11579- uuid: "00000000-0000-4000-8000-000000000905",
11580- text: "Covered. Waiting for the rest.",
11581- }),
11582- );
11583- yield* harness.awaitContinuation(1);
11584-
11585- const notification = harness.continuationRequests[0]?.notification;
11586- assert.deepEqual(
11587- { source: notification?.source, outcome: notification?.outcome },
11588- { source: { kind: "monitor" }, outcome: "updated" },
11589- );
11501+ it.effect.each([
11502+ { laterWake: "ends with its own result" },
11503+ { laterWake: "is absorbed by a user turn" },
11504+ ] as const)(
11505+ "tags a Claude Monitor event wake as a monitor update (later wake $laterWake)",
11506+ ({ laterWake }) =>
11507+ Effect.gen(function* () {
11508+ const absorbed = laterWake === "is absorbed by a user turn";
11509+ const harness = yield* makeWakeHarness;
11510+ const now = yield* DateTime.now;
11511+ const taskId = "bi5kfx7xp";
11512+ const toolUseId = "toolu_monitor_events";
1159011513
11591- // A wake buffered behind the queued Monitor continuation must not run under its tag.
11592- for (const frame of [
11593- makeResultFrame({
11594- uuid: "00000000-0000-4000-8000-000000000906",
11595- result: "Covered. Waiting for the rest.",
11596- origin: { kind: "task-notification" },
11597- }),
11598- claudeSdkFrame({
11599- type: "system",
11600- subtype: "init",
11601- uuid: "00000000-0000-4000-8000-000000000907",
11602- session_id: WAKE_NATIVE_SESSION,
11603- }),
11604- makeAssistantTextFrame({
11605- uuid: "00000000-0000-4000-8000-000000000908",
11606- text: "Scheduled answer.",
11607- }),
11608- makeResultFrame({
11609- uuid: "00000000-0000-4000-8000-000000000909",
11610- result: "Scheduled answer.",
11611- }),
11612- ]) {
11613- yield* harness.offerAndWait(frame);
11614- }
11615- const assistantTexts = () =>
11616- harness.events.flatMap((event) =>
11617- event.type === "message.updated" && event.message.role === "assistant"
11618- ? [event.message.text]
11619- : [],
11620- );
11621- const startContinuation = (attempt: string, providerTurnOrdinal: number) =>
11622- harness.runtime.startTurn(
11514+ yield* harness.runtime.startTurn(
1162311515 makeClaudeTestTurnInput({
1162411516 threadId: harness.threadId,
1162511517 providerThread: harness.providerThread,
1162611518 now,
11627- attemptId: RunAttemptId.make(attempt),
11628- text: "Background task completed .",
11519+ attemptId: RunAttemptId.make(" attempt-claude-monitor-event" ),
11520+ text: "Watch the mutation run and report each survivor .",
1162911521 attachments: [],
11630- providerTurnOrdinal,
11631- messageCreatedBy: "agent",
11632- messageCreationSource: "provider",
1163311522 }),
1163411523 );
11635- yield* startContinuation("attempt-claude-monitor-event-2", 2);
11636- yield* harness.awaitTerminal(2);
11637- assert.notInclude(assistantTexts(), "Scheduled answer.");
11638- yield* harness.awaitContinuation(2);
11639- assert.isUndefined(harness.continuationRequests[1]?.notification);
11524+ for (const frame of [
11525+ claudeSdkFrame({
11526+ type: "assistant",
11527+ message: {
11528+ model: "claude-sonnet-4-6",
11529+ id: "msg_monitor_events",
11530+ type: "message",
11531+ role: "assistant",
11532+ content: [
11533+ {
11534+ type: "tool_use",
11535+ id: toolUseId,
11536+ name: "Monitor",
11537+ input: { description: "line monitor", command: "./mutate.sh" },
11538+ },
11539+ ],
11540+ stop_reason: null,
11541+ stop_sequence: null,
11542+ usage: { input_tokens: 1, output_tokens: 1 },
11543+ },
11544+ parent_tool_use_id: null,
11545+ uuid: "00000000-0000-4000-8000-000000000901",
11546+ session_id: WAKE_NATIVE_SESSION,
11547+ }),
11548+ claudeSdkFrame({
11549+ type: "system",
11550+ subtype: "task_started",
11551+ task_id: taskId,
11552+ tool_use_id: toolUseId,
11553+ description: "line monitor",
11554+ is_backgrounded: true,
11555+ task_type: "local_bash",
11556+ uuid: "00000000-0000-4000-8000-000000000902",
11557+ session_id: WAKE_NATIVE_SESSION,
11558+ }),
11559+ claudeSdkFrame({
11560+ type: "user",
11561+ message: {
11562+ role: "user",
11563+ content: [
11564+ {
11565+ type: "tool_result",
11566+ tool_use_id: toolUseId,
11567+ content: `Monitor started (task ${taskId}). You will be notified on each event.`,
11568+ },
11569+ ],
11570+ },
11571+ parent_tool_use_id: null,
11572+ uuid: "00000000-0000-4000-8000-000000000903",
11573+ session_id: WAKE_NATIVE_SESSION,
11574+ tool_use_result: { taskId, persistent: false },
11575+ }),
11576+ makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000904", result: "STARTED" }),
11577+ ]) {
11578+ yield* Queue.offer(harness.sdkMessages, frame);
11579+ }
11580+ yield* harness.awaitTerminal(1);
1164011581
11641- yield* startContinuation("attempt-claude-monitor-event-3", 3);
11642- yield* harness.awaitTerminal(3);
11643- assert.include(assistantTexts(), "Scheduled answer.");
11644- assert.lengthOf(harness.continuationRequests, 2);
11645- }).pipe(
11646- Effect.provide(
11647- Layer.mergeAll(
11648- IdAllocator.layer,
11649- McpProviderSessions.layer,
11650- NodeServices.layer,
11651- NodeCrypto.layer,
11582+ yield* harness.offerAndWait(wakeTurnInit);
11583+ yield* harness.offerAndWait(
11584+ makeAssistantTextFrame({
11585+ uuid: "00000000-0000-4000-8000-000000000905",
11586+ text: "Covered. Waiting for the rest.",
11587+ }),
11588+ );
11589+ yield* harness.awaitContinuation(1);
11590+
11591+ const notification = harness.continuationRequests[0]?.notification;
11592+ assert.deepEqual(
11593+ { source: notification?.source, outcome: notification?.outcome },
11594+ { source: { kind: "monitor" }, outcome: "updated" },
11595+ );
11596+
11597+ // A wake buffered behind the queued Monitor continuation must not run under its tag.
11598+ for (const frame of [
11599+ makeResultFrame({
11600+ uuid: "00000000-0000-4000-8000-000000000906",
11601+ result: "Covered. Waiting for the rest.",
11602+ origin: { kind: "task-notification" },
11603+ }),
11604+ claudeSdkFrame({
11605+ type: "system",
11606+ subtype: "init",
11607+ uuid: "00000000-0000-4000-8000-000000000907",
11608+ session_id: WAKE_NATIVE_SESSION,
11609+ }),
11610+ makeAssistantTextFrame({
11611+ uuid: "00000000-0000-4000-8000-000000000908",
11612+ text: "Scheduled answer.",
11613+ }),
11614+ ]) {
11615+ yield* harness.offerAndWait(frame);
11616+ }
11617+ const laterResult = makeResultFrame({
11618+ uuid: "00000000-0000-4000-8000-000000000909",
11619+ result: absorbed ? "No survivors yet." : "Scheduled answer.",
11620+ });
11621+ if (absorbed) {
11622+ yield* harness.runtime.startTurn(
11623+ makeClaudeTestTurnInput({
11624+ threadId: harness.threadId,
11625+ providerThread: harness.providerThread,
11626+ now,
11627+ attemptId: RunAttemptId.make("attempt-claude-monitor-event-user"),
11628+ text: "Any survivors yet?",
11629+ attachments: [],
11630+ providerTurnOrdinal: 2,
11631+ }),
11632+ );
11633+ yield* harness.until(() => harness.offeredMessages.length === 2);
11634+ yield* Queue.offer(harness.sdkMessages, laterResult);
11635+ yield* harness.awaitTerminal(2);
11636+ } else {
11637+ yield* harness.offerAndWait(laterResult);
11638+ }
11639+ const settled = absorbed ? 2 : 1;
11640+ const assistantTexts = () =>
11641+ harness.events.flatMap((event) =>
11642+ event.type === "message.updated" && event.message.role === "assistant"
11643+ ? [event.message.text]
11644+ : [],
11645+ );
11646+ const startContinuation = (attempt: string, providerTurnOrdinal: number) =>
11647+ harness.runtime.startTurn(
11648+ makeClaudeTestTurnInput({
11649+ threadId: harness.threadId,
11650+ providerThread: harness.providerThread,
11651+ now,
11652+ attemptId: RunAttemptId.make(attempt),
11653+ text: "Background task completed.",
11654+ attachments: [],
11655+ providerTurnOrdinal,
11656+ messageCreatedBy: "agent",
11657+ messageCreationSource: "provider",
11658+ }),
11659+ );
11660+ yield* startContinuation("attempt-claude-monitor-event-2", settled + 1);
11661+ yield* harness.awaitTerminal(settled + 1);
11662+ assert.notInclude(assistantTexts(), "Scheduled answer.");
11663+ yield* harness.awaitContinuation(2);
11664+ // With no result of its own in the buffer, the later wake reads as another Monitor event.
11665+ assert.equal(
11666+ harness.continuationRequests[1]?.notification?.source.kind,
11667+ absorbed ? "monitor" : undefined,
11668+ );
11669+
11670+ yield* startContinuation("attempt-claude-monitor-event-3", settled + 2);
11671+ yield* harness.awaitTerminal(settled + 2);
11672+ assert.equal(harness.terminalEvents()[settled + 1]?.status, "completed");
11673+ assert.include(assistantTexts(), "Scheduled answer.");
11674+ assert.lengthOf(harness.continuationRequests, 2);
11675+ }).pipe(
11676+ Effect.provide(
11677+ Layer.mergeAll(
11678+ IdAllocator.layer,
11679+ McpProviderSessions.layer,
11680+ NodeServices.layer,
11681+ NodeCrypto.layer,
11682+ ),
1165211683 ),
1165311684 ),
11654- ),
1165511685 );
1165611686
1165711687 const scheduleWakeupFrames = (input: {
0 commit comments