Skip to content

ADR 0025 — Derived tables: the whole-record pages in seconds, cold

Status: proposed · 2026-10-21 · builds on ADR 0001, ADR 0007, ADR 0013 and ADR 0019, RFC 0029 §8 and #336; answers #336's first open question; changes no line, no envelope, no SPEC

Context. #336 made every page of the Library answer warm within a millisecond on a record of five million lines (/gaps, which reads its clock again, in 0.06 s). It did that with three things. The server keeps a page by its tag (Site._keep). It reads what several pages read alike once for the record as it stands (Site.shared, keyed by Site._state: logbook.json, the record's today and the files beside the chain). And its readers stream through Index.stream instead of holding a kind whole. Cold is another matter. "Cold" means the first request after the server starts, or after a sync moves the head, and the scheduled runs move it four times a day (docs/schedule.md). At 5 M lines the pages that read the whole record still take minutes cold. These are #336's numbers on main (docs/serve.md, Measured), in seconds:

page cold at 5 M after append peak memory
/people 502 354 1.8 GB
/people/ID 522 522 1.6 GB
/manifest 187 187 700 MB
/org/ID 87 188 649 MB
/manifest/people 91 91 688 MB
/promises 293 277 86 MB
/world 280 293 2.8 GB
/agents 68 64 96 MB
/places 46 47 1.3 GB
/trips, /journeys 41, 39 38, 37 1.9, 2.1 GB
/ideas 20 20 278 MB

The cause is the same on every row: each reader recomputes from the lines. people.read streams every message, call, mail, event, transcript, photo and note line of its window and derives the owner's stays again. promises.extract runs its rules over every mail, message, transcript and note. orgs.read reads every line of seven kinds whole. world reads the record one year at a time through reading.read, every line of every day. All of that is thrown away at the next head, even when the sync appended a few thousand messages of one day. RFC 0043 §11–12 proposes a timeline with year and all-years zooms (/timeline/life: per place and per vessel, the visits, the years and who was there). Built the same way, those zooms would be the slowest pages of all.

336 asked the owner whether to keep these readings across a restart and grow them with each sync. docs/serve.md calls it a decision for the record's owner, because those readings hold what the pages read of sealed lines. This ADR is that proposal.

The rule stays as ADR 0001, 0007 and 0013 set it. The files are the truth and every database is a cache. Anything derived is disposable and can be reproduced from the files.

Decision.

  1. A derived table is a reader's cache, never a line. Its rows are computed from the standing lines by the reader's own code, the function the page calls on a miss. They are never appended, never in the record's folder and never read by verify. Deleting them loses nothing: a reader that finds them missing, stale or of another version computes from the lines as it does today. The pages' output is unchanged byte for byte (decision 9).

  2. They live in a second cache file beside the index, not in it. The file is summaries.sqlite, in the same cache folder as index.sqlite (<cache dir>/logbook/<owner_id>/, Logbook.index_path; RFC 0029 §8), so a copy of the record folder never carries it. Both places were weighed:

  3. Inside index.sqlite, as observed_entities is (schema 8):
    • for: one file to discard, and one transaction holds the lines and their summaries at one head.
    • against, first: the index is written by every append, inside the writer lock (Logbook.append → Index.add), under SQLite's busy timeout. A build of summaries holds a write transaction for seconds after a sync, and minutes the first time. That is the lock a writer must never wait on, and it is why #336 builds even its two added indexes with a busy timeout of 0 and only from the Library (Index.add_indexes).
    • against, second: Index.rebuild drops and remakes the tables it owns, as it does observed_entities. A rebuilt index would take the summaries with it, even when only the index's own schema had moved.
    • against, third: it moves SCHEMA_VERSION (decision 10).
  4. A second file: its own lock, its own version and its own stamp. A writer never opens it. A build never touches the index's lock, and a reader of the index never waits on a build. It runs in WAL mode, so a page that reads it is not blocked by a build that writes it. Consistency with the index comes from the stamp (decision 3), not from a shared transaction.

