Repository navigation
fix(server): a thread awaiting a delegated task's result no longer reports completed early - #40881
Closed
KitsuneKode wants to merge 1 commit into
Closed
KitsuneKode wants to merge 1 commit into
KitsuneKode wants to merge 1 commit into
Conversation
…ports completed early
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an async delegated subagent (
origin: "app_owned") finishes while its parent thread is waiting on it, Transaction A marks thesubagentturn item terminal (completed) and transitions itscompletionDeliveryto"claimed"~140–160 ms before Transaction B (ProviderContinuationService) creates the parent's wake run.During that gap,
derivePendingBackgroundWorkand the SQL shell/awareness/settlement projections only inspect non-terminal turn items and provider rosters, sopendingBackgroundTasksdrops 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: ExtendderivePendingBackgroundWorkwith optionalsubagentsanduserMessageIdonPendingBackgroundWorkRunso terminalapp_ownedsubagents withcompletionDelivery.statein("pending", "claimed")on open, non-rolled-back parent runs keep holding completion until their wake run actually starts."claimed"subagent's dedicated wake run (run.userMessageId === delivery.messageId) has started (statusnot"queued"or"cancelled"), it stops holding immediately—preventing stale holds during the wake run or between wake-run completion andfinalizeDelegatedCompletionDelivery."pending"sibling deliveries keep holding because finalization schedules a successor wake.providerInstanceIdon these synthetic entries sohasPendingBackgroundSubagentnever grants idle parent providers native caller access.apps/server/src/persistence/Migrations/108_PendingSubagentDeliveryIndex.ts&Migrations.ts: Add partial indexorchestration_v2_projection_subagents_pending_delivery_idxon(thread_id) WHERE origin = 'app_owned' AND status IN ('completed', 'failed', 'cancelled', 'interrupted') AND completion_delivery_state IN ('pending', 'claimed').thread_id, whereaslistActiveShellIndexscans active threads globally with nothread_idfilter. Without this partial index, everylistActiveShellIndexread would scan the full subagent index; adding it follows the existing partial-index pattern used by everymay_be_livesource.apps/server/src/orchestration-v2/ProjectionStore.ts: WireselectShellPendingSubagentRows(using the new partial index) intogetThreadShell,readShellSnapshot/getShellSnapshot,getAgentAwarenessSnapshot,getSettlementCandidates,threadShellFromProjection, andlistActiveShellIndex(may_be_live), keeping SQL and JS wake-run semantics identical (includingqueued/cancelledwake runs, missing/stopped/rolled-back parent runs, archived threads, andprovider_nativeexclusion).packages/client-runtime/src/state/threadExecution.ts&apps/web/src/components/ChatView.logic.ts: Pass unarchivedprojection.subagentstoderivePendingBackgroundWorkso the open chat view matches the server shell.packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts), full 5-path SQL/memory parity +EXPLAIN QUERY PLAN+assertShellIndexCoversShellstests (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_owneddelegated subagent completions:ThreadNotificationCoordinatoruntouched so it respects fix(web): play completion sounds without delay #29146 (which intentionally removed client continuation grace) and does not conflict with fix(web): "Thread completed" alerts arrive with the thread, not 2 seconds before it #38318. With the server fix in place, the shell transitions directly from held → queued with no settled gap.thread.background-work.settlealready disposes pending/claimed cohorts viaholdStoppedThread, preserving the escape hatch.Verification
11:57:19.482Z, wake run Add open-in-editor feature with Cmd+O shortcut #2 requested at11:57:19.635Z(153 ms gap withpendingBackgroundTasks = []).12:29:17.256Z, wake run feat: Github Integration #5 requested at12:29:17.397Z(141 ms gap withpendingBackgroundTasks = []).upstream/main(620cf4770d) and pass on this branch (4793e46c92):upstream/main:packages/shared/src/orchestrationV2PendingBackgroundWork.test.tsfails: dropsnode:claimed,node:claimed-cancelled,node:claimed-queued, andnode:pending-failed.apps/server/src/orchestration-v2/ProjectionStore.test.tsfails acrossgetThreadShell,getShellSnapshot,getAgentAwarenessSnapshot, andgetSettlementCandidates:4793e46c92:claimedwith no wake run or aqueued/cancelledwake run; releases immediately once the wake run is running/completed (beforefinalizeDelegatedCompletionDeliveryruns); excludesprovider_native,disposed/delivered/acknowledged, stopped cohorts, rolled-back runs, and archived threads; verifiedEXPLAIN QUERY PLANusesorchestration_v2_projection_subagents_pending_delivery_idx.vp run check:changedpasses cleanly on4793e46c92:format(0.3s),lint(0 errors),knip1/2 & 2/2,graphql,typecheck(37/37 packages),test(8/8 files, 212/212 tests includingProjectionStore.test.ts63/63,Migrations.test.ts11/11,reconcileV2PreviewMigration.test.ts5/5,orchestrationV2PendingBackgroundWork.test.ts22/22), and React Compiler coverage.Written with Gemini 4 Neon (Antigravity) and audited with Claude Opus 5.5 (Claude Code) in T3 Code.