agent-bios 0.19.4 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEPENDENCIES.md +19 -5
- package/README.md +14 -19
- package/claude/agents/frontier.md +1 -1
- package/claude/guides/claude-prompting.md +475 -234
- package/claude/guides/cli-multi-model-workflow.md +4 -3
- package/claude/guides/gpt-prompting.md +235 -136
- package/claude/guides/session-distill-workflow.md +93 -43
- package/claude/guides/tooling-gotchas.md +3 -1
- package/codex/guides/claude-prompting.md +475 -234
- package/codex/guides/cli-multi-model-workflow.md +4 -3
- package/codex/guides/gpt-prompting.md +235 -136
- package/codex/guides/session-distill-workflow.md +93 -43
- package/codex/guides/tooling-gotchas.md +3 -1
- package/compose/assemble.py +4 -1
- package/compose/canary.sh +8 -6
- package/compose/host_platform.py +158 -15
- package/compose/instructions-state.py +183 -41
- package/compose/instructions_install.py +222 -27
- package/compose/instructions_session.py +250 -75
- package/compose/instructions_setup_cli.py +2 -36
- package/compose/instructions_store.py +160 -3
- package/compose/instructions_transaction.py +29 -5
- package/docs/advanced-launch.md +4 -0
- package/docs/releases/0.20.0.md +102 -0
- package/install.sh +42 -14
- package/launch/agent-launch.py +152 -18
- package/launch/agent-launch.toml +17 -7
- package/launch/agent-launch.zsh +104 -13
- package/launch/claude_subcommands.py +179 -0
- package/launch/shell_integration.py +7 -1
- package/learn/collect-learning.py +60 -9
- package/learn/migrate-learnings.py +70 -4
- package/learn/redact.py +2 -1
- package/package.json +36 -3
- package/provenance.json +1 -1
- package/session-cost.py +80 -9
- package/session-distill/coverage.json +6 -0
- package/session-distill/distill.py +2575 -0
- package/session-distill/grammar/__init__.py +0 -0
- package/session-distill/grammar/owner_lines.py +874 -0
- package/session-distill/originators.json +9 -0
- package/session-distill/packets/cross_review.md +31 -0
- package/session-distill/packets/form.md +86 -0
- package/session-distill/packets/judge.md +81 -0
- package/session-distill/packets/placement_contract.md +90 -0
- package/session-distill/packets/propose.md +124 -0
- package/session-distill/packets/read.md +57 -0
- package/session-distill/packets/reconsider.md +53 -0
- package/session-distill/packets/s7b.md +65 -0
- package/session-distill/packets/s9b.md +39 -0
- package/session-distill/packets/stitch.md +35 -0
- package/session-distill/packets/sweep.md +33 -0
- package/session-distill/runlib/__init__.py +0 -0
- package/session-distill/runlib/bound.py +1102 -0
- package/session-distill/runlib/breaker.py +467 -0
- package/session-distill/runlib/census.py +2347 -0
- package/session-distill/runlib/claims.py +420 -0
- package/session-distill/runlib/controls.py +902 -0
- package/session-distill/runlib/coverage.py +881 -0
- package/session-distill/runlib/declare.py +888 -0
- package/session-distill/runlib/delivery_evidence.py +1145 -0
- package/session-distill/runlib/derive.py +812 -0
- package/session-distill/runlib/destinations.py +3072 -0
- package/session-distill/runlib/dispatch.py +7939 -0
- package/session-distill/runlib/drift.py +1077 -0
- package/session-distill/runlib/entry.py +401 -0
- package/session-distill/runlib/evidence.py +890 -0
- package/session-distill/runlib/exposure.py +1162 -0
- package/session-distill/runlib/gate.py +2727 -0
- package/session-distill/runlib/gitread.py +79 -0
- package/session-distill/runlib/inventory.py +19624 -0
- package/session-distill/runlib/journal.py +1919 -0
- package/session-distill/runlib/ledger.py +3210 -0
- package/session-distill/runlib/ledger_compat.py +101 -0
- package/session-distill/runlib/ledger_migration.py +190 -0
- package/session-distill/runlib/lifecycle_intake.py +693 -0
- package/session-distill/runlib/lineage.py +454 -0
- package/session-distill/runlib/measures.py +249 -0
- package/session-distill/runlib/monitor.py +1180 -0
- package/session-distill/runlib/operations.py +522 -0
- package/session-distill/runlib/packet.py +838 -0
- package/session-distill/runlib/placement.py +1769 -0
- package/session-distill/runlib/projection.py +4090 -0
- package/session-distill/runlib/ranking.py +740 -0
- package/session-distill/runlib/receipts.py +200 -0
- package/session-distill/runlib/records.py +1965 -0
- package/session-distill/runlib/search.py +249 -0
- package/session-distill/runlib/sequencing.py +829 -0
- package/session-distill/runlib/sources.py +641 -0
- package/session-distill/runlib/stage_adjudicate.py +845 -0
- package/session-distill/runlib/stage_apply.py +1378 -0
- package/session-distill/runlib/stage_approvals.py +758 -0
- package/session-distill/runlib/stage_close.py +1220 -0
- package/session-distill/runlib/stage_confirm.py +929 -0
- package/session-distill/runlib/stage_delivery.py +3080 -0
- package/session-distill/runlib/stage_form.py +2576 -0
- package/session-distill/runlib/stage_judge.py +4248 -0
- package/session-distill/runlib/stage_propose.py +3307 -0
- package/session-distill/runlib/stage_read.py +5179 -0
- package/session-distill/runlib/stage_render.py +2739 -0
- package/session-distill/runlib/stage_retrieve.py +924 -0
- package/session-distill/runlib/stage_review_korean.py +1002 -0
- package/session-distill/runlib/stage_review_proposals.py +1292 -0
- package/session-distill/runlib/stagelock.py +412 -0
- package/session-distill/runlib/stop.py +1061 -0
- package/session-distill/runlib/targets.py +809 -0
- package/session-distill/runlib/usage.py +280 -0
- package/session-distill/runlib/validate.py +319 -0
- package/session-distill/runlib/views.py +4327 -0
- package/session-distill/schemas/adjudication.schema.json +35 -0
- package/session-distill/schemas/approval.schema.json +35 -0
- package/session-distill/schemas/census.schema.json +128 -0
- package/session-distill/schemas/checks.schema.json +207 -0
- package/session-distill/schemas/claim.schema.json +16 -0
- package/session-distill/schemas/confirmation.schema.json +24 -0
- package/session-distill/schemas/coverage.schema.json +66 -0
- package/session-distill/schemas/cross_review.schema.json +24 -0
- package/session-distill/schemas/event.schema.json +20 -0
- package/session-distill/schemas/evidence.schema.json +20 -0
- package/session-distill/schemas/expected_observation.schema.json +18 -0
- package/session-distill/schemas/form.schema.json +103 -0
- package/session-distill/schemas/intake.schema.json +71 -0
- package/session-distill/schemas/inventory.schema.json +872 -0
- package/session-distill/schemas/judge.schema.json +77 -0
- package/session-distill/schemas/ledger.schema.json +313 -0
- package/session-distill/schemas/observation.schema.json +16 -0
- package/session-distill/schemas/propose.schema.json +555 -0
- package/session-distill/schemas/read.schema.json +76 -0
- package/session-distill/schemas/reconsider.schema.json +122 -0
- package/session-distill/schemas/run.schema.json +150 -0
- package/session-distill/schemas/s7b.schema.json +200 -0
- package/session-distill/schemas/s9b.schema.json +28 -0
- package/session-distill/schemas/stitch.schema.json +25 -0
- package/session-distill/schemas/sweep.schema.json +25 -0
- package/session-distill/seats.json +149 -0
- package/session-distill/tables/allowed_pairs.json +257 -0
- package/session-distill/tables/controls.json +30 -0
- package/session-distill/tables/delivery_evidence.json +36 -0
- package/session-distill/tables/delivery_evidence_fixtures.json +731 -0
- package/session-distill/tables/destinations.json +684 -0
- package/session-distill/tables/drift_layers.json +68 -0
- package/session-distill/tables/exposure_rules.json +192 -0
- package/session-distill/tables/placement_rules.json +148 -0
- package/session-distill/tables/propose_fixtures.json +154 -0
- package/session-distill/tables/ranking.json +194 -0
- package/session-distill/tables/redundancy.json +10 -0
- package/session-distill/tables/structural_rules.json +44 -0
- package/session-distill/tables/transition_fixtures.json +398 -0
- package/session-distill/tables/transitions.json +69 -0
- package/session-distill/tables/unit_outcome.json +67 -0
- package/session-distill/tables/unit_outcome_fixtures.json +2364 -0
- package/wrappers/claude-run.sh +481 -33
- package/wrappers/claude_hermetic_evidence.py +1423 -0
- package/wrappers/codex-helm.sh +38 -7
- package/wrappers/codex-run.sh +183 -20
- package/wrappers/codex_frame_filter.py +1199 -0
- package/wrappers/codex_protocol.py +737 -0
- package/wrappers/jsonl_lines.py +65 -0
|
@@ -0,0 +1,809 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""One identity contract for an operation's target (WP-21, review round 2, item C).
|
|
3
|
+
|
|
4
|
+
Four places judged what a target IS and judged it differently. `runlib/drift.py` refused
|
|
5
|
+
`./claude/CLAUDE.md` while `runlib/journal.py` accepted it, so one spelling was two answers
|
|
6
|
+
depending on which surface was asked. `claude/guides/to-root/CLAUDE.md` passed every lexical rule
|
|
7
|
+
there was, and with `to-root` a symlink to `claude/` that path REACHES the always-loaded global file
|
|
8
|
+
while being classified as a guide — the global-file judgment, the merge requirement and the ceiling
|
|
9
|
+
all belong to the layer the path was classified as, so a symlink moved an operation out from under
|
|
10
|
+
all three. And on a case-insensitive filesystem `claude/guides/G.md` and `claude/guides/g.md` are
|
|
11
|
+
one file and two identities: two operations, each with its own before-hash, neither seeing the
|
|
12
|
+
other's write.
|
|
13
|
+
|
|
14
|
+
So there is one owner for the question, and `drift`, `journal`, sequencing and application all ask
|
|
15
|
+
it here. The contract has three layers, and each says what it cannot establish rather than guessing:
|
|
16
|
+
|
|
17
|
+
* **lexical** — the spelling. A target is a relative path inside the checkout in canonical form.
|
|
18
|
+
Two spellings of one file are REFUSED, never normalized: the owner approved the text they read,
|
|
19
|
+
and an approval naming `claude/guides//g.md` must not apply to `claude/guides/g.md`.
|
|
20
|
+
* **filesystem** — the path as the kernel will walk it, from the DECLARED checkout root, one
|
|
21
|
+
component at a time with `O_NOFOLLOW`. A symlink anywhere in the chain is refused. A component
|
|
22
|
+
that does not exist yet is not a defect (a `create` names a path that should not be there), and
|
|
23
|
+
the walk stops there rather than claiming anything about what follows.
|
|
24
|
+
* **identity collision** — two targets of one approved set that name one file. Case is the live
|
|
25
|
+
case: whether two spellings collide depends on the filesystem, so the filesystem is asked, and
|
|
26
|
+
when it cannot be established the collision is refused rather than allowed.
|
|
27
|
+
|
|
28
|
+
`identity()` answers what the identity IS; the `*_problems` functions answer why a target may not be
|
|
29
|
+
applied. The hash chain, the layer map, the journal and the application all key on `identity()`, so
|
|
30
|
+
there is one string per file rather than one per spelling.
|
|
31
|
+
|
|
32
|
+
targets.py --self-test
|
|
33
|
+
"""
|
|
34
|
+
import errno
|
|
35
|
+
import os
|
|
36
|
+
import stat
|
|
37
|
+
import pathlib
|
|
38
|
+
import posixpath
|
|
39
|
+
import unicodedata
|
|
40
|
+
import sys
|
|
41
|
+
|
|
42
|
+
if __package__ in (None, ""): # run as a script: put session-distill/ on the import path
|
|
43
|
+
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
44
|
+
|
|
45
|
+
# Whether a root's filesystem folds case, by root. Probed READ-ONLY (see `folds_case`) and cached,
|
|
46
|
+
# because the answer cannot change under one run and the probe costs a directory listing.
|
|
47
|
+
_FOLDS_CASE = {}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def file_of(target):
|
|
51
|
+
"""The file a target names, as written: `path` of `path` or `path#heading`.
|
|
52
|
+
|
|
53
|
+
A target may name one section of a file (`runlib/operations.py`'s contract), and the SECTION is
|
|
54
|
+
not part of the file's identity. Readers that took the whole string as the path made
|
|
55
|
+
`claude/CLAUDE.md#A` a file of no layer - so an addition to the always-loaded file passed the
|
|
56
|
+
global rule by naming a heading - and made a section's deletion the file's (the S11 review,
|
|
57
|
+
2026-11-13). Every reader that needs the file asks here first."""
|
|
58
|
+
return str(target).partition("#")[0] if isinstance(target, str) else target
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def identity(target):
|
|
62
|
+
"""The identity of the file this target names, or None when the target is not a path.
|
|
63
|
+
|
|
64
|
+
Canonical, relative, POSIX. `drift.layer_of`, the hash chain, the journal and the application
|
|
65
|
+
all key on this, so one file has one identity however it was spelled — anything less makes two
|
|
66
|
+
spellings of one file into two files, and the chain keyed on the raw string let one file be
|
|
67
|
+
edited twice with neither operation seeing the other's after-hash.
|
|
68
|
+
"""
|
|
69
|
+
if not isinstance(target, str) or not target:
|
|
70
|
+
return None
|
|
71
|
+
# NFC, because a name is bytes on disk and a CHARACTER SEQUENCE here. `é` composed and `é`
|
|
72
|
+
# decomposed are the same file on macOS and were two identities: the chain, the journal and the
|
|
73
|
+
# layer map keyed on different strings for one file, and the collision check grouped them apart
|
|
74
|
+
# (the round-4 review). The policy is NFC everywhere identity is taken, and the spelling on disk
|
|
75
|
+
# is compared as the directory entry gives it — see `_spelling_on_disk`.
|
|
76
|
+
return unicodedata.normalize("NFC", posixpath.normpath(target))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
CONTROL_CATEGORIES = ("Cc", "Cf", "Zl", "Zp")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def control_characters(text):
|
|
83
|
+
"""The control, format and line/paragraph-separator characters in `text`, as `U+XXXX`, sorted."""
|
|
84
|
+
return sorted({f"U+{ord(ch):04X}" for ch in str(text)
|
|
85
|
+
if unicodedata.category(ch) in CONTROL_CATEGORIES})
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def lexical_problems(target):
|
|
89
|
+
"""Why this target's SPELLING may not be applied, by name. Empty means it is canonical."""
|
|
90
|
+
if not isinstance(target, str) or not target:
|
|
91
|
+
return [f"TARGET_ABSENT {target!r}: an operation names a path"]
|
|
92
|
+
problems = []
|
|
93
|
+
held_controls = control_characters(target)
|
|
94
|
+
if held_controls:
|
|
95
|
+
# Before any other rule and before any filesystem call: a NUL reached `os.open` and came
|
|
96
|
+
# back as a ValueError rather than a named problem, and a newline in a target is a second
|
|
97
|
+
# line in every record that carries it (the round-6 review). The journal holds its records
|
|
98
|
+
# to this same function.
|
|
99
|
+
return [f"TARGET_CONTROL_CHARACTER {target!r}: it carries {', '.join(held_controls)}, and "
|
|
100
|
+
f"a path is written into records a line at a time"]
|
|
101
|
+
if target.startswith("/") or target.startswith("~"):
|
|
102
|
+
problems.append(f"TARGET_ABSOLUTE {target!r}: a target is a path inside the checkout")
|
|
103
|
+
parts = target.split("/")
|
|
104
|
+
if ".." in parts:
|
|
105
|
+
problems.append(f"TARGET_TRAVERSAL {target!r}: a `..` segment leaves the checkout")
|
|
106
|
+
if "." in parts or "" in parts[1:] or target.endswith("/"):
|
|
107
|
+
problems.append(f"TARGET_SPELLING {target!r}: an empty or `.` component makes two spellings "
|
|
108
|
+
f"of one file, and the chain, the layer and the journal would key on "
|
|
109
|
+
f"different ones")
|
|
110
|
+
if target != posixpath.normpath(target):
|
|
111
|
+
problems.append(f"TARGET_UNCANONICAL {target!r}: its canonical form is "
|
|
112
|
+
f"{posixpath.normpath(target)!r}")
|
|
113
|
+
return problems
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def folds_case(root):
|
|
117
|
+
"""Whether this root's filesystem treats two cases of one name as one file, or None if unknown.
|
|
118
|
+
|
|
119
|
+
Probed WITHOUT WRITING: an existing entry's name is stat'd in a flipped case and compared by
|
|
120
|
+
inode. The checkout is the owner's working tree, and a containment contract that creates a probe
|
|
121
|
+
file in it to decide a question about it has already failed at its own job.
|
|
122
|
+
|
|
123
|
+
None means the filesystem did not say — an empty directory, or no entry with a letter in its
|
|
124
|
+
name. Callers refuse a case collision under None rather than allowing it: 'nobody knows whether
|
|
125
|
+
these two names are one file' is not a licence to apply both.
|
|
126
|
+
"""
|
|
127
|
+
base = pathlib.Path(root)
|
|
128
|
+
key = str(base)
|
|
129
|
+
if key in _FOLDS_CASE:
|
|
130
|
+
return _FOLDS_CASE[key]
|
|
131
|
+
# Three answers, not two: True folds, False does not, and None means THIS PROBE COULD NOT TELL.
|
|
132
|
+
# An `OSError` from the flipped stat used to be read as False — "case-sensitive" — so a
|
|
133
|
+
# directory the run may list but not stat answered "sensitive" and `CASE_ALIAS` stopped being
|
|
134
|
+
# reported. Only ENOENT answers the question (the flipped name is absent, so names are
|
|
135
|
+
# distinct); every other error is an unanswered probe, and an unanswered probe refuses rather
|
|
136
|
+
# than assumes (the round-3 review).
|
|
137
|
+
answer = None
|
|
138
|
+
try:
|
|
139
|
+
with os.scandir(base) as entries:
|
|
140
|
+
for entry in entries:
|
|
141
|
+
flipped = entry.name.swapcase()
|
|
142
|
+
if flipped == entry.name: # no letters to flip
|
|
143
|
+
continue
|
|
144
|
+
try:
|
|
145
|
+
answer = os.stat(base / flipped, follow_symlinks=False).st_ino == \
|
|
146
|
+
os.stat(base / entry.name, follow_symlinks=False).st_ino
|
|
147
|
+
except FileNotFoundError:
|
|
148
|
+
answer = False # the flipped name is not there: distinct names
|
|
149
|
+
except OSError:
|
|
150
|
+
answer = None # cannot tell, which is not "sensitive"
|
|
151
|
+
break
|
|
152
|
+
except OSError:
|
|
153
|
+
answer = None
|
|
154
|
+
_FOLDS_CASE[key] = answer
|
|
155
|
+
return answer
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def filesystem_problems(target, checkout):
|
|
159
|
+
"""Why the path the KERNEL will walk is not the path the target names, by name.
|
|
160
|
+
|
|
161
|
+
The walk starts at the declared checkout root and opens one component at a time with
|
|
162
|
+
`O_NOFOLLOW`, so a symlink anywhere in the chain is refused rather than followed. `O_NOFOLLOW`
|
|
163
|
+
on the leaf alone protects nothing: `claude/guides/to-root/CLAUDE.md` has its symlink in the
|
|
164
|
+
middle, and that is the case that reached the global file while being judged a guide.
|
|
165
|
+
"""
|
|
166
|
+
held = lexical_problems(target)
|
|
167
|
+
if held:
|
|
168
|
+
return held # an unspellable target has no path to walk
|
|
169
|
+
problems = []
|
|
170
|
+
parts = identity(target).split("/")
|
|
171
|
+
try:
|
|
172
|
+
root_fd = os.open(checkout, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
|
173
|
+
except OSError as exc:
|
|
174
|
+
return [f"CHECKOUT_UNOPENABLE {checkout}: {exc.strerror} — the declared checkout root must "
|
|
175
|
+
f"be a real directory that is not itself a symlink, or no target under it can be "
|
|
176
|
+
f"judged"]
|
|
177
|
+
open_fds = [root_fd]
|
|
178
|
+
try:
|
|
179
|
+
for index, part in enumerate(parts):
|
|
180
|
+
last = index == len(parts) - 1
|
|
181
|
+
# the leaf NONBLOCKING: a FIFO with no writer blocks in `open` itself, before any check of
|
|
182
|
+
# what it is could run, and `O_NOFOLLOW` does nothing about that (the S11 review)
|
|
183
|
+
flags = os.O_RDONLY | os.O_NOFOLLOW | (os.O_NONBLOCK if last else os.O_DIRECTORY)
|
|
184
|
+
try:
|
|
185
|
+
held_fd = os.open(part, flags, dir_fd=open_fds[-1])
|
|
186
|
+
except OSError as exc:
|
|
187
|
+
reached = "/".join(parts[:index + 1])
|
|
188
|
+
# WHAT the component is decides, not which errno the platform chose to report.
|
|
189
|
+
# `O_DIRECTORY|O_NOFOLLOW` over a symlink to a directory raises ELOOP on Linux and
|
|
190
|
+
# ENOTDIR on macOS (measured on darwin 25.5), so a rule written on either errno
|
|
191
|
+
# alone reads a symlink as an ordinary non-directory on the other platform — and
|
|
192
|
+
# this walk is the containment, so it may not depend on that.
|
|
193
|
+
linked = False
|
|
194
|
+
try:
|
|
195
|
+
linked = stat.S_ISLNK(os.lstat(part, dir_fd=open_fds[-1]).st_mode)
|
|
196
|
+
except OSError:
|
|
197
|
+
linked = False
|
|
198
|
+
if linked or exc.errno in (errno.ELOOP, errno.EMLINK):
|
|
199
|
+
problems.append(f"TARGET_SYMLINK {target!r}: {reached} is a symbolic link, so "
|
|
200
|
+
f"the path the kernel walks is not the path this target names "
|
|
201
|
+
f"and the layer it was classified as is not the layer it "
|
|
202
|
+
f"reaches")
|
|
203
|
+
elif exc.errno == errno.ENOENT:
|
|
204
|
+
# Nothing there yet. A `create` names exactly that, and what lies beyond a
|
|
205
|
+
# component that does not exist is not a question this walk can answer.
|
|
206
|
+
pass
|
|
207
|
+
elif exc.errno == errno.ENOTDIR:
|
|
208
|
+
problems.append(f"TARGET_NOT_A_DIRECTORY {target!r}: {reached} is not a "
|
|
209
|
+
f"directory, so the components after it name nothing")
|
|
210
|
+
else:
|
|
211
|
+
problems.append(f"TARGET_UNWALKABLE {target!r}: {reached} could not be opened "
|
|
212
|
+
f"({exc.strerror}), so containment cannot be established")
|
|
213
|
+
break
|
|
214
|
+
open_fds.append(held_fd)
|
|
215
|
+
if last:
|
|
216
|
+
# A second NAME for the leaf's bytes is a second path to them, and no walk over this
|
|
217
|
+
# name can see where the other one lies: a file outside the checkout hard-linked to
|
|
218
|
+
# an approved name inside it is written and truncated through that name (the round-5
|
|
219
|
+
# review). The descriptor is asked, not the path, so the answer is about the file
|
|
220
|
+
# the walk reached.
|
|
221
|
+
held_stat = os.fstat(held_fd)
|
|
222
|
+
if not (stat.S_ISREG(held_stat.st_mode) or stat.S_ISDIR(held_stat.st_mode)):
|
|
223
|
+
problems.append(f"TARGET_NOT_FILE {target!r}: it is neither a regular file nor a "
|
|
224
|
+
f"directory (a FIFO, a socket or a device), and an operation "
|
|
225
|
+
f"reads and writes a file")
|
|
226
|
+
if stat.S_ISREG(held_stat.st_mode) and held_stat.st_nlink != 1:
|
|
227
|
+
problems.append(f"TARGET_HARD_LINKED {target!r}: the file has "
|
|
228
|
+
f"{held_stat.st_nlink} names, so writing it through this one "
|
|
229
|
+
f"changes a file this walk cannot place inside the checkout")
|
|
230
|
+
# EVERY component that exists, each through its own parent descriptor. Comparing only
|
|
231
|
+
# the last one let `CLAUDE/CLAUDE.md` pass on a case-folding filesystem: the directory
|
|
232
|
+
# opened happily under a spelling nobody approved, and the leaf matched (the round-3
|
|
233
|
+
# review). The spelling of a directory is as much a name as the file's.
|
|
234
|
+
problems += _spelling_on_disk(parts[:index + 1], open_fds[-2], target)
|
|
235
|
+
finally:
|
|
236
|
+
for held_fd in open_fds:
|
|
237
|
+
try:
|
|
238
|
+
os.close(held_fd)
|
|
239
|
+
except OSError:
|
|
240
|
+
pass
|
|
241
|
+
return problems
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _spelling_on_disk(parts, parent_fd, target):
|
|
245
|
+
"""Whether the entry's own spelling is the one the target used.
|
|
246
|
+
|
|
247
|
+
On a case-folding filesystem `os.open` of `G.md` happily opens `g.md`, so the walk succeeding
|
|
248
|
+
says nothing about which name the owner approved. The approved spelling must be the spelling on
|
|
249
|
+
disk: otherwise the journal, the chain and the file itself carry two different names for one
|
|
250
|
+
thing.
|
|
251
|
+
"""
|
|
252
|
+
name = parts[-1]
|
|
253
|
+
try:
|
|
254
|
+
held = os.listdir(parent_fd)
|
|
255
|
+
except OSError as exc:
|
|
256
|
+
# An unreadable parent is not an approved spelling. It used to return `[]`, which reads as
|
|
257
|
+
# "the name on disk is the name the owner approved" from a directory nobody could list.
|
|
258
|
+
return [f"TARGET_SPELLING_UNREADABLE {target!r}: {'/'.join(parts)}'s parent could not be "
|
|
259
|
+
f"listed ({exc.strerror}), so whether the name on disk is the approved one cannot "
|
|
260
|
+
f"be established"]
|
|
261
|
+
if name in held:
|
|
262
|
+
return []
|
|
263
|
+
# Canonical equivalence is ONE NAME, not a different one: `é` composed and `é` decomposed are
|
|
264
|
+
# the same characters and the same file, which is why `identity` normalises to NFC. An entry
|
|
265
|
+
# that is canonically equal to the approved name is therefore the approved name, and `in` is
|
|
266
|
+
# the only test that could not see it (the round-4 review). A CASE difference is a different
|
|
267
|
+
# name and stays reported below.
|
|
268
|
+
if any(unicodedata.normalize("NFC", entry) == unicodedata.normalize("NFC", name)
|
|
269
|
+
for entry in held):
|
|
270
|
+
return []
|
|
271
|
+
folded = [entry for entry in held if entry.casefold() == name.casefold()
|
|
272
|
+
or unicodedata.normalize("NFC", entry).casefold()
|
|
273
|
+
== unicodedata.normalize("NFC", name).casefold()]
|
|
274
|
+
if folded:
|
|
275
|
+
return [f"TARGET_CASE_ON_DISK {target!r}: the file is spelled {folded[0]!r} on disk, and an "
|
|
276
|
+
f"operation that names it otherwise writes under a name the owner did not approve"]
|
|
277
|
+
# No entry matches, which is the ordinary case for a `create`: the walk runs over the components
|
|
278
|
+
# that EXIST, and a leaf that does not exist yet has no entry to be spelled differently from. A
|
|
279
|
+
# refusal here would refuse every creation.
|
|
280
|
+
return []
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def collision_problems(targets, checkout=None):
|
|
284
|
+
"""Two targets of one set that name ONE file, by name.
|
|
285
|
+
|
|
286
|
+
Identity collisions are refused for the set, not for one target: whether two spellings are one
|
|
287
|
+
file is a property of the pair. An exact repeat is one file by any rule; a case difference is
|
|
288
|
+
one file only where the filesystem folds case, so the filesystem is asked — and when it will
|
|
289
|
+
not say, the pair is refused rather than applied, because the cost of being wrong is two
|
|
290
|
+
operations writing one file with neither seeing the other's hash.
|
|
291
|
+
"""
|
|
292
|
+
held = [identity(target) for target in targets or () if identity(target)]
|
|
293
|
+
problems = []
|
|
294
|
+
# A REPEATED identity is not a collision. §10.5 sequences a chain through one file — `a` creates
|
|
295
|
+
# it, `b` deletes it, `c` routes to it — and each operation carries the before-state hash of
|
|
296
|
+
# what the previous one left, which is exactly how the chain governs the repeat. Refusing it
|
|
297
|
+
# rejected valid approved sets (the round-3 review: z-first a->b, a-second b->c). What IS a
|
|
298
|
+
# collision is two DISTINCT SPELLINGS that name one file, because then neither operation's
|
|
299
|
+
# before-hash describes the state the other overwrote, and nothing orders them.
|
|
300
|
+
folds = folds_case(checkout) if checkout else None
|
|
301
|
+
groups = {}
|
|
302
|
+
for one in held:
|
|
303
|
+
groups.setdefault(one.casefold(), set()).add(one)
|
|
304
|
+
for folded, spellings in sorted(groups.items()):
|
|
305
|
+
if len(spellings) < 2:
|
|
306
|
+
continue
|
|
307
|
+
if folds is True:
|
|
308
|
+
problems.append(f"TARGET_CASE_ALIAS {', '.join(sorted(spellings))}: this filesystem "
|
|
309
|
+
f"folds case, so these are one file under two identities and each "
|
|
310
|
+
f"operation's before-hash describes a state the other overwrote")
|
|
311
|
+
elif folds is None:
|
|
312
|
+
problems.append(f"TARGET_CASE_UNDECIDED {', '.join(sorted(spellings))}: whether this "
|
|
313
|
+
f"filesystem folds case could not be established, so whether these are "
|
|
314
|
+
f"one file or two is unknown and the set is refused rather than applied")
|
|
315
|
+
return problems
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def problems(target, checkout=None):
|
|
319
|
+
"""Every reason this target may not be applied: the spelling, and the path the kernel walks.
|
|
320
|
+
|
|
321
|
+
With no checkout the answer is the spelling alone, and the caller is told so rather than being
|
|
322
|
+
left to read silence as containment.
|
|
323
|
+
"""
|
|
324
|
+
if checkout is None:
|
|
325
|
+
return lexical_problems(target)
|
|
326
|
+
return filesystem_problems(target, checkout)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def set_problems(targets, checkout=None):
|
|
330
|
+
"""Every target of a set judged, plus the collisions between them."""
|
|
331
|
+
held = []
|
|
332
|
+
for target in targets or ():
|
|
333
|
+
held += problems(target, checkout)
|
|
334
|
+
return held + collision_problems(targets, checkout)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
SPELLING_TESTS = (
|
|
338
|
+
('".." in', "a traversal test"),
|
|
339
|
+
('startswith("/")', "an absolute-path test"),
|
|
340
|
+
("startswith('/')", "an absolute-path test"),
|
|
341
|
+
('startswith("~")', "a home-prefix test"),
|
|
342
|
+
("posixpath.normpath(", "a canonicalisation"),
|
|
343
|
+
("os.path.normpath(", "a canonicalisation"),
|
|
344
|
+
)
|
|
345
|
+
SPELLING_SUBJECTS = ("target", "carrier")
|
|
346
|
+
# Keyed to the CODE, not to a line number, so it lapses when that code changes rather than when the
|
|
347
|
+
# file moves. Each entry says why its subject is not an approved target's spelling.
|
|
348
|
+
SPELLING_EXEMPT = {
|
|
349
|
+
("inventory.py", "target = pathlib.Path(os.path.normpath(str(base / label)))"):
|
|
350
|
+
"S2's copy-label containment (`inventory.confined`): the subject is a label out of the "
|
|
351
|
+
"owner's state being joined to a fixture base, and the question is whether the result stays "
|
|
352
|
+
"under that base — containment, not whether an approved target is spelled canonically. The "
|
|
353
|
+
"line itself is the second layer behind a string test and is unguarded-accepted "
|
|
354
|
+
"(ruled 2026-10-24).",
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def second_validator_problems(root=None):
|
|
359
|
+
"""Any module of `runlib` other than this one that tests a TARGET's spelling itself.
|
|
360
|
+
|
|
361
|
+
The contract has one owner, and the way that stops being true is quiet: `placement` had its own
|
|
362
|
+
carrier rules and accepted `./claude/CLAUDE.md` and `claude//CLAUDE.md` while this module
|
|
363
|
+
refused them, so one spelling was approved in the review and refused at the apply (the round-3
|
|
364
|
+
review). Decidable by source: a spelling TEST applied to a value named like a target or a
|
|
365
|
+
carrier, in any runlib module but this one.
|
|
366
|
+
|
|
367
|
+
Containment is NOT this contract — `destinations._inside` asks whether a path lies under a root,
|
|
368
|
+
which is a different question about a value that is already a valid path — so the scan is scoped
|
|
369
|
+
to the subject names, not to the predicates alone.
|
|
370
|
+
"""
|
|
371
|
+
base = pathlib.Path(root or pathlib.Path(__file__).resolve().parent)
|
|
372
|
+
problems, scanned = [], 0
|
|
373
|
+
for held in sorted(base.glob("*.py")):
|
|
374
|
+
if held.name == pathlib.Path(__file__).name:
|
|
375
|
+
continue
|
|
376
|
+
scanned += 1
|
|
377
|
+
for number, line in enumerate(held.read_text(encoding="utf-8",
|
|
378
|
+
errors="replace").splitlines(), 1):
|
|
379
|
+
stripped = line.strip()
|
|
380
|
+
if stripped.startswith(("#", '"', "'")):
|
|
381
|
+
continue
|
|
382
|
+
lowered = line.lower()
|
|
383
|
+
if not any(subject in lowered for subject in SPELLING_SUBJECTS):
|
|
384
|
+
continue
|
|
385
|
+
if (held.name, stripped) in SPELLING_EXEMPT:
|
|
386
|
+
continue
|
|
387
|
+
for mark, why in SPELLING_TESTS:
|
|
388
|
+
if mark in line:
|
|
389
|
+
problems.append(f"SECOND_TARGET_VALIDATOR {held.name}:{number}: {why} on a "
|
|
390
|
+
f"target or carrier ({stripped[:70]!r}) — the spelling contract "
|
|
391
|
+
f"has one owner, and a second one disagrees with it in some "
|
|
392
|
+
f"case nobody has found yet")
|
|
393
|
+
break
|
|
394
|
+
if not scanned:
|
|
395
|
+
problems.append("SECOND_TARGET_VALIDATOR_NO_MODULES: no runlib module was read, so the "
|
|
396
|
+
"scan would pass over an empty set")
|
|
397
|
+
# The exemptions are declared about THIS runlib's files, so their staleness is asked only when
|
|
398
|
+
# that is what is being scanned; a control scanning its own planted tree is not evidence that
|
|
399
|
+
# the licence here has lapsed.
|
|
400
|
+
own = base == pathlib.Path(__file__).resolve().parent
|
|
401
|
+
for (name, code), reason in sorted(SPELLING_EXEMPT.items()) if own else ():
|
|
402
|
+
held_file = base / name
|
|
403
|
+
if not held_file.is_file() or code not in held_file.read_text(encoding="utf-8",
|
|
404
|
+
errors="replace"):
|
|
405
|
+
problems.append(f"SECOND_TARGET_VALIDATOR_EXEMPTION_STALE {name}: the exemption names "
|
|
406
|
+
f"code this file no longer carries ({code[:50]!r}), so it excuses "
|
|
407
|
+
f"nothing and the reason it carried is no longer about anything")
|
|
408
|
+
return problems
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def _with_exemption(key, reason, call):
|
|
412
|
+
"""Run `call` with one more entry in SPELLING_EXEMPT, restoring it however the call ends."""
|
|
413
|
+
held = dict(SPELLING_EXEMPT)
|
|
414
|
+
try:
|
|
415
|
+
SPELLING_EXEMPT[key] = reason
|
|
416
|
+
return call()
|
|
417
|
+
finally:
|
|
418
|
+
SPELLING_EXEMPT.clear()
|
|
419
|
+
SPELLING_EXEMPT.update(held)
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def self_test():
|
|
423
|
+
"""One identity contract, each rule with a planted violation and its clean counterpart.
|
|
424
|
+
|
|
425
|
+
The body is wrapped so a control that DIES still speaks: a traceback is not a control firing,
|
|
426
|
+
and a revert trial that reads any output counts one as if it were.
|
|
427
|
+
"""
|
|
428
|
+
failures, ran = [], []
|
|
429
|
+
try:
|
|
430
|
+
return _self_test_body(failures, ran)
|
|
431
|
+
except BaseException as exc: # noqa: BLE001 - the suite must speak before it dies
|
|
432
|
+
held = ran[-1] if ran else "<no control ran>"
|
|
433
|
+
print(f"targets --self-test [FAIL] the suite died after {held!r}: "
|
|
434
|
+
f"{type(exc).__name__}: {exc}", file=sys.stderr)
|
|
435
|
+
for name, detail in failures:
|
|
436
|
+
print(f"targets --self-test [FAIL] {name}: {detail}", file=sys.stderr)
|
|
437
|
+
print(f"targets --self-test: INCOMPLETE ({len(ran)} controls ran, {len(failures)} failed "
|
|
438
|
+
f"before the suite died)", file=sys.stderr)
|
|
439
|
+
raise
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def _self_test_body(failures, ran):
|
|
443
|
+
# Imported here, before anything uses them: a later import inside this function makes the name
|
|
444
|
+
# a local for the WHOLE function. The module itself touches no filesystem of its own.
|
|
445
|
+
import json
|
|
446
|
+
import shutil
|
|
447
|
+
import tempfile
|
|
448
|
+
|
|
449
|
+
import shutil
|
|
450
|
+
import tempfile
|
|
451
|
+
|
|
452
|
+
def control(name, got, want):
|
|
453
|
+
ran.append(name)
|
|
454
|
+
if want not in str(got):
|
|
455
|
+
failures.append((name, f"expected {want!r} in {str(got)[:400]!r}"))
|
|
456
|
+
|
|
457
|
+
def clean(name, held):
|
|
458
|
+
ran.append(name)
|
|
459
|
+
if held:
|
|
460
|
+
failures.append((name, f"expected nothing, got {held}"))
|
|
461
|
+
|
|
462
|
+
def guarded(call):
|
|
463
|
+
try:
|
|
464
|
+
return call()
|
|
465
|
+
except BaseException as exc: # noqa: BLE001 - the exception is the answer here
|
|
466
|
+
return f"RAISED {type(exc).__name__}: {exc}"
|
|
467
|
+
|
|
468
|
+
for label, spelled in (("a newline", "claude/guides/g.md\nowner approved"),
|
|
469
|
+
("a NUL", "claude/guides/g\x00.md"),
|
|
470
|
+
("a zero-width joiner", "claude/guides/g\u200d.md")):
|
|
471
|
+
control(f"{label} in a target is one named problem, before any other rule",
|
|
472
|
+
[p.split()[0] for p in lexical_problems(spelled)], "['TARGET_CONTROL_CHARACTER']")
|
|
473
|
+
control("and a NUL never reaches the filesystem walk as an exception",
|
|
474
|
+
[p.split()[0] for p in guarded(lambda: filesystem_problems("claude/g\x00.md", "/tmp")) or ["<none>"]]
|
|
475
|
+
if isinstance(guarded(lambda: filesystem_problems("claude/g\x00.md", "/tmp")), list)
|
|
476
|
+
else guarded(lambda: filesystem_problems("claude/g\x00.md", "/tmp")),
|
|
477
|
+
"['TARGET_CONTROL_CHARACTER']")
|
|
478
|
+
# ---- the identity ----------------------------------------------------------------------------
|
|
479
|
+
control("one file has one identity however it was spelled",
|
|
480
|
+
identity("claude/guides//g.md"), "claude/guides/g.md")
|
|
481
|
+
control("and a leading `./` is the same file", identity("./claude/CLAUDE.md"),
|
|
482
|
+
"claude/CLAUDE.md")
|
|
483
|
+
control("a target that is not a path has no identity", str(identity(None)), "None")
|
|
484
|
+
|
|
485
|
+
# ---- the spelling ----------------------------------------------------------------------------
|
|
486
|
+
clean("a canonical relative path is spelled correctly",
|
|
487
|
+
lexical_problems("claude/guides/g.md"))
|
|
488
|
+
for bad, want in (("/etc/passwd", "TARGET_ABSOLUTE"), ("~/x", "TARGET_ABSOLUTE"),
|
|
489
|
+
("a/../../x", "TARGET_TRAVERSAL"), ("a/./x", "TARGET_SPELLING"),
|
|
490
|
+
("claude/guides//g.md", "TARGET_SPELLING"),
|
|
491
|
+
("claude/guides/", "TARGET_SPELLING"), ("", "TARGET_ABSENT"),
|
|
492
|
+
(None, "TARGET_ABSENT")):
|
|
493
|
+
control(f"a target spelled {bad!r} is refused by name",
|
|
494
|
+
"; ".join(lexical_problems(bad)), want)
|
|
495
|
+
# The two surfaces disagreed on exactly this one: drift refused it and the journal took it.
|
|
496
|
+
control("`./claude/CLAUDE.md` is refused, and by the ONE contract both surfaces ask",
|
|
497
|
+
"; ".join(lexical_problems("./claude/CLAUDE.md")), "TARGET_SPELLING")
|
|
498
|
+
|
|
499
|
+
root = pathlib.Path(tempfile.mkdtemp(prefix="targets-", dir="/tmp"))
|
|
500
|
+
try:
|
|
501
|
+
# ---- the path the kernel walks -----------------------------------------------------------
|
|
502
|
+
(root / "claude" / "guides").mkdir(parents=True)
|
|
503
|
+
(root / "claude" / "CLAUDE.md").write_text("global\n", encoding="utf-8")
|
|
504
|
+
(root / "claude" / "guides" / "g.md").write_text("guide\n", encoding="utf-8")
|
|
505
|
+
clean("a real path inside the checkout walks clean",
|
|
506
|
+
filesystem_problems("claude/guides/g.md", root))
|
|
507
|
+
clean("and so does a path that does not exist yet, which is what a `create` names",
|
|
508
|
+
filesystem_problems("claude/guides/new.md", root))
|
|
509
|
+
# The review's case: a symlinked DIRECTORY in the middle, reaching the global file while the
|
|
510
|
+
# path says guides.
|
|
511
|
+
os.symlink(root / "claude", root / "claude" / "guides" / "to-root")
|
|
512
|
+
held_link = filesystem_problems("claude/guides/to-root/CLAUDE.md", root)
|
|
513
|
+
control("a symlinked component in the MIDDLE is refused, which `O_NOFOLLOW` on the leaf "
|
|
514
|
+
"alone does not catch", "; ".join(held_link), "TARGET_SYMLINK")
|
|
515
|
+
control("and the refusal names the component that is the link",
|
|
516
|
+
"; ".join(held_link), "claude/guides/to-root is a symbolic link")
|
|
517
|
+
control("and says what it costs: the layer it reaches is not the layer it was classified as",
|
|
518
|
+
"; ".join(held_link), "not the layer it")
|
|
519
|
+
os.symlink(root / "claude" / "CLAUDE.md", root / "claude" / "guides" / "leaf.md")
|
|
520
|
+
control("a symlinked LEAF is refused too",
|
|
521
|
+
"; ".join(filesystem_problems("claude/guides/leaf.md", root)), "TARGET_SYMLINK")
|
|
522
|
+
control("a component that is a file where a directory was named is refused by its own name",
|
|
523
|
+
"; ".join(filesystem_problems("claude/CLAUDE.md/x.md", root)),
|
|
524
|
+
"TARGET_NOT_A_DIRECTORY")
|
|
525
|
+
control("a checkout root that is itself a symlink is refused, since nothing under it can "
|
|
526
|
+
"be judged",
|
|
527
|
+
"; ".join(filesystem_problems("claude/guides/g.md",
|
|
528
|
+
guarded(lambda: _linked_root(root)))),
|
|
529
|
+
"CHECKOUT_UNOPENABLE")
|
|
530
|
+
control("and an unspellable target is refused before any walk",
|
|
531
|
+
"; ".join(filesystem_problems("../x", root)), "TARGET_TRAVERSAL")
|
|
532
|
+
|
|
533
|
+
# ---- case -------------------------------------------------------------------------------
|
|
534
|
+
folds = folds_case(root)
|
|
535
|
+
control("the filesystem's case behaviour is established by reading, never by writing a "
|
|
536
|
+
"probe into the checkout",
|
|
537
|
+
"answered" if folds in (True, False) else f"unknown ({folds})", "answered")
|
|
538
|
+
clean("two files that differ by more than case are two files",
|
|
539
|
+
collision_problems(["claude/guides/g.md", "claude/guides/h.md"], root))
|
|
540
|
+
# A repeat is a CHAIN, not a collision: §10.5 sequences several operations through one file
|
|
541
|
+
# and each carries the before-state hash of what the previous one left. Refusing it rejected
|
|
542
|
+
# valid approved sets, so the set is clean and the hash chain governs the order.
|
|
543
|
+
clean("two spellings of ONE identity in a set are a chain, not a collision: the hash chain "
|
|
544
|
+
"is what orders them",
|
|
545
|
+
collision_problems(["claude/guides/g.md", "claude/guides//g.md"], root))
|
|
546
|
+
control("while two DISTINCT spellings of one file stay a collision, because then neither "
|
|
547
|
+
"operation's before-hash describes what the other overwrote",
|
|
548
|
+
"; ".join(collision_problems(["claude/guides/G.md", "claude/guides/g.md"], root))
|
|
549
|
+
or "no collision reported",
|
|
550
|
+
"TARGET_CASE" if folds_case(root) is not False else "no collision reported")
|
|
551
|
+
held_case = collision_problems(["claude/guides/G.md", "claude/guides/g.md"], root)
|
|
552
|
+
if folds:
|
|
553
|
+
control("on a case-folding filesystem two cases of one name are refused as one file",
|
|
554
|
+
"; ".join(held_case), "TARGET_CASE_ALIAS")
|
|
555
|
+
else:
|
|
556
|
+
clean("on a case-sensitive filesystem they are genuinely two files", held_case)
|
|
557
|
+
# Whatever this machine does, both answers are exercised: the probe's answer is forced.
|
|
558
|
+
_FOLDS_CASE[str(root)] = True
|
|
559
|
+
control("with the filesystem folding case, the pair is refused",
|
|
560
|
+
"; ".join(collision_problems(["claude/guides/G.md", "claude/guides/g.md"], root)),
|
|
561
|
+
"TARGET_CASE_ALIAS")
|
|
562
|
+
_FOLDS_CASE[str(root)] = False
|
|
563
|
+
clean("with it case-sensitive, the same pair is two files",
|
|
564
|
+
collision_problems(["claude/guides/G.md", "claude/guides/g.md"], root))
|
|
565
|
+
_FOLDS_CASE[str(root)] = None
|
|
566
|
+
control("and when it will not say, the pair is REFUSED rather than applied: unknown is not "
|
|
567
|
+
"a licence",
|
|
568
|
+
"; ".join(collision_problems(["claude/guides/G.md", "claude/guides/g.md"], root)),
|
|
569
|
+
"TARGET_CASE_UNDECIDED")
|
|
570
|
+
del _FOLDS_CASE[str(root)]
|
|
571
|
+
# The PROBE itself, over trees this control builds. Its three answers are not two: an
|
|
572
|
+
# OSError from the flipped stat used to read as "case-sensitive", so a directory the run may
|
|
573
|
+
# list and not stat answered the question with a guess, and CASE_ALIAS stopped being
|
|
574
|
+
# reported for every pair under it.
|
|
575
|
+
probe_root = pathlib.Path(tempfile.mkdtemp(prefix="targets-probe-", dir="/tmp"))
|
|
576
|
+
try:
|
|
577
|
+
(probe_root / "letters").mkdir()
|
|
578
|
+
(probe_root / "letters" / "Aa.md").write_text("x", encoding="utf-8")
|
|
579
|
+
_FOLDS_CASE.pop(str(probe_root / "letters"), None)
|
|
580
|
+
held_answer = folds_case(probe_root / "letters")
|
|
581
|
+
control("the probe answers from the filesystem it is given, True or False, never from "
|
|
582
|
+
"a guess", json.dumps(held_answer in (True, False)), "true")
|
|
583
|
+
_FOLDS_CASE.pop(str(probe_root / "letters"), None)
|
|
584
|
+
unreadable = probe_root / "closed"
|
|
585
|
+
unreadable.mkdir()
|
|
586
|
+
(unreadable / "Bb.md").write_text("x", encoding="utf-8")
|
|
587
|
+
held_listing = os.listdir(unreadable)
|
|
588
|
+
control("and the directory this control closed is readable before it is closed, so the "
|
|
589
|
+
"next answer is about the closing and not about an empty directory",
|
|
590
|
+
json.dumps(held_listing), '["Bb.md"]')
|
|
591
|
+
os.chmod(unreadable, 0o000)
|
|
592
|
+
try:
|
|
593
|
+
_FOLDS_CASE.pop(str(unreadable), None)
|
|
594
|
+
control("a probe that cannot read the directory answers None — cannot tell — and "
|
|
595
|
+
"not False, which would have meant case-sensitive",
|
|
596
|
+
json.dumps(folds_case(unreadable)), "null")
|
|
597
|
+
_FOLDS_CASE.pop(str(unreadable), None)
|
|
598
|
+
control("and a set judged against that checkout is REFUSED by name rather than "
|
|
599
|
+
"applied",
|
|
600
|
+
"; ".join(collision_problems(["claude/guides/G.md", "claude/guides/g.md"],
|
|
601
|
+
unreadable)),
|
|
602
|
+
"TARGET_CASE_UNDECIDED")
|
|
603
|
+
finally:
|
|
604
|
+
os.chmod(unreadable, 0o700)
|
|
605
|
+
_FOLDS_CASE.pop(str(unreadable), None)
|
|
606
|
+
# Only ENOENT answers "distinct names": the flipped name is absent, so the two names are
|
|
607
|
+
# not one file. Asked of the probe over a tree with exactly one entry.
|
|
608
|
+
(probe_root / "single").mkdir()
|
|
609
|
+
(probe_root / "single" / "Zz.md").write_text("x", encoding="utf-8")
|
|
610
|
+
_FOLDS_CASE.pop(str(probe_root / "single"), None)
|
|
611
|
+
control("an absent flipped name is the one error that answers the question",
|
|
612
|
+
json.dumps(folds_case(probe_root / "single") in (True, False)), "true")
|
|
613
|
+
# EVERY existing component's spelling, not the leaf's alone: a directory opened under a
|
|
614
|
+
# spelling nobody approved is as wrong as a file, and on this machine the open succeeds.
|
|
615
|
+
(probe_root / "CLAUDE" / "guides").mkdir(parents=True)
|
|
616
|
+
(probe_root / "CLAUDE" / "guides" / "g.md").write_text("x", encoding="utf-8")
|
|
617
|
+
control("a target whose DIRECTORY is spelled otherwise on disk is refused, which "
|
|
618
|
+
"comparing the leaf alone could not see",
|
|
619
|
+
"; ".join(filesystem_problems("claude/guides/g.md", str(probe_root))),
|
|
620
|
+
"TARGET_CASE_ON_DISK")
|
|
621
|
+
control("and the refusal names the spelling the disk carries",
|
|
622
|
+
"; ".join(filesystem_problems("claude/guides/g.md", str(probe_root))),
|
|
623
|
+
"spelled 'CLAUDE' on disk")
|
|
624
|
+
clean("while the spelling the disk carries passes",
|
|
625
|
+
filesystem_problems("CLAUDE/guides/g.md", str(probe_root)))
|
|
626
|
+
# ---- the Unicode identity policy ----------------------------------------------
|
|
627
|
+
#
|
|
628
|
+
# `é` composed (U+00E9) and `é` decomposed (U+0065 U+0301) are the same file on macOS
|
|
629
|
+
# and were two identities: the chain, the journal and the layer map keyed on different
|
|
630
|
+
# strings for one file, and the collision check grouped them apart (the round-4 review).
|
|
631
|
+
composed = unicodedata.normalize("NFC", "guides/\u00e9.md")
|
|
632
|
+
decomposed = unicodedata.normalize("NFD", "guides/\u00e9.md")
|
|
633
|
+
control("the two spellings really are different strings, so this control is not "
|
|
634
|
+
"comparing a value with itself",
|
|
635
|
+
json.dumps(composed != decomposed), "true")
|
|
636
|
+
control("and they resolve to ONE identity, so the chain and the journal key on one file",
|
|
637
|
+
json.dumps(identity(composed) == identity(decomposed)), "true")
|
|
638
|
+
clean("so an approved set naming both is a chain over one file, which §10.5 sequences, "
|
|
639
|
+
"and not two files the collision rule must separate",
|
|
640
|
+
collision_problems([composed, decomposed]))
|
|
641
|
+
# And the spelling on disk is the entry's own: a decomposed entry does not make a
|
|
642
|
+
# composed operation approved.
|
|
643
|
+
(probe_root / "nfd").mkdir()
|
|
644
|
+
(probe_root / "nfd" / unicodedata.normalize("NFD", "\u00e9.md")).write_text(
|
|
645
|
+
"x", encoding="utf-8")
|
|
646
|
+
held_entries = os.listdir(probe_root / "nfd")
|
|
647
|
+
control("the fixture really carries the decomposed entry, so what follows is not a "
|
|
648
|
+
"filesystem that normalised it away",
|
|
649
|
+
json.dumps([unicodedata.normalize("NFD", name) == name
|
|
650
|
+
for name in held_entries]), "[true]")
|
|
651
|
+
clean("a composed operation over a decomposed entry is the SAME name — canonical "
|
|
652
|
+
"equivalence is one name, which is why identity normalises — and `in` was the "
|
|
653
|
+
"only test that could not see it",
|
|
654
|
+
filesystem_problems(f"nfd/{unicodedata.normalize('NFC', chr(0xe9))}.md",
|
|
655
|
+
str(probe_root)))
|
|
656
|
+
clean("and so is the entry's own spelling",
|
|
657
|
+
filesystem_problems(f"nfd/{unicodedata.normalize('NFD', chr(0xe9))}.md",
|
|
658
|
+
str(probe_root)))
|
|
659
|
+
# A CASE difference is a different name, canonical equivalence or not: the policy
|
|
660
|
+
# separates the two questions rather than folding one into the other.
|
|
661
|
+
(probe_root / "nfd" / unicodedata.normalize("NFD", "\u00c9x.md")).write_text(
|
|
662
|
+
"x", encoding="utf-8")
|
|
663
|
+
control("while a case difference over canonically equal characters is still refused, so "
|
|
664
|
+
"normalising did not swallow the case rule",
|
|
665
|
+
"; ".join(filesystem_problems(
|
|
666
|
+
f"nfd/{unicodedata.normalize('NFC', chr(0xe9))}x.md", str(probe_root))),
|
|
667
|
+
"TARGET_CASE_ON_DISK")
|
|
668
|
+
|
|
669
|
+
# An unreadable parent is not an approved spelling. The realistic cause is a
|
|
670
|
+
# permission, and macOS checks that at OPEN: a descriptor taken while the directory was
|
|
671
|
+
# readable keeps listing after the mode drops, so the branch is reached here through the
|
|
672
|
+
# other error that gets to it — a descriptor that is not a directory at all. What is
|
|
673
|
+
# being held is that the function REPORTS instead of returning "the spelling is fine"
|
|
674
|
+
# from a listing it never read.
|
|
675
|
+
held_file_fd = os.open(probe_root / "CLAUDE" / "guides" / "g.md", os.O_RDONLY)
|
|
676
|
+
try:
|
|
677
|
+
control("a parent that cannot be listed is reported rather than read as approval",
|
|
678
|
+
"; ".join(_spelling_on_disk(["CLAUDE", "guides", "g.md"], held_file_fd,
|
|
679
|
+
"CLAUDE/guides/g.md")),
|
|
680
|
+
"TARGET_SPELLING_UNREADABLE")
|
|
681
|
+
control("and the refusal says the question could not be answered, not that the "
|
|
682
|
+
"name matched",
|
|
683
|
+
"; ".join(_spelling_on_disk(["CLAUDE", "guides", "g.md"], held_file_fd,
|
|
684
|
+
"CLAUDE/guides/g.md")),
|
|
685
|
+
"cannot be established")
|
|
686
|
+
finally:
|
|
687
|
+
os.close(held_file_fd)
|
|
688
|
+
finally:
|
|
689
|
+
shutil.rmtree(probe_root, ignore_errors=True)
|
|
690
|
+
# The on-disk spelling, which a case-folding filesystem would otherwise hide.
|
|
691
|
+
held_spelling = _spelling_on_disk(["claude", "guides", "G.md"],
|
|
692
|
+
os.open(root / "claude" / "guides", os.O_RDONLY),
|
|
693
|
+
"claude/guides/G.md")
|
|
694
|
+
control("a file spelled one way on disk and another in the approval is refused by name",
|
|
695
|
+
"; ".join(held_spelling), "TARGET_CASE_ON_DISK")
|
|
696
|
+
control("and the refusal carries the spelling that is really there",
|
|
697
|
+
"; ".join(held_spelling), "'g.md'")
|
|
698
|
+
|
|
699
|
+
# ---- ONE owner for the spelling, held by source ------------------------------------------
|
|
700
|
+
#
|
|
701
|
+
# The contract stops having one owner quietly: `placement` carried its own carrier rules and
|
|
702
|
+
# accepted `./claude/CLAUDE.md` and `claude//CLAUDE.md` while this module refused them, so a
|
|
703
|
+
# spelling was approved in the review and refused at the apply. The lead asked for this
|
|
704
|
+
# control by source, with its negative.
|
|
705
|
+
clean("no runlib module but this one tests a target's or a carrier's spelling itself",
|
|
706
|
+
second_validator_problems())
|
|
707
|
+
planted_root = pathlib.Path(tempfile.mkdtemp(prefix="targets-second-", dir="/tmp"))
|
|
708
|
+
try:
|
|
709
|
+
for held_name in ("alpha.py", "beta.py"):
|
|
710
|
+
(planted_root / held_name).write_text("x = 1\n", encoding="utf-8")
|
|
711
|
+
clean("and the scan is clean over a tree that has none",
|
|
712
|
+
second_validator_problems(planted_root))
|
|
713
|
+
(planted_root / "beta.py").write_text(
|
|
714
|
+
'def check(target):\n return target.startswith("/")\n', encoding="utf-8")
|
|
715
|
+
control("a second absolute-path test on a target is reported, with the file and the "
|
|
716
|
+
"line", "; ".join(second_validator_problems(planted_root)),
|
|
717
|
+
"SECOND_TARGET_VALIDATOR beta.py:2")
|
|
718
|
+
(planted_root / "beta.py").write_text(
|
|
719
|
+
'def check(carrier):\n return ".." in carrier.split("/")\n', encoding="utf-8")
|
|
720
|
+
control("and so is a traversal test on a carrier",
|
|
721
|
+
"; ".join(second_validator_problems(planted_root)),
|
|
722
|
+
"SECOND_TARGET_VALIDATOR beta.py:2")
|
|
723
|
+
(planted_root / "beta.py").write_text(
|
|
724
|
+
'def check(root, path):\n return path.startswith("/") and root\n',
|
|
725
|
+
encoding="utf-8")
|
|
726
|
+
clean("while the same test on a value that is not a target or a carrier is not this "
|
|
727
|
+
"contract's: containment asks a different question about a path that is already "
|
|
728
|
+
"valid", second_validator_problems(planted_root))
|
|
729
|
+
control("a scan that reads no module at all says so rather than passing",
|
|
730
|
+
"; ".join(second_validator_problems(
|
|
731
|
+
pathlib.Path(tempfile.mkdtemp(prefix="targets-empty-", dir="/tmp")))),
|
|
732
|
+
"SECOND_TARGET_VALIDATOR_NO_MODULES")
|
|
733
|
+
finally:
|
|
734
|
+
shutil.rmtree(planted_root, ignore_errors=True)
|
|
735
|
+
control("an exemption that names code its file no longer carries is reported, so the one "
|
|
736
|
+
"licence here cannot outlive its reason",
|
|
737
|
+
"; ".join(_with_exemption(("inventory.py", "code nobody wrote"), "planted",
|
|
738
|
+
second_validator_problems)),
|
|
739
|
+
"SECOND_TARGET_VALIDATOR_EXEMPTION_STALE inventory.py")
|
|
740
|
+
|
|
741
|
+
# ---- the set ----------------------------------------------------------------------------
|
|
742
|
+
held_set = set_problems(["claude/guides/g.md", "claude/guides/to-root/CLAUDE.md",
|
|
743
|
+
"claude/guides/g.md"], root)
|
|
744
|
+
control("a set is judged target by target", "; ".join(held_set), "TARGET_SYMLINK")
|
|
745
|
+
# A hard link: no symlink anywhere, the walk succeeds, and the bytes are a file outside.
|
|
746
|
+
outside_file = pathlib.Path(tempfile.mkdtemp(dir=root.parent)) / "outside.md"
|
|
747
|
+
outside_file.write_text("outside\n", encoding="utf-8")
|
|
748
|
+
os.link(outside_file, root / "claude" / "guides" / "twin.md")
|
|
749
|
+
held_twin = filesystem_problems("claude/guides/twin.md", root)
|
|
750
|
+
control("a leaf with a second name is refused by name: the other name may lie outside the "
|
|
751
|
+
"checkout, and writing through this one writes that file",
|
|
752
|
+
[p.split()[0] for p in held_twin], "['TARGET_HARD_LINKED']")
|
|
753
|
+
# A FIFO where a file is named: with no writer, a blocking open never returns, so the judgment
|
|
754
|
+
# is asked on another thread and held to a deadline. If it is still asking then, the open is
|
|
755
|
+
# released by giving the FIFO a writer, and the control fails by name instead of hanging.
|
|
756
|
+
import threading
|
|
757
|
+
pipe = root / "claude" / "guides" / "pipe.md"
|
|
758
|
+
os.mkfifo(pipe)
|
|
759
|
+
answered = []
|
|
760
|
+
asking = threading.Thread(target=lambda: answered.append(filesystem_problems("claude/guides/pipe.md", root)),
|
|
761
|
+
daemon=True)
|
|
762
|
+
asking.start()
|
|
763
|
+
asking.join(10)
|
|
764
|
+
waited = asking.is_alive() # an answer that came only after the release is not "at once"
|
|
765
|
+
if waited:
|
|
766
|
+
released = os.open(pipe, os.O_WRONLY | os.O_NONBLOCK)
|
|
767
|
+
asking.join(10)
|
|
768
|
+
os.close(released)
|
|
769
|
+
control("a FIFO named as a target is refused by name, at once: its open does not wait for a writer",
|
|
770
|
+
[waited, [[p.split()[0] for p in held] for held in answered[:1]]],
|
|
771
|
+
"[False, [['TARGET_NOT_FILE']]]")
|
|
772
|
+
pipe.unlink()
|
|
773
|
+
single = root / "claude" / "guides" / "single.md"
|
|
774
|
+
single.write_text("one name\n", encoding="utf-8")
|
|
775
|
+
clean("and a file with one name passes the same walk",
|
|
776
|
+
filesystem_problems("claude/guides/single.md", root))
|
|
777
|
+
clean("and a target it names twice is not reported as a collision",
|
|
778
|
+
[p for p in held_set if "TARGET_REPEATED" in p])
|
|
779
|
+
clean("with no checkout the answer is the spelling alone, and a real path passes it",
|
|
780
|
+
problems("claude/guides/g.md"))
|
|
781
|
+
finally:
|
|
782
|
+
shutil.rmtree(root, ignore_errors=True)
|
|
783
|
+
|
|
784
|
+
print(f"targets --self-test: {'FAILED' if failures else 'OK'} ({len(ran)} controls: one "
|
|
785
|
+
f"identity per file, the spelling rules both surfaces now share, the component walk that "
|
|
786
|
+
f"refuses a symlink anywhere in the chain, the on-disk spelling, and a case collision "
|
|
787
|
+
f"refused where the filesystem folds case and where it will not say)")
|
|
788
|
+
for name, detail in failures:
|
|
789
|
+
print(f"targets --self-test [FAIL] {name}: {detail}")
|
|
790
|
+
return 1 if failures else 0
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
def _linked_root(root):
|
|
794
|
+
"""A symlink pointing at `root`, for the control that refuses a linked checkout root."""
|
|
795
|
+
held = pathlib.Path(str(root) + "-link")
|
|
796
|
+
if not held.exists():
|
|
797
|
+
os.symlink(root, held)
|
|
798
|
+
return held
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
def main(argv):
|
|
802
|
+
if "--self-test" in argv:
|
|
803
|
+
return self_test()
|
|
804
|
+
print(__doc__)
|
|
805
|
+
return 0
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
if __name__ == "__main__":
|
|
809
|
+
sys.exit(main(sys.argv[1:]))
|