feat(780): commit a ruff config and enforce it in CI

Python lint here was a property of the operator's laptop, not of the repo: the
global instructions say to run `ruff check`/`ruff format --check`/`pyright`, no
workflow or hook ran any of them, and with no committed config ruff fell back to
whichever `~/.config/ruff/ruff.toml` the machine happened to have. A second
machine lints this repo differently, or not at all — Family E in
docs/defect-shapes-773.md, in our own toolchain.

Option 1 from the issue:

- `ruff.toml` at the root (the operator's global settings, pinned here — that is
  what the tree was de-facto written against, so adopting it cost a mechanical
  reformat rather than a rewrite).
- The `script-tests` job installs a PINNED `ruff==0.12.11` and runs
  `ruff check .` + `ruff format --check .` before pytest, so a style red arrives
  in seconds instead of after the four-minute suite. The pin is the second half
  of the fix: an unpinned install re-introduces the same divergence one layer up.
  Renamed the job to `Script lint and tests (ruff + pytest)` so a lint red is not
  triaged under a name that says pytest.
- Tree made clean: 72 errors and 20 files needing reformatting. Everything fixed
  except two config-level exemptions with reasons in the config (`S101`, `S105`
  under `scripts/tests/**` — the stub credentials that fire `S105` have no true
  positive anywhere in the tree) and two per-site `# noqa`s that carry their
  reason inline.

`pyright` stays ungated: its only findings are `reportMissingImports` for
`etv_client` in scripts/scripted-schedules/entrypoint.py, resolvable only in that
script's deploy environment, and gating it would put a node toolchain on the
git-only `small` lane to find nothing.

Population is what `ruff check .` discovers from the root, so a new `.py`
anywhere is covered without the job enumerating anything.

Docs: new record `ci.python-lint-ruff-config-committed`, cross-ref from
`ci.script-tests-job`, `docs/ci-cd.md` script-tests section, and
`docs/defect-shapes-773.md` §5.2 marked resolved with the measured numbers.

