Skip to content

fix(routing): rebuild the request-history index off the serving event loop (#6990) - #6999

Closed
lidge-jun wants to merge 22 commits into
devfrom
codex/6990-routing-history-rebuild-off-loop-261012
Closed

lidge-jun wants to merge 22 commits into
devfrom
codex/6990-routing-history-rebuild-off-loop-261012

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Summary

Closes #6990.

Rebuilding the request-history index ran on the serving event loop. One rebuild of a 2.34 GB ledger froze every provider for 487 s. With this change the proxy keeps serving while the index rebuilds, and the SQLite calls that stalled hosted Windows runners for 0.5 to 1.8 s (opening, closing, renaming and checkpointing index files) no longer run on the serving thread.

  • Large rebuilds run on a worker thread. For a ledger of 1 MiB or more, a Bun Worker (src/routing/history/rebuild-worker.ts, driven by rebuild-job.ts) does the whole rebuild:

    • it first holds the superseded generation open, so the serving thread's close of it is not the last connection;
    • it creates the private build and ingests the ledger up to the size the refresh recorded;
    • it switches the build to WAL, closes it and renames it to its generation name;
    • it writes once into the new WAL, then keeps the generation open until the serving thread has opened it.

    Smaller ledgers keep the inline cooperative build (25 ms async slices, 50 ms routing-time sync budget).

  • Checkpoints and cleanup run on workers. Serving connections use synchronous = NORMAL and wal_autocheckpoint = 0.

    • Once a tail leaves the WAL at 4 MiB or more, a worker runs wal_checkpoint(TRUNCATE) and then makes the first write into the emptied WAL itself, so the WAL restart and its header sync happen on the worker. Tails into that generation wait for the job.
    • A checkpoint a reader held back is retried a bounded number of times, between flights. A request for another generation that arrives while a job runs is kept and started afterwards.
    • Old generations and the pre-generation routing-history.sqlite are reclaimed by a worker.
    • Closing a connection whose WAL is 1 MiB or more, and any non-empty WAL closed by ledger retention, goes through a worker that holds the file first, so the last close and its checkpoint run there.
  • Routing never waits. The routing-time sync entry never runs worker requests. It hands the refresh to the background and throws RequestHistoryIndexBusyError, which health scoring reads as unknown evidence (unknownEvidence.health). The health cache is tied to an index epoch.

  • Published generations. A full rebuild publishes under a new name in <config>/routing-history/. The name is taken at publication and sorts strictly above every generation already published, with zero-padded fields, so a clock that moved back cannot make a new generation sort below an old one. Readers see the previous complete index or the new one.

  • Retryable errors. Busy and closed index errors answer 503 with Retry-After: 1 at the management boundary, for the list, row, route-decision and analytics routes.

  • Exact resume and specific reasons. Rows, indexed_offset and indexed_rows commit together. The per-refresh quick_check is gone; a corruption error from any statement schedules a rebuild. last_error keeps the reason that fired.

  • Earlier rounds on this branch, carried here: Windows-safe index paths, index-backed routing-health queries, a MEMORY journal and adaptive sub-transactions for the inline build, and publication I/O off the loop for the inline path.

Known limits: ledgers under 1 MiB still rebuild inline on the serving thread (a few short steps). A worker that overruns its 5 s exit bound gets its build-file cleanup when it does exit; test teardown does not wait for that. Nothing new reaches src/lab from src/router.ts, src/server/lifecycle.ts or src/server/responses/core.ts.

Verification

  • tests/server/server-history-rebuild-responsiveness.test.ts, bounds unchanged (event-loop lag < 300 ms, every probe < 1 s, at least 5 completed /healthz, dry-run and proxied requests during a 120,000-row cold rebuild). macOS, Bun 1.4.3: worker path max lag 29 to 34 ms, inline path 55 to 57 ms. Hosted Windows 6/9 is the deciding run.

  • New tests/usage/request-history-rebuild-worker.test.ts (22 tests):

    • the worker build equals the inline build on an edge-case ledger;
    • no ingest step runs on the serving thread;
    • busy handoff; close during a build; snapshot boundary; ledger replacement restart;
    • generation naming;
    • autocheckpoint off; worker checkpoint and its bounded retry; WAL kept bounded under many small tails;
    • tails wait during a checkpoint;
    • WAL header salts unchanged across the next serving tail after a checkpoint, a reader-blocked checkpoint and a worker publication;
    • deferred and retention closes;
    • a damaged old generation;
    • retry lifetime isolation and a new generation's checkpoint overlapping an old job.

    Each fix from review rounds 6 to 8 was confirmed to fail with its guard removed.

  • New tests/server/server-history-index-unavailable.test.ts: a history read cut off by an index close answers 503 with Retry-After: 1.

  • Focused gate with Bun 1.4.3: 1,330 pass / 0 fail across 72 files (all of tests/usage, routing analytics, responsiveness, Lab boundary, test layout, structure SSOT, file-size ratchet).

  • Full suite, Bun 1.4.3, at the first worker commit: 45,133 tests. The failures were in three files: claude-desktop-remote-hub and codex-runtime fail identically on unmodified dev on this machine; cli-access-audio-live hits its 20 ms readiness budget under parallel load and passes alone.

  • bun run typecheck, bun run structure:check, bun run privacy:scan: pass.

  • Independent review: GPT-6.1-Sol (high) over 9 rounds through GOAT, plus GPT-6-Luna for round 2 while Sol was at capacity. Every finding was fixed with a regression test. The reviewer accepted one decline: test teardown does not wait indefinitely for a worker stuck in native I/O. Round 9 returned PASS.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed. (docs-site routing reference, structure/dashboard-and-usage.md, devlog dispositions)
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults. (No auth or credential paths touched; privacy scan green.)

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants