Skip to content

Commit 25cf480

Browse files
ptr727claude
andcommitted
Derive the Payload List From the Block List, So a New Snippet Cannot Escape the Digest
`test_every_deployed_file_is_in_the_digest` matched only literal `HERE / "..."` reads, which is the hook and nothing else. The snippet names came from a list inside `main`, so the test never saw them and a new snippet would have passed while being absent from the digest. It asserted coverage and measured one file. The structural fix rather than a wider regex: `CLAUDE_MD_BLOCKS` is a module constant and `PAYLOAD_FILES` derives from it, so adding a block enters the digest with no second edit. Two lists maintained by hand are two lists that drift. Three readers each carried their own copy of the marker pair, which is the same hazard one level down, and all three now read `BLOCK_MARKERS`. Three cases: the scan covers both sources and asserts it matched more than the hook, the derivation holds, and no reader carries its own pair. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 3894a87 commit 25cf480

2 files changed

Lines changed: 42 additions & 11 deletions

File tree

‎host-setup/agent-safety/install.py‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,21 @@
3434
# A reader that predates a field needs to know the shape changed rather than infer it from a missing key.
3535
STAMP_VERSION=1
3636

37-
# The files whose bytes this kit actually places on a machine.
37+
# The marker-delimited blocks this kit maintains in CLAUDE.md, in the order they are written and hashed.
38+
# One list rather than the marker pair repeated at each reader.
39+
# A block added to one reader and not the others is installed and then never checked by what reports on it.
40+
CLAUDE_MD_BLOCKS= (
41+
("agent-safety", "claude-md-safety.md"),
42+
("fleet-bootstrap", "claude-md-fleet.md"),
43+
)
44+
BLOCK_MARKERS=tuple(markerformarker, _inCLAUDE_MD_BLOCKS)
45+
46+
# The files whose bytes this kit actually places on a machine, the hook first and then each block.
47+
# Derived rather than listed, so a block added above enters the digest without a second edit.
48+
# Written out, this list and the block list drifted apart silently and the digest stopped covering a file.
3849
# The digest is taken over these rather than over the commit, since it is the content that runs.
3950
# A clean commit and a dirty checkout install different bytes while reporting the same SHA.
40-
PAYLOAD_FILES= ("gh-write-guard.py","claude-md-safety.md", "claude-md-fleet.md")
51+
PAYLOAD_FILES= ("gh-write-guard.py",) +tuple(filenamefor_, filenameinCLAUDE_MD_BLOCKS)
4152

4253
# Distinguishes an absent key from one holding an explicit null, which `dict.get` reports alike.
4354
# The two need different answers, since a gap is filled and a null is a settings error.
@@ -182,7 +193,7 @@ def blocks_present(claude_md):
182193
return {}
183194
text=claude_md.read_text(encoding="utf-8", errors="replace")
184195
found= {}
185-
formarkerin("agent-safety", "fleet-bootstrap"):
196+
formarkerinBLOCK_MARKERS:
186197
# A start marker alone is a half-written block, which a presence check reads as installed.
187198
# Exactly one pair, since the installer writes one and a duplicate is a corrupted file.
188199
# Two blocks mean the second silently governs, and reporting the first as current hides that.
@@ -205,7 +216,7 @@ def marker_corruption(claude_md):
205216
text=claude_md.read_text(encoding="utf-8", errors="replace")
206217
valid=blocks_present(claude_md)
207218
out= []
208-
formarkerin("agent-safety", "fleet-bootstrap"):
219+
formarkerinBLOCK_MARKERS:
209220
ifre.search(rf"<!-- {marker} v\d+ (?:start|end) -->", text) andmarkernotinvalid:
210221
out.append(f"the {marker} markers in CLAUDE.md are duplicated or incomplete")
211222
returnout
@@ -228,7 +239,7 @@ def installed_digest(claude_home):
228239
h=hashlib.sha256()
229240
h.update(normalized(hook.read_bytes()))
230241
text=normalized(claude_md.read_text(encoding="utf-8", errors="replace"))
231-
formarkerin("agent-safety", "fleet-bootstrap"):
242+
formarkerinBLOCK_MARKERS:
232243
found=re.search(rf"<!-- {marker} v\d+ start -->.*?<!-- {marker} v\d+ end -->", text, re.DOTALL)
233244
ifnotfound:
234245
returnNone
@@ -548,15 +559,14 @@ def reject(where, held, want):
548559
# The two blocks install and update independently, so one can change without rewriting the other.
549560
# The safety block states restrictions only.
550561
# The fleet block enables, so it stays separate from a block whose own text says nothing in it widens a permission.
551-
blocks= [("agent-safety", "claude-md-safety.md"), ("fleet-bootstrap", "claude-md-fleet.md")]
552562
# Preserve CLAUDE.md's existing line endings: work in \n internally, write back with its own ending.
553563
ifclaude_md.exists():
554564
raw=claude_md.read_bytes()
555565
newline="\r\n"ifb"\r\n"inrawelse"\n"
556566
existing=normalized(raw.decode("utf-8"))
557567
else:
558568
newline, existing="\n", ""
559-
formarker, filenameinblocks:
569+
formarker, filenameinCLAUDE_MD_BLOCKS:
560570
snippet= (HERE/filename).read_text(encoding="utf-8").strip()
561571
block_re=re.compile(rf"<!-- {marker} v\d+ start -->.*?<!-- {marker} v\d+ end -->", re.DOTALL)
562572
ifblock_re.search(existing):

