[Feature]: Process-level write lock for build/update/postprocess/embed
Problem
Nothing stops two processes from writing the same .code-review-graph/graph.db at once. For example, a build started by a git hook can run while a CLI update or the MCP build_or_update_graph_tool is also running. SQLite serializes each transaction, but a build or update spans many commits, so one writer can post-process (flows, communities, FTS) a graph that the other writer has only partly written. Today the only guard is busy_timeout=5000, so a second writer stalls for up to 5s at each lock wait when it should fail fast. (v2.3.9)
Proposed solution
Before writing, every command that writes the graph takes a non-blocking exclusive lock on a fixed file. That covers the CLI build, update, postprocess, embed, forget and watch, and the matching MCP tools:
fd = os.open(data_dir / "write.lock", os.O_CREAT | os.O_RDWR)
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) # BlockingIOError -> "another write is running", exit non-zeroThe kernel releases a flock when its holder dies, so stale locks never need cleaning up.
The path should be documented. External tooling (Claude Code hooks, git hooks) can then take the same lock with flock -n .code-review-graph/write.lock code-review-graph build, and both sides exclude each other.
Affected area
Graph store / SQLite
Alternatives considered
We already take this lock in our wrapper scripts. It cannot cover a CLI command typed by hand, or a write from an MCP server that is already running.
Source: tirth8205/code-review-graph