Conversation
Concurrent appends each read the ledger, appended one decision, and wrote the whole file back, so the last writer silently dropped every decision appended in between; a sleep and a decide racing could forget an operator's refusal. DecisionLedger now takes the store layout and appends under the store lock, staging the new content and replacing the file in one step.
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 #19
What was wrong
DecisionLedger.appendwas an unlocked read-modify-write: it read the wholeledger, concatenated one decision, and wrote the file back. Two concurrent
appenders (a
sleepand adecide, or twodecidecalls) could both read thesame content and each write back only their own line, so the last writer
silently dropped every decision appended in between. The ledger is truth, not
cache: losing a rejection verdict means the operator's refusal is forgotten and
the same proposal can come back on the next sleep.
The fix
DecisionLedgernow takes the store layout and derives the ledger path fromit;
Manage._ledgerwas its only construction site.appendruns under the store lock, so every writer passes through the oneadvisory lock, and stages the new content into a sibling
.pendingfile thatatomically replaces the ledger — a crash can no longer leave the truth file
truncated mid-write.
Reproduction
Three processes appending six decisions each (18 expected), on the code before
this change:
After the change the same reproduction keeps all 18 in every run, and
test_two_processes_appending_decisions_lose_nothingintests/system/test_concurrency.pypins that invariant.Verification
On Linux, Python 3.12, from a clean checkout of this branch:
uv run pytest -q— 389 passed (388 before, plus the new test)uv run ruff check .— cleanuv run mypy— cleanOne scope note:
decided()still reads without the lock, so a reader canobserve the ledger mid-replace; since the replace is atomic the worst case is
reading the pre-append state, never a torn file. Left out of this change since
the issue is about lost appends, but happy to take it further if you want
readers serialized too.