‎host-setup/agent-safety/test_install.py‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,35 @@ def test_a_real_edit_to_a_snippet_is_still_reported(self):
465465
target.write_bytes(original)
466466

467467
deftest_every_deployed_file_is_in_the_digest(self):
468-
"""The inverse: the kit copies gh-write-guard.py and both snippets, and each must be covered."""
468+
"""The inverse: the kit copies gh-write-guard.py and every snippet, and each must be covered.
469+
470+
The source scan alone was a false positive. It matched only literal `HERE / "..."` reads,
471+
which is the hook and nothing else, while the snippet names came from a list inside `main`.
472+
A new snippet passed it while being absent from the digest, so the two sources of truth are
473+
both checked now, and the derivation below is what actually makes the gap impossible.
474+
"""
469475
source=INSTALL.read_text(encoding="utf-8")
470-
fornameinre.findall(r'HERE / "([^"]+\.(?:py|md))"', source):
471-
ifname=="install.py":
472-
continue
476+
named= {namefornameinre.findall(r'HERE / "([^"]+\.(?:py|md))"', source)}
477+
named|= {filenamefor_, filenameininstall.CLAUDE_MD_BLOCKS}
478+
named.discard("install.py")
479+
# The hook is the only literal read; every other entry arrives from the block list.
480+
self.assertGreater(len(named), 1, "the scan matched only one file, so it is not covering the blocks")
481+
fornameinsorted(named):
473482
self.assertIn(name, install.PAYLOAD_FILES,
474483
f"install.py reads {name} but PAYLOAD_FILES omits it, so the digest misses it")
475484

485+
deftest_the_payload_list_is_derived_from_the_block_list(self):
486+
"""Written out by hand, the two drifted and the digest stopped covering a deployed file."""
487+
self.assertEqual(install.PAYLOAD_FILES,
488+
("gh-write-guard.py",) +tuple(ffor_, fininstall.CLAUDE_MD_BLOCKS))
489+
490+
deftest_every_reader_uses_the_same_marker_list(self):
491+
"""Three readers each carried their own marker pair, so a new block could reach one only."""
492+
source=INSTALL.read_text(encoding="utf-8")
493+
self.assertNotIn('("agent-safety", "fleet-bootstrap")', source,
494+
"a reader is carrying its own marker pair instead of BLOCK_MARKERS")
495+
self.assertEqual(install.BLOCK_MARKERS, tuple(mform, _ininstall.CLAUDE_MD_BLOCKS))
496+
476497
deftest_the_one_line_summary_names_the_host_and_the_commit(self):
477498
self.install()
478499
stamp=json.loads(self.stamp.read_text(encoding="utf-8"))

0 commit comments

Comments
 (0)