Skip to content

perf(orchestration): delete settled Runs from orchestration.db a day after their last activity - #43322

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/orch-run-retention
Nov 17, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/orch-run-retention

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​290 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​290
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​397 $\color{#cf222e}{\Huge{\mathbf{−}}}$​19 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​378

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.

  • One owner, no new timer. The existing database maintenance pass (the one that prunes settled chat output copies) now runs hourly instead of every six hours. In the same pass it deletes retired Runs and expired rows. It keeps that pass's pacing: each step is one short transaction, and the pass rests nine times as long as it worked, so it uses about a tenth of the main thread.
  • A Run is deleted only when all of the following hold. Each is checked again inside the delete transaction.
    • It is not legacy, remote-homed, a peer Run, adopted, or linked to a remote server (remote_run_links, remote_placements, remote_dispatches, remote_owned_questions).
    • It has no pending, ready, dispatched or blocked Task, and no pending or dispatched Dispatch.
    • No worker or remote attachment is in a live state. No worker terminal is still owned and unreleased, has an armed auto-release, or has been handed to another Dispatch. No home auto-release is armed.
    • No question, decision gate or remote question is pending, and no relay item is unacked.
    • No row of the Run was written in the last 24 hours (Run, Task, Dispatch, worker, message, question, gate or delivery).
    • Every coordinator the Run bound is gone (its handles, its 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.
  • One Run per transaction. Rows keyed by Dispatch go first, while the Dispatch still names its Run, then rows keyed by Run, then the Run itself. Its triggers drop its coordinator handles and removed-work records. Rows that reset clears but the original branch missed are now deleted too: structured_mail_sends of the Run's messages, the Run mailbox's structured_pointer_operations row, and message_dispatch_provenance, now split into two indexed deletes.
  • Expired rows are swept table by table with a rowid cursor, so each step examines at most 256 rows:
    • relay items acked more than 24 hours ago, except the newest row of each stream (cursors and enqueue sequences continue from it) and peer conversations;
    • worker rows, worker terminals and terminal archives whose Dispatch is gone;
    • removed-work records whose Run is gone.
  • Bounded steps. Candidate selection examines 64 Runs per step, kept or not. Six indexes keep every per-Run lookup and delete off full scans: 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) and remote_placements(run_id). They are created on open alongside the existing CREATE INDEX IF NOT EXISTS set. The archive orphan check reads only dispatch_id, so it never pages through saved output copies.
  • No 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 orchestrationRunRetentionHours setting, 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.db until someone ran orca 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 list took 12 s at 88 MB and up to 116 s at 500 MB under load.

Perf evidence

Same machine (Omarchy, Linux, 28 cores), main vs this branch, using the issue's two shapes. The harness builds the database through the real OrchestrationDb API, 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 have auto_vacuum off, so that is what the base databases use.

main this PR
88 MB shape (251 Runs over 30 h): file after a day 81 → 197 MB, +4.8 MB every hour, 611 Runs kept 81 → 128 MB, the last 24 h only (371 Runs); flat once the window is full
500 MB shape (1,371 Runs, 10,960 Dispatches, 43,840 messages over 90 days): file after a day 555 → 697 MB, 1,731 Runs 555 MB, no growth. The first pass deletes 1,301 settled Runs (live, abandoned and legacy stay) and the day's writes reuse the 511 MB it frees
88 MB, new profile (incremental auto-vacuum) 81 → 198 MB 81 → 74 MB in the first 3 h, then the last 24 h only (125 MB)
Longest single main-thread step 0.3 ms (nothing to do) 9.8 ms (500 MB), 3.4 ms (88 MB)
Event-loop delay during a pass, max 3 ms 10 ms
Work per pass (500 MB) ~0.1 ms first pass 2.5 s of work, spread over about 25 s by the pacing; later passes p50 19 ms, p95 35 ms
Work per pass (88 MB) ~0.1 ms p50 12 ms, p95 15 ms, max 47 ms
Building the six indexes on first open (500 MB shape) — 6.3 ms

The 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.ts covers:

    • deleting a settled Run with every row it owns, leaving no removed-work records, while an unsettled Run stays;
    • keeping a Run while a coordinator it bound (by handle, run: mailbox or pane), a reader of its unread mail, or its worker terminal is live;
    • keeping a Run touched inside the window, one with an armed auto-release, and one whose worker terminal was handed to another Run;
    • deleting a Run whose terminal moved between its own Dispatches;
    • deleting acked relay items except the newest of each stream;
    • deleting orphaned worker rows.
  • Query plans for the selection and delete statements were checked. The only full scan left is of the legacy legacy_compatibility_principals table, which is normally empty.

  • Review: three passes by fresh reviewers, and then a council for the final repair (3/3 ready). Fixed findings:

    • a terminal moved between Dispatches of the same Run blocked that Run forever;
    • selection and Task-delete lookups were unindexed (the attempt-fact trigger);
    • candidate pages did not bound the Runs they examined;
    • expired-row batches rescanned retained rows;
    • a recent relay ack did not count as activity;
    • a worker terminal the user took over did not keep its Run;
    • a shorter receipt window would have shortened connector delivery dedupe, so it was dropped.
  • 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:changed on Linux: all 25 checks pass, including typecheck, lint, format and every ratchet, apart from one unrelated test. crew-read-model.test.ts failed in the shared pool with store.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-show at 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

  • Cross-platform: SQLite and timers only; no paths or processes.
  • Remote hosts: each runtime prunes its own database. Remote-homed, peer and remote-linked Runs are never deleted, and relay streams keep their newest row.
  • Mobile: no change.
  • Folder workspaces: not git-specific.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…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>
@coderabbitai

coderabbitai Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds 24-hour eligibility checks and transactional deletion for settled runs, with liveness checks for associated handles and panes. It adds paged sweeps for expired relay items and orphaned orchestration rows. Scheduled maintenance now runs these operations every minute and accepts the liveness callbacks they require. The change also adds database indexes and tests for run retirement and expired-row cleanup.

Merge Risk: Minimal · up to 6f75e

This change deletes settled orchestration Runs and expired rows through the existing hourly maintenance pass, in bounded, indexed batches with liveness rechecks inside each deletion transaction. No concrete defect or performance regression was established, so it looks safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: settled orchestration Runs are deleted from orchestration.db a day after their last activity. The changeset adds retention and expired-row sweeps for this purpose, and…
Description check Passed The description follows the repository template. It covers ELI5, What Changed, Why, a Linked Issue (Tracked in #41756, building on #41757), Visual Proof (N/A with reason), Testing, Risk (Level: Medium…
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.
Full details: Title check

Explanation

The title names the main change: settled orchestration Runs are deleted from orchestration.db a day after their last activity. The changeset adds retention and expired-row sweeps for this purpose, and the title's 'perf(orchestration)' prefix matches the stated goal of stopping database growth and speeding CLI reads.

Full details: Description check

Explanation

The description follows the repository template. It covers ELI5, What Changed, Why, a Linked Issue (Tracked in #41756, building on #41757), Visual Proof (N/A with reason), Testing, Risk (Level: Medium, with a revert note), AI Disclosure, Review, the upstream boundary checkbox, and the checklist. It gives detailed perf evidence and names the test file. Minor gaps: the Linked Issue section uses 'Tracked in' rather than the 'Fixes #' format, and the template's 'Fixes #' line is not filled in. The 'Level' and AI Disclosure are filled in. Overall the description is mostly complete.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b51cc56b-84b0-49d3-9b1e-9e3c36cf28de
Commits

Reviewing files that changed from the base of the PR and between 6ee09ca and 6f75efc.

Files selected for processing: 6

  • src/main/runtime/orchestration/db/maintenance/orchestration-db-maintenance.ts
  • src/main/runtime/orchestration/db/maintenance/settled-run-retention.test.ts
  • src/main/runtime/orchestration/db/maintenance/settled-run-retention.ts
  • src/main/runtime/orchestration/db/orchestration-db.ts
  • src/main/runtime/orchestration/db/runtime-orchestration-database-access.ts
  • src/main/runtime/orchestration/mailbox/routing/mailbox-delivery-target.ts

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

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Status: code ready for review. Tracked in #41756.

Problem. orchestration.db never deleted a Run, so heavy orchestration users saw it reach 242 MB in a day, or about 500 MB in three months, and CLI calls slowed to seconds or minutes.

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, main vs this PR, a simulated day at about 350 Runs/day):

  • 88 MB shape: main grows to 197 MB (+4.8 MB/h, unbounded). This PR ends at 128 MB, the last 24 h of Runs only, and stays there.
  • 500 MB shape: main grows to 697 MB. This PR deletes 1,301 settled Runs in its first pass, and the file stays at 555 MB because the day's writes reuse the freed pages. There is no VACUUM, by design.
  • Longest main-thread step: 9.8 ms. Max event-loop delay: 10 ms. The first 500 MB pass is 2.5 s of work spread over about 25 s. Building the indexes on open took 6.3 ms.

Review.

  • Pass 1 (Codex) found four P1s. All are fixed: a terminal moved within one Run blocked it forever; lookups and the Task-delete trigger were unindexed; candidate pages had no bound on the Runs they examined; expired-row batches rescanned.
  • Pass 2 (Codex) found the rescan only half fixed. It now uses rowid cursors.
  • Pass 3: Codex clean. Claude found two P2s, both fixed: a worker terminal the user took over now keeps its Run, and the receipt-window change was dropped because connector dedupe relies on 30 days.
  • The final repair went to a council: 3/3 ready, no P0/P1.
  • An earlier council chose the coordinator rule (keep every Run a live coordinator bound) 2 to 1 over capped windows.

Deferred.

  • While a coordinator's terminal or chat stays open, its Runs stay too. Bounding that needs a separate mechanism, such as refreshing a Run on reference with a clear "retired" error.
  • Messages with no Run are never pruned.
  • legacy_compatibility_principals is scanned once per Run delete; the table is small.

Verified.

  • settled-run-retention.test.ts: 8 cases.
  • Query-plan check of every selection and delete statement.
  • pnpm check:changed on Linux passes apart from the shared-pool flake in crew-read-model.test.ts (store.listRecords is not a function), which shows up on other branches and passes alone.
  • CI.

@brennanb2025
brennanb2025 merged commit 70a3667 into main Nov 17, 2026
47 checks passed
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
brennanb2025 added a commit that referenced this pull request Nov 17, 2026
…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.
Jinwoo-H pushed a commit that referenced this pull request Nov 17, 2026
…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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant