From 84a2ff6dc6b029300c193f4bc3cbc1304887bd60 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 25 Oct 2026 06:10:47 +0000 Subject: [PATCH 1/5] test(server): reproduce withdrawn Claude approval staying pending (#24690) A Claude PermissionRequest hook can answer before the user does; the CLI then cancels the can_use_tool control request and the SDK aborts the callback signal. The adapter resolves the callback but never emits a terminal runtime request update, so the approval card stays pending. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 9dc129c7aa0d..2cd53e60b81c 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -7708,6 +7708,59 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + // regression: #24690 a PermissionRequest hook that answers first withdraws the approval card + it.effect("cancels a pending approval when Claude withdraws its permission request", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + const bashToolUseId = "toolu_01WithdrawnApprovalBash"; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-withdrawn-approval"), + text: "Clean up.", + attachments: [], + runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY, + }), + ); + const controller = new AbortController(); + const permission = yield* Effect.promise(() => + harness.getOpenedOptions()!.canUseTool!( + "Bash", + { command: "rm -f $F/*" }, + { + signal: controller.signal, + toolUseID: bashToolUseId, + requestId: "request-withdrawn-approval", + }, + ), + ).pipe(Effect.forkScoped); + const pending = yield* Queue.take(harness.runtimeRequestReceipts); + assert.equal(pending.runtimeRequest.status, "pending"); + + // The CLI sends control_cancel_request, which the SDK turns into this abort. + controller.abort(); + assert.notEqual((yield* Fiber.join(permission))?.behavior, "allow"); + + const latest = harness.events.findLast( + (event) => + event.type === "runtime_request.updated" && + event.runtimeRequest.id === pending.runtimeRequest.id, + ); + assert.equal( + latest?.type === "runtime_request.updated" ? latest.runtimeRequest.status : undefined, + "cancelled", + ); + }).pipe( + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ), + ); + it.effect("releases output held for a prompt echo when the stream ends", () => Effect.scoped( Effect.gen(function* () { From 5b99f0b03f20cfeab3b968130986f1de7cec449e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 25 Oct 2026 06:17:15 +0000 Subject: [PATCH 2/5] fix(server): a Claude approval withdrawn by a hook no longer stays pending (#24690) When a PermissionRequest hook answers first, the CLI cancels the can_use_tool request and the SDK aborts the callback signal. The adapter resolved the callback but left the request, node and turn item pending, so the card stayed open and answering it failed with "No pending Claude runtime request". The abort path now claims the decision and emits the same cancelled shape the ingestor uses for dismissed native requests. A user answer completes the decision first, so it keeps its resolution. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 79 +++++++++++++++++-- .../Adapters/ClaudeAdapterV2.ts | 33 ++++++++ 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 2cd53e60b81c..e36de6a627b5 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -7744,15 +7744,82 @@ describe("ClaudeAdapterV2 background wake turns", () => { controller.abort(); assert.notEqual((yield* Fiber.join(permission))?.behavior, "allow"); - const latest = harness.events.findLast( + const cancelled = yield* Queue.take(harness.runtimeRequestReceipts); + assert.equal(cancelled.runtimeRequest.id, pending.runtimeRequest.id); + assert.equal(cancelled.runtimeRequest.status, "cancelled"); + assert.isNotNull(cancelled.runtimeRequest.resolvedAt); + yield* harness.until(() => + harness.events.some( + (event) => + event.type === "turn_item.updated" && + event.turnItem.nodeId === pending.runtimeRequest.nodeId && + event.turnItem.status === "cancelled", + ), + ); + const node = harness.events.findLast( (event) => - event.type === "runtime_request.updated" && - event.runtimeRequest.id === pending.runtimeRequest.id, + event.type === "node.updated" && event.node.id === pending.runtimeRequest.nodeId, ); - assert.equal( - latest?.type === "runtime_request.updated" ? latest.runtimeRequest.status : undefined, - "cancelled", + assert.equal(node?.type === "node.updated" ? node.node.status : undefined, "cancelled"); + }).pipe( + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ), + ); + + it.effect("keeps a user-cancelled approval resolved when Claude aborts afterwards", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-user-cancelled-approval"), + text: "Clean up.", + attachments: [], + runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY, + }), ); + const controller = new AbortController(); + const permission = yield* Effect.promise(() => + harness.getOpenedOptions()!.canUseTool!( + "Bash", + { command: "rm -f $F/*" }, + { + signal: controller.signal, + toolUseID: "toolu_01UserCancelledApprovalBash", + requestId: "request-user-cancelled-approval", + }, + ), + ).pipe(Effect.forkScoped); + const pending = yield* Queue.take(harness.runtimeRequestReceipts); + yield* harness.runtime.respondToRuntimeRequest({ + requestId: pending.runtimeRequest.id, + decision: "cancel", + }); + // The SDK aborts the signal when an interrupting deny ends the turn, racing the answer. + controller.abort(); + assert.equal((yield* Fiber.join(permission))?.behavior, "deny"); + + yield* Effect.promise(() => + harness.getOpenedOptions()!.canUseTool!( + "Bash", + { command: "rm -f $G/*" }, + { + signal: new AbortController().signal, + toolUseID: "toolu_01UserCancelledApprovalNext", + requestId: "request-user-cancelled-approval-next", + }, + ), + ).pipe(Effect.forkScoped); + // Events are ordered, so a withdrawal update for the answered request would arrive first. + const next = yield* Queue.take(harness.runtimeRequestReceipts); + assert.notEqual(next.runtimeRequest.id, pending.runtimeRequest.id); + assert.equal(next.runtimeRequest.status, "pending"); }).pipe( Effect.provide( Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 9dea43bf9e71..4d7823d6f8ee 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -9092,6 +9092,39 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }), ), ); + // An aborted signal means Claude withdrew the request, e.g. a PermissionRequest hook answered + // first. Claiming the decision tells that apart from a user answer the orchestrator already resolved. + if ( + callbackOptions.signal.aborted && + (yield* Deferred.succeed(decision, resolvedDecision)) + ) { + const now = yield* DateTime.now; + yield* Effect.all( + [ + emitProviderEvent({ + type: "node.updated", + driver: CLAUDE_PROVIDER, + node: { ...artifacts.node, status: "cancelled", completedAt: now }, + }), + emitProviderEvent({ + type: "runtime_request.updated", + driver: CLAUDE_PROVIDER, + runtimeRequest: { ...artifacts.request, status: "cancelled", resolvedAt: now }, + }), + emitProviderEvent({ + type: "turn_item.updated", + driver: CLAUDE_PROVIDER, + turnItem: { + ...artifacts.turnItem, + status: "cancelled", + completedAt: now, + updatedAt: now, + }, + }), + ], + { concurrency: 1 }, + ); + } return permissionResultFromDecision({ toolName, From 8625d7af75e47f140be2bfbb9c3cd462ae6766bc Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 25 Oct 2026 06:28:57 +0000 Subject: [PATCH 3/5] fix(server): a withdrawn approval no longer erases a committed answer (#24690) An approval answer commits before its outbox delivery reaches the provider. If Claude withdraws the request in that gap, the adapter's cancellation events overwrote the resolved request, losing the decision and feedback and flipping the node and item outcome. The EventSink cancellation guard now covers approvals too, and checks node and item cancellations against their request, since the adapter writes each in its own batch. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/EventSink.ts | 65 +++++--- .../ProviderEventIngestor.test.ts | 141 ++++++++++++++++++ .../orchestration-v2/ProviderEventIngestor.ts | 6 +- 3 files changed, 189 insertions(+), 23 deletions(-) diff --git a/apps/server/src/orchestration-v2/EventSink.ts b/apps/server/src/orchestration-v2/EventSink.ts index 939b80475782..f750c188f044 100644 --- a/apps/server/src/orchestration-v2/EventSink.ts +++ b/apps/server/src/orchestration-v2/EventSink.ts @@ -73,7 +73,7 @@ export interface EventSinkV2Shape { OrchestrationV2ProviderThread, "driver" | "providerInstanceId" | "appThreadId" | "nativeThreadRef" >; - readonly guardPendingUserInputCancellations?: boolean; + readonly guardPendingRuntimeRequestCancellations?: boolean; readonly commandId?: CommandId; readonly events: ReadonlyArray; }) => Effect.Effect, EventSinkV2Error>; @@ -87,13 +87,13 @@ export interface EventSinkV2Shape { OrchestrationV2ProviderThread, "driver" | "providerInstanceId" | "appThreadId" | "nativeThreadRef" >; - readonly guardPendingUserInputCancellations?: boolean; + readonly guardPendingRuntimeRequestCancellations?: boolean; readonly commandId?: CommandId; readonly events: ReadonlyArray; readonly effects: ReadonlyArray; }) => Effect.Effect, EventSinkV2Error>; readonly writeIfRunCurrent: (input: { - readonly guardPendingUserInputCancellations?: boolean; + readonly guardPendingRuntimeRequestCancellations?: boolean; readonly commandId?: CommandId; readonly threadId: ThreadId; readonly runId: RunId; @@ -110,7 +110,7 @@ export interface EventSinkV2Shape { >; /** Commits only if the expected attempt and ordinal still own the provider thread, protecting newer attempts. */ readonly writeIfProviderThreadOwner: (input: { - readonly guardPendingUserInputCancellations?: boolean; + readonly guardPendingRuntimeRequestCancellations?: boolean; readonly commandId?: CommandId; readonly providerThreadId: ProviderThreadId; readonly runId: RunId; @@ -278,19 +278,42 @@ const layerBase: Layer.Layer< ); }); - // A user can answer after terminal normalization reads the pending request. + // A user can answer after terminal normalization reads the pending request, and an approval + // answer commits before its outbox delivery reaches a provider that may withdraw the request. // Recheck inside the write transaction so stale cleanup cannot erase answers. - const guardUserInputCancellations = + const guardRuntimeRequestCancellations = /* This helper runs for individual orchestration events; the enclosing operation owns the span. */ Effect.fnUntraced(function* (events: ReadonlyArray) { const staleRequests = new Set(); const staleNodes = new Set(); for (const event of events) { + // Providers can withdraw the node and item in writes of their own, so check each against its request. if ( - event.type !== "runtime-request.updated" || - event.payload.kind !== "user_input" || - event.payload.status !== "cancelled" - ) + event.type === "node.updated" && + event.payload.status === "cancelled" && + event.payload.runtimeRequestId !== null + ) { + const current = yield* projectionStore.getRuntimeRequest( + event.threadId, + event.payload.runtimeRequestId, + ); + if (current?.status === "resolved") staleNodes.add(event.payload.id); + continue; + } + if ( + event.type === "turn-item.updated" && + (event.payload.type === "user_input_request" || + event.payload.type === "approval_request") && + event.payload.status === "cancelled" + ) { + const current = yield* projectionStore.getRuntimeRequest( + event.threadId, + event.payload.requestId, + ); + if (current?.status === "resolved") staleRequests.add(event.payload.requestId); + continue; + } + if (event.type !== "runtime-request.updated" || event.payload.status !== "cancelled") continue; const current = yield* projectionStore.getRuntimeRequest( event.threadId, @@ -298,9 +321,10 @@ const layerBase: Layer.Layer< ); if ( current?.status !== "pending" || - current.kind !== "user_input" || - current.providerTurnId !== event.payload.providerTurnId || - current.responseCapability.type === "message" + (event.payload.kind === "user_input" && + (current.kind !== "user_input" || + current.providerTurnId !== event.payload.providerTurnId || + current.responseCapability.type === "message")) ) { staleRequests.add(event.payload.id); staleNodes.add(event.payload.nodeId); @@ -314,7 +338,8 @@ const layerBase: Layer.Layer< return event.payload.status !== "cancelled" || !staleNodes.has(event.payload.id); case "turn-item.updated": return ( - event.payload.type !== "user_input_request" || + (event.payload.type !== "user_input_request" && + event.payload.type !== "approval_request") || event.payload.status !== "cancelled" || !staleRequests.has(event.payload.requestId) ); @@ -430,8 +455,8 @@ const layerBase: Layer.Layer< ) : input.events; const normalized = yield* normalizeEvents( - input.guardPendingUserInputCancellations === true - ? yield* guardUserInputCancellations(events) + input.guardPendingRuntimeRequestCancellations === true + ? yield* guardRuntimeRequestCancellations(events) : events, ); const committed = yield* eventStore.append({ @@ -489,8 +514,8 @@ const layerBase: Layer.Layer< } const normalized = yield* normalizeEvents( - input.guardPendingUserInputCancellations === true - ? yield* guardUserInputCancellations(input.events) + input.guardPendingRuntimeRequestCancellations === true + ? yield* guardRuntimeRequestCancellations(input.events) : input.events, ); const storedEvents = yield* eventStore.append({ @@ -554,8 +579,8 @@ const layerBase: Layer.Layer< } const normalized = yield* normalizeEvents( - input.guardPendingUserInputCancellations === true - ? yield* guardUserInputCancellations(input.events) + input.guardPendingRuntimeRequestCancellations === true + ? yield* guardRuntimeRequestCancellations(input.events) : input.events, ); const storedEvents = yield* eventStore.append({ diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 3fecc1c7d2d6..5d012dd34277 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -468,6 +468,147 @@ layer("ProviderEventIngestorV2", (it) => { }), ); + // regression: #24690 an answer commits before its outbox delivery reaches the provider + it.effect.each([ + { decision: "decline", feedback: "Use a safer command." }, + { decision: "accept" }, + ] as const)("keeps a committed $decision when the provider withdraws the approval", (answer) => + Effect.gen(function* () { + const now = yield* DateTime.now; + const eventSink = yield* EventSink.EventSinkV2; + const projectionStore = yield* ProjectionStore.ProjectionStoreV2; + const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2; + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const threadEvent = yield* threadCreatedEvent(now); + const threadId = threadEvent.threadId; + yield* eventSink.write({ events: [threadEvent] }); + const providerSessionId = yield* idAllocator.allocate.providerSession({ + providerInstanceId: modelSelection.instanceId, + threadId, + }); + const nodeId = NodeId.make(`${threadId}:approval`); + const request: OrchestrationV2RuntimeRequest = { + id: RuntimeRequestId.make(`${threadId}:approval`), + nodeId, + providerTurnId: null, + nativeRequestRef: null, + kind: "command", + status: "pending", + responseCapability: { type: "live", providerSessionId, supportsFeedback: true }, + createdAt: now, + resolvedAt: null, + }; + const node: OrchestrationV2ExecutionNode = { + id: nodeId, + threadId, + runId: null, + parentNodeId: null, + rootNodeId: nodeId, + kind: "approval_request", + status: "waiting", + countsForRun: false, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + runtimeRequestId: request.id, + checkpointScopeId: null, + startedAt: now, + completedAt: null, + }; + const item: OrchestrationV2TurnItem = { + id: TurnItemId.make(`${threadId}:approval`), + threadId, + runId: null, + nodeId, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 0, + status: "waiting", + title: null, + startedAt: now, + completedAt: null, + updatedAt: now, + type: "approval_request", + requestId: request.id, + requestKind: "command", + }; + const ingest = (status: "waiting" | "cancelled") => + Effect.forEach( + [ + { + type: "node.updated" as const, + driver: CODEX_DRIVER, + node: status === "waiting" ? node : { ...node, status, completedAt: now }, + }, + { + type: "runtime_request.updated" as const, + driver: CODEX_DRIVER, + runtimeRequest: + status === "waiting" ? request : { ...request, status, resolvedAt: now }, + }, + { + type: "turn_item.updated" as const, + driver: CODEX_DRIVER, + turnItem: status === "waiting" ? item : { ...item, status, completedAt: now }, + }, + ], + (event) => + ingestor.ingestNormalized({ + providerSessionId, + providerInstanceId: modelSelection.instanceId, + threadId, + event, + }), + { concurrency: 1 }, + ); + yield* ingest("waiting"); + + // The orchestrator's response transaction; its outbox delivery never runs here. + const outcome = answer.decision === "accept" ? "completed" : "cancelled"; + const resolved = { ...request, status: "resolved" as const, resolvedAt: now, ...answer }; + const answeredNode: OrchestrationV2ExecutionNode = { + ...node, + status: outcome, + completedAt: now, + }; + const answeredItem: OrchestrationV2TurnItem = { ...item, status: outcome, completedAt: now }; + const base = { threadId, nodeId, occurredAt: now }; + yield* eventSink.write({ + events: [ + { + ...base, + id: yield* idAllocator.allocate.event({ threadId }), + type: "runtime-request.updated", + payload: resolved, + }, + { + ...base, + id: yield* idAllocator.allocate.event({ threadId }), + type: "node.updated", + payload: answeredNode, + }, + { + ...base, + id: yield* idAllocator.allocate.event({ threadId }), + type: "turn-item.updated", + payload: answeredItem, + }, + ], + }); + + yield* ingest("cancelled"); + const projection = yield* projectionStore.getThreadProjection(threadId); + const stored = projection.runtimeRequests.find((entry) => entry.id === request.id); + assert.equal(stored?.status, "resolved"); + assert.equal(stored?.decision, answer.decision); + assert.equal(stored?.feedback, "feedback" in answer ? answer.feedback : undefined); + assert.equal(projection.nodes.find((entry) => entry.id === nodeId)?.status, outcome); + assert.equal(projection.turnItems.find((entry) => entry.id === item.id)?.status, outcome); + }), + ); + it.effect("rolls back runs a provider rewound off its active branch", () => Effect.gen(function* () { const now = yield* DateTime.now; diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index 480c7c6c738e..05819a6ce0fd 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -761,7 +761,7 @@ export const layer: Layer.Layer< if (input.writeIfProviderThreadOwner !== undefined) { const ownerResult = yield* eventSink .writeIfProviderThreadOwner({ - guardPendingUserInputCancellations: true, + guardPendingRuntimeRequestCancellations: true, ...(input.commandId === undefined ? {} : { commandId: input.commandId }), ...input.writeIfProviderThreadOwner, events, @@ -772,7 +772,7 @@ export const layer: Layer.Layer< if (input.writeIfRunCurrent === undefined) { return yield* eventSink .write({ - guardPendingUserInputCancellations: true, + guardPendingRuntimeRequestCancellations: true, ...(input.commandId === undefined ? {} : { commandId: input.commandId }), events, }) @@ -780,7 +780,7 @@ export const layer: Layer.Layer< } const result = yield* eventSink .writeIfRunCurrent({ - guardPendingUserInputCancellations: true, + guardPendingRuntimeRequestCancellations: true, ...(input.commandId === undefined ? {} : { commandId: input.commandId }), threadId: input.threadId, ...input.writeIfRunCurrent, From 2fa638cc8c958a33d6696ec34945537163db97b1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 01:12:42 +0000 Subject: [PATCH 4/5] fix(server): approval withdrawals only yield to committed answers A provider can withdraw an approval it never surfaced to projection; drop its cancellation only when the request is already resolved. AI-assisted; verified with the tests and tools listed above. --- apps/server/src/orchestration-v2/EventSink.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/apps/server/src/orchestration-v2/EventSink.ts b/apps/server/src/orchestration-v2/EventSink.ts index 8e44f501d895..19c9d5a236f5 100644 --- a/apps/server/src/orchestration-v2/EventSink.ts +++ b/apps/server/src/orchestration-v2/EventSink.ts @@ -352,13 +352,14 @@ const layerBase: Layer.Layer< event.threadId, event.payload.id, ); - if ( - current?.status !== "pending" || - (event.payload.kind === "user_input" && - (current.kind !== "user_input" || + const stale = + event.payload.kind === "user_input" + ? current?.status !== "pending" || + current.kind !== "user_input" || current.providerTurnId !== event.payload.providerTurnId || - current.responseCapability.type === "message")) - ) { + current.responseCapability.type === "message" + : current?.status === "resolved"; + if (stale) { staleRequests.add(event.payload.id); staleNodes.add(event.payload.nodeId); } From 7589a2394e810468282b5e39ded06494258aaf7a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 01:49:35 +0000 Subject: [PATCH 5/5] test(server): an approval withdrawn before projection still cancels AI-assisted; verified with the tests and tools listed above. --- .../ProviderEventIngestor.test.ts | 78 +++++++++---------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 3665ee489e3e..bff0318f8d75 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -571,9 +571,10 @@ layer("ProviderEventIngestorV2", (it) => { // regression: #24690 an answer commits before its outbox delivery reaches the provider it.effect.each([ - { decision: "decline", feedback: "Use a safer command." }, - { decision: "accept" }, - ] as const)("keeps a committed $decision when the provider withdraws the approval", (answer) => + { name: "keeps a committed decline", answer: { decision: "decline", feedback: "No." } }, + { name: "keeps a committed accept", answer: { decision: "accept" } }, + { name: "cancels an approval withdrawn before it was projected", answer: null }, + ] as const)("$name when the provider withdraws the approval", ({ answer }) => Effect.gen(function* () { const now = yield* DateTime.now; const eventSink = yield* EventSink.EventSinkV2; @@ -664,47 +665,44 @@ layer("ProviderEventIngestorV2", (it) => { }), { concurrency: 1 }, ); - yield* ingest("waiting"); - - // The orchestrator's response transaction; its outbox delivery never runs here. - const outcome = answer.decision === "accept" ? "completed" : "cancelled"; - const resolved = { ...request, status: "resolved" as const, resolvedAt: now, ...answer }; - const answeredNode: OrchestrationV2ExecutionNode = { - ...node, - status: outcome, - completedAt: now, - }; - const answeredItem: OrchestrationV2TurnItem = { ...item, status: outcome, completedAt: now }; - const base = { threadId, nodeId, occurredAt: now }; - yield* eventSink.write({ - events: [ - { - ...base, - id: yield* idAllocator.allocate.event({ threadId }), - type: "runtime-request.updated", - payload: resolved, - }, - { - ...base, - id: yield* idAllocator.allocate.event({ threadId }), - type: "node.updated", - payload: answeredNode, - }, - { - ...base, - id: yield* idAllocator.allocate.event({ threadId }), - type: "turn-item.updated", - payload: answeredItem, - }, - ], - }); + const outcome = answer?.decision === "accept" ? "completed" : "cancelled"; + if (answer !== null) { + yield* ingest("waiting"); + // The orchestrator's response transaction; its outbox delivery never runs here. + const base = { threadId, nodeId, occurredAt: now }; + yield* eventSink.write({ + events: [ + { + ...base, + id: yield* idAllocator.allocate.event({ threadId }), + type: "runtime-request.updated", + payload: { ...request, status: "resolved", resolvedAt: now, ...answer }, + }, + { + ...base, + id: yield* idAllocator.allocate.event({ threadId }), + type: "node.updated", + payload: { ...node, status: outcome, completedAt: now }, + }, + { + ...base, + id: yield* idAllocator.allocate.event({ threadId }), + type: "turn-item.updated", + payload: { ...item, status: outcome, completedAt: now }, + }, + ], + }); + } yield* ingest("cancelled"); const projection = yield* projectionStore.getThreadProjection(threadId); const stored = projection.runtimeRequests.find((entry) => entry.id === request.id); - assert.equal(stored?.status, "resolved"); - assert.equal(stored?.decision, answer.decision); - assert.equal(stored?.feedback, "feedback" in answer ? answer.feedback : undefined); + assert.equal(stored?.status, answer === null ? "cancelled" : "resolved"); + assert.equal(stored?.decision, answer?.decision); + assert.equal( + stored?.feedback, + answer !== null && "feedback" in answer ? answer.feedback : undefined, + ); assert.equal(projection.nodes.find((entry) => entry.id === nodeId)?.status, outcome); assert.equal(projection.turnItems.find((entry) => entry.id === item.id)?.status, outcome); }),