Repository navigation
perf(orchestration): delete settled Runs from orchestration.db a day after their last activity - #43322
Conversation
…after their last activity The hourly database maintenance pass now deletes each Run whose work all settled and that nothing touched for 24 hours, one Run per short write transaction at a tenth of the thread, unless a coordinator it bound or a reader of its unread mail is still live. It also deletes acknowledged relay items (keeping each stream's newest) and worker rows whose Dispatch is gone. Co-authored-by: Yasyf Mohamedali <yasyf@poetic.com>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
Status: code ready for review. Tracked in #41756. Problem. User-facing change. Orca now deletes a settled Run's records 24 h after anything last touched it, in the existing hourly database maintenance pass. There is no setting and nothing to run. Live work is never deleted: pending or active Tasks, Dispatches and workers; pending questions and gates; unacked relay items; armed auto-release; remote, peer and legacy Runs; a Run whose coordinator, unread-mail reader or worker terminal is still live. Acked relay items (each stream keeps its newest) and worker rows whose Dispatch is gone are pruned too. This builds on @yasyf's #41757, credited as co-author. Perf (Omarchy,
Review.
Deferred.
Verified.
|
…thread never stalls The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last after the same live-party and retirement checks. Every page re-checks both, so a pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages examine 8 Runs instead of 64 and the orphaned-output-copy sweep deletes at most 8 per step.
…thread never stalls The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. Party liveness is read before the first page and again before the Run row; every page re-checks that the Run is still settled, so a pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages examine 8 Runs instead of 64 and the orphaned-output- copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs.
…thread never stalls The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. Party liveness is read before the first page and again before the Run row, and pane liveness (an in-memory lookup) plus the settled check on every page, so a reattached coordinator or a new write stops the deletes; a pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages examine 8 Runs instead of 64 and the orphaned-output- copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs.
…thread never stalls The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. The full settled check runs once first and again before the Run row; each page checks only pane liveness (an in-memory lookup) and whether the Run was touched (a newer updated_at or newer row, 0.07 ms), so a reattached coordinator or a new write stops the deletes. A pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages now read only Run-level columns (the full check for 8 large Runs took up to 197 ms cold), and the orphaned-output-copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs.
…thread never stalls The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. The full settled check runs once first and again before the Run row; each page checks only pane liveness (an in-memory lookup) and whether the Run was touched (a newer updated_at or newer row, 0.07 ms), so a reattached coordinator or a new write stops the deletes. A pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages now read only Run-level columns (the full check for 8 large Runs took up to 197 ms cold), and the orphaned-output-copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs. While a pass prunes, WAL checkpoints run on a worker thread's own connection (PASSIVE, every 250 ms) and main's auto-checkpoint is off: a commit that crossed the 1000-page threshold checkpointed and fsynced the 1.7 GB file on main, which a CPU profile of the pass showed as every remaining stall (up to 934 ms on a busy disk). The worker failing or exiting hands checkpoints back to main at once, and the pass restores the previous threshold when it ends.
…thread never stalls The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. The full settled check runs once first and again before the Run row; each page checks only pane liveness (an in-memory lookup) and whether the Run was touched (a newer updated_at or newer row, 0.07 ms), so a reattached coordinator or a new write stops the deletes. A pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages now read only Run-level columns (the full check for 8 large Runs took up to 197 ms cold), and the orphaned-output-copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs. While a pass prunes, WAL checkpoints run on a worker thread's own connection (PASSIVE, every 250 ms) and main's auto-checkpoint is off: a commit that crossed the 1000-page threshold checkpointed and fsynced the 1.7 GB file on main, which a CPU profile of the pass showed as every remaining stall (up to 934 ms on a busy disk). The worker failing or exiting hands checkpoints back to main at once, and the pass restores the previous threshold when it ends.
…thread never stalls The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. The full settled check runs once first and again before the Run row; each page checks only pane liveness (an in-memory lookup) and whether the Run was touched (a newer updated_at or newer row, or reopened work: a pending Task, Dispatch, gate or question, all indexed probes), so a reattached coordinator, a new write or a gate opened mid-way stops the deletes before a page removes the work the final check would have kept the Run for. A pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages now read only Run-level columns (the full check for 8 large Runs took up to 197 ms cold), and the orphaned-output-copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs. While a pass prunes, WAL checkpoints run on a worker thread's own connection (PASSIVE, every 250 ms) and main's auto-checkpoint is off: a commit that crossed the 1000-page threshold checkpointed and fsynced the 1.7 GB file on main, which a CPU profile of the pass showed as every remaining stall (up to 934 ms on a busy disk). The worker failing or exiting hands checkpoints back to main at once, and the pass restores the previous threshold when it ends.
…thread never stalls (#43550) The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. The full settled check runs once first and again before the Run row; each page checks only pane liveness (an in-memory lookup) and whether the Run was touched (a newer updated_at or newer row, or reopened work: a pending Task, Dispatch, gate or question, all indexed probes), so a reattached coordinator, a new write or a gate opened mid-way stops the deletes before a page removes the work the final check would have kept the Run for. A pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages now read only Run-level columns (the full check for 8 large Runs took up to 197 ms cold), and the orphaned-output-copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs. While a pass prunes, WAL checkpoints run on a worker thread's own connection (PASSIVE, every 250 ms) and main's auto-checkpoint is off: a commit that crossed the 1000-page threshold checkpointed and fsynced the 1.7 GB file on main, which a CPU profile of the pass showed as every remaining stall (up to 934 ms on a busy disk). The worker failing or exiting hands checkpoints back to main at once, and the pass restores the previous threshold when it ends.
…thread never stalls (#43550) The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. The full settled check runs once first and again before the Run row; each page checks only pane liveness (an in-memory lookup) and whether the Run was touched (a newer updated_at or newer row, or reopened work: a pending Task, Dispatch, gate or question, all indexed probes), so a reattached coordinator, a new write or a gate opened mid-way stops the deletes before a page removes the work the final check would have kept the Run for. A pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages now read only Run-level columns (the full check for 8 large Runs took up to 197 ms cold), and the orphaned-output-copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs. While a pass prunes, WAL checkpoints run on a worker thread's own connection (PASSIVE, every 250 ms) and main's auto-checkpoint is off: a commit that crossed the 1000-page threshold checkpointed and fsynced the 1.7 GB file on main, which a CPU profile of the pass showed as every remaining stall (up to 934 ms on a busy disk). The worker failing or exiting hands checkpoints back to main at once, and the pass restores the previous threshold when it ends. (cherry picked from commit c5ea444) Conflict: src/main/worker-threads/worker-thread-roles.ts (added by #43327, not on this branch) removed; this branch has no worker role list. Also rewords the page-size doc comment in settled-run-retirement.ts.
…thread never stalls (#43550) The hourly pass from #43322 deleted each settled Run, every message included, in one synchronous step. On a real 1.7 GB orchestration.db that blocked the main thread for up to 1.3 s per large Run (98 one-second windows over 100 ms in the first pass). Each Run is now retired in bounded steps, each its own transaction, with the pass's rest between them: its coordinator panes are read 256 messages at a time, then its rows are deleted one table page at a time (128 rows; 64 messages; 8 saved output copies; removed-work records too), and the Run row goes last. The full settled check runs once first and again before the Run row; each page checks only pane liveness (an in-memory lookup) and whether the Run was touched (a newer updated_at or newer row, or reopened work: a pending Task, Dispatch, gate or question, all indexed probes), so a reattached coordinator, a new write or a gate opened mid-way stops the deletes before a page removes the work the final check would have kept the Run for. A pass stopped by a quit or crash leaves the Run with fewer settled rows for the next pass to finish. Candidate pages now read only Run-level columns (the full check for 8 large Runs took up to 197 ms cold), and the orphaned-output-copy sweep deletes at most 8 per step. The settled-Run check's moved-terminal clause used NOT IN over the Run's Dispatches, which SQLite rebuilt per row: 133 ms per check for a Run with 675 Dispatches, 8.3 s to classify all 1,226 Runs. It is now NOT EXISTS on the Dispatch id: 11 ms and 0.45 s, selecting the same Runs. While a pass prunes, WAL checkpoints run on a worker thread's own connection (PASSIVE, every 250 ms) and main's auto-checkpoint is off: a commit that crossed the 1000-page threshold checkpointed and fsynced the 1.7 GB file on main, which a CPU profile of the pass showed as every remaining stall (up to 934 ms on a busy disk). The worker failing or exiting hands checkpoints back to main at once, and the pass restores the previous threshold when it ends. (cherry picked from commit c5ea444)
ELI5
Orca never threw away finished orchestration work, so its database file kept growing until CLI calls slowed to seconds or minutes. Now Orca deletes a finished Run's records a day after anything last touched it, in the background, and never touches work that is still live.
What Changed
Once a Run's work has settled and nothing has touched it for 24 hours, Orca deletes the Run and its records in the background. There is no setting and nothing to run. Live work is never touched.
remote_run_links,remote_placements,remote_dispatches,remote_owned_questions).run:mailbox, its coordinator session, and every pane its coordinators worked from). So is every reader of its unread mail, and every worker terminal of the Run, including one handed to the user. A coordinator keeps every Run it created and can name any of them again with--run <id>, so retention leaves all of them alone while that coordinator is alive.structured_mail_sendsof the Run's messages, the Run mailbox'sstructured_pointer_operationsrow, andmessage_dispatch_provenance, now split into two indexed deletes.question_threads(run_id),worker_terminal_resources(origin_dispatch_id),attempt_observation_facts(task_id)(its Task delete trigger),message_dispatch_provenance(dispatch_id),remote_run_links(run_id)andremote_placements(run_id). They are created on open alongside the existingCREATE INDEX IF NOT EXISTSset. The archive orphan check reads onlydispatch_id, so it never pages through saved output copies.VACUUM. Freed pages are reused by later writes, and databases created with incremental auto-vacuum give them back through the existing reclaim step.Compared with #41757: this keeps its retirement and liveness rules and its delete order. It runs inside the existing maintenance owner instead of a second scheduler. It drops the
orchestrationRunRetentionHourssetting, since a fixed window is enough and 0/off would be a hidden switch with no UI. It drops receipt compaction and the receipt-window change: connector delivery dedupe relies on the 30-day receipts, and the existing 10,000-row cap already bounds them. It also covers the tables above and the review findings below.Limitation
Every Run a still-live coordinator bound is kept. If a coordinator's terminal or chat stays open for days, its own Runs stay with it. They go once it closes. Deleting them earlier would break coordinators that come back to an older Run id. Bounding that case needs a separate mechanism, for example refreshing a Run when it is referenced and returning a clear "retired" error, and is left for a follow-up.
Why
Orca never deleted an orchestration Run. Every Task, Dispatch, message, question, relay item and worker row stayed in
orchestration.dbuntil someone ranorca orchestration reset, and reset removes all Runs, live ones included. Heavy users saw the file reach 242 MB in a day, or about 500 MB after three months. Every CLI call that reads the database slowed down with it:orca terminal listtook 12 s at 88 MB and up to 116 s at 500 MB under load.Perf evidence
Same machine (Omarchy, Linux, 28 cores),
mainvs this branch, using the issue's two shapes. The harness builds the database through the realOrchestrationDbAPI, then simulates a day. Each hour it moves every timestamp back an hour, adds 15 settled Runs (about 350 a day), and runs one maintenance pass. Live coordinators hold 1 in 25 and 1 in 50 of the base Runs; the 500 MB shape also has 13 abandoned open Dispatches. Existing profiles haveauto_vacuumoff, so that is what the base databases use.mainThe file does not shrink on existing profiles: there is no
VACUUM, by design. It stops growing, because freed pages are reused. New profiles, created with incremental auto-vacuum, give pages back through the existing reclaim step.Linked Issue
Tracked in #41756. This builds on @yasyf's proposal and branch in #41757; their commit is credited as co-author.
Visual Proof
N/A: no UI change. The effect is a database that stops growing, and faster orchestration reads.
Testing
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
settled-run-retention.test.tscovers:run:mailbox or pane), a reader of its unread mail, or its worker terminal is live;Query plans for the selection and delete statements were checked. The only full scan left is of the legacy
legacy_compatibility_principalstable, which is normally empty.Review: three passes by fresh reviewers, and then a council for the final repair (3/3 ready). Fixed findings:
The coordinator rule (keep every Run a live coordinator bound) was put to a council against shorter or capped windows; it chose this rule 2 to 1.
pnpm check:changedon Linux: all 25 checks pass, including typecheck, lint, format and every ratchet, apart from one unrelated test.crew-read-model.test.tsfailed in the shared pool withstore.listRecords is not a function. The same failure shows up in other branches' runs on that machine, and the test passes when run alone.Risk
Level: Medium
The risk is deleting a Run something still uses. Every guard is checked again inside the delete transaction, and a party added since the liveness read keeps the Run. A wrong verdict would show as a Run missing from
orca orchestration run-showat least a day after its last activity. Reverting stops retention; Runs already deleted stay deleted.AI Disclosure
Written with Claude Code (Claude Opus 5.5); reviewed by Claude and OpenAI Codex reviewers.
Review
Agent skill upstream boundary
Notes
Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)