Refs: #780
Decisions-Edit: yes
This commit is contained in:
2026-08-21 22:47:22 +02:00
parent 706674272c
commit 18b2a46beb
27 changed files with 1535 additions and 967 deletions
+1 -3
View File
@@ -24,6 +24,4 @@ import pytest
@pytest.fixture(autouse=True)
def isolate_hook_fire_log(tmp_path_factory, monkeypatch):
monkeypatch.setenv(
"ETV_HOOK_FIRE_LOG_DIR", str(tmp_path_factory.mktemp("hook-fire-log"))
)
monkeypatch.setenv("ETV_HOOK_FIRE_LOG_DIR", str(tmp_path_factory.mktemp("hook-fire-log")))
+27 -14
View File
@@ -36,9 +36,17 @@ BOM = b"\xef\xbb\xbf"
def _git(cwd: Path, *args: str) -> None:
subprocess.run(
["git", *args], cwd=str(cwd), check=True, capture_output=True,
env={**os.environ, "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@e",
"GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@e"},
["git", *args],
cwd=str(cwd),
check=True,
capture_output=True,
env={
**os.environ,
"GIT_AUTHOR_NAME": "t",
"GIT_AUTHOR_EMAIL": "t@e",
"GIT_COMMITTER_NAME": "t",
"GIT_COMMITTER_EMAIL": "t@e",
},
)
@@ -54,12 +62,18 @@ def _repo_with(tmp_path: Path, name: str, content: bytes) -> Path:
def _run(hook: Path, repo: Path, env: dict) -> tuple[int, bytes]:
payload = json.dumps({
"session_id": "s", "hook_event_name": "PreToolUse", "tool_name": "Bash",
"cwd": str(repo), "tool_input": {"command": "git commit -m x"},
})
p = subprocess.run(["bash", str(hook)], input=payload.encode(), capture_output=True,
cwd=str(repo), env=env, timeout=60)
payload = json.dumps(
{
"session_id": "s",
"hook_event_name": "PreToolUse",
"tool_name": "Bash",
"cwd": str(repo),
"tool_input": {"command": "git commit -m x"},
}
)
p = subprocess.run(
["bash", str(hook)], input=payload.encode(), capture_output=True, cwd=str(repo), env=env, timeout=60
)
return p.returncode, p.stdout
@@ -105,8 +119,9 @@ def _path_without_xxd(tmp_path: Path) -> dict:
def test_the_fixture_really_stages_a_BOM(tmp_path):
repo = _repo_with(tmp_path, "Bad.cs", BOM + b"class A {}\n")
assert (repo / "Bad.cs").read_bytes()[:3] == BOM
staged = subprocess.run(["git", "diff", "--name-only", "--cached"], cwd=str(repo),
capture_output=True, text=True).stdout.split()
staged = subprocess.run(
["git", "diff", "--name-only", "--cached"], cwd=str(repo), capture_output=True, text=True
).stdout.split()
assert staged == ["Bad.cs"], f"nothing was staged, so the guard would have nothing to read: {staged}"
@@ -179,6 +194,4 @@ def test_DISARMING_the_BOM_comparison_stops_detection(tmp_path):
rc, out = _run(mutated, repo, dict(os.environ))
assert rc == 0
assert out == b"", (
f"disarming the BOM comparison did not stop detection, so it is not load-bearing: {out!r}"
)
assert out == b"", f"disarming the BOM comparison did not stop detection, so it is not load-bearing: {out!r}"
+38 -34
View File
@@ -115,13 +115,13 @@ def _guard_buckets(job: str):
argv = _guard(job)["run"].split()
assert "--always" in argv and "--gated" in argv, argv
a, g = argv.index("--always"), argv.index("--gated")
return argv[a + 1:g], argv[g + 1:]
return argv[a + 1 : g], argv[g + 1 :]
# Mirrors the `if:` every gated step in these jobs carries. Compared as a normalised string rather
# than by parsing the expression: what matters is that a step's gating and the guard's bucketing are
# the SAME condition, and any rewrite of one that is not mirrored in the other should be loud.
SKIP_GATE = ("steps.detect.outputs.docs_only!='true'&&steps.revalidate.outputs.skip!='true'")
SKIP_GATE = "steps.detect.outputs.docs_only!='true'&&steps.revalidate.outputs.skip!='true'"
def _is_gated(step) -> bool:
@@ -211,17 +211,14 @@ def test_every_consequential_run_step_marks_itself_as_its_FIRST_act(job):
# prefix as a preceding command and reddens every correctly-written step.
lines = s["run"].splitlines()
at = next(i for i, ln in enumerate(lines) if _MARK.search(ln))
preceding = [
ln.strip() for ln in lines[:at]
if ln.strip() and not ln.strip().startswith("#")
]
preceding = [ln.strip() for ln in lines[:at] if ln.strip() and not ln.strip().startswith("#")]
if [ln for ln in preceding if not ln.startswith("set -")]:
late.append((s.get("name", "?"), preceding))
assert not missing, (
f"these run: steps of the REQUIRED job '{job}' do not record that they executed: {missing}. "
"A step the runner drops concludes success, so without a marker its non-execution takes the "
"whole required context green having done no work (ersatztv#756). Add "
'`\"${GITHUB_WORKSPACE:-.}/scripts/ci-step-ran.sh\" mark <key>` as the step\'s first line and '
'`"${GITHUB_WORKSPACE:-.}/scripts/ci-step-ran.sh" mark <key>` as the step\'s first line and '
"the key to the guard step's --always/--gated list."
)
assert not late, (
@@ -357,8 +354,10 @@ def test_the_guards_OWN_body_cannot_be_dropped_by_the_mechanism_it_guards_agains
# a red here blocks every merge through the combined status, so brittleness is a real cost and
# not a free strictness win. Whitespace inside the delimiters is normalised for the same reason.
env = {k: re.sub(r"\s+", "", str(v)) for k, v in (guard.get("env") or {}).items()}
for name, want in (("ETV_DOCS_ONLY", "${{steps.detect.outputs.docs_only}}"),
("ETV_REVALIDATE_SKIP", "${{steps.revalidate.outputs.skip}}")):
for name, want in (
("ETV_DOCS_ONLY", "${{steps.detect.outputs.docs_only}}"),
("ETV_REVALIDATE_SKIP", "${{steps.revalidate.outputs.skip}}"),
):
assert env.get(name) == want, (
f"the '{job}' guard's env: has {name}={guard.get('env', {}).get(name)!r}, expected the "
f"output the gated steps' own `if:` reads ({want}). A typo here is SILENT rather than "
@@ -427,12 +426,10 @@ def _env(tmp_path, **extra):
def _run(script: str, env):
return subprocess.run(["bash", "-c", script], cwd=REPO_ROOT, env=env,
capture_output=True, text=True)
return subprocess.run(["bash", "-c", script], cwd=REPO_ROOT, env=env, capture_output=True, text=True)
@pytest.mark.parametrize("gate", GATE_VALUES_IN_THE_WILD,
ids=["gate-false", "gate-empty", "gate-unset"])
@pytest.mark.parametrize("gate", GATE_VALUES_IN_THE_WILD, ids=["gate-false", "gate-empty", "gate-unset"])
@pytest.mark.parametrize("job", MARKED_JOBS)
def test_the_guard_PASSES_when_every_step_marked_itself(job, gate, tmp_path):
"""The positive control. Without it, a guard that always failed would satisfy every case below.
@@ -458,7 +455,8 @@ def test_the_guard_PASSES_when_every_step_marked_itself(job, gate, tmp_path):
# inverted provenance test would otherwise ship silently, and the operator reading this line to
# settle the promotion question would read it wrong.
assert f"Marker identity: job={job} run=424242 attempt=7 (from the runner)" in r.stdout, (
f"the guard misreported its marker identity: {r.stdout!r}")
f"the guard misreported its marker identity: {r.stdout!r}"
)
@pytest.mark.parametrize("job", MARKED_JOBS)
@@ -534,8 +532,7 @@ def test_an_EMPTY_gate_value_requires_the_gated_steps(job, tmp_path):
what is required.
"""
guard = _guard(job)["run"]
r = _run("\n".join(["set -e", guard]),
_env(tmp_path, GITHUB_JOB=job, ETV_DOCS_ONLY="", ETV_REVALIDATE_SKIP=""))
r = _run("\n".join(["set -e", guard]), _env(tmp_path, GITHUB_JOB=job, ETV_DOCS_ONLY="", ETV_REVALIDATE_SKIP=""))
assert r.returncode != 0
assert _guard_buckets(job)[1][-1] in (r.stdout + r.stderr), (
f"empty gate values were read as a skip, so the gated steps went unchecked: {r.stdout}"
@@ -571,8 +568,7 @@ def test_a_STALE_marker_from_another_run_cannot_satisfy_the_guard(tmp_path):
# ...nor may the OTHER job in the same run inherit them.
sibling = _env(tmp_path, GITHUB_JOB="migrations", GITHUB_RUN_ID="111", GITHUB_RUN_ATTEMPT="1")
assert _run(_guard("migrations")["run"], sibling).returncode != 0, (
"the two required jobs share one marker file, so one job's markers answer for the other's "
"dropped steps"
"the two required jobs share one marker file, so one job's markers answer for the other's dropped steps"
)
@@ -611,8 +607,7 @@ def test_a_key_is_matched_WHOLE_not_as_a_substring(tmp_path):
assert _run(f"{SCRIPT} mark web-build && {SCRIPT} mark web-test", env).returncode == 0
r = _run(f"{SCRIPT} assert --always build", env)
assert r.returncode != 0, (
"the key 'build' was satisfied by a marker for 'web-build' — a dropped `dotnet build` would "
"pass unnoticed"
"the key 'build' was satisfied by a marker for 'web-build' — a dropped `dotnet build` would pass unnoticed"
)
@@ -638,9 +633,11 @@ def test_a_degraded_run_IDENTITY_refuses_rather_than_sharing_a_marker_path(tmp_p
f"{r.stdout}{r.stderr}"
)
assert "cannot identify this run" in (r.stdout + r.stderr), (
f"refused, but without naming the cause: {r.stdout!r} {r.stderr!r}")
f"refused, but without naming the cause: {r.stdout!r} {r.stderr!r}"
)
assert not list(tmp_path.iterdir()), (
"a degraded-identity `mark` still created a marker file somewhere under RUNNER_TEMP")
"a degraded-identity `mark` still created a marker file somewhere under RUNNER_TEMP"
)
def test_the_marker_identity_is_REPORTED_on_stdout_every_run(tmp_path):
@@ -661,12 +658,15 @@ def test_the_marker_identity_is_REPORTED_on_stdout_every_run(tmp_path):
documented contract (the record's `mechanics:`), and a future reader is told to trust it.
"""
marks = [_mark_line(s) for s, _ in _marked("test")]
r = _run("\n".join(["set -e", *marks, _guard("test")["run"]]),
_env(tmp_path, GITHUB_RUN_ID="1916", GITHUB_RUN_ATTEMPT="4"))
r = _run(
"\n".join(["set -e", *marks, _guard("test")["run"]]),
_env(tmp_path, GITHUB_RUN_ID="1916", GITHUB_RUN_ATTEMPT="4"),
)
assert r.returncode == 0, r.stdout + r.stderr
assert "Marker identity: job=test run=1916 attempt=4 (from the runner)" in r.stdout, (
"the guard did not report the identity its marker path was actually keyed on, so a reader "
f"cannot audit the keying from a run log: {r.stdout!r}")
f"cannot audit the keying from a run log: {r.stdout!r}"
)
def test_a_skip_gate_that_empties_the_expected_set_REFUSES(tmp_path):
@@ -681,15 +681,16 @@ def test_a_skip_gate_that_empties_the_expected_set_REFUSES(tmp_path):
that it proved everything while proving nothing" is the failure this whole file exists to remove.
"""
r = _run(f"{SCRIPT} assert --always --gated foo", _env(tmp_path, ETV_DOCS_ONLY="true"))
assert r.returncode != 0, (
f"the guard passed with an empty post-gate expectation set: {r.stdout!r}")
assert r.returncode != 0, f"the guard passed with an empty post-gate expectation set: {r.stdout!r}"
assert "no expected keys" in (r.stdout + r.stderr).lower() or "NO expected keys" in r.stderr
@pytest.mark.parametrize("revalidate", ["true", "false", "", None],
ids=lambda v: f"reval-{v if v is not None else 'unset'}")
@pytest.mark.parametrize("docs_only", ["true", "false", "", None],
ids=lambda v: f"docs-{v if v is not None else 'unset'}")
@pytest.mark.parametrize(
"revalidate", ["true", "false", "", None], ids=lambda v: f"reval-{v if v is not None else 'unset'}"
)
@pytest.mark.parametrize(
"docs_only", ["true", "false", "", None], ids=lambda v: f"docs-{v if v is not None else 'unset'}"
)
def test_the_skip_gate_over_the_WHOLE_value_matrix(docs_only, revalidate, tmp_path):
"""Every combination of the two gate values, not just the diagonal — cold review's last finding.
@@ -713,11 +714,14 @@ def test_the_skip_gate_over_the_WHOLE_value_matrix(docs_only, revalidate, tmp_pa
job = "test"
always, gated = _guard_buckets(job)
marks = [_mark_line(s) for s, k in _marked(job) if k in always]
r = _run("\n".join(["set -e", *marks, _guard(job)["run"]]),
_env(tmp_path, ETV_DOCS_ONLY=docs_only, ETV_REVALIDATE_SKIP=revalidate))
r = _run(
"\n".join(["set -e", *marks, _guard(job)["run"]]),
_env(tmp_path, ETV_DOCS_ONLY=docs_only, ETV_REVALIDATE_SKIP=revalidate),
)
should_skip = docs_only == "true" or revalidate == "true"
assert (r.returncode == 0) is should_skip, (
f"with docs_only={docs_only!r} and revalidate={revalidate!r} the guard "
f"{'passed' if r.returncode == 0 else 'failed'}, expected it to "
f"{'skip the gated keys' if should_skip else 'require them'}. The gate must treat a value as "
"a skip if and only if it is exactly `true` in EITHER variable.\n" + r.stdout + r.stderr)
"a skip if and only if it is exactly `true` in EITHER variable.\n" + r.stdout + r.stderr
)
@@ -198,8 +198,7 @@ def test_the_registry_partitions_every_job_in_the_workflow():
)
assert not (TOOLCHAIN_JOBS & BARE_RUNNER_JOBS), "a job cannot be in both lists"
assert all_jobs == TOOLCHAIN_JOBS | BARE_RUNNER_JOBS, (
f"the registry names jobs that do not exist: "
f"{sorted((TOOLCHAIN_JOBS | BARE_RUNNER_JOBS) - all_jobs)}"
f"the registry names jobs that do not exist: {sorted((TOOLCHAIN_JOBS | BARE_RUNNER_JOBS) - all_jobs)}"
)
@@ -242,8 +241,7 @@ def test_a_single_job_losing_its_pin_is_DETECTED(doc):
empty list would satisfy the live assertion above and prove nothing — which is how #621 and
#685 both shipped."""
assert pin_population_faults(doc), (
"the population check accepted a workflow in which a container job no longer runs the "
"pinned toolchain image"
"the population check accepted a workflow in which a container job no longer runs the pinned toolchain image"
)
+23 -25
View File
@@ -328,8 +328,7 @@ def _run_the_real_scan_body(repo: Path, tmp_path: Path):
(repo / _NONCE_FILE).write_text(nonce)
env[_FENCE] = nonce
(tmp_path / "runner").mkdir(exist_ok=True)
return subprocess.run(["bash", "-c", body], cwd=repo, env=env,
capture_output=True, text=True)
return subprocess.run(["bash", "-c", body], cwd=repo, env=env, capture_output=True, text=True)
@_nested
@@ -358,7 +357,7 @@ def test_the_scan_step_REALLY_FAILS_on_a_poisoned_workflow(tmp_path):
text = wf.read_text()
anchor = ' IMG="${IMAGE}:${SMOKE_SHORT_SHA}"'
assert anchor in text, "anchor for the poison is gone — rewrite this control"
wf.write_text(text.replace(anchor, ' # ${{ steps.meta.outputs.short }}\n' + anchor, 1))
wf.write_text(text.replace(anchor, " # ${{ steps.meta.outputs.short }}\n" + anchor, 1))
res = _run_the_real_scan_body(repo, tmp_path)
assert res.returncode != 0, (
@@ -403,8 +402,10 @@ def test_the_PROOF_SCRIPT_itself_refuses_when_the_ban_is_deselected(tmp_path):
)
res = subprocess.run(
["bash", str(repo / "scripts" / "ci-prove-ban-detects.sh")],
cwd=repo, env={**os.environ, "GITHUB_WORKSPACE": str(repo)},
capture_output=True, text=True,
cwd=repo,
env={**os.environ, "GITHUB_WORKSPACE": str(repo)},
capture_output=True,
text=True,
)
assert res.returncode != 0, (
"ci-prove-ban-detects.sh vouched for the gate while the ban test was deselected at the "
@@ -446,19 +447,16 @@ def test_the_PROOF_SCRIPT_refuses_when_the_WRONG_test_fails(tmp_path):
# landing on the "not enforcing" branch instead of the one under test. (First draft of this test
# did exactly that and was red for the wrong reason.)
ban = repo / BAN_TEST_FILE
ban.write_text(
ban.read_text()
+ "\n\ndef test_an_unrelated_failure_for_this_probe():\n assert False\n"
)
ban.write_text(ban.read_text() + "\n\ndef test_an_unrelated_failure_for_this_probe():\n assert False\n")
res = subprocess.run(
["bash", str(repo / "scripts" / "ci-prove-ban-detects.sh")],
cwd=repo, env={**os.environ, "GITHUB_WORKSPACE": str(repo)},
capture_output=True, text=True,
cwd=repo,
env={**os.environ, "GITHUB_WORKSPACE": str(repo)},
capture_output=True,
text=True,
)
combined = res.stdout + res.stderr
assert res.returncode != 0, (
f"the script read an unrelated test's failure as proof.\n{combined}"
)
assert res.returncode != 0, f"the script read an unrelated test's failure as proof.\n{combined}"
# THE SPECIFIC branch. This scenario is built to land on "wrong test failed" (exit 1, no `[build]`
# failure); accepting "could not prove anything" too would let it drift onto the exit-5 branch and
# silently cover a branch it was not written for, while still looking green.
@@ -474,15 +472,17 @@ def test_the_PROOF_SCRIPT_passes_on_a_clean_tree(tmp_path):
repo = _repo_copy(tmp_path)
res = subprocess.run(
["bash", str(repo / "scripts" / "ci-prove-ban-detects.sh")],
cwd=repo, env={**os.environ, "GITHUB_WORKSPACE": str(repo)},
capture_output=True, text=True,
cwd=repo,
env={**os.environ, "GITHUB_WORKSPACE": str(repo)},
capture_output=True,
text=True,
)
assert res.returncode == 0, (
f"ci-prove-ban-detects.sh failed on a clean tree.\nstdout:\n{res.stdout}\nstderr:\n{res.stderr}"
)
assert (repo / ".gitea" / "workflows" / "docker-build.yml").read_text() == (
WORKFLOW.read_text()
), "the script did not restore the workflow file it poisoned"
assert (repo / ".gitea" / "workflows" / "docker-build.yml").read_text() == (WORKFLOW.read_text()), (
"the script did not restore the workflow file it poisoned"
)
def test_the_pytest_invocation_cannot_DESELECT_or_swallow_its_result():
@@ -494,7 +494,7 @@ def test_the_pytest_invocation_cannot_DESELECT_or_swallow_its_result():
# Only the tokens AFTER `pytest` are pytest's own arguments. Checking the whole line would flag
# the `-m` in `python3 -m pytest`, which is how the interpreter is invoked — a false positive
# that would make this test red on the correct command.
args = tokens[tokens.index("pytest") + 1:]
args = tokens[tokens.index("pytest") + 1 :]
banned = {"-k", "-m", "--deselect", "--ignore", "--collect-only", "--co"}
assert not (banned & set(args)), f"pytest invocation may deselect tests: {line!r}"
for op in ("||", "&&", ";", "|"):
@@ -527,8 +527,7 @@ def test_no_step_in_the_scan_job_is_advisory():
@pytest.mark.parametrize(
"step_name",
[s.get("name", "?") for s in yaml.safe_load(WORKFLOW.read_text())["jobs"][JOB]["steps"]
if s.get("run")],
[s.get("name", "?") for s in yaml.safe_load(WORKFLOW.read_text())["jobs"][JOB]["steps"] if s.get("run")],
)
def test_every_run_body_in_the_scan_job_is_delimiter_free(step_name):
"""The guard must not be vulnerable to the defect it guards against.
@@ -561,7 +560,7 @@ def test_the_guard_expectations_match_the_markers_exactly():
restated here, so adding a step without a marker is a red."""
argv = _guard()["run"].split()
assert "--always" in argv, argv
always = argv[argv.index("--always") + 1:]
always = argv[argv.index("--always") + 1 :]
assert "--gated" not in argv, "every step in this job is unconditional; there is nothing to gate"
assert sorted(always) == sorted(k for _, k in _marked())
@@ -601,8 +600,7 @@ def _env(tmp_path, **extra):
def _run(script: str, env):
return subprocess.run(["bash", "-c", script], cwd=REPO_ROOT, env=env,
capture_output=True, text=True)
return subprocess.run(["bash", "-c", script], cwd=REPO_ROOT, env=env, capture_output=True, text=True)
def test_the_guard_PASSES_when_every_step_ran(tmp_path):
+11 -10
View File
@@ -744,7 +744,9 @@ def test_wing_faults_block_scalar_record_fails_loudly(tmp_path):
this corpus's very long `rule:` values — makes the whole record silently invisible."""
records, archive = _wing(tmp_path)
bad = records / "ci" / "blockscalar.md"
bad.write_text(_GOOD.replace("rule: 'a rule on one quoted line'\n", "rule: >-\n a long rule wrapped\n over two lines\n"))
bad.write_text(
_GOOD.replace("rule: 'a rule on one quoted line'\n", "rule: >-\n a long rule wrapped\n over two lines\n")
)
# Precondition: this really is the silent-vanish case, not some other parse error.
assert dl.parse_file(bad) == [], "expected the reader to drop the record entirely"
@@ -805,8 +807,10 @@ def test_wing_faults_exempts_stripped_legacy_archive_files(tmp_path):
def test_validate_surfaces_wing_faults_as_errors(tmp_path):
"""Faults must arrive as validator ERRORS (exit 1), not warnings."""
errs = _v([_rec(key="ci.a", source=Path("docs/decisions/records/ci/a.md"), heading="A")],
wing_faults=["docs/decisions/records/ci/x.md: parsed to 0 records, expected exactly 1"])
errs = _v(
[_rec(key="ci.a", source=Path("docs/decisions/records/ci/a.md"), heading="A")],
wing_faults=["docs/decisions/records/ci/x.md: parsed to 0 records, expected exactly 1"],
)
assert any("x.md" in e for e in errs), errs
@@ -826,7 +830,7 @@ def test_wing_faults_sees_a_DEEPER_nested_archive_record(tmp_path):
one level down'."""
records, archive = _wing(tmp_path)
(records / "ci" / "good.md").write_text(_GOOD)
(archive / "api.md").write_text("# api\n\n## Records formerly in this file\n") # still exempt
(archive / "api.md").write_text("# api\n\n## Records formerly in this file\n") # still exempt
deep = archive / "ci" / "sub"
deep.mkdir(parents=True)
(deep / "broken.md").write_text("# not a record\n")
@@ -887,8 +891,7 @@ def test_junk_frontmatter_key_from_a_split_value_is_faulted(tmp_path):
PyYAML rejects this input, so the hand reader is more permissive than the writer."""
records, archive = _wing(tmp_path)
bad = records / "ci" / "corrupt.md"
bad.write_text(_GOOD.replace("rule: 'a rule on one quoted line'\n",
"rule: >-\nthe real rule: with a colon\n"))
bad.write_text(_GOOD.replace("rule: 'a rule on one quoted line'\n", "rule: >-\nthe real rule: with a colon\n"))
recs = dl.parse_file(bad)
assert len(recs) == 1 and recs[0].key, "precondition: this parses to one KEYED record"
assert recs[0].rule == ">-", f"precondition: the real value was truncated, got {recs[0].rule!r}"
@@ -903,7 +906,7 @@ def test_an_empty_or_missing_record_wing_is_LOUD(tmp_path):
missing = dv.record_wing_faults(tmp_path / "nope" / "records", tmp_path / "nope" / "archive")
assert missing and "missing or contains no" in missing[0], missing
records, archive = _wing(tmp_path) # exists but holds no *.md
records, archive = _wing(tmp_path) # exists but holds no *.md
empty = dv.record_wing_faults(records, archive)
assert empty and "missing or contains no" in empty[0], empty
@@ -1437,7 +1440,6 @@ def test_no_budget_flag_means_no_retirement_warning(capsys):
assert "is RETIRED and was IGNORED" not in capsys.readouterr().err
def test_main_reports_ceiling_drift_as_a_NOTICE_and_still_exits_0(capsys):
"""The fine claim's live wiring (#688): the drift notice must fire, and must NOT turn the run
red — the entire point of the v5 split.
@@ -1511,8 +1513,7 @@ def test_main_actually_REPORTS_the_ceiling_and_the_trend(capsys):
err = capsys.readouterr().err
assert "prose lines across" in err, "the aggregate trend notice must always print"
over = [r.key for r in dl.all_active_records()
if r.key and dv.record_prose_lines(r) > dv.RECORD_CEILING_DEFAULT]
over = [r.key for r in dl.all_active_records() if r.key and dv.record_prose_lines(r) > dv.RECORD_CEILING_DEFAULT]
warned = "exceed the" in err and "prose ceiling" in err
assert warned is bool(over), f"ceiling warning printed={warned} but {len(over)} record(s) are over it"
if over:
+20 -22
View File
@@ -72,16 +72,17 @@ def derived_guard_files() -> set[str]:
not a guard, and globbing would drag in every helper and make the inventory a chore that gets
rubber-stamped.
"""
found = {
str(p.relative_to(REPO_ROOT)) for p in HOOKS_DIR.glob("*.sh")
} | {
str(p.relative_to(REPO_ROOT)) for p in HUSKY_DIR.iterdir() if p.is_file()
} | {
# `pr-checks.yml` runs `pytest scripts/tests` as a directory, so every file in it is
# invoked and none is individually named anywhere. Globbing is the only derivation that
# matches how they actually run.
str(p.relative_to(REPO_ROOT)) for p in (REPO_ROOT / "scripts" / "tests").glob("test_*.py")
}
found = (
{str(p.relative_to(REPO_ROOT)) for p in HOOKS_DIR.glob("*.sh")}
| {str(p.relative_to(REPO_ROOT)) for p in HUSKY_DIR.iterdir() if p.is_file()}
| {
# `pr-checks.yml` runs `pytest scripts/tests` as a directory, so every file in it is
# invoked and none is individually named anywhere. Globbing is the only derivation that
# matches how they actually run.
str(p.relative_to(REPO_ROOT))
for p in (REPO_ROOT / "scripts" / "tests").glob("test_*.py")
}
)
callers = list(WORKFLOWS_DIR.glob("*.yml")) + list(HOOKS_DIR.glob("*.sh"))
callers += [p for p in HUSKY_DIR.iterdir() if p.is_file()]
@@ -113,13 +114,8 @@ def wired_hook_files() -> set[str]:
text = (REPO_ROOT / ".claude" / "settings.json").read_text()
for husky in HUSKY_DIR.iterdir():
if husky.is_file():
text += "\n".join(
line for line in husky.read_text().splitlines()
if not line.lstrip().startswith("#")
)
return {
str(p.relative_to(REPO_ROOT)) for p in HOOKS_DIR.glob("*.sh") if p.name in text
}
text += "\n".join(line for line in husky.read_text().splitlines() if not line.lstrip().startswith("#"))
return {str(p.relative_to(REPO_ROOT)) for p in HOOKS_DIR.glob("*.sh") if p.name in text}
def inventory_rows() -> list[tuple[str, str, str, str]]:
@@ -235,9 +231,7 @@ def test_proof_rows_do_not_themselves_claim_a_proof():
for guard, kind, proof, ref in inventory_rows():
if kind == "PROOF":
assert proof == "NONE", f"{guard} is PROOF but claims Proof {proof}"
assert guard.startswith("scripts/tests/"), (
f"{guard} is marked PROOF but does not live in scripts/tests/"
)
assert guard.startswith("scripts/tests/"), f"{guard} is marked PROOF but does not live in scripts/tests/"
assert ref in ("—", "-", ""), f"{guard}: PROOF rows carry no proof ref"
@@ -283,8 +277,12 @@ def test_the_summary_counts_match_the_table():
)
claimed = tuple(int(g) for g in m.groups())
actual = (
kinds["GUARD"], kinds["TOOLING"], kinds["PROOF"],
grades["MUTATION"], grades["BEHAVIOUR-ONLY"], grades["NONE"],
kinds["GUARD"],
kinds["TOOLING"],
kinds["PROOF"],
grades["MUTATION"],
grades["BEHAVIOUR-ONLY"],
grades["NONE"],
)
assert claimed == actual, (
f"the summary claims (guards, tooling, proofs, mutation, behaviour-only, none) = {claimed} "
+321 -203
View File
@@ -27,9 +27,9 @@ that is fine alone and lethal in context.
from __future__ import annotations
import os
import pty
import re
import subprocess
import pty
from pathlib import Path
import pytest
@@ -103,9 +103,7 @@ def instrumentation_faults(text: str, name: str) -> list[str]:
if not mode:
faults.append(f"{name}: etv_hook_fire_begin names no stdout mode")
elif mode.group(1) != want:
faults.append(
f"{name}: begins in {mode.group(1)!r} mode but its wiring implies {want!r}"
)
faults.append(f"{name}: begins in {mode.group(1)!r} mode but its wiring implies {want!r}")
# Order is the whole point: 8 of the hooks slurp stdin with `input=$(cat)`, and a begin
# placed after that read would find the pipe already drained — recording a fire with no
@@ -133,8 +131,9 @@ def strip_instrumentation(text: str) -> str:
out.pop()
continue
if skipping:
if line.startswith(("#", "ETV_HOOK_FIRE_LIB=", "[ -r ", "type etv_hook_fire_begin",
"etv_hook_fire_begin ")):
if line.startswith(
("#", "ETV_HOOK_FIRE_LIB=", "[ -r ", "type etv_hook_fire_begin", "etv_hook_fire_begin ")
):
continue
skipping = False
out.append(line)
@@ -173,8 +172,15 @@ def test_the_stripper_removes_EXACTLY_the_preamble_and_nothing_else():
"""
import difflib
allowed = ("# ersatztv#776", "# git hook:", "# Claude hook:", "ETV_HOOK_FIRE_LIB=",
"[ -r \"$ETV_HOOK_FIRE_LIB\"", "type etv_hook_fire_begin", "etv_hook_fire_begin ")
allowed = (
"# ersatztv#776",
"# git hook:",
"# Claude hook:",
"ETV_HOOK_FIRE_LIB=",
'[ -r "$ETV_HOOK_FIRE_LIB"',
"type etv_hook_fire_begin",
"etv_hook_fire_begin ",
)
for hook in hook_scripts():
cur = hook.read_text().splitlines(keepends=True)
stripped = strip_instrumentation("".join(cur)).splitlines(keepends=True)
@@ -189,7 +195,6 @@ def test_the_stripper_removes_EXACTLY_the_preamble_and_nothing_else():
)
def test_the_stripper_actually_strips():
"""The A/B control and the mutation are the same function. If it were a no-op, the differential
test would compare each hook against itself and pass on a wrapper that breaks everything."""
@@ -211,8 +216,9 @@ def test_every_hook_reports_that_it_fired():
for hook in hook_scripts():
faults += instrumentation_faults(hook.read_text(), hook.stem)
assert not faults, (
"these hooks do not report their own execution:\n " + "\n ".join(faults) +
"\n\nAdd the three-line preamble after the `set -` line and BEFORE any stdin read. A hook "
"these hooks do not report their own execution:\n "
+ "\n ".join(faults)
+ "\n\nAdd the three-line preamble after the `set -` line and BEFORE any stdin read. A hook "
"that does not report is one whose firing we can only infer, which is ersatztv#776."
)
@@ -235,7 +241,7 @@ def test_a_hook_that_LOSES_its_instrumentation_is_DETECTED():
def test_begin_placed_AFTER_the_stdin_read_is_DETECTED():
"""The subtler mutation: present but too late. Ordering is the property that makes it work."""
late = 'set -euo pipefail\ninput=$(cat)\nETV_HOOK_FIRE_LIB="x"\n[ -r "$ETV_HOOK_FIRE_LIB" ] && . "$ETV_HOOK_FIRE_LIB" || true\netv_hook_fire_begin demo "" capture || true\n'
late = 'set -euo pipefail\ninput=$(cat)\nETV_HOOK_FIRE_LIB="x"\n[ -r "$ETV_HOOK_FIRE_LIB" ] && . "$ETV_HOOK_FIRE_LIB" || true\netv_hook_fire_begin demo "" capture || true\n' # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
faults = instrumentation_faults(late, "demo")
assert any("reads stdin before" in f for f in faults), (
"a begin call placed after `input=$(cat)` was accepted. It would record a fire with no "
@@ -257,15 +263,15 @@ def test_begin_placed_AFTER_the_stdin_read_is_DETECTED():
# issue `deny`. An A/B over silent allows proves transparency on the one path where there is nothing
# to be transparent about.
PAYLOADS = {
"bash-deny": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Bash","cwd":"%(cwd)s","tool_input":{"command":"ETV_UPDATE_GOLDENS=1 dotnet test"}}',
"bash-allow": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Bash","cwd":"%(cwd)s","tool_input":{"command":"ls -la"}}',
"git-commit": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Bash","cwd":"%(cwd)s","tool_input":{"command":"git commit -m x"}}',
"bash-deny": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Bash","cwd":"%(cwd)s","tool_input":{"command":"ETV_UPDATE_GOLDENS=1 dotnet test"}}', # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
"bash-allow": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Bash","cwd":"%(cwd)s","tool_input":{"command":"ls -la"}}', # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
"git-commit": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Bash","cwd":"%(cwd)s","tool_input":{"command":"git commit -m x"}}', # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
"nav-deny": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"mcp__x__navigate","cwd":"%(cwd)s","tool_input":{"url":"http://h/iptv/channels.m3u"}}',
"agent-no-model": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Agent","cwd":"%(cwd)s","tool_input":{"description":"d","prompt":"p"}}',
"agent-model": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Agent","cwd":"%(cwd)s","tool_input":{"description":"d","prompt":"p","model":"sonnet"}}',
"merge": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"mcp__gitea__pull_request_write","cwd":"%(cwd)s","tool_input":{"method":"merge","owner":"timothy","repo":"ersatztv","pull_number":1}}',
"worktree-add": '{"session_id":"s","hook_event_name":"PostToolUse","tool_name":"Bash","cwd":"%(cwd)s","tool_input":{"command":"git worktree add /tmp/nope-%(nonce)s HEAD"}}',
"ui-edit": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Edit","cwd":"%(cwd)s","tool_input":{"file_path":"web/src/screens/Channels.tsx"}}',
"agent-no-model": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Agent","cwd":"%(cwd)s","tool_input":{"description":"d","prompt":"p"}}', # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
"agent-model": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Agent","cwd":"%(cwd)s","tool_input":{"description":"d","prompt":"p","model":"sonnet"}}', # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
"merge": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"mcp__gitea__pull_request_write","cwd":"%(cwd)s","tool_input":{"method":"merge","owner":"timothy","repo":"ersatztv","pull_number":1}}', # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
"worktree-add": '{"session_id":"s","hook_event_name":"PostToolUse","tool_name":"Bash","cwd":"%(cwd)s","tool_input":{"command":"git worktree add /tmp/nope-%(nonce)s HEAD"}}', # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
"ui-edit": '{"session_id":"s","hook_event_name":"PreToolUse","tool_name":"Edit","cwd":"%(cwd)s","tool_input":{"file_path":"web/src/screens/Channels.tsx"}}', # noqa: E501 - one-line JSON/shell fixture; splitting it would change the payload
"empty": "",
"garbage": "not json at all",
}
@@ -300,17 +306,23 @@ def sandbox(tmp_path_factory):
def _run(script: Path, payload: str, env: dict, cwd: Path, arg: str | None = None):
cmd = ["bash", str(script)] + ([arg] if arg else [])
p = subprocess.run(
cmd, input=payload.encode(), capture_output=True, cwd=str(cwd), env=env, timeout=60
)
p = subprocess.run(cmd, input=payload.encode(), capture_output=True, cwd=str(cwd), env=env, timeout=60)
return p.returncode, p.stdout
def _git(cwd: Path, *args: str) -> None:
subprocess.run(
["git", *args], cwd=str(cwd), check=True, capture_output=True,
env={**os.environ, "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@e",
"GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@e"},
["git", *args],
cwd=str(cwd),
check=True,
capture_output=True,
env={
**os.environ,
"GIT_AUTHOR_NAME": "t",
"GIT_AUTHOR_EMAIL": "t@e",
"GIT_COMMITTER_NAME": "t",
"GIT_COMMITTER_EMAIL": "t@e",
},
)
@@ -328,18 +340,23 @@ def positives(sandbox, tmp_path_factory):
def pay(**kw) -> str:
import json as _json
return _json.dumps({"session_id": "me", "hook_event_name": "PreToolUse",
"tool_name": "Bash", **kw})
return _json.dumps({"session_id": "me", "hook_event_name": "PreToolUse", "tool_name": "Bash", **kw})
# --- worktree-guard: a marker naming ANOTHER session, on a `git commit` -------------------
wt = w / "sibling"
wt.mkdir()
_git(wt, "init", "-q", ".")
(wt / ".claude-worktree-owner").write_text("SOME-OTHER-SESSION\n")
cases["pretooluse-worktree-guard"] = [(
"foreign-marker-deny",
pay(cwd=str(wt), tool_input={"command": "git commit -m x"}), None, base_env, wt,
)]
cases["pretooluse-worktree-guard"] = [
(
"foreign-marker-deny",
pay(cwd=str(wt), tool_input={"command": "git commit -m x"}),
None,
base_env,
wt,
)
]
# --- bom-guard: repo path must match `*ersatztv*`, with a staged BOM-carrying .cs ----------
br = w / "ersatztv-scratch"
@@ -347,10 +364,15 @@ def positives(sandbox, tmp_path_factory):
_git(br, "init", "-q", ".")
(br / "Bad.cs").write_bytes(b"\xef\xbb\xbfclass A {}\n")
_git(br, "add", "Bad.cs")
cases["pretooluse-bom-guard"] = [(
"staged-bom-deny",
pay(cwd=str(br), tool_input={"command": "git commit -m x"}), None, base_env, br,
)]
cases["pretooluse-bom-guard"] = [
(
"staged-bom-deny",
pay(cwd=str(br), tool_input={"command": "git commit -m x"}),
None,
base_env,
br,
)
]
# --- agent-ram: both thresholds, via a stubbed `memory_pressure` ---------------------------
ram_cases = []
@@ -362,11 +384,15 @@ def positives(sandbox, tmp_path_factory):
stub.chmod(0o755)
env = dict(base_env)
env["PATH"] = f"{bindir}:{os.environ['PATH']}"
ram_cases.append((
f"free-{pct}pct-{label}",
pay(tool_name="Agent", cwd=str(w), tool_input={"prompt": "p", "model": "sonnet"}),
None, env, w,
))
ram_cases.append(
(
f"free-{pct}pct-{label}",
pay(tool_name="Agent", cwd=str(w), tool_input={"prompt": "p", "model": "sonnet"}),
None,
env,
w,
)
)
cases["pretooluse-agent-ram"] = ram_cases
# --- decisions-guard: a validator that prints and blocks, and one that prints and passes ---
@@ -409,12 +435,16 @@ def positives(sandbox, tmp_path_factory):
_git(ahead, "add", "-A")
_git(ahead, "commit", "-qm", "advance main")
_git(ahead, "push", "-q", "origin", "main")
head = subprocess.run(["git", "rev-parse", "HEAD"], cwd=str(behind),
capture_output=True, text=True).stdout.strip()
cases["prepush-rebase-check"] = [(
"behind-origin-main-blocks",
f"refs/heads/feat {head} refs/heads/feat {'0' * 40}\n", None, base_env, behind,
)]
head = subprocess.run(["git", "rev-parse", "HEAD"], cwd=str(behind), capture_output=True, text=True).stdout.strip()
cases["prepush-rebase-check"] = [
(
"behind-origin-main-blocks",
f"refs/heads/feat {head} refs/heads/feat {'0' * 40}\n",
None,
base_env,
behind,
)
]
# clean-worktree-check: a file both MODIFIED in the tree and present in the pushed set.
dirty = clone("dirty")
@@ -422,13 +452,16 @@ def positives(sandbox, tmp_path_factory):
_git(dirty, "add", "-A")
_git(dirty, "commit", "-qm", "change file")
(dirty / "file.txt").write_text("uncommitted change\n")
dhead = subprocess.run(["git", "rev-parse", "HEAD"], cwd=str(dirty),
capture_output=True, text=True).stdout.strip()
cases["prepush-clean-worktree-check"] = [(
"dirty-file-in-pushed-set-blocks",
f"refs/heads/main {dhead} refs/heads/main {'0' * 40}\n", None, base_env, dirty,
)]
dhead = subprocess.run(["git", "rev-parse", "HEAD"], cwd=str(dirty), capture_output=True, text=True).stdout.strip()
cases["prepush-clean-worktree-check"] = [
(
"dirty-file-in-pushed-set-blocks",
f"refs/heads/main {dhead} refs/heads/main {'0' * 40}\n",
None,
base_env,
dirty,
)
]
# --- prepush-donewhen: a push to main closing an issue with an unticked Done-when box -------
#
@@ -457,20 +490,23 @@ def positives(sandbox, tmp_path_factory):
threading.Thread(target=srv.serve_forever, daemon=True).start()
dw = clone("donewhen")
(dw / "src.cs").write_text("class A {}\n") # NOT docs-only, so the exemption does not apply
(dw / "src.cs").write_text("class A {}\n") # NOT docs-only, so the exemption does not apply
_git(dw, "add", "-A")
_git(dw, "commit", "-qm", "feat: thing\n\nfixes #999")
dw_head = subprocess.run(["git", "rev-parse", "HEAD"], cwd=str(dw),
capture_output=True, text=True).stdout.strip()
dw_prev = subprocess.run(["git", "rev-parse", "HEAD~1"], cwd=str(dw),
capture_output=True, text=True).stdout.strip()
dw_head = subprocess.run(["git", "rev-parse", "HEAD"], cwd=str(dw), capture_output=True, text=True).stdout.strip()
dw_prev = subprocess.run(["git", "rev-parse", "HEAD~1"], cwd=str(dw), capture_output=True, text=True).stdout.strip()
dw_env = dict(base_env)
dw_env["ETV_GITEA_URL"] = f"http://127.0.0.1:{srv.server_address[1]}"
dw_env["ETV_GITEA_BASICAUTH"] = "stub:stub"
cases["prepush-donewhen"] = [(
"unticked-donewhen-blocks-push-to-main",
f"refs/heads/main {dw_head} refs/heads/main {dw_prev}\n", None, dw_env, dw,
)]
cases["prepush-donewhen"] = [
(
"unticked-donewhen-blocks-push-to-main",
f"refs/heads/main {dw_head} refs/heads/main {dw_prev}\n",
None,
dw_env,
dw,
)
]
return cases
@@ -482,10 +518,15 @@ def _ab_cases(hook: Path, sandbox, positives) -> list[tuple]:
args = [None, "start", "finish"] if hook.stem == "design-sync-reminder" else [None]
for name, template in PAYLOADS.items():
for arg in args:
cases.append((
f"matrix:{name}:{arg}", template % {"cwd": str(root), "nonce": name},
arg, env, root,
))
cases.append(
(
f"matrix:{name}:{arg}",
template % {"cwd": str(root), "nonce": name},
arg,
env,
root,
)
)
for label, payload, arg, penv, cwd in positives.get(hook.stem, []):
cases.append((f"positive:{label}", payload, arg, penv, cwd))
return cases
@@ -513,8 +554,7 @@ def _ab_run(hook: Path, sandbox, case, tag_root: str):
def one(script: Path, t: str):
cmd = ["bash", str(script)] + ([arg] if arg else [])
p = subprocess.run(cmd, input=payload.encode(), capture_output=True,
cwd=str(cwd), env=side(t), timeout=90)
p = subprocess.run(cmd, input=payload.encode(), capture_output=True, cwd=str(cwd), env=side(t), timeout=90)
return p.returncode, p.stdout, p.stderr
return one(control, "control"), one(hook, "instrumented")
@@ -613,11 +653,16 @@ def test_the_worktree_markers_SIDE_EFFECT_is_unchanged(sandbox, tmp_path):
script = tmp_path / "control-marker.sh"
script.write_text(strip_instrumentation(hook.read_text()))
import json as _json
payload = _json.dumps({
"session_id": "SESSION-XYZ", "hook_event_name": "PostToolUse", "tool_name": "Bash",
"cwd": str(repo),
"tool_input": {"command": f"git worktree add {target} HEAD"},
})
payload = _json.dumps(
{
"session_id": "SESSION-XYZ",
"hook_event_name": "PostToolUse",
"tool_name": "Bash",
"cwd": str(repo),
"tool_input": {"command": f"git worktree add {target} HEAD"},
}
)
_git(repo, "worktree", "add", "-q", str(target), "HEAD")
_run(script, payload, env, repo)
marker = target / ".claude-worktree-owner"
@@ -641,15 +686,13 @@ def test_stdout_is_replayed_BYTE_EXACT(sandbox):
hook.write_text(
"#!/usr/bin/env bash\nset -euo pipefail\n"
f'. "{SINK}"\n'
"etv_hook_fire_begin trailing \"\" capture || true\n"
'etv_hook_fire_begin trailing "" capture || true\n'
"input=$(cat)\n"
r'printf "{\"hookSpecificOutput\":{\"permissionDecision\":\"deny\"}}\n\n"' + "\n"
)
rc, out = _run(hook, '{"tool_name":"Bash"}', env, root)
assert rc == 0
assert out == b'{"hookSpecificOutput":{"permissionDecision":"deny"}}\n\n', (
f"trailing bytes were altered: {out!r}"
)
assert out == b'{"hookSpecificOutput":{"permissionDecision":"deny"}}\n\n', f"trailing bytes were altered: {out!r}"
@pytest.mark.parametrize("code", [0, 1, 2, 3])
@@ -704,8 +747,12 @@ def test_a_TTY_stdin_is_not_slurped(sandbox):
master, slave = pty.openpty()
try:
p = subprocess.Popen(
["bash", str(hook)], stdin=slave, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, cwd=str(root), env=env,
["bash", str(hook)],
stdin=slave,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
cwd=str(root),
env=env,
)
os.close(slave)
try:
@@ -751,11 +798,11 @@ def test_logging_failure_does_not_break_the_hook(sandbox):
@pytest.mark.parametrize(
"emit,mode,expected",
[
(r'{\"hookSpecificOutput\":{\"permissionDecision\":\"deny\"}}', "capture", "deny"),
(r'{\"hookSpecificOutput\":{\"permissionDecision\":\"ask\"}}', "capture", "ask"),
(r'{\"hookSpecificOutput\":{\"permissionDecision\":\"allow\"}}', "capture", "allow"),
(r'{\"hookSpecificOutput\":{\"additionalContext\":\"hi\"}}', "capture", "context"),
(r'{\"decision\":\"block\",\"reason\":\"r\"}', "capture", "block"),
(r"{\"hookSpecificOutput\":{\"permissionDecision\":\"deny\"}}", "capture", "deny"),
(r"{\"hookSpecificOutput\":{\"permissionDecision\":\"ask\"}}", "capture", "ask"),
(r"{\"hookSpecificOutput\":{\"permissionDecision\":\"allow\"}}", "capture", "allow"),
(r"{\"hookSpecificOutput\":{\"additionalContext\":\"hi\"}}", "capture", "context"),
(r"{\"decision\":\"block\",\"reason\":\"r\"}", "capture", "block"),
("", "capture", "no-op"),
("", "stream", "pass"),
],
@@ -822,7 +869,10 @@ def test_the_report_names_hooks_that_NEVER_fired(sandbox):
)
p = subprocess.run(
["bash", str(SINK), "report", "--all", "--dir", str(logdir)],
capture_output=True, cwd=str(REPO_ROOT), env=env, timeout=60,
capture_output=True,
cwd=str(REPO_ROOT),
env=env,
timeout=60,
)
out = p.stdout.decode()
assert p.returncode == 0, p.stderr.decode()
@@ -859,7 +909,10 @@ def test_the_report_REFUSES_an_empty_population(tmp_path, sandbox):
runenv.pop("CLAUDE_PROJECT_DIR", None)
p = subprocess.run(
["bash", str(copied), "report", "--all", "--dir", str(tmp_path / "log")],
capture_output=True, cwd=str(tmp_path), env=runenv, timeout=60,
capture_output=True,
cwd=str(tmp_path),
env=runenv,
timeout=60,
)
assert p.returncode == 2, (
"the report exited 0 over an empty hook population. It must refuse rather than print a "
@@ -895,12 +948,11 @@ def test_stderr_is_NOT_silenced(sandbox):
'printf "IMPORTANT DIAGNOSTIC\\n" >&2\n'
"exit 1\n"
)
p = subprocess.run(["bash", str(hook)], input=b'{"tool_name":"Bash"}',
capture_output=True, cwd=str(root), env=env, timeout=60)
assert p.returncode == 1
assert b"IMPORTANT DIAGNOSTIC" in p.stderr, (
f"the hook's stderr was swallowed by the instrumentation: {p.stderr!r}"
p = subprocess.run(
["bash", str(hook)], input=b'{"tool_name":"Bash"}', capture_output=True, cwd=str(root), env=env, timeout=60
)
assert p.returncode == 1
assert b"IMPORTANT DIAGNOSTIC" in p.stderr, f"the hook's stderr was swallowed by the instrumentation: {p.stderr!r}"
def test_output_SURVIVES_a_vanished_stdout_tempfile(sandbox):
@@ -923,8 +975,14 @@ def test_output_SURVIVES_a_vanished_stdout_tempfile(sandbox):
)
tmp = root / "vanish-tmp"
tmp.mkdir(exist_ok=True)
p = subprocess.run(["bash", str(hook)], input=b'{"tool_name":"Bash"}', capture_output=True,
cwd=str(root), env={**env, "TMPDIR": str(tmp)}, timeout=60)
p = subprocess.run(
["bash", str(hook)],
input=b'{"tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env={**env, "TMPDIR": str(tmp)},
timeout=60,
)
assert p.returncode == 0
assert b'"permissionDecision":"deny"' in p.stdout, (
"the guard's deny was DISCARDED when its stdout scratch file vanished mid-run. The hook "
@@ -932,24 +990,22 @@ def test_output_SURVIVES_a_vanished_stdout_tempfile(sandbox):
)
@pytest.mark.parametrize(
"emit,code,expected",
[
# A NON-CANONICAL value is recorded as non-canonical, not laundered into a valid decision.
# This case went both ways before settling: first filed as unclassified prose, then
# lowercased into a clean `deny` (manufacturing a decision the harness may never honour).
(r'{\"permissionDecision\":\"Deny\"}', 0, "unrecognized"),
(r"{\"permissionDecision\":\"Deny\"}", 0, "unrecognized"),
# A non-zero status neither ERASES a printed decision nor annotates it. The status has its
# own field; the decision field states the decision and nothing else.
(r'{\"permissionDecision\":\"deny\"}', 1, "deny"),
(r"{\"permissionDecision\":\"deny\"}", 1, "deny"),
# A failure with nothing classifiable IS an error.
("", 1, "error"),
# Exit 2 is the harness's block channel and DOMINATES the printed JSON. Recording the
# printed `allow` would report a permit for a call that was actually refused — the one
# direction a log of security decisions must never be wrong in.
(r'{\"permissionDecision\":\"allow\"}', 2, "deny-exit2"),
(r"{\"permissionDecision\":\"allow\"}", 2, "deny-exit2"),
],
)
def test_the_classifier_does_not_MISREPORT(sandbox, emit, code, expected):
@@ -961,12 +1017,16 @@ def test_the_classifier_does_not_MISREPORT(sandbox, emit, code, expected):
"#!/usr/bin/env bash\nset -uo pipefail\n"
f'. "{SINK}"\n'
'etv_hook_fire_begin cls "" capture || true\n'
"input=$(cat)\n"
+ (f'printf "{emit}"\n' if emit else "")
+ f"exit {code}\n"
"input=$(cat)\n" + (f'printf "{emit}"\n' if emit else "") + f"exit {code}\n"
)
subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env=runenv,
timeout=60,
)
subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(root), env=runenv, timeout=60)
rec = [ln for ln in (logdir / "s1.jsonl").read_text().splitlines() if '"phase":"exit"' in ln]
assert f'"decision":"{expected}"' in rec[0], f"expected {expected}, got {rec[0]}"
@@ -983,9 +1043,14 @@ def test_the_word_additionalContext_in_PROSE_is_not_a_decision(sandbox):
"input=$(cat)\n"
'printf "note: this hook does not use additionalContext at all\\n"\n'
)
subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)}, timeout=60)
subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=60,
)
rec = [ln for ln in (logdir / "s1.jsonl").read_text().splitlines() if '"phase":"exit"' in ln]
assert '"decision":"output"' in rec[0], f"prose was classified as a decision: {rec[0]}"
@@ -1001,14 +1066,17 @@ def test_the_session_id_cannot_ESCAPE_the_log_directory(sandbox, tmp_path):
logdir = tmp_path / "logs"
hook = tmp_path / "esc.sh"
hook.write_text(
"#!/usr/bin/env bash\nset -uo pipefail\n"
f'. "{SINK}"\n'
'etv_hook_fire_begin esc "" capture || true\n'
"input=$(cat)\n"
f'#!/usr/bin/env bash\nset -uo pipefail\n. "{SINK}"\netv_hook_fire_begin esc "" capture || true\ninput=$(cat)\n'
)
evil = '{"session_id":"../../escaped","tool_name":"Bash"}'
subprocess.run(["bash", str(hook)], input=evil.encode(), capture_output=True,
cwd=str(tmp_path), env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)}, timeout=60)
subprocess.run(
["bash", str(hook)],
input=evil.encode(),
capture_output=True,
cwd=str(tmp_path),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=60,
)
written = list(logdir.glob("*.jsonl"))
assert written, "nothing was logged at all"
for f in written:
@@ -1040,8 +1108,14 @@ def test_the_suite_does_not_write_to_the_PRODUCTION_log(sandbox):
'etv_hook_fire_begin prodcheck "" capture || true\n'
"input=$(cat)\n"
)
subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(root), env=env, timeout=60)
subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env=env,
timeout=60,
)
after = sorted(p.stat().st_mtime_ns for p in default.glob("*.jsonl")) if default.exists() else []
assert before == after, "a test run modified the production hook-fire log"
@@ -1075,16 +1149,17 @@ def test_DELETING_the_replay_makes_the_differential_go_RED(sandbox, positives, t
"input=$(cat)\n"
r'printf "{\"hookSpecificOutput\":{\"permissionDecision\":\"deny\"}}"' + "\n"
)
p = subprocess.run(["bash", str(hook)], input=b'{"tool_name":"Bash"}', capture_output=True,
cwd=str(root), env={**env, "TMPDIR": str(tmp_path)}, timeout=60)
assert p.stdout == b"", (
"the mutation did not actually disarm the replay, so this proof establishes nothing: "
f"stdout={p.stdout!r}"
p = subprocess.run(
["bash", str(hook)],
input=b'{"tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env={**env, "TMPDIR": str(tmp_path)},
timeout=60,
)
assert p.stdout == b"", (
f"the mutation did not actually disarm the replay, so this proof establishes nothing: stdout={p.stdout!r}"
)
def test_no_exec_in_the_sink_carries_a_STDERR_REDIRECT():
@@ -1123,17 +1198,20 @@ def test_the_log_and_the_REPLAY_agree_when_the_hook_uses_fd_4(sandbox):
f'. "{SINK}"\n'
'etv_hook_fire_begin fd4 "" capture || true\n'
"input=$(cat)\n"
'exec 4</dev/null\n' # the hook takes fd 4 for itself
"exec 4</dev/null\n" # the hook takes fd 4 for itself
r'printf "{\"hookSpecificOutput\":{\"permissionDecision\":\"deny\"}}"' + "\n"
)
p = subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)}, timeout=60)
p = subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=60,
)
assert b'"permissionDecision":"deny"' in p.stdout, f"replay lost the decision: {p.stdout!r}"
exits = [r for r in (logdir / "s1.jsonl").read_text().splitlines() if '"phase":"exit"' in r]
assert '"decision":"deny"' in exits[0], (
f"the harness saw a deny but the log recorded something else: {exits[0]}"
)
assert '"decision":"deny"' in exits[0], f"the harness saw a deny but the log recorded something else: {exits[0]}"
def test_an_UNWRITABLE_log_file_is_still_stderr_transparent(sandbox, tmp_path):
@@ -1160,9 +1238,14 @@ def test_an_UNWRITABLE_log_file_is_still_stderr_transparent(sandbox, tmp_path):
"input=$(cat)\n"
r'printf "{\"hookSpecificOutput\":{\"permissionDecision\":\"deny\"}}"' + "\n"
)
p = subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(tmp_path),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)}, timeout=60)
p = subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(tmp_path),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=60,
)
assert p.returncode == 0
assert p.stdout == b'{"hookSpecificOutput":{"permissionDecision":"deny"}}'
assert p.stderr == b"", (
@@ -1171,10 +1254,6 @@ def test_an_UNWRITABLE_log_file_is_still_stderr_transparent(sandbox, tmp_path):
)
def test_NUL_bytes_in_hook_output_survive(sandbox):
"""A shell variable cannot hold a NUL, so replaying through `$(...)` silently drops them.
@@ -1192,8 +1271,9 @@ def test_NUL_bytes_in_hook_output_survive(sandbox):
"input=$(cat)\n"
r"printf 'a\000b\n'" + "\n"
)
p = subprocess.run(["bash", str(hook)], input=b'{"tool_name":"Bash"}', capture_output=True,
cwd=str(root), env=env, timeout=60)
p = subprocess.run(
["bash", str(hook)], input=b'{"tool_name":"Bash"}', capture_output=True, cwd=str(root), env=env, timeout=60
)
assert p.stdout == b"a\x00b\n", f"NUL-containing output was mangled: {p.stdout!r}"
assert b"null byte" not in p.stderr, f"a warning leaked to the harness: {p.stderr!r}"
@@ -1202,8 +1282,8 @@ def test_NUL_bytes_in_hook_output_survive(sandbox):
"emit,code,expected",
[
# Every non-canonical value, not just the ones a restricted character class admits.
(r'{\"permissionDecision\":\"deny2\"}', 0, "unrecognized"),
(r'{\"permissionDecision\":\"deny_now\"}', 0, "unrecognized"),
(r"{\"permissionDecision\":\"deny2\"}", 0, "unrecognized"),
(r"{\"permissionDecision\":\"deny_now\"}", 0, "unrecognized"),
# Unclassified output plus a FAILING exit is an error, not `output`: the report histograms
# the decision, so filing it as `output` hid the failure entirely.
("diagnostic text", 1, "error"),
@@ -1217,13 +1297,16 @@ def test_odd_values_and_failing_exits_are_not_LAUNDERED(sandbox, emit, code, exp
"#!/usr/bin/env bash\nset -uo pipefail\n"
f'. "{SINK}"\n'
'etv_hook_fire_begin odd "" capture || true\n'
"input=$(cat)\n"
+ (f'printf "{emit}"\n' if emit else "")
+ f"exit {code}\n"
"input=$(cat)\n" + (f'printf "{emit}"\n' if emit else "") + f"exit {code}\n"
)
subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=60,
)
subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)}, timeout=60)
rec = [r for r in (logdir / "s1.jsonl").read_text().splitlines() if '"phase":"exit"' in r]
assert f'"decision":"{expected}"' in rec[0], f"expected {expected}, got {rec[0]}"
@@ -1247,9 +1330,12 @@ def test_an_INHERITED_flushed_flag_does_not_disable_reporting(sandbox):
r'printf "{\"hookSpecificOutput\":{\"permissionDecision\":\"deny\"}}"' + "\n"
)
p = subprocess.run(
["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}', capture_output=True,
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir), "ETV_HOOK_FIRE_FLUSHED": "1"}, timeout=60,
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir), "ETV_HOOK_FIRE_FLUSHED": "1"},
timeout=60,
)
assert p.stdout == b'{"hookSpecificOutput":{"permissionDecision":"deny"}}', (
f"an inherited FLUSHED=1 left stdout redirected and swallowed the decision: {p.stdout!r}"
@@ -1284,9 +1370,7 @@ def test_a_SIGNALLED_hook_behaves_EXACTLY_as_an_uninstrumented_one(sandbox, sign
)
instrumented = root / f"sigab-{signame}.sh"
instrumented.write_text(
"#!/usr/bin/env bash\nset -uo pipefail\n"
f'. "{SINK}"\n'
f'etv_hook_fire_begin sigab "" stream || true\n' + body
f'#!/usr/bin/env bash\nset -uo pipefail\n. "{SINK}"\netv_hook_fire_begin sigab "" stream || true\n' + body
)
control = root / f"sigab-control-{signame}.sh"
control.write_text("#!/usr/bin/env bash\nset -uo pipefail\n" + body)
@@ -1302,9 +1386,15 @@ def test_a_SIGNALLED_hook_behaves_EXACTLY_as_an_uninstrumented_one(sandbox, sign
# anything. Measured: pid-only gives control 4.0s and trapped 4.1s; group signalling
# gives control 0.002s and trapped 0.054s. A supervisor kills the group, so this is
# also the shape that actually occurs.
p = subprocess.Popen(["bash", str(script)], stdin=fh, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=str(root), env=env,
start_new_session=True)
p = subprocess.Popen(
["bash", str(script)],
stdin=fh,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=str(root),
env=env,
start_new_session=True,
)
time.sleep(1.0)
t0 = time.monotonic()
os.killpg(os.getpgid(p.pid), sig)
@@ -1312,7 +1402,8 @@ def test_a_SIGNALLED_hook_behaves_EXACTLY_as_an_uninstrumented_one(sandbox, sign
results[tag] = (p.returncode, time.monotonic() - t0, out, err)
(rc_a, dt_a, out_a, err_a), (rc_b, dt_b, out_b, err_b) = (
results["control"], results["instrumented"],
results["control"],
results["instrumented"],
)
assert out_a == out_b, f"SIG{signame}: stdout differs under signal: {out_a!r} vs {out_b!r}"
@@ -1329,13 +1420,11 @@ def test_a_SIGNALLED_hook_behaves_EXACTLY_as_an_uninstrumented_one(sandbox, sign
# require one of bash's four signal words followed by `:` or whitespace, so an ordinary
# diagnostic still fails the comparison.
return b"\n".join(
ln for ln in e.split(b"\n")
if not re.search(rb"(^|\s)(Terminated|Hangup|Interrupt|Killed)(:|\s|$)", ln)
ln for ln in e.split(b"\n") if not re.search(rb"(^|\s)(Terminated|Hangup|Interrupt|Killed)(:|\s|$)", ln)
)
assert strip_jobnotice(err_a) == strip_jobnotice(err_b), (
f"SIG{signame}: stderr differs under signal beyond bash's job-control notice: "
f"{err_a!r} vs {err_b!r}"
f"SIG{signame}: stderr differs under signal beyond bash's job-control notice: {err_a!r} vs {err_b!r}"
)
assert rc_a == rc_b, (
f"SIG{signame}: exit status differs, control={rc_a} instrumented={rc_b}. git and the "
@@ -1348,14 +1437,15 @@ def test_a_SIGNALLED_hook_behaves_EXACTLY_as_an_uninstrumented_one(sandbox, sign
)
@pytest.mark.parametrize("locale_env", [
{"LANG": "en_US.UTF-8"},
{"LC_CTYPE": "en_US.UTF-8"},
{"LC_CTYPE": "UTF-8"}, # macOS Terminal's default, and the case that broke the fix
{"LC_ALL": "en_US.UTF-8"},
])
@pytest.mark.parametrize(
"locale_env",
[
{"LANG": "en_US.UTF-8"},
{"LC_CTYPE": "en_US.UTF-8"},
{"LC_CTYPE": "UTF-8"}, # macOS Terminal's default, and the case that broke the fix
{"LC_ALL": "en_US.UTF-8"},
],
)
def test_an_INVALID_UTF8_byte_in_a_decision_is_still_classified(tmp_path, locale_env):
"""`local LC_ALL=C` does not export, so the child `sed`/`tr` never saw it — the fix was INERT.
@@ -1387,10 +1477,15 @@ def test_an_INVALID_UTF8_byte_in_a_decision_is_still_classified(tmp_path, locale
'printf \'{"hookSpecificOutput":{"permissionDecision":"deny",'
'"permissionDecisionReason":"caf\\xe9"}}\'\n'
)
env = {"PATH": os.environ["PATH"], "HOME": os.environ["HOME"],
"ETV_HOOK_FIRE_LOG_DIR": str(logdir), **locale_env}
p = subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(tmp_path), env=env, timeout=60)
env = {"PATH": os.environ["PATH"], "HOME": os.environ["HOME"], "ETV_HOOK_FIRE_LOG_DIR": str(logdir), **locale_env}
p = subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(tmp_path),
env=env,
timeout=60,
)
assert p.stderr == b"", f"{locale_env}: a locale diagnostic leaked to the harness: {p.stderr!r}"
record = (logdir / "s1.jsonl").read_text()
@@ -1419,16 +1514,24 @@ def test_a_NESTED_identity_field_does_not_outrank_the_TOP_LEVEL_one(tmp_path):
"input=$(cat)\n"
)
import json as _json
payload = _json.dumps({
"session_id": "TOP-SESSION",
"hook_event_name": "PreToolUse",
"tool_name": "TopTool",
"tool_input": {"filler": "x" * 1000},
"tool_response": {"session_id": "NESTED-SESSION", "tool_name": "NestedTool"},
})
subprocess.run(["bash", str(hook)], input=payload.encode(), capture_output=True,
cwd=str(tmp_path), env={**os.environ, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=60)
payload = _json.dumps(
{
"session_id": "TOP-SESSION",
"hook_event_name": "PreToolUse",
"tool_name": "TopTool",
"tool_input": {"filler": "x" * 1000},
"tool_response": {"session_id": "NESTED-SESSION", "tool_name": "NestedTool"},
}
)
subprocess.run(
["bash", str(hook)],
input=payload.encode(),
capture_output=True,
cwd=str(tmp_path),
env={**os.environ, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=60,
)
assert (logdir / "TOP-SESSION.jsonl").exists(), (
f"records filed under the wrong session: {[f.name for f in logdir.glob('*.jsonl')]}"
@@ -1451,9 +1554,14 @@ def test_a_PRESENT_but_empty_decision_is_not_laundered(sandbox):
"input=$(cat)\n"
r'printf "{\"hookSpecificOutput\":{\"permissionDecision\":\"\"}}"' + "\n"
)
subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)}, timeout=60)
subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env={**env, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=60,
)
rec = [r for r in (logdir / "s1.jsonl").read_text().splitlines() if '"phase":"exit"' in r]
assert '"decision":"unrecognized"' in rec[0], f"an empty decision was laundered: {rec[0]}"
@@ -1473,21 +1581,25 @@ def test_identity_fields_BEYOND_the_fast_path_cap_are_still_found(tmp_path):
hook = tmp_path / "big.sh"
hook.write_text(
"#!/usr/bin/env bash\nset -uo pipefail\n"
f'. "{SINK}"\n'
'etv_hook_fire_begin big "" capture || true\n'
"input=$(cat)\n"
f'#!/usr/bin/env bash\nset -uo pipefail\n. "{SINK}"\netv_hook_fire_begin big "" capture || true\ninput=$(cat)\n'
)
logdir = tmp_path / "biglog"
payload = _json.dumps({
"tool_input": {"filler": "x" * 400_000},
"session_id": "TOP-SESSION",
"hook_event_name": "PreToolUse",
"tool_name": "TopTool",
})
subprocess.run(["bash", str(hook)], input=payload.encode(), capture_output=True,
cwd=str(tmp_path), env={**os.environ, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=90)
payload = _json.dumps(
{
"tool_input": {"filler": "x" * 400_000},
"session_id": "TOP-SESSION",
"hook_event_name": "PreToolUse",
"tool_name": "TopTool",
}
)
subprocess.run(
["bash", str(hook)],
input=payload.encode(),
capture_output=True,
cwd=str(tmp_path),
env={**os.environ, "ETV_HOOK_FIRE_LOG_DIR": str(logdir)},
timeout=90,
)
assert (logdir / "TOP-SESSION.jsonl").exists(), (
"identity beyond the fast-path cap was not found, so the fire filed under "
@@ -1510,11 +1622,17 @@ def test_the_field_helper_does_not_LEAK_into_the_hooks_namespace(sandbox):
f'. "{SINK}"\n'
'etv_hook_fire_begin ns "" capture || true\n'
"input=$(cat)\n"
'if declare -F _etv_field >/dev/null 2>&1; then echo LEAKED_etv_field; fi\n'
'if declare -F etv_hook_fire__field >/dev/null 2>&1; then echo LEAKED_namespaced; fi\n'
"if declare -F _etv_field >/dev/null 2>&1; then echo LEAKED_etv_field; fi\n"
"if declare -F etv_hook_fire__field >/dev/null 2>&1; then echo LEAKED_namespaced; fi\n"
"echo CLEAN\n"
)
p = subprocess.run(["bash", str(hook)], input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True, cwd=str(root), env=env, timeout=60)
p = subprocess.run(
["bash", str(hook)],
input=b'{"session_id":"s1","tool_name":"Bash"}',
capture_output=True,
cwd=str(root),
env=env,
timeout=60,
)
assert b"LEAKED" not in p.stdout, f"a helper leaked into the hook's namespace: {p.stdout!r}"
assert b"CLEAN" in p.stdout
+72 -58
View File
@@ -51,12 +51,12 @@ def preflight(tmp_path):
so every test passed.
"""
shim = bindir / "jq"
body = "#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then\n"
body = '#!/bin/sh\nif [ "$1" = "--version" ]; then\n'
if version_line:
body += ' printf "%%s\\n" %s\n' % _shq(version_line)
body += f' printf "%s\\n" {_shq(version_line)}\n'
if stderr:
body += ' printf "%%s\\n" %s >&2\n' % _shq(stderr)
body += " exit %d\nfi\nexit 0\n" % exit_code
body += f' printf "%s\\n" {_shq(stderr)} >&2\n'
body += f" exit {exit_code}\nfi\nexit 0\n"
shim.write_text(body)
shim.chmod(0o755)
@@ -73,8 +73,7 @@ def preflight(tmp_path):
# but jq itself (`command -v` is a builtin, and bash is invoked by absolute path), so
# there is nothing to keep.
env["PATH"] = str(bindir)
return subprocess.run([BASH, str(SCRIPT), *args],
env=env, capture_output=True, text=True)
return subprocess.run([BASH, str(SCRIPT), *args], env=env, capture_output=True, text=True)
def run_bytes(self, *args):
"""Same, but WITHOUT text mode.
@@ -87,8 +86,7 @@ def preflight(tmp_path):
"""
env = dict(os.environ)
env["PATH"] = str(bindir)
return subprocess.run([BASH, str(SCRIPT), *args],
env=env, capture_output=True)
return subprocess.run([BASH, str(SCRIPT), *args], env=env, capture_output=True)
return Handle()
@@ -172,11 +170,15 @@ def test_expect_without_a_value_is_a_usage_error_WITH_output(preflight):
# the script exited 0 having asserted NOTHING. That is this script's own stated failure mode,
# reproduced inside itself, which is why these cases are pinned rather than left to inspection.
@pytest.mark.parametrize("version_line", [
"jq version 1.6", # some distro wrappers print this form
"JQ-1.6",
"jq-1.6-dirty",
])
@pytest.mark.parametrize(
"version_line",
[
"jq version 1.6", # some distro wrappers print this form
"JQ-1.6",
"jq-1.6-dirty",
],
)
def test_unusual_but_parseable_version_forms_are_accepted(preflight, version_line):
preflight.with_jq(version_line)
r = preflight.run()
@@ -190,7 +192,8 @@ def test_unparseable_version_fails_CLOSED_rather_than_asserting_nothing(prefligh
r = preflight.run()
assert r.returncode == 1, (
f"{version_line!r} exited {r.returncode}: an unparsed version must never reach — or "
"silently skip — the floor assertion")
"silently skip — the floor assertion"
)
assert "could not parse" in r.stderr
@@ -202,20 +205,21 @@ def test_a_jq_that_cannot_START_fails_closed(preflight):
discarding the exit status, so that message became the parse input, `2.34` matched, and the floor
was certified green on a jq that cannot run at all.
"""
preflight.with_jq(
"", stderr="jq: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found",
exit_code=127)
preflight.with_jq("", stderr="jq: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found", exit_code=127)
r = preflight.run()
assert r.returncode == 1
assert "cannot run" in r.stderr
assert "parsed 2.34" not in r.stdout, "stderr must never be parsed as a version"
@pytest.mark.parametrize("version_line", [
"warning: something 3.14", # a noise line carrying a plausible number
"2026.07.26 jq-1.6", # a date prefix, which outranks the real version if unanchored
"jq-master-v0.0.0-1.6",
])
@pytest.mark.parametrize(
"version_line",
[
"warning: something 3.14", # a noise line carrying a plausible number
"2026.07.26 jq-1.6", # a date prefix, which outranks the real version if unanchored
"jq-master-v0.0.0-1.6",
],
)
def test_a_number_that_is_not_the_VERSION_is_not_accepted_as_one(preflight, version_line):
"""Matching the first `<digits>.<digits>` ANYWHERE let a prefix win over the real version.
`2026.07.26 jq-1.6` parsed as 2026.07 and sailed over the floor. The pattern is anchored to the
@@ -226,10 +230,13 @@ def test_a_number_that_is_not_the_VERSION_is_not_accepted_as_one(preflight, vers
assert "could not parse" in r.stderr
@pytest.mark.parametrize("version_line", [
"jq-99999999999999999999999.0",
"jq-1.99999999999999999999999",
])
@pytest.mark.parametrize(
"version_line",
[
"jq-99999999999999999999999.0",
"jq-1.99999999999999999999999",
],
)
def test_an_OUT_OF_RANGE_digit_run_fails_closed(preflight, version_line):
"""The round-1 fail-open mechanism, resurrected via an over-long number.
@@ -247,25 +254,28 @@ def test_an_OUT_OF_RANGE_digit_run_fails_closed(preflight, version_line):
assert r.returncode == 1, f"{version_line!r} exited 0 — the floor was not asserted"
@pytest.mark.parametrize("version_line", [
# Killed by the SEPARATOR restriction (a blank separator must be followed by `version`).
"jq\n2.34: cannot load shared library",
"jq\n\n\n99.9",
"jq -- 2.34 (real jq-1.6)",
"jq\t\t9.9",
# Killed ONLY by the first-line slice + `[[:blank:]]`. These carry the literal word `version`,
# so the separator restriction is satisfied and cannot save us — the newline must be excluded
# from the separator class AND the parse confined to line one.
#
# Without these, a round-5 mutation check found that reverting BOTH of those changes together
# (`[[:blank:]]`→`[[:space:]]` and parsing `$raw` instead of `$first`) left the whole suite
# GREEN: the four cases above are all killed by the separator alone, so they attributed the fix
# to the wrong layer. A test that passes for the wrong reason is how the previous three rounds
# each shipped a defect.
"jq\nversion\n9.9",
"jq\nversion 9.9",
"jq \n version \n 9.9",
])
@pytest.mark.parametrize(
"version_line",
[
# Killed by the SEPARATOR restriction (a blank separator must be followed by `version`).
"jq\n2.34: cannot load shared library",
"jq\n\n\n99.9",
"jq -- 2.34 (real jq-1.6)",
"jq\t\t9.9",
# Killed ONLY by the first-line slice + `[[:blank:]]`. These carry the literal word `version`,
# so the separator restriction is satisfied and cannot save us — the newline must be excluded
# from the separator class AND the parse confined to line one.
#
# Without these, a round-5 mutation check found that reverting BOTH of those changes together
# (`[[:blank:]]`→`[[:space:]]` and parsing `$raw` instead of `$first`) left the whole suite
# GREEN: the four cases above are all killed by the separator alone, so they attributed the fix
# to the wrong layer. A test that passes for the wrong reason is how the previous three rounds
# each shipped a defect.
"jq\nversion\n9.9",
"jq\nversion 9.9",
"jq \n version \n 9.9",
],
)
def test_a_number_AFTER_the_jq_token_is_not_reachable_across_filler(preflight, version_line):
"""Two independent layers keep a stray number from being read as the version, and both are
pinned here: the separator must be one of the forms real jq emits (`jq-1.6` / `jq version 1.6`),
@@ -306,14 +316,17 @@ def test_the_observability_line_stays_on_ONE_line(preflight):
assert "trailing noise" not in r.stdout
@pytest.mark.parametrize("version_line,expected", [
("jq-1.6 (Debian 1.6-2.1)", "1.6"), # distro packaging suffix
("jq-1.10", "1.10"), # two-digit minor: must compare numerically, not lexically
("jq-1.7.1", "1.7"),
("jq-1.6.0", "1.6"),
("jq-v1.6", "1.6"),
("JQ-1.6", "1.6"),
])
@pytest.mark.parametrize(
"version_line,expected",
[
("jq-1.6 (Debian 1.6-2.1)", "1.6"), # distro packaging suffix
("jq-1.10", "1.10"), # two-digit minor: must compare numerically, not lexically
("jq-1.7.1", "1.7"),
("jq-1.6.0", "1.6"),
("jq-v1.6", "1.6"),
("JQ-1.6", "1.6"),
],
)
def test_legitimate_forms_still_parse_to_the_right_version(preflight, version_line, expected):
preflight.with_jq(version_line)
r = preflight.run()
@@ -323,6 +336,7 @@ def test_legitimate_forms_still_parse_to_the_right_version(preflight, version_li
# --- Wiring guards: the preflight is worthless if a caller silently stops running it ------------
def test_script_tests_pins_the_jq_version():
"""The pin is the tripwire, so its presence is asserted rather than merely commented.
@@ -333,8 +347,7 @@ def test_script_tests_pins_the_jq_version():
is `main`, which does not yet contain `scripts/jq-preflight.sh`.
"""
pr_checks = (WORKFLOWS / "pr-checks.yml").read_text()
assert "jq-preflight.sh --expect" in pr_checks, \
"script-tests must pin the jq version — that pin is the tripwire"
assert "jq-preflight.sh --expect" in pr_checks, "script-tests must pin the jq version — that pin is the tripwire"
def test_review_verdict_never_pins_a_jq_version():
@@ -344,6 +357,7 @@ def test_review_verdict_never_pins_a_jq_version():
follow-up PR adds the floor-only call — rather than being a comment someone can miss.
"""
review_verdict = (WORKFLOWS / "review-verdict.yml").read_text()
assert "jq-preflight.sh --expect" not in review_verdict, \
("review-verdict.yml must NOT pin a jq version: it writes the required review-verdict/h10 "
"status, so a pin would deadlock every merge on a jq bump (ersatztv#648)")
assert "jq-preflight.sh --expect" not in review_verdict, (
"review-verdict.yml must NOT pin a jq version: it writes the required review-verdict/h10 "
"status, so a pin would deadlock every merge on a jq bump (ersatztv#648)"
)
+25 -20
View File
@@ -33,7 +33,7 @@ SHA = "a9e3e23abf337980ca4c05854f5b1e210099d08b"
# The PR is deliberately NOT docs-only: the docs-only exemption short-circuits the whole gate, so a
# docs PR would never reach the base check and the tests would pass without exercising it.
CURL_SHIM = r'''#!/usr/bin/env python3
CURL_SHIM = r"""#!/usr/bin/env python3
import json, os, sys, pathlib, urllib.parse
state = pathlib.Path(os.environ["STUB_DIR"])
@@ -75,14 +75,18 @@ if "/pulls/" in url:
sys.exit(0)
print("{}")
'''
"""
@pytest.fixture
def hook(tmp_path):
bindir = tmp_path / "bin"; bindir.mkdir()
curl = bindir / "curl"; curl.write_text(CURL_SHIM); curl.chmod(0o755)
state = tmp_path / "state"; state.mkdir()
bindir = tmp_path / "bin"
bindir.mkdir()
curl = bindir / "curl"
curl.write_text(CURL_SHIM)
curl.chmod(0o755)
state = tmp_path / "state"
state.mkdir()
(state / "live_base").write_text("main")
(state / "verdict_desc").write_text("Review-verdict: MERGEABLE @ a9e3e23 (base: main)")
@@ -103,10 +107,8 @@ def hook(tmp_path):
(state / "verdict_desc").write_text(desc)
def decision(self):
payload = {"tool_input": {"method": "merge", "owner": "timothy",
"repo": "ersatztv", "pull_number": 42}}
r = subprocess.run(["bash", str(HOOK)], input=json.dumps(payload),
env=env, capture_output=True, text=True)
payload = {"tool_input": {"method": "merge", "owner": "timothy", "repo": "ersatztv", "pull_number": 42}}
r = subprocess.run(["bash", str(HOOK)], input=json.dumps(payload), env=env, capture_output=True, text=True)
assert r.returncode == 0, r.stderr
if not r.stdout.strip():
return None
@@ -124,21 +126,24 @@ def test_a_retargeted_base_denies_a_verdict_formed_against_the_old_one(hook):
reason = hook.reason()
assert "deny" in reason, "a verdict formed against a different base was allowed to stand"
assert "release/26.4" in reason and "main" in reason, (
"the deny must name both bases; a reader cannot act on 'the base changed'")
"the deny must name both bases; a reader cannot act on 'the base changed'"
)
def test_positive_control_an_unchanged_base_does_not_trigger_the_base_deny(hook):
"""Without this, the test above could pass because the hook denies on every path — which it
very nearly does, since this PR is non-docs and the rest of the gate is unstubbed."""
reason = hook.reason()
assert "ersatztv#632" not in reason, (
"the base check fired on a PR whose base never moved")
assert "ersatztv#632" not in reason, "the base check fired on a PR whose base never moved"
@pytest.mark.parametrize("desc", [
"Review-verdict: MERGEABLE @ a9e3e23", # posted before #632
"NONE", # no verdict status on this head at all
])
@pytest.mark.parametrize(
"desc",
[
"Review-verdict: MERGEABLE @ a9e3e23", # posted before #632
"NONE", # no verdict status on this head at all
],
)
def test_a_verdict_with_no_recorded_base_gets_no_opinion(hook, desc):
"""Graceful adoption. Denying here would block every in-flight PR the day this lands, and the
window closes on its own: verdicts are per-head and short-lived, so every verdict posted after
@@ -151,7 +156,8 @@ def test_a_verdict_with_no_recorded_base_gets_no_opinion(hook, desc):
hook.set_live_base("release/26.4")
hook.set_verdict_description(desc)
assert "base" not in hook.reason(), (
"a pre-#632 verdict drew a base-related decision for a field it could not have carried")
"a pre-#632 verdict drew a base-related decision for a field it could not have carried"
)
@pytest.mark.parametrize("failure", ["SCALAR-ROW", "NONSTRING-DESC"])
@@ -173,7 +179,7 @@ def test_a_malformed_status_MEMBER_asks_too(hook, failure):
@pytest.mark.parametrize("failure", ["TRANSPORT-ERROR", "GARBAGE"])
def test_an_UNREADABLE_status_response_asks_rather_than_skipping_the_check(hook, failure):
""""Could not check" is a third outcome, not a quiet synonym for "no base recorded".
""" "Could not check" is a third outcome, not a quiet synonym for "no base recorded".
The first draft collapsed the two: an unreadable status response produced an empty
`recorded_base`, took the graceful-adoption path, and skipped validation in silence — after
@@ -202,5 +208,4 @@ def test_the_comparator_is_the_base_REF_not_its_tip_sha():
A base branch that merely ADVANCES must be silent here; rebasing onto it moves the head sha,
which the per-sha binding already covers."""
assert ".base.ref" in HOOK.read_text(), "the hook must compare the base BRANCH, not its tip sha"
assert ".base.sha" not in HOOK.read_text(), (
"comparing base.sha deadlocks every open PR whenever main advances")
assert ".base.sha" not in HOOK.read_text(), "comparing base.sha deadlocks every open PR whenever main advances"
+62 -50
View File
@@ -33,7 +33,7 @@ HOOK = REPO_ROOT / ".claude" / "hooks" / "pretooluse-merge-consent.sh"
SHA = "a9e3e23abf337980ca4c05854f5b1e210099d08b"
# Serves paged `pulls/N/files`, plus the minimal PR object the hook reads first.
CURL_SHIM = r'''#!/usr/bin/env python3
CURL_SHIM = r"""#!/usr/bin/env python3
import json, os, sys, pathlib, urllib.parse
state = pathlib.Path(os.environ["STUB_DIR"])
@@ -88,7 +88,7 @@ if "/pulls/" in url:
sys.exit(0)
print("{}")
'''
"""
def _rows(paths):
@@ -97,9 +97,13 @@ def _rows(paths):
@pytest.fixture
def hook(tmp_path):
bindir = tmp_path / "bin"; bindir.mkdir()
shim = bindir / "curl"; shim.write_text(CURL_SHIM); shim.chmod(0o755)
state = tmp_path / "state"; state.mkdir()
bindir = tmp_path / "bin"
bindir.mkdir()
shim = bindir / "curl"
shim.write_text(CURL_SHIM)
shim.chmod(0o755)
state = tmp_path / "state"
state.mkdir()
env = dict(os.environ)
env["PATH"] = f"{bindir}{os.pathsep}{env['PATH']}"
@@ -114,10 +118,10 @@ def hook(tmp_path):
(state / "pages.json").write_text(json.dumps(list(pages)))
def run(self):
payload = {"tool_input": {"method": "merge", "owner": "timothy",
"repo": "ersatztv", "pull_number": 42}}
return subprocess.run(["bash", str(HOOK)], input=json.dumps(payload),
env=env, capture_output=True, text=True)
payload = {"tool_input": {"method": "merge", "owner": "timothy", "repo": "ersatztv", "pull_number": 42}}
return subprocess.run(
["bash", str(HOOK)], input=json.dumps(payload), env=env, capture_output=True, text=True
)
def exempted(self):
"""Exempt == passthrough == exit 0 with no decision JSON."""
@@ -140,16 +144,14 @@ def test_code_pr_is_not_exempt(hook):
def test_protected_path_on_a_LATER_page_is_still_seen(hook):
"""The #619 shape: 50 docs files on page 1, code hiding on page 2."""
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(50)]),
_rows(["scripts/decisions_lib.py"]))
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(50)]), _rows(["scripts/decisions_lib.py"]))
assert hook.exempted() is False
def test_full_first_page_alone_does_not_end_enumeration(hook):
"""A full 50-row page must trigger a second fetch, not terminate the loop."""
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(50)]),
_rows(["docs/tail.md"]))
assert hook.exempted() is True # genuinely all docs, but only provable by reading page 2
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(50)]), _rows(["docs/tail.md"]))
assert hook.exempted() is True # genuinely all docs, but only provable by reading page 2
def test_rename_of_code_into_docs_is_not_exempt(hook):
@@ -161,15 +163,15 @@ def test_rename_of_code_into_docs_is_not_exempt(hook):
exempt. That is the hook's contract and differs from `review-verdict.yml`'s stricter
PROTECTED list, which must never auto-post a green status for those paths.
"""
hook.set_pages([{"filename": "docs/innocuous-note.md", "status": "renamed",
"previous_filename": "ErsatzTV/Program.cs"}])
hook.set_pages(
[{"filename": "docs/innocuous-note.md", "status": "renamed", "previous_filename": "ErsatzTV/Program.cs"}]
)
assert hook.exempted() is False
def test_rename_between_two_exempt_paths_stays_exempt(hook):
"""Guards the above: reading previous_filename must not over-trigger on legitimate moves."""
hook.set_pages([{"filename": "docs/b.md", "status": "renamed",
"previous_filename": "docs/a.md"}])
hook.set_pages([{"filename": "docs/b.md", "status": "renamed", "previous_filename": "docs/a.md"}])
assert hook.exempted() is True
@@ -232,14 +234,15 @@ def test_malformed_rename_row_withholds_the_exemption(hook):
Real Gitea always populates it (verified by constructing a rename), so this is the
malformed-2xx class the guard claims to fail closed on; the claim should match the behaviour.
"""
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(50)]),
[{"filename": "docs/moved.md", "status": "renamed"}])
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(50)]), [{"filename": "docs/moved.md", "status": "renamed"}])
assert hook.exempted() is False
def test_rename_row_with_empty_previous_filename_withholds_the_exemption(hook):
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(50)]),
[{"filename": "docs/moved.md", "status": "renamed", "previous_filename": ""}])
hook.set_pages(
_rows([f"docs/f{i}.md" for i in range(50)]),
[{"filename": "docs/moved.md", "status": "renamed", "previous_filename": ""}],
)
assert hook.exempted() is False
@@ -254,9 +257,13 @@ def test_ordinary_row_without_previous_filename_is_still_valid(hook):
`test_a_rename_disguised_by_an_unknown_status_is_rejected[None]`. The statusless row this test
used to carry was incidental to what it is actually pinning.
"""
hook.set_pages([{"filename": "docs/a.md", "status": "modified"},
{"filename": "docs/b.md", "status": "added"},
{"filename": "docs/c.md", "status": "changed"}])
hook.set_pages(
[
{"filename": "docs/a.md", "status": "modified"},
{"filename": "docs/b.md", "status": "added"},
{"filename": "docs/c.md", "status": "changed"},
]
)
assert hook.exempted() is True
@@ -312,10 +319,14 @@ sys.exit(127)
@pytest.fixture
def hook_jq16(tmp_path):
"""Same harness as `hook`, plus a jq shim emulating jq 1.6's empty-input exit status."""
bindir = tmp_path / "bin"; bindir.mkdir()
(bindir / "curl").write_text(CURL_SHIM); (bindir / "curl").chmod(0o755)
(bindir / "jq").write_text(_JQ16_SHIM); (bindir / "jq").chmod(0o755)
state = tmp_path / "state"; state.mkdir()
bindir = tmp_path / "bin"
bindir.mkdir()
(bindir / "curl").write_text(CURL_SHIM)
(bindir / "curl").chmod(0o755)
(bindir / "jq").write_text(_JQ16_SHIM)
(bindir / "jq").chmod(0o755)
state = tmp_path / "state"
state.mkdir()
env = dict(os.environ)
env["PATH"] = f"{bindir}{os.pathsep}{env['PATH']}"
@@ -330,10 +341,8 @@ def hook_jq16(tmp_path):
(state / "pages.json").write_text(json.dumps(list(pages)))
def exempted(self):
payload = {"tool_input": {"method": "merge", "owner": "timothy",
"repo": "ersatztv", "pull_number": 42}}
r = subprocess.run(["bash", str(HOOK)], input=json.dumps(payload),
env=env, capture_output=True, text=True)
payload = {"tool_input": {"method": "merge", "owner": "timothy", "repo": "ersatztv", "pull_number": 42}}
r = subprocess.run(["bash", str(HOOK)], input=json.dumps(payload), env=env, capture_output=True, text=True)
assert r.returncode == 0, r.stderr
return r.stdout.strip() == ""
@@ -394,8 +403,9 @@ def test_newline_in_previous_filename_is_also_rejected(hook):
rejects on its own merits, so the test passed with the newline guard entirely removed — it
asserted the outcome without ever exercising the mechanism. That is the same
filter-hides-the-defect trap the guard itself is about."""
hook.set_pages([{"filename": "docs/ok.md", "previous_filename": "safe.md\ndocs/Program.cs",
"status": "renamed"}], [])
hook.set_pages(
[{"filename": "docs/ok.md", "previous_filename": "safe.md\ndocs/Program.cs", "status": "renamed"}], []
)
assert hook.exempted() is False
@@ -406,8 +416,7 @@ def test_previous_filename_is_validated_on_NON_renamed_rows_too(hook, status):
`chunk` emits `(.previous_filename // empty)` for EVERY row regardless of `.status`, but the
field was validated only when `.status == "renamed"`. A row marked `modified` (or Gitea's
distinct `copied`) carrying a newline in `previous_filename` was reproducibly exempted."""
hook.set_pages([{"filename": "docs/ok.md", "status": status,
"previous_filename": "safe.md\ndocs/Program.cs"}], [])
hook.set_pages([{"filename": "docs/ok.md", "status": status, "previous_filename": "safe.md\ndocs/Program.cs"}], [])
assert hook.exempted() is False
@@ -421,21 +430,18 @@ def test_dotdot_path_component_is_rejected(hook):
def test_legitimate_rename_within_docs_still_exempts(hook):
"""Positive control: the tightened row schema must not break a real docs-only rename."""
hook.set_pages([{"filename": "docs/b.md", "status": "renamed",
"previous_filename": "docs/a.md"}], [])
hook.set_pages([{"filename": "docs/b.md", "status": "renamed", "previous_filename": "docs/a.md"}], [])
assert hook.exempted() is True
def test_short_NONTERMINAL_page_does_not_end_the_enumeration(hook):
""""Fewer rows than we asked for" must not be read as "last page".
""" "Fewer rows than we asked for" must not be read as "last page".
Gitea caps `limit` at the server-wide MAX_RESPONSE_ITEMS (default 50, configurable) and may
return fewer rows than requested. A 30-row docs page followed by a page of code would otherwise
complete the enumeration over a PARTIAL list — the same fail-open, reached with no transport
error at all. Only a validated EMPTY page may terminate it."""
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(30)]),
_rows(["ErsatzTV/Program.cs"]),
[])
hook.set_pages(_rows([f"docs/f{i}.md" for i in range(30)]), _rows(["ErsatzTV/Program.cs"]), [])
assert hook.exempted() is False
@@ -473,9 +479,14 @@ def test_gitea_real_status_values_are_accepted(hook):
"""Positive control for the closed set. The real Gitea 1.25.4 value for an edit is `changed`,
NOT `modified` — a closed allow-list built from the wrong vocabulary would reject every real
docs-only PR, which is a far worse failure than the hole it closes."""
hook.set_pages([{"filename": "docs/a.md", "status": "changed"},
{"filename": "docs/b.md", "status": "added"},
{"filename": "docs/c.md", "status": "deleted"}], [])
hook.set_pages(
[
{"filename": "docs/a.md", "status": "changed"},
{"filename": "docs/b.md", "status": "added"},
{"filename": "docs/c.md", "status": "deleted"},
],
[],
)
assert hook.exempted() is True
@@ -483,6 +494,7 @@ def test_gitea_real_status_values_are_accepted(hook):
# The round-3 `..` finding was an anchor subversion, and mutating the anchors showed no test
# covered them: dropping `^` from the docs/ alternative, or `$` from `.md`, both survived.
def test_docs_must_be_a_PREFIX_not_a_substring(hook):
"""Dropping `^` would exempt `ErsatzTV/docs/Program.cs`."""
hook.set_pages([{"filename": "ErsatzTV/docs/Program.cs", "status": "changed"}], [])
@@ -517,7 +529,6 @@ def test_object_valued_status_is_also_rejected(hook):
assert hook.exempted() is False
# --- The `grep -q` / pipefail inversion, on the ADVISORY side (ersatztv#698) --------------------
#
# Round-2 cross-family review noted the enforced gate gained large-input regression tests while the
@@ -530,6 +541,7 @@ def test_object_valued_status_is_also_rejected(hook):
# negated docs-only test. ~171KB is needed to cross the threshold; every other test in this file uses a
# handful of short paths, which is exactly why the class was invisible here.
def _many_docs(n=1900):
return [f"docs/{'d' * 40}-{i:040d}.md" for i in range(n)]
@@ -539,12 +551,12 @@ def test_a_LARGE_pr_containing_a_code_file_is_NOT_exempt(hook):
bulk of ~171KB still to write."""
hook.set_pages(_rows(["A.cs", *_many_docs()]))
assert hook.exempted() is False, (
"a large PR containing A.cs was granted the docs-only exemption — the predicate inverted")
"a large PR containing A.cs was granted the docs-only exemption — the predicate inverted"
)
def test_positive_control_a_LARGE_genuinely_docs_only_pr_IS_still_exempt(hook):
"""Guards the opposite failure: if large lists merely errored, the test above would pass while the
hook prompted on every big docs PR. Without this, 'fixed' and 'broken' are indistinguishable."""
hook.set_pages(_rows(_many_docs()))
assert hook.exempted() is True, (
"a large but genuinely docs-only PR lost its exemption")
assert hook.exempted() is True, "a large but genuinely docs-only PR lost its exemption"
@@ -45,7 +45,7 @@ SHORT = SHA[:7]
# The PR is deliberately NOT docs-only: the docs-only exemption short-circuits the whole gate before
# the scheduled-merge branch is reached, so a docs PR would pass these tests without ever running
# the code under test.
CURL_SHIM = r'''#!/usr/bin/env python3
CURL_SHIM = r"""#!/usr/bin/env python3
import json, os, sys, pathlib, urllib.parse
state = pathlib.Path(os.environ["STUB_DIR"])
@@ -242,14 +242,18 @@ if "/pulls/" in url:
sys.exit(0)
print("{}")
'''
"""
@pytest.fixture
def hook(tmp_path):
bindir = tmp_path / "bin"; bindir.mkdir()
curl = bindir / "curl"; curl.write_text(CURL_SHIM); curl.chmod(0o755)
state = tmp_path / "state"; state.mkdir()
bindir = tmp_path / "bin"
bindir.mkdir()
curl = bindir / "curl"
curl.write_text(CURL_SHIM)
curl.chmod(0o755)
state = tmp_path / "state"
state.mkdir()
(state / "bp").write_text("GUARDED")
env = dict(os.environ)
@@ -283,11 +287,16 @@ def hook(tmp_path):
return f.read_text().splitlines() if f.exists() else []
def decision(self, scheduled=True):
payload = {"tool_input": {"method": "merge", "owner": "timothy",
"repo": "ersatztv", "pull_number": 42,
"merge_when_checks_succeed": scheduled}}
r = subprocess.run(["bash", str(HOOK)], input=json.dumps(payload),
env=env, capture_output=True, text=True)
payload = {
"tool_input": {
"method": "merge",
"owner": "timothy",
"repo": "ersatztv",
"pull_number": 42,
"merge_when_checks_succeed": scheduled,
}
}
r = subprocess.run(["bash", str(HOOK)], input=json.dumps(payload), env=env, capture_output=True, text=True)
assert r.returncode == 0, r.stderr
if not r.stdout.strip():
return None
@@ -307,9 +316,11 @@ def test_a_base_without_the_required_check_denies_a_SCHEDULED_merge(hook):
reason = hook.reason()
assert "deny" in reason, (
"a scheduled auto-merge was armed with no 'review-verdict/h10' required check on the base — "
"that is ersatztv#622's hole reopened")
"that is ersatztv#622's hole reopened"
)
assert "review-verdict/h10" in reason, (
"the deny must name the missing context; a reader cannot act on 'branch protection is wrong'")
"the deny must name the missing context; a reader cannot act on 'branch protection is wrong'"
)
def test_status_checks_disabled_wholesale_also_denies(hook):
@@ -318,8 +329,7 @@ def test_status_checks_disabled_wholesale_also_denies(hook):
check-the-label-not-the-capability shape (#697/#698) this repo has paid for twice."""
hook.set_branch_protection("STATUS-CHECK-OFF")
reason = hook.reason()
assert "deny" in reason, (
"status checks were disabled entirely and the listed context was read as protection anyway")
assert "deny" in reason, "status checks were disabled entirely and the listed context was read as protection anyway"
def test_positive_control_a_guarded_base_REACHES_the_check_and_still_auto_grants(hook):
@@ -334,7 +344,8 @@ def test_positive_control_a_guarded_base_REACHES_the_check_and_still_auto_grants
decision = hook.decision()
assert hook.branch_protection_urls(), (
"the guarded case never reached the branch-protection endpoint, so the other tests are not "
"exercising the code they claim to")
"exercising the code they claim to"
)
assert decision is not None, "the hook passed through instead of auto-granting"
verdict = decision["hookSpecificOutput"]["permissionDecision"]
assert verdict == "allow", f"a fully-satisfied gate did not auto-grant (got {verdict!r})"
@@ -354,9 +365,11 @@ def test_a_context_name_that_merely_CONTAINS_the_required_one_does_not_satisfy_i
hook.set_branch_protection(shape)
reason = hook.reason()
assert "allow" not in reason or "deny" in reason or "ask" in reason, (
f"payload shape {shape} auto-granted a scheduled merge")
f"payload shape {shape} auto-granted a scheduled merge"
)
assert "satisfied" not in reason, (
f"a context name that merely contains 'review-verdict/h10' ({shape}) was accepted as it")
f"a context name that merely contains 'review-verdict/h10' ({shape}) was accepted as it"
)
@pytest.mark.parametrize("shape", ["EMPTY-CONTEXTS", "NULL-CONTEXTS"])
@@ -366,8 +379,7 @@ def test_an_empty_or_null_contexts_list_DENIES_rather_than_asking(hook, shape):
unknown-shape ask alongside genuinely unreadable payloads."""
hook.set_branch_protection(shape)
reason = hook.reason()
assert "deny" in reason, (
f"{shape} was treated as unreadable rather than as a confirmed absent required check")
assert "deny" in reason, f"{shape} was treated as unreadable rather than as a confirmed absent required check"
def test_a_FALSE_contexts_value_asks_rather_than_denying(hook):
@@ -400,7 +412,8 @@ def test_a_malformed_contexts_MEMBER_asks_rather_than_denying_with_the_wrong_rea
reason = hook.reason()
assert "ask" in reason, "a malformed contexts member produced a decision instead of a question"
assert "NOT a required status check" not in reason, (
"an unreadable payload was reported as a confirmed missing required check")
"an unreadable payload was reported as a confirmed missing required check"
)
def test_a_base_with_NO_branch_protection_at_all_denies_rather_than_asking(hook):
@@ -417,11 +430,10 @@ def test_a_base_with_NO_branch_protection_at_all_denies_rather_than_asking(hook)
"""
hook.set_branch_protection("EMPTY-LIST")
reason = hook.reason()
assert "deny" in reason, (
"a base with no branch protection at all did not deny a scheduled auto-merge")
assert "deny" in reason, "a base with no branch protection at all did not deny a scheduled auto-merge"
assert "none matches" in reason, (
"the deny must distinguish 'the list was read and nothing governs this base' from "
"'could not read'")
"the deny must distinguish 'the list was read and nothing governs this base' from 'could not read'"
)
def test_a_403_asks_because_it_says_only_that_we_could_not_look(hook):
@@ -431,13 +443,12 @@ def test_a_403_asks_because_it_says_only_that_we_could_not_look(hook):
hook.set_branch_protection("FORBIDDEN")
reason = hook.reason()
assert "ask" in reason, "a 403 was treated as evidence about the protection"
assert "none matches" not in reason, (
"a 403 was reported as a confirmed absence of branch protection")
assert "none matches" not in reason, "a 403 was reported as a confirmed absence of branch protection"
@pytest.mark.parametrize("failure", ["TRANSPORT-ERROR", "GARBAGE", "EMPTY", "LIST-404"])
def test_an_UNREADABLE_branch_protection_asks_rather_than_denying_or_passing(hook, failure):
""""Could not check" is a third outcome, not a synonym for either neighbour.
""" "Could not check" is a third outcome, not a synonym for either neighbour.
Denying would strand every scheduled merge on a Gitea hiccup or on credentials without the
repo-admin scope this endpoint needs. Passing would be worse: it would restore the exact
@@ -446,10 +457,8 @@ def test_an_UNREADABLE_branch_protection_asks_rather_than_denying_or_passing(hoo
"""
hook.set_branch_protection(failure)
reason = hook.reason()
assert "ask" in reason, (
f"an unreadable branch-protection response ({failure}) did not fall through to a human")
assert "branch-protection rules" in reason, (
"the ask must name what could not be checked")
assert "ask" in reason, f"an unreadable branch-protection response ({failure}) did not fall through to a human"
assert "branch-protection rules" in reason, "the ask must name what could not be checked"
def test_an_IMMEDIATE_merge_is_not_subjected_to_this_check(hook):
@@ -465,7 +474,8 @@ def test_an_IMMEDIATE_merge_is_not_subjected_to_this_check(hook):
hook.set_branch_protection("MISSING-CONTEXT")
reason = hook.reason(scheduled=False)
assert "NOT a required status check" not in reason, (
"the required-check deny fired on an immediate merge, which has no post-scheduling window")
"the required-check deny fired on an immediate merge, which has no post-scheduling window"
)
def test_a_NON_BOOLEAN_enable_status_check_asks(hook):
@@ -504,8 +514,8 @@ def test_a_SCALAR_status_row_asks_instead_of_killing_the_hook(hook):
hook.set_branch_protection("SCALAR-STATUS-ROW")
decision = hook.decision()
assert decision is not None, (
"the hook emitted no decision at all for a malformed statuses payload — it neither granted, "
"denied nor asked")
"the hook emitted no decision at all for a malformed statuses payload — it neither granted, denied nor asked"
)
assert decision["hookSpecificOutput"]["permissionDecision"] == "ask"
@@ -521,8 +531,7 @@ def test_a_PERSISTENT_retarget_denies_on_the_IMMEDIATE_path_too(hook):
hook.drop_recorded_base()
hook.set_retarget("scratch")
reason = hook.reason(scheduled=False)
assert "deny" in reason, (
"an immediate merge was auto-granted after the PR was retargeted mid-evaluation")
assert "deny" in reason, "an immediate merge was auto-granted after the PR was retargeted mid-evaluation"
assert "scratch" in reason and "main" in reason
@@ -540,10 +549,10 @@ def test_a_PERSISTENT_retarget_after_the_first_read_denies(hook):
hook.drop_recorded_base()
hook.set_retarget("scratch")
reason = hook.reason()
assert "deny" in reason, (
"the PR was retargeted mid-evaluation and the gate still granted on the original base")
assert "deny" in reason, "the PR was retargeted mid-evaluation and the gate still granted on the original base"
assert "scratch" in reason and "main" in reason, (
"the deny must name both branches; a reader cannot act on 'the base changed'")
"the deny must name both branches; a reader cannot act on 'the base changed'"
)
def test_a_NON_STRING_status_reaches_the_scheduled_validator(hook):
@@ -573,13 +582,12 @@ def test_a_base_that_a_GLOB_rule_could_govern_ASKS(hook):
"""
hook.set_branch_protection("GLOB-RULE")
reason = hook.reason()
assert "ask" in reason, (
"a base a glob rule could govern was decided rather than referred to a human")
assert "ask" in reason, "a base a glob rule could govern was decided rather than referred to a human"
assert "could govern it" in reason, (
"the ask came from the generic could-not-read arm, not the undecidable-glob arm — those "
"are different outcomes and a crashed classifier must not pass as a correct classification")
assert "none matches" not in reason, (
"a base a glob rule could govern was reported as having no protection at all")
"are different outcomes and a crashed classifier must not pass as a correct classification"
)
assert "none matches" not in reason, "a base a glob rule could govern was reported as having no protection at all"
def test_an_unreadable_rule_LIST_asks_rather_than_denying(hook):
@@ -606,8 +614,7 @@ def test_the_protection_lookup_puts_NO_ref_in_the_url(hook):
urls = hook.branch_protection_urls()
assert urls, "the branch-protection endpoint was never requested"
for u in urls:
assert u.rstrip("/").endswith("/branch_protections"), (
f"a ref reached the branch-protection URL: {u}")
assert u.rstrip("/").endswith("/branch_protections"), f"a ref reached the branch-protection URL: {u}"
def test_a_rule_name_with_REGEX_METACHARACTERS_does_not_match_a_different_base(hook):
@@ -624,11 +631,11 @@ def test_a_rule_name_with_REGEX_METACHARACTERS_does_not_match_a_different_base(h
"""
hook.set_branch_protection("REGEX-META-RULE")
reason = hook.reason()
assert "deny" in reason, (
"a rule named 'mai.' was regex-matched against base 'main' and read as protection")
assert "deny" in reason, "a rule named 'mai.' was regex-matched against base 'main' and read as protection"
assert "none matches" in reason, (
"'mai.' contains no GLOB metacharacter, so it is decidable: it simply does not govern "
"'main', and the base is genuinely unprotected")
"'main', and the base is genuinely unprotected"
)
def test_a_GLOB_rule_whose_literal_part_has_a_metacharacter_still_MATCHES(hook):
@@ -645,17 +652,19 @@ def test_a_GLOB_rule_whose_literal_part_has_a_metacharacter_still_MATCHES(hook):
rather than reporting the base as unprotected.
"""
hook.set_base("release/26.4")
hook.drop_recorded_base() # keep the #632 comparison out of this test's way
hook.drop_recorded_base() # keep the #632 comparison out of this test's way
hook.set_branch_protection("GLOB-WITH-DOT-RULE")
reason = hook.reason()
assert "ask" in reason, (
"the glob rule 'release/26.*' could govern base 'release/26.4', which is undecidable here "
"and must ask")
"the glob rule 'release/26.*' could govern base 'release/26.4', which is undecidable here and must ask"
)
assert "could govern it" in reason, (
"the ask must come from the undecidable-glob arm, not from the classifier failing")
"the ask must come from the undecidable-glob arm, not from the classifier failing"
)
assert "none matches" not in reason, (
"a base a glob rule could govern was reported as entirely unprotected — the over-escaping "
"failure this test exists to catch")
"failure this test exists to catch"
)
def test_a_rule_name_containing_a_CHAR_CLASS_bracket_does_not_crash_the_matcher(hook):
@@ -667,12 +676,13 @@ def test_a_rule_name_containing_a_CHAR_CLASS_bracket_does_not_crash_the_matcher(
assert reason, "the hook emitted no decision at all"
assert "ask" not in reason, (
"the exact-name rule was decidable and must have been honoured; an ask here means the "
"classifier failed rather than classified")
"classifier failed rather than classified"
)
assert "none matches" not in reason, (
"one rule with a bracket in its name discarded the whole list, including the exact-name "
"rule that actually protects this base")
assert "deny" not in reason, (
"an exact-name rule requiring review-verdict/h10 was present and was not honoured")
"rule that actually protects this base"
)
assert "deny" not in reason, "an exact-name rule requiring review-verdict/h10 was present and was not honoured"
def test_a_plain_rule_name_is_matched_CASE_INSENSITIVELY(hook):
@@ -683,9 +693,11 @@ def test_a_plain_rule_name_is_matched_CASE_INSENSITIVELY(hook):
reason = hook.reason()
assert "ask" not in reason, (
"the case-folded exact rule was decidable and must have been honoured; an ask means the "
"classifier failed rather than classified")
"classifier failed rather than classified"
)
assert "none matches" not in reason, (
"a rule named 'MAIN' governs base 'main' in Gitea but was missed by a case-sensitive compare")
"a rule named 'MAIN' governs base 'main' in Gitea but was missed by a case-sensitive compare"
)
assert "deny" not in reason
@@ -707,12 +719,13 @@ def test_a_BACKSLASH_ESCAPED_metacharacter_in_a_rule_name_is_undecidable_not_abs
hook.set_branch_protection("ESCAPED-META-RULE")
reason = hook.reason()
assert "none matches" not in reason, (
"a rule whose escaped brace governs this base was reported as unable to govern it")
assert "ask" in reason, (
"an escaped-metacharacter rule is undecidable here and must ask")
"a rule whose escaped brace governs this base was reported as unable to govern it"
)
assert "ask" in reason, "an escaped-metacharacter rule is undecidable here and must ask"
assert "could govern it" in reason, (
"the ask must come from the undecidable-glob arm, not from the classifier failing — this "
"test hits the same arm as its two siblings and needs the same pin")
"test hits the same arm as its two siblings and needs the same pin"
)
def test_the_precedence_check_runs_even_when_an_exactly_named_rule_EXISTS(hook):
@@ -732,13 +745,13 @@ def test_the_precedence_check_runs_even_when_an_exactly_named_rule_EXISTS(hook):
"""
hook.set_branch_protection("EXACT-PLUS-GLOB-RULE")
reason = hook.reason()
assert "ask" in reason, (
"an exactly-named rule was trusted without asking which rule Gitea would actually apply")
assert "ask" in reason, "an exactly-named rule was trusted without asking which rule Gitea would actually apply"
assert "could govern it" in reason
# And the by-name endpoint must not be consulted at all — its existence is what split the paths.
for u in hook.branch_protection_urls():
assert u.rstrip("/").endswith("/branch_protections"), (
f"the by-name lookup is back, and with it the unguarded path: {u}")
f"the by-name lookup is back, and with it the unguarded path: {u}"
)
def test_a_GLOB_rule_that_could_outrank_an_exact_one_wins_and_ASKS(hook):
@@ -758,9 +771,11 @@ def test_a_GLOB_rule_that_could_outrank_an_exact_one_wins_and_ASKS(hook):
reason = hook.reason()
assert "ask" in reason, (
"an exact rule was trusted while a glob rule could outrank it — the gate granted on a base "
"whose enforced rule it never identified")
"whose enforced rule it never identified"
)
assert "could govern it" in reason, (
"the ask must come from the undecidable-glob arm, not from the classifier failing")
"the ask must come from the undecidable-glob arm, not from the classifier failing"
)
def test_two_rules_differing_only_in_CASE_are_undecidable(hook):
@@ -769,8 +784,7 @@ def test_two_rules_differing_only_in_CASE_are_undecidable(hook):
whose enforced rule was never identified — the same defect as the arm order, one level down."""
hook.set_branch_protection("TWO-CASE-VARIANT-RULES")
reason = hook.reason()
assert "ask" in reason, (
"two fold-equal rules disagree about review-verdict/h10 and one was picked by list order")
assert "ask" in reason, "two fold-equal rules disagree about review-verdict/h10 and one was picked by list order"
assert "could govern it" in reason
@@ -784,7 +798,8 @@ def test_a_NON_ASCII_rule_or_base_is_undecidable_rather_than_fold_compared(hook)
hook.set_branch_protection("NONASCII-RULE")
reason = hook.reason()
assert "none matches" not in reason, (
"a rule that folds equal to this base under EqualFold was reported as unable to govern it")
"a rule that folds equal to this base under EqualFold was reported as unable to govern it"
)
assert "ask" in reason
@@ -803,8 +818,7 @@ def test_an_HTTP_404_on_the_LIST_read_asks_and_does_not_claim_the_list_was_read(
hook.set_branch_protection("LIST-404")
reason = hook.reason()
assert "ask" in reason, "an unreadable repo was treated as evidence about the base"
assert "none matches" not in reason, (
"a 404 read claimed the full rule list had been read and matched nothing")
assert "none matches" not in reason, "a 404 read claimed the full rule list had been read and matched nothing"
@pytest.mark.parametrize("shape", ["UNPARSEABLE-RULES", "GARBAGE", "EMPTY"])
@@ -825,6 +839,5 @@ def test_a_200_the_classifier_cannot_PARSE_asks_without_blaming_the_transport(ho
hook.set_branch_protection(shape)
reason = hook.reason()
assert "ask" in reason, "an unparseable rule list produced a decision instead of a question"
assert "could not parse" in reason, (
"the ask blamed the transport for a 200 the classifier simply could not read")
assert "could not parse" in reason, "the ask blamed the transport for a 200 the classifier simply could not read"
assert "unreachable" not in reason
+12 -9
View File
@@ -127,7 +127,9 @@ def gitea(tmp_path):
def run(self, *args):
return subprocess.run(
["bash", str(SCRIPT), *args],
env=env, capture_output=True, text=True,
env=env,
capture_output=True,
text=True,
)
def posts(self):
@@ -229,6 +231,7 @@ def test_unreachable_pr_is_an_error_not_a_silent_success(gitea):
# --- Cross-checks against the hook's own condition-(c) parser -----------------------------------
def _classify(body: str, head: str) -> str:
"""Run the REAL H10 classifier over a comment body — no Python mirror of the grammar.
@@ -239,9 +242,7 @@ def _classify(body: str, head: str) -> str:
passing here while the shell drifted.
"""
payload = json.dumps([{"body": body}])
p = subprocess.run(
["bash", str(CLASSIFIER), "--head", head], input=payload, capture_output=True, text=True
)
p = subprocess.run(["bash", str(CLASSIFIER), "--head", head], input=payload, capture_output=True, text=True)
assert p.returncode == 0, f"classifier errored: {p.stderr}"
return p.stdout.strip()
@@ -280,6 +281,7 @@ def test_note_cannot_forge_a_second_verdict_line(gitea):
# mirror case: the head sha and the status both hold still while the effective DIFF changes, so the
# verdict keeps reading green for a review nobody performed against that base.
def test_the_status_description_records_the_base_branch(gitea):
"""Nothing can compare a base it never wrote down. This field is what the hook reads back."""
assert gitea.run("42", "MERGEABLE").returncode == 0
@@ -333,7 +335,8 @@ def test_a_failed_HEAD_RECHECK_writes_no_status(gitea):
result = gitea.run("42", "MERGEABLE")
assert result.returncode != 0
assert gitea.statuses() == [], (
"a status was written even though the head/base re-read failed — nothing was confirmed")
"a status was written even though the head/base re-read failed — nothing was confirmed"
)
# --- write-side polarity, the half the #774 rescue initially missed ------------------------------
@@ -404,8 +407,8 @@ def test_a_reread_that_LOSES_a_field_refuses_instead_of_posting(head_seq, base_s
gitea.set_base_sequence(*base_seq)
result = gitea.run("42", "MERGEABLE")
assert result.returncode != 0, (
f"a re-read missing its {field} was accepted; the script posted a verdict having confirmed "
"nothing about it")
f"a re-read missing its {field} was accepted; the script posted a verdict having confirmed nothing about it"
)
assert not gitea.statuses(), (
f"a status was written despite the re-read carrying no {field} — this is the fail-open "
"the -n conjunct created")
f"a status was written despite the re-read carrying no {field} — this is the fail-open the -n conjunct created"
)
File diff suppressed because it is too large Load Diff
+46 -27
View File
@@ -35,14 +35,18 @@ PROVE_FIX = Path(os.environ.get("PROVE_FIX_PATH") or (REPO_ROOT / "scripts" / "p
def _git(repo: Path, *args: str) -> str:
return subprocess.run(
["git", "-C", str(repo), *args],
check=True, capture_output=True, text=True,
check=True,
capture_output=True,
text=True,
).stdout.strip()
def _run(repo: Path, *args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
["bash", str(PROVE_FIX), "--repo", str(repo), *args],
capture_output=True, text=True, cwd=str(repo),
capture_output=True,
text=True,
cwd=str(repo),
)
@@ -61,9 +65,7 @@ def fixrepo(tmp_path: Path) -> Path:
# --- commit 1: the bug, plus a test that cannot see it
(repo / "calc.py").write_text("def add(a, b):\n return a - b # bug\n")
(repo / "scripts" / "tests" / "test_unrelated.py").write_text(
"def test_unrelated():\n assert 1 + 1 == 2\n"
)
(repo / "scripts" / "tests" / "test_unrelated.py").write_text("def test_unrelated():\n assert 1 + 1 == 2\n")
_git(repo, "add", "-A")
_git(repo, "commit", "-q", "-m", "initial: buggy add, unrelated test")
@@ -73,8 +75,7 @@ def fixrepo(tmp_path: Path) -> Path:
"from calc import add\n\n\ndef test_add():\n assert add(2, 3) == 5\n"
)
_git(repo, "add", "-A")
_git(repo, "commit", "-q", "-m",
"fix: add() returned a difference\n\nProves: scripts/tests/test_add.py")
_git(repo, "commit", "-q", "-m", "fix: add() returned a difference\n\nProves: scripts/tests/test_add.py")
return repo
@@ -143,7 +144,8 @@ def test_added_code_file_is_removed_not_checked_out(tmp_path: Path) -> None:
_git(repo, "config", "user.email", "t@example.com")
_git(repo, "config", "user.name", "T")
(repo / "scripts" / "tests" / "keep.py").write_text("# placeholder\n")
_git(repo, "add", "-A"); _git(repo, "commit", "-q", "-m", "initial")
_git(repo, "add", "-A")
_git(repo, "commit", "-q", "-m", "initial")
# the fix ADDS helper.py (it has no parent version) and a test that needs it
(repo / "helper.py").write_text("def shout(s):\n return s.upper()\n")
@@ -165,7 +167,8 @@ def test_root_commit_REFUSES(tmp_path: Path) -> None:
_git(repo, "config", "user.email", "t@example.com")
_git(repo, "config", "user.name", "T")
(repo / "a.py").write_text("x = 1\n")
_git(repo, "add", "-A"); _git(repo, "commit", "-q", "-m", "root")
_git(repo, "add", "-A")
_git(repo, "commit", "-q", "-m", "root")
r = _run(repo, "HEAD", "scripts/tests")
assert r.returncode == 5
assert "root commit" in r.stderr
@@ -185,9 +188,10 @@ def test_runs_under_the_system_bash(fixrepo: Path) -> None:
# developer Mac. The 3.2 hazard (mapfile yielding an empty array) is what motivated it.
ver = subprocess.run([str(system_bash), "--version"], capture_output=True, text=True).stdout
r = subprocess.run(
[str(system_bash), str(PROVE_FIX), "--repo", str(fixrepo), "HEAD",
"scripts/tests/test_add.py"],
capture_output=True, text=True, cwd=str(fixrepo),
[str(system_bash), str(PROVE_FIX), "--repo", str(fixrepo), "HEAD", "scripts/tests/test_add.py"],
capture_output=True,
text=True,
cwd=str(fixrepo),
)
assert r.returncode == 0, (
f"prove-fix.sh must work under the system bash ({ver.splitlines()[0] if ver else '?'}); "
@@ -204,8 +208,7 @@ def test_control_failure_REFUSES(fixrepo: Path) -> None:
)
(fixrepo / "calc.py").write_text("def add(a, b):\n return a + b # unchanged\n")
_git(fixrepo, "add", "-A")
_git(fixrepo, "commit", "-q", "-m",
"fix: with an already-failing test\n\nProves: scripts/tests/test_broken.py")
_git(fixrepo, "commit", "-q", "-m", "fix: with an already-failing test\n\nProves: scripts/tests/test_broken.py")
r = _run(fixrepo, "HEAD")
assert r.returncode == 6, f"expected 6 (control failed), got {r.returncode}\n{r.stdout}\n{r.stderr}"
assert "control FAILED" in r.stderr
@@ -248,9 +251,19 @@ def test_MUTATION_disarming_the_UNPROVEN_clause_reddens_the_refusal_test(
env = {**os.environ, "PROVE_FIX_PATH": str(mutant), "PROVE_FIX_MUTATION_RUN": "1"}
nested = subprocess.run(
["python3", "-m", "pytest", f"{Path(__file__).name}::test_unrelated_test_is_UNPROVEN",
"-q", "-p", "no:cacheprovider"],
cwd=str(Path(__file__).parent), env=env, capture_output=True, text=True,
[
"python3",
"-m",
"pytest",
f"{Path(__file__).name}::test_unrelated_test_is_UNPROVEN",
"-q",
"-p",
"no:cacheprovider",
],
cwd=str(Path(__file__).parent),
env=env,
capture_output=True,
text=True,
)
out = nested.stdout + nested.stderr
assert nested.returncode == 1, (
@@ -282,17 +295,20 @@ def test_SIGTERM_mid_run_never_reports_PROVEN(tmp_path: Path) -> None:
_git(repo, "config", "user.name", "T")
(repo / "mod.py").write_text("VALUE = 1\n")
(repo / "scripts" / "tests" / "test_slow.py").write_text(
"import time\nfrom mod import VALUE\n\n\n"
"def test_slow():\n time.sleep(20)\n assert VALUE == 2\n"
"import time\nfrom mod import VALUE\n\n\ndef test_slow():\n time.sleep(20)\n assert VALUE == 2\n"
)
_git(repo, "add", "-A"); _git(repo, "commit", "-q", "-m", "initial")
_git(repo, "add", "-A")
_git(repo, "commit", "-q", "-m", "initial")
(repo / "mod.py").write_text("VALUE = 2\n")
_git(repo, "add", "-A")
_git(repo, "commit", "-q", "-m", "fix: bump\n\nProves: scripts/tests/test_slow.py")
proc = subprocess.Popen(
["bash", str(PROVE_FIX), "--repo", str(repo), "HEAD"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, cwd=str(repo),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
cwd=str(repo),
)
time.sleep(4) # inside the control run, which sleeps 20s
assert proc.poll() is None, "the run finished before it could be signalled; test is void"
@@ -305,9 +321,7 @@ def test_SIGTERM_mid_run_never_reports_PROVEN(tmp_path: Path) -> None:
# bugs landing on the same observable is not a witnessed fix. Cold review measured that
# pair failing to separate old from new; rc==5 AND the handler's own message do separate
# them.
assert proc.returncode == 5, (
f"a signalled run must exit 5 from on_signal, got {proc.returncode}\n{out}\n{err}"
)
assert proc.returncode == 5, f"a signalled run must exit 5 from on_signal, got {proc.returncode}\n{out}\n{err}"
assert "interrupted by signal" in err, (
f"expected the signal handler's own message, so this test cannot be satisfied by an "
f"unrelated later failure:\n{err}"
@@ -335,7 +349,8 @@ def test_a_harness_failure_is_NOT_reported_as_PROVEN(tmp_path: Path) -> None:
_git(repo, "config", "user.email", "t@example.com")
_git(repo, "config", "user.name", "T")
(repo / "scripts" / "tests" / "keep.py").write_text("# placeholder\n")
_git(repo, "add", "-A"); _git(repo, "commit", "-q", "-m", "initial")
_git(repo, "add", "-A")
_git(repo, "commit", "-q", "-m", "initial")
(repo / "added.py").write_text("def val():\n return 7\n")
(repo / "scripts" / "tests" / "test_added.py").write_text(
"from added import val\n\n\ndef test_val():\n assert val() == 7\n"
@@ -343,7 +358,8 @@ def test_a_harness_failure_is_NOT_reported_as_PROVEN(tmp_path: Path) -> None:
_git(repo, "add", "-A")
_git(repo, "commit", "-q", "-m", "feat: add val\n\nProves: scripts/tests/test_added.py")
shim = tmp_path / "bin"; shim.mkdir()
shim = tmp_path / "bin"
shim.mkdir()
counter = tmp_path / "count"
(shim / "git").write_text(
"#!/bin/sh\n"
@@ -358,7 +374,10 @@ def test_a_harness_failure_is_NOT_reported_as_PROVEN(tmp_path: Path) -> None:
env = {**os.environ, "PATH": f"{shim}:{os.environ['PATH']}"}
r = subprocess.run(
["bash", str(PROVE_FIX), "--repo", str(repo), "HEAD"],
capture_output=True, text=True, cwd=str(repo), env=env,
capture_output=True,
text=True,
cwd=str(repo),
env=env,
)
assert "PROVEN" not in r.stdout, (
"a phase whose worktree does not exist cannot witness anything; pre-fix this "
+28 -23
View File
@@ -109,7 +109,8 @@ def _tracked_files() -> list[str]:
"""
proc = subprocess.run(
["git", "-C", str(REPO_ROOT), "ls-files", "-z"],
capture_output=True, check=True,
capture_output=True,
check=True,
)
return [p for p in proc.stdout.decode().split("\0") if p]
@@ -150,7 +151,8 @@ def _inventory_section(text: str) -> str:
raise AssertionError(
f"{INVENTORY.name} has no {heading!r} heading. Row parsing is bounded by "
"'## The inventory' and '## Limits'; renaming or reordering either one would "
"silently change which rows are checked, so it fails here instead.")
"silently change which rows are checked, so it fails here instead."
)
start = text.index("## The inventory")
try:
end = text.index("## Limits", start)
@@ -161,7 +163,8 @@ def _inventory_section(text: str) -> str:
# either returns an index >= start or raises. A guard that cannot execute proves nothing.
raise AssertionError(
f"{INVENTORY.name}: '## Limits' precedes '## The inventory', so the parsed window "
"would be empty and every completeness assertion would pass vacuously.") from None
"would be empty and every completeness assertion would pass vacuously."
) from None
return text[start:end]
@@ -188,10 +191,11 @@ def test_anti_vacuity_the_derivation_and_the_table_both_found_something():
sites = inventory_sites()
assert len(population) >= 40, (
f"derived only {len(population)} in-scope files — the globs are broken, not the repo "
"(the scope held 59 files on 2026-08-16, and it only grows)")
"(the scope held 59 files on 2026-08-16, and it only grows)"
)
assert len(sites) >= 40, (
f"parsed only {len(sites)} rows out of the inventory — the row regex has drifted from the "
"table format")
f"parsed only {len(sites)} rows out of the inventory — the row regex has drifted from the table format"
)
def test_every_in_scope_file_has_a_row_and_every_row_names_a_real_file():
@@ -208,9 +212,9 @@ def test_every_in_scope_file_has_a_row_and_every_row_names_a_real_file():
phantom = sorted(sites - population)
assert not missing, (
"in scope but absent from docs/remote-state-inventory.md (classify each as "
f"PINNED / CAS / UNSAFE-KNOWN / N/A): {missing}")
assert not phantom, (
f"listed in docs/remote-state-inventory.md but no such in-scope file exists: {phantom}")
f"PINNED / CAS / UNSAFE-KNOWN / N/A): {missing}"
)
assert not phantom, f"listed in docs/remote-state-inventory.md but no such in-scope file exists: {phantom}"
def test_MUTATION_PROOF_a_dropped_row_and_a_phantom_row_are_both_detected():
@@ -228,27 +232,28 @@ def test_MUTATION_PROOF_a_dropped_row_and_a_phantom_row_are_both_detected():
population = derived_population()
victim = sorted(population)[0]
dropped = "\n".join(
line for line in text.splitlines() if not line.startswith(f"| `{victim}`"))
dropped = "\n".join(line for line in text.splitlines() if not line.startswith(f"| `{victim}`"))
assert victim not in inventory_sites(dropped), (
f"the mutation did not actually remove {victim}; the proof below would be vacuous")
f"the mutation did not actually remove {victim}; the proof below would be vacuous"
)
assert population - inventory_sites(dropped), (
"a row was removed from the inventory and the comparison still reported complete coverage")
"a row was removed from the inventory and the comparison still reported complete coverage"
)
# Inserted INSIDE the inventory section, not appended to the file: rows are parsed only between
# "## The inventory" and "## Limits", so appending at the end would test nothing.
phantom = text.replace(
"## Limits",
"| `scripts/does-not-exist.sh` — invented | `PINNED` | n/a |\n\n## Limits", 1)
phantom = text.replace("## Limits", "| `scripts/does-not-exist.sh` — invented | `PINNED` | n/a |\n\n## Limits", 1)
assert inventory_sites(phantom) - population == {"scripts/does-not-exist.sh"}, (
"a row naming a file that does not exist was not reported as phantom")
"a row naming a file that does not exist was not reported as phantom"
)
def test_every_class_cell_comes_from_the_closed_vocabulary():
bad = sorted({cls for _, cls in inventory_rows() if cls not in CLASSES})
assert not bad, (
f"unknown classification(s) {bad}; allowed: {sorted(CLASSES)}. A typo here would silently "
"create a state nobody reviews.")
"create a state nobody reviews."
)
def test_every_unsafe_row_states_why_the_residual_is_accepted():
@@ -268,8 +273,7 @@ def test_every_unsafe_row_states_why_the_residual_is_accepted():
site, note = cells[0], cells[-1]
if len(note) < 120:
thin.append(site[:60])
assert not thin, (
f"UNSAFE-KNOWN row(s) with no stated justification: {thin}")
assert not thin, f"UNSAFE-KNOWN row(s) with no stated justification: {thin}"
def test_the_population_never_includes_a_file_git_does_not_track(monkeypatch):
@@ -296,14 +300,15 @@ def test_the_population_never_includes_a_file_git_does_not_track(monkeypatch):
)
# Patch the module object this test is running inside, whatever name it was imported under.
import sys
mod = sys.modules[__name__]
monkeypatch.setattr(mod, "_tracked_files", lambda: tracked)
assert (REPO_ROOT / victim).is_file(), (
f"{victim} must still exist on disk for this proof to mean anything")
assert (REPO_ROOT / victim).is_file(), f"{victim} must still exist on disk for this proof to mean anything"
assert victim not in derived_population(), (
f"{victim} is on disk and matches the scope, but git no longer tracks it — it must not enter "
"the population, or untracked build output can redden this guard again")
"the population, or untracked build output can redden this guard again"
)
def test_every_derived_member_is_tracked():