Skip to content

Commit e2265f1

Browse files
committed
fix(server): keep an approval answered during a stop from being cancelled
A stopped turn's pending approvals are read, then cancelled in a later write. An answer committed between the two was overwritten, because the sink only rechecked user-input questions before applying a cancellation. The recheck now covers approval requests as well: the cancellation is dropped unless the request is still pending, of the same kind, on the same turn.
1 parent 90f7837 commit e2265f1

2 files changed

Lines changed: 183 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
RuntimeRequestId,
1111
NodeId,
1212
type ProjectId,
13+
ProviderRequestKind,
1314
ThreadId,
1415
} from "@t3tools/contracts";
1516
import * as Context from "effect/Context";
@@ -275,8 +276,9 @@ const layerBase: Layer.Layer<
275276
);
276277
});
277278

278-
// A user can answer after terminal normalization reads the pending request.
279+
// A user can answer a question or approval after terminal normalization reads it pending.
279280
// Recheck inside the write transaction so stale cleanup cannot erase answers.
281+
const isApprovalRequestKind = Schema.is(ProviderRequestKind);
280282
const guardUserInputCancellations =
281283
/* This helper runs for individual orchestration events; the enclosing operation owns the span. */
282284
Effect.fnUntraced(function* (events: ReadonlyArray<OrchestrationV2DomainEvent>) {
@@ -285,7 +287,7 @@ const layerBase: Layer.Layer<
285287
for (const event of events) {
286288
if (
287289
event.type !== "runtime-request.updated" ||
288-
event.payload.kind !== "user_input" ||
290+
(event.payload.kind !== "user_input" && !isApprovalRequestKind(event.payload.kind)) ||
289291
event.payload.status !== "cancelled"
290292
)
291293
continue;
@@ -295,7 +297,7 @@ const layerBase: Layer.Layer<
295297
);
296298
if (
297299
current?.status !== "pending" ||
298-
current.kind !== "user_input" ||
300+
current.kind !== event.payload.kind ||
299301
current.providerTurnId !== event.payload.providerTurnId ||
300302
current.responseCapability.type === "message"
301303
) {
@@ -311,7 +313,8 @@ const layerBase: Layer.Layer<
311313
return event.payload.status !== "cancelled" || !staleNodes.has(event.payload.id);
312314
case "turn-item.updated":
313315
return (
314-
event.payload.type !== "user_input_request" ||
316+
(event.payload.type !== "user_input_request" &&
317+
event.payload.type !== "approval_request") ||
315318
event.payload.status !== "cancelled" ||
316319
!staleRequests.has(event.payload.requestId)
317320
);

‎apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,182 @@ layer("ProviderEventIngestorV2", (it) => {
13351335
),
13361336
);
13371337

1338+
it.effect.each(["interrupted", "cancelled"] as const)(
1339+
"preserves an approval resolved after terminal normalization reads it pending (%s)",
1340+
(status) =>
1341+
Effect.scoped(
1342+
Effect.gen(function* () {
1343+
const eventSink = yield* EventSink.EventSinkV2;
1344+
const eventStore = yield* EventStore.EventStoreV2;
1345+
const projections = yield* ProjectionStore.ProjectionStoreV2;
1346+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
1347+
const now = yield* DateTime.now;
1348+
const threadEvent = yield* threadCreatedEvent(now);
1349+
const threadId = threadEvent.threadId;
1350+
const providerSessionId = yield* idAllocator.allocate.providerSession({
1351+
providerInstanceId: modelSelection.instanceId,
1352+
threadId,
1353+
});
1354+
const providerThreadId = idAllocator.derive.providerThread({
1355+
driver: CODEX_DRIVER,
1356+
nativeThreadId: `${threadId}:approval-race`,
1357+
});
1358+
const providerTurnId = idAllocator.derive.providerTurn({
1359+
driver: CODEX_DRIVER,
1360+
nativeTurnId: `${threadId}:approval-race`,
1361+
});
1362+
const nodeId = NodeId.make(`${threadId}:approval`);
1363+
const request: OrchestrationV2RuntimeRequest = {
1364+
id: RuntimeRequestId.make(`${threadId}:approval`),
1365+
nodeId,
1366+
providerTurnId,
1367+
nativeRequestRef: null,
1368+
kind: "command",
1369+
status: "pending",
1370+
responseCapability: { type: "live", providerSessionId },
1371+
createdAt: now,
1372+
resolvedAt: null,
1373+
};
1374+
const node: OrchestrationV2ExecutionNode = {
1375+
id: nodeId,
1376+
threadId,
1377+
runId: null,
1378+
parentNodeId: null,
1379+
rootNodeId: nodeId,
1380+
kind: "approval_request",
1381+
status: "waiting",
1382+
countsForRun: false,
1383+
providerThreadId,
1384+
providerTurnId,
1385+
nativeItemRef: null,
1386+
runtimeRequestId: request.id,
1387+
checkpointScopeId: null,
1388+
startedAt: now,
1389+
completedAt: null,
1390+
};
1391+
const item: OrchestrationV2TurnItem = {
1392+
id: TurnItemId.make(`${threadId}:approval`),
1393+
threadId,
1394+
runId: null,
1395+
nodeId,
1396+
providerThreadId,
1397+
providerTurnId,
1398+
nativeItemRef: null,
1399+
parentItemId: null,
1400+
ordinal: 1,
1401+
status: "waiting",
1402+
title: null,
1403+
startedAt: now,
1404+
completedAt: null,
1405+
updatedAt: now,
1406+
type: "approval_request",
1407+
requestId: request.id,
1408+
requestKind: "command",
1409+
};
1410+
const seedEvents: Array<OrchestrationV2DomainEvent> = [threadEvent];
1411+
for (const payload of [
1412+
{ type: "runtime-request.updated" as const, payload: request },
1413+
{ type: "node.updated" as const, payload: node },
1414+
{ type: "turn-item.updated" as const, payload: item },
1415+
])
1416+
seedEvents.push({
1417+
id: yield* idAllocator.allocate.event({ threadId }),
1418+
threadId,
1419+
occurredAt: now,
1420+
...payload,
1421+
});
1422+
yield* eventSink.write({ events: seedEvents });
1423+
const normalized = yield* Deferred.make<void>();
1424+
const releaseTerminalWrite = yield* Deferred.make<void>();
1425+
const gatedSink = EventSink.EventSinkV2.of({
1426+
...eventSink,
1427+
write: (input) =>
1428+
Deferred.succeed(normalized, undefined).pipe(
1429+
Effect.andThen(Deferred.await(releaseTerminalWrite)),
1430+
Effect.andThen(eventSink.write(input)),
1431+
),
1432+
});
1433+
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2.pipe(
1434+
Effect.provide(Layer.fresh(ProviderEventIngestor.layer)),
1435+
Effect.provideService(EventSink.EventSinkV2, gatedSink),
1436+
);
1437+
const terminal = yield* ingestor
1438+
.ingestNormalized({
1439+
providerSessionId,
1440+
providerInstanceId: modelSelection.instanceId,
1441+
threadId,
1442+
event: {
1443+
type: "provider_turn.updated",
1444+
driver: CODEX_DRIVER,
1445+
providerTurn: {
1446+
id: providerTurnId,
1447+
providerThreadId,
1448+
nodeId,
1449+
runAttemptId: null,
1450+
nativeTurnRef: null,
1451+
ordinal: 1,
1452+
status,
1453+
startedAt: now,
1454+
completedAt: now,
1455+
},
1456+
},
1457+
})
1458+
.pipe(Effect.forkScoped);
1459+
yield* Deferred.await(normalized);
1460+
const responseEvents: Array<OrchestrationV2DomainEvent> = [];
1461+
for (const payload of [
1462+
{
1463+
type: "runtime-request.updated" as const,
1464+
payload: {
1465+
...request,
1466+
status: "resolved" as const,
1467+
decision: "accept" as const,
1468+
resolvedAt: now,
1469+
},
1470+
},
1471+
{
1472+
type: "node.updated" as const,
1473+
payload: { ...node, status: "completed" as const, completedAt: now },
1474+
},
1475+
{
1476+
type: "turn-item.updated" as const,
1477+
payload: { ...item, status: "completed" as const, completedAt: now },
1478+
},
1479+
])
1480+
responseEvents.push({
1481+
id: yield* idAllocator.allocate.event({ threadId }),
1482+
threadId,
1483+
occurredAt: now,
1484+
...payload,
1485+
});
1486+
yield* eventSink.write({ events: responseEvents });
1487+
yield* Deferred.succeed(releaseTerminalWrite, undefined);
1488+
const committedTerminal = yield* Fiber.join(terminal);
1489+
const projection = yield* projections.getThreadProjection(threadId);
1490+
const approved = projection.runtimeRequests.find((entry) => entry.id === request.id)!;
1491+
assert.equal(approved.status, "resolved");
1492+
assert.equal(approved.decision, "accept");
1493+
assert.equal(projection.nodes.find((entry) => entry.id === nodeId)!.status, "completed");
1494+
assert.equal(
1495+
projection.turnItems.find((entry) => entry.id === item.id)!.status,
1496+
"completed",
1497+
);
1498+
assert.deepEqual(
1499+
committedTerminal.map((stored) => stored.event.type),
1500+
["provider-turn.updated"],
1501+
);
1502+
const history = yield* eventStore.read({ threadId }).pipe(Stream.runCollect);
1503+
assert.isFalse(
1504+
history.some(
1505+
(stored) =>
1506+
stored.event.type === "runtime-request.updated" &&
1507+
stored.event.payload.status === "cancelled",
1508+
),
1509+
);
1510+
}),
1511+
),
1512+
);
1513+
13381514
it.effect("persists a failed provider terminal as one expected error item", () =>
13391515
Effect.gen(function* () {
13401516
const now = yield* DateTime.now;

0 commit comments

Comments
 (0)