Repository navigation
Description
What happens
Saving the input history raises PermissionError: [WinError 5] on Windows, and the cap the code enforces is never written. The failure is intermittent, so the symptom a user sees is "sometimes the history does not move" rather than an error.
emrg/session.py:1034 writes the history the same way on every platform: a sibling .tmp file, then os.replace(tmp, path).
tmp = path.with_name(path.name + ".tmp")
_refuse_a_non_regular_subject(tmp)
with open(tmp, "w", encoding="utf-8") as f:
for record in records:
f.write(json.dumps(record, ensure_ascii=False) + "\n")
os.replace(tmp, path) # emrg/session.py:1034
On Windows os.replace fails with WinError 5 whenever anything else holds the destination open — a second handle in the same process, an indexer or scanner, or a reader that has the file open for a moment. The failure is not in the caller's control, and the current code treats it as fatal.
Where it was observed
tests/test_session.py::TestInputHistory::test_the_file_is_capped_oldest_first, on a clean tree with no patch applied: master 657f5d8e (tree 1e3df58a), Windows Server 2022, Python from the project venv.
E PermissionError: [WinError 5] ...:
'...\.emrg\sessions\s1\input_history.jsonl.tmp' -> '...\.emrg\sessions\s1\input_history.jsonl'
emrg\session.py:1034: PermissionError
The rate is high enough to make any plan-suite run unreliable and low enough to look like environment noise. Samples of this single test on unpatched trees:
| run | tree | result |
|---|---|---|
| 8 consecutive runs, 2026-11-13 | base 011a1730 |
PASS 3, FAIL 5 |
| 3 direct asks, 2026-11-14 | master 657f5d8e |
FAIL, FAIL, FAIL |
| 3 direct asks, 2026-11-15 | master d4f68e58 |
PASS, FAIL, FAIL |
That is 11 failures in 14 samples, and the same command on the same tree answers differently between runs.
Why it matters beyond the test
Two consequences, both measured elsewhere and neither fixable by retrying a test:
- The history write is not merely slow to settle, it is lost. A caller that hits
WinError 5leaves the new.tmpon disk and the oldinput_history.jsonlin place, so the cap is not applied and the previous content is what a later read returns. Nothing reports that the write did not happen. - Any gate that samples a tree once reads a coin toss. A row that fails on the base tree itself gets attributed to whichever PR was being measured at that moment, and the remedy such a gate prints for an owned row — push the PR again — voids that PR's votes. The attribution half has its own issue; this one is the red itself.
What the fix must satisfy
- The rename retries within a bounded total time with a short backoff, so a transient holder does not turn a save into a failure. The bound is explicit, not "retry until it works".
- A retry that actually happened is visible: the write path reports it (a log line or a counter), because a silent retry turns this defect into a hidden one.
- When the bound is exhausted the error names the destination path and the operation that failed, so the reader knows which file could not be replaced.
- A test proves the retry by making the first rename fail and the second succeed, and asserts both that the content landed and that the retry was reported.
- A reverse arm proves the bounded path is real: with the retry window set to zero the same test fails, and it fails on the rename rather than on an assertion about the content.
Acceptance
uv run pytest tests/test_session.py -qgreen on both CI legs, repeated enough times that a single pass is not the evidence.- The reverse arm above reddens with the window at zero.
- No test asserts the OS's own error text: the message for a genuine failure is compared on the parts the code controls (the destination path), since the platform's wording differs by locale.
Activity
how2how2how2-arch commentedon Nov 15, 2026
Handled by #2903
emrg: the input-history save retries a rename a second handle refuses (…