Skip to content

Write Host Rule Files Atomically in global_rules.py #4317

Closed

Description

@ptr727

Problem

host-setup/agent-safety/global_rules.py writes each host-wide rule file in place. An interrupt that lands after the file is truncated and before the write completes leaves the file truncated. Since #4305 the script raises KeyboardInterrupt on SIGINT and SIGTERM, but the window predates that change. The rule files hold the maintainer's own host rules beside the kit's marked block, so a truncation can lose content no install restores.

CodeRabbit raised this on the promotion #4315 as an outside-diff finding, at global_rules.py:211.

Done looks like

  • Each persistent rule-file write goes to a temporary file in the target's directory, then os.replace swaps it in. A target that is a symlink, such as a dotfiles link, keeps its link: the write resolves the link and replaces the file it points at.
  • A test interrupts a write and shows the original file intact.

The fix edits host-setup/agent-safety/, so it needs the maintainer's approval on this issue before an attended run implements it.

🤖 Generated with Claude Code

Activity

  1. added
    bug
    on Nov 20, 2026
  2. ptr727 commented on Nov 21, 2026

    @ptr727
    OwnerAuthor

    Sibling in the same kit: CodeRabbit raised the same in-place truncation on #4324 as an outside-diff finding. It sits in the shared persistent writer _write_open_file in host-setup/agent-safety/claude/install.py, which calls os.ftruncate and then rewrites in place. An interrupt between the two leaves settings.json empty or partial. The writer predates #4324, which only adds one more caller to it. This issue's fix should cover both files, with the same temp-beside-target plus os.replace shape, the same symlink preservation, and the same interrupted-write test.

  3. ptr727 commented on Nov 21, 2026

    @ptr727
    OwnerAuthor

    Maintainer approval, 2026-11-21: approved for an attended run, covering both files: host-setup/agent-safety/global_rules.py and the shared persistent writer in host-setup/agent-safety/claude/install.py. Each write goes to a temp file beside the target and is then swapped in with os.replace. Symlinks are preserved, and an interrupted-write test shows the original intact.

  4. ptr727 commented on Nov 22, 2026

    @ptr727
    OwnerAuthor

    Backlog burndown claim (round 3, 2026-11-22): branch burndown/rule-files-atomic-write. Predicted files: host-setup/agent-safety/global_rules.py, host-setup/agent-safety/claude/install.py, tests/test_global_rules.py, host-setup/agent-safety/claude/test_install.py. No other group may edit these files while this claim stands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions