Skip to content

Commit 76da7ae

Browse files
fix(server): a failed start also cancels requests waiting in a subagent's thread
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 916e1e7 commit 76da7ae

2 files changed

Lines changed: 97 additions & 7 deletions

File tree

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,45 @@ export const layer: Layer.Layer<
215215
if (threadId === run.threadId || linkedChildThreadIds.has(threadId)) continue;
216216
const child = yield* projectionStore.getRuntimeRecoveryProjection(threadId).pipe(
217217
Effect.map(Option.some),
218-
Effect.catchTag("ProjectionStoreThreadNotFoundError", () =>
219-
Effect.succeed(Option.none()),
220-
),
218+
Effect.catchTags({
219+
ProjectionStoreThreadNotFoundError: () => Effect.succeed(Option.none()),
220+
}),
221221
);
222222
if (Option.isNone(child) || !isProviderNativeSubagentThread(child.value.thread)) continue;
223223
linkedChildThreadIds.add(threadId);
224224
// A child thread has no runs, so the recovery read leaves out its
225-
// streaming replies; read them by thread instead.
226-
const { messages } = yield* projectionStore.getThreadRecords(threadId, ["messages"], {
227-
messageRoles: ["assistant"],
225+
// streaming replies and its pending requests' nodes and items; read
226+
// them by thread instead.
227+
const records = yield* projectionStore.getThreadRecords(
228+
threadId,
229+
["messages", "nodes", "turnItems"],
230+
{
231+
messageRoles: ["assistant"],
232+
turnItemTypes: ["approval_request", "user_input_request"],
233+
turnItemStatuses: ["pending", "running", "waiting"],
234+
},
235+
);
236+
const requestNodeIds = new Set(
237+
child.value.runtimeRequests.flatMap((request) =>
238+
request.status === "pending" ? [request.nodeId] : [],
239+
),
240+
);
241+
const knownNodeIds = new Set(child.value.nodes.map((node) => node.id));
242+
const knownItemIds = new Set(child.value.turnItems.map((item) => item.id));
243+
threads.push({
244+
...child.value,
245+
nodes: [
246+
...child.value.nodes,
247+
...records.nodes.filter(
248+
(node) => requestNodeIds.has(node.id) && !knownNodeIds.has(node.id),
249+
),
250+
],
251+
turnItems: [
252+
...child.value.turnItems,
253+
...records.turnItems.filter((item) => !knownItemIds.has(item.id)),
254+
],
255+
messages: records.messages.filter((message) => message.streaming),
228256
});
229-
threads.push({ ...child.value, messages: messages.filter((message) => message.streaming) });
230257
pending.push(...(yield* nativeLinks(threadId)));
231258
}
232259
const linked = RunExecutionService.openRunOwnedWorkFromProjection({

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ function openTurnWork(
125125
const subagentNodeId = NodeId.make(`node:subagent:${input.attemptId}`);
126126
const childThreadId = providerNativeChildThreadId(input.threadId);
127127
const grandchildThreadId = providerNativeChildThreadId(childThreadId);
128+
const childApprovalNodeId = NodeId.make(`node:child-approval:${input.attemptId}`);
129+
const childRequestId = RuntimeRequestId.make(`request:child-approval:${input.attemptId}`);
128130
const requestId = RuntimeRequestId.make(`request:approval:${input.attemptId}`);
129131
return [
130132
{
@@ -235,6 +237,61 @@ function openTurnWork(
235237
result: null,
236238
},
237239
},
240+
// The subagent is waiting on an approval in its own thread. Its rows carry
241+
// no run id; the child thread has no runs.
242+
{
243+
type: "node.updated",
244+
driver,
245+
node: {
246+
id: childApprovalNodeId,
247+
threadId: childThreadId,
248+
runId: null,
249+
parentNodeId: null,
250+
rootNodeId: childApprovalNodeId,
251+
kind: "approval_request",
252+
status: "waiting",
253+
countsForRun: false,
254+
providerThreadId: input.providerThread.id,
255+
providerTurnId,
256+
nativeItemRef: null,
257+
runtimeRequestId: childRequestId,
258+
checkpointScopeId: null,
259+
startedAt: now,
260+
completedAt: null,
261+
},
262+
},
263+
{
264+
type: "runtime_request.updated",
265+
driver,
266+
threadId: childThreadId,
267+
runtimeRequest: {
268+
id: childRequestId,
269+
nodeId: childApprovalNodeId,
270+
providerTurnId,
271+
nativeRequestRef: null,
272+
kind: "command",
273+
status: "pending",
274+
responseCapability: { type: "live", providerSessionId },
275+
createdAt: now,
276+
resolvedAt: null,
277+
},
278+
},
279+
{
280+
type: "turn_item.updated",
281+
driver,
282+
turnItem: {
283+
...base,
284+
id: TurnItemId.make(`turn-item:child-approval:${input.attemptId}`),
285+
threadId: childThreadId,
286+
runId: null,
287+
nodeId: childApprovalNodeId,
288+
ordinal: 3,
289+
status: "waiting",
290+
type: "approval_request",
291+
requestId: childRequestId,
292+
requestKind: "command",
293+
},
294+
},
238295
{
239296
type: "message.updated",
240297
driver,
@@ -826,6 +883,7 @@ it.live("settles the work a restarted run inherited when its replacement never o
826883
projection,
827884
nativeChildItems,
828885
nativeChildStreaming,
886+
nativeChildRequests,
829887
nativeGrandchildItems,
830888
delegatedItems,
831889
} = yield* Effect.gen(function* () {
@@ -990,6 +1048,9 @@ it.live("settles the work a restarted run inherited when its replacement never o
9901048
nativeChildStreaming: (yield* orchestrator.getThreadProjection(
9911049
providerNativeChildThreadId(threadId),
9921050
)).messages.some((message) => message.streaming),
1051+
nativeChildRequests: (yield* orchestrator.getThreadProjection(
1052+
providerNativeChildThreadId(threadId),
1053+
)).runtimeRequests.map((request) => request.status),
9931054
nativeGrandchildItems: openItems(
9941055
yield* orchestrator.getThreadProjection(
9951056
providerNativeChildThreadId(providerNativeChildThreadId(threadId)),
@@ -1025,10 +1086,12 @@ it.live("settles the work a restarted run inherited when its replacement never o
10251086
// The provider-native subagent's own thread ends with the run, and so does
10261087
// the thread of a nested subagent whose row already settled.
10271088
assert.deepEqual(nativeChildItems, [
1089+
["approval_request", "cancelled"],
10281090
["subagent", "completed"],
10291091
["command_execution", "cancelled"],
10301092
]);
10311093
assert.isFalse(nativeChildStreaming);
1094+
assert.deepEqual(nativeChildRequests, ["cancelled"]);
10321095
assert.deepEqual(nativeGrandchildItems, [["command_execution", "cancelled"]]);
10331096
// The delegated task and its thread keep running.
10341097
assert.deepEqual(

0 commit comments

Comments
 (0)