feat: add KB mutation locks and atomic state writes#86
Open
gwokhou wants to merge 3 commits into
Open
Conversation
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.
Summary
This PR adds cooperative KB-level locking and atomic state/config writes.
It is both a reliability hardening change for the current CLI and a foundation for future concurrent ingestion / compilation work.
Why
Several OpenKB commands mutate shared KB files such as
.openkb/hashes.json,.openkb/config.yaml,wiki/,raw/, and generated reports. If two OpenKB processes modify the sameKB at the same time, or if a process is interrupted while writing state, the KB can be left in a partially written or inconsistent state.
This matters today for commands such as
openkb add,openkb remove,openkb recompile,openkb watch, andopenkb lint --fix.It also matters for planned concurrency improvements. Before OpenKB can safely run more ingestion or compilation work in parallel, it needs a clear synchronization boundary around
KB-level mutations and a safer persistence primitive for state files. This PR introduces that boundary without changing the public CLI interface.
I did not find an existing upstream issue that directly tracks this race condition, so this is a preventive hardening / enabling change.
Changes
openkb.lockswith KB-level advisory locks:addremoverecompilewatchlintliststatusNotes
The lock is advisory and cooperative: it protects OpenKB commands that use these helpers. It does not attempt to make arbitrary external edits to the KB directory safe.
This PR intentionally keeps the scope narrow. It does not introduce parallel ingestion yet; it adds the synchronization and persistence primitives needed before that work can be
done safely.
Testing
tests/test_locks.pycovering lock behavior and atomic write helpers.