@devries-cameron20 / t3code Mirror · read-only / commits / aac4c15522
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 08:15 UTC

fix(server): delegated tasks finish even when the child leaves a watcher running (#27468)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Byntham committed Nov 1, 2026 at 13:46 UTC aac4c15522611288362449e26154752dfb888c79
4 files changed +32 -15
apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+6 -3
@@ -3193,9 +3193,12 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
3193 occurredAt: now,
3194 payload: {
3195 ...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 + ],
3202 },
3203 }
3204 : {
apps/server/src/orchestration-v2/Orchestrator.ts
+3 -2
@@ -77,6 +77,7 @@ import {
77 import { modelSelectionsEqual } from "@t3tools/shared/model";
78 import { recordThreadOpen } from "@t3tools/shared/paletteSearch";
79 import {
80 + backgroundWorkHoldsCompletion,
81 derivePendingBackgroundWork,
82 pendingBackgroundTurnItems,
83 } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
@@ -13715,14 +13716,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13716
13717 // A child whose run ends while its background tasks or its own subagents still run has no
13718 // 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.
13720 const providerThreadsWithBackgroundWork = new Set<string>();
13721 const settlesChildWork = (stored: OrchestrationV2StoredEvent) => {
13722 const { event } = stored;
13723 if (event.type === "subagent.updated")
13724 return !isOrchestrationV2WorkActive(event.payload.status);
13725 if (event.type !== "provider-thread.updated") return false;
13725 - if ((event.payload.pendingBackgroundTasks?.length ?? 0) > 0) {
13726 + if (backgroundWorkHoldsCompletion(event.payload.pendingBackgroundTasks ?? [])) {
13727 providerThreadsWithBackgroundWork.add(event.payload.id);
13728 return false;
13729 }
apps/server/src/orchestration-v2/SubagentProjection.test.ts
+19 -9
@@ -274,15 +274,25 @@ it("waits for nested work and retains the report across monitor acknowledgements
274 "result_available",
275 );
276 }
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 + }
296 const pending = {
297 ...run,
298 id: RunId.make("followup"),
packages/provider-core/src/server/subagentProjection.ts
+4 -1
@@ -18,6 +18,7 @@ import type {
18 ThreadId,
19 TurnItemId,
20 } from "@t3tools/contracts";
21 +import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
22 import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
23 import * as DateTime from "effect/DateTime";
24 import * as Effect from "effect/Effect";
@@ -258,7 +259,9 @@ export function delegatedTaskProgress(projection: {
259 (task.completionDelivery?.state === "pending" ||
260 task.completionDelivery?.state === "claimed")),
261 ) ||
261 - projection.providerThreads.some((thread) => (thread.pendingBackgroundTasks?.length ?? 0) > 0);
262 + projection.providerThreads.some((thread) =>
263 + backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []),
264 + );
265 const resultRun = workRuns
266 .filter((run) => terminal(run.status) && (run.startedAt !== null || run.ordinal === 1))
267 .toSorted((a, b) => (runRanAfter(a, b) ? -1 : runRanAfter(b, a) ? 1 : 0))[0];