Repository navigation
fix(hosts): write instructions atomically - #643
Conversation
9ff4318 to
61cb2b5
Compare
There was a problem hiding this comment.
The atomic-write direction is good, but this changes the behavior for user-managed symlinks: os.replace(temporary, path) replaces the symlink itself, while the previous path.write_text(...) followed it. An AGENTS.md/CLAUDE.md symlink into a dotfiles repo would therefore become a regular file without updating its referent.
Please preserve that behavior and keep paths as pathlib.Path objects. os.fdopen() is still appropriate for the descriptor returned by mkstemp(), but the temporary path can be converted immediately:
target = path.resolve() if path.is_symlink() else path
fd, temporary_name = tempfile.mkstemp(
dir=target.parent,
prefix=".tmp-",
suffix=target.suffix,
)
temporary = Path(temporary_name)
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
handle.write(updated)
if target.exists():
shutil.copymode(target, temporary)
temporary.replace(target)
finally:
temporary.unlink(missing_ok=True)Please also add a regression test asserting that the symlink survives and its referent is updated.
|
@xnne-bot Addressed in 5d2e1f7.
Added a regression test asserting that an instruction symlink survives and its referent receives the managed block. Verified with |
📝 Pull Request Summary
Prevent user-owned host instruction files from being left empty or partial when an instruction update is interrupted.
✅ What does this PR do?
Path.write_text()overwrites with a same-directory temporary-file write followed byos.replace()..bakbackup behavior and the existing file mode.🤔 Why is this change needed?
Path.write_text()truncates AGENTS.md, CLAUDE.md, and similar host instruction files before it completes the new write. An interruption can therefore destroy or tear the user instructions.Fixes #642.
🔍 Type of Change
✅ PR Quality Checklist
🧪 Testing instructions
Run
make checkandmake test.Validated locally in the system environment:
pre-commit run -amypydeptry srcpytest --cov --cov-config=pyproject.toml --cov-report=xml— 412 passed, 1 skippedNote:
uv lock --lockedreports the existing base lockfile as stale despite no dependency-file change in this PR.📌 Optional