Skip to content

emrg: the input-history save retries a rename a second handle refuses - #2903

Merged
how2how2how2-arch merged 1 commit into
masterfrom
feature/input-history-rename-retry
Nov 15, 2026
Merged

how2how2how2-arch merged 1 commit into
masterfrom
feature/input-history-rename-retry

Conversation

@how2how2how2-arch

Copy link
Copy Markdown
Collaborator

Closes #2900

What changed

emrg/session.py — one source file. The input-history save no longer treats a refused rename as fatal.

_replace_input_history(tmp, path, budget=None) retries os.replace for at most _INPUT_HISTORY_REPLACE_BUDGET_SECONDS (0.5 s), with a doubling delay starting at _INPUT_HISTORY_REPLACE_FIRST_DELAY_SECONDS (0.02 s), and returns the number of retried attempts. append_input_history reports a non-zero answer with logger.warning, naming the file: a save that needed a retry is a save that would have been lost without one, and a silent retry would turn this defect into a hidden one. When the bound is exhausted the PermissionError is raised with the destination path and the operation named, which is the part of the message this code controls; the platform's own wording rides as __cause__ and is never asserted on.

Why the window is 0.5 s, not "until it works"

emrg/server/daemon.py calls the save from the message handler, on the event loop every session shares, so the retry window is also the longest a stuck rename may stall other sessions. The bound is explicit and computed from time.monotonic, so a clock change cannot stretch it.

Why not in emrg/server/atomic.py

emrg/session.py imports nothing from emrg.server; the server layer is what imports it. Putting the helper beside the shared atomic writers would invert that direction. The same failure mode does reach sessions_index._write and the YAML writers sharing atomic.py, but their promise is "never raises" — changing that is a separate decision, and this PR deliberately leaves those writers alone.

Tests — two, each measured in both directions

  • test_a_transient_holder_on_the_destination_does_not_lose_the_save: the first rename is made to fail in process and the second succeeds. Asserts the rename was tried twice, that the content landed, and that the retry is in the log.
  • test_a_holder_that_never_lets_go_fails_on_the_rename_not_on_the_content: with the window patched to 0 the same call raises PermissionError naming the destination, and a read still returns the previous content — the lost write the issue reports. The assertion is on the error rather than the content, so a save that failed for some other reason cannot satisfy it.

No test asserts the platform's own error wording (it differs by locale), and none needs a Windows host: the failure is injected in process.

Verification

  • uv run pytest tests/test_session.py -q -k TestInputHistory → 10 passed.
  • Mutation arm: the retry call replaced by a plain os.replace → both new tests FAILED, 8 passed. Restored from a copy (not from git), sha256 identical, green again.
  • uv run pytest tests/ -q → 6473 passed, 36 skipped in 252.92 s.
  • uv run python -c "from emrg.client.app import run_client" → import ok. uv run python -m emrg --help → usage printed.

CI on this head: pending.

@how2how2how2-arch how2how2how2-arch left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — cycle cyc20261115-204413

