Skip to content

Commit 60b0dfe

Browse files
t3dotggclaude
andcommitted
fix(server): interrupt a provider thread through the turn that owns its work
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8bfd33e commit 60b0dfe

3 files changed

Lines changed: 80 additions & 61 deletions

File tree

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

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
229229
const codexProviderThread = (yield* orchestrator.getThreadProjection(threadId))
230230
.providerThreads[0]!;
231231
// A settled run with its root node, attempt, provider turn and,
232-
// optionally, a command it left running.
232+
// optionally, a command or native subagent it left running.
233233
const settledRun = (input: {
234234
readonly ordinal: number;
235235
readonly providerThreadId: ProviderThreadId;
236-
readonly runningCommandId?: TurnItemId;
236+
readonly runningItem?: { readonly id: TurnItemId; readonly kind: "command" | "subagent" };
237237
}) => {
238238
const runId = RunId.make(`run:${input.ordinal}`);
239239
const attemptId = RunAttemptId.make(`attempt:${input.ordinal}`);
@@ -324,49 +324,73 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
324324
},
325325
},
326326
];
327-
if (input.runningCommandId !== undefined) {
327+
const item = input.runningItem;
328+
if (item !== undefined) {
329+
const base = {
330+
id: item.id,
331+
threadId,
332+
runId,
333+
nodeId,
334+
providerThreadId: input.providerThreadId,
335+
providerTurnId,
336+
nativeItemRef: null,
337+
parentItemId: null,
338+
ordinal: input.ordinal * 100,
339+
status: "running",
340+
title: null,
341+
startedAt: now,
342+
completedAt: null,
343+
updatedAt: now,
344+
} as const;
328345
events.push({
329-
id: EventId.make(`command:${input.ordinal}`),
346+
id: EventId.make(`item:${input.ordinal}`),
330347
type: "turn-item.updated",
331348
threadId,
332349
runId,
333350
occurredAt: now,
334-
payload: {
335-
id: input.runningCommandId,
336-
threadId,
337-
runId,
338-
nodeId,
339-
providerThreadId: input.providerThreadId,
340-
providerTurnId,
341-
nativeItemRef: null,
342-
parentItemId: null,
343-
ordinal: input.ordinal * 100,
344-
status: "running",
345-
title: null,
346-
startedAt: now,
347-
completedAt: null,
348-
updatedAt: now,
349-
type: "command_execution",
350-
input: "vp run test --watch",
351-
},
351+
payload:
352+
item.kind === "command"
353+
? { ...base, type: "command_execution", input: "vp run test --watch" }
354+
: {
355+
...base,
356+
// A native subagent item names its own provider thread
357+
// but its parent's provider turn.
358+
providerThreadId: ProviderThreadId.make("provider-thread:codex-subagent"),
359+
type: "subagent",
360+
subagentId: NodeId.make(`subagent:${input.ordinal}`),
361+
origin: "provider_native",
362+
driver,
363+
providerInstanceId: instanceId,
364+
childThreadId: null,
365+
prompt: "Review the change",
366+
result: null,
367+
},
352368
});
353369
}
354370
return { runId, providerTurnId, events };
355371
};
356372

