Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 89 additions & 41 deletions host-setup/agent-safety/claude/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 +541,29 @@ def payload_digest():
return h.hexdigest()[:16]


def read_claude_md(claude_md):
"""CLAUDE.md's text, or None where it is absent, not a regular file, or unreadable.

One reader for every caller, so a file none of them can read gives each the same answer rather
than a traceback in whichever reads it first.
"""
try:
if not claude_md.is_file():
return None
return claude_md.read_text(encoding="utf-8", errors="replace")
except OSError:
return None


def blocks_present(claude_md):
"""The marker version of each block actually in CLAUDE.md, by name.

Read from the file rather than from what the installer meant to write, since the question the
stamp answers is what is on the machine.
"""
if not claude_md.exists():
text = read_claude_md(claude_md)
if text is None:
return {}
text = claude_md.read_text(encoding="utf-8", errors="replace")
found = {}
for marker in BLOCK_MARKERS:
# A start marker alone is a half-written block, which a presence check reads as installed.
Expand All @@ -569,9 +583,9 @@ def marker_corruption(claude_md):
CLAUDE.md records the same empty block set it reads, so the stamp and the file agree and the
corruption reads as a match. Two wrong answers agreeing is the failure this exists to catch.
"""
if not claude_md.is_file():
text = read_claude_md(claude_md)
if text is None:
return []
text = claude_md.read_text(encoding="utf-8", errors="replace")
valid = blocks_present(claude_md)
out = []
for marker in BLOCK_MARKERS:
Expand All @@ -594,12 +608,13 @@ def installed_digest(claude_home):
# A hook added to the deploy list and not to this one installs and is never covered by the currentness digest.
deployed = [claude_home / "hooks" / name for name in DEPLOYED_HOOKS]
claude_md = claude_home / "CLAUDE.md"
if not all(f.is_file() for f in deployed) or not claude_md.is_file():
claude_text = read_claude_md(claude_md)
if not all(f.is_file() for f in deployed) or claude_text is None:
return None
h = hashlib.sha256()
for f in deployed:
h.update(normalized(f.read_bytes()))
text = normalized(claude_md.read_text(encoding="utf-8", errors="replace"))
text = normalized(claude_text)
for marker in BLOCK_MARKERS:
found = re.search(
rf"<!-- {marker} v\d+ start -->.*?<!-- {marker} v\d+ end -->", text, re.DOTALL
Expand All @@ -626,8 +641,8 @@ def build_stamp(claude_home, installed, instructions_digest=None):
"blocks": blocks_present(claude_home / "CLAUDE.md"),
"instructionsDigest": instructions_digest
or (
text_digest((claude_home / "CLAUDE.md").read_text(encoding="utf-8", errors="replace"))
if (claude_home / "CLAUDE.md").is_file()
text_digest(claude_text)
if (claude_text := read_claude_md(claude_home / "CLAUDE.md")) is not None
else None
),
"installedUtc": installed,
Expand Down Expand Up @@ -909,7 +924,7 @@ def report(claude_home):
live_installed = installed_digest(claude_home)
if live_installed is None:
problems.append(
"the deployed hook or CLAUDE.md is missing, so the kit is not fully installed"
"a deployed hook or CLAUDE.md is missing or unreadable, so the kit is not fully installed"
)
elif live_installed != current:
problems.append("the installed content differs from what this checkout would write")
Expand Down Expand Up @@ -944,10 +959,14 @@ def report(claude_home):
claude_md = claude_home / "CLAUDE.md"
local_path = local_instructions_path()
local_text, local_problem = read_local_instructions(local_path)
claude_text = read_claude_md(claude_md)
claude_unreadable = os.path.lexists(claude_md) and claude_text is None
if local_problem:
problems.append(local_problem)
elif claude_md.is_file():
live_text = normalized(claude_md.read_text(encoding="utf-8", errors="replace"))
if claude_unreadable:
problems.append(f"{claude_md} exists but is not a readable regular file")
elif not local_problem and claude_text is not None:
live_text = normalized(claude_text)
if live_text != render_instructions(local_path, local_text):
if text_digest(live_text) == stamp.get("instructionsDigest"):
problems.append(
Expand Down Expand Up @@ -975,6 +994,8 @@ def report(claude_home):
# The installer refuses an unusable local file, so re-running first would only repeat the refusal.
if local_problem:
print(f" Fix {local_path} first, since the installer refuses it as it stands.")
if claude_unreadable:
print(f" If the re-run refuses {claude_md}, move it aside first.")
print(
" Re-run the installer with no arguments. It is idempotent, and it backs up a "
"CLAUDE.md edited since the last install before rewriting it."
Expand Down Expand Up @@ -1026,8 +1047,62 @@ def main():
if local_problem:
sys.stderr.write(f"Nothing was installed: {local_problem}.\n")
return 1

print(f"Installing agent host-safety kit into: {claude_home}")
claude_home.mkdir(parents=True, exist_ok=True)

# 0. CLAUDE.md is rendered whole: a header, one marker block per snippet, then the host-local file.
# It goes first, so a file that cannot be read, backed up, or written stops the run before any hook or setting changes.
# Attempting the real read and write is the check, since a predicted one missed cases the write then raised on.
# The safety block states restrictions only.
# The fleet block enables, so it stays separate from a block whose own text says nothing in it widens a permission.
# A file whose digest matches the stamp is the one the last install wrote, so it is replaced silently.
# Anything else is a hand edit, or a file from before whole-file ownership, so it is backed up first.
# Preserve CLAUDE.md's existing line endings: work in \n internally, write back with its own ending.
# A file that is not valid UTF-8 still decodes for the comparison, and the backup keeps its raw bytes.
# A file already holding the render is left unwritten, so a read-only file that is current still installs.
rendered = render_instructions(local_path, local_text)
# Anything else at the path, a FIFO or a link to a device, would hang the write or swallow it.
# A dangling link is let through, so a dotfiles link whose target is not created yet still writes it.
try:
dangling = claude_md.is_symlink() and not os.path.exists(claude_md)
if os.path.lexists(claude_md) and not claude_md.is_file() and not dangling:
sys.stderr.write(
f"Nothing was installed: {claude_md} is not a regular file. Move it aside and re-run.\n"
)
return 1
raw = claude_md.read_bytes() if claude_md.is_file() else None
except OSError as e:
sys.stderr.write(
f"Nothing was installed: {claude_md} cannot be read ({e}). Move it aside and re-run.\n"
)
return 1
newline = "\r\n" if raw is not None and b"\r\n" in raw else "\n"
existing = None if raw is None else normalized(raw.decode("utf-8", errors="replace"))
backup = None
if existing == rendered:
action = "already current"
else:
try:
if existing is not None and text_digest(existing) != stamped_instructions_digest(
claude_home
):
backup = write_backup(claude_md, raw)
claude_md.write_bytes(rendered.replace("\n", newline).encode("utf-8"))
except OSError as e:
kept = f" Its prior content is backed up at {backup}." if backup else ""
sys.stderr.write(
f"Nothing was installed: {claude_md} could not be rewritten ({e}).{kept} "
"Move it aside or make it writable, then re-run.\n"
)
return 1
if backup is not None:
action = f"rewritten, the edited prior file backed up to {backup}"
else:
action = "updated" if existing is not None else "written"
print(f" CLAUDE.md -> {claude_md} ({action})")
if backup is not None:
print(f" Move anything host-specific into {local_path}, then re-run to append it.")

hooks_dir.mkdir(parents=True, exist_ok=True)

# 1. Stage every hook beside its live path, self-test each staged copy, and only then replace.
Expand All @@ -1044,7 +1119,7 @@ def main():
except OSError as e:
for _n, f in staged:
f.unlink(missing_ok=True)
sys.stderr.write(f"staging {src_name} failed ({e}); nothing was replaced.\n")
sys.stderr.write(f"staging {src_name} failed ({e}); no hook was replaced.\n")
return 1
try:
os.chmod(tmp, 0o755)
Expand All @@ -1061,7 +1136,7 @@ def main():
for _n, f in staged:
f.unlink(missing_ok=True)
sys.stderr.write(
f"{src_name} self-test FAILED; nothing was replaced.\n" + r.stdout + r.stderr
f"{src_name} self-test FAILED; no hook was replaced.\n" + r.stdout + r.stderr
)
return 1
print(f" {src_name} self-test: PASS")
Expand Down Expand Up @@ -1241,33 +1316,6 @@ def reject(where, held, want):
for line in done:
print(f" settings -> {settings} ({line})")

# 4. CLAUDE.md is rendered whole: a header, one marker block per snippet, then the host-local file.
# The safety block states restrictions only.
# The fleet block enables, so it stays separate from a block whose own text says nothing in it widens a permission.
# A file whose digest matches the stamp is the one the last install wrote, so it is replaced silently.
# Anything else is a hand edit, or a file from before whole-file ownership, so it is backed up first.
# Preserve CLAUDE.md's existing line endings: work in \n internally, write back with its own ending.
# A file that is not valid UTF-8 still decodes for the comparison, and the backup keeps its raw bytes.
rendered = render_instructions(local_path, local_text)
backup = None
if claude_md.exists():
raw = claude_md.read_bytes()
newline = "\r\n" if b"\r\n" in raw else "\n"
existing = normalized(raw.decode("utf-8", errors="replace"))
if existing == rendered:
action = "already current"
elif text_digest(existing) == stamped_instructions_digest(claude_home):
action = "updated"
else:
backup = write_backup(claude_md, raw)
action = f"rewritten, the edited prior file backed up to {backup}"
else:
newline, action = "\n", "written"
claude_md.write_bytes(rendered.replace("\n", newline).encode("utf-8"))
print(f" CLAUDE.md -> {claude_md} ({action})")
if backup is not None:
print(f" Move anything host-specific into {local_path}, then re-run to append it.")

# 5. Stamp the machine, written last so it records a completed install rather than an attempted one.
# The blocks are read back off disk here, so the stamp reports what CLAUDE.md holds rather than what was intended.
stamp_path = claude_home / "agent-safety-stamp.json"
Expand Down
114 changes: 114 additions & 0 deletions host-setup/agent-safety/claude/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,120 @@ def test_a_local_file_in_a_directory_that_cannot_be_entered_stops_with_a_message
self.assertNotIn("Traceback", r.stderr)
self.assertFalse(self.home.exists())

def assert_refused_with_nothing_changed(self, r, reason):
"""A refusal at the CLAUDE.md step leaves no hook, settings file, or stamp behind."""
self.assertEqual(r.returncode, 1, r.stdout + r.stderr)
self.assertIn("Nothing was installed", r.stderr)
self.assertIn(reason, r.stderr)
self.assertNotIn("Traceback", r.stderr)
self.assertFalse((self.home / "hooks").exists())
self.assertFalse((self.home / "settings.json").exists())
self.assertFalse(self.stamp.exists())

def needs_posix_non_root(self):
if os.name != "posix" or os.geteuid() == 0:
self.skipTest("needs a non-root POSIX user for a file mode to deny access")

def test_a_claude_md_that_is_a_directory_stops_the_install_with_nothing_changed(self):
"""The write would raise after the hooks and settings were already replaced."""
self.md.mkdir(parents=True)
self.assert_refused_with_nothing_changed(run(self.home), "is not a regular file")

def test_a_claude_md_fifo_is_refused_rather_than_hanging_the_write(self):
if not hasattr(os, "mkfifo"):
self.skipTest("this host has no FIFOs")
self.home.mkdir(parents=True)
os.mkfifo(self.md)
self.assert_refused_with_nothing_changed(run(self.home), "is not a regular file")

def test_a_claude_md_link_to_a_device_is_refused_rather_than_written_into_nothing(self):
if not os.path.exists(os.devnull) or os.name != "posix":
self.skipTest("needs a POSIX null device")
self.home.mkdir(parents=True)
self.md.symlink_to(os.devnull)
self.assert_refused_with_nothing_changed(run(self.home), "is not a regular file")

def test_a_claude_md_dangling_symlink_stops_the_install_with_nothing_changed(self):
"""A dotfiles link whose repository is not cloned yet has nowhere to write."""
self.home.mkdir(parents=True)
try:
self.md.symlink_to(self.home.parent / "not-cloned" / "CLAUDE.md")
except OSError:
self.skipTest("this host cannot create a symlink")
self.assert_refused_with_nothing_changed(run(self.home), "could not be rewritten")

def test_a_claude_md_symlink_loop_stops_the_install_with_nothing_changed(self):
self.home.mkdir(parents=True)
try:
self.md.symlink_to(self.md)
except OSError:
self.skipTest("this host cannot create a symlink")
self.assert_refused_with_nothing_changed(run(self.home), str(self.md))

def test_an_unreadable_claude_md_stops_the_install_with_nothing_changed(self):
self.needs_posix_non_root()
self.home.mkdir(parents=True)
self.md.write_text("A constructed note.\n", encoding="utf-8")
self.md.chmod(0)
self.addCleanup(self.md.chmod, 0o644)
self.assert_refused_with_nothing_changed(run(self.home), "cannot be read")

def test_a_read_only_edited_claude_md_is_refused_with_its_backup_kept(self):
"""The backup lands before the write fails, and the refusal names where it is."""
self.needs_posix_non_root()
self.home.mkdir(parents=True)
self.md.write_text("A constructed note.\n", encoding="utf-8")
self.md.chmod(0o444)
self.addCleanup(self.md.chmod, 0o644)
r = run(self.home)
self.assert_refused_with_nothing_changed(r, "could not be rewritten")
self.assertIn("backed up at", r.stderr)
self.assertEqual(len(self.backups()), 1)

def test_a_read_only_claude_md_that_is_current_still_installs_and_reports_current(self):
"""Nothing would change, so the file is not written and its mode does not matter."""
self.needs_posix_non_root()
self.install()
self.md.chmod(0o444)
self.addCleanup(self.md.chmod, 0o644)
r = self.install()
self.assertIn("(already current)", r.stdout)
self.assertEqual(run(self.home, "--report").returncode, 0)

def test_a_symlinked_claude_md_installs_through_the_link(self):
"""The dotfiles case: the link stays a link and the kit lands in its target."""
self.home.mkdir(parents=True)
target = self.home.parent / "dotfiles-CLAUDE.md"
try:
self.md.symlink_to(target)
except OSError:
self.skipTest("this host cannot create a symlink")
target.write_text("", encoding="utf-8")
self.install()
self.assertTrue(self.md.is_symlink())
self.assertIn("agent-safety", target.read_text(encoding="utf-8"))
self.assertEqual(run(self.home, "--report").returncode, 0)

def test_the_report_on_a_claude_md_directory_names_it_rather_than_crashing(self):
self.install()
self.md.unlink()
self.md.mkdir()
r = run(self.home, "--report")
self.assertEqual(r.returncode, 1, r.stdout + r.stderr)
self.assertNotIn("Traceback", r.stderr)
self.assertIn("exists but is not a readable regular file", r.stdout)
self.assertIn("move it aside first", r.stdout)

def test_the_report_on_an_unreadable_claude_md_gives_a_verdict_rather_than_crashing(self):
self.needs_posix_non_root()
self.install()
self.md.chmod(0)
self.addCleanup(self.md.chmod, 0o644)
r = run(self.home, "--report")
self.assertEqual(r.returncode, 1, r.stdout + r.stderr)
self.assertNotIn("Traceback", r.stderr)
self.assertIn("exists but is not a readable regular file", r.stdout)

def test_a_stamp_failing_its_shape_check_does_not_vouch_for_the_file(self):
"""A hand-edited stamp could otherwise carry a digest that skips the backup."""
self.install()
Expand Down
Loading