Skip to content

Store: history reads reach any ancestor in O(log n), and pages stop at 256 KiB (#472) - #557

Merged
lydakis merged 8 commits into
mainfrom
claude/project-thread-je4o8y
Oct 31, 2026
Merged

lydakis merged 8 commits into
mainfrom
claude/project-thread-je4o8y

Conversation

@lydakis

@lydakis lydakis commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Item 1b of the daemon proposals from the fleet performance review, after #552. Closes #472.

Problem

The history reads that #552 moved beside the service loop still did work that grows with the length of a history, not with the size of the page:

  • an oldest-first history_nodes page walked and sorted the whole ancestry past its floor before its LIMIT;
  • checking that from is in the lineage walked from the head down to it;
  • a history_items batch walked from the head down to the oldest node it asked for;
  • completing a page's turn walked back to the turn's start.

Separately, a page had no byte bound (#472): one whose turns ran in hundreds of long folders could approach the 1 MiB event limit.

What changes

  • Skip pointers (src/store/db.rs). Each node gets a narrow lineage row (parent, depth, skip, start), written by the same insert that writes the node. It sits in its own table so a walk never pages through item blobs. skip is chosen as Bitcoin Core's block index chooses pskip (GetSkipHeight), and ancestor_by follows GetAncestor. Together they reach any ancestor in O(log n) steps for any tree shape, forks included. start is the nearest turn start at or above the node.
    • in_lineage, which checks from and every history_items node, becomes one ancestor lookup.
    • An oldest-first page finds where the ids at or past its floor begin by halving depths, then jumps to the end of its first page.
    • A page that ends mid-turn takes its turn from start.
    • A new node's turn_seq comes from its parent's start instead of a walk.
    • Bot deletion removes lineage rows with their nodes. Ancestors of surviving nodes are never freed, so skip targets always exist.
  • Page byte budget. A page sends at most 256 KiB, as approvals already does. When its turns ran in many long folders it holds fewer nodes than limit, and the next page continues from next_from (newest first) or next_newer (oldest first). Its first node always fits. workspaces is now grouped in Rust from one row per turn, so it lists only the folders of the nodes that are kept.
  • Schema 73 (72 went to Pull cord: keep every pause, end detached commands, name the parked agents #549's pauses). A store from before it places every node when it opens: one pass in id order, with the places held in memory. Ids grow along every lineage, so parents are placed first.
  • Docs: RUST_PROTOTYPE.md (the history_nodes and history_items paragraphs), MULTI_DAEMON.md (the bound-the-work item, which asked for a measured index), and a Perf screen in DAEMON_MEASUREMENTS.md.

George picked skip pointers over the proposal's per-segment keyset paging. Nodes have no owner column, so keyset paging would have needed an owner column plus a fork table, and it would have assumed each bot's history is a single line.

Measurements

One bot with 2,000 turns of ten 2 KB nodes (20,000 nodes, 77.5 MiB), release build, main against this branch, two alternating runs each:

Measure main this PR
write, µs per node (own commit each) 906, 956 1,008, 1,022
store size, MiB 77.5 77.7
newest page of 400, ms 0.75, 0.74 1.06, 0.89
oldest-first page after 10%, ms 50.5, 48.6 1.4, 1.2
oldest-first page after half, ms 28.4, 29.1 1.3, 1.2
page from an early node, ms 44.8, 43.2 0.96, 0.86
one early item, ms 48.0, 48.1 0.61, 0.95
100 items spread over the history, ms 46.9, 58.7 4.4, 6.5

The deep reads are 30 to 80 times faster here, and they no longer grow with the history.

Costs:

  • A write costs 7 to 11% more: the lineage row's page goes in the same commit, plus O(log n) point reads for the skip.
  • Storage grows by about 10 bytes per node.
  • The newest page costs about 0.2 ms more, spent sizing it against the byte budget.

Tests

  • skip_pointers_reach_any_ancestor_in_logarithmic_steps: a 20,000-node tree with forks. 2,000 random ancestor lookups all match a parent walk, and none takes more than 60 steps.
  • history_pages_stop_at_their_byte_budget_and_continue: 120 turns, each in its own 3 KB folder. Every page stays within the budget and lists exactly its own turns' folders, and both directions of paging visit every node once, in order.
  • a_store_from_before_skip_pointers_places_every_node: a store with a fork is opened with its lineage table dropped at schema 72. The migration rebuilds identical rows, and pages come back identical.
  • The existing history, fork and deletion tests pass unchanged. cargo test --workspace and cargo clippy --workspace --all-targets -D warnings pass locally.

No protocol change: replies keep their shape, and a page may now hold fewer nodes than limit, which next_from already covered. Follow-up candidates on the same primitive are other walks whose cost grows with history length (history_number, turn lookups by ordinal, lineage_holds_output).

🤖 Generated with Claude Code

…eader pool

Observation reads (history_nodes, history_items, history_search, turns,
bots, events, usage, prompts) used to run inside the service loop, on one
reader connection. A deep history read or a search across every bot held
every other session's submit, interrupt or pause behind it, and every
read waited behind every other read.

They now run in an observation lane beside the loop, as Kubernetes' API
Priority and Fairness gives a priority level its own seats. The store
has a pool of WAL reader connections (--max-readers, one per logical
CPU by default). Observation reads take all but one of them, waiting in
the order they came, so a turn's own reads always find one free. A
session waits for its read before its next request is taken (the input
queue parks it), so a session's own requests keep their order.

events moves from the writer to a reader. usage and history_search,
which spawned a task each, now go through the lane and are bounded by it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGF9fn2EXiBvzYrQtaA
… ahead

Measured: sessions parked on reads kept the input queue full, so another
session's next request waited for room. A session now reads at most an
eighth of the queue ahead, and no further than its next request while it
waits on a read. A read's answer is written in its own task, not in the
service loop. Observation reads leave one reader for turns' own reads.
bench/fair_queuing.py floods history and searches from several sessions
over deep histories; the Perf screen is in docs/DAEMON_MEASUREMENTS.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGF9fn2EXiBvzYrQtaA
A session parked behind an observation read kept the room its queued
requests held, so enough parked sessions could fill the queue and hold
new connections and other clients' requests until a read finished.
Parking now hands those slots back, and a parked session reads nothing
more until it is answered; what it read ahead is taken uncharged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGF9fn2EXiBvzYrQtaA
A parked session's reader stopped at its next request, so lease
renewals behind it went unread until the observation was answered.
It now reads on to its share while parked, and what it queues then
takes no room, so renewals stay readable and the shared queue stays
free. The fair-queuing bench gives each flooder its own bots to create
and delete.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGF9fn2EXiBvzYrQtaA
…#472)

Each node gets a narrow lineage row (parent, depth, a skip pointer
chosen as Bitcoin Core's block index chooses pskip, and its turn's
start), written with the node. Checking that a node is in a bot's
lineage, finding where an oldest-first page starts, validating a
history_items batch, and naming a page's turn now take O(log n) point
lookups rather than a walk over the history. A history_nodes page
stops at 256 KiB sent and continues from next_from, or next_newer
oldest first. Schema 72 places every existing node when a store opens.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGF9fn2EXiBvzYrQtaA
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T20:23:55.639536Z 3b543b6 New commits
🔒 Security Review ✅ Completed 2026-10-31T20:04:19.443262Z 9a49159 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2c610e1b54

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/DAEMON_MEASUREMENTS.md Resolved
@lydakis
lydakis merged commit e10fea6 into main Oct 31, 2026
10 checks passed
lydakis pushed a commit that referenced this pull request Oct 31, 2026
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.

history_nodes: bound a page by bytes, not only by count

2 participants