From b47a5cf107556d7e9580d611c98cb6ad046faefb Mon Sep 17 00:00:00 2001 From: Renfei Zhou Date: Tue, 24 Nov 2026 15:18:58 +0000 Subject: [PATCH 1/2] test(server): cover a run whose turn ends while a steer restarts it A steer can read a run as running, and the run's provider turn can then end before the steer commits its restart. The restarted attempt must give the run its checkpoint, and a Stop that lands before the restarted turn starts must keep the first attempt's checkpoint. Today the restarted attempt's capture is dropped, so the run ends with no checkpoint. Co-Authored-By: Claude Opus 5.5 --- ...steerRestartCheckpoint.integration.test.ts | 346 ++++++++++++++++++ 1 file changed, 346 insertions(+) create mode 100644 apps/server/src/fork/steerRestartCheckpoint/steerRestartCheckpoint.integration.test.ts diff --git a/apps/server/src/fork/steerRestartCheckpoint/steerRestartCheckpoint.integration.test.ts b/apps/server/src/fork/steerRestartCheckpoint/steerRestartCheckpoint.integration.test.ts new file mode 100644 index 0000000000000..9fbcaabbfd218 --- /dev/null +++ b/apps/server/src/fork/steerRestartCheckpoint/steerRestartCheckpoint.integration.test.ts @@ -0,0 +1,346 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, it } from "@effect/vitest"; +import { + CommandId, + MessageId, + ProjectId, + ProviderDriverKind, + ProviderInstanceId, + ProviderThreadId, + ProviderTurnId, + ThreadId, +} from "@t3tools/contracts"; +import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; +import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace"; +import * as DateTime from "effect/DateTime"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as FileSystem from "effect/FileSystem"; +import * as Queue from "effect/Queue"; +import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; + +import { CodexProviderCapabilitiesV2 } from "../../orchestration-v2/Adapters/CodexAdapterV2.ts"; +import * as EffectWorker from "../../orchestration-v2/EffectWorker.ts"; +import * as Orchestrator from "../../orchestration-v2/Orchestrator.ts"; +import * as ProviderAdapterRegistry from "../../orchestration-v2/ProviderAdapterRegistry.ts"; +import * as ProviderReplayHarness from "../../orchestration-v2/testkit/ProviderReplayHarness.ts"; +import * as ThreadCommandExecutor from "../../orchestration-v2/ThreadCommandExecutor.ts"; + +const driver = ProviderDriverKind.make("codex"); +const instanceId = ProviderInstanceId.make("codex"); +const runSelection = { + instanceId, + model: "test-model", + options: [{ id: "effort", value: "xhigh" }], +}; +const composerSelection = { + ...runSelection, + options: [...runSelection.options, { id: "fastMode", value: false }], +}; + +// A provider that steers by interrupt-restart; the steer waits in planSelectionTransition. +const steerRestartHarness = Effect.fn("steerRestartHarness")(function* ( + name: string, + beforeSelectionTransition: Effect.Effect, +) { + const cwd = yield* checkpointWorkspace(name); + const events = yield* Queue.unbounded(); + const started: ProviderAdapter.ProviderAdapterV2TurnInput[] = []; + const capabilities = { + ...CodexProviderCapabilitiesV2, + turns: { + ...CodexProviderCapabilitiesV2.turns, + supportsActiveSteering: false, + supportsSteeringByInterruptRestart: true, + }, + }; + const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = { + instanceId, + driver, + getCapabilities: () => Effect.succeed(capabilities), + planSelectionTransition: () => + beforeSelectionTransition.pipe(Effect.as({ type: "apply_on_next_turn" as const })), + openSession: (input) => + Effect.gen(function* () { + const now = yield* DateTime.now; + const turnUpdated = ( + turn: ProviderAdapter.ProviderAdapterV2TurnInput, + status: "running" | "interrupted", + ) => + Queue.offer(events, { + type: "provider_turn.updated", + driver, + providerTurn: { + id: ProviderTurnId.make(`provider-turn:${turn.attemptId}`), + providerThreadId: turn.providerThread.id, + nodeId: turn.rootNodeId, + runAttemptId: turn.attemptId, + nativeTurnRef: { driver, nativeId: `native:${turn.attemptId}`, strength: "strong" }, + ordinal: turn.providerTurnOrdinal, + status, + startedAt: now, + completedAt: status === "running" ? null : now, + }, + }); + const startTurn = (turn: ProviderAdapter.ProviderAdapterV2TurnInput) => + Effect.gen(function* () { + started.push(turn); + yield* turnUpdated(turn, "running"); + }); + return { + instanceId, + driver, + providerSessionId: input.providerSessionId, + providerSession: { + id: input.providerSessionId, + driver, + providerInstanceId: instanceId, + status: "ready", + cwd, + model: runSelection.model, + capabilities, + createdAt: now, + updatedAt: now, + lastError: null, + }, + events: Stream.fromQueue(events), + ensureThread: ({ threadId }) => + Effect.succeed({ + id: ProviderThreadId.make(`provider-thread:${threadId}`), + driver, + providerInstanceId: instanceId, + providerSessionId: input.providerSessionId, + appThreadId: threadId, + ownerNodeId: null, + nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" }, + nativeConversationHeadRef: null, + status: "idle", + firstRunOrdinal: null, + lastRunOrdinal: null, + handoffIds: [], + forkedFrom: null, + createdAt: now, + updatedAt: now, + }), + resumeThread: ({ providerThread }) => Effect.succeed(providerThread), + startTurn, + compactThread: startTurn, + steerTurn: () => Effect.die("unused"), + interruptTurn: ({ providerTurnId }) => + Effect.forEach( + started.filter( + (turn) => ProviderTurnId.make(`provider-turn:${turn.attemptId}`) === providerTurnId, + ), + (turn) => + turnUpdated(turn, "interrupted").pipe( + Effect.andThen( + Queue.offer(events, { + type: "turn.terminal", + driver, + providerThreadId: turn.providerThread.id, + providerTurnId, + runOrdinal: turn.runOrdinal, + status: "interrupted", + failure: null, + threadDisposition: "reusable", + }), + ), + ), + { discard: true }, + ), + respondToRuntimeRequest: () => Effect.void, + readThreadSnapshot: () => Effect.die("unused"), + rollbackThread: () => Effect.die("unused"), + forkThread: () => Effect.die("unused"), + }; + }), + }; + const layer = ProviderReplayHarness.layerWithRegistry( + { name }, + ProviderAdapterRegistry.layerFromAdapters([adapter]), + { runEffectWorker: false }, + ); + const startFirstTurn = Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const threadId = ThreadId.make(`thread:${name}`); + yield* orchestrator.dispatch({ + type: "thread.create", + commandId: CommandId.make("create"), + threadId, + projectId: ProjectId.make(`project:${name}`), + title: "Steer restart at turn end", + modelSelection: runSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: cwd, + createdBy: "user", + creationSource: "web", + }); + const running = yield* orchestrator.streamDomainEvents.pipe( + Stream.filter( + (event) => event.type === "provider-turn.updated" && event.payload.status === "running", + ), + Stream.take(1), + Stream.runDrain, + Effect.forkScoped, + ); + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("first"), + threadId, + messageId: MessageId.make("message:first"), + text: "first", + attachments: [], + modelSelection: runSelection, + dispatchMode: { type: "start_immediately" }, + createdBy: "user", + creationSource: "web", + }); + yield* worker.drain(); + yield* Fiber.join(running); + yield* (yield* ThreadCommandExecutor.ThreadCommandExecutor).withLock(threadId, Effect.void); + return threadId; + }); + return { cwd, events, started, layer, startFirstTurn }; +}); + +it.effect.each(["completed", "interrupted", "stopped before it starts"] as const)( + "checkpoints a run whose turn ended while a steer restarted it (restart %s)", + (restartEnd) => + Effect.gen(function* () { + const steerReadRunning = yield* Deferred.make(); + const letSteerCommit = yield* Deferred.make(); + const { cwd, events, started, layer, startFirstTurn } = yield* steerRestartHarness( + `steer-restart-after-turn-end-${restartEnd}`, + Deferred.succeed(steerReadRunning, undefined).pipe( + Effect.andThen(Deferred.await(letSteerCommit)), + ), + ); + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const threadId = yield* startFirstTurn; + const runId = started[0]!.runId; + const endTurn = ( + turn: ProviderAdapter.ProviderAdapterV2TurnInput, + status: "completed" | "interrupted", + ) => + Effect.gen(function* () { + const providerTurn = (yield* orchestrator.getThreadProjection( + threadId, + )).providerTurns.find((candidate) => candidate.runAttemptId === turn.attemptId)!; + const settled = yield* orchestrator.streamDomainEvents.pipe( + Stream.filter( + (event) => + event.type === "run.updated" && + event.payload.id === runId && + event.payload.status === status, + ), + Stream.take(1), + Stream.runDrain, + Effect.forkScoped, + ); + yield* Queue.offer(events, { + type: "provider_turn.updated", + driver, + providerTurn: { ...providerTurn, status, completedAt: yield* DateTime.now }, + }); + yield* Queue.offer(events, { + type: "turn.terminal", + driver, + providerThreadId: providerTurn.providerThreadId, + providerTurnId: providerTurn.id, + runOrdinal: turn.runOrdinal, + status, + failure: null, + threadDisposition: "reusable", + }); + yield* Fiber.join(settled); + }); + const writeWorkspaceFile = (name: string) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + yield* fileSystem.writeFileString(`${cwd}/${name}`, `${name}\n`); + }).pipe(Effect.provide(NodeServices.layer), Effect.orDie); + // The first turn's capture finds the run restarting and retries with backoff. + const drainWithRetries = Effect.forEach( + [1, 2, 3, 4, 5], + () => TestClock.adjust("1 second").pipe(Effect.andThen(worker.drain())), + { discard: true }, + ); + const checkpointFiles = Effect.gen(function* () { + const projection = yield* orchestrator.getThreadProjection(threadId); + const run = projection.runs.find((candidate) => candidate.id === runId); + const checkpoint = projection.checkpoints.find( + (candidate) => candidate.id === run?.checkpointId, + ); + assert.equal(checkpoint?.status, "ready"); + return { status: run?.status, files: checkpoint?.files.map((file) => file.path) }; + }); + + // The steer reads the run as running, then the turn ends before it commits its restart. + const steer = yield* orchestrator + .dispatch({ + type: "message.dispatch", + commandId: CommandId.make("steer-at-turn-end"), + threadId, + messageId: MessageId.make("message:steer-at-turn-end"), + text: "also check the logs", + attachments: [], + modelSelection: composerSelection, + dispatchMode: { type: "steer_active", targetRunId: runId }, + createdBy: "agent", + creationSource: "mcp", + }) + .pipe(Effect.forkScoped); + yield* Deferred.await(steerReadRunning); + yield* writeWorkspaceFile("first-attempt.txt"); + yield* endTurn(started[0]!, "completed"); + yield* Deferred.succeed(letSteerCommit, undefined); + yield* Fiber.join(steer); + + if (restartEnd === "stopped before it starts") { + yield* worker.runOnce(); + yield* orchestrator.dispatch({ + type: "thread.stop", + commandId: CommandId.make("stop-before-restart-starts"), + threadId, + }); + yield* drainWithRetries; + assert.equal(started.length, 1); + const { status, files } = yield* checkpointFiles; + assert.equal(status, "interrupted"); + assert.include(files ?? [], "first-attempt.txt"); + return; + } + + const restartRunning = yield* orchestrator.streamDomainEvents.pipe( + Stream.filter( + (event) => + event.type === "provider-turn.updated" && + event.payload.status === "running" && + event.payload.runAttemptId !== started[0]!.attemptId, + ), + Stream.take(1), + Stream.runDrain, + Effect.forkScoped, + ); + yield* drainWithRetries; + yield* Fiber.join(restartRunning); + yield* (yield* ThreadCommandExecutor.ThreadCommandExecutor).withLock(threadId, Effect.void); + assert.equal(started.length, 2); + assert.equal(started[1]!.runId, runId); + + yield* writeWorkspaceFile("restarted-attempt.txt"); + yield* endTurn(started[1]!, restartEnd); + yield* worker.drain(); + const { status, files } = yield* checkpointFiles; + assert.equal(status, restartEnd); + assert.includeMembers(files ?? [], ["first-attempt.txt", "restarted-attempt.txt"]); + }).pipe(Effect.provide(layer)); + }), +); From 75d3faaca2dd512df69d43f7599f2f379930ebae Mon Sep 17 00:00:00 2001 From: Renfei Zhou Date: Tue, 24 Nov 2026 15:20:03 +0000 Subject: [PATCH 2/2] fix(server): a run restarted at turn end gets its restarted attempt's checkpoint The turn-end checkpoint capture used the run id as its outbox effect id. When a late steer restarted a run whose turn had just ended, the restarted attempt's capture reused that id and the outbox dropped it, so the run ended with no checkpoint and was not offered as the latest fork point. Key the capture effect id by run attempt, at turn end and at Stop. The command id stays per run, so the capture's commit receipt and startup recovery are unchanged, and the readers added with run completion at turn end now find a run's unsettled capture by that command id, which also finds rows queued before this change. Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Orchestrator.ts | 31 ++++++++++++------- .../orchestration-v2/RunExecutionService.ts | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index aff0021d6c61c..b00b08ec32c83 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -3578,22 +3578,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio // completed, and one Stop dropped cancelled. It runs next, in its place among waiting messages. // A run completes before its turn-end checkpoint capture. Steers and queued turns advance when // the capture commits and repeats the update, after the deliveries ahead of it on the lane. - const awaitsCheckpointCapture = (run: OrchestrationV2Run) => + const unsettledCheckpointCapture = (run: OrchestrationV2Run) => run.status !== "completed" || run.checkpointId !== null - ? Effect.succeed(false) - : effectOutbox.get(`effect:checkpoint.capture:${run.id}`).pipe( - Effect.map( - Option.exists((effect) => effect.status === "pending" || effect.status === "running"), - ), - Effect.orElseSucceed(() => false), - ); + ? Effect.succeed(Option.none()) + : effectOutbox + .listByCommandId(CommandId.make(`command:effect:checkpoint.capture:${run.id}`)) + .pipe( + Effect.map((effects) => + Option.fromNullishOr( + effects.find((effect) => effect.status === "pending" || effect.status === "running") + ?.id, + ), + ), + Effect.orElseSucceed(() => Option.none()), + ); + const awaitsCheckpointCapture = (run: OrchestrationV2Run) => + Effect.map(unsettledCheckpointCapture(run), Option.isSome); const awaitLatestRunCapture = Effect.fn("Orchestrator.awaitLatestRunCapture")( function* (threadId: ThreadId) { const { runs } = yield* projectionStore.getThreadRecords(threadId, ["runs"]); const latest = latestStartedRun(runs); - if (latest === undefined || !(yield* awaitsCheckpointCapture(latest))) return; - yield* effectOutbox.awaitSettled(`effect:checkpoint.capture:${latest.id}`); + if (latest === undefined) return; + const capture = yield* unsettledCheckpointCapture(latest); + if (Option.isNone(capture)) return; + yield* effectOutbox.awaitSettled(capture.value); }, (effect) => Effect.ignore(effect), ); @@ -13579,7 +13588,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio yield* Ref.update(input.effects, (effects) => [ ...effects, { - id: `effect:checkpoint.capture:${run.id}`, + id: `effect:checkpoint.capture:${attempt.id}`, commandId: CommandId.make(`command:effect:checkpoint.capture:${run.id}`), threadId: input.command.threadId, request: { diff --git a/apps/server/src/orchestration-v2/RunExecutionService.ts b/apps/server/src/orchestration-v2/RunExecutionService.ts index 46e0485d4014b..eec3a8cce0a3f 100644 --- a/apps/server/src/orchestration-v2/RunExecutionService.ts +++ b/apps/server/src/orchestration-v2/RunExecutionService.ts @@ -883,7 +883,7 @@ export const layer: Layer.Layer< input.terminal.status === "cancelled" ? [ { - id: `effect:checkpoint.capture:${input.run.id}`, + id: `effect:checkpoint.capture:${input.attempt.id}`, commandId: checkpointCaptureCommandId, threadId: input.run.threadId, request: {