Documentation

Docs / Reference

ADR 0052 - Re-index endpoint and the shared index lock

Status: Accepted (delegated review, 2026-11-07)

Context

UI decision U1 (the T016 task record (internal)) puts a Re-index button in the top bar; only "Check" ships today. Re-index is the first state-changing HTTP endpoint. ADR 0041 made graphvein serve read-only (GET/HEAD only, the store opened read-only) and required http.CrossOriginProtection before any mutating endpoint ships. ADR 0049 (WAL) lets SQLite readers answer from the last committed run while a writer works; Postgres readers always do (one run is one transaction).

Nothing serializes index runs today. Two concurrent graphvein index runs both scan and parse; on SQLite the second then fails on a read-to-write upgrade (SQLITE_BUSY_SNAPSHOT, ADR 0049 known gap), on Postgres it waits and then replaces the first. Fact cache rows are written outside the run transaction. A server that can start runs makes concurrent runs likely (a click while a terminal runs graphvein index), so the lock must be shared by the CLI and the server, on both stores, across processes, and must not outlive a crashed holder.

Constraints: no cgo outside ADR 0002; only internal/egress opens network connections; all persistence behind the store package; Windows, macOS and Linux; the boundary test rejects syscall and golang.org/x/sys imports outside a reviewed per-file list.

The design and its verified facts (F1-F7) are in the T016j task record (internal).

Options considered

1. Where the run executes

2. Lock primitive

SQLite store:

Postgres store:

3. Progress reporting

4. Cross-site protection

Decision

  1. graphvein serve gains POST /api/v0/index/run (start; 202, 409 index_locked, 409 reindex_unsupported, 400, 415, 403, 503 index_lock_unavailable) and GET /api/v0/index/run (lock state and the latest job of this server process: state, phase, progress, the run it replaces, the committed run or the error). The method rule becomes per path; only this path accepts POST. Schemas and the state table are in the task file and, when implemented, docs/cli/serve.md.
  2. The run executes in the server process with a writer store opened only for the run (option 1a), through internal/indexrun, which graphvein index uses too. Server runs use the CLI defaults and the built-in indexer only.
  3. internal/store gains IndexLocker/IndexLease/LockState/ ErrIndexLocked, implemented by internal/store/sqlite (option 2a, SQLite) and internal/store/postgres (option 2a, Postgres). Transaction gains Run() (the committed run's metadata). The lock is held from before the scan until after the writer store closes. graphvein index (built-in and --indexer) and the server acquire it with a 2 s wait; the CLI exits 1 when it is held. Before Commit the lease is checked; a lost lease fails and rolls back the run.
  4. Progress by polling (option 3a). A fraction is reported only where both numbers come from the run (cache lookups and parsing in files, ingest in records); every other phase has progress: null.
  5. Server shutdown cancels a running job; Serve returns only after the job rolled back, closed the writer and released the lock.
  6. The UI shows "indexing" only while the server reports a held lock and "fresh" for a new run only after the job reported it committed and a status answer names it (task file, "Governing UI principle").

Consequences

Known gaps

Implementation notes (server, CLI and storage part)

Recorded by the implementation of the decision-independent part (the UI, D1-D7, is not implemented yet). Where it differs from the text above, this section is what the code does.

Review fixes (2026-11-07)

Review notes (2026-11-07, delegated review)

Report a problem with this pageSource: docs/adr/0052-reindex-endpoint-and-index-lock.md