Skip to content

Name a Partial CLAUDE.md Write Correctly and Stop Re-Backing Up a Refused File #2426

Closed

Description

@ptr727

What happens

The host-safety installer renders ~/.claude/CLAUDE.md first and treats the real write as its own check. Two smaller gaps remain after #2423's follow-up fix:

  • A partial write is reported as nothing installed. write_bytes truncates the file before writing. When the write fails partway, for example on a full disk or a file-size limit, the refusal still says "Nothing was installed" and suggests a permissions remedy. The file is in fact left truncated, so new sessions load a partial CLAUDE.md until the next run.
    • A re-run recovers it. Its first step backs up the truncated file, and that backup is useless.
    • An atomic temp-file-and-replace write would close this, but it would replace a dotfiles symlink with a plain file. The fix needs to write through a link, or at least name the failure correctly.
  • Each refused re-run adds another backup. With a read-only CLAUDE.md holding a hand edit, every run writes a timestamped .bak and then fails at the write. The backups pile up. Taking the backup only once the write is known to be possible would stop that.

Shape that reproduces it

  • Partial write: set a 4 KiB file-size limit, give a stamp-matching CLAUDE.md larger than that, and run the installer. The run refuses with "Nothing was installed", and the file is left at 4096 bytes.
  • Backups piling up: run chmod 444 on a hand-edited CLAUDE.md, then run the installer twice. Both runs are refused, and two .bak files are left behind.

Found by the local strict review of the CLAUDE.md fix branch for #2425. These are low severity: no hand-written content is lost in either case.

Activity

  1. added
    agents
    on Oct 6, 2026
  2. ptr727 commented on Oct 6, 2026

    @ptr727
    OwnerAuthor

    A third gap of the same kind: a FIFO at ~/.claude/settings.json or ~/.claude/agent-safety-stamp.json hangs both the install and --report. The install reads and writes settings.json and the stamp, and the report reads both, all without a regular-file check. The stamp write runs last, so it leaves a half-finished install with no stamp. The local instruction file is now read through read_regular_file() in install.py, which opens non-blocking and checks the open descriptor. That helper is the shape to reuse for these reads. Found by the local strict review of the local-file FIFO fix for #2425; these reads predate that change.

  3. ptr727 commented on Oct 6, 2026

    @ptr727
    OwnerAuthor

    Windows limit, unverified on a native host: on Windows, os.lstat on a path whose parent component is a regular file is expected to raise FileNotFoundError (ERROR_PATH_NOT_FOUND), not NotADirectoryError. So #2429's refusal of such a local instruction path is POSIX-only, and Windows still reads it as absent. To confirm on Windows: python -c "import os,pathlib; p=pathlib.Path(os.environ['TEMP'])/'f'; p.write_text(''); os.lstat(p/'x')". A fix that works on both would check each ancestor after a FileNotFoundError.

  4. ptr727 commented on Oct 6, 2026

    @ptr727
    OwnerAuthor

    One more reader of the same kind: read_claude_md() in host-setup/agent-safety/claude/install.py checks is_file() and then calls read_text(), two separate path lookups. A CLAUDE.md swapped for a FIFO between them blocks the read, which needs a concurrent process to replace the file during a run. Moving it, the settings.json reads and the stamp reads onto read_regular_file() closes all of them at once. Raised by Copilot on #2425.

  5. ptr727 commented on Oct 7, 2026

    @ptr727
    OwnerAuthor

    Done, shipped in 2.0.853 through #2455 and promotion #2456. The native Windows check passed against main:

    • On Windows, os.lstat on a path whose parent is a regular file raises FileNotFoundError (WinError 3), as expected. The ancestor walk in read_local_instructions is what refuses that case there.
    • test_install.py run natively with the eight cases that exercise this change without the hook self-test: the ancestor walk (file parent refused, missing directory still absent), the directory fallback in read_regular_file (a CLAUDE.md directory and a local-path directory), and the close and truncate paths of write_regular_file. All eight pass.

    The full native suite cannot run on Windows yet, because the guard hook self-test fails there and every install stops at it. That is unrelated to this change and is tracked as #2458. The pre-existing partial-file state the local review found is #2454.

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

    agents

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions