TypeScript
fix(server): delegated tasks finish even when the child leaves a watcher running (#27468)
Bynthamaac4c1552261
diff --git a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.tsindex 5053e8d5687..87f6bbd1a67 100644--- a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts+++ b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts@@ -3193,9 +3193,12 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {occurredAt: now,payload: {...providerThread,- pendingBackgroundTasks: open- ? [{ taskId: "sleep-900", kind: "command" as const }]- : [],+ pendingBackgroundTasks: [+ ...(open+ ? [{ taskId: "sleep-900", kind: "command" as const, awaited: true as const }]+ : []),+ { taskId: "log-watcher", kind: "monitor" as const },+ ],},}: {diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 3734082d9f5..3d735bcd756 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -77,6 +77,7 @@ import {import { modelSelectionsEqual } from "@t3tools/shared/model";import { recordThreadOpen } from "@t3tools/shared/paletteSearch";import {+ backgroundWorkHoldsCompletion,derivePendingBackgroundWork,pendingBackgroundTurnItems,} from "@t3tools/shared/orchestrationV2PendingBackgroundWork";@@ -13715,14 +13716,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio// A child whose run ends while its background tasks or its own subagents still run has no// result yet. When that work settles there is no run update to finalize it, so these events- // do it. A background roster counts once it empties after it was seen non-empty.+ // do it. A background roster counts once it stops holding after it was seen holding.const providerThreadsWithBackgroundWork = new Set<string>();const settlesChildWork = (stored: OrchestrationV2StoredEvent) => {const { event } = stored;if (event.type === "subagent.updated")return !isOrchestrationV2WorkActive(event.payload.status);if (event.type !== "provider-thread.updated") return false;- if ((event.payload.pendingBackgroundTasks?.length ?? 0) > 0) {+ if (backgroundWorkHoldsCompletion(event.payload.pendingBackgroundTasks ?? [])) {providerThreadsWithBackgroundWork.add(event.payload.id);return false;}diff --git a/apps/server/src/orchestration-v2/SubagentProjection.test.ts b/apps/server/src/orchestration-v2/SubagentProjection.test.tsindex ed643c2ff1a..ae8f7e09d20 100644--- a/apps/server/src/orchestration-v2/SubagentProjection.test.ts+++ b/apps/server/src/orchestration-v2/SubagentProjection.test.ts@@ -274,15 +274,25 @@ it("waits for nested work and retains the report across monitor acknowledgements"result_available",);}- assert.equal(- delegatedTaskProgress({- ...projection,- providerThreads: [- { pendingBackgroundTasks: [{ taskId: "background-audit", kind: "command" }] },- ],- }).state,- "waiting_for_children",- );+ for (const [awaited, state] of [+ [true, "waiting_for_children"],+ [false, "result_available"],+ ] as const) {+ assert.equal(+ delegatedTaskProgress({+ ...projection,+ providerThreads: [+ {+ pendingBackgroundTasks: [+ { taskId: "background-audit", kind: "command", ...(awaited ? { awaited } : {}) },+ { taskId: "log-watcher", kind: "monitor" },+ ],+ },+ ],+ }).state,+ state,+ );+ }const pending = {...run,id: RunId.make("followup"),diff --git a/packages/provider-core/src/server/subagentProjection.ts b/packages/provider-core/src/server/subagentProjection.tsindex 7179398cf60..37758188925 100644--- a/packages/provider-core/src/server/subagentProjection.ts+++ b/packages/provider-core/src/server/subagentProjection.ts@@ -18,6 +18,7 @@ import type {ThreadId,TurnItemId,} from "@t3tools/contracts";+import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";import * as DateTime from "effect/DateTime";import * as Effect from "effect/Effect";@@ -258,7 +259,9 @@ export function delegatedTaskProgress(projection: {(task.completionDelivery?.state === "pending" ||task.completionDelivery?.state === "claimed")),) ||- projection.providerThreads.some((thread) => (thread.pendingBackgroundTasks?.length ?? 0) > 0);+ projection.providerThreads.some((thread) =>+ backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []),+ );const resultRun = workRuns.filter((run) => terminal(run.status) && (run.startedAt !== null || run.ordinal === 1)).toSorted((a, b) => (runRanAfter(a, b) ? -1 : runRanAfter(b, a) ? 1 : 0))[0];