Skip to content

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

Closed
KitsuneKode wants to merge 1 commit into
pingdotgg:mainfrom
KitsuneKode:t3/fix-subagent-completion-notifications
Closed

KitsuneKode wants to merge 1 commit into
pingdotgg:mainfrom
KitsuneKode:t3/fix-subagent-completion-notifications

Conversation

@KitsuneKode

Copy link
Copy Markdown

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:

Verification

  1. Reproduced from real Orchestration V2 state (two consecutive wakes in one thread):
  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.

@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Nov 22, 2026
@juliusmarminge

Copy link
Copy Markdown
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Thread fires premature "Thread completed" notification during the gap between a delegated subagent finishing and its wake run starting

2 participants