Skip to content

Commit fd49810

Browse files
committed
fix(server): settle native subagents after session loss
1 parent aa668fd commit fd49810

2 files changed

Lines changed: 375 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ import * as SqlClient from "effect/sql/SqlClient";
2424
import * as SqlitePersistence from "../persistence/Sqlite.ts";
2525
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
2626
import * as EffectOutbox from "./EffectOutbox.ts";
27+
import * as EffectWorker from "./EffectWorker.ts";
2728
import * as Orchestrator from "./Orchestrator.ts";
2829
import * as ProjectionStore from "./ProjectionStore.ts";
2930
import type { ProviderAdapterV2Shape } from "./ProviderAdapter.ts";
3031
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
32+
import { makeSubagentChildThread } from "./SubagentProjection.ts";
3133
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
3234

3335
const instanceId = ProviderInstanceId.make("codex");
@@ -79,6 +81,24 @@ it.effect("interrupts only the selected running native Codex subagent", () =>
7981
creationSource: "web",
8082
});
8183
}
84+
yield* projections.apply({
85+
id: EventId.make("child:stop-subagent:lineage"),
86+
type: "thread.metadata-updated",
87+
threadId: childThreadId,
88+
occurredAt: now,
89+
payload: makeSubagentChildThread({
90+
parentThread: yield* projections.getThread(parentThreadId),
91+
childThreadId,
92+
parentNodeId: subagentId,
93+
activeProviderThreadId: providerThreadId,
94+
providerInstanceId: instanceId,
95+
modelSelection,
96+
title: "Worker",
97+
now,
98+
createdBy: "agent",
99+
creationSource: "provider",
100+
}),
101+
});
82102
const subagent = {
83103
id: subagentId,
84104
threadId: parentThreadId,
@@ -149,6 +169,126 @@ it.effect("interrupts only the selected running native Codex subagent", () =>
149169
occurredAt: now,
150170
payload: providerTurn,
151171
});
172+
for (const [threadId, nodeId, kind] of [
173+
[parentThreadId, subagentId, "subagent"],
174+
[childThreadId, providerTurn.nodeId, "root_turn"],
175+
] as const) {
176+
yield* projections.apply({
177+
id: EventId.make(`node:${nodeId}`),
178+
type: "node.updated",
179+
threadId,
180+
occurredAt: now,
181+
payload: {
182+
id: nodeId,
183+
threadId,
184+
runId: null,
185+
parentNodeId: null,
186+
rootNodeId: nodeId,
187+
kind,
188+
status: "running",
189+
countsForRun: false,
190+
providerThreadId,
191+
providerTurnId,
192+
nativeItemRef: null,
193+
runtimeRequestId: null,
194+
checkpointScopeId: null,
195+
startedAt: now,
196+
completedAt: null,
197+
},
198+
});
199+
}
200+
const item = {
201+
id: TurnItemId.make("item:stop-subagent"),
202+
threadId: parentThreadId,
203+
runId: null,
204+
nodeId: subagentId,
205+
providerThreadId,
206+
providerTurnId: null,
207+
nativeItemRef: null,
208+
parentItemId: null,
209+
ordinal: 1,
210+
status: "running" as const,
211+
title: subagent.title,
212+
startedAt: now,
213+
completedAt: null,
214+
updatedAt: now,
215+
type: "subagent" as const,
216+
subagentId,
217+
origin: subagent.origin,
218+
driver: subagent.driver,
219+
providerInstanceId: instanceId,
220+
childThreadId,
221+
prompt: subagent.prompt,
222+
result: null,
223+
};
224+
yield* projections.apply({
225+
id: EventId.make("item:stop-subagent"),
226+
type: "turn-item.updated",
227+
threadId: parentThreadId,
228+
occurredAt: now,
229+
payload: item,
230+
});
231+
yield* projections.apply({
232+
id: EventId.make("command:stop-subagent"),
233+
type: "turn-item.updated",
234+
threadId: childThreadId,
235+
occurredAt: now,
236+
payload: {
237+
id: TurnItemId.make("command:stop-subagent"),
238+
threadId: childThreadId,
239+
runId: null,
240+
nodeId: providerTurn.nodeId,
241+
providerThreadId,
242+
providerTurnId,
243+
nativeItemRef: null,
244+
parentItemId: null,
245+
ordinal: 1,
246+
status: "running",
247+
title: null,
248+
startedAt: now,
249+
completedAt: null,
250+
updatedAt: now,
251+
type: "command_execution",
252+
input: "sleep 100",
253+
},
254+
});
255+
yield* projections.apply({
256+
id: EventId.make("message:stop-subagent"),
257+
type: "message.updated",
258+
threadId: childThreadId,
259+
occurredAt: now,
260+
payload: {
261+
id: MessageId.make("message:stop-subagent"),
262+
threadId: childThreadId,
263+
runId: null,
264+
nodeId: providerTurn.nodeId,
265+
role: "assistant",
266+
text: "Working",
267+
attachments: [],
268+
streaming: true,
269+
createdBy: "agent",
270+
creationSource: "provider",
271+
createdAt: now,
272+
updatedAt: now,
273+
},
274+
});
275+
yield* projections.apply({
276+
id: EventId.make("request:stop-subagent"),
277+
type: "runtime-request.updated",
278+
threadId: childThreadId,
279+
occurredAt: now,
280+
payload: {
281+
id: RuntimeRequestId.make("request:stop-subagent"),
282+
nodeId: providerTurn.nodeId,
283+
providerTurnId,
284+
nativeRequestRef: null,
285+
kind: "user_input",
286+
status: "pending",
287+
responseCapability: { type: "live", providerSessionId },
288+
createdAt: now,
289+
resolvedAt: null,
290+
},
291+
});
152292
const commandId = CommandId.make("interrupt:stop-subagent");
153293
const accepted = yield* orchestrator.dispatch({
154294
type: "subagent.interrupt",
@@ -181,6 +321,116 @@ it.effect("interrupts only the selected running native Codex subagent", () =>
181321
},
182322
],
183323
);
324+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
325+
yield* worker.drain();
326+
const child = yield* projections.getThreadProjection(childThreadId);
327+
const parent = yield* projections.getThreadProjection(parentThreadId);
328+
assert.equal(child.providerTurns[0]?.status, "interrupted");
329+
assert.equal(child.providerThreads[0]?.status, "idle");
330+
assert.equal(child.nodes[0]?.status, "interrupted");
331+
assert.equal(child.turnItems[0]?.status, "interrupted");
332+
assert.equal(child.messages[0]?.streaming, false);
333+
assert.equal(child.runtimeRequests[0]?.status, "cancelled");
334+
assert.equal(parent.subagents[0]?.status, "interrupted");
335+
assert.equal(parent.nodes[0]?.status, "interrupted");
336+
assert.equal(parent.turnItems[0]?.status, "interrupted");
337+
for (const race of [
338+
"completed-turn",
339+
"completed-task",
340+
"waiting-task",
341+
"newer-turn",
342+
] as const) {
343+
yield* projections.apply({
344+
id: EventId.make(`turn:${race}:running`),
345+
type: "provider-turn.updated",
346+
threadId: childThreadId,
347+
occurredAt: now,
348+
payload: providerTurn,
349+
});
350+
yield* projections.apply({
351+
id: EventId.make(`task:${race}:running`),
352+
type: "subagent.updated",
353+
threadId: parentThreadId,
354+
occurredAt: now,
355+
payload: subagent,
356+
});
357+
yield* projections.apply({
358+
id: EventId.make(`provider-thread:${race}:active`),
359+
type: "provider-thread.updated",
360+
threadId: childThreadId,
361+
occurredAt: now,
362+
payload: providerThread,
363+
});
364+
yield* orchestrator.dispatch({
365+
type: "subagent.interrupt",
366+
commandId: CommandId.make(`interrupt:${race}`),
367+
threadId: parentThreadId,
368+
subagentId,
369+
});
370+
const newerTurnId = ProviderTurnId.make("turn:stop-subagent:newer");
371+
if (race === "completed-turn" || race === "newer-turn") {
372+
yield* projections.apply({
373+
id: EventId.make(`turn:${race}:raced`),
374+
type: "provider-turn.updated",
375+
threadId: childThreadId,
376+
occurredAt: now,
377+
payload:
378+
race === "newer-turn"
379+
? { ...providerTurn, id: newerTurnId, ordinal: 2 }
380+
: { ...providerTurn, status: "completed", completedAt: now },
381+
});
382+
}
383+
if (race !== "newer-turn") {
384+
yield* projections.apply({
385+
id: EventId.make(`task:${race}:completed`),
386+
type: "subagent.updated",
387+
threadId: parentThreadId,
388+
occurredAt: now,
389+
payload: {
390+
...subagent,
391+
status: race === "waiting-task" ? "waiting" : "completed",
392+
completedAt: race === "waiting-task" ? null : now,
393+
},
394+
});
395+
}
396+
yield* worker.drain();
397+
const racedChild = yield* projections.getThreadProjection(childThreadId);
398+
assert.equal(
399+
racedChild.providerTurns.find((turn) => turn.id === providerTurnId)?.status,
400+
race === "completed-turn" ? "completed" : "interrupted",
401+
);
402+
assert.equal(
403+
(yield* projections.getThreadProjection(parentThreadId)).subagents[0]?.status,
404+
race === "newer-turn" ? "running" : race === "waiting-task" ? "interrupted" : "completed",
405+
);
406+
if (race === "newer-turn") {
407+
assert.equal(
408+
racedChild.providerTurns.find((turn) => turn.id === newerTurnId)?.status,
409+
"running",
410+
);
411+
assert.equal(racedChild.providerThreads[0]?.status, "active");
412+
yield* projections.apply({
413+
id: EventId.make("turn:newer:completed"),
414+
type: "provider-turn.updated",
415+
threadId: childThreadId,
416+
occurredAt: now,
417+
payload: {
418+
...providerTurn,
419+
id: newerTurnId,
420+
ordinal: 2,
421+
status: "completed",
422+
completedAt: now,
423+
},
424+
});
425+
}
426+
}
427+
yield* projections.apply({
428+
id: EventId.make("subagent:stop-subagent:running"),
429+
type: "subagent.updated",
430+
threadId: parentThreadId,
431+
occurredAt: now,
432+
payload: subagent,
433+
});
184434
for (const missing of ["turn", "session"] as const) {
185435
yield* projections.apply({
186436
id: EventId.make(`provider-turn:stop-subagent:missing-${missing}`),

0 commit comments

Comments
 (0)