Chosen: the second file. The module that owns it is logbook/core/summaries.py, because the core readers (days, trips, people) read from it and core imports nothing above it (ARCHITECTURE.md). The tables of the shelves are built by contrib modules that register with it.

  1. A table is keyed by the head it was computed at, and by everything else it reads. Each table has its own stamp in the file's meta, in the shape observed_entities_meta already has:
  2. the head and seq it was brought to;
  3. a SHA-256 of logbook.json without its head and seq: the timezone, the day_start, the owner_emails that leave the owner's own refs out of their contacts (people.read, present.owner_of), and whatever else a reader comes to read there, as Site._state digests the whole file;
  4. the recipients, and whether the sealed lines were opened (opened, as the index records it);
  5. the table's own version, raised when its reader's rule changes;
  6. a SHA-256 of each settings file its reader reads. Per table these are policy/stays.json, places.json, assets.json, policy/owner.json, policy/entities.json and policy/crossing.json.

A table whose stamp differs in anything but the head is built again whole. Settings change rarely, and a renamed place or a new home moves every night after it. A table whose head is behind is brought forward (decision 4). A process without the identity on a record with recipients never writes a table, since it cannot compute one equal to a keyed reader's. It reads the lines as today.

  1. Brought forward as lines are appended, from the lines appended since its head. The record is append-only, so the lines a table has not seen are those with seq above its stamp. The index finds them by its primary key, and only they are read from the files. Before anything else, the builder reads the line at the stamped seq and checks that its hash is the stamped head. If it is not, the chain was rewritten: a migration that sealed the past (SPEC §3.1), or a fork repaired. Every table is then built again whole, and a migration line forces the same.

Every day a new line touches is computed again whole, from that day's lines, by the reader's own per-day function. Nothing is added to a stored number in place (+=), so a stored day is always what the reader computes for it. That a line can only add to its own day is used only to bound which days are recomputed. Which days a line touches, kind by kind: 1. An observation without supersedes touches its own local day (the index's day_local). A backfill touches the past days it lands on, which is still exact. Three kinds touch more than that: - A line that feeds the owner's stays touches the days whose stays and nights it can change. These lines are a location point of the owner or of an asset assets.json names, and a line of presence evidence (a transcript, note, call, message or photo, or an event that happened somewhere; SPEC §3.2.3 rule 5, ADR 0023). They touch every day from the day of that subject's previous point before the line's at, through the line's own day. A stay runs until the next point (reading._first_points_after), so a point after a silence can lengthen a stay begun days before. They also touch the day before the first of those, whose night runs into the next morning (stays.night_window). - A health-sample line also touches the next day, whose sleep is the night that ends on it (SPEC §3.2.7 rule 8). - A flight line touches every day that holds a flight line with the same payload date. The standing set of flights is decided per key and per pair of numbers for one flight, without supersedes (SPEC §3.2.4, the standing set). 2. A correction, a line whose supersedes names a line of its own kind (SPEC §3; a line so named is not standing, §3.2), touches its own day and the day of the line it names, found by the lines_id index. These can be different days: a calendar event moved to another day, or a session with an agent that grew past midnight. When the named line was itself a correction, the line that one named stands or falls with it. Its day is touched too (#321: a line is superseded while at least one standing line of its kind supersedes it). 3. A retraction (RFC 0003) touches the day of the line it hides, wherever that line is (SPEC §3.2.1), and everything that line touched under rules i and ii: the stays and nights if it was a point or evidence, and the named line's day if it was a correction, since that line stands again. A hidden resolution or entity line is handled by rule iv. A retraction is never retracted (RFC 0003). 4. A resolution (RFC 0006) or an entity line that binds refs to an org or an idea (RFC 0037) is record-wide: one line re-attributes every past and future line with its refs (RFC 0006 rule 2; present's _resolve, orgs' bindings). So no stored row is attributed to a person or an entity. Rows are kept per ref, the handle or address as the line names it. The overlay (resolve.identities_from over the retractions and resolutions, which every reader already reads whole) is applied when the page reads the rows, so a resolution makes no stored row stale. Some rules need the overlay inside a day: present confirming a name written in a note, or the owner's own refs left out of their contacts (policy/owner.json and its closure over the resolutions). For those, the per-ref rows say which days hold a line with any ref the resolution names. Those days, and no others, are computed again. 5. The small kinds the pages read whole today stay read whole and are not summarised: status, task, goal, chapter, draft, story, the signatures, stars and reviews. They are the closing of a promise, the state of an org, the signed day. They are few, and the pages apply them at read time as now.

Bringing a table forward is a reader's act. It writes only the cache file, in one BEGIN IMMEDIATE transaction that commits only if the stamp is still the one it started from. Two builders, the scheduled run and the Library, cannot interleave, and the second finds the work done.

  1. Precomputed at the 06:00 close and after each sync. sync --scheduled brings every table forward right after its sync --all, before doctor. That covers the four scheduled runs (commands/sync.py, _sync_scheduled), the 06:00 close (--close) among them. It says so in one line: summaries: 3 days computed again in 2.1 s. A manual logbook sync brings them forward too, but never builds a table whole. The first full build, and any build again whole, is made by logbook repair index, which already shows its progress, and by the scheduled run when the schedule is installed. Installing the schedule is the owner's own opt-in, so no CPU is spent unasked (#336's second open question). The Library brings a table forward on the first request after a head moves, when the delta is all it lacks. It never builds a table whole inside a request: that page is computed from the lines as today, and doctor says why (decision 8).

  2. A warm Library keeps them in memory. What Site.shared builds from the lines today, it builds from the tables: the whole record's people, last contacts and open promises; the year's ranking; the orgs; the promises; the sessions with an agent. It is still keyed by _state and dropped when the state moves, and the page cache is unchanged. A head move costs the bring-forward plus reading the rows: seconds, not minutes. The in-memory form is rows, not the readers' full objects, so the resident size should fall with the cold time. That is an estimate, measured in each slice.

  3. What is summarised, page by page. Every table holds numbers, days, seqs, line ids, place ids, entity ids from places.json and assets.json, tiers and refs. A ref from a sealed line is stored blinded, as HMAC-SHA256 under the file's own key, exactly as the index blinds raw_id (RFC 0029 §8). The key is sealed to the recipients in the file's meta. A ref from a plain line is stored plain, as the index stores it. No word is stored: no quote, subject, title, message text or name. Names come from the overlay and from places.json at read time. The few words a page shows (a promise's quote, the title of a session or of an org's latest mail) are read from the lines that page shows, by seq through the index, as last_real_contact.what already does (person_page.read, one Index.by_id).

