Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(server): recover from provider interrupt failures (#7412)
Mark Griffin17822fab708a
diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.tsindex 8766e8cb7..15bd6da12 100644--- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts+++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts@@ -150,6 +150,8 @@ describe("ProviderCommandReactor", () => {readonly requiresNewThreadForModelChange?: boolean;readonly titleRegenerationCompletionDispatchFailures?: number;readonly titleRegenerationBeforeStart?: "one" | "two";+ readonly interruptTurnEffect?: () => Effect.Effect<void, ProviderAdapterRequestError>;+ readonly stopSessionEffect?: () => Effect.Effect<void, ProviderAdapterRequestError>;readonly startSessionEffect?: (session: ProviderSession,) => Effect.Effect<ProviderSession, ProviderAdapterRequestError>;@@ -235,23 +237,27 @@ describe("ProviderCommandReactor", () => {turnId: asTurnId("turn-1"),}),);- const interruptTurn = vi.fn((_: unknown) => Effect.void);+ const interruptTurn = vi.fn((_: unknown) => input?.interruptTurnEffect?.() ?? Effect.void);const respondToRequest = vi.fn<ProviderServiceShape["respondToRequest"]>(() => Effect.void);const respondToUserInput = vi.fn<ProviderServiceShape["respondToUserInput"]>(() => Effect.void);- const stopSession = vi.fn((input: unknown) =>- Effect.sync(() => {- const threadId =- typeof input === "object" && input !== null && "threadId" in input- ? (input as { threadId?: ThreadId }).threadId- : undefined;- if (!threadId) {- return;- }- const index = runtimeSessions.findIndex((session) => session.threadId === threadId);- if (index >= 0) {- runtimeSessions.splice(index, 1);- }- }),+ const stopSession = vi.fn((stopInput: unknown) =>+ (input?.stopSessionEffect?.() ?? Effect.void).pipe(+ Effect.tap(() =>+ Effect.sync(() => {+ const threadId =+ typeof stopInput === "object" && stopInput !== null && "threadId" in stopInput+ ? (stopInput as { threadId?: ThreadId }).threadId+ : undefined;+ if (!threadId) {+ return;+ }+ const index = runtimeSessions.findIndex((session) => session.threadId === threadId);+ if (index >= 0) {+ runtimeSessions.splice(index, 1);+ }+ }),+ ),+ ),);const renameBranch = vi.fn((input: unknown) =>Effect.succeed({@@ -2487,6 +2493,218 @@ describe("ProviderCommandReactor", () => {});});+ effectIt.effect(+ "stops a running session and records the failure when provider interrupt fails",+ () =>+ Effect.gen(function* () {+ const harness = yield* Effect.promise(() =>+ createHarness({+ interruptTurnEffect: () =>+ Effect.fail(+ new ProviderAdapterRequestError({+ provider: "codex",+ method: "thread.interrupt",+ detail: "provider session disappeared",+ }),+ ),+ stopSessionEffect: () =>+ Effect.fail(+ new ProviderAdapterRequestError({+ provider: "codex",+ method: "session.stop",+ detail: "provider process already exited",+ }),+ ),+ }),+ );+ const now = "2026-01-01T00:00:00.000Z";++ yield* harness.engine.dispatch({+ type: "thread.session.set",+ commandId: CommandId.make("cmd-session-set-interrupt-failure"),+ threadId: ThreadId.make("thread-1"),+ session: {+ threadId: ThreadId.make("thread-1"),+ status: "running",+ providerName: "codex",+ runtimeMode: "approval-required",+ activeTurnId: asTurnId("turn-1"),+ lastError: null,+ updatedAt: now,+ },+ createdAt: now,+ });++ yield* harness.engine.dispatch({+ type: "thread.turn.interrupt",+ commandId: CommandId.make("cmd-turn-interrupt-provider-failure"),+ threadId: ThreadId.make("thread-1"),+ turnId: asTurnId("turn-1"),+ createdAt: now,+ });++ yield* Effect.promise(() =>+ waitFor(async () => {+ const thread = (await harness.readModel()).threads.find(+ (entry) => entry.id === ThreadId.make("thread-1"),+ );+ return thread?.session?.status === "stopped";+ }),+ );++ const thread = (yield* Effect.promise(() => harness.readModel())).threads.find(+ (entry) => entry.id === ThreadId.make("thread-1"),+ );+ expect(thread?.session).toMatchObject({+ status: "stopped",+ activeTurnId: null,+ lastError: "provider session disappeared",+ });+ expect(+ thread?.activities.find((activity) => activity.kind === "provider.turn.interrupt.failed"),+ ).toMatchObject({+ summary: "Provider turn interrupt failed",+ payload: { detail: "provider session disappeared" },+ });+ expect(harness.stopSession).toHaveBeenCalledWith({ threadId: ThreadId.make("thread-1") });+ }),+ );++ effectIt.effect("stops a starting session without a bound turn when interrupt fails", () =>+ Effect.gen(function* () {+ const harness = yield* Effect.promise(() =>+ createHarness({+ interruptTurnEffect: () =>+ Effect.fail(+ new ProviderAdapterRequestError({+ provider: "codex",+ method: "thread.interrupt",+ detail: "provider session disappeared",+ }),+ ),+ }),+ );+ const now = "2026-01-01T00:00:00.000Z";++ yield* harness.engine.dispatch({+ type: "thread.session.set",+ commandId: CommandId.make("cmd-session-set-interrupt-starting"),+ threadId: ThreadId.make("thread-1"),+ session: {+ threadId: ThreadId.make("thread-1"),+ status: "starting",+ providerName: "codex",+ runtimeMode: "approval-required",+ activeTurnId: null,+ lastError: null,+ updatedAt: now,+ },+ createdAt: now,+ });++ yield* harness.engine.dispatch({+ type: "thread.turn.interrupt",+ commandId: CommandId.make("cmd-turn-interrupt-starting-provider-failure"),+ threadId: ThreadId.make("thread-1"),+ createdAt: now,+ });++ yield* Effect.promise(() => harness.drain());++ const thread = (yield* Effect.promise(() => harness.readModel())).threads.find(+ (entry) => entry.id === ThreadId.make("thread-1"),+ );+ expect(thread?.session).toMatchObject({+ status: "stopped",+ activeTurnId: null,+ lastError: "provider session disappeared",+ });+ expect(harness.stopSession).toHaveBeenCalledWith({ threadId: ThreadId.make("thread-1") });+ expect(+ thread?.activities.find((activity) => activity.kind === "provider.turn.interrupt.failed"),+ ).toMatchObject({ payload: { detail: "provider session disappeared" } });+ }),+ );++ effectIt.effect("does not overwrite a session that became ready while an interrupt failed", () =>+ Effect.gen(function* () {+ const harness = yield* Effect.promise(() => createHarness());+ const now = "2026-01-01T00:00:00.000Z";+ const completedAt = "2026-01-01T00:00:01.000Z";++ yield* harness.engine.dispatch({+ type: "thread.session.set",+ commandId: CommandId.make("cmd-session-set-interrupt-race"),+ threadId: ThreadId.make("thread-1"),+ session: {+ threadId: ThreadId.make("thread-1"),+ status: "running",+ providerName: "codex",+ runtimeMode: "approval-required",+ activeTurnId: asTurnId("turn-1"),+ lastError: null,+ updatedAt: now,+ },+ createdAt: now,+ });++ harness.interruptTurn.mockImplementation(() =>+ harness.engine+ .dispatch({+ type: "thread.session.set",+ commandId: CommandId.make("cmd-session-set-natural-completion"),+ threadId: ThreadId.make("thread-1"),+ session: {+ threadId: ThreadId.make("thread-1"),+ status: "ready",+ providerName: "codex",+ runtimeMode: "approval-required",+ activeTurnId: null,+ lastError: null,+ updatedAt: completedAt,+ },+ createdAt: completedAt,+ })+ .pipe(+ Effect.catchCause((cause) => Effect.die(cause)),+ Effect.andThen(+ Effect.fail(+ new ProviderAdapterRequestError({+ provider: "codex",+ method: "thread.interrupt",+ detail: "provider session disappeared",+ }),+ ),+ ),+ ),+ );++ yield* harness.engine.dispatch({+ type: "thread.turn.interrupt",+ commandId: CommandId.make("cmd-turn-interrupt-race"),+ threadId: ThreadId.make("thread-1"),+ turnId: asTurnId("turn-1"),+ createdAt: now,+ });++ yield* Effect.promise(() => harness.drain());++ const thread = (yield* Effect.promise(() => harness.readModel())).threads.find(+ (entry) => entry.id === ThreadId.make("thread-1"),+ );+ expect(thread?.session).toMatchObject({+ status: "ready",+ activeTurnId: null,+ lastError: null,+ updatedAt: completedAt,+ });+ expect(harness.stopSession).not.toHaveBeenCalled();+ expect(+ thread?.activities.some((activity) => activity.kind === "provider.turn.interrupt.failed"),+ ).toBe(false);+ }),+ );+it("starts a fresh session when only projected session state exists", 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.tsindex cfc95f261..c2d8d1e42 100644--- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts+++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts@@ -1180,8 +1180,8 @@ const make = Effect.gen(function* () {if (!thread) {return;}- const hasSession = thread.session && thread.session.status !== "stopped";- if (!hasSession) {+ const session = thread.session;+ if (!session || session.status === "stopped") {return yield* appendProviderFailureActivity({threadId: event.payload.threadId,kind: "provider.turn.interrupt.failed",@@ -1192,8 +1192,80 @@ const make = Effect.gen(function* () {});}+ const recoverInterruptFailure = (cause: Cause.Cause<unknown>) => {+ if (Cause.hasInterruptsOnly(cause)) {+ return Effect.interrupt;+ }++ const detail = formatFailureDetail(cause);+ return Effect.gen(function* () {+ const latestThread = yield* resolveThread(event.payload.threadId);+ const latestSession = latestThread?.session;+ if (+ !latestSession ||+ latestSession.status === "stopped" ||+ latestSession.status === "ready" ||+ (event.payload.turnId !== undefined &&+ latestSession.activeTurnId !== null &&+ latestSession.activeTurnId !== event.payload.turnId)+ ) {+ return;+ }++ yield* providerService.stopSession({ threadId: event.payload.threadId }).pipe(+ Effect.catchCause((stopCause) => {+ if (Cause.hasInterruptsOnly(stopCause)) {+ return Effect.interrupt;+ }+ return Effect.logWarning(+ "provider command reactor failed to stop session after interrupt failure",+ {+ threadId: event.payload.threadId,+ cause: Cause.pretty(stopCause),+ originalCause: Cause.pretty(cause),+ },+ );+ }),+ );+ const stoppedThread = yield* resolveThread(event.payload.threadId);+ const stoppedSession = stoppedThread?.session;+ if (+ !stoppedSession ||+ stoppedSession.status === "stopped" ||+ stoppedSession.status === "ready" ||+ (event.payload.turnId !== undefined &&+ stoppedSession.activeTurnId !== null &&+ stoppedSession.activeTurnId !== event.payload.turnId)+ ) {+ return;+ }++ yield* setThreadSession({+ threadId: event.payload.threadId,+ session: {+ ...stoppedSession,+ status: "stopped",+ activeTurnId: null,+ lastError: detail,+ updatedAt: event.payload.createdAt,+ },+ createdAt: event.payload.createdAt,+ });+ yield* appendProviderFailureActivity({+ threadId: event.payload.threadId,+ kind: "provider.turn.interrupt.failed",+ summary: "Provider turn interrupt failed",+ detail,+ turnId: event.payload.turnId ?? null,+ createdAt: event.payload.createdAt,+ });+ });+ };+// Orchestration turn ids are not provider turn ids, so interrupt by session.- yield* providerService.interruptTurn({ threadId: event.payload.threadId });+ yield* providerService+ .interruptTurn({ threadId: event.payload.threadId })+ .pipe(Effect.catchCause(recoverInterruptFailure));});const processApprovalResponseRequested = Effect.fn("processApprovalResponseRequested")(function* (