Repository navigation
Conversation
The outline and timeline routes and live timeline pushes now run their whole read (thread lookup, settings, caches, SQL and projection) in a pool of up to four worker threads, each with a read-only SQLite connection and an 8 MB page cache. The main thread sends the thread ID, its in-memory rewrite generation and provider facts, and keeps served windows, deltas and push fan-out. Live pushes are single-flight per thread and coalesce appends while a build runs. Reads fall back to the main thread when a worker is unavailable. Incremental caches key on the rewrite generation instead of PRAGMA data_version; retained-output expiry and history-compacted/rewritten notifications now bump it. The stored conversation outline (thread_conversation_outlines) and its main-thread save are deleted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…worker Workers return each top-level row's digest with the timeline window, and main seeds its digest cache with them, so delta computation, tagging and served-window sizing on main stop re-stringifying and hashing every cloned row on every push. The pool size never drops below one worker, and a pool created with size 0 runs reads in-process instead of indexing an empty slot list. Co-Authored-By: Claude Opus 5.5 <[email protected]>
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
The server reads SQLite synchronously on its main thread. Conversation outlines and timeline builds (page reads and live pushes) ran their SQL and projection there, so on a slow or contended disk one large read blocked heartbeats, WebSocket traffic and HTTP routing for seconds. #7753 (0.49.0, 15 GB bb.db) shows event-loop stalls with p99 30.5 s and max 61.7 s; its attributed stall work is mostly these reads (conversation-outline 4,762 s wall, timeline-build 2,804 s), and about 85% of slow-query time is off-CPU (I/O wait). This is item 3 of #6499 (Phase A: reads).
What changed
apps/server/src/services/threads/read-pool/,apps/server/src/thread-read-worker.ts, new bundledist/thread-read-worker.js).max(1, min(4, cores/2))workers, so a 1–2 vCPU host still gets one; a pool created with size 0 runs reads in-process, each with a read-only connection (createReadOnlyConnectionin@bb/db) and an 8 MB page cache. Each thread is pinned to one worker by thread ID. A worker that crashes or fails to start has its pending and later reads run on the main thread, logsThread read worker stopped; running its reads on the main thread, and restarts after 10 s.GET /threads/:id/conversation-outline,GET /threads/:id/timelineand live timeline pushes, the worker does the thread lookup, settings,maxSeq, outline-sequence check, both route caches, SQL, projection, output shortening and tagging, inside one deferred read transaction. Main sends only the thread ID, its in-memory rewrite generation and provider facts (cached per provider-registry revision), so it does no SQLite on these paths. Main keeps served windows, delta computation and push fan-out. The main-side outline LRU and timeline window cache are deleted; their equivalents live in the workers.hash,jsonLength) with the response, and main seeds its digest cache with them. Without this, delta computation, tagging and served-window sizing on main re-stringified and sha256-hashed every structured-cloned row on every push.thread-timeline-windows.ts). While a push build is in flight, furtherevents-appendednotifications set one pending flag; one follow-up push runs after it finishes. Subscribers and baselines are read after the build returns.PRAGMA data_version. On a reader connection, every main commit changesdata_version, which would make every worker cache cold. The outline, selection-memo, projection-fold, ordering-context and latest-state caches now key on the per-thread rewrite generation that main sends with each read. Retained-output expiry andhistory-compacted/history-rewrittennotifications now bump that generation, as the other history mutators already did.0158_drop_thread_conversation_outlinesdropsthread_conversation_outlines; its read and save paths, the rewind delete and its query-cost cases go with it. It existed (Speed up conversation outline reads #2668) to avoid main-thread outline rebuilds after a restart. Workers make that unnecessary, and its key held the process-local rewrite generation, so 83% of stored outlines in a production copy (2,143 of 2,574) could not match after a restart anyway. Its save was the last main-thread cost on the outline path: up to 1.2 s per save on a starved disk.Thread timeline build blocked the event loopis nowSlow thread timeline build. Its CPU and context-switch fields are process-wide while the build runs in a worker. Docs:docs/configuration.md,docs/debugging-and-qa.md,docs/timeline-pagination.md.HOST_DAEMON_PROTOCOL_VERSIONis unchanged. No CLI or SDK surface changes.Behaviour changes
thread_conversation_outlinesdroppedtimeline-buildalready did on main.Slow thread timeline buildreplacesThread timeline build blocked the event loop.How you verified
Kill-tests on a production copy
VACUUM INTOcopy of a 10 GB productionbb.db(9.3 GB, 16-core M4, Node 22.23.1). The harness runs this branch's server code against the copy without starting a server.K1, same output. All 20 largest threads (outline plus latest timeline window) give deep-equal results in-process and through the bundled worker: 40/40.
K2, transfer cost. Receiving the result on main, at most 1.07% of the build's main-thread time by structured clone (largest outline, 1.25 MB: 0.91 ms vs 84.7 ms) and 0.18% as a JSON string.
K3, starved disk. 20 concurrent outline and timeline loads across the 10 largest threads. Conditions: a fresh APFS clone (cold page cache), the process under
taskpolicy -d throttle, and 8F_FULLFSYNCwriters plus 8F_NOCACHErandom readers competing for the disk.With the stored-outline save still on main, the pool path was 1,204–1,238 ms, all of it saves; that is why the table is deleted. An earlier run with only the builds in workers was 289–328 ms; all of that was the outline route's small point reads left on main, which is why the whole read moved.
K4, push ordering. Two busy production turns were deleted from a copy and replayed event by event at 2 ms pacing, through today's synchronous push path (from
origin/main) and through the queued pool path. Each had two subscribers (20 segments deferred, 8 segments full). In both paths there were 0 failed delta applies and 0 out-of-order pushes, the last push reached the finalmaxSeq, the final client state equals a fresh read, and the final tags are identical. Main-loop p99 during replay: 3.2–4.3 ms with the pool vs 7.5–8.9 ms on today's path.Main-thread CPU per live push. Measured with a main-thread
--cpu-profon the K4 replays: inclusive time underrunScheduledPush/pushThread, divided by push signals sent.rowDigestis the per-row JSON plus sha256.main(build on main)main2 runs, before-seeding 2 runs, this PR 3 runs (1,636-event turn); 1 run each for the 971-event turn. Every run kept 0 failed applies, 0 out-of-order pushes and a final state equal to a fresh read.K5, memory (
footprint, not RSS, because RSS counts the mmappedbb.db). In-process warm median 1,202 MB (5 runs). The pool:Tests
thread-read-pool.test.ts:threadReadPoolSizenever returns less than 1;thread-timeline-push-queue.test.ts: one build in flight per thread; appends during a build coalesce into one ordered follow-up push whose deltas apply and match a fresh read. It fails with single-flight removed (expected 2 to be 1).conversation-outline-performance.test.ts: the worker's outline cache serves a repeat read without reading event rows, and a rename or a rewind re-projects.ThreadTimelineWindows.read(threadId, …); the deleted-thread test now asserts a 404 instead of a cache drop.pnpm exec turbo run typecheck lint:type-aware --filter='...[origin/main]'(20/20);apps/servertest/services/threads,test/public,test/system/thread-pruning-invalidation.test.ts,test/app/startup-diagnostics.test.ts(1,039 passed);packages/dbtest/data/events.test.ts,test/migrate.test.ts(164 passed);tests/integrationtimeline and lifecycle smoke tests (9 passed). An earlier full@bb/server+@bb/dbrun on the branch before the table deletion passed (414 + 66 files).Live (verify-bb, production build)
pnpm start:worktreeon a fresh dev data dir:buildHistoryFixture) opened from the sidebar in 2.7 s. Its outline listed all 2,001 user messages in order. Clicking Turn 1 in the outline paged back to it.Event loop stalled, 0 worker stops and 0 failed pushes in the server log. The built server and source mode also started workers cleanly on a fresh data dir.Unrelated issues found along the way:
buildHistoryFixturewrites a turn-scopeditem/backgroundTask/completedevent that the stored-event parser rejects (older pages of that fixture return 500 onmaintoo), and the verify-bb inventory reports thediagnosticsCLI family as unmapped.Part of #6499; see #7753.
🤖 Generated with Claude Code