Skip to content

perf(server): boot no longer re-finalizes live subagent children - #32211

Open
brauliobo wants to merge 1 commit into
pingdotgg:mainfrom
brauliobo:perf/boot-subagent-recovery
Open

brauliobo wants to merge 1 commit into
pingdotgg:mainfrom
brauliobo:perf/boot-subagent-recovery

Conversation

@brauliobo

Copy link
Copy Markdown
Contributor

Problem

Every server restart spends about 7 s in server.startup.orchestration-v2.delegated-tasks.recover before 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 in getRecoveryThreadIds("subagent-results"), then nine finalizeAppOwnedSubagent calls 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-results selector matched any terminal run of the task, but finalizeAppOwnedSubagentTask acts 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-results selector in ProjectionStore.getRecoveryThreadIds now 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 that delegatedTaskId) to be terminal. That is the run finalize looks at; runOutcome only maps completed/cancelled to interrupted, 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 no subagent_result transfer) and the child-thread lineage predicate, which still uses orchestration_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.ts gains 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 on main (the child is returned while still running) and passes with the fix. The existing plan assertion that the query uses threads_subagent_result_idx still 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:

before after
subagent-results candidates 7 0
subagent-results query, two runs each 0.76 s, 0.85 s 0.54 s, 0.57 s

The 7-9 avoided finalizeAppOwnedSubagent calls 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.

  • The remaining query time is mostly reading about 5k subagent payloads for result IS NULL. I did not find a column-based prefilter I could prove equivalent, so it is left as is.
  • Other boot work is out of scope: restart continuations and client resubscribes after ready, and about 5 s of node startup before server.startup begins.
  • ProviderRuntimeRecoveryService.reconcile (about 1.1 s in the trace) and the runtime recovery 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), plus ProjectionRecovery and ProjectionStore suites in full.

Written by Claude Sonnet 5.5 in T3 Code.

🤖 Generated with Claude Code

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]>
@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Nov 6, 2026
@coderabbitai

coderabbitai Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The subagent-results recovery query now checks the latest run for a delegated task. A regression test verifies that an active latest run excludes the child from recovery, while a completed latest run makes it eligible.

Changes: Subagent recovery eligibility

Layer / File(s) Summary
Latest-run recovery eligibility
apps/server/src/orchestration-v2/ProjectionStore.ts, apps/server/src/orchestration-v2/ProjectionRecovery.test.ts
The query selects app-owned child threads without a recorded result only when the latest run for the delegated task has a terminal status. The test checks that an active latest run blocks recovery and that adding a completed run makes the child eligible.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: yash-singh1

Merge Risk: Minimal · up to 6d3ad

The change prevents startup recovery from selecting a child whose latest delegated run is still live. No actionable merge-blocking risk is established.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title clearly describes the startup recovery change.
Description check Passed The description covers the problem, fix, scope, and verification. The Fix and Evidence headings serve the template's Change and Verification sections.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b90e554 and 6d3ad8a.

Files selected for processing: 2

  • apps/server/src/orchestration-v2/ProjectionRecovery.test.ts
  • apps/server/src/orchestration-v2/ProjectionStore.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@Dropje97

Copy link
Copy Markdown

The same reselection happens on our server, on 0.1.10-nightly.20261112.4606 with local patches that do not touch recovery. The database has about 7,500 threads and statev2.sqlite is 49 GB.

  • Production boots. At two boots 1.5 hours apart, the subagent-results selector returned 9 and 8 children. At the second boot, the selector took 8.8 s and the finalizeAppOwnedSubagent reads that followed took 16.2 s. The server answered HTTP 40 s after the restart.
  • On a copy of the database. The selector, which is unchanged on main at 5f69cac5c9, returns 4 children; this PR's selector returns none.
  • Selector time. It stays about 9 s, warm, both before and after this PR. The first branch takes 5.4 s before and 4.2 s after, the queued-work branch 4.7 s, and the outer thread scan 0.14 s. So this PR removes the finalize calls, and the selector itself remains the larger part of this phase on a large database.

Measured and written by Claude (Opus 5.5) in T3 Code; posted with the owner's approval.

@brauliobo

Copy link
Copy Markdown
Contributor Author

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.

@maria-rcks

maria-rcks commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Note

Written by gpt-6.1-sol on behalf of Maria

Verified in a real dev environment

verified head 6d3ad8a77dcd79c627e2fad36ab36b57065e7fb0 against github/main at 989c1b55b016b2e32b605b291ecedf7fa81eb5c2 on linux, using repository playwright dependencies, google chrome, and real codex gpt-6.1-sol turns through the local proxy. no source changes were needed.

  • created a project for a real git repo through the app, delegated an app-owned child, received CHILD_INITIAL_OK, sent follow-up work, and restarted during a provider turn. the child then queued more work to itself on main.
  • booted both versions sequentially from the same app-created database snapshot. the task had older cancelled runs, a newer held queued run, and no result. actual startup traces showed main: 1 recovery candidate / 1 finalizer call; pr: 0 candidates / 0 finalizer calls. the pr resumed the child and preserved its queued messages. the shared snapshot includes additive migrations from newer main.
  • checked nearby behavior in the pr app: stop interrupted the child; a fresh follow-up returned NEIGHBOR_OK, which the parent acknowledged; sending the saved queued message returned QUEUED_SECOND_OK.
  • blacksmith checks passed: 73 tests across ProjectionRecovery and ProjectionStore; check:changed against the pr parent passed format, lint, knip, graphql, affected typechecks, and 75 tests.

command transcript

real child follow-up and saved queued message completed

stop and fresh follow-up through the real codex client path

Not verified

native 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.

This branch has not been deployed

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

Labels

size:S 10-29 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.

3 participants