GrooveSeek

Semantic search over a Markdown knowledge base, served over MCP.

View the Project on GitHub alphabet-h/grooveseek

31. One writer per index, by an operating-system lock

Context and problem

The watcher reads what is on disk and writes the index while it holds the in-process mutex, so two writes inside one groove serve cannot interleave. A second process can: a groove index run while a server watches the same knowledge base reads a row, and the other process can change that row before the write lands. behavior.md has said so until now and left the rule “run one writer at a time” to the user.

The widest window is the prune at the end of a full rebuild. It deletes every row its walk did not visit, so a row the other process wrote during the run for a file that is really there is deleted, and no file event comes to put it back: the document drops out of search silently. The order of harm the fix was held to:

  1. Worst: a document that exists disappears from search without a word.
  2. Next: a deleted document keeps its row.
  3. Acceptable: a writer waits, or stops with an error that says why.

Decision drivers

Options considered

  1. Two correct writers: look again under BEGIN IMMEDIATE at every write point. Scored highest on not losing documents and on completeness, and lowest on cost: about five changes, a three-phase rework of the file rename, and a copy of the walk’s “would it collect this path” rule for the prune, which leaves rows that are never pruned once the copy and the walk drift apart. The watcher would wait for another process’s SQLite lock (up to the 30 s busy timeout) while holding the mutex that searches also take, and the meaning of every transaction would change at once. Rejected.
  2. Optimistic concurrency: a row generation (row_gen) kept by a trigger, and compare-and-swap writes. Admits silent loss in one case (a file made again with the same content after the walk missed it), has a ghost counterexample, does not see metadata-only updates, and depends on reading the token before the look at 30-45 places (the option’s own estimate), where one place in the wrong order passes silently. A schema and a trigger are expensive to take back. Rejected; recorded here, with its score and weaknesses, as a possible later layer of defence.
  3. A lease row in index_meta kept alive by a heartbeat. A crashed holder leaves its row behind, so taking over needs a timeout that can be wrong both ways, and claiming the lease is itself a write to the database. Rejected as the way to decide who holds the lease. A heartbeat is kept only to tell how fresh the display file is (below).
  4. The fs4 crate. std has had File::try_lock and File::unlock since Rust 1.89. Rejected: no new dependency.
  5. An exclusive operating-system lock on a file beside the database. Taken. The operating system drops it with the process, crash and kill included.

Variants of option 5 that were not taken:

The three designs were scored by three reviewers on five axes, 10 points each (150 in all):

Design Reviewer 1 (loss/ghost/completeness/cost/tests) Reviewer 2 Reviewer 3 Total Mean
single-writer-enforced (taken) 8/8/8/6/8 = 38 7/8/7/7/8 = 37 8/6/7/8/8 = 37 112 37.3
per-point-immediate (look-under-IMMEDIATE) 8/7/8/3/7 = 33 8/7/8/3/6 = 32 9/8/9/2/6 = 34 99 33.0
optimistic-cas-epoch (OCC, row_gen) 5/6/6/4/7 = 28 6/6/8/5/7 = 32 7/7/8/4/7 = 33 93 31.0
Axis (mean) lease look-under-IMMEDIATE OCC
no silent loss 7.67 8.33 6.00
no ghost 7.33 7.33 6.33
completeness 7.33 8.33 7.33
cost and risk 7.00 2.67 4.33
testability 8.00 6.33 7.00

Decision

Consequences

References