357-
// Codex leaves a second command in a later run. Then the thread moves
358-
// on to another provider thread, which also has a live session.
373+
// Later Codex runs leave a second command and a native subagent. Then
374+
// the thread moves on to another provider thread, which also has a
375+
// live session.
359376
const watcherId = TurnItemId.make("turn-item:watcher");
360-
const laterCodexRun = settledRun({
377+
const watcherRun = settledRun({
361378
ordinal: 2,
362379
providerThreadId: codexProviderThread.id,
363-
runningCommandId: watcherId,
380+
runningItem: { id: watcherId, kind: "command" },
381+
});
382+
const reviewerId = TurnItemId.make("turn-item:reviewer");
383+
const reviewerRun = settledRun({
384+
ordinal: 3,
385+
providerThreadId: codexProviderThread.id,
386+
runningItem: { id: reviewerId, kind: "subagent" },
364387
});
365388
const otherProviderThreadId = ProviderThreadId.make("provider-thread:other");
366-
const latestRun = settledRun({ ordinal: 3, providerThreadId: otherProviderThreadId });
389+
const latestRun = settledRun({ ordinal: 4, providerThreadId: otherProviderThreadId });
367390
yield* sink.write({
368391
events: [
369-
...laterCodexRun.events,
392+
...watcherRun.events,
393+
...reviewerRun.events,
370394
{
371395
id: EventId.make("provider-thread:other"),
372396
type: "provider-thread.updated",
@@ -375,8 +399,8 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
375399
payload: {
376400
...codexProviderThread,
377401
id: otherProviderThreadId,
378-
firstRunOrdinal: 3,
379-
lastRunOrdinal: 3,
402+
firstRunOrdinal: 4,
403+
lastRunOrdinal: 4,
380404
},
381405
},
382406
...latestRun.events,
@@ -392,20 +416,21 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
392416
yield* worker.drain();
393417

394418
// Stop reaches both provider threads. The Codex one is interrupted at
395-
// its latest pending work, so its settle covers both of its runs.
419+
// its latest pending work, the subagent's parent turn, so its settle
420+
// covers all three Codex runs.
396421
assert.sameDeepMembers(
397422
interrupts.map((interrupt) => [interrupt.providerThread.id, interrupt.providerTurnId]),
398423
[
399424
[otherProviderThreadId, latestRun.providerTurnId],
400-
[codexProviderThread.id, laterCodexRun.providerTurnId],
425+
[codexProviderThread.id, reviewerRun.providerTurnId],
401426
],
402427
);
403428
const after = yield* orchestrator.getThreadProjection(threadId);
404429
assert.deepEqual(
405-
[devServerId, watcherId].map(
430+
[devServerId, watcherId, reviewerId].map(
406431
(id) => after.turnItems.find((item) => item.id === id)?.status,
407432
),
408-
["interrupted", "interrupted"],
433+
["interrupted", "interrupted", "interrupted"],
409434
);
410435
}).pipe(
411436
Effect.provide(

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

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8120,53 +8120,47 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
81208120
);
81218121
const runOrdinalOf = (item: (typeof projection.turnItems)[number]) =>
81228122
item.runId === null ? -1 : (runOrdinals.get(item.runId) ?? -1);
8123-
// Each provider thread is interrupted at its latest pending item: that
8124-
// item's run bounds the settle follow-up, which must cover all of the
8125-
// thread's work.
8126-
const latestItemByProviderThread = new Map<
8123+
// Each provider thread is interrupted at its latest pending work: that
8124+
// turn's run bounds the settle follow-up, which must cover all of the
8125+
// thread's work. The target comes from the item's provider turn, since
8126+
// a native subagent item names its own provider thread but its
8127+
// parent's turn; interrupting the parent's turn reaches the subagent.
8128+
const latestTurnByProviderThread = new Map<
81278129
OrchestrationV2ProviderThread["id"],
8128-
(typeof projection.turnItems)[number]
8130+
{ readonly turn: (typeof projection.providerTurns)[number]; readonly runOrdinal: number }
81298131
>();
81308132
for (const item of pendingBackgroundTurnItems({
81318133
turnItems: projection.turnItems,
81328134
runs: projection.runs,
81338135
})) {
8134-
if (
8135-
item.providerThreadId == null ||
8136-
item.providerThreadId === providerThread.id ||
8137-
item.providerTurnId === null
8138-
) {
8139-
continue;
8140-
}
8141-
const latest = latestItemByProviderThread.get(item.providerThreadId);
8142-
if (latest === undefined || runOrdinalOf(item) > runOrdinalOf(latest)) {
8143-
latestItemByProviderThread.set(item.providerThreadId, item);
8136+
const turn = projection.providerTurns.find(
8137+
(candidate) => candidate.id === item.providerTurnId,
8138+
);
8139+
if (turn === undefined || turn.providerThreadId === providerThread.id) continue;
8140+
const runOrdinal = runOrdinalOf(item);
8141+
const latest = latestTurnByProviderThread.get(turn.providerThreadId);
8142+
if (latest === undefined || runOrdinal > latest.runOrdinal) {
8143+
latestTurnByProviderThread.set(turn.providerThreadId, { turn, runOrdinal });
81448144
}
81458145
}
8146-
for (const item of latestItemByProviderThread.values()) {
8146+
for (const { turn } of latestTurnByProviderThread.values()) {
81478147
const owner = projection.providerThreads.find(
8148-
(candidate) => candidate.id === item.providerThreadId,
8148+
(candidate) => candidate.id === turn.providerThreadId,
81498149
);
8150-
if (
8151-
owner === undefined ||
8152-
owner.providerSessionId === null ||
8153-
item.providerTurnId === null
8154-
) {
8155-
continue;
8156-
}
8150+
if (owner === undefined || owner.providerSessionId === null) continue;
81578151
const ownerSession = yield* providerSessions
81588152
.get(owner.providerSessionId)
81598153
.pipe(Effect.orElseSucceed(() => Option.none()));
81608154
if (Option.isNone(ownerSession)) continue;
81618155
otherProviderInterrupts.push({
8162-
id: `effect:${command.commandId}:provider-turn.interrupt:${item.providerTurnId}`,
8156+
id: `effect:${command.commandId}:provider-turn.interrupt:${turn.id}`,
81638157
commandId: command.commandId,
81648158
threadId: command.threadId,
81658159
request: {
81668160
type: "provider-turn.interrupt",
81678161
providerSessionId: owner.providerSessionId,
81688162
providerThreadId: owner.id,
8169-
providerTurnId: item.providerTurnId,
8163+
providerTurnId: turn.id,
81708164
},
81718165
});
81728166
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ it.effect.each([
247247
forkedFrom: null,
248248
createdAt: old,
249249
updatedAt: old,
250-
pendingBackgroundTasks: [{ taskId: "running-task", kind: "command" }],
250+
pendingBackgroundTasks: [{ taskId: "running-task", kind: "subagent" }],
251251
},
252252
});
253253
const candidates = yield* store.getSettlementCandidates();

0 commit comments

Comments
 (0)