Repository navigation
Conversation
The subagent-results recovery query selected a child thread when any run of its task was terminal while the task had no result. Finalizing only acts on the task latest run, so long-lived worker children whose earlier run was restart-cancelled but whose newest run is live matched on every boot and were re-read and left unchanged, each serialized behind its parent dispatch lock. The query now starts from tasks that still lack a result and requires the latest run of the task to be terminal. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
The same reselection happens on our server, on
Measured and written by Claude (Opus 5.5) in T3 Code; posted with the owner's approval. |
|
Thanks for measuring this on a large database. It matches the intent here: the PR removes the per-child finalize reads at boot, and the selector query itself is outside its scope. The ~9 s selector time on a 49 GB database is a separate cost; I'll look at it as its own change rather than widening this PR. |
|
Note Written by Verified in a real dev environmentverified head
Not verifiednative desktop/mobile clients, other providers, relay/tunnel connections, and production-size database timing. this verifies selection and avoided finalizer calls, without claiming a measured startup speedup. a simple interrupted active turn without newer queued work remains eligible on both versions; that is outside the filtered case. |


Problem
Every server restart spends about 7 s in
server.startup.orchestration-v2.delegated-tasks.recoverbefore the server is ready. In one real boot (server ready at +13.5 s, startup phases 8.3 s), that phase took 7.17 s: 0.9-1.3 s ingetRecoveryThreadIds("subagent-results"), then ninefinalizeAppOwnedSubagentcalls of about 0.65 s each. They run one after another because they share the parent thread's dispatch lock. Each reads the child thread, finds nothing to do, and writes nothing, so the same nine come back on every boot.The nine are long-lived worker children. Their task has no result yet, an earlier run of the task was cancelled by a restart, and a newer run of the same task is live. The
subagent-resultsselector matched any terminal run of the task, butfinalizeAppOwnedSubagentTaskacts only on the task's latest run and returns when that run is not terminal. So selection and finalizing disagreed, and the set never converged.Fix
The first branch of the
subagent-resultsselector inProjectionStore.getRecoveryThreadIdsnow starts from app-owned tasks that still lack a result (a small set) and requires the task's latest run (by ordinal, among runs with thatdelegatedTaskId) to be terminal. That is the run finalize looks at;runOutcomeonly mapscompleted/cancelledtointerrupted, so terminal status and terminal outcome agree. The result is a subset of what the old branch selected, minus the children finalize would skip. The second branch (latest unheld run terminal and nosubagent_resulttransfer) and the child-thread lineage predicate, which still usesorchestration_v2_projection_threads_subagent_result_idx, are unchanged. No schema change.Scope and approval
This changes which rows a startup recovery query returns so that it matches what the recovery step acts on. It adds no feature and changes no product default. It is one problem, restart recovery re-reading children it cannot change. I found no issue tracking it, so there is no maintainer-triaged issue to link.
Evidence
Regression test.
ProjectionRecovery.test.tsgains a case in which a child's task has an earlier cancelled run and a newer running run, with the child's result transfer already recorded so only the first branch can select it. It asserts the child is not returned while the latest run is live, and is returned once that run is terminal. It fails onmain(the child is returned while still running) and passes with the fix. The existing plan assertion that the query usesthreads_subagent_result_idxstill passes, as do the other recovery cases.Measured on a ZFS reflink copy of a real ~25 GB database (about 1,100 subagent child threads, 9 long-lived worker children at trace time; the live file was never opened for writing; back-to-back runs on one machine, warm cache). The database had moved on since the trace, so the old query now returned 7 children:
subagent-resultscandidatessubagent-resultsquery, two runs eachThe 7-9 avoided
finalizeAppOwnedSubagentcalls at about 0.65 s each, serial, are where the roughly 6 s saving comes from. I did not re-run a server boot, so that figure is the trace's per-call cost times the avoided calls, not a measured end-to-end number.What this does not show.
result IS NULL. I did not find a column-based prefilter I could prove equivalent, so it is left as is.server.startupbegins.ProviderRuntimeRecoveryService.reconcile(about 1.1 s in the trace) and theruntimerecovery kind (about 0.36 s) are unchanged; I did not look for a non-convergent cost there.Verified with
vp run check:changed(format, lint, knip, typecheck, tests for the changed files), plusProjectionRecoveryandProjectionStoresuites in full.Written by Claude Sonnet 5.5 in T3 Code.
🤖 Generated with Claude Code