Skip to content

Commit 190c7de

Browse files
fix(server): a session release during finalization still cancels the blocked tool
The finalization guard kept the tool running whenever its approval was no longer pending. A server-closed (not_resumable) approval proves the tool never started, so only an accepted approval, or one Claude withdrew with a live capability after a hook allowed the tool, now preserves it. The released approval's own outcome is still kept. AI-assisted; verified with the tests and tools listed above.
1 parent f2c8283 commit 190c7de

2 files changed

Lines changed: 72 additions & 25 deletions

File tree

‎apps/server/src/orchestration-v2/EventSink.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,14 @@ const layerBase: Layer.Layer<
359359
});
360360
});
361361

362-
// Run finalization cancels approvals it read as pending. One answered since then keeps
363-
// its decision, and the tool it unblocked is not marked as never started.
362+
// Run finalization cancels approvals it read as pending. One closed since then keeps
363+
// its outcome; if it let the tool run, the tool is not marked as never started.
364364
const guardApprovalCancellations =
365365
/* This helper runs for individual orchestration events; the enclosing operation owns the span. */
366366
Effect.fnUntraced(function* (events: ReadonlyArray<OrchestrationV2DomainEvent>) {
367367
const staleRequests = new Set<RuntimeRequestId>();
368368
const staleNodes = new Set<NodeId>();
369+
const startedToolParents = new Set<NodeId>();
369370
for (const event of events) {
370371
if (
371372
event.type !== "runtime-request.updated" ||
@@ -377,16 +378,23 @@ const layerBase: Layer.Layer<
377378
event.threadId,
378379
event.payload.id,
379380
);
380-
if (current?.status !== "pending") {
381-
staleRequests.add(event.payload.id);
382-
staleNodes.add(event.payload.nodeId);
381+
if (current?.status === "pending") continue;
382+
staleRequests.add(event.payload.id);
383+
staleNodes.add(event.payload.nodeId);
384+
// Accepted, or withdrawn by Claude after a hook allowed it. A server-closed
385+
// (not_resumable) approval still proves the tool never started.
386+
if (
387+
current?.status === "resolved" ||
388+
(current?.status === "cancelled" && current.responseCapability.type === "live")
389+
) {
390+
startedToolParents.add(event.payload.nodeId);
383391
}
384392
}
385393
if (staleRequests.size === 0) return events;
386394
for (const event of events) {
387395
if (
388396
event.type === "node.updated" &&
389-
staleNodes.has(event.payload.id) &&
397+
startedToolParents.has(event.payload.id) &&
390398
event.payload.parentNodeId !== null
391399
) {
392400
staleNodes.add(event.payload.parentNodeId);

‎apps/server/src/orchestration-v2/TerminalShellItems.integration.test.ts‎

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ it.effect.each([
4949
"approval",
5050
"dynamic approval",
5151
"accepted approval",
52+
"released approval",
5253
"withdrawn approval",
5354
"pending",
5455
"unlinked running",
@@ -318,26 +319,58 @@ it.effect.each([
318319
(item) => item.id === commandItem.id,
319320
);
320321
assert.ok(before);
321-
if (check === "accepted approval") {
322+
if (check === "accepted approval" || check === "released approval") {
323+
const eventSink = yield* EventSink.EventSinkV2;
322324
const stale = yield* orchestrator.getThreadProjection(threadId);
323-
yield* orchestrator.dispatch({
324-
type: "runtime-request.respond",
325-
commandId: CommandId.make("approve"),
326-
threadId,
327-
requestId,
328-
decision: "accept",
329-
});
330-
yield* worker.drain();
331-
// Finalization that read the approval before it was answered must not erase it.
332325
const request = stale.runtimeRequests.find((candidate) => candidate.id === requestId)!;
326+
const approvalNode = stale.nodes.find((node) => node.id === request.nodeId)!;
327+
const approvalItem = stale.turnItems.find(
328+
(item) => item.id === TurnItemId.make("item:approval"),
329+
)!;
333330
const cancelledAt = { status: "cancelled" as const, completedAt: now };
334331
const staleEvent = (id: string) => ({
335332
id: EventId.make(`stale-${id}`),
336333
threadId,
337334
runId: first.runId,
338335
occurredAt: now,
339336
});
340-
yield* (yield* EventSink.EventSinkV2).writeWithEffects({
337+
if (check === "accepted approval") {
338+
yield* orchestrator.dispatch({
339+
type: "runtime-request.respond",
340+
commandId: CommandId.make("approve"),
341+
threadId,
342+
requestId,
343+
decision: "accept",
344+
});
345+
yield* worker.drain();
346+
} else {
347+
yield* eventSink.write({
348+
events: [
349+
{
350+
...staleEvent("release-request"),
351+
type: "runtime-request.updated",
352+
payload: {
353+
...request,
354+
status: "cancelled",
355+
responseCapability: { type: "not_resumable", reason: "Session closed." },
356+
resolvedAt: now,
357+
},
358+
},
359+
{
360+
...staleEvent("release-node"),
361+
type: "node.updated",
362+
payload: { ...approvalNode, ...cancelledAt },
363+
},
364+
{
365+
...staleEvent("release-item"),
366+
type: "turn-item.updated",
367+
payload: { ...approvalItem, ...cancelledAt },
368+
},
369+
],
370+
});
371+
}
372+
// Finalization read the approval as pending before it was answered or released.
373+
yield* eventSink.writeWithEffects({
341374
guardPendingApprovalCancellations: true,
342375
effects: [],
343376
events: [
@@ -362,21 +395,27 @@ it.effect.each([
362395
{
363396
...staleEvent("approval-node"),
364397
type: "node.updated",
365-
payload: {
366-
...stale.nodes.find((node) => node.id === request.nodeId)!,
367-
...cancelledAt,
368-
},
398+
payload: { ...approvalNode, ...cancelledAt },
369399
},
370400
{
371401
...staleEvent("approval-item"),
372402
type: "turn-item.updated",
373-
payload: {
374-
...stale.turnItems.find((item) => item.id === TurnItemId.make("item:approval"))!,
375-
...cancelledAt,
376-
},
403+
payload: { ...approvalItem, ...cancelledAt },
377404
},
378405
],
379406
});
407+
if (check === "released approval") {
408+
const released = yield* orchestrator.getThreadProjection(threadId);
409+
assert.equal(
410+
released.turnItems.find((item) => item.id === commandItem.id)?.status,
411+
"cancelled",
412+
);
413+
assert.deepEqual(
414+
released.runtimeRequests.find((candidate) => candidate.id === requestId)
415+
?.responseCapability,
416+
{ type: "not_resumable", reason: "Session closed." },
417+
);
418+
}
380419
}
381420
if (check === "withdrawn approval") {
382421
// Claude withdraws the prompt when a PermissionRequest hook allows the tool, which then runs.

0 commit comments

Comments
 (0)