~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0

The index keeps what's asked and answers without waiting on its writes

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/hud:index-cost

HUD's index keeps only what's asked, indexes collections held across the whole network per author, and answers without waiting on its own writes. In production it held 4.8 GB, 98% of it one collection asked about once, and timed out queries at 10 s during backfills.

Claude Opus 5.5 · high

What changes

Expiry. Each collection, field and (for per-author collections) author records when it was last asked, kept in memory and written once a minute in the lowest lane. A sweep every minute drops what nobody asked for in HUD_INDEX_KEEP_DAYS (14), in small transactions: the FTS table, each index, rows 5,000 at a time, then the table; a later query fills it again. HUD_INDEX_KEEP names collections that never expire. Production's existing collections count as asked at the migration, so nothing goes for 14 days after deploy.

Network-scale collections. Before backfilling, the relay is asked how many repos hold the collection (stopping once past HUD_INDEX_MAX_REPOS, 20,000). Past it (app.bsky.feed.* and the like) the collection is indexed per author: a query's by reads those authors (50 new a query), live sync keeps only them, and a query without by answers partial with partialReason: perAuthor. HUD_INDEX_LARGE indexes one whole anyway.

Big backfills go slow, never stop. Past HUD_INDEX_SLOW_RECORDS (250,000) a collection's reads move to a 4-slot queue and its writes behind every other backfill; it still completes. Below HUD_INDEX_MIN_FREE (2 GB) free on the data disk, backfills pause (live sync goes on) until space returns.

Answers off the writer. One writer, taken in lane order: an answer's tables, live sync, backfill and background fills, slow backfill and the sweep. Answers never write: a missing table is built by the builder, the answer waiting 300 ms then answering partial (filling); a progress handler stops it at 2 s (deadline). A backfill writes 1,000 records a transaction in parts of 200, committing between parts when an earlier lane waits, and gives way to the answers reading as each record goes in. Readers are capped (HUD_INDEX_READERS, 6) at 8 MiB each (the writer 32 MiB), idle ones closed, the WAL truncated when quiet. Per-request PDS timeout 10 s (was 30), at_once 16 (was 64).

Lexicons (grown): query answers partialReason; getStatus answers each collection's indexed (whole, perAuthor), slow, records, lastAsked, and paused. HUD's ui/refresh.rs asks a partial answer again after 2 s and keeps a per-author one live. docs/index-service.md, docs/running.md, AGENTS.md; .changeset/index-keeps-what-is-asked.md (minor). Production's index.db migrates by ALTER TABLE ADD COLUMN alone, instant on 4.8 GB.

Why

Measured on production: index.db 4.8 GB, of which fm.teal.alpha.feed.play (2,997,173 records, 543 repos, asked once) was ~98%; the process held 650 MiB of heap at idle (32 MiB cache per reader, readers never closed); three backfills at once on one CPU left queries waiting until HUD's 10 s limit. An index is a cache: what nobody asks shouldn't stay, what's held by millions shouldn't be copied, and answering shouldn't wait on filling.

How it was checked

just check (just cov 100% lines and functions). Tests for expiry and its sweep, per-author intake against a fake relay over the limit, the slow lane and disk pause, lane order, a write cutting into a backfill between parts, a backfill giving way to readers, the reader cap, deadlines and partial reasons, and opening an old-schema file. examples/backfill_load.rs under docker --cpus=1 --memory=1g, two collections backfilling (270k plays, 90k favorites) while six readers query, main and the branch alternating, 5 runs each: all records 4,805/s main vs 4,671/s branch (median), a small collection filling beside the big one 3,780/s vs 9,202/s, query p50 equal, p99 about equal (2.6–17 ms main, 3.1–13.5 ms branch), no answer late or partial. One main run under heavier host load grew its WAL to 10.9 GB; no branch run passed 151 MiB. After merging main (backfill's protocol calls now from the shared crates), one run: 4,285/s, p99 within the earlier ranges, host busy.

Shortcomings

  • The load test builds tables beforehand, so it doesn't measure main's answers that build a table behind a backfill transaction, the likely cause of production's timeouts; the branch removes that path rather than measuring it faster.
  • Query p99 under a concurrent backfill is main's, not better.
  • HUD_INDEX_MIN_FREE takes bytes, no unit suffixes.
Approved

This pull makes the index drop collections, fields and authors that nobody has asked for in 14 days. It indexes collections held by more than 20,000 repos per author. It moves answers off the writer, with lane-ordered writes, a reader pool and a 2 s deadline. I found nothing that must change before it merges.

What I checked:

  • Migration. ALTER TABLE ADD COLUMN plus an UPDATE … SET asked only touches colls, fields and repos, which are small tables, so it should stay quick on the 4.8 GB file.
  • Expiry. flush runs before expire. Every insert path (set_rev, set_unread, count, NEW_COLL) stamps asked with the current time, so a new row never reads as stale and gets dropped. busy and kept protect collections still backfilling and those the operator keeps.
  • Writer lanes. put_all lets a backfill part commit early when an earlier lane waits, and the loop carries on with the remainder. The reader pool's take and close_idle keep open consistent.
  • Answers. answer_json picks deadline, then perAuthor, then filling as the partial reason. refresh.rs treats perAuthor as live and filling or deadline as ask-again-in-2 s. Both read consistently against the lexicon changes in the description.

What I did not read: the diff was too large to give whole. I read store.rs mainly in its non-test code (the lanes, pool, expire/purge, put_all, the Sink impl and the answer loop), sweep.rs, live.rs, server.rs, refresh.rs, and the counting and listing part of backfill.rs. I did not read the tests, the rest of backfill.rs, main.rs, examples/backfill_load.rs, the docs, the lexicon JSON or the changeset. The runner's checks (check, test, lexicons, docs, e2e and the others) were still queued or running when I looked, so I have no result from them. Those checks, including just cov's 100% gate, are what to rely on for the code I didn't read.

One thing to be aware of, not a blocker. A collection taken in per author stays that way once wide is set, and the relay isn't asked again. Raising HUD_INDEX_MAX_REPOS later won't turn it back into a whole collection, so the operator would need HUD_INDEX_LARGE. It's worth one line in docs/index-service.md if it isn't there already.

Merged as 2a659b5