table one row per what it holds the readers it serves, and what they compute today
days local day, gate the night after (where, home, aboard, in_transit, stay, located), country, moved_m, flights by line id, stays (count, attached, with_attachments), the confirmed people as refs, health (sleep_h, steps, resting_hr, hrv) days.summarise per day for /days and logbook days; the nights trips.trips folds; the night before and after on the Day page; the Year reader's countries, nights and health by month (year.read)
day_sources day, source, tier lines, first and newest at the Day's and /days' sources, and the gaps against the usual sources (days.usual_sources)
stays stay or stop of the owner's track, gate start, end, kind, place id or rounded position, aboard, promoted, attached counts by kind (messages, photos, calls, …), refs present with confirmed or proposed and the reason the timeline's moments (RFC 0043 §11); people._together's days together and nights; World's nights, photos, first and last per place and asset (world.build); the Places page's nights per named place
trips trip, gate trip:<start>:<end>, until, nights, in_transit, route and places as ids, asset, nights_aboard, flights in and out by line id, refs, lines trips.trips for /trips, /journeys and a journey's page; World's journeys. Recomputed whole from days and stays whenever a night changes, which is a fold over a few thousand rows (ADR 0019: a trip is still a reader's output)
places place or cluster, year, gate visits, nights, hours, first, last, photos, refs /timeline/life (RFC 0043 §12), World, the Places page's named-place nights. Recomputed whole from stays, like trips
contacts ref, day, kind, tier lines, first and last at, the last line's seq, spoke_s for a transcript, and the day's real-contact class (together, call, meeting, message) with its line people.read's channels (lines, first, last, tier, last_line, spoke_s) and days; people_drift.real_contacts; a person's page years, places, nights, drift, anniversaries
contact_years ref, year, kind, tier the year's days as a bit string (366 bits), lines, first, last /people's ranking by days and person_page.ranked. A person's days are the union of their refs' bit strings, exact where a sum of counts would count one day twice. Recomputed for the years a touched day is in
promise_candidates line holding a proposal seq, day, kind, source, the rule, the match's offsets in the line's body, due, the speaker's and counterpart's refs, tier promises.extract's proposals for /promises, a person's you_owe and they_owe, and the MCP. The section (open, kept, dropped) is applied at read from the task lines and the signatures (promises.closes, signing.disposed_lines), and the quote is read from the shown lines
touches ref kind (domain, login, repository, merchant, handle, Notion page), ref, day, kind, source, tier count, the last line's seq, Notion's words_delta orgs.read's touches, people, last, documents and money; ideas.read's touches, words, pages, people. Bound to an org or idea at read through the entity lines (rule iv)
sessions session line with an agent seq, day, at, end, source, agent, words, tool calls, the owner's and machine's turns, tier, the line it supersedes shelf_agents' sessions. Titles, summaries and turns are read for the 14 days a page shows, as today

The people review, the orgs' and ideas' review queues and the Places review keep their observed_entities rows (RFC 0037). They are clustered over the whole record (places_review anchors on the longest stays first), so they are not a sum of days, and this ADR does not make them one. What it changes in slice 3 is what their stamp names. Instead of the head, they name the seq of the last line of the kinds they read. A sync that appends only messages then leaves the Places review standing. observed_entities stores the labels it reads from opened lines in plain text (entity_review.py, people_review.py), which decision 7 refuses for these tables. That is noted here and not changed: it is its own issue.

  1. What is never summarised: anything a tier gate decides per request. The Library reads every tier (Site.tiers is None). export site passes its tiers, World takes the world ceiling of policy/crossing.json (1 and 2 by default), and the MCP server reads through GatedIndex. So:
  2. A count row holds the lines of one tier. A gated reader sums the rows whose tier is in its gate, as reading.crossing keeps the lines whose tier is.
  3. A derived row is per gate. Stays, nights, trips, places and presence are not sums: a stay promoted by a tier-2 message is a stop to a reader that cannot see tier 2. Each such row is keyed by the gate it was computed under. The Library's (every tier) is kept always. Another is kept for each ceiling a page or the MCP serves from, built the first time it is asked for. A gate the file does not hold is computed from the lines, as today.
  4. Retractions and resolutions are never gated (logbook/core/reading.py's module docstring), so the overlay is the same under every gate.
  5. The clock is never summarised. Today, this year, a silence and the coming anniversaries are applied at read, as CLOCK_PAGES already digests today into the tag. So are a page's request (?year, ?page, ?portfolio, ?money, ?person, ?from, ?to) and the owner's acts in state/.

  6. A summary is proven equal to the reader's own result. The check recomputes a sample and compares, row by row, as canonical JSON.

  7. What it samples: 20 days at random, seeded by the head so a run can be repeated, plus the last 7 days and every day the last bring-forward touched. For each sampled day it also takes the refs on that day, every trip that day is in, and every promise candidate of that day.
  8. How it compares: each is recomputed from the lines by the reader's own function and compared with the stored rows.
  9. doctor runs it as a check named summaries, reading the file read-only (?mode=ro, as its index check reads the index). A pass names what it compared: summaries 20 days, 41 refs, 3 trips equal at 9f2c…. A warn says the file is missing or behind, and how long a build would take. A fail names the table and the day, never a value, and says logbook repair summaries builds it again. The scheduled run's doctor makes it every run.
  10. The tests hold it three ways:

    • tests/test_library_golden.py's seven records render every page with the tables and without them, byte for byte equal to the fixture taken from main.
    • A property test builds the tables at one head, then appends random lines: backfilled past days, location points after a silence, corrections across days, retractions of points, corrections and resolutions, resolutions merging two refs, flights under two numbers. It brings the tables forward and compares every day, ref and trip with a full build and with the reader.
    • tests/test_library_perf.py counts, never times, that a cold /people, /days or /trips after a bring-forward reads from the files only the lines appended since.
  11. The cost.

    • Disk. Measured here on scripts/bench_library.py's record at 200,000 lines (--build-only, then logbook repair index): index.sqlite is 104 MB, 522 bytes a line. That makes about 2.6 GB at 5 M lines. The same record has 3,653 days, 28,620 distinct (day, kind, source, tier) groups, and 128,905 distinct (ref, day, kind) among 144,366 lines of the contact kinds.
    • At 5 M lines days, day_sources, stays, trips and places are tens of thousands of rows: a few MB.
    • contacts has at most one row per contact line, about 3.6 M at 5 M lines. On the 200,000-line record 89% of (ref, day, kind) are distinct, and only a denser record will show how far they fall below one a line as the handles active in a day saturate. So it is 1–3.6 M rows; at a guessed 60 bytes a row, 60–220 MB.
    • contact_years is a few thousand refs × ten years × kinds: about 100,000 rows.
    • promise_candidates, touches and sessions are smaller than contacts.
    • In all, about 100–300 MB at 5 M lines, a tenth of the index or less. These are estimates, to be measured by each slice.
    • Time to build whole at 5 M. A plain logbook repair index of the 200,000-line record took 9.0 s here (45 µs a line), so about 4 minutes at 5 M. RFC 0029 §9 measured 69 s at 3 M plain, and 382–481 s opening the sealed lines. The tables cost what the readers they replace cost once, sharing one reading of each line, so #336's cold times bound them:
    • days, stays, trips and places: between World's reading of every year, 280 s, and /days' rate: 2.29 s for its default fortnight (WINDOW_DAYS), 164 ms a day, about 600 s for 3,653 days.
    • contacts: about the whole-record people.read inside a person's page, a few minutes of its 522 s.
    • promise_candidates: about /promises' 293 s.
    • touches and sessions: about the Manifest's 187 s and Agents' 68 s.
    • Estimate: 5–10 minutes for slice 1, and 20–35 minutes for all three slices, once, on one core. On a sealed record, add the opening of every sealed line once (145–194 µs each, RFC 0029 §9).
    • Time to bring forward. A scheduled sync appends a few thousand lines over one or two days. That is two to four days computed again, at about a Day's reading each (0.67 s cold at 5 M, /day/DAY), plus the refs of those days: seconds. A backfill of a year touches 365 days, at about 164 ms each (/days' fortnight in 2.29 s): about a minute for days, about as much for contacts.
    • Cold page after. The target, held by slice 1's benchmark, is #336's unmet one: under five seconds cold at 5 M for every whole-record page. Reading the rows, applying the overlay and aggregating replaces reading the lines.
    • SCHEMA_VERSION. index.sqlite does not move: no column, index or table of it changes, and the builder reads it through lines(seq) and lines_day_local, which every index has, and lines_id, one of #336's ADDED indexes. An index built before #336 that the Library has not opened since lacks lines_id; the builder gives it one through Index.add_indexes, as the Library does, or finds a named line by a scan, which is slower but still exact. summaries.sqlite carries its own version, and each table its own. Had the tables gone inside the index, the move would have been 8 → 9, as observed_entities moved 7 → 8. Every existing index would then rebuild once, about 4 minutes at 5 M plain and five to seven times that sealed (RFC 0029 §9: 69 s plain against 382–481 s opening, at 3 M). Any older version that opened the index afterwards would rebuild it again for its own schema.
    • logbook-ts and conformance: no effect. The tables are a reader's cache. They are never in SPEC, never in conformance/, and never compared by §6.1, which compares a reader's JSON, unchanged here. logbook-ts may keep a cache of its own or none. It never opens this file, as it never opens index.sqlite (RFC 0029 §11 item 13 is a recommendation for a cache, not conformance). The envelope, the hash and the format are untouched: no SPEC version, no regenerated fixture.
  12. The plan, in slices, each a pull request.

    1. The days. summaries.sqlite, its meta, stamps and key, and the builder: whole, brought forward, and checked. The tables are days, day_sources, stays, trips and places. Also: the doctor check, logbook repair summaries, logbook repair index building them, and the scheduled run's step after the sync. The readers that move are days.read (/days), trips.trips (/trips, /journeys, a journey's page), the Day page's neighbouring nights and readiness window, and the Year reader. The timeline's zooms (RFC 0043 §11–12, once adopted) are built on these tables from their first commit. The per-day function of each reader is split out so the reader and the builder call the same one. The property test, the golden pages with and without the tables, and scripts/bench_library.py timing cold with the tables all come in this slice.
    2. The people. contacts, contact_years, presence per stay, and promise_candidates, since a person's page shows their open promises. The readers that move are people.read, people_drift.real_contacts, person_page.ranked and everyone (/people, /people/ID), and the people review's second whole-record people.read, with the overlay applied at read. The MCP's people readers go through their gate (decision 8).
    3. The Manifest and the shelves. touches for orgs.read and ideas.read (/manifest, /org/ID, /manifest/people, /ideas, /idea/ID). /promises reads promise_candidates, and /agents reads sessions. World reads stays, trips and places. The observed tables are stamped by the lines they read (decision 7).

    Each slice adds its changelog fragment and its tables to the check. It runs LOGBOOK_SLOW=1 uv run pytest tests/test_fuzz_readers.py before it merges, since it touches readers (CLAUDE.md, the merge rules), even though their output must not change.

