Skip to content

Commit aac4c15

Browse files
Bynthamclaudemaria-rcks
authored
fix(server): delegated tasks finish even when the child leaves a watcher running (#27468)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 9650541 commit aac4c15

4 files changed

Lines changed: 32 additions & 15 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,9 +3193,12 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
31933193
occurredAt: now,
31943194
payload: {
31953195
...providerThread,
3196-
pendingBackgroundTasks: open
3197-
? [{ taskId: "sleep-900", kind: "command" as const }]
3198-
: [],
3196+
pendingBackgroundTasks: [
3197+
...(open
3198+
? [{ taskId: "sleep-900", kind: "command" as const, awaited: true as const }]
3199+
: []),
3200+
{ taskId: "log-watcher", kind: "monitor" as const },
3201+
],
31993202
},
32003203
}
32013204
: {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import {
7777
import { modelSelectionsEqual } from "@t3tools/shared/model";
7878
import { recordThreadOpen } from "@t3tools/shared/paletteSearch";
7979
import {
80+
backgroundWorkHoldsCompletion,
8081
derivePendingBackgroundWork,
8182
pendingBackgroundTurnItems,
8283
} from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
@@ -13715,14 +13716,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1371513716

1371613717
// A child whose run ends while its background tasks or its own subagents still run has no
1371713718
// result yet. When that work settles there is no run update to finalize it, so these events
13718-
// do it. A background roster counts once it empties after it was seen non-empty.
13719+
// do it. A background roster counts once it stops holding after it was seen holding.
1371913720
const providerThreadsWithBackgroundWork = new Set<string>();
1372013721
const settlesChildWork = (stored: OrchestrationV2StoredEvent) => {
1372113722
const { event } = stored;
1372213723
if (event.type === "subagent.updated")
1372313724
return !isOrchestrationV2WorkActive(event.payload.status);
1372413725
if (event.type !== "provider-thread.updated") return false;
13725-
if ((event.payload.pendingBackgroundTasks?.length ?? 0) > 0) {
13726+
if (backgroundWorkHoldsCompletion(event.payload.pendingBackgroundTasks ?? [])) {
1372613727
providerThreadsWithBackgroundWork.add(event.payload.id);
1372713728
return false;
1372813729
}

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,25 @@ it("waits for nested work and retains the report across monitor acknowledgements
274274
"result_available",
275275
);
276276
}
277-
assert.equal(
278-
delegatedTaskProgress({
279-
...projection,
280-
providerThreads: [
281-
{ pendingBackgroundTasks: [{ taskId: "background-audit", kind: "command" }] },
282-
],
283-
}).state,
284-
"waiting_for_children",
285-
);
277+
for (const [awaited, state] of [
278+
[true, "waiting_for_children"],
279+
[false, "result_available"],
280+
] as const) {
281+
assert.equal(
282+
delegatedTaskProgress({
283+
...projection,
284+
providerThreads: [
285+
{
286+
pendingBackgroundTasks: [
287+
{ taskId: "background-audit", kind: "command", ...(awaited ? { awaited } : {}) },
288+
{ taskId: "log-watcher", kind: "monitor" },
289+
],
290+
},
291+
],
292+
}).state,
293+
state,
294+
);
295+
}
286296
const pending = {
287297
...run,
288298
id: RunId.make("followup"),

‎packages/provider-core/src/server/subagentProjection.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
ThreadId,
1919
TurnItemId,
2020
} from "@t3tools/contracts";
21+
import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
2122
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
2223
import * as DateTime from "effect/DateTime";
2324
import * as Effect from "effect/Effect";
@@ -258,7 +259,9 @@ export function delegatedTaskProgress(projection: {
258259
(task.completionDelivery?.state === "pending" ||
259260
task.completionDelivery?.state === "claimed")),
260261
) ||
261-
projection.providerThreads.some((thread) => (thread.pendingBackgroundTasks?.length ?? 0) > 0);
262+
projection.providerThreads.some((thread) =>
263+
backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []),
264+
);
262265
const resultRun = workRuns
263266
.filter((run) => terminal(run.status) && (run.startedAt !== null || run.ordinal === 1))
264267
.toSorted((a, b) => (runRanAfter(a, b) ? -1 : runRanAfter(b, a) ? 1 : 0))[0];

0 commit comments

Comments
 (0)