Skip to content

fix(hosts): write instructions atomically - #643

Merged
evan-ak merged 2 commits into
NevaMind-AI:mainfrom
wutongyuonce:agent/atomic-instruction-write
Aug 18, 2026
Merged

evan-ak merged 2 commits into
NevaMind-AI:mainfrom
wutongyuonce:agent/atomic-instruction-write

Conversation

@wutongyuonce

@wutongyuonce wutongyuonce commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

📝 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?

  • Replaces direct Path.write_text() overwrites with a same-directory temporary-file write followed by os.replace().
  • Preserves the existing .bak backup behavior and the existing file mode.
  • Adds a regression test: if replacement fails, the original instruction file remains intact and the temporary file is removed.

🤔 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

  • Bug fix
  • New feature
  • Documentation update
  • Refactor / cleanup
  • Other (please explain)

✅ PR Quality Checklist

  • PR title follows an allowed format
  • Changes are limited in scope and easy to review
  • Documentation updated where applicable (no user-facing documentation change is needed)
  • No breaking changes
  • Related issues or discussions linked

🧪 Testing instructions

Run make check and make test.

Validated locally in the system environment:

  • pre-commit run -a
  • mypy
  • deptry src
  • pytest --cov --cov-config=pyproject.toml --cov-report=xml — 412 passed, 1 skipped

Note: uv lock --locked reports the existing base lockfile as stale despite no dependency-file change in this PR.


📌 Optional

  • Screenshots or examples added (not applicable)
  • Edge cases considered (replacement failure and temporary-file cleanup)
  • Follow-up tasks mentioned

@wutongyuonce wutongyuonce changed the title Write host instructions atomically fix(hosts): write instructions atomically Aug 15, 2026
@wutongyuonce
wutongyuonce force-pushed the agent/atomic-instruction-write branch from 9ff4318 to 61cb2b5 Compare August 15, 2026 16:11
@wutongyuonce
wutongyuonce marked this pull request as ready for review August 15, 2026 16:14

@xnne-bot xnne-bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@wutongyuonce

Copy link
Copy Markdown
Contributor Author

@xnne-bot Addressed in 5d2e1f7.

_write() now resolves a symlink before creating the same-directory temporary file and replacing the target, so the symlink remains intact while its referent is updated. The temporary path is now a Path and cleanup runs in finally.

Added a regression test asserting that an instruction symlink survives and its referent receives the managed block. Verified with tests/test_host_instruction.py (42 passed) and Ruff.

@MrXnneHang MrXnneHang 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.

LGTMeow 🐾

@wutongyuonce
wutongyuonce requested a review from xnne-bot August 18, 2026 05:20
@evan-ak
evan-ak merged commit aec07c3 into NevaMind-AI:main Aug 18, 2026
3 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.

[BUG] Prevent instruction file truncation during updates

4 participants