Alternatives considered.

  • A long-lived process with everything in memory. This is Site.shared today, and it already makes warm pages a millisecond. What it costs:
  • Every head move reads the whole record again for each whole-record page. Warming them in the background (#336's second question) costs, by the cold column above, up to 36 minutes of one core per sync at 5 M, less what pages share (everyone serves every person's page, orgs.read the Manifest's three). Call it half an hour, four times a day: an estimate.
  • It holds up to 2.8 GB resident (/world) while it does.
  • A restart pays it all again: an upgrade, a reboot, serve --install-service starting the server at login.
  • Nothing outside the Library shares it: logbook people, logbook days, the MCP server.

Applying each delta to the in-memory readings instead is decision 4 without the file: the same rules of invalidation, lost at every restart. This ADR keeps that process, with the tables beneath it (decision 6). - Materialised SQLite views. SQLite has no materialised view, and a plain view is computed again at every query. The readers' rules are a stays clustering with its night window, a promise rule's regular expressions, an alias walk of four hops and a flight's standing set. They cannot be written in SQL, or only as a second implementation of each rule that would drift from the Python one. Triggers on lines that keep counts would make every writer pay inside Index.add, under the writer lock and against the 20,000 lines a second floor (RFC 0029 §9). The counts are the cheap part anyway: Index.source_days is an aggregate over the index already. Rejected. - A columnar file per year (Parquet or Arrow, or a format of our own). The reference stack is the standard library and tzdata (ARCHITECTURE.md). Parquet needs pyarrow, and a format of our own is one more format to keep. A year's file is rewritten whenever a day of that year changes, so a backfill rewrites years. It makes scanning columns fast, but the time is not in the scan: it is in parsing payloads and applying the rules, the stays fold, the regular expressions and present. A columnar copy of the payloads is a plaintext copy of the sealed ones. RFC 0029 §8 would not keep even a plain raw_id column for a sealed line, and rejected decrypting an index to a temporary file (its option 1) as a multi-gigabyte plaintext on disk. Rejected. - Doing nothing. Nothing to build or keep equal, no file, no check. The cost: - The first visit after each of the four daily syncs, and after every start, takes minutes on every whole-record page, and grows with the record: linearly at least, since every reader reads every line. - The owner's "extremely slow, unbearable" of 18 October stays true for that first visit. - The timeline's year and all-years zooms (RFC 0043) would cost what World costs, 280 s at 5 M, or need a design of their own.

Consequences.

  • Equal output. A page computed from the tables is the page computed from the lines, byte for byte, and the golden test holds it. A wrong table is a slow page's bug turned into a fast page's lie. That is why the check runs in doctor at every scheduled run, and why a table that fails it is not read until it is built again.
  • One code path per rule. Every reader whose page reads a table has its per-day part split out. The builder and the reader on a miss call that same function. A rule changed in one place and not the other raises the table's version, or the check finds it.
  • What a copy of the cache folder now shows. Someone with the cache folder learns more of the record's shape than index.sqlite gives them today. They learn that a blinded someone was in contact on these days, so many times by these kinds, and was at these stays. They still learn no name, number, address or word, and cannot confirm a guess without the key (RFC 0029 §2, §8). That is more than the envelope's shape, and it is the owner's decision. The alternative, decided per record, keeps the rows of sealed lines in memory only. A sealed record's people pages are then minutes cold once per start, and seconds after each sync.
  • The cache directory grows by about a tenth of the index (decision 10). doctor's disk check reads the free space of the record's volume, not the cache's, so the summaries check names the file's size.
  • Two processes may build. The stamp's compare-and-commit makes the second a no-op. A writer of the record never opens the file, so append and its floors are unchanged.
  • Out of scope. The Day page's own lines are still read through the index: it shows them, and it is under a second already. observed_entities' plaintext labels (decision 7) are their own issue.