diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index fe7559f603aaa..b786d6cd1265c 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -17950,6 +17950,365 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + type WakeHarness = Effect.Success>; + const messageTextWakes = (harness: WakeHarness) => + harness.continuationRequests.filter((request) => request.delivery === "message_text"); + // Starts a turn that leaves the background task running, awaited unless told otherwise. + const settleWithBackgroundTask = Effect.fnUntraced(function* ( + harness: WakeHarness, + options?: { readonly awaited?: boolean }, + ) { + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-owed-wake-1"), + text: "Run the build in the background and wait for it.", + attachments: [], + }), + ); + yield* harness.offerAndWait(wakeTaskStarted); + if (options?.awaited !== false) { + yield* harness.runtime.awaitBackgroundTasks!({ + providerThread: harness.providerThread, + taskIds: [WAKE_TASK_ID], + awaited: true, + }); + } + yield* harness.offerAndWait(turnOneResult); + yield* harness.awaitTerminal(1); + }); + + it.effect.each<{ + readonly scenario: string; + readonly awaited?: boolean; + readonly beforeClose: ( + harness: WakeHarness, + ) => Effect.Effect; + readonly deliveredBy?: "session close" | "process exit"; + }>([ + { + scenario: "the session closes before Claude opens its wake turn", + beforeClose: (harness) => harness.offerAndWait(wakeNotification), + deliveredBy: "session close", + }, + { + // Real CLIs send the empty roster first, which drops the live awaited mark. + scenario: "the empty roster level arrives before the notification", + beforeClose: (harness) => + harness + .offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "background_tasks_changed", + tasks: [], + uuid: "00000000-0000-4000-8000-000000000150", + session_id: WAKE_NATIVE_SESSION, + }), + ) + .pipe(Effect.andThen(harness.offerAndWait(wakeNotification))), + deliveredBy: "session close", + }, + { + scenario: "the Claude process exits before its wake turn", + beforeClose: (harness) => + Effect.gen(function* () { + yield* harness.offerAndWait(wakeNotification); + yield* Queue.shutdown(harness.sdkMessages); + yield* harness.awaitContinuation(1); + }), + deliveredBy: "process exit", + }, + { + scenario: "nothing is owed for a task the agent did not wait on", + awaited: false, + beforeClose: (harness) => harness.offerAndWait(wakeNotification), + }, + { + scenario: "nothing is owed once Claude's own wake turn ends", + beforeClose: (harness) => + Effect.forEach([wakeNotification, wakeTurnInit, wakeAssistant, wakeResult], (frame) => + harness.offerAndWait(frame), + ), + }, + { + scenario: "nothing is owed once a user turn absorbs the wake", + beforeClose: (harness) => + Effect.gen(function* () { + yield* harness.offerAndWait(wakeNotification); + yield* harness.offerAndWait(wakeTurnInit); + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-owed-wake-user"), + text: "Is the build done?", + attachments: [], + providerTurnOrdinal: 2, + }), + ); + yield* harness.offerAndWait( + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000151", result: "Done." }), + ); + yield* harness.awaitTerminal(2); + }), + }, + { + scenario: "nothing is owed after a Stop restarts the runtime", + beforeClose: (harness) => + Effect.gen(function* () { + yield* harness.offerAndWait(wakeNotification); + yield* harness.runtime.interruptTurn({ + providerThread: harness.providerThread, + providerTurnId: harness.terminalEvents()[0]!.providerTurnId, + requestRuntimeRestart: true, + }); + }), + }, + ])("an awaited task that finishes while idle reaches the agent once: $scenario", (row) => + Effect.gen(function* () { + const sessionScope = yield* Scope.make(); + const harness = yield* makeWakeHarnessWithOptions({ + close: (sdkMessages) => Queue.shutdown(sdkMessages), + }).pipe(Scope.provide(sessionScope)); + yield* settleWithBackgroundTask( + harness, + row.awaited === undefined ? undefined : { awaited: row.awaited }, + ); + + yield* row.beforeClose(harness); + assert.lengthOf(messageTextWakes(harness), row.deliveredBy === "process exit" ? 1 : 0); + yield* Scope.close(sessionScope, Exit.void); + + const delivered = messageTextWakes(harness); + assert.lengthOf(delivered, row.deliveredBy === undefined ? 0 : 1); + if (row.deliveredBy === undefined) return; + assert.equal(delivered[0]?.threadId, harness.threadId); + assert.equal(delivered[0]?.providerThreadId, harness.providerThread.id); + assert.include(delivered[0]?.detail, WAKE_TASK_ID); + assert.include(delivered[0]?.detail, WAKE_SUMMARY); + assert.include(delivered[0]?.detail, `/tmp/claude-replay/tasks/${WAKE_TASK_ID}.output`); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect( + "a wake continuation that starts after its session closed prompts with the result", + () => + Effect.gen(function* () { + const sessionScope = yield* Scope.make(); + const harness = yield* makeWakeHarnessWithOptions({ freshQueueOnReopen: true }).pipe( + Scope.provide(sessionScope), + ); + yield* settleWithBackgroundTask(harness); + yield* harness.offerAndWait(wakeNotification); + yield* harness.offerAndWait(wakeTurnInit); + assert.lengthOf(harness.continuationRequests, 1); + // The close kills the wake turn Claude opened; its pending continuation now carries the result. + yield* Scope.close(sessionScope, Exit.void); + assert.lengthOf(harness.continuationRequests, 1); + + const reopenedScope = yield* Scope.make(); + const reopened = yield* harness.adapter + .openSession({ + threadId: harness.threadId, + providerSessionId: ProviderSessionId.make("provider-session-claude-owed-wake-reopened"), + modelSelection: CLAUDE_TEST_MODEL_SELECTION, + runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY, + }) + .pipe(Scope.provide(reopenedScope)); + yield* reopened.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-owed-wake-continuation"), + text: WAKE_SUMMARY, + attachments: [], + providerTurnOrdinal: 2, + messageCreatedBy: "agent", + messageCreationSource: "provider", + }), + ); + assert.lengthOf(harness.offeredMessages, 2); + const prompt = JSON.stringify(harness.offeredMessages[1]?.message.content); + assert.include(prompt, WAKE_TASK_ID); + assert.include(prompt, `/tmp/claude-replay/tasks/${WAKE_TASK_ID}.output`); + + yield* Scope.close(reopenedScope, Exit.void); + assert.lengthOf(harness.continuationRequests, 1); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect("nothing is owed once the wake continuation's live result arrives", () => + Effect.gen(function* () { + const sessionScope = yield* Scope.make(); + const harness = yield* makeWakeHarness.pipe(Scope.provide(sessionScope)); + yield* settleWithBackgroundTask(harness); + yield* harness.offerAndWait(wakeNotification); + yield* harness.offerAndWait(wakeTurnInit); + assert.lengthOf(harness.continuationRequests, 1); + + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-owed-wake-live-result"), + text: WAKE_SUMMARY, + attachments: [], + providerTurnOrdinal: 2, + messageCreatedBy: "agent", + messageCreationSource: "provider", + }), + ); + yield* harness.offerAndWait(wakeAssistant); + yield* harness.offerAndWait(wakeResult); + yield* harness.awaitTerminal(2); + yield* Scope.close(sessionScope, Exit.void); + + assert.lengthOf(messageTextWakes(harness), 0); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect("prompts with the result once when Claude exits partway through its wake turn", () => + Effect.gen(function* () { + const sessionScope = yield* Scope.make(); + const harness = yield* makeWakeHarnessWithOptions({ freshQueueOnReopen: true }).pipe( + Scope.provide(sessionScope), + ); + yield* settleWithBackgroundTask(harness); + yield* harness.offerAndWait(wakeNotification); + yield* harness.offerAndWait(wakeTurnInit); + assert.lengthOf(harness.continuationRequests, 1); + yield* Queue.shutdown(harness.sdkMessages); + yield* yieldTicks(50); + + // The same session replays the opened wake turn, but its process is gone and no result follows. + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-owed-wake-exited"), + text: WAKE_SUMMARY, + attachments: [], + providerTurnOrdinal: 2, + messageCreatedBy: "agent", + messageCreationSource: "provider", + }), + ); + yield* harness.awaitTerminal(2); + const delivered = messageTextWakes(harness); + assert.lengthOf(delivered, 1); + + const deliveredTurn = makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-owed-wake-delivered"), + text: delivered[0]?.detail ?? "", + attachments: [], + providerTurnOrdinal: 3, + messageCreatedBy: "agent", + messageCreationSource: "server", + }); + yield* harness.runtime.startTurn({ + ...deliveredTurn, + message: { ...deliveredTurn.message, messageId: delivered[0]!.messageId! }, + }); + yield* Scope.close(sessionScope, Exit.void); + + const prompts = harness.offeredMessages.filter((message) => + JSON.stringify(message.message.content).includes(WAKE_TASK_ID), + ); + assert.lengthOf(prompts, 1); + assert.lengthOf(messageTextWakes(harness), 1); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect.each(["before", "after"] as const)( + "delivers the result once when the wake continuation is cancelled %s its session closes", + (cancelled) => + Effect.gen(function* () { + const sessionScope = yield* Scope.make(); + const harness = yield* makeWakeHarness.pipe(Scope.provide(sessionScope)); + yield* settleWithBackgroundTask(harness); + yield* harness.offerAndWait(wakeNotification); + yield* harness.offerAndWait(wakeTurnInit); + const buffered = harness.continuationRequests[0]; + if (buffered?.onRunEndedWithoutStarting === undefined) { + return yield* Effect.die("Missing the buffered wake continuation."); + } + + // Settling cancels the queued continuation and closes the session, in either order. + if (cancelled === "before") yield* buffered.onRunEndedWithoutStarting(); + assert.lengthOf(messageTextWakes(harness), 0); + yield* Scope.close(sessionScope, Exit.void); + if (cancelled === "after") { + assert.lengthOf(messageTextWakes(harness), 0); + yield* buffered.onRunEndedWithoutStarting(); + } + assert.lengthOf(messageTextWakes(harness), 1); + + // Each cancelled delivery is offered again under a new message id, up to the cap. + for (const attempt of [1, 2, 3]) { + const delivery = messageTextWakes(harness)[attempt - 1]; + assert.include(delivery?.detail, WAKE_TASK_ID); + yield* delivery!.onRunEndedWithoutStarting!(); + } + const deliveries = messageTextWakes(harness); + assert.lengthOf(deliveries, 3); + assert.lengthOf(new Set(deliveries.map((delivery) => delivery.messageId)), 3); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + const makeSubagentAssistantFrames = (input: { readonly parentToolUseId: string; readonly uuid: string; diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index c1f1eef7bc1ae..8ff3b3f3d5258 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -100,7 +100,7 @@ import { defaultInstanceIdForDriver, isCompactionCommandMessage, parseGoalCommand, - type MessageId, + MessageId, type ModelSelection, type OrchestrationV2ConversationMessage, type OrchestrationV2ExecutionNode, @@ -365,6 +365,8 @@ const CLAUDE_BOOT_TIMEOUT = "5 minutes"; // Claude can consume a subagent result before the launching turn ends, leaving // its notification with no native wake turn to answer the continuation. const CLAUDE_WAKE_TURN_START_DEADLINE = "2 minutes"; +// Deliveries of one owed wake that may end without starting before it is dropped. +const CLAUDE_OWED_WAKE_MAX_ATTEMPTS = 3; // Claude Code flushes transcript writes every 100 ms, so a workflow member's // reply can land after the progress frame that settles it. Its transcript // stays the source of its answer this long after it settles. @@ -4724,6 +4726,36 @@ function isClaudeEmptyTaskNotificationSuccess(message: SDKMessage): boolean { ); } +// An awaited background task that finished while idle, until a turn the agent ran answers it. +interface ClaudeOwedWake { + readonly threadId: ThreadId; + readonly providerThreadId: ProviderThreadId; + readonly nativeThreadId: string; + readonly taskId: string; + readonly detail: string; + readonly report: BackgroundWorkReport; + readonly attempt: number; + readonly offered: boolean; + // Claude opened a root turn after the notification, so that turn's result answers it. + readonly turnStarted: boolean; + // The buffered wake continuation that covers it; `closed` once its session closed first. + readonly bufferedRun: { readonly token: symbol; readonly closed: boolean } | null; +} + +// The CLI already wrote the notification into the transcript, so this points at the result +// instead of presenting it as news. +function claudeOwedWakeDetail( + notification: Extract, +): string { + const summary = notification.summary.trim(); + const header = `Background command ${notification.status} (task ${notification.task_id})${ + summary.length === 0 ? "" : `: ${summary}` + }`; + const output = + notification.output_file.length === 0 ? "" : `\n\nOutput file: ${notification.output_file}`; + return `${header}${output}\n\nThe background task you were waiting on has finished. Read its output and continue.`; +} + // The CLI throttles unchanged rate_limit_event frames, but repeats this // undeclared payload on each blocked assistant response. const isClaudeBlockedAssistantRateLimit = Schema.is( @@ -5652,6 +5684,91 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const continuationRequests = adapterOptions.continuationRequests ?? { offer: () => Effect.void, }; + // Outlives sessions: closing one kills Claude's own wake turn, so its close delivers what is owed. + const owedWakes = new Map(); + const owedWakeKey = (wake: Pick) => + `${wake.threadId}\u0000${wake.taskId}`; + const owedWakeMessageId = (wake: ClaudeOwedWake) => + MessageId.make(`message:claude-owed-wake:${wake.threadId}:${wake.taskId}:${wake.attempt}`); + const owedWakesMatching = (matches: (wake: ClaudeOwedWake) => boolean) => + owedWakes.size === 0 ? [] : [...owedWakes.values()].filter(matches); + // Compare-and-set: session closes and process exits run outside the SDK message lock. + const replaceOwedWake = (expected: ClaudeOwedWake, next: ClaudeOwedWake | null) => { + const key = owedWakeKey(expected); + if (owedWakes.get(key) !== expected) return false; + if (next === null) owedWakes.delete(key); + else owedWakes.set(key, next); + return true; + }; + const retireOwedWakes = (matches: (wake: ClaudeOwedWake) => boolean) => + Effect.sync(() => { + for (const wake of owedWakesMatching(matches)) replaceOwedWake(wake, null); + }); + const retireAnsweredOwedWakes = (nativeThreadId: string) => + retireOwedWakes((wake) => wake.nativeThreadId === nativeThreadId && wake.turnStarted); + const markOwedWakesTurnStarted = (matches: (wake: ClaudeOwedWake) => boolean) => { + for (const wake of owedWakesMatching((wake) => !wake.turnStarted && matches(wake))) { + replaceOwedWake(wake, { ...wake, turnStarted: true }); + } + }; + const deliverOwedWake = (wake: ClaudeOwedWake): Effect.Effect => + Effect.suspend(() => { + const offered = { ...wake, offered: true, bufferedRun: null }; + if (!replaceOwedWake(wake, offered)) return Effect.void; + const notification = backgroundWorkNotification([wake.report]); + return continuationRequests.offer({ + threadId: wake.threadId, + providerThreadId: wake.providerThreadId, + driver: CLAUDE_PROVIDER, + delivery: "message_text", + messageId: owedWakeMessageId(offered), + detail: wake.detail, + notification, + onRunEndedWithoutStarting: () => redeliverOwedWake(offered), + }); + }); + const redeliverOwedWake = (offered: ClaudeOwedWake): Effect.Effect => + Effect.suspend(() => { + const current = owedWakes.get(owedWakeKey(offered)); + if (current === undefined || !current.offered || current.attempt !== offered.attempt) { + return Effect.void; + } + if (current.attempt >= CLAUDE_OWED_WAKE_MAX_ATTEMPTS) { + replaceOwedWake(current, null); + return Effect.logWarning("orchestration-v2.claude-owed-wake-dropped", { + threadId: current.threadId, + providerThreadId: current.providerThreadId, + taskId: current.taskId, + attempts: current.attempt, + }); + } + const next = { ...current, attempt: current.attempt + 1, offered: false }; + return replaceOwedWake(current, next) ? deliverOwedWake(next) : Effect.void; + }); + const deliverOwedWakesOnClose = (matches: (wake: ClaudeOwedWake) => boolean) => + Effect.suspend(() => + Effect.forEach( + owedWakesMatching((wake) => !wake.offered && matches(wake)), + (wake) => { + if (wake.bufferedRun === null) return deliverOwedWake(wake); + replaceOwedWake(wake, { ...wake, bufferedRun: { ...wake.bufferedRun, closed: true } }); + return Effect.void; + }, + { discard: true }, + ), + ); + const releaseOwedWakesFromBufferedRun = (token: symbol) => + Effect.suspend(() => + Effect.forEach( + owedWakesMatching((wake) => wake.bufferedRun?.token === token), + (wake) => { + const released = { ...wake, bufferedRun: null }; + if (!replaceOwedWake(wake, released)) return Effect.void; + return wake.bufferedRun?.closed === true ? deliverOwedWake(released) : Effect.void; + }, + { discard: true }, + ), + ); const homePath = yield* resolveClaudeHomePath( adapterOptions.settings, adapterOptions.environment, @@ -6939,6 +7056,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( /* Provider events include streaming deltas; avoid a span for each update. */ Effect.fnUntraced(function* (nativeThreadId: string) { yield* clearWakeReports(nativeThreadId); + yield* retireOwedWakes((wake) => wake.nativeThreadId === nativeThreadId); yield* Ref.update(wakeBuffers, (current) => { if (!current.has(nativeThreadId)) { return current; @@ -6969,6 +7087,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( // Claude runs one root turn at a time, so a user turn's result also ends a wake turn it // folded in. The continuation replays that wake's buffered frames but gets no result of its own. const markAbsorbedWake = Effect.fnUntraced(function* (nativeThreadId: string) { + yield* retireOwedWakes((wake) => wake.nativeThreadId === nativeThreadId); const latch = (yield* Ref.get(requestedContinuations)).get(nativeThreadId); if (latch !== undefined && latch.retired !== null) { yield* releaseContinuationLatch(nativeThreadId); @@ -9823,6 +9942,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( cause?: Cause.Cause, ) { const current = yield* Ref.get(activeTurn); + if (!closedQuery.stopping) { + yield* deliverOwedWakesOnClose( + (wake) => wake.nativeThreadId === closedQuery.nativeThreadId, + ); + } // A replacement query can reuse the native thread id; only this query's turn is ours. const context = closedQuery.lastTurn; if (context === null) return; @@ -9920,6 +10044,41 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }); }); + // The empty roster level can arrive first and prune the live mark; the published roster keeps it. + const recordOwedWake = Effect.fnUntraced(function* ( + nativeThreadId: string, + notification: Extract, + report: BackgroundWorkReport, + ) { + const route = (yield* Ref.get(lastTurnRouteByNativeThread)).get(nativeThreadId); + if (route === undefined) return; + const awaited = + taskIdSetForNativeThread( + yield* Ref.get(awaitedBackgroundTasksByNativeThread), + nativeThreadId, + ).has(notification.task_id) || + ((yield* Ref.get(lastProviderThreadByNativeThread)) + .get(nativeThreadId) + ?.pendingBackgroundTasks?.some( + (task) => task.taskId === notification.task_id && task.awaited === true, + ) ?? + false); + if (!awaited) return; + const wake: ClaudeOwedWake = { + threadId: route.threadId, + providerThreadId: route.providerThreadId, + nativeThreadId, + taskId: notification.task_id, + detail: claudeOwedWakeDetail(notification), + report, + attempt: 1, + offered: false, + turnStarted: false, + bufferedRun: null, + }; + owedWakes.set(owedWakeKey(wake), wake); + }); + const bufferWakeMessage = Effect.fnUntraced(function* (wakeInput: { readonly nativeThreadId: string; readonly message: SDKMessage; @@ -10015,6 +10174,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( if (!isWakeEvidence) { return; } + if (isClaudeRootTurnFrame(message)) { + markOwedWakesTurnStarted((wake) => wake.nativeThreadId === wakeInput.nativeThreadId); + } else if (message.type === "result" && !isClaudeEmptyTaskNotificationSuccess(message)) { + yield* retireAnsweredOwedWakes(wakeInput.nativeThreadId); + } const retired = (yield* Ref.get(requestedContinuations)).get( wakeInput.nativeThreadId, )?.retired; @@ -10048,11 +10212,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ? message.summary : null; if (isPendingTaskNotification) { - yield* recordWakeReport( - wakeInput.nativeThreadId, - message.task_id, - yield* opaqueTaskWakeReport(wakeInput.nativeThreadId, message), - ); + const report = yield* opaqueTaskWakeReport(wakeInput.nativeThreadId, message); + yield* recordWakeReport(wakeInput.nativeThreadId, message.task_id, report); + yield* recordOwedWake(wakeInput.nativeThreadId, message, report); } else if (isPendingSubagentNotification) { const registered = (yield* Ref.get(sessionSubagentsByTaskId)).get(message.task_id); const started = bufferedMessages.find( @@ -10177,6 +10339,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( // A process reset can clear and re-latch this thread; a stale request must not retire the newer wake. const retire = (): Effect.Effect => sdkMessageLock.withPermits(1)(retireOfferedWake(nativeThreadId, token)); + for (const wake of owedWakesMatching( + (wake) => wake.nativeThreadId === nativeThreadId && !wake.offered, + )) { + replaceOwedWake(wake, { ...wake, bufferedRun: { token, closed: false } }); + } yield* continuationRequests.offer({ threadId: route.threadId, providerThreadId: route.providerThreadId, @@ -10184,7 +10351,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( detail: buffered?.detail ?? null, ...(notification === null ? {} : { notification }), clearIfCurrent: retire, - onRunEndedWithoutStarting: retire, + onRunEndedWithoutStarting: () => + retire().pipe(Effect.andThen(releaseOwedWakesFromBufferedRun(token))), }); }); @@ -12002,6 +12170,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( } if (!isClaudeProviderContinuationTurn(context.input)) { yield* markAbsorbedWake(liveQuery.nativeThreadId); + } else if (!isClaudeEmptyTaskNotificationSuccess(message)) { + yield* retireAnsweredOwedWakes(liveQuery.nativeThreadId); } const usageLimited = context.authenticationFailureMessage === undefined && @@ -13247,11 +13417,56 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( return context; }); + // A buffered wake continuation that starts after its session closed has no output to replay, + // so it prompts with the results still owed. A delivered owed wake is its own prompt. + // One whose process exited mid-turn replays what it buffered; nothing live will finish it. + const withOwedWakePrompt = Effect.fnUntraced(function* ( + turnInput: ProviderAdapter.ProviderAdapterV2TurnInput, + nativeThreadId: string, + ) { + if (!isClaudeProviderContinuationTurn(turnInput)) { + return { + turnInput, + owedWakesInPrompt: owedWakesMatching( + (wake) => wake.offered && owedWakeMessageId(wake) === turnInput.message.messageId, + ), + owedWakesOfExitedTurn: new Set(), + }; + } + const covered = owedWakesMatching( + (wake) => wake.nativeThreadId === nativeThreadId && wake.bufferedRun !== null, + ); + for (const wake of covered) replaceOwedWake(wake, { ...wake, bufferedRun: null }); + if (covered.length === 0 || (yield* Ref.get(wakeBuffers)).has(nativeThreadId)) { + return { + turnInput, + owedWakesInPrompt: [], + owedWakesOfExitedTurn: new Set( + covered.filter((wake) => wake.bufferedRun?.closed === true).map(owedWakeKey), + ), + }; + } + return { + turnInput: { + ...turnInput, + message: { + ...turnInput.message, + text: covered.map((wake) => wake.detail).join("\n\n"), + creationSource: "server" as const, + }, + }, + owedWakesInPrompt: covered, + owedWakesOfExitedTurn: new Set(), + }; + }); + const startTurn = Effect.fn("ClaudeAdapterV2.startTurn")( - function* (turnInput: ProviderAdapter.ProviderAdapterV2TurnInput) { + function* (requestedTurnInput: ProviderAdapter.ProviderAdapterV2TurnInput) { modelCatalog = yield* loadModelCatalog; const startedAt = yield* DateTime.now; - const nativeThreadId = yield* getNativeThreadId(turnInput.providerThread); + const nativeThreadId = yield* getNativeThreadId(requestedTurnInput.providerThread); + const { turnInput, owedWakesInPrompt, owedWakesOfExitedTurn } = + yield* withOwedWakePrompt(requestedTurnInput, nativeThreadId); const nativeTurnId = `turn:${turnInput.attemptId}`; // Use fresh uuids: native sessions can outlive this database, and Claude skips previously recorded prompts. const promptUuid = isClaudeProviderContinuationTurn(turnInput) @@ -13550,6 +13765,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ), ), ), + Effect.andThen( + retireOwedWakes((wake) => + owedWakesInPrompt.some((owed) => owedWakeKey(owed) === owedWakeKey(wake)), + ), + ), ); if (userMessage !== null && !turnInput.resume) { yield* attachTurn; @@ -13589,6 +13809,17 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( yield* finalizeActiveTurn({ context, status: "completed", completedAt }); return; } + const drainedTaskIds = new Set( + drained.flatMap((entry) => + entry.type === "system" && entry.subtype === "task_notification" + ? [entry.task_id] + : [], + ), + ); + markOwedWakesTurnStarted( + (wake) => + wake.nativeThreadId === nativeThreadId && drainedTaskIds.has(wake.taskId), + ); // Replay results last to avoid rebuffering later frames. An empty turn's result must not settle a newer running turn. const lastResultIndex = drained.findLastIndex((entry) => entry.type === "result"); const tailResult = drained[lastResultIndex]; @@ -13647,6 +13878,16 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( if (leavesLaterWake) yield* offerBufferedWake(nativeThreadId); return; } + const unanswered = owedWakesMatching( + (wake) => owedWakesOfExitedTurn.has(owedWakeKey(wake)) && !wake.offered, + ); + if (unanswered.length > 0) { + const completedAt = yield* DateTime.now; + yield* finalizeActiveTurn({ context, status: "completed", completedAt }); + yield* Effect.forEach(unanswered, deliverOwedWake, { discard: true }); + if (leavesLaterWake) yield* offerBufferedWake(nativeThreadId); + return; + } if (wakeAbsorbed || (hasOpaqueTaskNotification && !context.nativeWakeStarted)) { const completedAt = yield* DateTime.now; yield* finalizeActiveTurn({ context, status: "completed", completedAt }); @@ -14028,6 +14269,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( yield* Deferred.succeed(existing.closed, undefined); yield* existing.query.close.pipe(Effect.ignore); } + const sessionNativeThreads = yield* Ref.get(lastTurnRouteByNativeThread); + yield* deliverOwedWakesOnClose((wake) => sessionNativeThreads.has(wake.nativeThreadId)); yield* Effect.yieldNow; yield* queryRunner.assertComplete.pipe( Effect.catchCause((cause) => diff --git a/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts b/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts index 93177fffbdde8..15592147aea9c 100644 --- a/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts +++ b/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts @@ -22,11 +22,17 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as Queue from "effect/Queue"; import * as Schema from "effect/Schema"; +import type * as Scope from "effect/Scope"; import * as Stream from "effect/Stream"; +import * as GitManager from "../git/GitManager.ts"; +import * as ProjectSetupScriptRunner from "../project/ProjectSetupScriptRunner.ts"; +import * as PullRequestService from "../pullRequest/PullRequestService.ts"; import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts"; +import * as TerminalManager from "../terminal/Manager.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; import * as SecretRequests from "../secrets/SecretRequests.ts"; import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts"; @@ -41,9 +47,13 @@ import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderS import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts"; import * as Orchestrator from "./Orchestrator.ts"; +import * as ProjectionStore from "./ProjectionStore.ts"; +import * as ProjectStore from "./ProjectStore.ts"; import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; +import * as ProviderSessionManager from "./ProviderSessionManager.ts"; import * as ThreadLaunchService from "./ThreadLaunchService.ts"; import * as ThreadManagementService from "./ThreadManagementService.ts"; +import * as ThreadSettlementService from "./ThreadSettlementService.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts"; import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace"; @@ -795,10 +805,33 @@ const wakeAssistant = (uuid: string) => }, }); -it.effect("offers a continuation for a later wake after a queued continuation is cancelled", () => +// The real orchestrator, effect worker and continuation service around a Claude adapter whose +// SDK frames the test scripts, on one thread. +const withClaudeOrchestrator = ( + name: string, + test: (harness: { + readonly threadId: ThreadId; + readonly orchestrator: Orchestrator.OrchestratorV2["Service"]; + readonly worker: EffectWorker.OrchestrationEffectWorkerV2["Service"]; + readonly projections: ProjectionStore.ProjectionStoreV2Shape; + readonly continuations: ReadonlyArray; + readonly prompts: ReadonlyArray; + readonly offerAndWait: (message: SDKMessage) => Effect.Effect; + readonly watch: ( + predicate: (event: OrchestrationV2DomainEvent) => boolean, + ) => Effect.Effect, never, Scope.Scope>; + readonly userTurnWithBackgroundTask: ( + id: string, + taskId: string, + options?: { readonly awaited?: boolean }, + ) => Effect.Effect; + }) => Effect.Effect, +) => Effect.gen(function* () { - const cwd = yield* checkpointWorkspace("claude-cancelled-continuation"); - const sdkMessages = yield* Queue.unbounded(); + const cwd = yield* checkpointWorkspace(name); + // Each query reads its own frames; a closed one must not take the next query's. + let sdkMessages = yield* Queue.unbounded(); + let queryClosed = false; const processed = new WeakMap>(); const offerAndWait = Effect.fnUntraced(function* (message: SDKMessage) { const done = yield* Deferred.make(); @@ -811,6 +844,8 @@ it.effect("offers a continuation for a later wake after a queued continuation is const random = yield* SecureRandom.SecureRandom; const idAllocator = yield* IdAllocator.IdAllocatorV2; const continuations: ProviderContinuationRequests.ProviderContinuationRequest[] = []; + const prompts: SDKUserMessage[] = []; + let projections: ProjectionStore.ProjectionStoreV2Shape | undefined; const services = yield* Effect.context< Effect.Services> @@ -836,37 +871,46 @@ it.effect("offers a continuation for a later wake after a queued continuation is queryRunner: { allocateSessionId: Effect.succeed(sessionId), open: () => - Effect.succeed({ - // Resolve each frame's Deferred once the adapter pulls the next one. - messages: Stream.fromQueue(sdkMessages).pipe( - Stream.flatMap((message) => - Stream.make(message).pipe( - Stream.concat( - Stream.fromEffect( - Effect.suspend(() => { - const done = processed.get(message); - return done === undefined - ? Effect.void - : Deferred.succeed(done, undefined); - }), - ).pipe(Stream.drain), + Effect.gen(function* () { + if (queryClosed) { + sdkMessages = yield* Queue.unbounded(); + queryClosed = false; + } + const frames = sdkMessages; + return { + // Resolve each frame's Deferred once the adapter pulls the next one. + messages: Stream.fromQueue(frames).pipe( + Stream.flatMap((message) => + Stream.make(message).pipe( + Stream.concat( + Stream.fromEffect( + Effect.suspend(() => { + const done = processed.get(message); + return done === undefined + ? Effect.void + : Deferred.succeed(done, undefined); + }), + ).pipe(Stream.drain), + ), ), ), ), - ), - offer: () => Effect.void, - setModel: () => Effect.void, - applyFlagSettings: () => Effect.void, - setPermissionMode: () => Effect.void, - renameSession: () => Effect.void, - backgroundTasks: () => Effect.succeed(false), - mcpServerStatus: Effect.succeed([]), - readMcpResource: () => Effect.die("unexpected MCP resource read"), - callMcpTool: () => Effect.die("unexpected MCP tool call"), - listPermissionRules: Effect.die("unexpected permission rules read"), - stopTask: () => Effect.void, - interrupt: Effect.void, - close: Effect.void, + offer: (message) => Effect.sync(() => prompts.push(message)), + setModel: () => Effect.void, + applyFlagSettings: () => Effect.void, + setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, + backgroundTasks: () => Effect.succeed(false), + mcpServerStatus: Effect.succeed([]), + readMcpResource: () => Effect.die("unexpected MCP resource read"), + callMcpTool: () => Effect.die("unexpected MCP tool call"), + listPermissionRules: Effect.die("unexpected permission rules read"), + stopTask: () => Effect.void, + interrupt: Effect.void, + close: Effect.sync(() => { + queryClosed = true; + }).pipe(Effect.andThen(Queue.shutdown(frames))), + }; }), forkSession: () => Effect.die("unused"), subagentLaunchToolUseId: () => Effect.succeed(null), @@ -880,12 +924,14 @@ it.effect("offers a continuation for a later wake after a queued continuation is yield* Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; - const threadId = ThreadId.make("thread:cancelled-continuation"); + const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2; + const threadId = ThreadId.make(`thread:${name}`); const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) => orchestrator.streamDomainEvents.pipe( Stream.filter(predicate), Stream.take(1), Stream.runDrain, + Effect.orDie, Effect.forkScoped, ); const runUpdated = (messageId: string, status: string) => @@ -895,8 +941,27 @@ it.effect("offers a continuation for a later wake after a queued continuation is event.payload.userMessageId === messageId && event.payload.status === status, ); + // What the agent's t3_background_wait call does. + const awaitBackgroundTask = Effect.fnUntraced(function* (taskId: string) { + const records = yield* orchestrator.getThreadRecords(threadId, ["providerThreads"]); + const providerThread = records.providerThreads.find( + (candidate) => candidate.id === records.thread.activeProviderThreadId, + ); + assert.ok(providerThread?.providerSessionId != null); + const runtime = yield* sessions.get(providerThread.providerSessionId); + assert.ok(Option.isSome(runtime) && runtime.value.awaitBackgroundTasks !== undefined); + yield* runtime.value.awaitBackgroundTasks({ + providerThread, + taskIds: [taskId], + awaited: true, + }); + }); // A user turn that leaves a background task running when it ends. - const userTurnWithBackgroundTask = Effect.fnUntraced(function* (id: string, taskId: string) { + const userTurnWithBackgroundTask = Effect.fnUntraced(function* ( + id: string, + taskId: string, + options?: { readonly awaited?: boolean }, + ) { const running = yield* runUpdated(id, "running"); yield* orchestrator.dispatch({ type: "message.dispatch", @@ -912,7 +977,12 @@ it.effect("offers a continuation for a later wake after a queued continuation is yield* worker.drain(); yield* Fiber.join(running); const waiting = yield* runUpdated(id, "waiting"); - yield* Queue.offer(sdkMessages, backgroundTaskStarted(taskId)); + if (options?.awaited === true) { + yield* offerAndWait(backgroundTaskStarted(taskId)); + yield* awaitBackgroundTask(taskId); + } else { + yield* Queue.offer(sdkMessages, backgroundTaskStarted(taskId)); + } yield* Queue.offer(sdkMessages, result(`result-${id}`, false)); yield* Fiber.join(waiting); }); @@ -920,8 +990,8 @@ it.effect("offers a continuation for a later wake after a queued continuation is type: "thread.create", commandId: CommandId.make("create"), threadId, - projectId: ProjectId.make("project:cancelled-continuation"), - title: "Cancelled continuation", + projectId: ProjectId.make(`project:${name}`), + title: name, modelSelection, runtimeMode: "full-access", interactionMode: "default", @@ -930,94 +1000,29 @@ it.effect("offers a continuation for a later wake after a queued continuation is createdBy: "user", creationSource: "web", }); - - // The first run is still finalizing when its background task wakes Claude. - yield* userTurnWithBackgroundTask("first", "build-1"); - const queued = yield* watch( - (event) => event.type === "run.created" && event.payload.status === "queued", - ); - yield* offerAndWait(backgroundTaskNotification("build-1")); - yield* offerAndWait(wakeAssistant("wake-1")); - yield* Fiber.join(queued); - assert.lengthOf(continuations, 1); - const continuationRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( - (run) => run.status === "queued", - ); - assert.ok(continuationRun !== undefined); - yield* orchestrator.dispatch({ - type: "queued-run.cancel", - commandId: CommandId.make("cancel"), + assert.ok(projections !== undefined); + yield* test({ threadId, - runId: continuationRun.id, - }); - yield* offerAndWait(result("wake-1-result", false)); - yield* worker.drain(); - yield* orchestrator.resumeQueuedRuns; - yield* worker.drain(); - const idle = yield* orchestrator.getThreadProjection(threadId); - assert.equal(idle.runs.find((run) => run.id === continuationRun.id)?.status, "cancelled"); - assert.isFalse(idle.runs.some((run) => run.status === "queued")); - - yield* userTurnWithBackgroundTask("second", "build-2"); - yield* worker.drain(); - // The second user run has completed, so this continuation starts without queueing. - const isContinuationRun = (event: OrchestrationV2DomainEvent, type: string, status: string) => - (event.type === "run.created" || event.type === "run.updated") && - event.type === type && - event.payload.status === status && - event.payload.userMessageId !== MessageId.make("first") && - event.payload.userMessageId !== MessageId.make("second"); - const secondCreated = yield* watch((event) => - isContinuationRun(event, "run.created", "starting"), - ); - const secondRunning = yield* watch((event) => - isContinuationRun(event, "run.updated", "running"), - ); - yield* offerAndWait(backgroundTaskNotification("build-2")); - yield* offerAndWait(wakeAssistant("wake-2")); - assert.lengthOf( + orchestrator, + worker, + projections, continuations, - 2, - "the wake after a cancelled queued continuation must offer a new continuation", - ); - yield* Fiber.join(secondCreated); - yield* worker.drain(); - yield* Fiber.join(secondRunning); - const secondContinuation = (yield* orchestrator.getThreadProjection(threadId)).runs.find( - (run) => run.status === "running", - ); - assert.ok(secondContinuation !== undefined); - const secondWaiting = yield* watch( - (event) => - event.type === "run.updated" && - event.payload.id === secondContinuation.id && - event.payload.status === "waiting", - ); - yield* offerAndWait(result("wake-2-result", false)); - yield* Fiber.join(secondWaiting); - yield* worker.drain(); - const settled = yield* orchestrator.getThreadProjection(threadId); - assert.equal( - settled.runs.find((run) => run.id === secondContinuation.id)?.status, - "completed", - ); - assert.deepEqual( - settled.turnItems.flatMap((item) => - item.runId === secondContinuation.id && item.type === "assistant_message" - ? [item.text] - : [], - ), - ["Background build finished (wake-2)."], - "the continuation must carry only its own wake's output", - ); - assert.lengthOf(continuations, 2, "two wakes must offer exactly two continuations"); + prompts, + offerAndWait, + watch, + userTurnWithBackgroundTask: (id, taskId, options) => + userTurnWithBackgroundTask(id, taskId, options).pipe(Effect.orDie), + }); }).pipe( Effect.provide( - ProviderReplayHarness.layerWithRegistry( - { name: "claude-cancelled-continuation" }, - registry, - { runEffectWorker: false, runContinuationWorker: true }, - ), + ProviderReplayHarness.layerWithRegistry({ name }, registry, { + runEffectWorker: false, + runContinuationWorker: true, + wrapProjectionStore: (store) => { + projections = store; + return store; + }, + }), ), ); }).pipe( @@ -1029,6 +1034,297 @@ it.effect("offers a continuation for a later wake after a queued continuation is NodeCrypto.layer, ), ), + ); + +it.effect("offers a continuation for a later wake after a queued continuation is cancelled", () => + withClaudeOrchestrator( + "claude-cancelled-continuation", + ({ + threadId, + orchestrator, + worker, + continuations, + offerAndWait, + watch, + userTurnWithBackgroundTask, + }) => + Effect.gen(function* () { + // The first run is still finalizing when its background task wakes Claude. + yield* userTurnWithBackgroundTask("first", "build-1"); + const queued = yield* watch( + (event) => event.type === "run.created" && event.payload.status === "queued", + ); + yield* offerAndWait(backgroundTaskNotification("build-1")); + yield* offerAndWait(wakeAssistant("wake-1")); + yield* Fiber.join(queued); + assert.lengthOf(continuations, 1); + const continuationRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( + (run) => run.status === "queued", + ); + assert.ok(continuationRun !== undefined); + yield* orchestrator.dispatch({ + type: "queued-run.cancel", + commandId: CommandId.make("cancel"), + threadId, + runId: continuationRun.id, + }); + yield* offerAndWait(result("wake-1-result", false)); + yield* worker.drain(); + yield* orchestrator.resumeQueuedRuns; + yield* worker.drain(); + const idle = yield* orchestrator.getThreadProjection(threadId); + assert.equal(idle.runs.find((run) => run.id === continuationRun.id)?.status, "cancelled"); + assert.isFalse(idle.runs.some((run) => run.status === "queued")); + + yield* userTurnWithBackgroundTask("second", "build-2"); + yield* worker.drain(); + // The second user run has completed, so this continuation starts without queueing. + const isContinuationRun = ( + event: OrchestrationV2DomainEvent, + type: string, + status: string, + ) => + (event.type === "run.created" || event.type === "run.updated") && + event.type === type && + event.payload.status === status && + event.payload.userMessageId !== MessageId.make("first") && + event.payload.userMessageId !== MessageId.make("second"); + const secondCreated = yield* watch((event) => + isContinuationRun(event, "run.created", "starting"), + ); + const secondRunning = yield* watch((event) => + isContinuationRun(event, "run.updated", "running"), + ); + yield* offerAndWait(backgroundTaskNotification("build-2")); + yield* offerAndWait(wakeAssistant("wake-2")); + assert.lengthOf( + continuations, + 2, + "the wake after a cancelled queued continuation must offer a new continuation", + ); + yield* Fiber.join(secondCreated); + yield* worker.drain(); + yield* Fiber.join(secondRunning); + const secondContinuation = (yield* orchestrator.getThreadProjection(threadId)).runs.find( + (run) => run.status === "running", + ); + assert.ok(secondContinuation !== undefined); + const secondWaiting = yield* watch( + (event) => + event.type === "run.updated" && + event.payload.id === secondContinuation.id && + event.payload.status === "waiting", + ); + yield* offerAndWait(result("wake-2-result", false)); + yield* Fiber.join(secondWaiting); + yield* worker.drain(); + const settled = yield* orchestrator.getThreadProjection(threadId); + assert.equal( + settled.runs.find((run) => run.id === secondContinuation.id)?.status, + "completed", + ); + assert.deepEqual( + settled.turnItems.flatMap((item) => + item.runId === secondContinuation.id && item.type === "assistant_message" + ? [item.text] + : [], + ), + ["Background build finished (wake-2)."], + "the continuation must carry only its own wake's output", + ); + assert.lengthOf(continuations, 2, "two wakes must offer exactly two continuations"); + }), + ), +); + +const emptyBackgroundTasks = frame({ + type: "system", + subtype: "background_tasks_changed", + tasks: [], + uuid: "background-tasks-empty", + session_id: sessionId, +}); +const mergedPullRequest = { host: "github.com", repository: "acme/app", number: 7 } as const; + +// Settles on merge like the server does; the git and pull request reads stay unused because the +// thread's link already carries the merged snapshot. +const makeSettlement = ( + orchestrator: Orchestrator.OrchestratorV2["Service"], + projections: ProjectionStore.ProjectionStoreV2Shape, + candidateReads: Queue.Queue, +) => + ThreadSettlementService.make.pipe( + Effect.provide( + Layer.mergeAll( + Layer.succeed(Orchestrator.OrchestratorV2, orchestrator), + Layer.succeed(ProjectionStore.ProjectionStoreV2, { + ...projections, + getSettlementCandidates: (threadId) => + projections + .getSettlementCandidates(threadId) + .pipe(Effect.tap(() => Queue.offer(candidateReads, undefined))), + }), + Layer.mock(ProjectStore.ProjectStoreV2)({ listShells: () => Effect.succeed([]) }), + ServerSettings.layerTest({ sidebarAutoSettleOnMerge: true }), + Layer.mock(GitManager.GitManager)({}), + Layer.mock(PullRequestService.PullRequestService)({ + subscribeMerges: Effect.succeed(Stream.never), + subscribeRefreshes: Stream.never, + }), + Layer.mock(TerminalManager.TerminalManager)({ closeOnSettle: () => Effect.void }), + Layer.mock(ProjectSetupScriptRunner.ProjectSetupScriptRunner)({ + runForThread: () => Effect.succeed({ status: "no-script" } as const), + }), + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ); + +// The agent merged its pull request, started a deploy poll, waited on it and ended its turn. +// The poll finishes and the merge settles the thread, closing Claude's session, before Claude +// opens its own wake turn. +it.effect("runs an awaited task's result when a merge settles the thread before Claude wakes", () => + withClaudeOrchestrator( + "claude-owed-wake-settle", + ({ + threadId, + orchestrator, + worker, + projections, + continuations, + prompts, + offerAndWait, + watch, + userTurnWithBackgroundTask, + }) => + Effect.gen(function* () { + // Starting a settlement service sweeps every thread once; wait for that sweep to finish. + const sweep = Effect.scoped( + Effect.gen(function* () { + const candidateReads = yield* Queue.unbounded(); + const settlement = yield* makeSettlement(orchestrator, projections, candidateReads); + yield* settlement.start(); + yield* Queue.take(candidateReads); + yield* settlement.drain; + }), + ); + const runOf = (messageId: MessageId | undefined) => + orchestrator + .getThreadProjection(threadId) + .pipe(Effect.map(({ runs }) => runs.find((run) => run.userMessageId === messageId))); + const isOwedWakeMessage = (messageId: string) => + messageId.startsWith("message:claude-owed-wake:"); + const deliveredRunCreated = yield* watch( + (event) => event.type === "run.created" && isOwedWakeMessage(event.payload.userMessageId), + ); + const settledTwice = yield* orchestrator.streamDomainEvents.pipe( + Stream.filter((event) => event.type === "thread.settled"), + Stream.take(2), + Stream.runDrain, + Effect.forkScoped, + ); + + yield* userTurnWithBackgroundTask("deploy", "deploy-poll", { awaited: true }); + const now = DateTime.formatIso(yield* DateTime.now); + yield* orchestrator.dispatch({ + type: "thread.pull-request.link", + commandId: CommandId.make("link"), + threadId, + ...mergedPullRequest, + url: "https://github.com/acme/app/pull/7", + source: "agent", + }); + yield* orchestrator.dispatch({ + type: "thread.pull-request-link.sync", + commandId: CommandId.make("merged"), + threadId, + ...mergedPullRequest, + snapshot: { + state: "merged", + title: "Ship it", + headBranch: "feature", + baseBranch: "main", + isDraft: false, + updatedAt: now, + syncedAt: now, + mergedAt: now, + }, + stack: null, + }); + // The real CLI reports the empty roster before the notification. + yield* offerAndWait(emptyBackgroundTasks); + yield* offerAndWait(backgroundTaskNotification("deploy-poll")); + + yield* worker.drain(); + // The command a settlement sweep sends when its candidate read lands before the wake. + const { thread } = yield* orchestrator.getThreadProjection(threadId); + yield* orchestrator.dispatch({ + type: "thread.auto-settle", + commandId: CommandId.make("auto-settle"), + threadId, + snapshotAt: thread.updatedAt, + }); + assert.equal( + (yield* orchestrator.getThreadProjection(threadId)).thread.settledOverride, + "settled", + ); + // Settling detaches the session, which closes Claude's query. + yield* worker.drain(); + const delivered = continuations.filter((request) => request.delivery === "message_text"); + assert.lengthOf(delivered, 1, "the closed session must deliver the task result once"); + const deliveredMessageId = delivered[0]!.messageId; + assert.ok(deliveredMessageId !== undefined); + yield* Fiber.join(deliveredRunCreated); + + // A sweep after the delivered run is dispatched and before it starts leaves it alone. + const beforeSweep = (yield* runOf(deliveredMessageId))?.status; + yield* sweep; + assert.equal((yield* runOf(deliveredMessageId))?.status, beforeSweep); + assert.notEqual(beforeSweep, "cancelled"); + + const running = yield* watch( + (event) => + event.type === "run.updated" && + event.payload.userMessageId === deliveredMessageId && + event.payload.status === "running", + ); + yield* worker.drain(); + yield* Fiber.join(running); + assert.isNull( + (yield* orchestrator.getThreadProjection(threadId)).thread.settledOverride, + "the delivered run unsettles the thread", + ); + const prompt = JSON.stringify(prompts.at(-1)?.message.content); + assert.include(prompt, "deploy-poll"); + assert.include(prompt, "/tmp/claude-replay/tasks/deploy-poll.output"); + + const ended = yield* watch( + (event) => + event.type === "run.updated" && + event.payload.userMessageId === deliveredMessageId && + (event.payload.status === "waiting" || event.payload.status === "completed"), + ); + yield* offerAndWait(wakeAssistant("deploy-wake")); + yield* offerAndWait(result("deploy-wake-result", false)); + yield* Fiber.join(ended); + yield* worker.drain(); + assert.equal((yield* runOf(deliveredMessageId))?.status, "completed"); + // Once the delivered turn ends, the merge settles the thread again. + yield* sweep; + yield* Fiber.join(settledTwice); + const final = yield* orchestrator.getThreadProjection(threadId); + assert.equal(final.thread.settledOverride, "settled"); + assert.lengthOf( + final.runs.filter((run) => isOwedWakeMessage(run.userMessageId)), + 1, + "exactly one run delivers the task result", + ); + assert.lengthOf( + continuations.filter((request) => request.delivery === "message_text"), + 1, + ); + }), ), );