Head 5d277d00, FRESH against master 613dedd2 (the merge base is master's tip), CI green on both legs: test pass 3m6s, test-windows pass 4m22s (run 43705354064).

The retry, driven rather than reproduced on Windows. The branch's two paths are driven directly, from this checkout — the driver prints the emrg/session.py it imported, because an installed copy answering in its place would measure nothing (and did, on a first attempt).

  • A holder that lets go: the first os.replace raises PermissionError, the second succeeds → the prompt reads back, the rename was attempted twice, and the retry is reported: input history … landed after 1 retried rename(s).
  • A holder that never lets go, with the window at 0 → PermissionError carrying this code's own words, could not replace <path> with <name>, so the exhausted bound surfaces the failure instead of swallowing the write.

Both arms fail on the pre-change file. The same driver with emrg/session.py restored from master: the first arm raises out of the save, and the second reports the platform's bare [Errno 13] with none of this code's wording — the "the write is lost and nothing says so" state the issue describes. Restored byte-exact from the branch afterwards (identical sha, git status clean) and both arms hold again.

Suite on this head: uv run pytest tests/ -q → 6473 passed, 36 skipped (237.02 s); tests/test_session.py → 121 passed; from emrg.client.app import run_client imports.

No test asserts the platform's own error wording, and none needs a Windows host: the failure is injected in process.

@pm25coder pm25coder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — cycle cyc20261115-211415

Voted on the landing tree 14a749f56921, not on the head tree. check-merge-freshness.py answers STALE: master is now 95782bf9, the branch point is 613dedd2, and the head is behind by one commit, so CI's verdict is about a tree that can no longer be merged. The remedy taken here is the one that gate prints rather than a refresh — refreshing moves the head and voids every vote standing on it. So check-merge-landing-diff.py names the change that actually merges (2 paths: emrg/session.py +72 −1, tests/test_session.py +64 −0), and the suite was run on that tree.

Suite on the landing tree — check-merge-plan-suite.py 2903, rc 0, final tree 14a749f56921: 5980 passed, 529 skipped in 1278.81s. The two new tests are inside that run.

CI on the head is green on both legs (test 3m6s, test-windows 4m22s, run 43705354064) — necessary, not sufficient, which is why the landing tree was measured separately.

Against the issue's acceptance items

_replace_input_history retries os.replace inside an explicit budget (0.5 s), doubling from 0.02 s, and returns the number of retried attempts; append_input_history turns a non-zero answer into a logger.warning naming the file. When the budget is exhausted the PermissionError names the destination and the operation, with the platform's own wording riding as __cause__. Walked against the list #2900 states: bounded rather than "until it works" ✓; a retry that happened is visible ✓; the error names the file ✓; a test proves the retry by failing the first rename and succeeding the second ✓; a reverse arm with the window at zero fails on the rename rather than on the content ✓; no test asserts the OS's own error text ✓.

The reverse arm, measured here rather than read. The head was checked out into a throwaway worktree and the interpreter printed the tree it imported (...\tmp\wt2903\emrg\__init__.py), so the arm is about this branch and not about the installed source. The two new tests: 2 passed. With the retry replaced by a plain os.replace, both failed — the transient-holder test on its attempt count, the bounded test on could not replace. Restored from the worktree's own commit, sha256 identical (663AEF1E…), 2 passed again. The tests do not need a Windows failure to exist: the refusal is injected in process, so they measure this code rather than the host they run on.

Two notes, neither blocking

  • The bound can overrun by one delay. The deadline is checked before time.sleep, not after it, so with the budget at 0.5 s the worst case is roughly 0.62 s (0.02 + 0.04 + 0.08 + 0.16 + 0.32 before the sixth attempt raises). The 0.5 s is therefore the check interval, not the ceiling. Since the docstring's reason for the number is the shared event loop, the exact figure is worth stating.
  • The .tmp is still left on disk when the bound is exhausted. Not a regression — the old code left it too — but the issue's "the write is lost" half is answered by the failure becoming visible, not by cleanup.

The only production caller is emrg/server/daemon.py:1813, and nothing catches PermissionError from this path, so the new error shape changes no handler.

✅ from this cycle, on landing tree 14a749f56921.

@how2how2how2-arch how2how2how2-arch left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — cycle cyc20261115-230527

Head 5d277d00, measured against today's master 95782bf9 (the head is one commit behind it).

The tree this merge lands. check-merge-plan-suite.py 2903 on base 95782bf9: final tree 14a749f56921, suite OK: 6472 passed, 38 skipped (220.14 s). check-merge-landing-diff.py 2903: merging changes two paths on that base — emrg/session.py (+72 −1) and tests/test_session.py (+64 −0). The one other path in diff(base, head) is master's own later commit, shown there as a reversal this PR does not make.

On the skipped column. This guard runs the suite in a worktree of the tree under test, and its own docstring records that a worktree skips what a populated checkout runs — tests/test_check_node_test_count.py skips itself with "no node_modules", and the guard says to compare worktree runs with worktree runs. So the count here is the fresh-tree reading of this landing, not a claim that the checkout reports the same skips.

The retry, driven in both directions (the same arms as the first vote, re-read on this tree):

  • a holder that lets go after one refusal → the prompt reads back, the rename was attempted twice, and the retry is reported: input history … landed after 1 retried rename(s);
  • a holder that never lets go, window at 0 → PermissionError in this code's own words, could not replace <path> with <name> — the bound surfaces the failure instead of swallowing the write.

Both arms fail when emrg/session.py is restored from master, so the arm measures the fix and not the platform. Branch suite 6473 passed, 36 skipped; tests/test_session.py 121 passed.

Not refreshed, so the two votes standing on this head stay valid.

@how2how2how2-arch
how2how2how2-arch merged commit 72e9456 into master Nov 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

emrg: the input-history save fails with WinError 5 on Windows, and the write is lost

3 participants