perf(core): stop rebuilding the whole messages_fts index in every incremental finalize - #77
Open
Cyb3rN8 wants to merge 1 commit into
Open
perf(core): stop rebuilding the whole messages_fts index in every incremental finalize#77Cyb3rN8 wants to merge 1 commit into
Cyb3rN8 wants to merge 1 commit into
Conversation
…remental finalize
The finalize step ran INSERT INTO messages_fts(messages_fts) VALUES('rebuild')
on every build, regenerating the full-text index from scratch no matter how
little changed. On a real-world 1.8 GB index (~688k messages) that rebuild
alone is ~10 s under unicode61 and ~30 s under a trigram tokenizer — the
dominant fixed cost of an otherwise cheap incremental build, paid on every
daemon refresh and every query-path freshness build.
The rebuild is redundant on the incremental path: messages_fts is an
external-content table maintained by the ai/au/ad triggers, and persist writes
messages with INSERT ... ON CONFLICT DO UPDATE (fires AU) and deletes them with
plain DELETE (fires AD), so no write shape bypasses the triggers. The historic
reason for the wholesale rebuild — INSERT OR REPLACE writes, whose implicit
deletes fire no DELETE trigger — no longer exists in the messages write path.
Incremental finalize now performs the rebuild exactly once per index, to heal
databases whose rows predate trigger-only maintenance, and records the
__messages_fts_synced__ marker in index_state within the same transaction (an
interrupted build redoes the heal; the marker never runs ahead of the work).
The force path keeps its wholesale rebuild as the last line of defence and
re-records the marker it wipes with DELETE FROM index_state.
Verified with a poison-posting probe (a messages_fts row with no messages
counterpart): it survives an incremental build (rebuild skipped), is cleared by
a force build, and is cleared by the one-time heal on a marker-less index.
FTS5 integrity-check (rank=1) passes after each path, and an upsert/delete
round-trip pins the trigger semantics the finalize used to bail out.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Aug 24, 2026
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.
Fixes #75.
Root cause and reproduction
Every finalize runs
messages_fts('rebuild'), regenerating the FTS index from the wholemessagestable. Cost is proportional to index size, not change size: measured on a 1.8 GB / 688k-message index, ~10.3 s under unicode61 and ~30.2 s under trigram (#14), out of a ~50 s incremental build. File stat-comparison (8,669 jsonl, 0.07 s) is not the bottleneck — the rebuild is.The rebuild is redundant on the incremental path:
messages_ftsis external-content maintained by theai/au/adtriggers, persist upserts messages viaON CONFLICT DO UPDATE(fires AU) and deletes via plainDELETE(fires AD). No current write shape bypasses the triggers; FTS5integrity-check(rank=1) passes on the live index.Change
INSERT OR REPLACE-era version) and records__messages_fts_synced__inindex_statein the same transaction — an interrupted build redoes the heal; the marker never runs ahead of the work (converges when re-run, per the contributing guide's destructive-work rule).DELETE FROM index_state.rebuildMemoryFtsis deliberately untouched:memoriesis tiny, and--attunewrites it outside build transactions, so the cheap belt-and-suspenders refresh stays.Verification
tests/incremental-fts-sync-marker.test.mjs:messagescounterpart): survives an incremental build (rebuild skipped), cleared by a force build, cleared exactly once for a marker-less index (the heal), withintegrity-checkafter each path;🤖 Generated with Claude Code