Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): a thread awaiting a delegated task's result no longer reports completed early #40881

ClosedKitsuneKode(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/40881/headopened View on GitHub

Problem

When an async delegated subagent (origin: "app_owned") finishes while its parent thread is waiting on it, Transaction A marks the subagent turn item terminal (completed) and transitions its completionDelivery to "claimed" ~140–160 ms before Transaction B (ProviderContinuationService) creates the parent's wake run.

During that gap, derivePendingBackgroundWork and the SQL shell/awareness/settlement projections only inspect non-terminal turn items and provider rosters, so pendingBackgroundTasks drops to []. The thread briefly looks settled across sidebar, notifications, push relay, auto-settlement, and MCP status, firing a premature "Thread completed" notification before the wake run starts and then firing a second notification when the wake run finishes.

Change

  • packages/shared/src/orchestrationV2PendingBackgroundWork.ts: Extend derivePendingBackgroundWork with optional subagents and userMessageId on PendingBackgroundWorkRun so terminal app_owned subagents with completionDelivery.state in ("pending", "claimed") on open, non-rolled-back parent runs keep holding completion until their wake run actually starts.
    • Once a "claimed" subagent's dedicated wake run (run.userMessageId === delivery.messageId) has started (status not "queued" or "cancelled"), it stops holding immediately—preventing stale holds during the wake run or between wake-run completion and finalizeDelegatedCompletionDelivery.
    • Cancelled wake runs and "pending" sibling deliveries keep holding because finalization schedules a successor wake.
    • Intentionally omit providerInstanceId on these synthetic entries so hasPendingBackgroundSubagent never grants idle parent providers native caller access.
  • apps/server/src/persistence/Migrations/108_PendingSubagentDeliveryIndex.ts & Migrations.ts: Add partial index orchestration_v2_projection_subagents_pending_delivery_idx on (thread_id) WHERE origin = 'app_owned' AND status IN ('completed', 'failed', 'cancelled', 'interrupted') AND completion_delivery_state IN ('pending', 'claimed').
    • Why this migration is needed: The existing subagents index from migration 075 is prefixed by thread_id, whereas listActiveShellIndex scans active threads globally with no thread_id filter. Without this partial index, every listActiveShellIndex read would scan the full subagent index; adding it follows the existing partial-index pattern used by every may_be_live source.
  • apps/server/src/orchestration-v2/ProjectionStore.ts: Wire selectShellPendingSubagentRows (using the new partial index) into getThreadShell, readShellSnapshot/getShellSnapshot, getAgentAwarenessSnapshot, getSettlementCandidates, threadShellFromProjection, and listActiveShellIndex (may_be_live), keeping SQL and JS wake-run semantics identical (including queued/cancelled wake runs, missing/stopped/rolled-back parent runs, archived threads, and provider_native exclusion).
  • packages/client-runtime/src/state/threadExecution.ts & apps/web/src/components/ChatView.logic.ts: Pass unarchived projection.subagents to derivePendingBackgroundWork so the open chat view matches the server shell.
  • Tests: Add shared unit tests (packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts), full 5-path SQL/memory parity + EXPLAIN QUERY PLAN + assertShellIndexCoversShells tests (apps/server/src/orchestration-v2/ProjectionStore.test.ts), and migration test coverage (reconcileV2PreviewMigration.test.ts).

Scope

Fixes #40879.

Kept strictly to the server/shared hold gap for app_owned delegated subagent completions:

  • Deliberately leaves ThreadNotificationCoordinator untouched so it respects #29146 (which intentionally removed client continuation grace) and does not conflict with #38318. With the server fix in place, the shell transitions directly from held → queued with no settled gap.
  • Stop / thread.background-work.settle already disposes pending/claimed cohorts via holdStoppedThread, preserving the escape hatch.

Verification

  1. Reproduced from real Orchestration V2 state (two consecutive wakes in one thread):
    • Wake 1: subagent completed & claimed at 11:57:19.482Z, wake run #2 requested at 11:57:19.635Z (153 ms gap with pendingBackgroundTasks = []).
    • Wake 2: subagent completed & claimed at 12:29:17.256Z, wake run #5 requested at 12:29:17.397Z (141 ms gap with pendingBackgroundTasks = []).
  2. Confirmed both new tests fail on upstream/main (620cf4770d) and pass on this branch (4793e46c92):
    • On upstream/main:
      • packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts fails: drops node:claimed, node:claimed-cancelled, node:claimed-queued, and node:pending-failed.
      • apps/server/src/orchestration-v2/ProjectionStore.test.ts fails across getThreadShell, getShellSnapshot, getAgentAwarenessSnapshot, and getSettlementCandidates:
        AssertionError: expected [] to deeply equal [ { taskId: "node:delegated-wake-gap:subagent", description: "Audit PRs", kind: "subagent", childThreadId: "thread:delegated-wake-gap:child" } ]
    • On 4793e46c92:
      • Holds the thread across all 5 read paths while claimed with no wake run or a queued/cancelled wake run; releases immediately once the wake run is running/completed (before finalizeDelegatedCompletionDelivery runs); excludes provider_native, disposed/delivered/acknowledged, stopped cohorts, rolled-back runs, and archived threads; verified EXPLAIN QUERY PLAN uses orchestration_v2_projection_subagents_pending_delivery_idx.
  3. vp run check:changed passes cleanly on 4793e46c92:
    • format (0.3s), lint (0 errors), knip 1/2 & 2/2, graphql, typecheck (37/37 packages), test (8/8 files, 212/212 tests including ProjectionStore.test.ts 63/63, Migrations.test.ts 11/11, reconcileV2PreviewMigration.test.ts 5/5, orchestrationV2PendingBackgroundWork.test.ts 22/22), and React Compiler coverage.

Written with Gemini 4 Neon (Antigravity) and audited with Claude Opus 5.5 (Claude Code) in T3 Code.

    1. tanstack-source added labelsbug

    2. Thanks for this. #31357 makes the same change and is the one being kept.

      #31357 covers the same delegated-result handoff with a smaller change. It requires a reserved wake delivery before holding a finished task, avoiding an indefinite wait for pending results that have no wake scheduled.

      Your SQL and in-memory parity coverage would be useful additional coverage for that fix.

      Closing as a duplicate.

    3. juliusmarminge closed this

Sign in to join this conversation.