From 0ddba3829c69a7edbda62a4018bac89099570897 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Wed, 22 Jul 2026 05:04:27 -0700 Subject: [PATCH] Handle interrupted pending starts --- .../Layers/ProjectionSnapshotQuery.ts | 126 ++++++++++-------- .../Layers/ProviderCommandReactor.test.ts | 38 ++++++ .../Layers/ProviderCommandReactor.ts | 47 ++++--- 3 files changed, 134 insertions(+), 77 deletions(-) diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts index 345b223163..25341eb9ea 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts @@ -1907,69 +1907,81 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { }); const getThreadShellById: ProjectionSnapshotQueryShape["getThreadShellById"] = (threadId) => - Effect.gen(function* () { - const [threadRow, latestTurnRow, sessionRow, pendingTurnStartRow] = yield* Effect.all([ - getActiveThreadRowById({ threadId }).pipe( - Effect.mapError( - toPersistenceSqlOrDecodeError( - "ProjectionSnapshotQuery.getThreadShellById:getThread:query", - "ProjectionSnapshotQuery.getThreadShellById:getThread:decodeRow", + sql + .withTransaction( + Effect.gen(function* () { + const [threadRow, latestTurnRow, sessionRow, pendingTurnStartRow] = yield* Effect.all([ + getActiveThreadRowById({ threadId }).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getThreadShellById:getThread:query", + "ProjectionSnapshotQuery.getThreadShellById:getThread:decodeRow", + ), + ), ), - ), - ), - getLatestTurnRowByThread({ threadId }).pipe( - Effect.mapError( - toPersistenceSqlOrDecodeError( - "ProjectionSnapshotQuery.getThreadShellById:getLatestTurn:query", - "ProjectionSnapshotQuery.getThreadShellById:getLatestTurn:decodeRow", + getLatestTurnRowByThread({ threadId }).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getThreadShellById:getLatestTurn:query", + "ProjectionSnapshotQuery.getThreadShellById:getLatestTurn:decodeRow", + ), + ), ), - ), - ), - getThreadSessionRowByThread({ threadId }).pipe( - Effect.mapError( - toPersistenceSqlOrDecodeError( - "ProjectionSnapshotQuery.getThreadShellById:getSession:query", - "ProjectionSnapshotQuery.getThreadShellById:getSession:decodeRow", + getThreadSessionRowByThread({ threadId }).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getThreadShellById:getSession:query", + "ProjectionSnapshotQuery.getThreadShellById:getSession:decodeRow", + ), + ), ), - ), - ), - getPendingTurnStartRowByThread({ threadId }).pipe( - Effect.mapError( - toPersistenceSqlOrDecodeError( - "ProjectionSnapshotQuery.getThreadShellById:getPendingTurnStart:query", - "ProjectionSnapshotQuery.getThreadShellById:getPendingTurnStart:decodeRow", + getPendingTurnStartRowByThread({ threadId }).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getThreadShellById:getPendingTurnStart:query", + "ProjectionSnapshotQuery.getThreadShellById:getPendingTurnStart:decodeRow", + ), + ), ), - ), - ), - ]); + ]); - if (Option.isNone(threadRow)) { - return Option.none(); - } + if (Option.isNone(threadRow)) { + return Option.none(); + } - return Option.some({ - id: threadRow.value.threadId, - projectId: threadRow.value.projectId, - title: threadRow.value.title, - modelSelection: threadRow.value.modelSelection, - runtimeMode: threadRow.value.runtimeMode, - interactionMode: threadRow.value.interactionMode, - branch: threadRow.value.branch, - worktreePath: threadRow.value.worktreePath, - latestTurn: Option.isSome(latestTurnRow) ? mapLatestTurn(latestTurnRow.value) : null, - createdAt: threadRow.value.createdAt, - updatedAt: threadRow.value.updatedAt, - archivedAt: threadRow.value.archivedAt, - settledOverride: threadRow.value.settledOverride, - settledAt: threadRow.value.settledAt, - session: Option.isSome(sessionRow) ? mapSessionRow(sessionRow.value) : null, - latestUserMessageAt: threadRow.value.latestUserMessageAt, - hasPendingTurnStart: Option.isSome(pendingTurnStartRow), - hasPendingApprovals: threadRow.value.pendingApprovalCount > 0, - hasPendingUserInput: threadRow.value.pendingUserInputCount > 0, - hasActionableProposedPlan: threadRow.value.hasActionableProposedPlan > 0, - } satisfies OrchestrationThreadShell); - }); + return Option.some({ + id: threadRow.value.threadId, + projectId: threadRow.value.projectId, + title: threadRow.value.title, + modelSelection: threadRow.value.modelSelection, + runtimeMode: threadRow.value.runtimeMode, + interactionMode: threadRow.value.interactionMode, + branch: threadRow.value.branch, + worktreePath: threadRow.value.worktreePath, + latestTurn: Option.isSome(latestTurnRow) ? mapLatestTurn(latestTurnRow.value) : null, + createdAt: threadRow.value.createdAt, + updatedAt: threadRow.value.updatedAt, + archivedAt: threadRow.value.archivedAt, + settledOverride: threadRow.value.settledOverride, + settledAt: threadRow.value.settledAt, + session: Option.isSome(sessionRow) ? mapSessionRow(sessionRow.value) : null, + latestUserMessageAt: threadRow.value.latestUserMessageAt, + hasPendingTurnStart: Option.isSome(pendingTurnStartRow), + hasPendingApprovals: threadRow.value.pendingApprovalCount > 0, + hasPendingUserInput: threadRow.value.pendingUserInputCount > 0, + hasActionableProposedPlan: threadRow.value.hasActionableProposedPlan > 0, + } satisfies OrchestrationThreadShell); + }), + ) + .pipe( + Effect.mapError((error) => + isPersistenceError(error) + ? error + : toPersistenceSqlError("ProjectionSnapshotQuery.getThreadShellById:transaction")( + error, + ), + ), + ); const getThreadDetailById: ProjectionSnapshotQueryShape["getThreadDetailById"] = (threadId) => Effect.gen(function* () { diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts index 0dc770defd..3764e3c8ed 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts @@ -514,6 +514,44 @@ describe("ProviderCommandReactor", () => { }), ); + effectIt.effect("clears pending work when turn startup is interrupted", () => + Effect.gen(function* () { + const harness = yield* Effect.promise(() => createHarness()); + const now = "2026-01-01T00:00:00.000Z"; + const messageId = asMessageId("user-message-start-interrupted"); + harness.sendTurn.mockImplementationOnce(() => Effect.interrupt); + + yield* harness.engine.dispatch({ + type: "thread.turn.start", + commandId: CommandId.make("cmd-turn-start-interrupted"), + threadId: ThreadId.make("thread-1"), + message: { + messageId, + role: "user", + text: "hello reactor", + attachments: [], + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + createdAt: now, + }); + + yield* Effect.promise(() => + waitFor(async () => { + const readModel = await harness.readModel(); + return ( + readModel.threads[0]?.activities.some( + (activity) => activity.kind === "provider.turn.start.failed", + ) ?? false + ); + }), + ); + + const shellSnapshot = yield* harness.snapshotQuery.getShellSnapshot(); + expect(shellSnapshot.threads[0]?.hasPendingTurnStart).toBe(false); + }), + ); + it("generates a thread title on the first turn", async () => { const harness = await createHarness(); const now = "2026-01-01T00:00:00.000Z"; diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts index 3ec829500a..570335ff30 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts @@ -802,30 +802,37 @@ const make = Effect.gen(function* () { } } - const handleTurnStartFailure = (cause: Cause.Cause) => { - if (Cause.hasInterruptsOnly(cause)) { - return Effect.void; + const handleTurnStartFailure = Effect.fnUntraced(function* (cause: Cause.Cause) { + const interrupted = Cause.hasInterruptsOnly(cause); + if (interrupted) { + const threadShell = yield* projectionSnapshotQuery.getThreadShellById( + event.payload.threadId, + ); + if (Option.isNone(threadShell) || threadShell.value.hasPendingTurnStart !== true) { + return; + } } - const detail = formatFailureDetail(cause); - return setThreadSessionErrorOnTurnStartFailure({ + + const detail = interrupted + ? "Provider turn start was interrupted before it began." + : formatFailureDetail(cause); + if (!interrupted) { + yield* setThreadSessionErrorOnTurnStartFailure({ + threadId: event.payload.threadId, + detail, + createdAt: event.payload.createdAt, + }); + } + yield* appendProviderFailureActivity({ threadId: event.payload.threadId, + kind: "provider.turn.start.failed", + summary: "Provider turn start failed", detail, + turnId: null, createdAt: event.payload.createdAt, - }).pipe( - Effect.flatMap(() => - appendProviderFailureActivity({ - threadId: event.payload.threadId, - kind: "provider.turn.start.failed", - summary: "Provider turn start failed", - detail, - turnId: null, - createdAt: event.payload.createdAt, - requestId: event.payload.messageId, - }), - ), - Effect.asVoid, - ); - }; + requestId: event.payload.messageId, + }); + }); const recoverTurnStartFailure = (cause: Cause.Cause) => handleTurnStartFailure(cause).pipe(