Skip to content

Commit dcd4ca7

Browse files
committed
fix(server): withdraw a stopped turn's pending approvals
When a provider turn ended, only its pending user-input requests were withdrawn. Approvals stayed pending after Stop, kept the thread's pending-request summary set and blocked settling. Interrupted and cancelled turns now withdraw their approvals too, marked not resumable. Completed and failed turns keep them, since a root turn can finish while its subagents still wait on approvals filed under it.
1 parent e85539a commit dcd4ca7

5 files changed

Lines changed: 237 additions & 50 deletions

File tree

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,129 @@ it.effect("SQL projection keeps background thread updates out of updatedAt", ()
782782
backgroundThreadUpdatesKeepTimestamp.pipe(Effect.provide(layerTest)),
783783
);
784784

785+
const pendingNativeRequestsStayWithTheirTurn = Effect.gen(function* () {
786+
const store = yield* ProjectionStore.ProjectionStoreV2;
787+
const now = yield* DateTime.now;
788+
const suffix = "pending-native-requests";
789+
const threadId = yield* addRolledBackRecoveryCandidate(suffix);
790+
const providerThreadId = ProviderThreadId.make(`provider-thread:${suffix}`);
791+
const turnId = ProviderTurnId.make(`turn:${suffix}`);
792+
const otherTurnId = ProviderTurnId.make(`turn:${suffix}:other`);
793+
const live = {
794+
type: "live" as const,
795+
providerSessionId: ProviderSessionId.make(`session:${suffix}`),
796+
};
797+
const requests = [
798+
["approval", turnId, "command", "pending", live],
799+
["other-turn-approval", otherTurnId, "command", "pending", live],
800+
["session-approval", null, "command", "pending", live],
801+
["resolved-approval", turnId, "command", "resolved", live],
802+
["dynamic-tool", turnId, "dynamic_tool_call", "pending", live],
803+
["question", turnId, "user_input", "pending", live],
804+
["message-question", turnId, "user_input", "pending", { type: "message" as const }],
805+
] as const;
806+
for (const [
807+
ordinal,
808+
[name, providerTurnId, kind, status, responseCapability],
809+
] of requests.entries()) {
810+
const requestId = RuntimeRequestId.make(`request:${suffix}:${name}`);
811+
const nodeId = NodeId.make(`node:${suffix}:${name}`);
812+
const approval = kind === "command";
813+
yield* store.apply({
814+
id: EventId.make(`event:${suffix}:${name}:request`),
815+
type: "runtime-request.updated",
816+
threadId,
817+
nodeId,
818+
occurredAt: now,
819+
payload: {
820+
id: requestId,
821+
nodeId,
822+
providerTurnId,
823+
nativeRequestRef: null,
824+
kind,
825+
status,
826+
responseCapability,
827+
createdAt: now,
828+
resolvedAt: status === "pending" ? null : now,
829+
},
830+
});
831+
yield* store.apply({
832+
id: EventId.make(`event:${suffix}:${name}:node`),
833+
type: "node.updated",
834+
threadId,
835+
nodeId,
836+
occurredAt: now,
837+
payload: {
838+
id: nodeId,
839+
threadId,
840+
runId: null,
841+
parentNodeId: null,
842+
rootNodeId: nodeId,
843+
kind: approval ? "approval_request" : "user_input_request",
844+
status: "waiting",
845+
countsForRun: false,
846+
providerThreadId,
847+
providerTurnId,
848+
nativeItemRef: null,
849+
runtimeRequestId: requestId,
850+
checkpointScopeId: null,
851+
startedAt: now,
852+
completedAt: null,
853+
},
854+
});
855+
if (kind === "dynamic_tool_call") continue;
856+
yield* store.apply({
857+
id: EventId.make(`event:${suffix}:${name}:item`),
858+
type: "turn-item.updated",
859+
threadId,
860+
nodeId,
861+
occurredAt: now,
862+
payload: {
863+
id: TurnItemId.make(`item:${suffix}:${name}`),
864+
threadId,
865+
runId: null,
866+
nodeId,
867+
providerThreadId,
868+
providerTurnId,
869+
nativeItemRef: null,
870+
parentItemId: null,
871+
ordinal,
872+
status: "waiting",
873+
title: null,
874+
startedAt: now,
875+
completedAt: null,
876+
updatedAt: now,
877+
...(approval
878+
? { type: "approval_request" as const, requestId, requestKind: "command" as const }
879+
: { type: "user_input_request" as const, requestId, questions: [] }),
880+
},
881+
});
882+
}
883+
884+
const names = (records: ReadonlyArray<{ readonly id: string }>) =>
885+
records.map((record) => record.id.slice(record.id.lastIndexOf(":") + 1)).toSorted();
886+
const withoutApprovals = yield* store.getPendingNativeRequests(threadId, turnId, {
887+
includeApprovals: false,
888+
});
889+
assert.deepEqual(names(withoutApprovals.runtimeRequests), ["question"]);
890+
assert.deepEqual(names(withoutApprovals.nodes), ["question"]);
891+
assert.deepEqual(names(withoutApprovals.turnItems), ["question"]);
892+
const withApprovals = yield* store.getPendingNativeRequests(threadId, turnId, {
893+
includeApprovals: true,
894+
});
895+
assert.deepEqual(names(withApprovals.runtimeRequests), ["approval", "question"]);
896+
assert.deepEqual(names(withApprovals.nodes), ["approval", "question"]);
897+
assert.deepEqual(names(withApprovals.turnItems), ["approval", "question"]);
898+
});
899+
900+
it.effect("memory pending native requests: approvals are opt-in and stay with their turn", () =>
901+
pendingNativeRequestsStayWithTheirTurn.pipe(Effect.provide(ProjectionStore.layerMemory)),
902+
);
903+
904+
it.effect("SQL pending native requests: approvals are opt-in and stay with their turn", () =>
905+
pendingNativeRequestsStayWithTheirTurn.pipe(Effect.provide(layerTest)),
906+
);
907+
785908
it.effect("memory recovery selection ignores unfinished items from rolled-back runs", () =>
786909
Effect.gen(function* () {
787910
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
OrchestrationV2ExecutionNode,
2222
OrchestrationV2PlanArtifact,
2323
OrchestrationV2PendingRuntimeRequestSummary,
24+
OrchestrationV2RuntimeRequest,
2425
PlanId,
2526
OrchestrationV2ConversationMessage,
2627
OrchestrationV2DomainEvent,
@@ -67,6 +68,7 @@ import {
6768
TurnItemId,
6869
NodeId,
6970
ProviderInstanceId,
71+
ProviderRequestKind,
7072
} from "@t3tools/contracts";
7173
import {
7274
createOrchestrationV2TurnItemVisibility,
@@ -297,11 +299,22 @@ export type ProjectionRuntimeRecoveryState = Pick<
297299
| "turnItems"
298300
>;
299301

300-
export type ProjectionPendingUserInputs = Pick<
302+
export type ProjectionPendingNativeRequests = Pick<
301303
OrchestrationV2ThreadProjection,
302304
"runtimeRequests" | "nodes" | "turnItems"
303305
>;
304306

307+
const isApprovalRequestKind = Schema.is(ProviderRequestKind);
308+
309+
function isPendingNativeRequest(
310+
request: OrchestrationV2RuntimeRequest,
311+
includeApprovals: boolean,
312+
): boolean {
313+
return request.kind === "user_input"
314+
? request.responseCapability.type !== "message"
315+
: includeApprovals && isApprovalRequestKind(request.kind);
316+
}
317+
305318
export type ProjectionThreadProviderContext = Pick<
306319
OrchestrationV2ThreadProjection,
307320
"thread" | "providerSessions" | "providerThreads"
@@ -486,10 +499,11 @@ export interface ProjectionStoreV2Shape {
486499
readonly getRuntimeRecoveryProjection: (
487500
threadId: ThreadId,
488501
) => Effect.Effect<ProjectionRuntimeRecoveryState, ProjectionStoreV2Error>;
489-
readonly getPendingNativeUserInputs: (
502+
readonly getPendingNativeRequests: (
490503
threadId: ThreadId,
491504
providerTurnId: ProviderTurnId,
492-
) => Effect.Effect<ProjectionPendingUserInputs, ProjectionStoreV2Error>;
505+
options: { readonly includeApprovals: boolean },
506+
) => Effect.Effect<ProjectionPendingNativeRequests, ProjectionStoreV2Error>;
493507
readonly getPlan: (
494508
threadId: ThreadId,
495509
planId: PlanId,
@@ -4498,22 +4512,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
44984512
? cause
44994513
: new ProjectionStoreReadError({ threadId, cause });
45004514

4501-
const getPendingNativeUserInputs: ProjectionStoreV2Shape["getPendingNativeUserInputs"] = (
4515+
const getPendingNativeRequests: ProjectionStoreV2Shape["getPendingNativeRequests"] = (
45024516
threadId,
45034517
providerTurnId,
4518+
{ includeApprovals },
45044519
) =>
45054520
sql
45064521
.withTransaction(
45074522
Effect.gen(function* () {
45084523
const rows = yield* sql<PayloadRow>`
45094524
SELECT payload_json FROM orchestration_v2_projection_runtime_requests
45104525
WHERE thread_id = ${threadId} AND provider_turn_id = ${providerTurnId}
4511-
AND kind = 'user_input' AND status = 'pending'
4526+
AND status = 'pending'
45124527
`;
45134528
const runtimeRequests = (yield* decodeRows(
45144529
decodeRuntimeRequestPayload,
45154530
threadId,
4516-
)(rows)).filter((request) => request.responseCapability.type !== "message");
4531+
)(rows)).filter((request) => isPendingNativeRequest(request, includeApprovals));
45174532
if (runtimeRequests.length === 0) return { runtimeRequests, nodes: [], turnItems: [] };
45184533
const nodeRows = yield* sql<PayloadRow>`
45194534
SELECT payload_json FROM orchestration_v2_projection_nodes
@@ -4525,14 +4540,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
45254540
const itemRows = yield* sql<PayloadRow>`
45264541
SELECT payload_json FROM orchestration_v2_projection_turn_items
45274542
WHERE thread_id = ${threadId} AND provider_turn_id = ${providerTurnId}
4528-
AND type = 'user_input_request'
4543+
AND type IN ('user_input_request', 'approval_request')
45294544
`;
45304545
const requestIds = new Set(runtimeRequests.map((request) => request.id));
45314546
return {
45324547
runtimeRequests,
45334548
nodes: yield* decodeRows(decodeNodePayload, threadId)(nodeRows),
45344549
turnItems: (yield* decodeRows(decodeTurnItemPayload, threadId)(itemRows)).filter(
4535-
(item) => item.type === "user_input_request" && requestIds.has(item.requestId),
4550+
(item) =>
4551+
(item.type === "user_input_request" || item.type === "approval_request") &&
4552+
requestIds.has(item.requestId),
45364553
),
45374554
};
45384555
}),
@@ -6316,7 +6333,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
63166333
getCheckpointRollbackContext,
63176334
getRunMessage,
63186335
canStartQueuedRun,
6319-
getPendingNativeUserInputs,
6336+
getPendingNativeRequests,
63206337
hasUnpairedRunInterruptRequest,
63216338
isNodeStored,
63226339
getMessageCount,
@@ -6556,28 +6573,31 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
65566573
.map((projection) => projection.thread.id);
65576574
}),
65586575
getUnreadableThreadIds: () => Effect.succeed([]),
6559-
getPendingNativeUserInputs: Effect.fn("ProjectionStore.getPendingNativeUserInputs")(
6560-
function* (threadId, providerTurnId) {
6561-
const projection = (yield* Ref.get(replayState)).projections.get(threadId);
6562-
if (projection === undefined) return { runtimeRequests: [], nodes: [], turnItems: [] };
6563-
const runtimeRequests = projection.runtimeRequests.filter(
6564-
(request) =>
6565-
request.providerTurnId === providerTurnId &&
6566-
request.kind === "user_input" &&
6567-
request.status === "pending" &&
6568-
request.responseCapability.type !== "message",
6569-
);
6570-
const requestIds = new Set(runtimeRequests.map((request) => request.id));
6571-
const nodeIds = new Set(runtimeRequests.map((request) => request.nodeId));
6572-
return {
6573-
runtimeRequests,
6574-
nodes: projection.nodes.filter((node) => nodeIds.has(node.id)),
6575-
turnItems: projection.turnItems.filter(
6576-
(item) => item.type === "user_input_request" && requestIds.has(item.requestId),
6577-
),
6578-
};
6579-
},
6580-
),
6576+
getPendingNativeRequests: Effect.fn("ProjectionStore.getPendingNativeRequests")(function* (
6577+
threadId,
6578+
providerTurnId,
6579+
{ includeApprovals },
6580+
) {
6581+
const projection = (yield* Ref.get(replayState)).projections.get(threadId);
6582+
if (projection === undefined) return { runtimeRequests: [], nodes: [], turnItems: [] };
6583+
const runtimeRequests = projection.runtimeRequests.filter(
6584+
(request) =>
6585+
request.providerTurnId === providerTurnId &&
6586+
request.status === "pending" &&
6587+
isPendingNativeRequest(request, includeApprovals),
6588+
);
6589+
const requestIds = new Set(runtimeRequests.map((request) => request.id));
6590+
const nodeIds = new Set(runtimeRequests.map((request) => request.nodeId));
6591+
return {
6592+
runtimeRequests,
6593+
nodes: projection.nodes.filter((node) => nodeIds.has(node.id)),
6594+
turnItems: projection.turnItems.filter(
6595+
(item) =>
6596+
(item.type === "user_input_request" || item.type === "approval_request") &&
6597+
requestIds.has(item.requestId),
6598+
),
6599+
};
6600+
}),
65816601
getPlan: Effect.fn("ProjectionStore.getPlan")(function* (threadId, planId) {
65826602
const projection = (yield* Ref.get(replayState)).projections.get(threadId);
65836603
if (projection === undefined)

0 commit comments

Comments
 (0)