diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 1add2822a01e9..ca8a97bd00c8d 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -66,6 +66,7 @@ import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; +import * as Logger from "effect/Logger"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as PubSub from "effect/PubSub"; @@ -3506,7 +3507,6 @@ describe("ClaudeAdapterV2 background wake turns", () => { ); if (outcome === "release-before") { yield* Deferred.succeed(release, undefined); - yield* Fiber.join(pending); } yield* harness.offerAndWait( claudeSdkFrame({ @@ -3523,6 +3523,9 @@ describe("ClaudeAdapterV2 background wake turns", () => { } else if (outcome === "release-after") { yield* Deferred.succeed(release, undefined); yield* Fiber.join(pending); + } else if (outcome === "release-before") { + // Held until the compaction ended. + yield* Fiber.join(pending); } // Flush the budget update through the event stream before trying again. yield* harness.offerAndWait( @@ -3593,6 +3596,172 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + // Forks a steer and returns once the adapter holds it for the running compaction. + const holdSteerForCompaction = Effect.fnUntraced(function* ( + harness: Effect.Success, + input: { + readonly runId: ProviderAdapter.ProviderAdapterV2SteerInput["runId"]; + readonly providerTurnId: ProviderAdapter.ProviderAdapterV2SteerInput["providerTurnId"]; + readonly text: string; + }, + ) { + const held = yield* Deferred.make(); + const logger = Logger.make(({ message }) => { + if (String(message).includes("claude-steer-held-for-compaction")) { + Deferred.doneUnsafe(held, Effect.void); + } + }); + const steer = yield* harness.runtime + .steerTurn({ + threadId: harness.threadId, + runId: input.runId, + providerThread: harness.providerThread, + providerTurnId: input.providerTurnId, + message: { + createdBy: "user", + creationSource: "web", + messageId: MessageId.make(`compaction-${input.text}`), + text: input.text, + attachments: [], + }, + }) + .pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: true })), Effect.forkChild); + yield* Deferred.await(held); + return steer; + }); + + const startCompactingTurn = Effect.fnUntraced(function* ( + harness: Effect.Success, + attemptId: string, + ) { + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const input = makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make(attemptId), + text: "Keep going.", + attachments: [], + }); + yield* harness.runtime.startTurn(input); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "status", + status: "compacting", + uuid: "00000000-0000-4000-8000-000000000821", + session_id: WAKE_NATIVE_SESSION, + }), + ); + return { + runId: input.runId, + providerTurnId: idAllocator.derive.providerTurn({ + driver: ClaudeAdapterV2.CLAUDE_PROVIDER, + nativeTurnId: `turn:${input.attemptId}`, + }), + }; + }); + + // regression: #25796 a `now` steer offered mid-compaction aborts the compaction + it.effect("holds steers sent during compaction until the compaction ends", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + const turn = yield* startCompactingTurn(harness, "steer-during-compaction"); + const pending = []; + for (const text of ["First steer.", "Second steer."]) { + pending.push(yield* holdSteerForCompaction(harness, { ...turn, text })); + } + assert.lengthOf( + harness.offeredMessages, + 1, + "a steer sent while compacting reached Claude before the compaction ended", + ); + // Settled while held, so the thread's outbox is free for a Stop. + yield* Fiber.joinAll(pending); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "status", + status: null, + compact_result: "success", + uuid: "00000000-0000-4000-8000-000000000822", + session_id: WAKE_NATIVE_SESSION, + }), + ); + const steers = harness.offeredMessages.slice(1); + assert.deepEqual( + steers.map((message) => message.priority), + ["now", "now"], + ); + assert.match(encodeJsonString(steers[0]?.message.content), /First steer/); + assert.match(encodeJsonString(steers[1]?.message.content), /Second steer/); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect("sends a steer held for compaction before the turn is stopped", () => + Effect.gen(function* () { + let offeredAtInterrupt: number | undefined; + const harness = yield* makeWakeHarnessWithOptions({ + close: (sdkMessages) => Queue.shutdown(sdkMessages), + interrupt: Effect.sync(() => { + offeredAtInterrupt = harness.offeredMessages.length; + }), + }); + const turn = yield* startCompactingTurn(harness, "steer-then-stop-during-compaction"); + const steer = yield* holdSteerForCompaction(harness, { ...turn, text: "Then stop." }); + yield* harness.runtime.interruptTurn({ + providerThread: harness.providerThread, + providerTurnId: turn.providerTurnId, + }); + assert.equal(offeredAtInterrupt, 2); + yield* Fiber.join(steer); + const terminal = yield* Queue.take(harness.terminalReceipts); + assert.equal(terminal.status, "interrupted"); + assert.match(encodeJsonString(harness.offeredMessages[1]?.message.content), /Then stop/); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect("does not send a held steer once its turn has ended", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + const turn = yield* startCompactingTurn(harness, "turn-ends-during-compaction"); + const steer = yield* holdSteerForCompaction(harness, { ...turn, text: "Too late." }); + yield* harness.offerAndWait( + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000823", result: "Done." }), + ); + yield* Fiber.join(steer); + yield* Queue.take(harness.terminalReceipts); + assert.lengthOf(harness.offeredMessages, 1); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + const attachmentBudgetOutcomes = [ "resume", "legacy", diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 7b63b7efc0b65..2f92e72926996 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4402,6 +4402,14 @@ interface ActiveClaudeTurnContext { readonly heldRootFrames: Array; // Claude can run another model than requested, such as when an organization disallows it. executedModel: string | undefined; + // A "now" steer offered while compaction runs aborts it. Read and written under sdkMessageLock. + readonly heldSteers: Array; +} + +interface HeldClaudeSteer { + readonly live: ClaudeLiveQueryContext; + readonly message: SDKUserMessage; + readonly pdfReservation: Effect.Success>; } interface ActiveClaudeProviderRetry { @@ -7956,6 +7964,68 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( subagentToolCalls.clear(); }); + const offerSteer = Effect.fnUntraced(function* (input: { + readonly live: ClaudeLiveQueryContext; + readonly providerTurnId: OrchestrationV2ProviderTurn["id"]; + readonly message: SDKUserMessage; + readonly pdfReservation: HeldClaudeSteer["pdfReservation"]; + }) { + yield* Ref.update(steeredTurns, (current) => { + const next = new Set(current); + next.add(input.providerTurnId); + return next; + }); + yield* Effect.uninterruptibleMask((restore) => + restore( + input.live.query.offer(input.message, () => { + input.pdfReservation.commit(); + }), + ).pipe( + Effect.tap(() => + Effect.sync(() => { + input.pdfReservation.commit(); + }), + ), + ), + ); + }); + const steerNotSent = (providerTurnId: OrchestrationV2ProviderTurn["id"]) => + new ProviderAdapter.ProviderAdapterProtocolError({ + driver: CLAUDE_PROVIDER, + detail: `Claude provider turn ${providerTurnId} ended before the steer was sent.`, + }); + // Never offered once their turn ended: that would start a turn nobody asked for. + const dropHeldSteers = Effect.fnUntraced(function* (context: ActiveClaudeTurnContext) { + const dropped = context.heldSteers.splice(0).length; + if (dropped === 0) return; + yield* Effect.logWarning("orchestration-v2.claude-held-steers-dropped", { + providerTurnId: context.providerTurnId, + dropped, + }); + }); + // Callers hold sdkMessageLock, so a newer steer cannot overtake the held ones. + const releaseHeldSteers = Effect.fnUntraced(function* (context: ActiveClaudeTurnContext) { + for (;;) { + const steer = context.heldSteers[0]; + if (steer === undefined) return; + if ( + (yield* Ref.get(activeTurn)) !== context || + (yield* Ref.get(queryContext)) !== steer.live + ) { + return yield* dropHeldSteers(context); + } + context.heldSteers.shift(); + yield* offerSteer({ ...steer, providerTurnId: context.providerTurnId }).pipe( + Effect.catchCause((cause) => + Effect.logWarning("orchestration-v2.claude-held-steer-offer-failed", { + providerTurnId: context.providerTurnId, + cause: Cause.pretty(cause), + }), + ), + ); + } + }); + const finalizeActiveTurn = Effect.fnUntraced(function* (input: { readonly context: ActiveClaudeTurnContext; readonly status: Extract< @@ -8259,6 +8329,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( yield* Ref.update(activeTurn, (current) => current?.providerTurnId === input.context.providerTurnId ? null : current, ); + // After the turn is cleared, so a steer cannot be held on it once this runs. + yield* dropHeldSteers(input.context); if (input.status === "completed") { settledTurnByNativeThread.set(input.context.nativeThreadId, input.context); } else { @@ -9241,6 +9313,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( if (budget !== undefined) budget.compacting = null; } } + // Offered once compaction has ended, a "now" steer is answered in this turn. + yield* releaseHeldSteers(context); } if ( @@ -9330,6 +9404,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }, }); context.compaction = null; + // Some CLI versions end compaction with the boundary alone. + yield* releaseHeldSteers(context); return; } @@ -11308,6 +11384,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( nativeWakeStarted: false, gatedFramesBeforeEcho: 0, heldRootFrames: [], + heldSteers: [], executedModel: undefined, }; const attachTurn = Effect.gen(function* () { @@ -11566,6 +11643,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( detail: `Claude provider turn ${turnInput.providerTurnId} is not the active turn.`, }); } + // Like an unheld steer, a held one reaches Claude before the interrupt. + if (currentTurn.heldSteers.length > 0) { + yield* sdkMessageLock.withPermits(1)(releaseHeldSteers(currentTurn)); + } yield* Ref.update(interruptedTurns, (current) => { const next = new Set(current); next.add(turnInput.providerTurnId); @@ -11718,36 +11799,47 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( Effect.provideService(Path.Path, path), ); yield* backgroundForegroundSubagents(existing, backgroundDeadline); - // The turn can end while the moves are answered. A steer offered - // then would start a new turn the orchestrator never asked for, so - // the steer fails and the orchestrator starts the follow-up turn. - if ( - (yield* Ref.get(activeTurn))?.providerTurnId !== turnInput.providerTurnId || - (yield* Ref.get(queryContext)) !== existing - ) { - return yield* new ProviderAdapter.ProviderAdapterProtocolError({ - driver: CLAUDE_PROVIDER, - detail: `Claude provider turn ${turnInput.providerTurnId} ended before the steer was sent.`, + const held: HeldClaudeSteer = { live: existing, message: userMessage, pdfReservation }; + // Under the frame lock, so a compaction cannot end between the check and the hold. + const delivery = yield* sdkMessageLock.withPermits(1)( + Effect.sync(() => { + // The turn can end while the moves are answered. A steer offered + // then would start a new turn the orchestrator never asked for, so + // the steer fails and the orchestrator starts the follow-up turn. + if ( + Ref.getUnsafe(activeTurn) !== currentTurn || + Ref.getUnsafe(queryContext) !== existing + ) { + return "ended" as const; + } + if ( + currentTurn.compaction?.status !== "running" && + currentTurn.heldSteers.length === 0 + ) { + return "offer" as const; + } + currentTurn.heldSteers.push(held); + return "held" as const; + }), + ); + if (delivery === "ended") { + return yield* steerNotSent(turnInput.providerTurnId); + } + if (delivery === "held") { + // Returning frees the thread's outbox so Stop can reach interruptTurn, which sends + // held steers first. Committed now: this scope's release would otherwise drop it. + pdfReservation.commit(); + yield* Effect.logInfo("orchestration-v2.claude-steer-held-for-compaction", { + providerTurnId: turnInput.providerTurnId, }); + return; } - yield* Ref.update(steeredTurns, (current) => { - const next = new Set(current); - next.add(turnInput.providerTurnId); - return next; + yield* offerSteer({ + live: existing, + providerTurnId: turnInput.providerTurnId, + message: userMessage, + pdfReservation, }); - yield* Effect.uninterruptibleMask((restore) => - restore( - existing.query.offer(userMessage, () => { - pdfReservation.commit(); - }), - ).pipe( - Effect.tap(() => - Effect.sync(() => { - pdfReservation.commit(); - }), - ), - ), - ); }, (effect, turnInput) => effect.pipe( diff --git a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts index bc8fc9a20e3d3..906e410131da5 100644 --- a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts +++ b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts @@ -1516,6 +1516,42 @@ it.effect( ), ); +it.effect("sends a pending steer before the Stop that follows it", () => + Effect.scoped( + Effect.gen(function* () { + const { steered, interrupted, layer, startFirstTurn } = + yield* nextTurnSelectionHarness("steer-before-stop"); + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const threadId = yield* startFirstTurn; + const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!; + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("waiting-steer"), + threadId, + messageId: MessageId.make("message:waiting-steer"), + text: "steer", + attachments: [], + dispatchMode: { type: "steer_active", targetRunId: run.id }, + createdBy: "user", + creationSource: "web", + }); + yield* orchestrator.dispatch({ + type: "thread.stop", + commandId: CommandId.make("stop-after-steer"), + threadId, + }); + yield* worker.drain(); + + const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; + assert.deepEqual(steered, ["steer"]); + assert.deepEqual(interrupted, [turn.id]); + }).pipe(Effect.provide(layer)); + }), + ), +); + // Codex flushes held drafts after the final answer; reserve their pre-steer position before turn end. it.effect("keeps a held commentary draft before the steer when turn end flushes it", () => Effect.scoped(