diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts index 97379b94b8..383d8284b6 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts @@ -2606,7 +2606,7 @@ describe("ProviderCommandReactor", () => { }); }); - it("reacts to thread.approval.respond by forwarding provider approval response", async () => { + it("forwards approval declines without interrupting the provider turn", async () => { const harness = await createHarness(); const now = "2026-01-01T00:00:00.000Z"; @@ -2631,20 +2631,136 @@ describe("ProviderCommandReactor", () => { await Effect.runPromise( harness.engine.dispatch({ type: "thread.approval.respond", - commandId: CommandId.make("cmd-approval-respond"), + commandId: CommandId.make("cmd-approval-decline"), threadId: ThreadId.make("thread-1"), requestId: asApprovalRequestId("approval-request-1"), - decision: "accept", + decision: "decline", createdAt: now, }), ); await waitFor(() => harness.respondToRequest.mock.calls.length === 1); + await harness.drain(); expect(harness.respondToRequest.mock.calls[0]?.[0]).toEqual({ threadId: "thread-1", requestId: "approval-request-1", - decision: "accept", + decision: "decline", }); + expect(harness.interruptTurn).not.toHaveBeenCalled(); + }); + + it("responds to approval cancellation before interrupting the provider turn", async () => { + const harness = await createHarness(); + const now = "2026-01-01T00:00:00.000Z"; + const callOrder: Array<"respond" | "interrupt"> = []; + harness.respondToRequest.mockImplementation(() => + Effect.sync(() => { + callOrder.push("respond"); + }), + ); + harness.interruptTurn.mockImplementation(() => + Effect.sync(() => { + callOrder.push("interrupt"); + }), + ); + + await Effect.runPromise( + harness.engine.dispatch({ + type: "thread.session.set", + commandId: CommandId.make("cmd-session-set-for-approval-cancel"), + 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, + }), + ); + + await Effect.runPromise( + harness.engine.dispatch({ + type: "thread.approval.respond", + commandId: CommandId.make("cmd-approval-cancel"), + threadId: ThreadId.make("thread-1"), + requestId: asApprovalRequestId("approval-request-1"), + decision: "cancel", + createdAt: now, + }), + ); + + await waitFor(() => harness.interruptTurn.mock.calls.length === 1); + await harness.drain(); + expect(harness.respondToRequest.mock.calls[0]?.[0]).toEqual({ + threadId: "thread-1", + requestId: "approval-request-1", + decision: "cancel", + }); + expect(harness.interruptTurn.mock.calls[0]?.[0]).toEqual({ + threadId: "thread-1", + }); + expect(callOrder).toEqual(["respond", "interrupt"]); + }); + + it("does not interrupt when the approval cancellation response fails", async () => { + const harness = await createHarness(); + const now = "2026-01-01T00:00:00.000Z"; + harness.respondToRequest.mockImplementation(() => + Effect.fail( + new ProviderAdapterRequestError({ + provider: ProviderDriverKind.make("codex"), + method: "session/request_permission", + detail: "Approval response failed", + }), + ), + ); + + await Effect.runPromise( + harness.engine.dispatch({ + type: "thread.session.set", + commandId: CommandId.make("cmd-session-set-for-failed-approval-cancel"), + 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, + }), + ); + + await Effect.runPromise( + harness.engine.dispatch({ + type: "thread.approval.respond", + commandId: CommandId.make("cmd-approval-cancel-failed"), + threadId: ThreadId.make("thread-1"), + requestId: asApprovalRequestId("approval-request-1"), + decision: "cancel", + createdAt: now, + }), + ); + + await waitFor(async () => { + const readModel = await harness.readModel(); + const thread = readModel.threads.find((entry) => entry.id === ThreadId.make("thread-1")); + return ( + thread?.activities.some( + (activity) => activity.kind === "provider.approval.respond.failed", + ) ?? false + ); + }); + await harness.drain(); + + expect(harness.respondToRequest).toHaveBeenCalledTimes(1); + expect(harness.interruptTurn).not.toHaveBeenCalled(); }); it("reacts to thread.user-input.respond by forwarding structured user input answers", async () => { diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts index ff63979717..f1afd492ec 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts @@ -1227,13 +1227,14 @@ const make = Effect.gen(function* () { }); } - yield* providerService + const responseSucceeded = yield* providerService .respondToRequest({ threadId: event.payload.threadId, requestId: event.payload.requestId, decision: event.payload.decision, }) .pipe( + Effect.as(true), Effect.catchCause((cause) => appendProviderFailureActivity({ threadId: event.payload.threadId, @@ -1245,9 +1246,13 @@ const make = Effect.gen(function* () { turnId: null, createdAt: event.payload.createdAt, requestId: event.payload.requestId, - }), + }).pipe(Effect.as(false)), ), ); + + if (responseSucceeded && event.payload.decision === "cancel") { + yield* providerService.interruptTurn({ threadId: event.payload.threadId }); + } }); const processUserInputResponseRequested = Effect.fn("processUserInputResponseRequested")(