From 3970386064d85139b301e81a27f55dc9cdbdde33 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:31:15 +0000 Subject: [PATCH 01/57] feat(lock): derive and enforce chock.lock's engine constraint (IR-F71) sync writes ">=.,<" derived from the running engine; `chock check --only verify` fails in one line when the running engine is outside the lock's constraint, passes the legacy ">=0.1,<0.2" with a note to re-sync, and fails a missing or unparseable constraint. No new dependency: version tuples are compared by hand. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/ir-f71-lock-engine-constraint.md | 5 + src/chock/lock.py | 77 ++++++++++-- tests/test_lock_engine.py | 119 +++++++++++++++++++ 3 files changed, 194 insertions(+), 7 deletions(-) create mode 100644 changelog.d/ir-f71-lock-engine-constraint.md create mode 100644 tests/test_lock_engine.py diff --git a/changelog.d/ir-f71-lock-engine-constraint.md b/changelog.d/ir-f71-lock-engine-constraint.md new file mode 100644 index 00000000..ca70d97d --- /dev/null +++ b/changelog.d/ir-f71-lock-engine-constraint.md @@ -0,0 +1,5 @@ +- `chock.lock`'s `engine` field is now true and read (IR-F71): `sync` writes `>=.,<` + derived from the running engine (`>=1.1,<2` at 1.1.x, was the stale `>=0.1,<0.2`), and `chock check --only verify` + fails with one line naming the running engine, the lock's constraint and the fix (`pip install` a matching chock, + or `chock sync --repo .` with the newer engine) when the engine is outside it. A lock that still says `>=0.1,<0.2` + passes with a one-line note to run `chock sync`; a missing or unparseable constraint fails. diff --git a/src/chock/lock.py b/src/chock/lock.py index 1a0e1b5b..bfee1fac 100644 --- a/src/chock/lock.py +++ b/src/chock/lock.py @@ -5,17 +5,22 @@ import argparse import hashlib import json +import re import sys from pathlib import Path from typing import Any +from chock import __version__ from chock.policies import discover_policy_dirs from chock.scaffold.pin import is_remote_source from chock.vendored import vendored_differences LOCKFILE_NAME = "chock.lock" LOCKFILE_VERSION = "2" -ENGINE_CONSTRAINT = ">=0.1,<0.2" +#: What every lock said before the field was read; such a lock passes with a note to re-lock. +LEGACY_ENGINE_CONSTRAINT = ">=0.1,<0.2" +_VERSION_PREFIX = re.compile(r"(\d+)\.(\d+)(?:\.(\d+))?") +_CONSTRAINT_RE = re.compile(r"\s*>=\s*(\d+(?:\.\d+){0,2})\s*,\s*<\s*(\d+(?:\.\d+){0,2})\s*") POLICIES_REL = Path(".agents") / "policies" LOCAL_SOURCE = "local" _PROVENANCE_KEYS = ("source", "source_commit", "source_ref") @@ -25,6 +30,53 @@ class LockError(RuntimeError): """chock.lock exists but cannot be read as a lockfile.""" +def _version_tuple(version: str) -> tuple[int, int, int] | None: + """(major, minor, patch) from the leading digits of a version string; None when it has none.""" + match = _VERSION_PREFIX.match(version.strip()) + if not match: + return None + major, minor, patch = match.groups() + return int(major), int(minor), int(patch or 0) + + +def _padded(dotted: str) -> tuple[int, int, int]: + """A constraint bound as (major, minor, patch), missing parts 0.""" + major, minor, patch, *_ = [*(int(p) for p in dotted.split(".")), 0, 0] + return major, minor, patch + + +def engine_constraint(version: str = __version__) -> str: + """The constraint a lock written by engine `version` carries: `>=.,<`.""" + parsed = _version_tuple(version) + if parsed is None: + msg = f"engine version {version!r} has no . prefix" + raise ValueError(msg) + major, minor, _patch = parsed + return f">={major}.{minor},<{major + 1}" + + +def check_engine(constraint: object, version: str = __version__) -> tuple[str | None, str | None]: + """(failure, note) for the running engine against a lock's `engine` field; both None when it satisfies it.""" + relock = "or run `chock sync --repo .` with the newer engine to re-lock" + if constraint == LEGACY_ENGINE_CONSTRAINT: + return None, ( + f"note: {LOCKFILE_NAME} engine constraint {constraint!r} was written before constraints were read; " + "run `chock sync --repo .` to record this engine's" + ) + match = _CONSTRAINT_RE.fullmatch(constraint) if isinstance(constraint, str) else None + running = _version_tuple(version) + if match is None or running is None: + what = f"engine {version}" if running is None else f"{LOCKFILE_NAME} engine constraint {constraint!r}" + return f"{what} cannot be read; expected `>=X.Y, str: h = hashlib.sha256() h.update(path.read_bytes()) @@ -121,7 +173,7 @@ def read_lock(repo_root: Path | None = None) -> dict[str, Any]: repo_root = repo_root or Path.cwd().resolve() path = repo_root / LOCKFILE_NAME if not path.exists(): - return {"lockfile_version": LOCKFILE_VERSION, "engine": ENGINE_CONSTRAINT, "packs": []} + return {"lockfile_version": LOCKFILE_VERSION, "engine": engine_constraint(), "packs": []} try: loaded = json.loads(path.read_text(encoding="utf-8")) except (json.JSONDecodeError, UnicodeDecodeError) as exc: @@ -189,7 +241,7 @@ def build_lock(repo_root: Path) -> dict[str, Any]: prior = _prior_provenance(repo_root) lock: dict[str, Any] = { "lockfile_version": LOCKFILE_VERSION, - "engine": ENGINE_CONSTRAINT, + "engine": engine_constraint(), "packs": [], } for pack_dir in discover_policy_dirs(repo_root): @@ -213,14 +265,22 @@ def build_lock(repo_root: Path) -> dict[str, Any]: return lock -def verify_lock(repo_root: Path | None = None) -> tuple[bool, list[str]]: - """Recompute pack and compiled-artifact hashes and report drift.""" +def _engine_failures(lock: dict[str, Any], notes: list[str] | None) -> list[str]: + """`check_engine` on a lock: its failure as a list, its note appended to `notes` when given.""" + failure, note = check_engine(lock.get("engine")) + if note and notes is not None: + notes.append(note) + return [failure] if failure else [] + + +def verify_lock(repo_root: Path | None = None, notes: list[str] | None = None) -> tuple[bool, list[str]]: + """Check the running engine against the lock's constraint, recompute hashes, and report drift.""" repo_root = repo_root or Path.cwd().resolve() try: lock = read_lock(repo_root) except LockError as exc: return False, [str(exc)] - failures: list[str] = [] + failures = _engine_failures(lock, notes) failures += [ f"vendored runtime modified ({d}) -- this is what executes gates" for d in vendored_differences(repo_root) @@ -284,7 +344,10 @@ def main(argv: list[str] | None = None) -> int: print(f"Wrote {LOCKFILE_NAME} with {len(lock['packs'])} pack(s)") return 0 if args.command == "verify": - ok, failures = verify_lock(repo_root) + notes: list[str] = [] + ok, failures = verify_lock(repo_root, notes) + for note in notes: + print(note) if ok: print("verify: all packs match lockfile") return 0 diff --git a/tests/test_lock_engine.py b/tests/test_lock_engine.py new file mode 100644 index 00000000..d99c9872 --- /dev/null +++ b/tests/test_lock_engine.py @@ -0,0 +1,119 @@ +"""chock.lock's `engine` field is derived from the running engine and enforced by `check --only verify` (IR-F71).""" + +from __future__ import annotations + +import json +from pathlib import Path + +import pytest + +import chock +from chock import lock +from chock.lifecycle import check_main +from chock.lock import ( + LEGACY_ENGINE_CONSTRAINT, + build_lock, + check_engine, + engine_constraint, + verify_lock, + write_lock, +) + + +def _locked(repo: Path, constraint: object) -> Path: + (repo / ".agents" / "policies").mkdir(parents=True) + write_lock({"lockfile_version": "2", "engine": constraint, "packs": []}, repo) + return repo + + +@pytest.mark.parametrize( + ("version", "expected"), + [("1.2.0", ">=1.2,<2"), ("1.2.7", ">=1.2,<2"), ("2.0.0rc1", ">=2.0,<3"), ("0.0.1", ">=0.0,<1")], +) +def test_constraint_is_derived_from_the_engine_version(version: str, expected: str) -> None: + assert engine_constraint(version) == expected + + +def test_written_lock_carries_the_running_engines_constraint(tmp_path: Path) -> None: + (tmp_path / ".agents" / "policies").mkdir(parents=True) + assert build_lock(tmp_path)["engine"] == engine_constraint(chock.__version__) + assert engine_constraint(chock.__version__) != LEGACY_ENGINE_CONSTRAINT + + +@pytest.mark.parametrize("version", ["1.2.0", "1.2.9", "1.9.0"]) +def test_engine_inside_the_constraint_passes(version: str) -> None: + assert check_engine(">=1.2,<2", version) == (None, None) + + +@pytest.mark.parametrize(("version", "side"), [("1.1.9", "below"), ("2.0.0", "above"), ("3.1.0", "above")]) +def test_engine_outside_the_constraint_fails_naming_engine_lock_and_fix(version: str, side: str) -> None: + failure, note = check_engine(">=1.2,<2", version) + assert note is None, side + assert failure is not None + assert "\n" not in failure + assert f"engine {version}" in failure + assert "'>=1.2,<2'" in failure + assert 'pip install "chock>=1.2,<2"' in failure + assert "chock sync --repo ." in failure + + +def test_legacy_constraint_passes_with_a_note_to_resync() -> None: + failure, note = check_engine(LEGACY_ENGINE_CONSTRAINT, "1.2.0") + assert failure is None + assert note is not None + assert "chock sync --repo ." in note + + +@pytest.mark.parametrize("constraint", ["x", "", "^1.2", ">=1.2", "<2,>=1.2", None, 12, ["1.2"]]) +def test_unparseable_constraint_fails(constraint: object) -> None: + failure, note = check_engine(constraint, "1.2.0") + assert note is None + assert failure is not None + assert "cannot be read" in failure + + +def test_verify_fails_when_the_running_engine_is_outside_the_lock( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] +) -> None: + repo = _locked(tmp_path, ">=9.0,<10") + ok, failures = verify_lock(repo) + assert not ok + assert len(failures) == 1 + assert "outside chock.lock's constraint '>=9.0,<10'" in failures[0] + monkeypatch.chdir(repo) + assert check_main(["--only", "verify", "--repo", str(repo)]) == 1 + err = capsys.readouterr().err + assert err.count("verify FAIL:") == 1 + assert f"engine {chock.__version__} is outside" in err + + +def test_verify_passes_inside_the_lock(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: + repo = _locked(tmp_path, engine_constraint(chock.__version__)) + assert verify_lock(repo) == (True, []) + assert lock.main(["verify", "--repo", str(repo)]) == 0 + assert "note:" not in capsys.readouterr().out + + +def test_verify_passes_a_legacy_lock_with_one_note(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: + repo = _locked(tmp_path, LEGACY_ENGINE_CONSTRAINT) + notes: list[str] = [] + assert verify_lock(repo, notes) == (True, []) + assert len(notes) == 1 + assert lock.main(["verify", "--repo", str(repo)]) == 0 + assert capsys.readouterr().out.count("note:") == 1 + + +def test_verify_fails_an_unparseable_constraint(tmp_path: Path) -> None: + repo = _locked(tmp_path, ">=one, None: + repo = tmp_path + (repo / ".agents" / "policies").mkdir(parents=True) + (repo / "chock.lock").write_text(json.dumps({"lockfile_version": "2", "packs": []}), encoding="utf-8") + ok, failures = verify_lock(repo) + assert not ok + assert "cannot be read" in failures[0] From f57be53141486c1139a5a81d775974f1a1c0acfc Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:31:32 +0000 Subject: [PATCH 02/57] fix(lock): finish the legacy-constraint note's sentence Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- src/chock/lock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chock/lock.py b/src/chock/lock.py index bfee1fac..f3a3b02b 100644 --- a/src/chock/lock.py +++ b/src/chock/lock.py @@ -61,7 +61,7 @@ def check_engine(constraint: object, version: str = __version__) -> tuple[str | if constraint == LEGACY_ENGINE_CONSTRAINT: return None, ( f"note: {LOCKFILE_NAME} engine constraint {constraint!r} was written before constraints were read; " - "run `chock sync --repo .` to record this engine's" + "run `chock sync --repo .` to record this engine's constraint" ) match = _CONSTRAINT_RE.fullmatch(constraint) if isinstance(constraint, str) else None running = _version_tuple(version) From 7a5dccd390968adc4f22102ca1230f4f67542aee Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:32:09 +0000 Subject: [PATCH 03/57] chock add: default to the catalog release tag (W1-B) With no --ref and the default catalog, chock add resolves the tag v to its commit, fetches that commit, prints both, and records source_commit and source_ref in chock.lock. A missing tag fails with one line naming --ref <40-hex commit> or --ref main; there is no silent fallback to the default branch. --ref and other --from sources are unchanged. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/add-default-pin.md | 5 ++ src/chock/scaffold/add.py | 61 ++++++++++++++--- src/chock/scaffold/pin.py | 16 +++++ tests/test_add_default_pin.py | 118 +++++++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+), 11 deletions(-) create mode 100644 changelog.d/add-default-pin.md create mode 100644 tests/test_add_default_pin.py diff --git a/changelog.d/add-default-pin.md b/changelog.d/add-default-pin.md new file mode 100644 index 00000000..764c56fe --- /dev/null +++ b/changelog.d/add-default-pin.md @@ -0,0 +1,5 @@ +- `chock add ` with no `--ref` from the default catalog now fetches the catalog release tag + `v`, prints the tag and the commit it resolved to, and records both in + `chock.lock`. If that tag does not exist it fails and names `--ref <40-hex commit>` or + `--ref main`; it never falls back to the moving branch. `--ref`, and `--from` another catalog, + are unchanged. diff --git a/src/chock/scaffold/add.py b/src/chock/scaffold/add.py index 2e815611..e8516931 100644 --- a/src/chock/scaffold/add.py +++ b/src/chock/scaffold/add.py @@ -12,6 +12,7 @@ import yaml +import chock from chock import yamlio from chock.config import agents_from_config as _agents_from_config from chock.lock import ( @@ -24,7 +25,7 @@ write_lock, ) from chock.output import error -from chock.scaffold.pin import PinError, fetch_catalog, redact_source +from chock.scaffold.pin import PinError, fetch_catalog, redact_source, tag_commit from chock.scaffold.recompile import BookkeepingError, recompile @@ -178,6 +179,43 @@ def record_provenance(repo_root: Path, artifact_id: str, source: str, ref: str | return +def is_default_catalog(source: str) -> bool: + """True when `source` names the default catalog, however its URL ends.""" + + def bare(url: str) -> str: + return url.rstrip("/").removesuffix(".git") + + return bare(source) == bare(DEFAULT_CATALOG) + + +def default_tag(source: str, ref: str | None) -> str | None: + """The catalog release tag to fetch when none was asked for: the default catalog only.""" + return f"v{chock.__version__}" if ref is None and is_default_catalog(source) else None + + +def resolve_default_ref(source: str, tag: str) -> str: + """The commit the default catalog's release tag names; refuse rather than take the moving branch.""" + commit = tag_commit(source, tag) + if commit is None: + msg = ( + f"chock add: catalog {redact_source(source)} has no tag {tag} for this chock; " + "pin --ref <40-hex commit>, or --ref main to take the moving branch on purpose (nothing installed)" + ) + raise PinError(msg) + return commit + + +def _report(args: argparse.Namespace, added: Added, tag: str | None) -> None: + if added.commit: + print(f" from {redact_source(args.source)} at {added.commit}" + (f" (tag {tag})" if tag else "")) + print(f" sha256 {added.sha256}") + pin = f"--ref {added.commit}, or --verify-sha {added.sha256}" + if tag and added.commit: + print(f" (a tag can move; the strong pin is {pin})") + elif not args.ref and added.commit: + print(f" (unpinned: a later run may fetch another commit. Pin with {pin})") + + def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser( prog="chock add", description="Install a policy or skill from a catalog and compile it" @@ -185,7 +223,12 @@ def main(argv: list[str] | None = None) -> int: parser.add_argument("artifact_id", help="Policy or skill id, e.g. protect-main-branch") parser.add_argument("--repo", default=".", help="Target repo root") parser.add_argument("--from", dest="source", default=DEFAULT_CATALOG, help="Catalog URL or local path") - parser.add_argument("--ref", default=None, help="Full 40-hex commit SHA (recommended), or a branch/tag (mutable)") + parser.add_argument( + "--ref", + default=None, + help="Full 40-hex commit SHA (recommended), or a branch/tag (mutable). " + "Default for the default catalog: the tag v; elsewhere: the default branch", + ) parser.add_argument("--force", action="store_true", help="Replace an artifact that is already installed") parser.add_argument("--skip-compile", action="store_true", help="Copy only; do not compile or install hooks") parser.add_argument( @@ -197,8 +240,10 @@ def main(argv: list[str] | None = None) -> int: args = parser.parse_args(argv) repo_root = Path(args.repo).resolve() + tag = default_tag(args.source, args.ref) try: - added = add(repo_root, args.artifact_id, args.source, args.ref, force=args.force, verify_sha=args.verify_sha) + ref = resolve_default_ref(args.source, tag) if tag else args.ref + added = add(repo_root, args.artifact_id, args.source, ref, force=args.force, verify_sha=args.verify_sha) except PinError as exc: print(exc, file=sys.stderr) return 2 @@ -208,13 +253,7 @@ def main(argv: list[str] | None = None) -> int: rel = added.path.relative_to(repo_root).as_posix() print(f"Added {args.artifact_id} to {rel}") - - if added.commit: - print(f" from {redact_source(args.source)} at {added.commit}") - print(f" sha256 {added.sha256}") - if not args.ref and added.commit: - pin = f"Pin with --ref {added.commit}, or --verify-sha {added.sha256}" - print(f" (unpinned: a later run may fetch another commit. {pin})") + _report(args, added, tag) if args.skip_compile: print("Run `chock sync --repo .` to compile it.") @@ -230,7 +269,7 @@ def main(argv: list[str] | None = None) -> int: except BookkeepingError as exc: error(str(exc)) return 1 - record_provenance(repo_root, args.artifact_id, args.source, args.ref, added) + record_provenance(repo_root, args.artifact_id, args.source, tag or args.ref, added) print("Compiled. Run `chock sync --repo .` to activate commit-time enforcement.") return 0 diff --git a/src/chock/scaffold/pin.py b/src/chock/scaffold/pin.py index ab3cb775..8ef2437d 100644 --- a/src/chock/scaffold/pin.py +++ b/src/chock/scaffold/pin.py @@ -160,6 +160,22 @@ def _require_published(source: str, remote: str, sha: str, into: Path) -> None: raise _pin_error(source, sha, why) +def tag_commit(source: str, tag: str) -> str | None: + """The commit tag `tag` of `source` names (peeled), or None when the source has no such tag.""" + _check_ref_syntax(source, tag) + local = local_source(source) + remote = str(local.resolve()) if local is not None else source + name = f"refs/tags/{tag}" + listed = _run(["git", *INERT, "ls-remote", "--tags", "--", remote, name, f"{name}^{{}}"], REMOTE_TIMEOUT) + if listed.returncode != 0: + detail = _scrub(listed.stderr.strip(), source) or "git step failed" + msg = f"could not list the tags of catalog {redact_source(source)}: {detail}" + raise RuntimeError(msg) + found = dict(reversed(line.split("\t", 1)) for line in listed.stdout.splitlines() if "\t" in line) + sha = found.get(f"{name}^{{}}") or found.get(name) + return sha.lower() if sha and _FULL_SHA.fullmatch(sha) else None + + def _clone_ref(source: str, remote: str, ref: str | None, into: Path) -> str | None: args = ["git", *INERT, "clone", "--quiet", "--depth", "1", "--template="] if ref: diff --git a/tests/test_add_default_pin.py b/tests/test_add_default_pin.py new file mode 100644 index 00000000..b186119c --- /dev/null +++ b/tests/test_add_default_pin.py @@ -0,0 +1,118 @@ +"""`chock add ` with no `--ref` takes the default catalog's release tag, never its moving branch.""" + +from __future__ import annotations + +import json +import shutil +from pathlib import Path + +import pytest +from add_pin_support import ID, commit, make_remote +from add_pin_support import git as _git + +import chock +from chock.lock import build_lock, write_lock +from chock.scaffold import add as add_mod + +pytestmark = pytest.mark.skipif(shutil.which("git") is None, reason="git required") + +FRAMEWORK_ROOT = Path(__file__).resolve().parents[1] +POLICY = "protect-main-branch" +VERSION = "9.8.7" +TAG = f"v{VERSION}" + + +@pytest.fixture +def catalog(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, str, str]: + """A bare catalog standing in for the default one: (path, tagged commit, newer commit on main).""" + work, _, _ = make_remote(tmp_path) + shutil.copytree(FRAMEWORK_ROOT / ".agents" / "policies" / POLICY, work / "base" / POLICY) + _git(work, "add", "-A") + _git(work, "commit", "--quiet", "-m", "policy") + tagged = _git(work, "rev-parse", "HEAD") + _git(work, "tag", "-a", TAG, "-m", "release", tagged) + newer = commit(work, "newer") + bare = tmp_path / "catalog.git" + _git(tmp_path, "clone", "--quiet", "--bare", str(work), str(bare)) + _git(bare, "config", "uploadpack.allowAnySHA1InWant", "true") + monkeypatch.setattr(add_mod, "DEFAULT_CATALOG", bare.as_uri()) + monkeypatch.setattr(chock, "__version__", VERSION) + return bare, tagged, newer + + +@pytest.fixture +def repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + r = tmp_path / "repo" + r.mkdir() + _git(r, "init", "--quiet", ".") + monkeypatch.setattr(add_mod, "_agents_from_config", lambda _root: []) + monkeypatch.setattr(add_mod, "recompile", lambda root, _agents, **_kw: write_lock(build_lock(root), root)) + return r + + +def _installed_body(repo: Path) -> str: + return (repo / ".agents" / "skills" / ID / "SKILL.md").read_text(encoding="utf-8") + + +def _entry(repo: Path) -> dict: + packs = json.loads((repo / "chock.lock").read_text(encoding="utf-8"))["packs"] + return next(p for p in packs if p["id"] == POLICY) + + +def test_no_ref_takes_the_release_tag_and_records_it(catalog, repo: Path, capsys) -> None: + _, tagged, newer = catalog + assert add_mod.main([ID, "--repo", str(repo)]) == 0 + out = capsys.readouterr().out + assert f"at {tagged} (tag {TAG})" in out + assert f"the strong pin is --ref {tagged}, or --verify-sha" in out + assert "unpinned" not in out + assert "good" in _installed_body(repo) + assert newer != tagged + assert add_mod.main([POLICY, "--repo", str(repo)]) == 0 + entry = _entry(repo) + assert entry["source_commit"] == tagged + assert entry["source_ref"] == TAG + + +def test_a_missing_tag_fails_and_never_falls_back_to_the_branch(catalog, repo: Path, capsys, monkeypatch) -> None: + monkeypatch.setattr(chock, "__version__", "0.0.1.dev0") + assert add_mod.main([ID, "--repo", str(repo)]) == 2 + err = capsys.readouterr().err.strip() + assert len(err.splitlines()) == 1 + assert "no tag v0.0.1.dev0" in err + assert "--ref <40-hex commit>" in err + assert "--ref main" in err + assert not (repo / ".agents").exists() + + +def test_an_explicit_ref_is_unchanged(catalog, repo: Path, capsys) -> None: + _, _, newer = catalog + assert add_mod.main([ID, "--repo", str(repo), "--ref", "main", "--skip-compile"]) == 0 + captured = capsys.readouterr() + assert "can move" in captured.err + assert f"at {newer}\n" in captured.out + assert "newer" in _installed_body(repo) + + +def test_another_catalog_without_ref_takes_its_default_branch(catalog, repo: Path, tmp_path: Path, capsys) -> None: + bare, _, newer = catalog + local = tmp_path / "local-catalog" + _git(tmp_path, "clone", "--quiet", str(bare), str(local)) + assert add_mod.main([ID, "--repo", str(repo), "--from", str(local), "--skip-compile"]) == 0 + out = capsys.readouterr().out + assert f"at {newer}\n" in out + assert "unpinned" in out + assert "newer" in _installed_body(repo) + + +@pytest.mark.parametrize("suffix", ["", "/", ".git"]) +def test_the_default_catalog_is_recognised_however_it_ends(suffix: str) -> None: + assert add_mod.default_tag(add_mod.DEFAULT_CATALOG + suffix, None) == f"v{chock.__version__}" + assert add_mod.default_tag(add_mod.DEFAULT_CATALOG, "main") is None + assert add_mod.default_tag("https://example.invalid/catalog", None) is None + + +def test_help_names_the_default(capsys) -> None: + with pytest.raises(SystemExit): + add_mod.main(["--help"]) + assert "v" in " ".join(capsys.readouterr().out.split()) From 47be85726702e7e61ed9f1ab92734e613dcbbb10 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:33:49 +0000 Subject: [PATCH 04/57] test(lock): import chock once in the engine-constraint tests Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- tests/test_lock_engine.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_lock_engine.py b/tests/test_lock_engine.py index d99c9872..b4a5e393 100644 --- a/tests/test_lock_engine.py +++ b/tests/test_lock_engine.py @@ -7,8 +7,7 @@ import pytest -import chock -from chock import lock +from chock import __version__, lock from chock.lifecycle import check_main from chock.lock import ( LEGACY_ENGINE_CONSTRAINT, @@ -36,8 +35,8 @@ def test_constraint_is_derived_from_the_engine_version(version: str, expected: s def test_written_lock_carries_the_running_engines_constraint(tmp_path: Path) -> None: (tmp_path / ".agents" / "policies").mkdir(parents=True) - assert build_lock(tmp_path)["engine"] == engine_constraint(chock.__version__) - assert engine_constraint(chock.__version__) != LEGACY_ENGINE_CONSTRAINT + assert build_lock(tmp_path)["engine"] == engine_constraint(__version__) + assert engine_constraint(__version__) != LEGACY_ENGINE_CONSTRAINT @pytest.mark.parametrize("version", ["1.2.0", "1.2.9", "1.9.0"]) @@ -84,11 +83,11 @@ def test_verify_fails_when_the_running_engine_is_outside_the_lock( assert check_main(["--only", "verify", "--repo", str(repo)]) == 1 err = capsys.readouterr().err assert err.count("verify FAIL:") == 1 - assert f"engine {chock.__version__} is outside" in err + assert f"engine {__version__} is outside" in err def test_verify_passes_inside_the_lock(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: - repo = _locked(tmp_path, engine_constraint(chock.__version__)) + repo = _locked(tmp_path, engine_constraint(__version__)) assert verify_lock(repo) == (True, []) assert lock.main(["verify", "--repo", str(repo)]) == 0 assert "note:" not in capsys.readouterr().out From 2f9fe9128db93e0eb4338b0fd03e81de3bf519dc Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:35:26 +0000 Subject: [PATCH 05/57] chock add: import the engine version once (CodeQL py/import-and-import-from) Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- src/chock/scaffold/add.py | 4 ++-- tests/test_add_default_pin.py | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/chock/scaffold/add.py b/src/chock/scaffold/add.py index e8516931..3ff2253c 100644 --- a/src/chock/scaffold/add.py +++ b/src/chock/scaffold/add.py @@ -12,7 +12,7 @@ import yaml -import chock +from chock import __version__ as engine_version from chock import yamlio from chock.config import agents_from_config as _agents_from_config from chock.lock import ( @@ -190,7 +190,7 @@ def bare(url: str) -> str: def default_tag(source: str, ref: str | None) -> str | None: """The catalog release tag to fetch when none was asked for: the default catalog only.""" - return f"v{chock.__version__}" if ref is None and is_default_catalog(source) else None + return f"v{engine_version}" if ref is None and is_default_catalog(source) else None def resolve_default_ref(source: str, tag: str) -> str: diff --git a/tests/test_add_default_pin.py b/tests/test_add_default_pin.py index b186119c..58ceb6c2 100644 --- a/tests/test_add_default_pin.py +++ b/tests/test_add_default_pin.py @@ -10,7 +10,6 @@ from add_pin_support import ID, commit, make_remote from add_pin_support import git as _git -import chock from chock.lock import build_lock, write_lock from chock.scaffold import add as add_mod @@ -36,7 +35,7 @@ def catalog(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, str, _git(tmp_path, "clone", "--quiet", "--bare", str(work), str(bare)) _git(bare, "config", "uploadpack.allowAnySHA1InWant", "true") monkeypatch.setattr(add_mod, "DEFAULT_CATALOG", bare.as_uri()) - monkeypatch.setattr(chock, "__version__", VERSION) + monkeypatch.setattr(add_mod, "engine_version", VERSION) return bare, tagged, newer @@ -75,7 +74,7 @@ def test_no_ref_takes_the_release_tag_and_records_it(catalog, repo: Path, capsys def test_a_missing_tag_fails_and_never_falls_back_to_the_branch(catalog, repo: Path, capsys, monkeypatch) -> None: - monkeypatch.setattr(chock, "__version__", "0.0.1.dev0") + monkeypatch.setattr(add_mod, "engine_version", "0.0.1.dev0") assert add_mod.main([ID, "--repo", str(repo)]) == 2 err = capsys.readouterr().err.strip() assert len(err.splitlines()) == 1 @@ -107,7 +106,7 @@ def test_another_catalog_without_ref_takes_its_default_branch(catalog, repo: Pat @pytest.mark.parametrize("suffix", ["", "/", ".git"]) def test_the_default_catalog_is_recognised_however_it_ends(suffix: str) -> None: - assert add_mod.default_tag(add_mod.DEFAULT_CATALOG + suffix, None) == f"v{chock.__version__}" + assert add_mod.default_tag(add_mod.DEFAULT_CATALOG + suffix, None) == f"v{add_mod.engine_version}" assert add_mod.default_tag(add_mod.DEFAULT_CATALOG, "main") is None assert add_mod.default_tag("https://example.invalid/catalog", None) is None From 7e58a4e77dff68f0bc3c79b9d4a14b62497c34b9 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:40:46 +0000 Subject: [PATCH 06/57] doctor: --live runs each installed pre-tool hook on a canary and a harmless command For each target doctor lists, read the pre-tool commands from the very config file the client reads, run them as written from the repo in that client's shell, and feed that client's own pre-tool payload. The canary is the first refusing eval case of the first guard the hook runs (there is no policy-independent refusal in the runtime that writes no state); it must be refused, by that policy. A harmless command must be allowed. The probe runs at log level off and in its own session, whose state file it removes. The runtime payloads and wire denies move from tests/runtime_payloads.py into package data; the test module re-exports them. Part of 1.2.0 (W1-D). Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- .github/workflows/windows-paths.yml | 1 + changelog.d/w1d-doctor-live.md | 10 ++ src/chock/data/commands.json | 2 +- src/chock/data/runtime_payloads.json | 157 +++++++++++++++++ src/chock/doctor/checks.py | 17 +- src/chock/doctor/cli.py | 15 +- src/chock/doctor/live.py | 246 +++++++++++++++++++++++++++ src/chock/doctor/payloads.py | 36 ++++ tests/fixtures/cli_help_golden.txt | 2 +- tests/runtime_payloads.py | 127 +------------- tests/test_doctor_live.py | 153 +++++++++++++++++ 11 files changed, 630 insertions(+), 136 deletions(-) create mode 100644 changelog.d/w1d-doctor-live.md create mode 100644 src/chock/data/runtime_payloads.json create mode 100644 src/chock/doctor/live.py create mode 100644 src/chock/doctor/payloads.py create mode 100644 tests/test_doctor_live.py diff --git a/.github/workflows/windows-paths.yml b/.github/workflows/windows-paths.yml index 04315d31..40c1e1b6 100644 --- a/.github/workflows/windows-paths.yml +++ b/.github/workflows/windows-paths.yml @@ -43,6 +43,7 @@ jobs: run: >- pytest -q tests/test_cursor_windows_write_gate.py + tests/test_doctor_live.py tests/test_repo_stop_gap1d.py tests/test_pack_hash_platform.py tests/test_install_command.py diff --git a/changelog.d/w1d-doctor-live.md b/changelog.d/w1d-doctor-live.md new file mode 100644 index 00000000..760e6e8a --- /dev/null +++ b/changelog.d/w1d-doctor-live.md @@ -0,0 +1,10 @@ +- `chock doctor --live` (preview): for each target `doctor` lists, runs the pre-tool hook command + the client's own config file holds, as written and from the repo, in the client's shell, on that + client's own payload. It sends the first refusing eval case of the first policy the hook runs (it + must be refused, by that policy) and `echo chock-doctor-live` (it must be allowed): two rows, + `live: refuses the canary` and `live: allows a harmless command`, each with the fix doctor + already prints. With no in-agent policy on the client's shell tool the canary row is skipped. It + runs no client, sends nothing anywhere, logs nothing (the probe runs at log level off and in its + own session, whose state file it removes), and does not prove the client calls the hook: that is + the heartbeat row. The per-vendor payloads and wire denies the tests used are now package data + (`chock.doctor.payloads`), one source for the engine and the tests. diff --git a/src/chock/data/commands.json b/src/chock/data/commands.json index 723cfc90..7779e2d0 100644 --- a/src/chock/data/commands.json +++ b/src/chock/data/commands.json @@ -48,7 +48,7 @@ }, "doctor": { "module": "chock.doctor.cli", - "help": "Check each client's hooks on each route: present, current, and seen running (--client X)" + "help": "Check each client's hooks on each route: present, current, and seen running (--client X); --live runs each hook on a canary" }, "incident": { "module": "chock.incident", diff --git a/src/chock/data/runtime_payloads.json b/src/chock/data/runtime_payloads.json new file mode 100644 index 00000000..957efe64 --- /dev/null +++ b/src/chock/data/runtime_payloads.json @@ -0,0 +1,157 @@ +{ + "_doc": "One shell pre-tool payload per vendored runtime (__COMMAND__, __ROOT__ swapped in), and the deny word each one's wire carries: block is the devin-family spelling, exit-2 windsurf's wordless exit-code grammar (G5). The last four payloads are agentseam's golden pre_tool payloads (tests/fixtures/golden at bebbfa6), shell tool.", + "payloads": { + "claude_code": { + "hook_event_name": "PreToolUse", + "tool_name": "Bash", + "tool_input": { + "command": "__COMMAND__" + }, + "session_id": "s", + "transcript_path": "/t", + "permission_mode": "default" + }, + "vscode_copilot": { + "hook_event_name": "PreToolUse", + "timestamp": "2026-08-31T00:00:00Z", + "tool_name": "Bash", + "tool_input": { + "command": "__COMMAND__" + }, + "tool_use_id": "1" + }, + "cursor": { + "hook_event_name": "beforeShellExecution", + "conversation_id": "a", + "generation_id": "b", + "cursor_version": "3.17.8", + "workspace_roots": [ + "__ROOT__" + ], + "command": "__COMMAND__", + "cwd": "__ROOT__", + "sandbox": false + }, + "codex_cli": { + "hook_event_name": "PreToolUse", + "turn_id": "t1", + "tool_name": "Bash", + "tool_input": { + "command": "__COMMAND__" + } + }, + "antigravity": { + "toolCall": { + "name": "run_command", + "args": { + "CommandLine": "__COMMAND__", + "Cwd": "__ROOT__" + } + }, + "conversationId": "c1", + "stepIdx": 1, + "workspacePaths": [ + "__ROOT__" + ] + }, + "devin": { + "hook_event_name": "PreToolUse", + "prompt_id": "p1", + "tool_name": "Bash", + "tool_input": { + "command": "__COMMAND__" + }, + "session_id": "s" + }, + "gemini_cli": { + "hook_event_name": "BeforeTool", + "tool_name": "run_shell_command", + "tool_input": { + "command": "__COMMAND__" + }, + "session_id": "s" + }, + "grok": { + "hookEventName": "PreToolUse", + "toolName": "Bash", + "toolInput": { + "command": "__COMMAND__" + }, + "sessionId": "s" + }, + "tabnine": { + "hook_event_name": "BeforeTool", + "timestamp": "2026-08-31T00:00:00Z", + "tool_name": "shell", + "tool_input": { + "command": "__COMMAND__" + } + }, + "windsurf": { + "trajectory_id": "t1", + "tool_info": { + "command_line": "__COMMAND__" + }, + "cwd": "__ROOT__" + }, + "factory_droid": { + "hook_event_name": "PreToolUse", + "permission_mode": "auto-medium", + "session_id": "s", + "transcript_path": "__ROOT__/.factory/projects/x/session.jsonl", + "cwd": "__ROOT__", + "tool_name": "Execute", + "tool_input": { + "command": "__COMMAND__" + } + }, + "goose": { + "event": "PreToolUse", + "matcher_context": "shell", + "session_id": "s", + "tool_call_id": "call-1", + "tool_name": "shell", + "tool_input": { + "command": "__COMMAND__" + }, + "working_dir": "__ROOT__" + }, + "openhands": { + "event_type": "PreToolUse", + "session_id": "s", + "tool_name": "terminal", + "tool_input": { + "command": "__COMMAND__" + }, + "working_dir": "__ROOT__" + }, + "mistral_vibe": { + "hook_event_name": "pre_tool", + "parent_session_id": null, + "session_id": "s", + "tool_call_id": "call-1", + "tool_name": "bash", + "tool_input": { + "command": "__COMMAND__" + }, + "transcript_path": "__ROOT__/.vibe/session.jsonl", + "cwd": "__ROOT__" + } + }, + "deny_on_the_wire": { + "claude_code": "deny", + "codex_cli": "deny", + "cursor": "deny", + "vscode_copilot": "deny", + "antigravity": "deny", + "devin": "block", + "gemini_cli": "deny", + "grok": "deny", + "tabnine": "deny", + "windsurf": "exit-2", + "factory_droid": "deny", + "goose": "block", + "openhands": "deny", + "mistral_vibe": "deny" + } +} diff --git a/src/chock/doctor/checks.py b/src/chock/doctor/checks.py index 44e14cbd..8202aea7 100644 --- a/src/chock/doctor/checks.py +++ b/src/chock/doctor/checks.py @@ -34,7 +34,8 @@ class Check: fix: str = "" -def _rebuild(target: Target) -> str: +def rebuild(target: Target) -> str: + """The one-line fix for a target whose hooks are missing, stale or broken.""" if target.route == ROUTE_SYNC: return f"run `chock sync --repo {target.root}`" return f"rebuild it: `chock install --selection --client {target.client}`" @@ -44,8 +45,8 @@ def hooks_present(target: Target) -> Check: if target.wired() and target.runtime.is_file(): return Check(PASS, "hooks", f"{target.hooks} runs {target.runtime.name}") if target.wired(): - return Check(FAIL, "hooks", f"{target.hooks} runs {target.runtime}, which is missing", _rebuild(target)) - return Check(FAIL, "hooks", f"{target.hooks} does not run {target.runtime.name}", _rebuild(target)) + return Check(FAIL, "hooks", f"{target.hooks} runs {target.runtime}, which is missing", rebuild(target)) + return Check(FAIL, "hooks", f"{target.hooks} does not run {target.runtime.name}", rebuild(target)) def _python_version(name: str) -> tuple[int, ...] | None: @@ -66,7 +67,7 @@ def _python_version(name: str) -> tuple[int, ...] | None: def launcher(target: Target) -> Check: if not target.launcher.is_file(): - return Check(FAIL, "launcher", f"{target.launcher} is missing: every hook refuses", _rebuild(target)) + return Check(FAIL, "launcher", f"{target.launcher} is missing: every hook refuses", rebuild(target)) least = ".".join(map(str, launch.MIN_PYTHON)) usable = [(n, v) for n in PYTHONS if (v := _python_version(n)) is not None and v >= launch.MIN_PYTHON] if not usable: @@ -90,16 +91,16 @@ def engine(target: Target) -> Check: try: same = path.read_text(encoding="utf-8") == expected except (OSError, UnicodeDecodeError): - return Check(FAIL, "engine", f"{path} is unreadable", _rebuild(target)) + return Check(FAIL, "engine", f"{path} is unreadable", rebuild(target)) if not same: - return Check(FAIL, "engine", f"{path.name} was not built by {_engine_names()}", _rebuild(target)) + return Check(FAIL, "engine", f"{path.name} was not built by {_engine_names()}", rebuild(target)) if target.route == ROUTE_SYNC: return Check(PASS, "engine", f"runtime matches {_engine_names()}") built = marker_engine(target) recorded, version = built.get("commit"), built.get("version") if version != __version__ or (recorded and engine_commit() and recorded != engine_commit()): what = f"chock {version}" + (f" ({str(recorded)[:12]})" if recorded else "") - return Check(WARN, "engine", f"runtime matches, but built by {what}, not {_engine_names()}", _rebuild(target)) + return Check(WARN, "engine", f"runtime matches, but built by {what}, not {_engine_names()}", rebuild(target)) return Check(PASS, "engine", f"runtime matches; built by {_engine_names()}") @@ -146,7 +147,7 @@ def _heartbeat(target: Target, beats: list[dict], hooks: list[dict], sessions: i WARN, "heartbeat", what, - f"start a new session; if none appears, the client is not loading it: {_rebuild(target)}", + f"start a new session; if none appears, the client is not loading it: {rebuild(target)}", ) since = beats[-sessions:][0]["ts"] quiet = not any(r["ts"] >= since for r in hooks) diff --git a/src/chock/doctor/cli.py b/src/chock/doctor/cli.py index 7fe57bfe..c6347144 100644 --- a/src/chock/doctor/cli.py +++ b/src/chock/doctor/cli.py @@ -1,4 +1,4 @@ -"""`chock doctor [--client X] [--repo .]`: each client's hooks on each route, PASS/WARN/FAIL with a fix.""" +"""`chock doctor [--client X] [--repo .] [--live]`: each client's hooks on each route, PASS/WARN/FAIL with a fix.""" from __future__ import annotations @@ -9,7 +9,7 @@ from pathlib import Path from chock.display import inert -from chock.doctor import checks +from chock.doctor import checks, live from chock.doctor.targets import plugin_targets, sync_targets from chock.stop_everywhere import everywhere_line @@ -18,6 +18,14 @@ def _say(line: str) -> None: sys.stdout.write(inert(line) + "\n") +_LIVE_HELP = ( + "also run each installed pre-tool hook command, exactly as the client's config file writes it, from the repo: " + "once on the first refusing eval case of the first policy it runs (it must refuse) and once on " + f"`{live.HARMLESS}` (it must allow). It runs nothing else, starts no client, sends nothing anywhere and " + "logs nothing. It does not prove the client calls the hook: that is the heartbeat row" +) + + def _parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(prog="chock doctor", description=__doc__) parser.add_argument("--repo", default=".", help="the repository whose `chock sync` hooks to check") @@ -25,6 +33,7 @@ def _parser() -> argparse.ArgumentParser: parser.add_argument("--client-version", help="the client's version, compared with what agentseam witnessed") parser.add_argument("--sessions", type=int, default=checks.SESSIONS, help="sessions the heartbeat check covers") parser.add_argument("--json", action="store_true", help="one JSON object per target") + parser.add_argument("--live", action="store_true", help=_LIVE_HELP) return parser @@ -43,6 +52,8 @@ def main(argv: list[str] | None = None) -> int: failed = False for target in targets: found = checks.run(target, repo, max(1, args.sessions), args.client_version) + if args.live: + found += live.run(target, repo) failed = failed or any(c.status == checks.FAIL for c in found) if args.json: row = {"client": target.client, "agent": target.agent, "route": target.route, "root": str(target.root)} diff --git a/src/chock/doctor/live.py b/src/chock/doctor/live.py new file mode 100644 index 00000000..a412ee27 --- /dev/null +++ b/src/chock/doctor/live.py @@ -0,0 +1,246 @@ +"""`chock doctor --live`: run each installed pre-tool hook as its client calls it, on a canary and a harmless command.""" + +from __future__ import annotations + +import json +import os +import re +import shutil +import subprocess +import tomllib +from collections.abc import Iterator +from pathlib import Path +from typing import Any + +import yaml +from agentseam import packaging + +from chock import vendors +from chock.doctor.checks import FAIL, INFO, PASS, Check, rebuild +from chock.doctor.payloads import DENY_ON_THE_WIRE, EXIT_2, payload +from chock.doctor.targets import ROUTE_PLUGIN, Target +from chock.gate.guard_runner import bash_candidates +from chock.gate.runner.journal import LOG_LEVEL_ENV, LOG_OFF +from chock.plugin import hook_dir, store +from chock.plugin.clients import CLIENTS +from chock.plugin.devin import DEVIN_PLUGIN_ROOT_VAR + +REFUSES, ALLOWS, LIVE = "live: refuses the canary", "live: allows a harmless command", "live" +#: A command every policy lets through: it reads nothing and writes nothing. +HARMLESS = "echo chock-doctor-live" +#: Seconds one hook command may take; a client's own timeouts are of this order. +TIMEOUT = 60 +#: The exit code every client reads as a refusal. +REFUSAL_EXIT = 2 +#: The probe logs nothing, so the decision log never holds a refusal no agent asked for. +QUIET = {LOG_LEVEL_ENV: LOG_OFF} +_REFERENCE = re.compile(r'--(guard|multi)\s+"?([^"\s]+)"?') +_TOKEN = re.compile(r"\$\{(\w+)\}") +_TOOL_KEYS = ("tool_name", "toolName") +_ANY = (None, "", "*") +#: The per-shell keys a hook entry may carry in place of `command` (Copilot's `bash` and `powershell`). +BASH, POWERSHELL = "bash", "powershell" +#: The probe's own session: its calls never join a real session's log, and that file is removed afterwards. +PROBE_SESSION = "chock-doctor-live" +_SESSION_KEYS = ("session_id", "sessionId", "conversation_id", "conversationId", "trajectory_id") +_STATE = (".chock", "state") + + +def _root_vars() -> list[str]: + """Every plugin-root variable a store or agentseam records: the client sets it to the plugin folder.""" + tokens = [hook_dir.layout(fmt, client.agent)["plugin_root"] or "" for fmt, client in CLIENTS.items()] + tokens += [t for a in packaging.agents() for t in packaging.layout(a)["plugin_root"]] + return sorted({name for t in tokens for name in _TOKEN.findall(t)} | {DEVIN_PLUGIN_ROOT_VAR}) + + +def _document(path: Path) -> Any: + text = path.read_text(encoding="utf-8") + return tomllib.loads(text) if path.suffix == ".toml" else json.loads(text) + + +def _matches(matcher: Any, tool: str | None) -> bool: + if matcher in _ANY or tool is None or not isinstance(matcher, str): + return True + try: + return re.fullmatch(matcher, tool) is not None + except re.error: + return True + + +def _entry(node: dict[str, Any]) -> tuple[str, str] | None: + """(the key it was read from, the command) the client runs for one hook entry on this OS.""" + for key in ("command", POWERSHELL if os.name == "nt" else BASH): + if isinstance(node.get(key), str): + return key, node[key] + return None + + +def _collect(node: Any, tool: str | None, matcher: Any = None) -> Iterator[tuple[str, str]]: + if isinstance(node, dict): + matcher = node.get("matcher", matcher) + entry = _entry(node) + if entry is not None and _matches(matcher, tool): + yield entry + for value in node.values(): + yield from _collect(value, tool, matcher) + elif isinstance(node, list): + for item in node: + yield from _collect(item, tool, matcher) + + +def _under(node: Any, event: str) -> Iterator[Any]: + if isinstance(node, dict): + for key, value in node.items(): + if key == event: + yield value + else: + yield from _under(value, event) + elif isinstance(node, list): + for item in node: + yield from _under(item, event) + + +def commands(target: Target) -> list[tuple[str, str]]: + """The pre-tool commands the client runs for its shell tool, read from the file it reads; plugin roots filled in.""" + doc = _document(target.hooks) + sample = payload(target.agent, HARMLESS) if target.agent in DENY_ON_THE_WIRE else {} + tool = next((sample[k] for k in _TOOL_KEYS if isinstance(sample.get(k), str)), None) + found = [c for part in _under(doc, vendors.shell_gate_event(target.agent)) for c in _collect(part, tool)] + if target.route != ROUTE_PLUGIN: + return found + root = hook_dir.root_path(target.root) + names = _root_vars() + return [(key, _TOKEN.sub(lambda m: root if m.group(1) in names else m.group(0), c)) for key, c in found] + + +def _guards(command: str, root: Path) -> Iterator[Path]: + for flag, ref in _REFERENCE.findall(command): + path = Path(ref) if Path(ref).is_absolute() else root / ref + if flag == "guard": + yield path + continue + try: + members = json.loads(path.read_text(encoding="utf-8")).get("members") or [] + except (OSError, ValueError, AttributeError): + continue + for member in members: + argv = (member.get("argv") if isinstance(member, dict) else None) or [] + yield from (root / argv[i + 1] for i, word in enumerate(argv[:-1]) if word == "--guard") + + +def _first_block(suite: Path) -> str | None: + try: + cases = (yaml.safe_load(suite.read_text(encoding="utf-8")) or {}).get("suite", {}).get("cases") or [] + except (OSError, yaml.YAMLError, AttributeError): + return None + for case in cases: + run = case.get("execute") if isinstance(case, dict) else None + if isinstance(run, dict) and run.get("expect") == "block" and isinstance(run.get("command"), str): + return run["command"] + return None + + +def canary(target: Target, repo: Path, found: list[tuple[str, str]]) -> tuple[str, str] | None: + """(policy id, command): the first installed guard's own first refusing eval case; None when no guard has one.""" + for _key, command in found: + for guard in _guards(command, target.root): + policy = guard.parent.parent if guard.parent.name == "implementations" else None + suites = [policy / "evals" / "suite.yaml"] if policy else [] + suites.append(repo / ".agents" / "policies" / guard.stem / "evals" / "suite.yaml") + for suite in suites: + case = _first_block(suite) + if case is not None: + return suite.parent.parent.name, case + return None + + +def _argv(agent: str, key: str, command: str) -> list[str] | str: + """The shell the client runs a hook in on this OS: sh on POSIX; on Windows PowerShell for a `powershell` entry, + cmd.exe where the store says so, else Git Bash.""" + if os.name != "nt": + return [shutil.which("sh") or "sh", "-c", command] + if key == POWERSHELL: + return [shutil.which("powershell") or "powershell", "-NoProfile", "-Command", command] + cmd = any(c.agent == agent and store.windows_hook_command(f) == hook_dir.QUOTE_FREE for f, c in CLIENTS.items()) + bash = [] if cmd else bash_candidates() + return [bash[0], "-c", command] if bash else command + + +def _probe(agent: str, asked: str, repo: Path) -> dict[str, Any]: + sent = payload(agent, asked, repo.as_posix()) + named = [key for key in _SESSION_KEYS if key in sent] or [_SESSION_KEYS[0]] + return {**sent, **dict.fromkeys(named, PROBE_SESSION)} + + +def _call(target: Target, repo: Path, entry: tuple[str, str], asked: str) -> subprocess.CompletedProcess | str: + """One hook command's answer to `asked`, or why it gave none.""" + roots = dict.fromkeys(_root_vars(), hook_dir.root_path(target.root)) if target.route == ROUTE_PLUGIN else {} + argv = _argv(target.agent, *entry) + try: + return subprocess.run( # noqa: S603 -- the hook command the client itself runs, read from its own config + argv, + shell=isinstance(argv, str), + cwd=repo, + env={**os.environ, **roots, **QUIET}, + input=json.dumps(_probe(target.agent, asked, repo)), + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + timeout=TIMEOUT, + check=False, + ) + except (OSError, subprocess.TimeoutExpired) as exc: + return f"{type(exc).__name__}: {exc}" + + +def refused(agent: str, done: subprocess.CompletedProcess) -> bool: + """Whether the answer is a refusal in `agent`'s grammar: its deny word on stdout, or exit 2.""" + word = DENY_ON_THE_WIRE[agent] + return done.returncode == REFUSAL_EXIT or (word != EXIT_2 and done.returncode == 0 and f'"{word}"' in done.stdout) + + +def _said(done: subprocess.CompletedProcess | str) -> str: + if isinstance(done, str): + return done + text = (done.stderr.strip() or done.stdout.strip()).splitlines() + return f"exit {done.returncode}" + (f": {text[0][:160]}" if text else "") + + +def _refuses(target: Target, repo: Path, found: list[tuple[str, str]], probe: tuple[str, str] | None) -> Check: + if probe is None: + return Check(INFO, LIVE, "skipped (no in-agent policy installed on its shell tool)") + policy, case = probe + answers = [_call(target, repo, c, case) for c in found] + for done in answers: + if not isinstance(done, str) and refused(target.agent, done) and policy in done.stdout + done.stderr: + return Check(PASS, REFUSES, f"{policy} refused `{case}`") + return Check(FAIL, REFUSES, f"{policy} did not refuse `{case}`: {_said(answers[0])}", rebuild(target)) + + +def _allows(target: Target, repo: Path, found: list[tuple[str, str]]) -> Check: + for done in (_call(target, repo, c, HARMLESS) for c in found): + if isinstance(done, str) or done.returncode != 0 or refused(target.agent, done): + return Check(FAIL, ALLOWS, f"`{HARMLESS}` was not allowed: {_said(done)}", rebuild(target)) + return Check(PASS, ALLOWS, f"`{HARMLESS}` allowed by {len(found)} hook command(s)") + + +def run(target: Target, repo: Path) -> list[Check]: + """The two live rows for one target, or the one row that says why it could not run.""" + if target.agent not in DENY_ON_THE_WIRE: + return [Check(INFO, LIVE, f"skipped (no recorded {target.agent} pre-tool payload)")] + try: + found = commands(target) + except (OSError, ValueError, tomllib.TOMLDecodeError) as exc: + return [Check(FAIL, LIVE, f"{target.hooks} is unreadable ({exc})", rebuild(target))] + if not found: + return [Check(FAIL, LIVE, f"{target.hooks} holds no pre-tool hook command", rebuild(target))] + logs = [ + base.joinpath(*_STATE, PROBE_SESSION + suffix) for base in {repo, target.root} for suffix in (".jsonl", ".tmp") + ] + fresh = [path for path in logs if not path.exists()] + try: + return [_refuses(target, repo, found, canary(target, repo, found)), _allows(target, repo, found)] + finally: + for path in fresh: + path.unlink(missing_ok=True) diff --git a/src/chock/doctor/payloads.py b/src/chock/doctor/payloads.py new file mode 100644 index 00000000..8769cee5 --- /dev/null +++ b/src/chock/doctor/payloads.py @@ -0,0 +1,36 @@ +"""Each vendored runtime's own shell pre-tool payload, and the deny its wire carries: read by `doctor --live` and the tests.""" + +from __future__ import annotations + +import json +from functools import partial +from typing import Any + +from chock.resources import package_data_dir + +COMMAND, ROOT = "__COMMAND__", "__ROOT__" +#: The root a payload names when the caller gives none: what the tests swap for their own repository. +DEFAULT_ROOT = "/x" +#: The wordless deny: the client reads exit code 2 as the refusal. +EXIT_2 = "exit-2" + +_DATA = json.loads(package_data_dir("chock", "data").joinpath("runtime_payloads.json").read_text(encoding="utf-8")) + + +def _fill(value: Any, command: str, root: str) -> Any: + if isinstance(value, str): + return command if value == COMMAND else value.replace(ROOT, root) + if isinstance(value, list): + return [_fill(item, command, root) for item in value] + if isinstance(value, dict): + return {key: _fill(item, command, root) for key, item in value.items()} + return value + + +def payload(agent: str, command: str, root: str = DEFAULT_ROOT) -> dict[str, Any]: + """`agent`'s pre-tool payload for one shell `command`, its paths naming `root`.""" + return _fill(_DATA["payloads"][agent], command, root) + + +PAYLOADS = {agent: partial(payload, agent) for agent in _DATA["payloads"]} +DENY_ON_THE_WIRE: dict[str, str] = dict(_DATA["deny_on_the_wire"]) diff --git a/tests/fixtures/cli_help_golden.txt b/tests/fixtures/cli_help_golden.txt index 9c48e9ee..b0080895 100644 --- a/tests/fixtures/cli_help_golden.txt +++ b/tests/fixtures/cli_help_golden.txt @@ -12,7 +12,7 @@ Everyday: status Policy states and coverage (--only registry,log for more) stop Refuse every agent tool call, commit and push here until resume (--agent C for one client) resume Lift a stop: agents are judged by the policies again - doctor Check each client's hooks on each route: present, current, and seen running (--client X) + doctor Check each client's hooks on each route: present, current, and seen running (--client X); --live runs each hook on a canary incident An ordered, redacted timeline from the local decision log (incident --since T [--until T] [--share]) witness Export one client's redacted decision log for its agentseam evidence issue (witness export --client C) enable Enable a policy by id diff --git a/tests/runtime_payloads.py b/tests/runtime_payloads.py index b2efa3cb..651faec2 100644 --- a/tests/runtime_payloads.py +++ b/tests/runtime_payloads.py @@ -1,128 +1,7 @@ -"""One shell pre-tool payload per vendored runtime, and the deny word each one's wire carries.""" +"""The runtime payloads and wire denies live in the package (`chock.doctor.payloads`); this keeps the old import path.""" from __future__ import annotations -PAYLOADS = { - "claude_code": lambda c: { - "hook_event_name": "PreToolUse", - "tool_name": "Bash", - "tool_input": {"command": c}, - "session_id": "s", - "transcript_path": "/t", - "permission_mode": "default", - }, - "vscode_copilot": lambda c: { - "hook_event_name": "PreToolUse", - "timestamp": "2026-08-31T00:00:00Z", - "tool_name": "Bash", - "tool_input": {"command": c}, - "tool_use_id": "1", - }, - "cursor": lambda c: { - "hook_event_name": "beforeShellExecution", - "conversation_id": "a", - "generation_id": "b", - "cursor_version": "3.17.8", - "workspace_roots": ["/x"], - "command": c, - "cwd": "/x", - "sandbox": False, - }, - "codex_cli": lambda c: { - "hook_event_name": "PreToolUse", - "turn_id": "t1", - "tool_name": "Bash", - "tool_input": {"command": c}, - }, - "antigravity": lambda c: { - "toolCall": {"name": "run_command", "args": {"CommandLine": c, "Cwd": "/x"}}, - "conversationId": "c1", - "stepIdx": 1, - "workspacePaths": ["/x"], - }, - "devin": lambda c: { - "hook_event_name": "PreToolUse", - "prompt_id": "p1", - "tool_name": "Bash", - "tool_input": {"command": c}, - "session_id": "s", - }, - "gemini_cli": lambda c: { - "hook_event_name": "BeforeTool", - "tool_name": "run_shell_command", - "tool_input": {"command": c}, - "session_id": "s", - }, - "grok": lambda c: { - "hookEventName": "PreToolUse", - "toolName": "Bash", - "toolInput": {"command": c}, - "sessionId": "s", - }, - "tabnine": lambda c: { - "hook_event_name": "BeforeTool", - "timestamp": "2026-08-31T00:00:00Z", - "tool_name": "shell", - "tool_input": {"command": c}, - }, - "windsurf": lambda c: { - "trajectory_id": "t1", - "tool_info": {"command_line": c}, - "cwd": "/x", - }, - # The next four: agentseam's golden pre_tool payloads (tests/fixtures/golden at bebbfa6), shell tool. - "factory_droid": lambda c: { - "hook_event_name": "PreToolUse", - "permission_mode": "auto-medium", - "session_id": "s", - "transcript_path": "/x/.factory/projects/x/session.jsonl", - "cwd": "/x", - "tool_name": "Execute", - "tool_input": {"command": c}, - }, - "goose": lambda c: { - "event": "PreToolUse", - "matcher_context": "shell", - "session_id": "s", - "tool_call_id": "call-1", - "tool_name": "shell", - "tool_input": {"command": c}, - "working_dir": "/x", - }, - "openhands": lambda c: { - "event_type": "PreToolUse", - "session_id": "s", - "tool_name": "terminal", - "tool_input": {"command": c}, - "working_dir": "/x", - }, - "mistral_vibe": lambda c: { - "hook_event_name": "pre_tool", - "parent_session_id": None, - "session_id": "s", - "tool_call_id": "call-1", - "tool_name": "bash", - "tool_input": {"command": c}, - "transcript_path": "/x/.vibe/session.jsonl", - "cwd": "/x", - }, -} +from chock.doctor.payloads import DENY_ON_THE_WIRE, PAYLOADS -#: The deny each vendor's wire actually carries, witnessed by the deny-guard fixture below: -#: `block` is the devin-family spelling, `exit-2` windsurf's wordless exit-code grammar (G5). -DENY_ON_THE_WIRE = { - "claude_code": "deny", - "codex_cli": "deny", - "cursor": "deny", - "vscode_copilot": "deny", - "antigravity": "deny", - "devin": "block", - "gemini_cli": "deny", - "grok": "deny", - "tabnine": "deny", - "windsurf": "exit-2", - "factory_droid": "deny", - "goose": "block", - "openhands": "deny", - "mistral_vibe": "deny", -} +__all__ = ["DENY_ON_THE_WIRE", "PAYLOADS"] diff --git a/tests/test_doctor_live.py b/tests/test_doctor_live.py new file mode 100644 index 00000000..0ae7ee57 --- /dev/null +++ b/tests/test_doctor_live.py @@ -0,0 +1,153 @@ +"""W1-D: `chock doctor --live` runs each installed pre-tool hook as its client would, on a canary and a harmless call.""" + +from __future__ import annotations + +import json +import shutil +from pathlib import Path + +import pytest +from conftest import baseline_policy, init_repo + +from chock.doctor import checks, cli, live +from chock.doctor.payloads import DENY_ON_THE_WIRE, PAYLOADS, payload +from chock.doctor.targets import ROUTE_PLUGIN, Target, sync_targets +from chock.hooks import launch +from chock.scaffold.recompile import recompile + +pytestmark = pytest.mark.skipif(shutil.which("git") is None, reason="git required") + +POLICY = "block-no-verify" +AGENT = "claude_code" + + +@pytest.fixture +def synced(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + """A repository `sync` wired for Claude Code with one in-agent policy, and a private HOME.""" + (tmp_path / "repo").mkdir() + repo = init_repo(tmp_path / "repo") + shutil.copytree(baseline_policy(POLICY), repo / ".agents" / "policies" / POLICY) + home = tmp_path / "home" + home.mkdir() + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("USERPROFILE", str(home)) + monkeypatch.delenv("CHOCK_GATE_LOG", raising=False) + monkeypatch.setenv("CHOCK_LOG_LEVEL", "info") + recompile(repo, ["claude"], skip_hooks=False) + return repo + + +def _tree(repo: Path) -> dict[str, bytes]: + return { + p.relative_to(repo).as_posix(): p.read_bytes() for p in repo.rglob("*") if p.is_file() and ".git" not in p.parts + } + + +def _live(repo: Path) -> dict[str, checks.Check]: + [target] = [t for t in sync_targets(repo) if t.agent == AGENT] + return {c.name: c for c in live.run(target, repo)} + + +def test_an_installed_policy_refuses_its_own_canary_and_allows_a_harmless_command(synced: Path) -> None: + found = _live(synced) + assert found[live.REFUSES].status == checks.PASS, found + assert POLICY in found[live.REFUSES].detail + assert found[live.ALLOWS].status == checks.PASS, found + + +def test_the_probe_writes_nothing_in_the_repo_or_its_decision_log(synced: Path) -> None: + before = _tree(synced) + _live(synced) + assert _tree(synced) == before + + +def test_the_cli_prints_both_rows_and_json_carries_them(synced: Path, capsys: pytest.CaptureFixture[str]) -> None: + assert cli.main(["--repo", str(synced), "--client", AGENT, "--live"]) in (0, 1) + text = capsys.readouterr().out + assert f"PASS {live.REFUSES}: {POLICY} refused" in text + assert f"PASS {live.ALLOWS}:" in text + cli.main(["--repo", str(synced), "--client", AGENT, "--live", "--json"]) + [row] = [json.loads(line) for line in capsys.readouterr().out.splitlines() if line.startswith("{")] + statuses = {c["name"]: c["status"] for c in row["checks"]} + assert statuses[live.REFUSES] == statuses[live.ALLOWS] == checks.PASS + + +def test_without_live_no_hook_is_run(synced: Path, capsys: pytest.CaptureFixture[str]) -> None: + cli.main(["--repo", str(synced), "--client", AGENT]) + assert "live:" not in capsys.readouterr().out + + +def test_a_broken_launcher_path_fails_both_rows_with_the_sync_fix(synced: Path) -> None: + (synced / launch.LAUNCHER_REL).unlink() + found = _live(synced) + for name in (live.REFUSES, live.ALLOWS): + assert found[name].status == checks.FAIL, found + assert found[name].fix == f"run `chock sync --repo {synced}`" + assert "launch.sh" in found[live.REFUSES].detail + + +def test_a_refusal_by_anything_but_the_policy_is_not_a_pass(synced: Path) -> None: + settings = synced / ".claude" / "settings.json" + doc = json.loads(settings.read_text(encoding="utf-8")) + for group in doc["hooks"]["PreToolUse"]: + for hook in group["hooks"]: + hook["command"] = hook["command"].replace(".chock/bin/claude_code.py", ".chock/bin/gone.py") + settings.write_text(json.dumps(doc), encoding="utf-8") + found = _live(synced) + assert found[live.REFUSES].status == checks.FAIL + assert found[live.ALLOWS].status == checks.FAIL + + +def test_no_guard_on_the_shell_tool_is_reported_skipped(synced: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(live, "canary", lambda *_: None) + found = _live(synced) + assert found[live.LIVE].status == checks.INFO + assert found[live.LIVE].detail == "skipped (no in-agent policy installed on its shell tool)" + assert found[live.ALLOWS].status == checks.PASS + + +def test_a_hooks_file_with_no_pre_tool_command_fails(synced: Path) -> None: + (synced / ".claude" / "settings.json").write_text(json.dumps({"hooks": {}}), encoding="utf-8") + [target] = [t for t in sync_targets(synced) if t.agent == AGENT] + [row] = live.run(target, synced) + assert row.status == checks.FAIL + assert "no pre-tool hook command" in row.detail + + +def test_a_plugin_command_has_its_root_token_filled_in_and_its_matcher_honoured(tmp_path: Path) -> None: + folder = tmp_path / "plugin dir" + hooks = folder / "hooks" / "hooks.json" + hooks.parent.mkdir(parents=True) + doc = { + "hooks": { + "PreToolUse": [ + {"matcher": "Bash", "hooks": [{"command": 'sh "${CLAUDE_PLUGIN_ROOT}/scripts/launch.sh" --guard x'}]}, + {"matcher": "Write", "hooks": [{"command": "never"}]}, + ], + "Stop": [{"hooks": [{"command": "stop"}]}], + } + } + hooks.write_text(json.dumps(doc), encoding="utf-8") + target = Target("claude-code", AGENT, ROUTE_PLUGIN, folder, folder, folder, hooks) + assert live.commands(target) == [("command", f'sh "{folder.as_posix()}/scripts/launch.sh" --guard x')] + + +def test_copilot_s_per_shell_entry_is_read() -> None: + assert live._entry({"bash": "b", "powershell": "p"}) in (("bash", "b"), ("powershell", "p")) + assert live._entry({"command": "c", "bash": "b"}) == ("command", "c") + + +def test_the_payloads_are_one_source_with_the_old_test_module() -> None: + import runtime_payloads + + assert runtime_payloads.PAYLOADS is PAYLOADS + assert runtime_payloads.DENY_ON_THE_WIRE is DENY_ON_THE_WIRE + assert set(PAYLOADS) == set(DENY_ON_THE_WIRE) + assert PAYLOADS["cursor"]("ls")["workspace_roots"] == ["/x"] + assert payload("factory_droid", "ls", "/r")["transcript_path"] == "/r/.factory/projects/x/session.jsonl" + + +def test_the_probe_names_its_own_session() -> None: + for agent in PAYLOADS: + sent = live._probe(agent, "ls", Path("/r")) + assert live.PROBE_SESSION in sent.values(), agent From 180213af1b1b339288b6251794989f7bac757599 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:44:10 +0000 Subject: [PATCH 07/57] fix: say each defaulted manifest field once, with its path; ASCII symbols on a non-UTF console IR-F75: every manifest_default print site (index builder, registry scan, compiler, toggles, validation report) goes through manifest.warn_defaults, which names the artifact and says each (artifact, field) pair once per command. The index builder's copies had no path; the validation report skips the ones already printed and dedupes its own findings. IR-F76: output.py decides once whether a stream is UTF. On any other stream chock's own symbols (U+2026, em dash, arrows, section sign, box drawing) print as ASCII; the eval table's truncation uses output.ellipsis(). Other text is left to the stream's encoder as before. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- .../ir-f75-f76-quiet-legible-output.md | 5 + src/chock/cli.py | 2 + src/chock/compile/compiler.py | 9 +- src/chock/eval/report.py | 3 +- src/chock/index/builder.py | 8 +- src/chock/manifest.py | 26 +++++ src/chock/output.py | 71 +++++++++++++- src/chock/pipe.py | 4 +- src/chock/registry/core.py | 4 +- src/chock/toggles.py | 9 +- src/chock/validation/checks_policy_toggles.py | 4 +- src/chock/validation/engine.py | 4 +- src/chock/validation/report.py | 11 +++ tests/test_output_legible_once.py | 98 +++++++++++++++++++ 14 files changed, 234 insertions(+), 24 deletions(-) create mode 100644 changelog.d/ir-f75-f76-quiet-legible-output.md create mode 100644 tests/test_output_legible_once.py diff --git a/changelog.d/ir-f75-f76-quiet-legible-output.md b/changelog.d/ir-f75-f76-quiet-legible-output.md new file mode 100644 index 00000000..479d6609 --- /dev/null +++ b/changelog.d/ir-f75-f76-quiet-legible-output.md @@ -0,0 +1,5 @@ +- Each defaulted manifest field is warned about once per command, always with the artifact's path: + `chock check` no longer repeats a skill's eleven `manifest_default` warnings, two copies of them with + no path (IR-F75). The registry scan's copy now goes to stderr with the others. +- On a console stream that is not UTF (a Windows code page such as cp1252), chock prints ASCII for its + own symbols: the eval table's truncation reads `...`, not U+2026 shown as another letter (IR-F76). diff --git a/src/chock/cli.py b/src/chock/cli.py index a77eaf3d..61ad12cc 100644 --- a/src/chock/cli.py +++ b/src/chock/cli.py @@ -11,6 +11,7 @@ from pathlib import Path from chock import __version__ +from chock.output import new_command from chock.pipe import guard_stdout, silence_interpreter_flush from chock.resources import package_data_dir from chock.stop_common import committed_refused, person_only_refused @@ -97,6 +98,7 @@ def _is_sole_flag_form(command: str, argv: list[str]) -> bool: def main(argv: list[str] | None = None) -> int: writer = guard_stdout() + new_command() try: return _dispatch(list(sys.argv[1:] if argv is None else argv)) finally: diff --git a/src/chock/compile/compiler.py b/src/chock/compile/compiler.py index 8f47fdb7..8ba395bd 100644 --- a/src/chock/compile/compiler.py +++ b/src/chock/compile/compiler.py @@ -31,8 +31,8 @@ ) from chock.config import agents_from_config from chock.hooks.in_agent_install import WIRED_VENDORS, installed_policy_ids -from chock.manifest import ManifestSourceError, load_manifest -from chock.output import error, warn +from chock.manifest import ManifestSourceError, load_manifest, warn_defaults +from chock.output import error from chock.policy_id import InvalidPolicyIdError, validate_policy_id from chock.scaffold.install_ci import ci_workflow_installed from chock.vendors import CHOCK_AGENT @@ -73,9 +73,8 @@ def _load_manifest(policy_dir: Path) -> dict[str, Any]: return {} if result is None: return {} - data, _ = result - for warning in warnings: - warn(f"{policy_dir}: manifest_default: {warning}") + data, manifest_path = result + warn_defaults(manifest_path, warnings) return data diff --git a/src/chock/eval/report.py b/src/chock/eval/report.py index 632431d4..6d0b5c6c 100644 --- a/src/chock/eval/report.py +++ b/src/chock/eval/report.py @@ -6,6 +6,7 @@ from typing import Any from chock.eval.model import PolicyResult +from chock.output import ellipsis MARK = {"pass": "PASS", "fail": "FAIL", "pending": "PEND", "skipped": "SKIP", "error": "ERR "} @@ -13,7 +14,7 @@ def _trim(detail: str) -> str: - return detail if len(detail) <= _DETAIL_WIDTH else detail[: _DETAIL_WIDTH - 1] + "…" + return detail if len(detail) <= _DETAIL_WIDTH else detail[: _DETAIL_WIDTH - len(ellipsis())] + ellipsis() def render_text(policies: list[PolicyResult], *, verbose: bool = False) -> str: diff --git a/src/chock/index/builder.py b/src/chock/index/builder.py index 067987b3..1599a4df 100644 --- a/src/chock/index/builder.py +++ b/src/chock/index/builder.py @@ -12,7 +12,7 @@ from chock.compile.surface_kinds import Surface from chock.config import _disabled_list, load_config, policy_status from chock.gate.build import build_gate_json -from chock.manifest import ManifestSourceError, load_manifest +from chock.manifest import ManifestSourceError, load_manifest, warn_defaults from chock.output import warn _ENFORCEMENT_ORDER = {"block": 0, "verify": 1, "advise": 2} @@ -47,15 +47,17 @@ def _default_id(artifact_dir: Path, manifest: dict[str, Any]) -> str: def _load_policy(root: Path, artifact_dir: Path, warnings: list[str]) -> tuple[dict[str, Any], str] | None: - """Return (manifest, manifest_relative_path) or None on any failure.""" + """Return (manifest, manifest_relative_path) or None on any failure; defaulted fields are printed, once, here.""" + defaults: list[str] = [] try: - result = load_manifest(artifact_dir, warnings=warnings) + result = load_manifest(artifact_dir, warnings=defaults) except (yaml.YAMLError, OSError, ManifestSourceError) as exc: warnings.append(f"{artifact_dir}: manifest_parse: {exc}") return None if result is None: return None manifest, manifest_path = result + warn_defaults(manifest_path, defaults) rel_path = manifest_path.relative_to(root).as_posix() return manifest, rel_path diff --git a/src/chock/manifest.py b/src/chock/manifest.py index f58ada09..1a26c5a9 100644 --- a/src/chock/manifest.py +++ b/src/chock/manifest.py @@ -8,6 +8,7 @@ import yaml from chock import yamlio +from chock.output import warn_once from chock.skill_metadata import _as_bool, _as_list, _chock_metadata CANONICAL_MANIFEST = "manifest.yaml" @@ -20,6 +21,10 @@ DETERMINIZATION_REVIEWED_KEY = "determinization_reviewed" AGENT_SPECIFIC_VOCABULARY_KEY = "agent_specific_vocabulary" +#: Finding check name for a field the manifest left out and chock filled in. +MANIFEST_DEFAULT = "manifest_default" +_ARTIFACT_FILES = frozenset({CANONICAL_MANIFEST, SKILL_MD, INTERFACE_YAML}) + #: Longest a manifest/frontmatter `name` may be before it's flagged. _MAX_NAME_LENGTH = 128 @@ -28,6 +33,27 @@ class ManifestSourceError(Exception): """A manifest directory has an ambiguous or incomplete source of truth.""" +def artifact_key(path: Path | str) -> str: + """One artifact's identity whether a caller names its folder or its manifest file.""" + found = Path(path) + return (found.parent if found.name in _ARTIFACT_FILES else found).resolve().as_posix() + + +def shown_path(path: Path | str) -> str: + """`path` relative to the working directory when it is under it.""" + found = Path(path) + try: + return found.resolve().relative_to(Path.cwd().resolve()).as_posix() + except ValueError: + return found.as_posix() + + +def warn_defaults(path: Path | str, warnings: list[str]) -> None: + """Print each defaulted field once per command, always with the artifact's path.""" + for warning in warnings: + warn_once((artifact_key(path), warning), f"{shown_path(path)}: {MANIFEST_DEFAULT}: {warning}") + + def _set_or_default( data: dict[str, Any], dotted_path: str, diff --git a/src/chock/output.py b/src/chock/output.py index 30779923..0356a849 100644 --- a/src/chock/output.py +++ b/src/chock/output.py @@ -1,8 +1,10 @@ -"""Shared stderr diagnostics: one place owning the `[WARN]`/`[ERROR]` prefix convention.""" +"""Shared console output: the `[WARN]`/`[ERROR]` prefix, say-once diagnostics, and text a console can show.""" from __future__ import annotations +import codecs import sys +from typing import TextIO # CodeQL (py/clear-text-logging-sensitive-data) flags both prints below by name-based # heuristic, not by an actual credential flow: every caller passes a diagnostic string -- @@ -16,12 +18,75 @@ # pyproject.toml: this is the designated output surface, and the codeql[...] suppression # comments below read as commented-out code to ERA001. +#: Every non-ASCII character chock's own strings print, and its ASCII stand-in. +ASCII_FOR = str.maketrans( + { + "\u2026": "...", + "\u2014": "--", + "\u2013": "-", + "\u2192": "->", + "\u2190": "<-", + "\u2264": "<=", + "\u2265": ">=", + "\u2208": " in ", + "\u2227": " and ", + "\u00a7": "s.", + "\u2500": "-", + "\u2502": "|", + "\u251c": "+", + "\u2514": "+", + "\u200c": "", + "\u200d": "", + "\ufeff": "", + } +) + +_said: set[tuple[str, ...]] = set() + + +def unicode_ok(stream: TextIO | None = None) -> bool: + """True only for a UTF stream: a legacy code page (cp1252) can encode U+2026 yet the console shows it as another letter.""" + encoding = getattr(stream if stream is not None else sys.stdout, "encoding", None) + try: + return codecs.lookup(encoding or "ascii").name.startswith("utf") + except LookupError: + return False + + +def legible(text: str, stream: TextIO | None = None) -> str: + """`text` for `stream`: on a non-UTF stream chock's own symbols become ASCII; other text is the stream's to encode.""" + if text.isascii() or unicode_ok(stream): + return text + return text.translate(ASCII_FOR) + + +def ellipsis(stream: TextIO | None = None) -> str: + return legible("\u2026", stream) + + +def first_time(key: tuple[str, ...]) -> bool: + """True the first time this process asks about `key`; a fact is then said once per command.""" + if key in _said: + return False + _said.add(key) + return True + + +def new_command() -> None: + """Forget what was said: the once-per-command rule restarts with each `chock` command run in this process.""" + _said.clear() + def warn(message: str) -> None: # codeql[py/clear-text-logging-sensitive-data] - print(f"[WARN] {message}", file=sys.stderr) + print(f"[WARN] {legible(message, sys.stderr)}", file=sys.stderr) + + +def warn_once(key: tuple[str, ...], message: str) -> None: + if first_time(key): + warn(message) def error(message: str) -> None: # codeql[py/clear-text-logging-sensitive-data] - print(f"[ERROR] {message}", file=sys.stderr) + print(f"[ERROR] {legible(message, sys.stderr)}", file=sys.stderr) diff --git a/src/chock/pipe.py b/src/chock/pipe.py index b5b06606..b62efe71 100644 --- a/src/chock/pipe.py +++ b/src/chock/pipe.py @@ -7,6 +7,8 @@ import sys from typing import Any, TextIO +from chock.output import legible + def _is_broken_pipe(exc: OSError) -> bool: return isinstance(exc, BrokenPipeError) or exc.errno in (errno.EPIPE, errno.EINVAL) @@ -24,7 +26,7 @@ def write(self, text: str) -> int: return len(text) try: try: - return self._stream.write(text) + return self._stream.write(legible(text, self._stream)) except UnicodeEncodeError: encoding = getattr(self._stream, "encoding", None) or "ascii" return self._stream.write(text.encode(encoding, "backslashreplace").decode(encoding)) diff --git a/src/chock/registry/core.py b/src/chock/registry/core.py index 4d4814e9..11014ef2 100644 --- a/src/chock/registry/core.py +++ b/src/chock/registry/core.py @@ -16,6 +16,7 @@ ManifestSourceError, load_manifest, resolve_manifest_path, + warn_defaults, ) REGISTRY_DIR = Path(".chock") @@ -195,8 +196,7 @@ def scan( except (yaml.YAMLError, OSError, ManifestSourceError) as exc: skips.append(SkippedManifest(manifest, f"{type(exc).__name__}: {exc}")) continue - for warning in warnings: - print(f"[WARN] {entry.path} :: manifest_default: {warning}") + warn_defaults(manifest, warnings) entries.setdefault(entry.id, []).append(entry) return entries, skips diff --git a/src/chock/toggles.py b/src/chock/toggles.py index 93d21baa..19dae6f0 100644 --- a/src/chock/toggles.py +++ b/src/chock/toggles.py @@ -17,8 +17,8 @@ from chock.display import inert from chock.gate.build import CatastrophicPatternError from chock.index.cli import cmd_refresh -from chock.manifest import ManifestSourceError, load_manifest -from chock.output import error, warn +from chock.manifest import ManifestSourceError, load_manifest, warn_defaults +from chock.output import error from chock.policies import discover_policy_dirs from chock.scaffold.adapters import parse_agent_selection from chock.scaffold.recompile import BookkeepingError, compiled_differences, recompile @@ -44,9 +44,8 @@ def _load_manifest(pack_dir: Path) -> dict[str, Any]: return {} if result is None: return {} - data, _ = result - for warning in warnings: - warn(f"{pack_dir}: manifest_default: {warning}") + data, manifest_path = result + warn_defaults(manifest_path, warnings) return data diff --git a/src/chock/validation/checks_policy_toggles.py b/src/chock/validation/checks_policy_toggles.py index ed524a9b..6e37d983 100644 --- a/src/chock/validation/checks_policy_toggles.py +++ b/src/chock/validation/checks_policy_toggles.py @@ -9,7 +9,7 @@ from chock.compile.surface_kinds import Surface from chock.config import load_config -from chock.manifest import CANONICAL_MANIFEST, ManifestSourceError, load_manifest +from chock.manifest import CANONICAL_MANIFEST, MANIFEST_DEFAULT, ManifestSourceError, load_manifest from chock.scaffold.recompile import discover_policy_dirs from chock.validation.report import Finding, Report @@ -30,7 +30,7 @@ def _load_manifest(pack_dir: Path, report: Report | None = None) -> dict[str, An data, manifest_path = result if report is not None: for warning in warnings: - report.add(Finding(str(manifest_path), "manifest_default", "warning", warning)) + report.add(Finding(str(manifest_path), MANIFEST_DEFAULT, "warning", warning)) return data diff --git a/src/chock/validation/engine.py b/src/chock/validation/engine.py index 90ff07ad..80797f01 100644 --- a/src/chock/validation/engine.py +++ b/src/chock/validation/engine.py @@ -10,7 +10,7 @@ import yaml -from chock.manifest import MANIFEST_NAMES, ManifestSourceError, load_manifest +from chock.manifest import MANIFEST_DEFAULT, MANIFEST_NAMES, ManifestSourceError, load_manifest from chock.validation.checks_content import ( check_description_parity, check_no_agent_specific_leakage, @@ -87,7 +87,7 @@ def validate_artifact( manifest, manifest_path = result for warning in warnings: - report.add(Finding(str(manifest_path or artifact_dir), "manifest_default", "warning", warning)) + report.add(Finding(str(manifest_path or artifact_dir), MANIFEST_DEFAULT, "warning", warning)) schema_name = { "rule": MANIFEST_SCHEMA, diff --git a/src/chock/validation/report.py b/src/chock/validation/report.py index 084a14eb..29bebbdc 100644 --- a/src/chock/validation/report.py +++ b/src/chock/validation/report.py @@ -5,6 +5,9 @@ import json from dataclasses import dataclass, field +from chock.manifest import MANIFEST_DEFAULT, artifact_key +from chock.output import first_time + @dataclass class Finding: @@ -19,8 +22,14 @@ class Report: errors: list[Finding] = field(default_factory=list) warnings: list[Finding] = field(default_factory=list) infos: list[Finding] = field(default_factory=list) + _defaults: set[tuple[str, str]] = field(default_factory=set, repr=False) def add(self, finding: Finding) -> None: + if finding.check == MANIFEST_DEFAULT: + key = (artifact_key(finding.path), finding.message) + if key in self._defaults: + return + self._defaults.add(key) if finding.severity == "error": self.errors.append(finding) elif finding.severity == "warning": @@ -57,6 +66,8 @@ def emit(report: Report, *, use_json: bool) -> None: for finding in report.errors: print(f" [ERROR] {finding.path} :: {finding.check}: {finding.message}") for finding in report.warnings: + if finding.check == MANIFEST_DEFAULT and not first_time((artifact_key(finding.path), finding.message)): + continue # already printed, with its path, earlier in this command print(f" [WARN] {finding.path} :: {finding.check}: {finding.message}") for finding in report.infos: print(f" [INFO] {finding.path} :: {finding.check}: {finding.message}") diff --git a/tests/test_output_legible_once.py b/tests/test_output_legible_once.py new file mode 100644 index 00000000..1308e405 --- /dev/null +++ b/tests/test_output_legible_once.py @@ -0,0 +1,98 @@ +"""IR-F75: each defaulted manifest field is said once per command, with its path. IR-F76: no character a console cannot show.""" + +from __future__ import annotations + +import io +import re +import subprocess +import sys +from pathlib import Path + +import pytest + +from chock.cli import main +from chock.eval.model import Case, CaseResult, PolicyResult +from chock.eval.report import render_text +from chock.output import ellipsis, legible, new_command, warn +from chock.pipe import PipeTolerantWriter + +DEFAULTED = re.compile(r"defaulted (\S+) to ") +SKILL = ".agents/skills/demo" + + +def _repo_with_bare_skill(root: Path) -> Path: + subprocess.run(["git", "init", "-q", str(root)], check=True) + skill = root / SKILL + skill.mkdir(parents=True) + (skill / "SKILL.md").write_text("---\nname: demo\ndescription: A demo skill.\n---\n# Demo\n", encoding="utf-8") + return root + + +def _cp1252() -> io.TextIOWrapper: + return io.TextIOWrapper(io.BytesIO(), encoding="cp1252") + + +def test_check_says_each_defaulted_field_once_and_names_the_skill(tmp_path: Path, capsys, monkeypatch) -> None: + repo = _repo_with_bare_skill(tmp_path) + monkeypatch.chdir(repo) + + main(["check", "--repo", "."]) + + captured = capsys.readouterr() + lines = (captured.err + captured.out).splitlines() + defaulted = [line for line in lines if DEFAULTED.search(line)] + fields = [DEFAULTED.search(line).group(1) for line in defaulted] + assert "version" in fields + assert len(fields) == len(set(fields)), defaulted + assert all(SKILL in line for line in defaulted), defaulted + + +def test_a_second_command_in_the_same_process_says_them_again(tmp_path: Path, capsys, monkeypatch) -> None: + repo = _repo_with_bare_skill(tmp_path) + monkeypatch.chdir(repo) + counts = [] + for _ in range(2): + main(["check", "--repo", "."]) + captured = capsys.readouterr() + counts.append(sum("defaulted version" in line for line in (captured.out + captured.err).splitlines())) + assert counts == [1, 1] + + +def _long_result() -> PolicyResult: + case = Case(id="c1", category="block", prompt="p", expect="block", policy_id="demo") + return PolicyResult("demo", "deterministic", [CaseResult(case, "fail", detail="x" * 200)]) + + +def test_the_eval_table_on_a_cp1252_stream_has_no_ellipsis_character(monkeypatch) -> None: + stream = _cp1252() + monkeypatch.setattr(sys, "stdout", stream) + + PipeTolerantWriter(stream).write(render_text([_long_result()])) + stream.flush() + + text = stream.buffer.getvalue().decode("cp1252") + assert "\u2026" not in text + assert "x" * 60 + "..." in text + + +def test_a_utf8_stream_keeps_the_ellipsis(monkeypatch) -> None: + monkeypatch.setattr(sys, "stdout", io.TextIOWrapper(io.BytesIO(), encoding="utf-8")) + assert ellipsis() == "\u2026" + assert "\u2026" in render_text([_long_result()]) + + +@pytest.mark.parametrize( + ("text", "shown"), + [("a \u2014 b", "a -- b"), ("x \u2192 y", "x -> y"), ("\u00a75", "s.5"), ("caf\u00e9 \u20ac", "caf\u00e9 \u20ac")], +) +def test_legible_turns_chock_symbols_into_ascii_on_a_legacy_code_page(text: str, shown: str) -> None: + assert legible(text, _cp1252()) == shown + + +def test_warn_on_a_cp1252_stderr_never_raises_and_prints_ascii(monkeypatch) -> None: + stream = _cp1252() + monkeypatch.setattr(sys, "stderr", stream) + new_command() + warn("lock \u2014 run the regen \u2026") + stream.flush() + assert stream.buffer.getvalue().decode("ascii") == "[WARN] lock -- run the regen ...\n" From 17a5e61e3aef37bf34989c1440b86a2932c8f313 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:46:44 +0000 Subject: [PATCH 08/57] refactor(lock): move the engine constraint to lock_engine.py lock.py was 357 lines, over the 300-line review budget that tests/test_repo_standards.py::test_no_file_exceeds_line_budget enforces. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- src/chock/lock.py | 65 ++---------------------------------- src/chock/lock_engine.py | 69 +++++++++++++++++++++++++++++++++++++++ tests/test_lock_engine.py | 10 ++---- 3 files changed, 73 insertions(+), 71 deletions(-) create mode 100644 src/chock/lock_engine.py diff --git a/src/chock/lock.py b/src/chock/lock.py index f3a3b02b..e55c3249 100644 --- a/src/chock/lock.py +++ b/src/chock/lock.py @@ -5,22 +5,16 @@ import argparse import hashlib import json -import re import sys from pathlib import Path from typing import Any -from chock import __version__ +from chock.lock_engine import LOCKFILE_NAME, engine_constraint, engine_failures from chock.policies import discover_policy_dirs from chock.scaffold.pin import is_remote_source from chock.vendored import vendored_differences -LOCKFILE_NAME = "chock.lock" LOCKFILE_VERSION = "2" -#: What every lock said before the field was read; such a lock passes with a note to re-lock. -LEGACY_ENGINE_CONSTRAINT = ">=0.1,<0.2" -_VERSION_PREFIX = re.compile(r"(\d+)\.(\d+)(?:\.(\d+))?") -_CONSTRAINT_RE = re.compile(r"\s*>=\s*(\d+(?:\.\d+){0,2})\s*,\s*<\s*(\d+(?:\.\d+){0,2})\s*") POLICIES_REL = Path(".agents") / "policies" LOCAL_SOURCE = "local" _PROVENANCE_KEYS = ("source", "source_commit", "source_ref") @@ -30,53 +24,6 @@ class LockError(RuntimeError): """chock.lock exists but cannot be read as a lockfile.""" -def _version_tuple(version: str) -> tuple[int, int, int] | None: - """(major, minor, patch) from the leading digits of a version string; None when it has none.""" - match = _VERSION_PREFIX.match(version.strip()) - if not match: - return None - major, minor, patch = match.groups() - return int(major), int(minor), int(patch or 0) - - -def _padded(dotted: str) -> tuple[int, int, int]: - """A constraint bound as (major, minor, patch), missing parts 0.""" - major, minor, patch, *_ = [*(int(p) for p in dotted.split(".")), 0, 0] - return major, minor, patch - - -def engine_constraint(version: str = __version__) -> str: - """The constraint a lock written by engine `version` carries: `>=.,<`.""" - parsed = _version_tuple(version) - if parsed is None: - msg = f"engine version {version!r} has no . prefix" - raise ValueError(msg) - major, minor, _patch = parsed - return f">={major}.{minor},<{major + 1}" - - -def check_engine(constraint: object, version: str = __version__) -> tuple[str | None, str | None]: - """(failure, note) for the running engine against a lock's `engine` field; both None when it satisfies it.""" - relock = "or run `chock sync --repo .` with the newer engine to re-lock" - if constraint == LEGACY_ENGINE_CONSTRAINT: - return None, ( - f"note: {LOCKFILE_NAME} engine constraint {constraint!r} was written before constraints were read; " - "run `chock sync --repo .` to record this engine's constraint" - ) - match = _CONSTRAINT_RE.fullmatch(constraint) if isinstance(constraint, str) else None - running = _version_tuple(version) - if match is None or running is None: - what = f"engine {version}" if running is None else f"{LOCKFILE_NAME} engine constraint {constraint!r}" - return f"{what} cannot be read; expected `>=X.Y, str: h = hashlib.sha256() h.update(path.read_bytes()) @@ -265,14 +212,6 @@ def build_lock(repo_root: Path) -> dict[str, Any]: return lock -def _engine_failures(lock: dict[str, Any], notes: list[str] | None) -> list[str]: - """`check_engine` on a lock: its failure as a list, its note appended to `notes` when given.""" - failure, note = check_engine(lock.get("engine")) - if note and notes is not None: - notes.append(note) - return [failure] if failure else [] - - def verify_lock(repo_root: Path | None = None, notes: list[str] | None = None) -> tuple[bool, list[str]]: """Check the running engine against the lock's constraint, recompute hashes, and report drift.""" repo_root = repo_root or Path.cwd().resolve() @@ -280,7 +219,7 @@ def verify_lock(repo_root: Path | None = None, notes: list[str] | None = None) - lock = read_lock(repo_root) except LockError as exc: return False, [str(exc)] - failures = _engine_failures(lock, notes) + failures = engine_failures(lock, notes) failures += [ f"vendored runtime modified ({d}) -- this is what executes gates" for d in vendored_differences(repo_root) diff --git a/src/chock/lock_engine.py b/src/chock/lock_engine.py new file mode 100644 index 00000000..c92bb954 --- /dev/null +++ b/src/chock/lock_engine.py @@ -0,0 +1,69 @@ +"""chock.lock's `engine` constraint: derived from the running engine, checked against it (IR-F71).""" + +from __future__ import annotations + +import re +from typing import Any + +from chock import __version__ + +LOCKFILE_NAME = "chock.lock" +#: What every lock said before the field was read; such a lock passes with a note to re-lock. +LEGACY_ENGINE_CONSTRAINT = ">=0.1,<0.2" +_VERSION_PREFIX = re.compile(r"(\d+)\.(\d+)(?:\.(\d+))?") +_CONSTRAINT_RE = re.compile(r"\s*>=\s*(\d+(?:\.\d+){0,2})\s*,\s*<\s*(\d+(?:\.\d+){0,2})\s*") + + +def _version_tuple(version: str) -> tuple[int, int, int] | None: + """(major, minor, patch) from the leading digits of a version string; None when it has none.""" + match = _VERSION_PREFIX.match(version.strip()) + if not match: + return None + major, minor, patch = match.groups() + return int(major), int(minor), int(patch or 0) + + +def _padded(dotted: str) -> tuple[int, int, int]: + """A constraint bound as (major, minor, patch), missing parts 0.""" + major, minor, patch, *_ = [*(int(p) for p in dotted.split(".")), 0, 0] + return major, minor, patch + + +def engine_constraint(version: str = __version__) -> str: + """The constraint a lock written by engine `version` carries: `>=.,<`.""" + parsed = _version_tuple(version) + if parsed is None: + msg = f"engine version {version!r} has no . prefix" + raise ValueError(msg) + major, minor, _patch = parsed + return f">={major}.{minor},<{major + 1}" + + +def check_engine(constraint: object, version: str = __version__) -> tuple[str | None, str | None]: + """(failure, note) for the running engine against a lock's `engine` field; both None when it satisfies it.""" + relock = "or run `chock sync --repo .` with the newer engine to re-lock" + if constraint == LEGACY_ENGINE_CONSTRAINT: + return None, ( + f"note: {LOCKFILE_NAME} engine constraint {constraint!r} was written before constraints were read; " + "run `chock sync --repo .` to record this engine's constraint" + ) + match = _CONSTRAINT_RE.fullmatch(constraint) if isinstance(constraint, str) else None + running = _version_tuple(version) + if match is None or running is None: + what = f"engine {version}" if running is None else f"{LOCKFILE_NAME} engine constraint {constraint!r}" + return f"{what} cannot be read; expected `>=X.Y, list[str]: + """`check_engine` on a lock: its failure as a list, its note appended to `notes` when given.""" + failure, note = check_engine(lock.get("engine")) + if note and notes is not None: + notes.append(note) + return [failure] if failure else [] diff --git a/tests/test_lock_engine.py b/tests/test_lock_engine.py index b4a5e393..186c88c8 100644 --- a/tests/test_lock_engine.py +++ b/tests/test_lock_engine.py @@ -9,14 +9,8 @@ from chock import __version__, lock from chock.lifecycle import check_main -from chock.lock import ( - LEGACY_ENGINE_CONSTRAINT, - build_lock, - check_engine, - engine_constraint, - verify_lock, - write_lock, -) +from chock.lock import build_lock, verify_lock, write_lock +from chock.lock_engine import LEGACY_ENGINE_CONSTRAINT, check_engine, engine_constraint def _locked(repo: Path, constraint: object) -> Path: From 80849b7f7516750885c3a45fba05affa54b2242d Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:50:27 +0000 Subject: [PATCH 09/57] pin: keep pin.py within the 300-line budget; match tag refs exactly The lookup now matches only refs/tags/ and its peeled ^{} line, so a ref ending in the same path cannot stand in for it. Adds a lightweight-tag test. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- src/chock/scaffold/pin.py | 11 ++++------- tests/test_add_default_pin.py | 8 ++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/chock/scaffold/pin.py b/src/chock/scaffold/pin.py index 8ef2437d..ea825909 100644 --- a/src/chock/scaffold/pin.py +++ b/src/chock/scaffold/pin.py @@ -163,17 +163,14 @@ def _require_published(source: str, remote: str, sha: str, into: Path) -> None: def tag_commit(source: str, tag: str) -> str | None: """The commit tag `tag` of `source` names (peeled), or None when the source has no such tag.""" _check_ref_syntax(source, tag) - local = local_source(source) - remote = str(local.resolve()) if local is not None else source + remote = str(local.resolve()) if (local := local_source(source)) is not None else source name = f"refs/tags/{tag}" listed = _run(["git", *INERT, "ls-remote", "--tags", "--", remote, name, f"{name}^{{}}"], REMOTE_TIMEOUT) if listed.returncode != 0: - detail = _scrub(listed.stderr.strip(), source) or "git step failed" - msg = f"could not list the tags of catalog {redact_source(source)}: {detail}" + msg = f"could not list the tags of catalog {redact_source(source)}: {_scrub(listed.stderr.strip(), source)}" raise RuntimeError(msg) - found = dict(reversed(line.split("\t", 1)) for line in listed.stdout.splitlines() if "\t" in line) - sha = found.get(f"{name}^{{}}") or found.get(name) - return sha.lower() if sha and _FULL_SHA.fullmatch(sha) else None + found = {ref: sha for sha, _, ref in (line.partition("\t") for line in listed.stdout.splitlines())} + return next((found[k].lower() for k in (f"{name}^{{}}", name) if _FULL_SHA.fullmatch(found.get(k, ""))), None) def _clone_ref(source: str, remote: str, ref: str | None, into: Path) -> str | None: diff --git a/tests/test_add_default_pin.py b/tests/test_add_default_pin.py index 58ceb6c2..4268cfa5 100644 --- a/tests/test_add_default_pin.py +++ b/tests/test_add_default_pin.py @@ -115,3 +115,11 @@ def test_help_names_the_default(capsys) -> None: with pytest.raises(SystemExit): add_mod.main(["--help"]) assert "v" in " ".join(capsys.readouterr().out.split()) + + +def test_a_lightweight_tag_resolves_too(catalog, repo: Path, capsys, monkeypatch) -> None: + bare, tagged, _ = catalog + _git(bare, "tag", "v9.9.9", tagged) + monkeypatch.setattr(add_mod, "engine_version", "9.9.9") + assert add_mod.main([ID, "--repo", str(repo), "--skip-compile"]) == 0 + assert f"at {tagged} (tag v9.9.9)" in capsys.readouterr().out From 9d4a7d08d5d53ab5adbbe3714e31fbb36eb2ef14 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:15:18 +0000 Subject: [PATCH 10/57] doctor --live: run only chock's own hook commands, and only on this engine's runtime Review finding: --live ran every pre-tool command in the client's hook file, so a cloned repo's foreign hook strings would run with no trust prompt. Now an entry runs only when it is byte-equal to an entry this engine's installer renders for the target (rendered into a scratch copy from the repo's policies and compiled tree); others are counted in one INFO row and never run. Nothing runs unless the launcher, the agent runtime, the shared runtime and every vendored runtime are byte-equal to what this engine renders. Plugin-route targets are skipped: their hook commands cannot be re-rendered without the person's selection. Part of 1.2.0 (W1-D). Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w1d-doctor-live.md | 23 +++++---- src/chock/doctor/cli.py | 10 ++-- src/chock/doctor/live.py | 88 ++++++++++++++++++++++------------ tests/test_doctor_live.py | 88 +++++++++++++++++++++------------- 4 files changed, 133 insertions(+), 76 deletions(-) diff --git a/changelog.d/w1d-doctor-live.md b/changelog.d/w1d-doctor-live.md index 760e6e8a..ffd38f62 100644 --- a/changelog.d/w1d-doctor-live.md +++ b/changelog.d/w1d-doctor-live.md @@ -1,10 +1,13 @@ -- `chock doctor --live` (preview): for each target `doctor` lists, runs the pre-tool hook command - the client's own config file holds, as written and from the repo, in the client's shell, on that - client's own payload. It sends the first refusing eval case of the first policy the hook runs (it - must be refused, by that policy) and `echo chock-doctor-live` (it must be allowed): two rows, - `live: refuses the canary` and `live: allows a harmless command`, each with the fix doctor - already prints. With no in-agent policy on the client's shell tool the canary row is skipped. It - runs no client, sends nothing anywhere, logs nothing (the probe runs at log level off and in its - own session, whose state file it removes), and does not prove the client calls the hook: that is - the heartbeat row. The per-vendor payloads and wire denies the tests used are now package data - (`chock.doctor.payloads`), one source for the engine and the tests. +- `chock doctor --live` (preview): for each sync-route target `doctor` lists, runs chock's own + pre-tool hook commands from the client's config file, as written and from the repo, in the + client's shell, on that client's own payload. Only an entry byte-equal to what this engine's + installer renders for that target is run, and only when the launcher and vendored runtime are + this engine's; any other hook command is counted in an INFO row and never run. The installed + policies' guard code runs, as it does in `chock check` evals. It sends the first refusing eval + case of the first policy the hook runs (it must be refused, by that policy) and + `echo chock-doctor-live` (it must be allowed): two rows, `live: refuses the canary` and + `live: allows a harmless command`, each with the fix doctor already prints. Plugin-route targets + are skipped. It runs no client, sends nothing anywhere, logs nothing (the probe runs at log level + off and in its own session, whose state file it removes), and does not prove the client calls + the hook: that is the heartbeat row. The per-vendor payloads and wire denies the tests used are + now package data (`chock.doctor.payloads`), one source for the engine and the tests. diff --git a/src/chock/doctor/cli.py b/src/chock/doctor/cli.py index c6347144..6a264fd2 100644 --- a/src/chock/doctor/cli.py +++ b/src/chock/doctor/cli.py @@ -19,10 +19,12 @@ def _say(line: str) -> None: _LIVE_HELP = ( - "also run each installed pre-tool hook command, exactly as the client's config file writes it, from the repo: " - "once on the first refusing eval case of the first policy it runs (it must refuse) and once on " - f"`{live.HARMLESS}` (it must allow). It runs nothing else, starts no client, sends nothing anywhere and " - "logs nothing. It does not prove the client calls the hook: that is the heartbeat row" + "also run chock's own pre-tool hook commands, only those byte-equal to what this engine renders and only when " + "the launcher and runtime are this engine's, as the client's config file writes them, from the repo: once on " + "the first refusing eval case of the first policy they run (it must refuse) and once on " + f"`{live.HARMLESS}` (it must allow). Any other hook command is counted, never run. The installed policies' " + "guard code runs, as it does in `chock check` evals. It starts no client, sends nothing anywhere and logs " + "nothing. It does not prove the client calls the hook: that is the heartbeat row" ) diff --git a/src/chock/doctor/live.py b/src/chock/doctor/live.py index a412ee27..aa30d041 100644 --- a/src/chock/doctor/live.py +++ b/src/chock/doctor/live.py @@ -1,4 +1,4 @@ -"""`chock doctor --live`: run each installed pre-tool hook as its client calls it, on a canary and a harmless command.""" +"""`chock doctor --live`: run chock's own installed pre-tool hooks as the client calls them, on a canary and a harmless command.""" from __future__ import annotations @@ -7,23 +7,26 @@ import re import shutil import subprocess +import tempfile import tomllib from collections.abc import Iterator from pathlib import Path from typing import Any import yaml -from agentseam import packaging from chock import vendors -from chock.doctor.checks import FAIL, INFO, PASS, Check, rebuild +from chock.doctor.checks import FAIL, INFO, PASS, Check, engine, rebuild from chock.doctor.payloads import DENY_ON_THE_WIRE, EXIT_2, payload -from chock.doctor.targets import ROUTE_PLUGIN, Target +from chock.doctor.targets import ROUTE_SYNC, Target from chock.gate.guard_runner import bash_candidates from chock.gate.runner.journal import LOG_LEVEL_ENV, LOG_OFF +from chock.hooks import launch +from chock.hooks.in_agent_install import install_hooks from chock.plugin import hook_dir, store from chock.plugin.clients import CLIENTS -from chock.plugin.devin import DEVIN_PLUGIN_ROOT_VAR +from chock.policies import discover_policy_dirs +from chock.vendored import vendored_differences REFUSES, ALLOWS, LIVE = "live: refuses the canary", "live: allows a harmless command", "live" #: A command every policy lets through: it reads nothing and writes nothing. @@ -35,7 +38,6 @@ #: The probe logs nothing, so the decision log never holds a refusal no agent asked for. QUIET = {LOG_LEVEL_ENV: LOG_OFF} _REFERENCE = re.compile(r'--(guard|multi)\s+"?([^"\s]+)"?') -_TOKEN = re.compile(r"\$\{(\w+)\}") _TOOL_KEYS = ("tool_name", "toolName") _ANY = (None, "", "*") #: The per-shell keys a hook entry may carry in place of `command` (Copilot's `bash` and `powershell`). @@ -46,13 +48,6 @@ _STATE = (".chock", "state") -def _root_vars() -> list[str]: - """Every plugin-root variable a store or agentseam records: the client sets it to the plugin folder.""" - tokens = [hook_dir.layout(fmt, client.agent)["plugin_root"] or "" for fmt, client in CLIENTS.items()] - tokens += [t for a in packaging.agents() for t in packaging.layout(a)["plugin_root"]] - return sorted({name for t in tokens for name in _TOKEN.findall(t)} | {DEVIN_PLUGIN_ROOT_VAR}) - - def _document(path: Path) -> Any: text = path.read_text(encoding="utf-8") return tomllib.loads(text) if path.suffix == ".toml" else json.loads(text) @@ -101,16 +96,35 @@ def _under(node: Any, event: str) -> Iterator[Any]: def commands(target: Target) -> list[tuple[str, str]]: - """The pre-tool commands the client runs for its shell tool, read from the file it reads; plugin roots filled in.""" + """The pre-tool entries the client runs for its shell tool, read from the very file it reads.""" doc = _document(target.hooks) sample = payload(target.agent, HARMLESS) if target.agent in DENY_ON_THE_WIRE else {} tool = next((sample[k] for k in _TOOL_KEYS if isinstance(sample.get(k), str)), None) - found = [c for part in _under(doc, vendors.shell_gate_event(target.agent)) for c in _collect(part, tool)] - if target.route != ROUTE_PLUGIN: - return found - root = hook_dir.root_path(target.root) - names = _root_vars() - return [(key, _TOKEN.sub(lambda m: root if m.group(1) in names else m.group(0), c)) for key, c in found] + return [c for part in _under(doc, vendors.shell_gate_event(target.agent)) for c in _collect(part, tool)] + + +def rendered(target: Target) -> set[tuple[str, str]]: + """Every entry this engine's installer writes for the target, rendered from the repo's policies into a scratch copy.""" + rel = target.hooks.relative_to(target.root) + with tempfile.TemporaryDirectory(prefix="chock-live-") as tmp: + shadow = Path(tmp) + compiled = target.root / ".chock" / "compiled" + if compiled.is_dir(): + shutil.copytree(compiled, shadow / ".chock" / "compiled") + for pack in discover_policy_dirs(target.root): + shutil.copytree(pack, shadow / pack.relative_to(target.root), dirs_exist_ok=True) + install_hooks(shadow, target.agent) + written = shadow / rel + return set(_collect(_document(written), None)) if written.is_file() else set() + + +def own_runtime(target: Target) -> bool: + """Whether the launcher and every vendored runtime are byte-equal to what this engine renders.""" + try: + same_launcher = target.launcher.read_text(encoding="utf-8") == launch.launcher_text() + except (OSError, UnicodeDecodeError): + return False + return same_launcher and engine(target).status == PASS and not vendored_differences(target.root) def _guards(command: str, root: Path) -> Iterator[Path]: @@ -174,14 +188,13 @@ def _probe(agent: str, asked: str, repo: Path) -> dict[str, Any]: def _call(target: Target, repo: Path, entry: tuple[str, str], asked: str) -> subprocess.CompletedProcess | str: """One hook command's answer to `asked`, or why it gave none.""" - roots = dict.fromkeys(_root_vars(), hook_dir.root_path(target.root)) if target.route == ROUTE_PLUGIN else {} argv = _argv(target.agent, *entry) try: - return subprocess.run( # noqa: S603 -- the hook command the client itself runs, read from its own config + return subprocess.run( # noqa: S603 -- a hook command this engine rendered, byte-equal, as the client runs it argv, shell=isinstance(argv, str), cwd=repo, - env={**os.environ, **roots, **QUIET}, + env={**os.environ, **QUIET}, input=json.dumps(_probe(target.agent, asked, repo)), capture_output=True, text=True, @@ -225,22 +238,37 @@ def _allows(target: Target, repo: Path, found: list[tuple[str, str]]) -> Check: return Check(PASS, ALLOWS, f"`{HARMLESS}` allowed by {len(found)} hook command(s)") -def run(target: Target, repo: Path) -> list[Check]: - """The two live rows for one target, or the one row that says why it could not run.""" +def _runnable(target: Target) -> tuple[list[tuple[str, str]], list[Check]]: + """(chock's own pre-tool entries, the rows that say what was not run); no entries when nothing may run.""" + if target.route != ROUTE_SYNC: + return [], [Check(INFO, LIVE, "skipped (plugin route: chock cannot re-render a plugin's hook commands)")] if target.agent not in DENY_ON_THE_WIRE: - return [Check(INFO, LIVE, f"skipped (no recorded {target.agent} pre-tool payload)")] + return [], [Check(INFO, LIVE, f"skipped (no recorded {target.agent} pre-tool payload)")] + if not own_runtime(target): + return [], [Check(INFO, LIVE, "skipped (runtime is not this engine's)", rebuild(target))] try: - found = commands(target) + found, ours = commands(target), rendered(target) except (OSError, ValueError, tomllib.TOMLDecodeError) as exc: - return [Check(FAIL, LIVE, f"{target.hooks} is unreadable ({exc})", rebuild(target))] + return [], [Check(FAIL, LIVE, f"{target.hooks} is unreadable ({exc})", rebuild(target))] + mine = [entry for entry in found if entry in ours] + other = len(found) - len(mine) + rows = [Check(INFO, LIVE, f"{other} other hook command(s) not run (not chock's)")] if other else [] + if not mine: + rows.append(Check(FAIL, LIVE, f"{target.hooks} holds no pre-tool command chock renders", rebuild(target))) + return mine, rows + + +def run(target: Target, repo: Path) -> list[Check]: + """The two live rows for one target, after a row for anything not run; or the one row that says why nothing ran.""" + found, rows = _runnable(target) if not found: - return [Check(FAIL, LIVE, f"{target.hooks} holds no pre-tool hook command", rebuild(target))] + return rows logs = [ base.joinpath(*_STATE, PROBE_SESSION + suffix) for base in {repo, target.root} for suffix in (".jsonl", ".tmp") ] fresh = [path for path in logs if not path.exists()] try: - return [_refuses(target, repo, found, canary(target, repo, found)), _allows(target, repo, found)] + return [*rows, _refuses(target, repo, found, canary(target, repo, found)), _allows(target, repo, found)] finally: for path in fresh: path.unlink(missing_ok=True) diff --git a/tests/test_doctor_live.py b/tests/test_doctor_live.py index 0ae7ee57..3642576b 100644 --- a/tests/test_doctor_live.py +++ b/tests/test_doctor_live.py @@ -77,25 +77,61 @@ def test_without_live_no_hook_is_run(synced: Path, capsys: pytest.CaptureFixture assert "live:" not in capsys.readouterr().out -def test_a_broken_launcher_path_fails_both_rows_with_the_sync_fix(synced: Path) -> None: +#: What a foreign hook command or an edited launcher would leave behind if `--live` ran it. +MARKER = "live-marker" +WRITE_MARKER = f"echo x > {MARKER}" + + +def _settings(repo: Path) -> tuple[Path, dict]: + path = repo / ".claude" / "settings.json" + return path, json.loads(path.read_text(encoding="utf-8")) + + +def test_a_foreign_hook_command_is_counted_never_run(synced: Path) -> None: + path, doc = _settings(synced) + doc["hooks"]["PreToolUse"].append({"matcher": "Bash", "hooks": [{"type": "command", "command": WRITE_MARKER}]}) + path.write_text(json.dumps(doc), encoding="utf-8") + found = _live(synced) + assert not (synced / MARKER).exists() + assert found[live.LIVE].status == checks.INFO + assert found[live.LIVE].detail == "1 other hook command(s) not run (not chock's)" + assert found[live.REFUSES].status == found[live.ALLOWS].status == checks.PASS + + +def test_an_edited_launcher_runs_nothing(synced: Path) -> None: + launcher = synced / launch.LAUNCHER_REL + launcher.write_text(f"{WRITE_MARKER}\n" + launcher.read_text(encoding="utf-8"), encoding="utf-8") + [row] = _live(synced).values() + assert not (synced / MARKER).exists() + assert row.detail == "skipped (runtime is not this engine's)" + assert row.fix == f"run `chock sync --repo {synced}`" + + +def test_a_missing_launcher_runs_nothing(synced: Path) -> None: (synced / launch.LAUNCHER_REL).unlink() + [row] = _live(synced).values() + assert row.detail == "skipped (runtime is not this engine's)" + + +def test_a_tampered_pinned_index_fails_both_rows_with_the_sync_fix(synced: Path) -> None: + for index in (synced / ".chock" / "bin" / "index").glob("claude_code-*.json"): + index.write_text(index.read_text(encoding="utf-8") + " ", encoding="utf-8") found = _live(synced) for name in (live.REFUSES, live.ALLOWS): assert found[name].status == checks.FAIL, found assert found[name].fix == f"run `chock sync --repo {synced}`" - assert "launch.sh" in found[live.REFUSES].detail -def test_a_refusal_by_anything_but_the_policy_is_not_a_pass(synced: Path) -> None: - settings = synced / ".claude" / "settings.json" - doc = json.loads(settings.read_text(encoding="utf-8")) +def test_an_edited_chock_command_is_not_chock_s(synced: Path) -> None: + path, doc = _settings(synced) for group in doc["hooks"]["PreToolUse"]: for hook in group["hooks"]: - hook["command"] = hook["command"].replace(".chock/bin/claude_code.py", ".chock/bin/gone.py") - settings.write_text(json.dumps(doc), encoding="utf-8") + hook["command"] += f"; {WRITE_MARKER}" + path.write_text(json.dumps(doc), encoding="utf-8") found = _live(synced) - assert found[live.REFUSES].status == checks.FAIL - assert found[live.ALLOWS].status == checks.FAIL + assert not (synced / MARKER).exists() + assert found[live.LIVE].status == checks.FAIL + assert "no pre-tool command chock renders" in found[live.LIVE].detail def test_no_guard_on_the_shell_tool_is_reported_skipped(synced: Path, monkeypatch: pytest.MonkeyPatch) -> None: @@ -106,30 +142,18 @@ def test_no_guard_on_the_shell_tool_is_reported_skipped(synced: Path, monkeypatc assert found[live.ALLOWS].status == checks.PASS -def test_a_hooks_file_with_no_pre_tool_command_fails(synced: Path) -> None: - (synced / ".claude" / "settings.json").write_text(json.dumps({"hooks": {}}), encoding="utf-8") +def test_a_plugin_target_is_skipped_and_nothing_runs(tmp_path: Path) -> None: + folder = tmp_path / "plugin" + target = Target("claude-code", AGENT, ROUTE_PLUGIN, folder, folder, folder, folder / "hooks.json") + [row] = live.run(target, tmp_path) + assert row.status == checks.INFO + assert row.detail.startswith("skipped (plugin route") + + +def test_the_matcher_is_honoured(synced: Path) -> None: [target] = [t for t in sync_targets(synced) if t.agent == AGENT] - [row] = live.run(target, synced) - assert row.status == checks.FAIL - assert "no pre-tool hook command" in row.detail - - -def test_a_plugin_command_has_its_root_token_filled_in_and_its_matcher_honoured(tmp_path: Path) -> None: - folder = tmp_path / "plugin dir" - hooks = folder / "hooks" / "hooks.json" - hooks.parent.mkdir(parents=True) - doc = { - "hooks": { - "PreToolUse": [ - {"matcher": "Bash", "hooks": [{"command": 'sh "${CLAUDE_PLUGIN_ROOT}/scripts/launch.sh" --guard x'}]}, - {"matcher": "Write", "hooks": [{"command": "never"}]}, - ], - "Stop": [{"hooks": [{"command": "stop"}]}], - } - } - hooks.write_text(json.dumps(doc), encoding="utf-8") - target = Target("claude-code", AGENT, ROUTE_PLUGIN, folder, folder, folder, hooks) - assert live.commands(target) == [("command", f'sh "{folder.as_posix()}/scripts/launch.sh" --guard x')] + assert live.commands(target) + assert all(entry in live.rendered(target) for entry in live.commands(target)) def test_copilot_s_per_shell_entry_is_read() -> None: From 85d0034d70969a11d12e5f725e39e34b191e11b4 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:33:00 +0000 Subject: [PATCH 11/57] feat(check): chock check --floor judges a pull request with its base's gates The engine half of the server-side floor (W2-A1). The policy set, gate programs and waiver lines come from the base ref; the installed engine's runner judges base...head; no head file executes. Head gates that are new or differ also run when declarative; head script gates are reported, not run. - event ci-floor: a waiver counts only when the base has the line; ask refuses; script gates get `deleted` and a PATH/HOME-only environment (script payload contract version 2 in spec/gate-dsl.md) - tamper rules from src/chock/data/tamper.json: hook-removed, gate-removed, gate-weakened, lock-pack-removed, runtime-removed, ci-step-removed; regeneration stays silent - per-commit pass over merge-base..head with the base's content gates - exit 0 clean, 1 refused, 2 undecided Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2a1-check-floor.md | 10 + spec/gate-dsl.md | 10 +- src/chock/data/commands.json | 2 +- src/chock/data/tamper.json | 27 +++ src/chock/gate/runner/__init__.py | 2 + src/chock/gate/runner/constants.py | 7 +- src/chock/gate/runner/script.py | 20 +- src/chock/gate/sarif.py | 10 + src/chock/history/gitlog.py | 12 +- src/chock/history/scan.py | 6 +- src/chock/lifecycle.py | 26 ++- src/chock/validation/checks_floor.py | 211 +++++++++++++++++++++ src/chock/validation/floor_cli.py | 27 +++ src/chock/validation/floor_context.py | 49 +++++ src/chock/validation/floor_git.py | 110 +++++++++++ src/chock/validation/floor_history.py | 58 ++++++ src/chock/validation/tamper.py | 253 ++++++++++++++++++++++++++ tests/fixtures/cli_help_golden.txt | 2 +- tests/floor_support.py | 134 ++++++++++++++ tests/test_floor_check.py | 180 ++++++++++++++++++ tests/test_floor_tamper.py | 153 ++++++++++++++++ 21 files changed, 1278 insertions(+), 31 deletions(-) create mode 100644 changelog.d/w2a1-check-floor.md create mode 100644 src/chock/data/tamper.json create mode 100644 src/chock/validation/checks_floor.py create mode 100644 src/chock/validation/floor_cli.py create mode 100644 src/chock/validation/floor_context.py create mode 100644 src/chock/validation/floor_git.py create mode 100644 src/chock/validation/floor_history.py create mode 100644 src/chock/validation/tamper.py create mode 100644 tests/floor_support.py create mode 100644 tests/test_floor_check.py create mode 100644 tests/test_floor_tamper.py diff --git a/changelog.d/w2a1-check-floor.md b/changelog.d/w2a1-check-floor.md new file mode 100644 index 00000000..4aa86b2e --- /dev/null +++ b/changelog.d/w2a1-check-floor.md @@ -0,0 +1,10 @@ +- `chock check --floor --base [--head ] [--head-ref ]` (preview): judges a pull request's + `base...head` with the base's compiled ci gates, run by the installed engine, never `.chock/bin/gate.py` or any + other head file. A head gate that is new or differs also runs when it is declarative; a head script gate is + reported "not judged at floor". A waiver counts only when the base already has the line (event `ci-floor`); `ask` + refuses; script gates receive deleted paths under `deleted` and an environment of `PATH`, `HOME` (and `SYSTEMROOT`) + only (script payload contract version 2, `spec/gate-dsl.md`). Built-in tamper rules from `src/chock/data/tamper.json` + refuse by name a removed hook entry, a removed or weakened compiled gate, a pack dropped from `chock.lock`, a + deleted `.chock/bin` file and a removed job, step or trigger of the guarded workflow; regeneration is silent. A + per-commit pass runs the base's content gates over `merge-base..head`. Exit 0 clean, 1 refused, 2 undecided. It is + a floor only where the repository makes it a required check, which chock does not verify. diff --git a/spec/gate-dsl.md b/spec/gate-dsl.md index 8238017f..b056f870 100644 --- a/spec/gate-dsl.md +++ b/spec/gate-dsl.md @@ -227,7 +227,15 @@ script lives beside this gate file" instead of "under the repository root". Only plugin writes it: there the gate, the runner and the script travel together and no repository holds them. -`event` is `commit`, `push`, `tool_use` or (see above) `tool_call`. `session` is present only at the agent events. `writes` is the staged blobs at `commit` and +Script payload contract: **version 2**. Version 1 (unnumbered) had no `ci-floor` event; version 2 adds +it, with the `deleted` key and the reduced environment below, and changes nothing at any other event. + +`event` is `commit`, `push`, `tool_use`, `ci-floor` or (see above) `tool_call`. `session` is present only at the agent events. +At `ci-floor` (`chock check --floor`) the payload also carries `deleted`: the sorted paths the +range deletes, so a deletion reaches the script (a run with only deletions still runs it). There +the script's environment holds only `PATH`, `HOME` and, where set, `SYSTEMROOT`, and `writes` is +the head's blobs; the script itself is the base's copy, a waiver counts only when the base already +has that line, and an `ask` refuses. `writes` is the staged blobs at `commit` and `push`, and the write itself at `tool_use` -- the file a tool call is about to write, or what the turn left on disk at its end -- so one script serves every surface, and it reaches the write path and the turn's end exactly as `content_regex` does. The script answers with its diff --git a/src/chock/data/commands.json b/src/chock/data/commands.json index 723cfc90..59556192 100644 --- a/src/chock/data/commands.json +++ b/src/chock/data/commands.json @@ -29,7 +29,7 @@ "check": { "module": "chock.history.cli", "fn": "check_main", - "help": "Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits)" + "help": "Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor judges a pull request with its base's gates)" }, "status": { "module": "chock.lifecycle", diff --git a/src/chock/data/tamper.json b/src/chock/data/tamper.json new file mode 100644 index 00000000..bfe053a8 --- /dev/null +++ b/src/chock/data/tamper.json @@ -0,0 +1,27 @@ +{ + "$comment": "Facts `chock check --floor` compares base against head: each rule refuses only a removal or weakening. Ships inside the engine, so a pull request cannot edit what judges it.", + "hook_files": [ + {"glob": ".claude/settings.json", "pointer": "/hooks"}, + {"glob": ".cursor/hooks.json", "pointer": "/hooks"}, + {"glob": ".github/hooks/*.json", "pointer": "/hooks"}, + {"glob": ".codex/hooks.json", "pointer": "/hooks"}, + {"glob": ".gemini/settings.json", "pointer": "/hooks"}, + {"glob": ".windsurf/hooks.json", "pointer": "/hooks"}, + {"glob": ".devin/hooks.json", "pointer": "/hooks"}, + {"glob": ".devin/hooks.v1.json", "pointer": ""}, + {"glob": ".grok/hooks/*.json", "pointer": "/hooks"} + ], + "hook_nested_key": "hooks", + "hook_matcher_key": "matcher", + "hook_matcher_separator": "|", + "hook_command_keys": ["command", "commandWindows", "bash", "powershell"], + "volatile_pattern": "[0-9a-f]{7,}|\\d+(?:\\.\\d+)+", + "volatile_placeholder": "", + "compiled_prefix": ".chock/compiled/", + "gate_surfaces": ["ci-gate", "git-hook"], + "gate_file": "gate.json", + "runtime_prefix": ".chock/bin/", + "lock_file": "chock.lock", + "workflow_default": ".github/workflows/chock.yml", + "workflow_config_key": "floor_workflow" +} diff --git a/src/chock/gate/runner/__init__.py b/src/chock/gate/runner/__init__.py index 85dcda59..a73809a1 100644 --- a/src/chock/gate/runner/__init__.py +++ b/src/chock/gate/runner/__init__.py @@ -21,6 +21,7 @@ CONFIG_AGENT_ENV_KEY, EXIT_ASK, EXIT_WARN, + FLOOR_EVENT, GATE_LOG_ENV, HEAD_WAIVER_EVENTS, POLICIES_PREFIX, @@ -57,6 +58,7 @@ "EXIT_ASK", "EXIT_WARN", "EXTRACTORS", + "FLOOR_EVENT", "GATE_LOG_ENV", "HEAD_WAIVER_EVENTS", "KINDS", diff --git a/src/chock/gate/runner/constants.py b/src/chock/gate/runner/constants.py index 4088852b..29762718 100644 --- a/src/chock/gate/runner/constants.py +++ b/src/chock/gate/runner/constants.py @@ -22,6 +22,7 @@ "CONFIG_AGENT_ENV_KEY", "EXIT_ASK", "EXIT_WARN", + "FLOOR_EVENT", "GATE_LOG_ENV", "HEAD_WAIVER_EVENTS", "POLICIES_PREFIX", @@ -107,8 +108,12 @@ #: adds is not honoured. Coverage (`on: [commit]`) is still decided by the name `commit`. AGENT_COMMIT_EVENT = "agent-commit" +#: The event `chock check --floor` judges at: the change under judgement may be the refused party's, so a +#: waiver counts only when the base already has that line (the floor's `committed_blob` reads the base). +FLOOR_EVENT = "ci-floor" + #: Events where the actor may be the refused agent: a waiver counts only when HEAD already has that line. -HEAD_WAIVER_EVENTS = frozenset({AGENT_COMMIT_EVENT, TOOL_USE_EVENT}) +HEAD_WAIVER_EVENTS = frozenset({AGENT_COMMIT_EVENT, TOOL_USE_EVENT, FLOOR_EVENT}) _HUMAN_ENV = frozenset({"0", "false", "no", "off"}) diff --git a/src/chock/gate/runner/script.py b/src/chock/gate/runner/script.py index 098d3805..e517444b 100644 --- a/src/chock/gate/runner/script.py +++ b/src/chock/gate/runner/script.py @@ -12,6 +12,7 @@ from pathlib import Path from ..budget import ENGINE_BUDGET_SECONDS, run_tree +from .constants import FLOOR_EVENT from .context import GateContext, GateResult # >>> gate.py 07 @@ -34,6 +35,15 @@ _FINDINGS_KEY = "findings" +#: All a script gate's environment holds at the floor: no token, secret or runner variable reaches it. +_FLOOR_ENV_KEYS = ("PATH", "HOME", "SYSTEMROOT") + + +def _env(event: object) -> dict[str, str]: + if event == FLOOR_EVENT: + return {key: os.environ[key] for key in _FLOOR_ENV_KEYS if key in os.environ} | _BYTECODE_OFF + return {**os.environ, **_BYTECODE_OFF} + def _spawn(script: Path, ctx: GateContext, payload: dict, timeout: float) -> subprocess.CompletedProcess[str]: return run_tree( @@ -44,7 +54,7 @@ def _spawn(script: Path, ctx: GateContext, payload: dict, timeout: float) -> sub encoding="utf-8", errors="replace", cwd=ctx.repo_root, - env={**os.environ, **_BYTECODE_OFF}, + env=_env(payload.get("event")), timeout=timeout, check=False, ) @@ -159,7 +169,7 @@ def _judge_findings( def _kind_script(ctx: GateContext, params: dict, event: str) -> GateResult: """Hand the material to the policy's own script and carry back its verdict. - The script reads `{"event", "repo_root", "writes": {path: text}}` on stdin -- the staged + The script reads `{"event", "repo_root", "writes": {path: text}}` on stdin (plus `deleted` at the floor) -- the staged blobs at commit and push, the write itself at tool use and at the turn's end -- so one script serves every surface the declarative kinds do, and reads them the same way. It answers with an exit code: 0 allows; 1 refuses, 3 asks, 4 warns, with its own words on stderr. @@ -175,9 +185,13 @@ def _kind_script(ctx: GateContext, params: dict, event: str) -> GateResult: if not script.is_file(): return GateResult(allowed=False, message=f"script gate: {named!r} is not installed{_UNDECIDED}") writes = {path: ctx.staged_blob(path) for path in ctx.staged_paths()} - if not writes: + # Only the floor hands a script the deleted paths: the contract every other event has is unchanged. + deleted = sorted(ctx.staged_paths("D")) if event == FLOOR_EVENT else [] + if not writes and not deleted: return GateResult(allowed=True) material = {"event": event, "repo_root": str(ctx.repo_root), "writes": writes} + if event == FLOOR_EVENT: + material["deleted"] = deleted if ctx.session: material["session"] = ctx.session started = time.monotonic() diff --git a/src/chock/gate/sarif.py b/src/chock/gate/sarif.py index e7b4ae2a..8d9aaa49 100644 --- a/src/chock/gate/sarif.py +++ b/src/chock/gate/sarif.py @@ -6,6 +6,7 @@ from __future__ import annotations +import argparse import hashlib import json import re @@ -282,3 +283,12 @@ def emit(repo: Path, base: str, head_ref: str | None, output: str | None) -> int return 2 sys.stderr.write(f"sarif: {len(log['runs'][0]['results'])} result(s) written to {output}\n") return code + + +def check_main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int: + """`chock check --format sarif`; the exit code is the gate step's own (0, 1 on a block, 2 undecided).""" + if args.format != "sarif": + parser.error("--output and --head-ref go with --format sarif") + if args.event != "ci" or not args.base or args.only or args.mode: + parser.error("--format sarif needs --event ci and --base, and takes no --only or --mode") + return emit(Path(args.repo), args.base, args.head_ref, args.output) diff --git a/src/chock/history/gitlog.py b/src/chock/history/gitlog.py index 885de0ef..37a00dc7 100644 --- a/src/chock/history/gitlog.py +++ b/src/chock/history/gitlog.py @@ -112,12 +112,12 @@ def is_shallow(repo: Path) -> bool: return _git(repo, ["rev-parse", "--is-shallow-repository"]).decode().strip() == "true" -def _range(since: str | None) -> list[str]: - return [f"{since}..HEAD"] if since else ["HEAD"] +def _range(since: str | None, head: str = "HEAD") -> list[str]: + return [f"{since}..{head}"] if since else [head] -def count_commits(repo: Path, since: str | None) -> int: - return int(_git(repo, ["rev-list", "--count", *_range(since), "--"]).decode().strip() or 0) +def count_commits(repo: Path, since: str | None, head: str = "HEAD") -> int: + return int(_git(repo, ["rev-list", "--count", *_range(since, head), "--"]).decode().strip() or 0) def _stream_tokens(proc: subprocess.Popen[bytes]) -> Iterator[bytes]: @@ -152,12 +152,12 @@ def _records(tokens: Iterator[bytes], log: Log) -> Iterator[tuple[str, str, str] yield commit, fields[3], path -def touches(repo: Path, since: str | None, max_commits: int) -> Log: +def touches(repo: Path, since: str | None, max_commits: int, head: str = "HEAD") -> Log: """Blobs added or changed per commit, oldest commit first (the newest `max_commits` of the range).""" args = [ "log", "-z", "--raw", "-m", "-M", "--topo-order", "--reverse", "--no-abbrev", "--no-ext-diff", "--no-textconv", "--no-show-signature", "--diff-filter=ACMRT", - "-n", str(max_commits), "--format=%x01%H", *_range(since), "--", + "-n", str(max_commits), "--format=%x01%H", *_range(since, head), "--", ] # fmt: skip result = Log() seen: set[Touch] = set() diff --git a/src/chock/history/scan.py b/src/chock/history/scan.py index db6e3fee..36cf00a7 100644 --- a/src/chock/history/scan.py +++ b/src/chock/history/scan.py @@ -168,7 +168,7 @@ def add(item: gitlog.Touch, rule: Rule, kind: str, line: int | None, key: str) - return found -def scan(repo: Path, rules: list[Rule], since: str | None, limits: Limits) -> Report: +def scan(repo: Path, rules: list[Rule], since: str | None, limits: Limits, head: str = "HEAD") -> Report: report = Report(shallow=gitlog.is_shallow(repo)) if report.shallow: if not limits.allow_shallow: @@ -177,11 +177,11 @@ def scan(repo: Path, rules: list[Rule], since: str | None, limits: Limits) -> Re "would claim too much. Fetch full history (git fetch --unshallow) or pass --allow-shallow." ) _say("history: WARNING shallow clone; commits beyond the shallow boundary were not scanned") - in_range = gitlog.count_commits(repo, since) + in_range = gitlog.count_commits(repo, since, head) report.truncated = in_range > limits.max_commits report.commits = min(in_range, limits.max_commits) _say(f"history: listing up to {report.commits} of {in_range} commits") - log = gitlog.touches(repo, since, limits.max_commits) + log = gitlog.touches(repo, since, limits.max_commits, head) report.skipped_submodules = log.submodules oids = sorted({t.oid for t in log.touches}) _say(f"history: {len(log.touches)} changed paths, {len(oids)} distinct blobs") diff --git a/src/chock/lifecycle.py b/src/chock/lifecycle.py index 2319dceb..f76df67d 100644 --- a/src/chock/lifecycle.py +++ b/src/chock/lifecycle.py @@ -102,17 +102,6 @@ def _run_mechanisms(args: argparse.Namespace) -> int: return _run("matrix mechanisms", mechanisms_main, ["--repo", args.repo]) -def _sarif_main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int: - """The ci gates' findings as SARIF; the exit code is the gate step's own (0, 1 on a block, 2 undecided).""" - if args.format != "sarif": - parser.error("--output and --head-ref go with --format sarif") - if args.event != "ci" or not args.base or args.only or args.mode: - parser.error("--format sarif needs --event ci and --base, and takes no --only or --mode") - from chock.gate.sarif import emit - - return emit(Path(args.repo), args.base, args.head_ref, args.output) - - def _check_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(prog="chock check") parser.add_argument("--repo", default=".", help="Repo root") @@ -127,9 +116,10 @@ def _check_parser() -> argparse.ArgumentParser: help="sarif: run the compiled ci gates (needs --event ci and --base) and print their findings as SARIF 2.1.0", ) parser.add_argument("--output", default=None, help="With --format sarif: write the log to this file, not stdout") - parser.add_argument( - "--head-ref", default=None, help="With --format sarif: the branch under test ($GITHUB_HEAD_REF)" - ) + parser.add_argument("--head-ref", default=None, help="With --format sarif or --floor: the branch under test") + from chock.validation.floor_cli import add_arguments + + add_arguments(parser) return parser @@ -161,8 +151,14 @@ def check_main(argv: list[str] | None) -> int: """Run every truth check: validate, verify, evals, matrix, index freshness, conflicts.""" parser = _check_parser() args = parser.parse_args(argv) + if args.floor or args.head: + from chock.validation.floor_cli import from_check + + return from_check(args, parser) if args.format == "sarif" or args.output or args.head_ref: - return _sarif_main(args, parser) + from chock.gate.sarif import check_main as sarif_main + + return sarif_main(args, parser) selected = _selected_checks(args.only) if selected is None: diff --git a/src/chock/validation/checks_floor.py b/src/chock/validation/checks_floor.py new file mode 100644 index 00000000..820f1005 --- /dev/null +++ b/src/chock/validation/checks_floor.py @@ -0,0 +1,211 @@ +"""`chock check --floor`: judge a pull request with the base's gates, run by this engine; no head file executes. + +Exit 0 clean, 1 any refusal (each naming its policy or tamper rule and path), 2 undecided. +""" + +from __future__ import annotations + +import argparse +import json +import sys +import tempfile +from dataclasses import dataclass +from pathlib import Path + +from chock.display import inert +from chock.gate.runner import ( + ACTION_ASK, + ACTION_BLOCK, + COMPILED_PREFIX, + FLOOR_EVENT, + KINDS, + POLICIES_PREFIX, + SCRIPT_BASE_GATE, + GateResult, + committed_rollout, +) +from chock.gate.runner.constants import _ACTION_RANK, _DEPENDENCY_KIND, _ROLLOUT_CEILING +from chock.gate.runner.context import ListingError +from chock.gate.runner.kinds import PatternDeadlineError +from chock.gate.runner.report import _reason +from chock.validation.floor_context import FloorContext +from chock.validation.floor_git import FloorError, Revision, merge_base, resolve +from chock.validation.floor_history import history_findings +from chock.validation.tamper import tamper + +EXIT_REFUSED, EXIT_UNDECIDED = 1, 2 +_CI_GATE = "ci-gate/gate.json" +_SCRIPT, _DEPENDENCY, _REF = "script", _DEPENDENCY_KIND, "forbidden_ref" +_REFUSING = (ACTION_BLOCK, ACTION_ASK) +_NOT_REQUIRED = ( + "floor: a floor only where this check is a required status check; that is the repository's own " + "setting, unverified by chock. It judges committed content only." +) + + +def _out(line: str) -> None: + sys.stdout.write(line + "\n") + + +def _err(line: str) -> None: + sys.stderr.write(line + "\n") + + +@dataclass(frozen=True) +class Gate: + """One compiled ci gate as one revision commits it.""" + + policy: str + source: str + rev: Revision + path: str + spec: dict + + +@dataclass +class Outcome: + refusals: list[str] + undecided: list[str] + notes: list[str] + + +def _gates(rev: Revision, source: str) -> dict[str, Gate]: + found: dict[str, Gate] = {} + for path in rev.paths(COMPILED_PREFIX): + rest = path.removeprefix(COMPILED_PREFIX) + policy, _, tail = rest.partition("/") + if tail != _CI_GATE: + continue + try: + spec = json.loads(rev.text(path) or "") + except ValueError: + spec = None + found[policy] = Gate(policy, source, rev, path, spec if isinstance(spec, dict) else {}) + return found + + +def policy_set(base: Revision, head: Revision) -> tuple[list[Gate], list[str]]: + """Every base gate, plus each head gate that is new or differs (both run; the stricter binds). + + A head script gate is new code and is never run here: the base set binds until it merges. + """ + at_base, at_head = _gates(base, "base"), _gates(head, "head") + run = list(at_base.values()) + notes: list[str] = [] + for policy, gate in sorted(at_head.items()): + same = policy in at_base and base.text(at_base[policy].path) == head.text(gate.path) + if same: + continue + if gate.spec.get("kind") == _SCRIPT: + notes.append(f"{policy} [head]: not judged at floor (new script code, runs after merge)") + else: + run.append(gate) + return run, notes + + +def _params(gate: Gate, tree: Path) -> dict: + """The gate's params, with every file it names taken from the gate's own revision, materialised under `tree`.""" + params = dict(gate.spec.get("params") or {}) + if gate.spec.get("kind") == _SCRIPT: + if gate.spec.get("script_base") == SCRIPT_BASE_GATE: + folder = gate.path.rsplit("/", 1)[0] + script = f"{folder}/{params.get('script', '')}" + else: + script = str(params.get("script", "")) + folder = script.rsplit("/", 1)[0] if "/" in script else "" + gate.rev.materialise(f"{folder}/" if folder else script, tree) + params["script"] = str(tree / script) + if gate.spec.get("kind") == _DEPENDENCY and params.get("allowlist_file"): + named = str(params["allowlist_file"]) + gate.rev.materialise(named, tree) + params["allowlist_file"] = str(tree / named) + return params + + +def _own(gate: Gate) -> tuple[str, ...]: + """Only the policy's source folder: its compiled gate is judged like any other file at the floor.""" + return (f"{POLICIES_PREFIX}{gate.policy}/",) + + +def _judge(gate: Gate, repo: Path, revs: tuple[str, str, str], head_ref: str | None, tree: Path) -> GateResult | str: + """The gate's result, or why it could not judge.""" + spec, label = gate.spec, f"{gate.policy} [{gate.source}]" + kind = KINDS.get(spec.get("kind")) + if not spec or kind is None or spec.get("action", ACTION_BLOCK) not in _ACTION_RANK: + return f"{label}: unreadable gate, unknown kind or unknown action" + if spec.get("kind") == _REF and not head_ref: + return f"{label}: a branch gate needs --head-ref " + base, head, fork = revs + ctx = FloorContext( + repo, base=base, head=head, merge_base=fork, head_ref=head_ref, scope=spec.get("paths"), own=_own(gate) + ) + try: + return kind(ctx, _params(gate, tree / gate.source), FLOOR_EVENT) + except (ListingError, PatternDeadlineError, FloorError) as exc: + return f"{label}: {inert(str(exc))}" + + +def _verdict(result: GateResult, declared: str, level: str) -> str: + if result.allowed: + return "allow" + return min(result.verdict or declared, declared, _ROLLOUT_CEILING[level], key=_ACTION_RANK.__getitem__) + + +def judge_gates(repo: Path, base: Revision, head: Revision, fork: str, head_ref: str | None) -> Outcome: + """Run the policy set over base...head; `ask` refuses, since nobody can be asked in CI.""" + gates, notes = policy_set(base, head) + outcome = Outcome([], [], notes) + level = committed_rollout(repo, base.sha) + with tempfile.TemporaryDirectory(prefix="chock-floor-") as scratch: + for gate in gates: + if "commit" not in (gate.spec.get("on") or []) and gate.spec: + continue + result = _judge(gate, repo, (base.sha, head.sha, fork), head_ref, Path(scratch)) + if isinstance(result, str): + outcome.undecided.append(result) + continue + verdict = _verdict(result, gate.spec.get("action", ACTION_BLOCK), level) + if verdict == "allow": + continue + reason = _reason(result, gate.spec, FLOOR_EVENT, gate.policy) + line = f"{gate.policy} [{gate.source}] ({verdict}): {reason}" + (outcome.refusals if verdict in _REFUSING else outcome.notes).append(line) + return outcome + + +def floor(repo: Path, base_ref: str, head_ref_rev: str, branch: str | None) -> int: + repo = repo.resolve() + base_sha, head_sha = resolve(repo, base_ref, "base"), resolve(repo, head_ref_rev, "head") + fork = merge_base(repo, base_sha, head_sha) + base, head = Revision(repo, base_sha), Revision(repo, head_sha) + _out(f"== floor: base {base_sha[:12]}, head {head_sha[:12]}, merge-base {fork[:12]}") + outcome = judge_gates(repo, base, head, fork, branch) + tampered = [t.render() for t in tamper(base, head)] + history, history_notes = history_findings(repo, base, head, fork) + for line in outcome.notes + history_notes: + _out(f"note: {line}") + refusals = outcome.refusals + tampered + history + for line in refusals: + _out(f"FAIL {line}") + for line in outcome.undecided: + _err(f"UNDECIDED {line}") + _out(_NOT_REQUIRED) + if outcome.undecided: + return EXIT_UNDECIDED + return EXIT_REFUSED if refusals else 0 + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(prog="chock check --floor") + parser.add_argument("--repo", default=".") + parser.add_argument("--base", required=True, help="The pull request's base: its gates, waivers and rules bind") + parser.add_argument("--head", default="HEAD", help="The commit under test, read as objects only") + parser.add_argument("--head-ref", default=None, help="The branch under test, for branch gates") + args = parser.parse_args(argv) + try: + return floor(Path(args.repo), args.base, args.head, args.head_ref) + except FloorError as exc: + _err(f"floor: {inert(str(exc))}") + except Exception as exc: # noqa: BLE001 -- 1 means refused; anything undecided is 2, naming only the type + _err(f"floor: no verdict was established ({type(exc).__name__})") + return EXIT_UNDECIDED diff --git a/src/chock/validation/floor_cli.py b/src/chock/validation/floor_cli.py new file mode 100644 index 00000000..5516c5d6 --- /dev/null +++ b/src/chock/validation/floor_cli.py @@ -0,0 +1,27 @@ +"""`chock check --floor`'s flags on the check parser, and its dispatch from `chock check`.""" + +from __future__ import annotations + +import argparse + +from chock.validation.checks_floor import main + + +def add_arguments(parser: argparse.ArgumentParser) -> None: + parser.add_argument( + "--floor", + action="store_true", + help="Judge base...head with the base's gates, waivers and tamper rules, run by this engine; " + "no head file executes (needs --base; runs alone)", + ) + parser.add_argument("--head", default=None, help="With --floor: the commit under test (default HEAD)") + + +def from_check(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int: + """The floor runs alone: every other check reads, and the evals run, the head's own files.""" + if not args.floor: + parser.error("--head goes with --floor") + if not args.base or args.only or args.mode or args.event or args.format != "text" or args.output: + parser.error("--floor needs --base and takes no --only, --mode, --event, --format or --output") + argv = ["--repo", args.repo, "--base", args.base, "--head", args.head or "HEAD"] + return main(argv + (["--head-ref", args.head_ref] if args.head_ref else [])) diff --git a/src/chock/validation/floor_context.py b/src/chock/validation/floor_context.py new file mode 100644 index 00000000..e539ef0a --- /dev/null +++ b/src/chock/validation/floor_context.py @@ -0,0 +1,49 @@ +"""The material a gate judges at the floor: the base...head range, read by revision, never from the working tree.""" + +from __future__ import annotations + +from collections.abc import Sequence +from pathlib import Path + +from chock.gate.runner import GateContext + + +class FloorContext(GateContext): + """`base...head` as the change; the head's files as the proposal; the base's as what a waiver must already be in. + + Every read is a `git show :`, so a head file is data and never reaches a program as code. + """ + + def __init__( # noqa: PLR0913 -- the range's three revisions beside GateContext's own + self, + repo_root: Path, + *, + base: str, + head: str, + merge_base: str, + head_ref: str | None, + scope: Sequence[str] | None = None, + own: Sequence[str] = (), + ) -> None: + super().__init__(repo_root=repo_root, base=base, head_ref=head_ref, scope=scope, own=own) + self.head = head + self.merge_base = merge_base + + def _range(self) -> list[str]: + return [f"{self.base}...{self.head}"] + + def staged_blob(self, path: str) -> str: + """The proposal: the file at the head.""" + return self._git("show", f"{self.head}:{path}") + + def head_blob(self, path: str) -> str: + """Before the change: the file at the merge-base, the same side the three-dot diff compares against.""" + return self._git("show", f"{self.merge_base}:{path}") + + def committed_blob(self, path: str) -> str: + """Where a waiver must already be for the floor to honour it: the file at the base.""" + return self._git("show", f"{self.base}:{path}") + + def current_branch(self) -> str: + """The branch under test is the one named; the checkout's own branch is the base's, never the head's.""" + return self.head_ref or "" diff --git a/src/chock/validation/floor_git.py b/src/chock/validation/floor_git.py new file mode 100644 index 00000000..bb871ed7 --- /dev/null +++ b/src/chock/validation/floor_git.py @@ -0,0 +1,110 @@ +"""Read-only git access for `chock check --floor`: committed files by revision, never the working tree.""" + +from __future__ import annotations + +import os +import subprocess +from dataclasses import dataclass, field +from pathlib import Path + +_GIT_TIMEOUT = 120 +_TREE_FIELDS = 2 +_BLOB = "blob" +_REGULAR_MODES = ("100644", "100755") + + +class FloorError(RuntimeError): + """The floor cannot reach a verdict: exit 2, never a pass.""" + + +def git(repo: Path, *args: str, data: bytes | None = None) -> subprocess.CompletedProcess[bytes]: + try: + return subprocess.run( # noqa: S603 -- reading committed revisions is this module's whole job + ["git", "-C", str(repo), "--literal-pathspecs", *args], # noqa: S607 + input=data, + capture_output=True, + check=False, + timeout=_GIT_TIMEOUT, + ) + except (OSError, subprocess.SubprocessError) as exc: + msg = f"git could not run ({type(exc).__name__})" + raise FloorError(msg) from exc + + +def resolve(repo: Path, rev: str, what: str) -> str: + """The commit `rev` names; FloorError when it names none.""" + proc = git(repo, "rev-parse", "--verify", "--quiet", f"{rev}^{{commit}}") + sha = proc.stdout.decode("ascii", "replace").strip() + if proc.returncode or not sha: + msg = f"{what} ref {rev!r} does not resolve -- refusing to judge an empty range. Fetch it (fetch-depth: 0)." + raise FloorError(msg) + return sha + + +@dataclass +class Revision: + """One commit's tree, read through git: path listing and file text, each read once.""" + + repo: Path + sha: str + _entries: dict[str, tuple[str, str]] | None = None + _texts: dict[str, str | None] = field(default_factory=dict) + + def entries(self) -> dict[str, tuple[str, str]]: + """path -> (mode, blob oid) for every regular file and symlink in the tree.""" + if self._entries is None: + proc = git(self.repo, "ls-tree", "-r", "-z", "--full-tree", self.sha) + if proc.returncode: + msg = f"git ls-tree {self.sha[:12]} failed" + raise FloorError(msg) + found: dict[str, tuple[str, str]] = {} + for record in proc.stdout.split(b"\0"): + meta, _, raw_path = record.partition(b"\t") + parts = meta.decode("ascii", "replace").split() + if len(parts) > _TREE_FIELDS and parts[1] == _BLOB: + found[os.fsdecode(raw_path)] = (parts[0], parts[2]) + self._entries = found + return self._entries + + def paths(self, prefix: str = "") -> list[str]: + return sorted(path for path in self.entries() if path.startswith(prefix)) + + def has(self, path: str) -> bool: + return path in self.entries() + + def text(self, path: str) -> str | None: + """The file as committed, decoded as utf-8 with replacement; None when the tree has no such file.""" + if path not in self._texts: + entry = self.entries().get(path) + if entry is None: + self._texts[path] = None + else: + proc = git(self.repo, "cat-file", "blob", entry[1]) + if proc.returncode: + msg = f"git cat-file {path} at {self.sha[:12]} failed" + raise FloorError(msg) + self._texts[path] = proc.stdout.decode("utf-8", "replace") + return self._texts[path] + + def materialise(self, prefix: str, into: Path) -> None: + """Write the regular files under `prefix` into `into` (symlinks are left out: never followed, never made).""" + for path in self.paths(prefix): + mode, _ = self.entries()[path] + if mode not in _REGULAR_MODES or ".." in Path(path).parts: + continue + target = into / path + target.parent.mkdir(parents=True, exist_ok=True) + proc = git(self.repo, "cat-file", "blob", self.entries()[path][1]) + if proc.returncode: + msg = f"git cat-file {path} at {self.sha[:12]} failed" + raise FloorError(msg) + target.write_bytes(proc.stdout) + + +def merge_base(repo: Path, base: str, head: str) -> str: + proc = git(repo, "merge-base", base, head) + sha = proc.stdout.decode("ascii", "replace").strip() + if proc.returncode or not sha: + msg = f"base {base[:12]} and head {head[:12]} share no history; refusing to judge an unrelated range" + raise FloorError(msg) + return sha diff --git a/src/chock/validation/floor_history.py b/src/chock/validation/floor_history.py new file mode 100644 index 00000000..4a3383b7 --- /dev/null +++ b/src/chock/validation/floor_history.py @@ -0,0 +1,58 @@ +"""The floor's per-commit pass: each commit in merge-base..head judged with the base's content gates. + +The cumulative diff misses a secret added in one commit and removed in the next; this pass does not. +A line the base already has is not the pull request's, and a waiver the pull request adds is not +honoured, so the gates run with no waiver pattern and lines present at the base are dropped. +""" + +from __future__ import annotations + +import dataclasses +import tempfile +from pathlib import Path + +from chock.display import inert +from chock.history import gitlog +from chock.history.gates import RULE_CONTENT, load_rules +from chock.history.scan import Finding, Limits, scan +from chock.validation.floor_git import FloorError, Revision + +_COMPILED = ".chock/compiled/" +_SHORT = 12 + + +def _introduced(finding: Finding, head: Revision, at_base: tuple[Revision, ...]) -> bool: + """Whether the finding is the pull request's: its line (or path) is not already at the base.""" + if finding.rule != RULE_CONTENT or not finding.line: + return not any(rev.has(finding.path) for rev in at_base) + commit = Revision(head.repo, finding.commit) + lines = (commit.text(finding.path) or "").splitlines() + line = lines[finding.line - 1] if finding.line <= len(lines) else None + return line is None or not any(line in (rev.text(finding.path) or "").splitlines() for rev in at_base) + + +def history_findings(repo: Path, base: Revision, head: Revision, fork: str) -> tuple[list[str], list[str]]: + """(refusals, notes) of the per-commit pass; FloorError when it cannot judge.""" + with tempfile.TemporaryDirectory(prefix="chock-floor-history-") as scratch: + root = Path(scratch) + base.materialise(_COMPILED, root) + try: + rules, _skipped = load_rules(root) + except gitlog.HistoryError as exc: + if not root.joinpath(_COMPILED).is_dir() or "no installed content gates" in str(exc): + return [], ["history: the base has no content gates; per-commit pass not run"] + msg = f"history: {exc}" + raise FloorError(msg) from exc + rules = [dataclasses.replace(rule, pragma_pattern=None) for rule in rules] + try: + report = scan(repo, rules, fork, Limits(), head.sha) + except gitlog.HistoryError as exc: + msg = f"history: {exc}" + raise FloorError(msg) from exc + at_base = (base, Revision(repo, fork)) + lines = [ + f"{f.policy} [history]: {f.commit[:_SHORT]} {inert(f.path)}{f':{f.line}' if f.line else ''} ({f.rule})" + for f in report.findings + if _introduced(f, head, at_base) + ] + return lines, [] diff --git a/src/chock/validation/tamper.py b/src/chock/validation/tamper.py new file mode 100644 index 00000000..b36366d2 --- /dev/null +++ b/src/chock/validation/tamper.py @@ -0,0 +1,253 @@ +"""The floor's built-in tamper rules: a guardrail the base has and the head removed or weakened, by name. + +Each rule compares base against head structurally and refuses only a removal or a weakening, so an +addition or a regeneration (`chock sync`) stays silent. The facts live in `data/tamper.json`, inside +the engine, so the pull request under judgement cannot edit them. +""" + +from __future__ import annotations + +import fnmatch +import json +import re +from dataclasses import dataclass +from functools import cache +from typing import Any + +import yaml + +from chock import yamlio +from chock.display import inert +from chock.gate.runner.constants import _ACTION_RANK +from chock.resources import package_data_dir +from chock.validation.floor_git import Revision + +_UNIVERSAL = frozenset({"", "*"}) +_SHOWN = 120 +_CONFIG = ".chock/config.yaml" +#: YAML 1.1 reads a bare `on:` key as boolean true. +_ON_KEYS = ("on", True) + + +@cache +def facts() -> dict[str, Any]: + return json.loads(package_data_dir("chock", "data").joinpath("tamper.json").read_text(encoding="utf-8")) + + +@dataclass(frozen=True) +class Tamper: + rule: str + path: str + detail: str + + def render(self) -> str: + return f"tamper/{self.rule} {inert(self.path)}: {inert(self.detail)}" + + +def _norm(text: str) -> str: + """`text` with the parts a regeneration changes (hashes, versions) replaced by one placeholder.""" + return re.sub(facts()["volatile_pattern"], facts()["volatile_placeholder"], text) + + +def _shown(text: str) -> str: + return text if len(text) <= _SHOWN else text[: _SHOWN - 3] + "..." + + +def _json(text: str | None) -> Any: + if text is None: + return None + try: + return json.loads(text) + except ValueError: + return None + + +# --- hook registrations --------------------------------------------------------------------------- + + +@dataclass(frozen=True) +class _Hook: + event: str + matchers: frozenset[str] + command: tuple[tuple[str, str], ...] + shown: str + + +def _pointed(doc: Any, pointer: str) -> Any: + for part in [p for p in pointer.split("/") if p]: + doc = doc.get(part) if isinstance(doc, dict) else None + return doc + + +def _hook(event: str, matcher: object, item: dict) -> _Hook | None: + keys = facts()["hook_command_keys"] + command = tuple((key, _norm(str(item[key]))) for key in keys if isinstance(item.get(key), str)) + if not command: + return None + raw = str(matcher) if isinstance(matcher, str) else "" + matchers = frozenset(part.strip() for part in raw.split(facts()["hook_matcher_separator"])) + return _Hook(event, matchers, command, _shown(str(item[keys[0]] if keys[0] in item else command[0][1]))) + + +def hooks(doc: Any, pointer: str) -> list[_Hook]: + """Every (event, matcher, command) entry of a vendor hook file, nested or flat.""" + table = _pointed(doc, pointer) + found: list[_Hook] = [] + nested, matcher_key = facts()["hook_nested_key"], facts()["hook_matcher_key"] + for event, items in table.items() if isinstance(table, dict) else (): + for item in items if isinstance(items, list) else (): + if not isinstance(item, dict): + continue + inner = item.get(nested) + leaves = [h for h in inner if isinstance(h, dict)] if isinstance(inner, list) else [item] + for leaf in leaves: + entry = _hook(str(event), leaf.get(matcher_key, item.get(matcher_key)), leaf) + if entry is not None: + found.append(entry) + return found + + +def _covers(head: frozenset[str], base: frozenset[str]) -> bool: + return bool(head & _UNIVERSAL) or (not base & _UNIVERSAL and head >= base) + + +def _kept(entry: _Hook, head: list[_Hook]) -> bool: + return any( + h.event == entry.event and h.command == entry.command and _covers(h.matchers, entry.matchers) for h in head + ) + + +def hook_removed(base: Revision, head: Revision) -> list[Tamper]: + found: list[Tamper] = [] + for spec in facts()["hook_files"]: + for path in [p for p in base.paths() if fnmatch.fnmatchcase(p, spec["glob"])]: + was = _json(base.text(path)) + if was is None: + continue + now = _json(head.text(path)) + kept = hooks(now, spec["pointer"]) if now is not None else [] + for entry in hooks(was, spec["pointer"]): + if not _kept(entry, kept): + found.append(Tamper("hook-removed", path, f"{entry.event} hook removed: {entry.shown}")) + return found + + +# --- compiled gates, the lock and the vendored runtime ------------------------------------------- + + +def _gate_paths(rev: Revision) -> list[str]: + prefix, name = facts()["compiled_prefix"], facts()["gate_file"] + return [ + path + for path in rev.paths(prefix) + if any(fnmatch.fnmatchcase(path, f"{prefix}*/{surface}/{name}") for surface in facts()["gate_surfaces"]) + ] + + +def gate_rules(base: Revision, head: Revision) -> list[Tamper]: + found: list[Tamper] = [] + for path in _gate_paths(base): + was = _json(base.text(path)) + if not isinstance(was, dict): + continue + if not head.has(path): + found.append(Tamper("gate-removed", path, "compiled gate present at the base, deleted at the head")) + continue + now = _json(head.text(path)) + if not isinstance(now, dict): + found.append(Tamper("gate-weakened", path, "gate is not a JSON object at the head")) + continue + lost = sorted(set(was.get("on") or []) - set(now.get("on") or [])) + if lost: + found.append(Tamper("gate-weakened", path, f"no longer bound to: {', '.join(map(str, lost))}")) + was_action, now_action = str(was.get("action", "block")), str(now.get("action", "block")) + if _ACTION_RANK.get(now_action, -1) < _ACTION_RANK.get(was_action, -1): + found.append(Tamper("gate-weakened", path, f"action {was_action} -> {now_action}")) + return found + + +def _pack_ids(text: str | None) -> set[str]: + doc = _json(text) + packs = doc.get("packs") if isinstance(doc, dict) else None + return {str(p["id"]) for p in packs or () if isinstance(p, dict) and "id" in p} + + +def lock_pack_removed(base: Revision, head: Revision) -> list[Tamper]: + lock = facts()["lock_file"] + gone = sorted(_pack_ids(base.text(lock)) - _pack_ids(head.text(lock))) + return [ + Tamper("lock-pack-removed", lock, f"pack {pack!r} is locked at the base, missing at the head") for pack in gone + ] + + +def runtime_removed(base: Revision, head: Revision) -> list[Tamper]: + prefix = facts()["runtime_prefix"] + kept = {_norm(path) for path in head.paths(prefix)} + return [ + Tamper("runtime-removed", path, "vendored runtime file deleted") + for path in base.paths(prefix) + if _norm(path) not in kept + ] + + +# --- the required workflow ------------------------------------------------------------------------ + + +def _yaml(text: str | None) -> Any: + if text is None: + return None + try: + return yamlio.safe_load(text) + except yaml.YAMLError: + return None + + +def workflow_path(base: Revision) -> str: + """The workflow the floor guards: the base config's `floor_workflow`, else the default.""" + config = _yaml(base.text(_CONFIG)) + named = config.get(facts()["workflow_config_key"]) if isinstance(config, dict) else None + return named if isinstance(named, str) and named.strip() else facts()["workflow_default"] + + +def _triggers(doc: dict) -> set[str]: + raw = next((doc[key] for key in _ON_KEYS if key in doc), None) + if isinstance(raw, str): + return {raw} + return {str(t) for t in raw} if isinstance(raw, list | dict) else set() + + +def _steps(job: object) -> set[tuple[str, str]]: + steps = job.get("steps") if isinstance(job, dict) else None + return { + (str(s.get("name", "")), _norm(str(s.get("run") or s.get("uses") or ""))) + for s in steps or () + if isinstance(s, dict) + } + + +def ci_step_removed(base: Revision, head: Revision) -> list[Tamper]: + path = workflow_path(base) + was = _yaml(base.text(path)) + if not isinstance(was, dict): + return [] + now = _yaml(head.text(path)) + if not isinstance(now, dict): + return [Tamper("ci-step-removed", path, "workflow deleted or unreadable at the head")] + found = [Tamper("ci-step-removed", path, f"trigger {t!r} removed") for t in sorted(_triggers(was) - _triggers(now))] + head_jobs = now.get("jobs") if isinstance(now.get("jobs"), dict) else {} + base_jobs = was.get("jobs") if isinstance(was.get("jobs"), dict) else {} + for job_id, job in base_jobs.items(): + if job_id not in head_jobs: + found.append(Tamper("ci-step-removed", path, f"job {job_id!r} removed")) + continue + for name, body in sorted(_steps(job) - _steps(head_jobs[job_id])): + found.append(Tamper("ci-step-removed", path, f"job {job_id!r}: step {_shown(name or body)!r} removed")) + return found + + +RULES = (hook_removed, gate_rules, lock_pack_removed, runtime_removed, ci_step_removed) + + +def tamper(base: Revision, head: Revision) -> list[Tamper]: + """Every removal or weakening of a guardrail from base to head, in rule order.""" + return [finding for rule in RULES for finding in rule(base, head)] diff --git a/tests/fixtures/cli_help_golden.txt b/tests/fixtures/cli_help_golden.txt index 9c48e9ee..579ede22 100644 --- a/tests/fixtures/cli_help_golden.txt +++ b/tests/fixtures/cli_help_golden.txt @@ -8,7 +8,7 @@ Everyday: install Build a chock.sh selection into one plugin for one agent (install --selection [--client C]) remove Remove an installed policy and resync sync Recompile + rewire so the repo matches its policies (--ci/--skills for extras) - check Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits) + check Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor judges a pull request with its base's gates) status Policy states and coverage (--only registry,log for more) stop Refuse every agent tool call, commit and push here until resume (--agent C for one client) resume Lift a stop: agents are judged by the policies again diff --git a/tests/floor_support.py b/tests/floor_support.py new file mode 100644 index 00000000..f63948d5 --- /dev/null +++ b/tests/floor_support.py @@ -0,0 +1,134 @@ +"""Fixture repositories for `chock check --floor`: a base commit on `main`, a pull request on `pr`.""" + +from __future__ import annotations + +import json +import subprocess +from pathlib import Path + +from chock.validation.checks_floor import main as floor_main + +TOKEN_PATTERN = "FLOOR-TOKEN-[0-9]+" +TEST_PRAGMA = "chock: test-removal-reviewed" +CHECKOUT = "actions/checkout@" + "0" * 40 + +TEST_INTEGRITY = { + "kind": "test_integrity", + "on": ["commit"], + "action": "block", + "message": "Tests were weakened, not fixed.", + "params": { + "test_path_regex": "(^|/)tests?/", + "assertion_pattern": "\\bassert\\b", + "allowlist_pragma": TEST_PRAGMA, + }, +} +FLOOR_TOKEN = { + "kind": "content_regex", + "on": ["commit", "tool_use"], + "action": "block", + "message": "No floor tokens.", + "params": {"content_pattern": TOKEN_PATTERN}, +} +PROBE = { + "kind": "script", + "on": ["commit"], + "action": "block", + "message": "probe", + "params": {"script": ".agents/policies/probe/implementations/probe.py"}, +} +PROBE_SCRIPT = """import json, os, sys +payload = json.load(sys.stdin) +if os.environ.get("FLOOR_PROBE_SECRET"): + sys.stderr.write("probe: the environment reached the script\\n") + sys.exit(1) +if payload.get("deleted"): + sys.stderr.write("probe: deleted " + ",".join(payload["deleted"]) + "\\n") + sys.exit(1) +sys.exit(0) +""" +MARKER_SCRIPT = """import pathlib, sys +pathlib.Path("MARKER").write_text("ran", encoding="utf-8") +sys.exit(0) +""" +CLAUDE = { + "hooks": { + "PreToolUse": [ + { + "matcher": "Bash", + "hooks": [{"type": "command", "command": "sh .chock/bin/launch.sh claude.py --sha abc1234def"}], + }, + { + "matcher": "Write|Edit", + "hooks": [{"type": "command", "command": "sh .chock/bin/launch.sh claude.py --write"}], + }, + ] + } +} +CURSOR = {"version": 1, "hooks": {"beforeShellExecution": [{"command": "sh .chock/bin/launch.sh cursor.py"}]}} +LOCK = {"lockfile_version": "2", "engine": ">=1.2,<2", "packs": [{"id": "test-integrity"}, {"id": "floor-token"}]} +WORKFLOW = f"""name: chock-floor +on: + pull_request_target: + merge_group: +jobs: + floor: + runs-on: ubuntu-latest + steps: + - uses: {CHECKOUT} + - name: Floor + run: chock check --floor --base "$BASE_SHA" --head "$HEAD_SHA" +""" +TEST_FILE = "def test_a():\n assert 1 + 1 == 2\n assert 2 * 2 == 4\n" + + +def git(repo: Path, *args: str) -> str: + proc = subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True, text=True) + return proc.stdout.strip() + + +def write(repo: Path, rel: str, content: object) -> None: + path = repo / rel + path.parent.mkdir(parents=True, exist_ok=True) + text = content if isinstance(content, str) else json.dumps(content, indent=2) + "\n" + path.write_text(text, encoding="utf-8") + + +def gate(repo: Path, policy: str, spec: dict, *, surfaces: tuple[str, ...] = ("ci-gate", "git-hook")) -> None: + for surface in surfaces: + write(repo, f".chock/compiled/{policy}/{surface}/gate.json", spec) + + +def commit(repo: Path, message: str) -> str: + git(repo, "add", "-A") + git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "--no-gpg-sign", "-m", message) + return git(repo, "rev-parse", "HEAD") + + +def base_repo(tmp_path: Path) -> Path: + """`main` carries two gates, a probe script gate, hook files, a lock, a runtime, a workflow and a test; `pr` is checked out.""" + repo = tmp_path / "repo" + repo.mkdir() + git(repo, "init", "-q", "-b", "main") + gate(repo, "test-integrity", TEST_INTEGRITY) + gate(repo, "floor-token", FLOOR_TOKEN) + gate(repo, "probe", PROBE, surfaces=("ci-gate",)) + write(repo, PROBE["params"]["script"], PROBE_SCRIPT) + write(repo, ".claude/settings.json", CLAUDE) + write(repo, ".cursor/hooks.json", CURSOR) + write(repo, "chock.lock", LOCK) + write(repo, ".chock/bin/gate.py", "print('runner')\n") + write(repo, ".chock/bin/index/claude-PreToolUse-3a2134a7.json", "{}\n") + write(repo, ".github/workflows/chock.yml", WORKFLOW) + write(repo, "tests/test_core.py", TEST_FILE) + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "base") + git(repo, "checkout", "-q", "-b", "pr") + return repo + + +def run_floor(repo: Path, capsys, *extra: str) -> tuple[int, str]: + """The floor's exit code and everything it printed.""" + code = floor_main(["--repo", str(repo), "--base", "main", *extra]) + out = capsys.readouterr() + return code, out.out + out.err diff --git a/tests/test_floor_check.py b/tests/test_floor_check.py new file mode 100644 index 00000000..793cee47 --- /dev/null +++ b/tests/test_floor_check.py @@ -0,0 +1,180 @@ +"""`chock check --floor`: the base's gates bind, waivers count only from the base, ask refuses, nothing of the head runs.""" + +from __future__ import annotations + +import pytest +from floor_support import ( + FLOOR_TOKEN, + MARKER_SCRIPT, + PROBE, + TEST_FILE, + TEST_PRAGMA, + base_repo, + commit, + gate, + git, + run_floor, + write, +) + +from chock.lifecycle import check_main + +NO_OP = {"kind": "content_regex", "on": ["commit"], "action": "warn", "params": {"content_pattern": "(?!)"}} + + +def test_a_head_rewrite_of_the_gate_does_not_stop_the_base_gate(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + gate(repo, "test-integrity", NO_OP) + (repo / "tests/test_core.py").unlink() + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "FAIL test-integrity [base] (block)" in out + assert "tests/test_core.py: test file deleted" in out + + +def test_a_waiver_the_pull_request_adds_is_not_honoured(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + write(repo, "tests/test_core.py", f"def test_a():\n # {TEST_PRAGMA}\n assert 1 + 1 == 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "assertions removed across tests" in out + + +def test_a_waiver_already_at_the_base_is_honoured(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, "tests/test_core.py", TEST_FILE.replace("== 4\n", f"== 4 # {TEST_PRAGMA}\n")) + commit(repo, "base waiver") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "tests/test_core.py", "def test_a():\n assert 1 + 1 == 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_ask_counts_as_a_refusal(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + gate(repo, "floor-token", {**FLOOR_TOKEN, "action": "ask"}) + commit(repo, "ask") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-1'\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "FAIL floor-token [base] (ask)" in out + + +def test_a_secret_added_then_removed_is_found_by_the_history_pass(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-7'\n") + first = commit(repo, "add") + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "remove") + code, out = run_floor(repo, capsys) + assert code == 1 + assert f"floor-token [history]: {first[:12]} src/app.py:1 (content-pattern)" in out + + +def test_a_line_already_at_the_base_is_not_the_pull_requests(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-3'\n") + commit(repo, "before adoption") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-3'\nOTHER = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_a_script_gate_gets_deleted_paths_and_no_environment(tmp_path, capsys, monkeypatch) -> None: + repo = base_repo(tmp_path) + (repo / "src/app.py").unlink() + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "probe: deleted src/app.py" in out + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "restore") + monkeypatch.setenv("FLOOR_PROBE_SECRET", "1") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_no_head_file_is_executed(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + write(repo, PROBE["params"]["script"], MARKER_SCRIPT) + gate(repo, "new-probe", {**PROBE, "params": {"script": "tools/new_probe.py"}}, surfaces=("ci-gate",)) + write(repo, "tools/new_probe.py", MARKER_SCRIPT) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert not (repo / "MARKER").exists() + assert "new-probe [head]: not judged at floor" in out + assert code == 0, out + + +def test_the_head_is_read_as_objects_from_a_base_checkout(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + (repo / ".cursor/hooks.json").unlink() + commit(repo, "pr") + git(repo, "checkout", "-q", "main") + code, out = run_floor(repo, capsys, "--head", "pr") + assert code == 1 + assert "tamper/hook-removed .cursor/hooks.json" in out + + +def test_an_unresolvable_base_is_undecided(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + code = check_main(["--repo", str(repo), "--floor", "--base", "no-such-ref"]) + assert code == 2 + assert "does not resolve" in capsys.readouterr().err + + +def test_a_branch_gate_without_the_branch_is_undecided(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + gate( + repo, + "protect-main", + {"kind": "forbidden_ref", "on": ["commit"], "action": "block", "params": {"refs": ["main"]}}, + ) + commit(repo, "ref gate") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/other.py", "X = 1\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 2 + assert "needs --head-ref" in out + code, out = run_floor(repo, capsys, "--head-ref", "pr") + assert code == 0, out + code, out = run_floor(repo, capsys, "--head-ref", "main") + assert code == 1 + + +@pytest.mark.parametrize( + "argv", + [ + ["--floor"], + ["--floor", "--base", "main", "--only", "verify"], + ["--head", "x"], + ["--floor", "--base", "m", "--format", "sarif"], + ], +) +def test_the_floor_runs_alone_and_needs_a_base(tmp_path, argv) -> None: + with pytest.raises(SystemExit) as exc: + check_main(["--repo", str(tmp_path), *argv]) + assert exc.value.code == 2 + + +def test_no_coverage_tier_is_credited_for_the_floor() -> None: + """A floor is the adopter's required-check setting, which chock cannot see: nothing grades it.""" + from chock.evidence import CLAIMS_PATH + + assert "floor" not in CLAIMS_PATH.read_text(encoding="utf-8").lower() + from chock.compile import surfaces + + assert not [name for name in vars(surfaces) if "floor" in name.lower()] diff --git a/tests/test_floor_tamper.py b/tests/test_floor_tamper.py new file mode 100644 index 00000000..c7936711 --- /dev/null +++ b/tests/test_floor_tamper.py @@ -0,0 +1,153 @@ +"""`chock check --floor` refuses, by name, a removed or weakened guardrail, and stays silent on a regeneration.""" + +from __future__ import annotations + +import json + +from floor_support import CLAUDE, CURSOR, FLOOR_TOKEN, LOCK, WORKFLOW, base_repo, commit, gate, run_floor, write + + +def test_an_unrelated_change_passes_with_no_tamper_line(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + write(repo, "src/other.py", "OTHER = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "tamper/" not in out + assert "unverified by chock" in out + + +def test_deleting_a_vendor_hook_file_is_refused_by_name(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + (repo / ".cursor/hooks.json").unlink() + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "tamper/hook-removed .cursor/hooks.json" in out + + +def test_removing_one_pre_tool_use_entry_names_the_event_and_command(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + settings = json.loads(json.dumps(CLAUDE)) + settings["hooks"]["PreToolUse"].pop(1) + write(repo, ".claude/settings.json", settings) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert ( + "tamper/hook-removed .claude/settings.json: PreToolUse hook removed: sh .chock/bin/launch.sh claude.py --write" + in out + ) + assert out.count("tamper/hook-removed") == 1 + + +def test_narrowing_a_hook_matcher_is_a_removal(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + settings = json.loads(json.dumps(CLAUDE)) + settings["hooks"]["PreToolUse"][1]["matcher"] = "Write" + write(repo, ".claude/settings.json", settings) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "tamper/hook-removed .claude/settings.json" in out + + +def test_a_regeneration_that_adds_a_policy_is_silent(tmp_path, capsys) -> None: + """New policy, new index hash in every command, a widened matcher, a renamed index file: no tamper line.""" + repo = base_repo(tmp_path) + settings = json.loads(json.dumps(CLAUDE)) + settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"] = "sh .chock/bin/launch.sh claude.py --sha 99887766ff" + settings["hooks"]["PreToolUse"][1]["matcher"] = "Write|Edit|MultiEdit" + settings["hooks"]["Stop"] = [ + {"hooks": [{"type": "command", "command": "sh .chock/bin/launch.sh claude.py --stop"}]} + ] + write(repo, ".claude/settings.json", settings) + cursor = json.loads(json.dumps(CURSOR)) + cursor["hooks"]["preToolUse"] = [{"command": "sh .chock/bin/launch.sh cursor.py --write"}] + write(repo, ".cursor/hooks.json", cursor) + (repo / ".chock/bin/index/claude-PreToolUse-3a2134a7.json").unlink() + write(repo, ".chock/bin/index/claude-PreToolUse-77fe01bc.json", "{}\n") + gate(repo, "new-policy", {**FLOOR_TOKEN, "params": {"content_pattern": "NEW-POLICY-[0-9]+"}}) + write(repo, "chock.lock", {**LOCK, "packs": [*LOCK["packs"], {"id": "new-policy"}]}) + write(repo, ".github/workflows/chock.yml", WORKFLOW.replace("0" * 40, "1" * 40)) + commit(repo, "regenerate") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "tamper/" not in out + + +def test_removing_a_pack_from_the_lock_is_refused(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + write(repo, "chock.lock", {**LOCK, "packs": LOCK["packs"][:1]}) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "tamper/lock-pack-removed chock.lock: pack 'floor-token'" in out + + +def test_removing_the_floor_step_from_the_workflow_is_refused(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + write(repo, ".github/workflows/chock.yml", WORKFLOW.split(" - name: Floor")[0]) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "tamper/ci-step-removed .github/workflows/chock.yml: job 'floor': step 'Floor' removed" in out + + +def test_dropping_a_trigger_is_refused(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + write(repo, ".github/workflows/chock.yml", WORKFLOW.replace(" merge_group:\n", "")) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "trigger 'merge_group' removed" in out + + +def test_the_guarded_workflow_is_named_by_the_base_config(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + (repo / ".github/workflows/chock.yml").rename(repo / ".github/workflows/floor.yml") + write(repo, ".chock/config.yaml", "floor_workflow: .github/workflows/floor.yml\n") + from floor_support import git + + git(repo, "checkout", "-q", "main") + commit(repo, "move the workflow") + git(repo, "checkout", "-q", "-B", "pr") + (repo / ".github/workflows/floor.yml").unlink() + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "tamper/ci-step-removed .github/workflows/floor.yml" in out + + +def test_weakening_a_gate_action_or_binding_is_refused(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + gate(repo, "floor-token", {**FLOOR_TOKEN, "action": "warn", "on": ["tool_use"]}, surfaces=("git-hook",)) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "tamper/gate-weakened .chock/compiled/floor-token/git-hook/gate.json: no longer bound to: commit" in out + assert "action block -> warn" in out + + +def test_deleting_a_compiled_gate_is_refused(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + (repo / ".chock/compiled/floor-token/ci-gate/gate.json").unlink() + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "tamper/gate-removed .chock/compiled/floor-token/ci-gate/gate.json" in out + + +def test_an_edited_runner_is_never_run_and_only_its_deletion_is_named(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + write(repo, ".chock/bin/gate.py", "raise SystemExit(0)\n") + (repo / "tests/test_core.py").unlink() + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1 + assert "tests/test_core.py: test file deleted" in out + assert "tamper/runtime-removed" not in out + (repo / ".chock/bin/gate.py").unlink() + commit(repo, "pr 2") + code, out = run_floor(repo, capsys) + assert "tamper/runtime-removed .chock/bin/gate.py" in out From 1a60f55967d2d7ffcebfc560fabdf285fe444f48 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:40:13 +0000 Subject: [PATCH 12/57] refactor: keep the warn/error print lines identical to main legible() runs on its own line first, so the audited sinks are unchanged and their existing code-scanning triage stays attached. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- src/chock/output.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/chock/output.py b/src/chock/output.py index 0356a849..cb5c35e1 100644 --- a/src/chock/output.py +++ b/src/chock/output.py @@ -77,16 +77,18 @@ def new_command() -> None: _said.clear() -def warn(message: str) -> None: - # codeql[py/clear-text-logging-sensitive-data] - print(f"[WARN] {legible(message, sys.stderr)}", file=sys.stderr) - - def warn_once(key: tuple[str, ...], message: str) -> None: if first_time(key): warn(message) +def warn(message: str) -> None: + message = legible(message, sys.stderr) + # codeql[py/clear-text-logging-sensitive-data] + print(f"[WARN] {message}", file=sys.stderr) + + def error(message: str) -> None: + message = legible(message, sys.stderr) # codeql[py/clear-text-logging-sensitive-data] - print(f"[ERROR] {legible(message, sys.stderr)}", file=sys.stderr) + print(f"[ERROR] {message}", file=sys.stderr) From ff2e9abe3bb58a4686728b3c5257309be08c3fc2 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:48:31 +0000 Subject: [PATCH 13/57] test(floor): refresh plugin goldens and fit gate-dsl.md in its budget The plugin index hash follows the runner source, which W2-A1 changed; only --index-sha256 moves. spec/gate-dsl.md back to 300 lines. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- spec/gate-dsl.md | 10 +--------- tests/fixtures/plugin_targets/antigravity.json | 4 ++-- tests/fixtures/plugin_targets/droid.json | 6 +++--- tests/fixtures/plugin_targets/goose.json | 6 +++--- tests/fixtures/plugin_targets/kimi.json | 8 ++++---- tests/fixtures/plugin_targets/openhands.json | 6 +++--- 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/spec/gate-dsl.md b/spec/gate-dsl.md index b056f870..9882422f 100644 --- a/spec/gate-dsl.md +++ b/spec/gate-dsl.md @@ -227,15 +227,7 @@ script lives beside this gate file" instead of "under the repository root". Only plugin writes it: there the gate, the runner and the script travel together and no repository holds them. -Script payload contract: **version 2**. Version 1 (unnumbered) had no `ci-floor` event; version 2 adds -it, with the `deleted` key and the reduced environment below, and changes nothing at any other event. - -`event` is `commit`, `push`, `tool_use`, `ci-floor` or (see above) `tool_call`. `session` is present only at the agent events. -At `ci-floor` (`chock check --floor`) the payload also carries `deleted`: the sorted paths the -range deletes, so a deletion reaches the script (a run with only deletions still runs it). There -the script's environment holds only `PATH`, `HOME` and, where set, `SYSTEMROOT`, and `writes` is -the head's blobs; the script itself is the base's copy, a waiver counts only when the base already -has that line, and an `ask` refuses. `writes` is the staged blobs at `commit` and +Script payload contract **v2** (v1 was unnumbered; v2 adds `ci-floor`, from `chock check --floor`, and nothing elsewhere). `event` is `commit`, `push`, `tool_use`, `ci-floor` or (see above) `tool_call`. `session` is present only at the agent events. At `ci-floor` the payload adds `deleted` (sorted deleted paths; a deletion-only range still runs the script), the environment is only `PATH`, `HOME`, `SYSTEMROOT`, the script is the base's copy, a waiver counts only when the base has the line, and `ask` refuses. `writes` is the staged blobs at `commit` and `push`, and the write itself at `tool_use` -- the file a tool call is about to write, or what the turn left on disk at its end -- so one script serves every surface, and it reaches the write path and the turn's end exactly as `content_regex` does. The script answers with its diff --git a/tests/fixtures/plugin_targets/antigravity.json b/tests/fixtures/plugin_targets/antigravity.json index ce84eab1..51a128b7 100644 --- a/tests/fixtures/plugin_targets/antigravity.json +++ b/tests/fixtures/plugin_targets/antigravity.json @@ -41,7 +41,7 @@ "Stop": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-501fc3f8.json\" --index-sha256 e1a30025412bd51fc9294802aec6f3f90f87a006577b73a0fbd3a754379bcbae --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-501fc3f8.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 }, { @@ -55,7 +55,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b4ab2f9e.json\" --index-sha256 654471a853e58075776985e5cb35e276eb57b86dcc39cb934351dd5559dcb8a1 --root \"__CHOCK_PLUGIN_ROOT__\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b4ab2f9e.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"__CHOCK_PLUGIN_ROOT__\" --stop-check", "timeout": 45 } ], diff --git a/tests/fixtures/plugin_targets/droid.json b/tests/fixtures/plugin_targets/droid.json index 9f231a64..f5492d97 100644 --- a/tests/fixtures/plugin_targets/droid.json +++ b/tests/fixtures/plugin_targets/droid.json @@ -44,7 +44,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-f8a1fdfc.json\" --index-sha256 79f98b11b926dbebaa76c2a28e5bfe44b7e9eb49d4a6b3038f2f5933210af36e --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-f8a1fdfc.json\" --index-sha256 1558e2ef98f7a3b8cf1b8c8aa00e9dcc168f65154abfeec52db142123edfc615 --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ], @@ -54,7 +54,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-91dc9fe9.json\" --index-sha256 654471a853e58075776985e5cb35e276eb57b86dcc39cb934351dd5559dcb8a1 --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-91dc9fe9.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ], @@ -75,7 +75,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/Stop-185f9750.json\" --index-sha256 e1a30025412bd51fc9294802aec6f3f90f87a006577b73a0fbd3a754379bcbae --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/Stop-185f9750.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ] diff --git a/tests/fixtures/plugin_targets/goose.json b/tests/fixtures/plugin_targets/goose.json index 3083fc6e..19a2e919 100644 --- a/tests/fixtures/plugin_targets/goose.json +++ b/tests/fixtures/plugin_targets/goose.json @@ -40,7 +40,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-b6718bef.json\" --index-sha256 79f98b11b926dbebaa76c2a28e5bfe44b7e9eb49d4a6b3038f2f5933210af36e --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-b6718bef.json\" --index-sha256 1558e2ef98f7a3b8cf1b8c8aa00e9dcc168f65154abfeec52db142123edfc615 --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } @@ -51,7 +51,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-031b12f7.json\" --index-sha256 654471a853e58075776985e5cb35e276eb57b86dcc39cb934351dd5559dcb8a1 --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-031b12f7.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } @@ -74,7 +74,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/Stop-4cd5d275.json\" --index-sha256 e1a30025412bd51fc9294802aec6f3f90f87a006577b73a0fbd3a754379bcbae --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/Stop-4cd5d275.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } diff --git a/tests/fixtures/plugin_targets/kimi.json b/tests/fixtures/plugin_targets/kimi.json index d51fb906..f7141e5f 100644 --- a/tests/fixtures/plugin_targets/kimi.json +++ b/tests/fixtures/plugin_targets/kimi.json @@ -39,12 +39,12 @@ "hooks": [ { "event": "Stop", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 e1a30025412bd51fc9294802aec6f3f90f87a006577b73a0fbd3a754379bcbae --root \"${KIMI_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"${KIMI_PLUGIN_ROOT}\"", "timeout": 45 }, { "event": "PreToolUse", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 654471a853e58075776985e5cb35e276eb57b86dcc39cb934351dd5559dcb8a1 --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", "timeout": 45 }, { @@ -62,12 +62,12 @@ "hooks": [ { "event": "Stop", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 e1a30025412bd51fc9294802aec6f3f90f87a006577b73a0fbd3a754379bcbae --root \"${KIMI_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"${KIMI_PLUGIN_ROOT}\"", "timeout": 45 }, { "event": "PreToolUse", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 654471a853e58075776985e5cb35e276eb57b86dcc39cb934351dd5559dcb8a1 --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", "timeout": 45 }, { diff --git a/tests/fixtures/plugin_targets/openhands.json b/tests/fixtures/plugin_targets/openhands.json index 7d5085f3..ff591ed2 100644 --- a/tests/fixtures/plugin_targets/openhands.json +++ b/tests/fixtures/plugin_targets/openhands.json @@ -45,7 +45,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b310004d.json\" --index-sha256 79f98b11b926dbebaa76c2a28e5bfe44b7e9eb49d4a6b3038f2f5933210af36e --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b310004d.json\" --index-sha256 1558e2ef98f7a3b8cf1b8c8aa00e9dcc168f65154abfeec52db142123edfc615 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ], @@ -55,7 +55,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-81fe7457.json\" --index-sha256 654471a853e58075776985e5cb35e276eb57b86dcc39cb934351dd5559dcb8a1 --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-81fe7457.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ], @@ -76,7 +76,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-185f9750.json\" --index-sha256 e1a30025412bd51fc9294802aec6f3f90f87a006577b73a0fbd3a754379bcbae --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-185f9750.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ] From b064d6b2eaf5b0cce21c20a195a83592db688bf2 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:16:46 +0000 Subject: [PATCH 14/57] feat: chock emit --managed claude_code writes a managed drop-in and its bundle under --out `chock emit --managed claude_code --out ` writes, under /claude_code/ only: managed-settings.d/50-chock.json, a self-contained bundle per policy (launcher, runtime, guard or gate, from the plugin route), deploy.json and labels.json. Hook commands name the bundle by absolute path (--deploy-root); every PreToolUse hook carries onFailure "block" and a 45 s timeout. Stop hooks carry no onFailure: the vendor docs say it has no effect there. The command refuses an --out that resolves inside a system path (symlinks followed) and never writes into an existing /claude_code. --lock adds allowManagedHooksOnly; allowManagedPermissionRulesOnly is never emitted. Every label is best-effort (vendor-docs), unwitnessed; a new `managed` witness surface holds the rows that may later say otherwise. The compile-time managed-setting stub is removed: it wrote a shape Claude Code does not read. Native permissions.deny rules are not emitted yet. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2b1-managed-emit-claude.md | 14 ++ src/chock/compile/compiler.py | 2 - src/chock/compile/emitters/claude_managed.py | 40 --- .../compile/emitters/managed/__init__.py | 136 ++++++++++ .../compile/emitters/managed/claude_code.py | 113 +++++++++ src/chock/compile/emitters/managed/cli.py | 128 ++++++++++ src/chock/data/commands.json | 4 + src/chock/data/managed_channels.json | 46 ++++ src/chock/evidence.py | 4 +- tests/fixtures/cli_help_golden.txt | 1 + .../managed-setting/managed-settings.json | 4 - .../managed-setting/managed-settings.json | 4 - .../managed-setting/managed-settings.json | 4 - .../managed_emit/block-no-verify.json | 27 ++ tests/fixtures/managed_emit/scan-secrets.json | 38 +++ tests/test_coverage_honesty_p0c.py | 39 +-- tests/test_generated_newlines.py | 2 +- tests/test_managed_emit_claude.py | 232 ++++++++++++++++++ tests/test_witness_ledger.py | 2 +- 19 files changed, 757 insertions(+), 83 deletions(-) create mode 100644 changelog.d/w2b1-managed-emit-claude.md delete mode 100644 src/chock/compile/emitters/claude_managed.py create mode 100644 src/chock/compile/emitters/managed/__init__.py create mode 100644 src/chock/compile/emitters/managed/claude_code.py create mode 100644 src/chock/compile/emitters/managed/cli.py create mode 100644 src/chock/data/managed_channels.json delete mode 100644 tests/fixtures/emitter_stability/golden/stability-hook/managed-setting/managed-settings.json delete mode 100644 tests/fixtures/emitter_stability/golden/stability-rule/managed-setting/managed-settings.json delete mode 100644 tests/fixtures/emitter_stability/golden/stability-script/managed-setting/managed-settings.json create mode 100644 tests/fixtures/managed_emit/block-no-verify.json create mode 100644 tests/fixtures/managed_emit/scan-secrets.json create mode 100644 tests/test_managed_emit_claude.py diff --git a/changelog.d/w2b1-managed-emit-claude.md b/changelog.d/w2b1-managed-emit-claude.md new file mode 100644 index 00000000..eaf193ae --- /dev/null +++ b/changelog.d/w2b1-managed-emit-claude.md @@ -0,0 +1,14 @@ +- `chock emit --managed claude_code --out ` (preview) writes, under `/claude_code/` and + nowhere else, a Claude Code managed-settings drop-in (`managed-settings.d/50-chock.json`), the + self-contained hook bundle it runs (`bundle//scripts/`: launcher, runtime, guard or gate), + `deploy.json` (where an admin places each part, what every machine needs, what it does not + promise, the undo step) and `labels.json`. It refuses an `--out` that resolves inside a system + path, never writes into an existing `/claude_code`, elevates nothing and deploys nothing. + Hook commands name the bundle by absolute path (`--deploy-root`, default the vendor's admin + folder + `/chock`); every `PreToolUse` hook carries `onFailure: "block"` and a 45 s timeout. + `--lock` adds `allowManagedHooksOnly` (off by default: it stops every other hook on the machine); + `allowManagedPermissionRulesOnly` is never emitted. Every label is `best-effort (vendor-docs)`, + unwitnessed, until a `managed` witness row exists. A local administrator can still edit or + remove the file. Native `permissions.deny` rules are not emitted yet. +- `chock compile` no longer writes `managed-setting/managed-settings.json`: that file used a shape + Claude Code does not read. diff --git a/src/chock/compile/compiler.py b/src/chock/compile/compiler.py index 8ba395bd..b214c8eb 100644 --- a/src/chock/compile/compiler.py +++ b/src/chock/compile/compiler.py @@ -16,7 +16,6 @@ from chock.compile.emitters import ( ambient, ci, - claude_managed, git_hook, in_agent, mcp_gateway, @@ -44,7 +43,6 @@ Surface.CI_GATE: ci, Surface.PRE_TOOL_USE: in_agent.pre_tool_use, Surface.STOP: in_agent.stop, - Surface.MANAGED_SETTING: claude_managed, Surface.AMBIENT_RULE: ambient, Surface.MCP_GATEWAY: mcp_gateway, Surface.AGENT_HOOKS: in_agent.agent_hooks, diff --git a/src/chock/compile/emitters/claude_managed.py b/src/chock/compile/emitters/claude_managed.py deleted file mode 100644 index 69f8000e..00000000 --- a/src/chock/compile/emitters/claude_managed.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Emit Claude managed-settings.json fragments for a policy.""" - -from __future__ import annotations - -from pathlib import Path -from typing import Any - -from chock.emit import write_generated_json - - -def emit(policy_dir: Path, output_dir: Path, manifest: dict[str, Any]) -> list[Path]: - """Write a managed-settings.json fragment reflecting the policy's controls.""" - policy_id = manifest.get("id", policy_dir.name) - fragments: dict[str, Any] = {"deny": [], "ask": []} - - if policy_id == "scan-secrets": - fragments["deny"].append( - { - "type": "file", - "pattern": "\\.(env|pem|key|p12|pfx|jks|keystore)$", - "message": "Credential files are denied in the workspace.", - } - ) - fragments["deny"].append( - { - "type": "text", - "pattern": ( - "(?i)(AKIA[0-9A-Z]{16}|ghp_[a-zA-Z0-9]{36}|xox[bpas]-[0-9A-Za-z-]{10,}" - "|(sk|rk)_live_[0-9A-Za-z]{16,}|sk-ant-[0-9A-Za-z_-]{20,}|AIza[0-9A-Za-z_-]{35}" - "|npm_[0-9A-Za-z]{36}|eyJ[A-Za-z0-9_-]*\\.[A-Za-z0-9_-]*\\.[A-Za-z0-9_-]*" - "|-----BEGIN .* PRIVATE KEY-----)" - ), - "message": "Credential-like values are denied in generated or edited files.", - } - ) - - output_dir.mkdir(parents=True, exist_ok=True) - dest = output_dir / "managed-settings.json" - write_generated_json(dest, fragments) - return [dest] diff --git a/src/chock/compile/emitters/managed/__init__.py b/src/chock/compile/emitters/managed/__init__.py new file mode 100644 index 00000000..885b4efe --- /dev/null +++ b/src/chock/compile/emitters/managed/__init__.py @@ -0,0 +1,136 @@ +"""Managed emit: a vendor's admin-deployed hook file and the bundle it runs, written only under `--out`.""" + +from __future__ import annotations + +import json +import os +import re +import sys +from pathlib import Path, PurePosixPath +from typing import Any + +from agentseam import matrix_terms + +from chock import evidence +from chock.compile.levels import Grade, capped, in_agent_level, render_grade +from chock.compile.surface_kinds import Surface +from chock.emit import write_generated +from chock.hooks.launch import QUOTED_UNSAFE +from chock.resources import package_data_dir + +CHANNELS: dict[str, Any] = json.loads( + package_data_dir("chock", "data").joinpath("managed_channels.json").read_text(encoding="utf-8") +) +PLATFORMS = tuple(CHANNELS["system_roots"]) +VENDORS = tuple(CHANNELS["vendors"]) +#: Every managed label rests on the vendor's own docs until a `managed` witness row says otherwise. +BASIS = matrix_terms.BASIS_DOCS +WINDOWS = "windows" +_WINDOWS_ROOT = re.compile(r"[A-Za-z]:/[^/].*") +#: Windows system folders that may sit on another drive than C:. +_WINDOWS_ENV_ROOTS = ("ProgramFiles", "ProgramFiles(x86)", "ProgramData", "SystemRoot") + +Files = dict[PurePosixPath, str] + + +class EmitRefusedError(ValueError): + """The command would write somewhere it must not, or has nothing true to write.""" + + +def facts(vendor: str) -> dict[str, Any]: + """`vendor`'s managed-channel facts, as read from its docs on the recorded date.""" + return CHANNELS["vendors"][vendor] + + +def host_platform() -> str: + """This machine's platform, in the data file's words.""" + if sys.platform == "win32": + return WINDOWS + return "macos" if sys.platform == "darwin" else "linux" + + +def _system_roots(platform: str) -> list[str]: + roots = list(CHANNELS["system_roots"][platform]) + if platform == WINDOWS: + roots += [os.environ[name].replace("\\", "/") for name in _WINDOWS_ENV_ROOTS if os.environ.get(name)] + return roots + + +def system_root_of(path: Path, platform: str | None = None) -> str | None: + """The system path `path` resolves inside (symlinks followed), else None.""" + platform = platform or host_platform() + fold = str.casefold if platform == WINDOWS else str + text = fold(Path(path).resolve().as_posix()) + for root in _system_roots(platform): + base = fold(root.rstrip("/")) + if text == base or text.startswith(f"{base}/"): + return root + return None + + +def vendor_out(out: Path, vendor: str) -> Path: + """`/`, refused when `out` resolves into a system path or the folder already exists.""" + root = system_root_of(out) + if root is not None: + msg = ( + f"--out {out} resolves inside the system path {root}; chock emit writes only to a folder you " + "deploy from, never to where a client reads it" + ) + raise EmitRefusedError(msg) + dest = Path(out).resolve() / vendor + if os.path.lexists(dest): + msg = f"{dest} already exists; give a new --out or remove that folder first" + raise EmitRefusedError(msg) + return dest + + +def deploy_root(text: str, platform: str) -> str: + """The absolute folder the admin will copy the bundle to, in forward-slash form; a shell-read character is refused.""" + root = (text.replace("\\", "/") if platform == WINDOWS else text).rstrip("/") + absolute = _WINDOWS_ROOT.fullmatch(root) if platform == WINDOWS else len(root) > 1 and root.startswith("/") + if not absolute or QUOTED_UNSAFE.search(root): + msg = ( + f"--deploy-root {text!r} must be an absolute {platform} folder holding no quote, $, backquote, " + "backslash or line break: the hook commands name it inside double quotes" + ) + raise EmitRefusedError(msg) + return root + + +def write_tree(dest: Path, files: Files) -> list[Path]: + """Write `files` under the new folder `dest`, refusing any path that would leave it.""" + dest.mkdir(parents=True) + top = dest.resolve() + written = [] + for rel, text in sorted(files.items()): + path = dest.joinpath(*rel.parts) + if rel.is_absolute() or ".." in rel.parts: + msg = f"{rel} is not a path inside {dest}" + raise EmitRefusedError(msg) + path.parent.mkdir(parents=True, exist_ok=True) + if path.is_symlink() or not path.parent.resolve().is_relative_to(top): + msg = f"{path} would be written outside {dest}" + raise EmitRefusedError(msg) + write_generated(path, text) + written.append(path) + return written + + +def label(policy_id: str, vendor: str, agent: str, halves: list[str]) -> dict[str, Any]: + """One (policy, vendor) cell: the in-agent word capped by vendor-docs evidence, with chock's own witness.""" + level = capped(in_agent_level(agent), [BASIS]) + grade = Grade(level, BASIS, witnessed=evidence.witness(vendor, evidence.MANAGED) is not None) + return { + "policy": policy_id, + "vendor": vendor, + "surface": Surface.MANAGED_SETTING.value, + "halves": halves, + "level": grade.level, + "basis": grade.basis, + "witnessed": grade.witnessed, + "label": render_grade(grade), + } + + +def as_json(data: Any) -> str: + return json.dumps(data, indent=2) + "\n" diff --git a/src/chock/compile/emitters/managed/claude_code.py b/src/chock/compile/emitters/managed/claude_code.py new file mode 100644 index 00000000..faffa7bc --- /dev/null +++ b/src/chock/compile/emitters/managed/claude_code.py @@ -0,0 +1,113 @@ +"""Claude Code's managed drop-in: the plugin route's hooks, rooted at an absolute deploy folder, failing closed.""" + +from __future__ import annotations + +import json +from pathlib import Path, PurePosixPath +from typing import Any + +from agentseam import packaging + +from chock.compile.emitters import managed +from chock.hooks.stop_check import STOP_CHECK_FLAG +from chock.plugin.claude import claude_plugin_files +from chock.plugin.multi_plugin import SCRIPTS_DIR + +VENDOR = "claude_code" +AGENT = "claude" +BUNDLE = "bundle" +_HOOKS_REL = Path(packaging.supports(VENDOR, packaging.HOOKS)) +#: The plugin-root token the plugin route's commands carry, swapped for the deploy folder. +ROOT_TOKEN = str(packaging.executable_ref(VENDOR, "x")).removesuffix("/x") +_FACTS = managed.facts(VENDOR) +_FAIL = _FACTS["fail_block"] +#: The halves a plugin hook runs, by the flag its command carries. +_HALF_FLAGS = {"guard": " --guard ", "gate": " --gate "} + + +def _is_stop_check(group: dict[str, Any]) -> bool: + return all(hook["command"].endswith(f" {STOP_CHECK_FLAG}") for hook in group["hooks"]) + + +def _rooted(group: dict[str, Any], event: str, root: str) -> dict[str, Any]: + """`group` with its plugin root swapped for `root` and, where the docs say it acts, onFailure set.""" + hooks = [] + for hook in group["hooks"]: + command = hook["command"].replace(ROOT_TOKEN, root) + if "${" in command: + msg = f"{command!r} still names a client token after rooting it at {root}" + raise managed.EmitRefusedError(msg) + rooted = {**hook, "command": command} + if event in _FAIL["events"]: + rooted[_FAIL["key"]] = _FAIL["value"] + hooks.append(rooted) + return {**group, "hooks": hooks} + + +def _policy_parts(policy_dir: Path, manifest: dict[str, Any], repo_root: Path) -> tuple[dict[str, Any], dict] | None: + """The plugin route's hooks and scripts for one policy, or None when it runs nothing.""" + files = claude_plugin_files(policy_dir, manifest, repo_root, folded=False) + if _HOOKS_REL not in files: + return None + scripts = {PurePosixPath(rel.as_posix()): text for rel, text in files.items() if rel.parts[0] == SCRIPTS_DIR} + return json.loads(files[_HOOKS_REL])["hooks"], scripts + + +def build( + policies: list[tuple[str, Path, dict[str, Any]]], repo_root: Path, root: str, *, lock: bool = False +) -> tuple[managed.Files, list[dict[str, Any]], list[str]]: + """(files under `/claude_code`, one label per emitted policy, the policies with nothing to run).""" + events: dict[str, list[dict[str, Any]]] = {} + files: managed.Files = {} + labels, skipped = [], [] + stop_check_kept = False + for policy_id, policy_dir, manifest in policies: + parts = _policy_parts(policy_dir, manifest, repo_root) + if parts is None: + skipped.append(policy_id) + continue + hooks, scripts = parts + commands = [] + for event, groups in hooks.items(): + for group in groups: + if _is_stop_check(group): + if stop_check_kept: + continue + stop_check_kept = True + events.setdefault(event, []).append(_rooted(group, event, f"{root}/{policy_id}")) + commands += [hook["command"] for hook in group["hooks"]] + for rel, text in scripts.items(): + files[PurePosixPath(BUNDLE, policy_id, rel)] = text + halves = [half for half, flag in _HALF_FLAGS.items() if any(flag in command for command in commands)] + labels.append(managed.label(policy_id, VENDOR, AGENT, halves)) + document: dict[str, Any] = {"hooks": events} + if lock: + document[_FACTS["lock_key"]] = True + files[PurePosixPath(_FACTS["drop_in"])] = managed.as_json(document) + return files, labels, skipped + + +def deploy_plan(platform: str, root: str, *, lock: bool) -> dict[str, Any]: + """Where an admin places each part, what it needs, what it does not promise, and how to take it out.""" + admin = _FACTS["admin_dir"][platform] + place = [ + {"from": _FACTS["drop_in"], "to": f"{admin}/{_FACTS['drop_in']}"}, + {"from": f"{BUNDLE}/", "to": f"{root}/"}, + ] + plan: dict[str, Any] = { + "vendor": VENDOR, + "platform": platform, + "deployed": False, + "basis": managed.BASIS, + "place": place, + "undo": [f"delete {step['to']}" for step in place], + "requirements": _FACTS["requirements"], + "caveats": _FACTS["caveats"], + "never_emitted": _FACTS["never_emitted"], + "notes": [_FACTS["never_note"], *([_FACTS["lock_note"]] if lock else [])], + "sources": _FACTS["sources"], + "read": _FACTS["read"], + } + if lock: + plan["lock_key"] = _FACTS["lock_key"] + return plan diff --git a/src/chock/compile/emitters/managed/cli.py b/src/chock/compile/emitters/managed/cli.py new file mode 100644 index 00000000..c366c675 --- /dev/null +++ b/src/chock/compile/emitters/managed/cli.py @@ -0,0 +1,128 @@ +"""`chock emit --managed --out `: write a managed-settings bundle for an admin to deploy; deploy nothing.""" + +from __future__ import annotations + +import argparse +import sys +from pathlib import Path, PurePosixPath +from typing import Any + +from chock.compile.emitters import managed +from chock.compile.emitters.managed import claude_code +from chock.config import load_config, policy_status +from chock.manifest import load_manifest, warn_defaults +from chock.output import error, legible +from chock.policies import discover_policy_dirs +from chock.policy_id import InvalidPolicyIdError, validate_policy_id + +BUILDERS = {claude_code.VENDOR: claude_code} +DEPLOY_FILE = "deploy.json" +LABELS_FILE = "labels.json" + + +def _parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser( + prog="chock emit", + description=( + "Write one vendor's managed-settings file, the self-contained hook bundle it runs, deploy.json " + "(where an admin places each part, what it needs, how to undo it) and labels.json, all under --out. " + "It writes no system path, elevates nothing and deploys nothing. Every label is vendor-docs " + "evidence, unwitnessed, at most best-effort; a local administrator can still edit or remove the file." + ), + ) + parser.add_argument("--managed", required=True, choices=managed.VENDORS, help="the vendor to emit for") + parser.add_argument( + "--out", required=True, type=Path, help="a folder outside any system path; // must not exist" + ) + parser.add_argument( + "--repo", type=Path, default=Path(), help="the repository whose policies are emitted (default: .)" + ) + parser.add_argument( + "--policies", help="comma-separated policy ids (default: every enabled policy with a guard or gate)" + ) + parser.add_argument("--platform", choices=managed.PLATFORMS, help="the OS the file is for (default: this one)") + parser.add_argument( + "--deploy-root", + help="absolute folder the admin copies bundle/ to (default: the vendor's admin folder + /chock)", + ) + parser.add_argument( + "--lock", + action="store_true", + help="also emit the vendor's managed-hooks-only key, which stops every user, project and plugin hook on the machine", + ) + return parser + + +def _manifest(policy_dir: Path) -> dict[str, Any]: + warnings: list[str] = [] + result = load_manifest(policy_dir, warnings=warnings) + if result is None: + return {} + data, path = result + warn_defaults(path, warnings) + return data + + +def _selected(repo: Path, wanted: str | None) -> list[tuple[str, Path, dict[str, Any]]]: + """(id, folder, manifest) of each policy to emit: the named ones, else every one the config leaves enabled.""" + found = {path.name: path for path in discover_policy_dirs(repo)} + names = [name.strip() for name in wanted.split(",") if name.strip()] if wanted else sorted(found) + missing = [name for name in names if name not in found] + if missing: + msg = f"no policy {', '.join(missing)} under {repo / '.agents' / 'policies'}" + raise managed.EmitRefusedError(msg) + config = load_config(repo) + chosen = [] + for name in names: + manifest = _manifest(found[name]) + policy_id = str(manifest.get("id") or name) + try: + validate_policy_id(policy_id, name) + except InvalidPolicyIdError as exc: + raise managed.EmitRefusedError(str(exc)) from exc + if wanted or policy_status(config, policy_id, manifest)["state"] != "disabled": + chosen.append((policy_id, found[name], manifest)) + return chosen + + +def _report(dest: Path, plan: dict[str, Any], labels: list[dict[str, Any]], skipped: list[str]) -> str: + lines = [f"chock emit: wrote {dest} for {plan['vendor']} on {plan['platform']}; nothing was deployed."] + lines += [f" place {step['from']} at {step['to']}" for step in plan["place"]] + lines += [ + f" {row['policy']}: {row['label']}, {'witnessed' if row['witnessed'] else 'unwitnessed'}" for row in labels + ] + lines += [f" skipped {policy_id}: no shell guard or write gate to run" for policy_id in skipped] + lines += [f" {note}" for note in plan["notes"]] + lines.append(f" {DEPLOY_FILE} lists what each machine needs, what this does not promise, and the undo step.") + return "\n".join(lines) + "\n" + + +def emit(args: argparse.Namespace) -> str: + """Build in memory, check every refusal, then write the tree; returns the report.""" + builder = BUILDERS[args.managed] + facts = managed.facts(args.managed) + platform = args.platform or managed.host_platform() + root = managed.deploy_root(args.deploy_root or facts["deploy_root"][platform], platform) + dest = managed.vendor_out(args.out, args.managed) + repo = args.repo.resolve() + files, labels, skipped = builder.build(_selected(repo, args.policies), repo, root, lock=args.lock) + if not labels: + msg = "no selected policy has a shell guard or a write gate, so there is nothing for a managed hook to run" + raise managed.EmitRefusedError(msg) + plan = builder.deploy_plan(platform, root, lock=args.lock) + plan["deploy_root"] = root + files[PurePosixPath(DEPLOY_FILE)] = managed.as_json(plan) + files[PurePosixPath(LABELS_FILE)] = managed.as_json(labels) + managed.write_tree(dest, files) + return _report(dest, plan, labels, skipped) + + +def main(argv: list[str] | None = None) -> int: + args = _parser().parse_args(argv) + try: + report = emit(args) + except managed.EmitRefusedError as exc: + error(f"chock emit: {exc}") + return 1 + sys.stdout.write(legible(report)) + return 0 diff --git a/src/chock/data/commands.json b/src/chock/data/commands.json index 7779e2d0..ae49e44e 100644 --- a/src/chock/data/commands.json +++ b/src/chock/data/commands.json @@ -85,6 +85,10 @@ "help": "Low-level single-policy compile", "person_only": true }, + "emit": { + "module": "chock.compile.emitters.managed.cli", + "help": "Write a vendor's managed-settings bundle under --out for an admin to deploy (emit --managed claude_code --out ); writes no system path" + }, "install-skills": { "module": "chock.scaffold.skills", "help": "Install bundled authoring skills into agent skill dirs (write mode; check via CI)" diff --git a/src/chock/data/managed_channels.json b/src/chock/data/managed_channels.json new file mode 100644 index 00000000..c32b4c1e --- /dev/null +++ b/src/chock/data/managed_channels.json @@ -0,0 +1,46 @@ +{ + "system_roots": { + "linux": ["/etc", "/usr", "/bin", "/sbin", "/lib", "/boot", "/opt"], + "macos": ["/Library", "/System", "/etc", "/private/etc", "/usr", "/bin", "/sbin", "/opt"], + "windows": ["C:/Program Files", "C:/Program Files (x86)", "C:/ProgramData", "C:/Windows"] + }, + "vendors": { + "claude_code": { + "read": "2026-11-12", + "sources": [ + "https://code.claude.com/docs/en/managed-settings", + "https://code.claude.com/docs/en/hooks", + "https://code.claude.com/docs/en/settings-reference" + ], + "admin_dir": { + "linux": "/etc/claude-code", + "macos": "/Library/Application Support/ClaudeCode", + "windows": "C:/Program Files/ClaudeCode" + }, + "drop_in": "managed-settings.d/50-chock.json", + "deploy_root": { + "linux": "/etc/claude-code/chock", + "macos": "/Library/Application Support/ClaudeCode/chock", + "windows": "C:/Program Files/ClaudeCode/chock" + }, + "fail_block": {"key": "onFailure", "value": "block", "events": ["PreToolUse"], "min_client": "2.1.295"}, + "lock_key": "allowManagedHooksOnly", + "never_emitted": ["allowManagedPermissionRulesOnly"], + "requirements": [ + "Every machine needs git, a POSIX sh (git's) and Python 3.11 or later on PATH. Without them, onFailure \"block\" refuses every matched tool call on that machine.", + "onFailure needs Claude Code 2.1.295 or later; an older client lets a crashed or timed-out hook through.", + "The bundle must sit at deploy_root exactly: the hook commands name it by absolute path." + ], + "caveats": [ + "Managed settings bind Claude Code only. A local administrator can edit or delete these files.", + "On WSL, /etc/claude-code is writable by the user as well.", + "By default (managedSourcesBehavior \"first-wins\") a machine that also receives server-managed or MDM policy skips the managed settings files without a warning; /status names the source in use.", + "onFailure has no effect on Stop hooks, so the turn-end gate fails open when it crashes.", + "A file in managed-settings.d that is not valid JSON stops Claude Code from starting.", + "The labels are vendor-docs evidence, unwitnessed: nobody has watched this file refuse a call yet." + ], + "lock_note": "--lock adds allowManagedHooksOnly: every user, project and plugin hook on the machine stops running, chock's own project hooks and plugins included.", + "never_note": "allowManagedPermissionRulesOnly is never emitted: it would discard every user and project permission rule." + } + } +} diff --git a/src/chock/evidence.py b/src/chock/evidence.py index 96609a63..d9b20ae1 100644 --- a/src/chock/evidence.py +++ b/src/chock/evidence.py @@ -25,7 +25,9 @@ #: Not a place chock blocks: the environment a vendor's shell hands a git hook, which is how an #: agent's commit is recognised. A row here witnesses a marker, never a capability. GIT_HOOK_ENV = "git-hook-env" -WITNESS_SURFACES = ("pre-tool-use", "agent-hooks", PLUGIN_HOOK, GIT_HOOK_ENV) +#: A vendor's admin-deployed file (`chock emit --managed`); until a row lands, every managed label stays vendor-docs. +MANAGED = "managed" +WITNESS_SURFACES = ("pre-tool-use", "agent-hooks", PLUGIN_HOOK, GIT_HOOK_ENV, MANAGED) HONOURS_ASK = "honours_ask" CLAIM_NAMES = (HONOURS_ASK,) diff --git a/tests/fixtures/cli_help_golden.txt b/tests/fixtures/cli_help_golden.txt index b0080895..92e8ff1a 100644 --- a/tests/fixtures/cli_help_golden.txt +++ b/tests/fixtures/cli_help_golden.txt @@ -22,6 +22,7 @@ Everyday: Authoring: new Create a deterministic artifact skeleton compile Low-level single-policy compile + emit Write a vendor's managed-settings bundle under --out for an admin to deploy (emit --managed claude_code --out ); writes no system path install-skills Install bundled authoring skills into agent skill dirs (write mode; check via CI) registry Scan/list/resolve the artifact registry plugin Package policies as installable plugins (plugin build [--format claude] [--check]) diff --git a/tests/fixtures/emitter_stability/golden/stability-hook/managed-setting/managed-settings.json b/tests/fixtures/emitter_stability/golden/stability-hook/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/tests/fixtures/emitter_stability/golden/stability-hook/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/tests/fixtures/emitter_stability/golden/stability-rule/managed-setting/managed-settings.json b/tests/fixtures/emitter_stability/golden/stability-rule/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/tests/fixtures/emitter_stability/golden/stability-rule/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/tests/fixtures/emitter_stability/golden/stability-script/managed-setting/managed-settings.json b/tests/fixtures/emitter_stability/golden/stability-script/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/tests/fixtures/emitter_stability/golden/stability-script/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/tests/fixtures/managed_emit/block-no-verify.json b/tests/fixtures/managed_emit/block-no-verify.json new file mode 100644 index 00000000..fc1a5471 --- /dev/null +++ b/tests/fixtures/managed_emit/block-no-verify.json @@ -0,0 +1,27 @@ +{ + "hooks": { + "PreToolUse": [ + { + "matcher": "Bash|PowerShell", + "hooks": [ + { + "type": "command", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/block-no-verify/scripts/launch.sh\" \"/srv/chock-managed/block-no-verify/scripts/claude_code.py\" --guard \"/srv/chock-managed/block-no-verify/scripts/block-no-verify.py\"", + "timeout": 45, + "onFailure": "block" + } + ] + }, + { + "hooks": [ + { + "type": "command", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/block-no-verify/scripts/launch.sh\" \"/srv/chock-managed/block-no-verify/scripts/claude_code.py\" --stop-check", + "timeout": 45, + "onFailure": "block" + } + ] + } + ] + } +} diff --git a/tests/fixtures/managed_emit/scan-secrets.json b/tests/fixtures/managed_emit/scan-secrets.json new file mode 100644 index 00000000..f24bc2b9 --- /dev/null +++ b/tests/fixtures/managed_emit/scan-secrets.json @@ -0,0 +1,38 @@ +{ + "hooks": { + "PreToolUse": [ + { + "matcher": "Write|Edit|MultiEdit|NotebookEdit", + "hooks": [ + { + "type": "command", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/scan-secrets/scripts/launch.sh\" \"/srv/chock-managed/scan-secrets/scripts/claude_code.py\" --gate \"/srv/chock-managed/scan-secrets/scripts/gate.json\"", + "timeout": 45, + "onFailure": "block" + } + ] + }, + { + "hooks": [ + { + "type": "command", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/scan-secrets/scripts/launch.sh\" \"/srv/chock-managed/scan-secrets/scripts/claude_code.py\" --stop-check", + "timeout": 45, + "onFailure": "block" + } + ] + } + ], + "Stop": [ + { + "hooks": [ + { + "type": "command", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/scan-secrets/scripts/launch.sh\" \"/srv/chock-managed/scan-secrets/scripts/claude_code.py\" --gate \"/srv/chock-managed/scan-secrets/scripts/gate.json\" --stop", + "timeout": 45 + } + ] + } + ] + } +} diff --git a/tests/test_coverage_honesty_p0c.py b/tests/test_coverage_honesty_p0c.py index e798fdeb..a5bf5d47 100644 --- a/tests/test_coverage_honesty_p0c.py +++ b/tests/test_coverage_honesty_p0c.py @@ -41,32 +41,19 @@ def test_commit_gate_still_enforced_at_commit(tmp_path: Path) -> None: assert result.coverage["protect-main-branch"]["claude"]["level"] == "enforced-at-commit" -def test_managed_patterns_are_honest_and_valid(tmp_path: Path) -> None: - import re - - pmb = tmp_path / ".chock" / "compiled" - compile_policy( - baseline_policy("protect-main-branch"), - targets=[Surface.MANAGED_SETTING.value], - output_root=pmb, - agents=["claude"], - ) - pmb_managed = json.loads((pmb / "protect-main-branch" / "managed-setting" / "managed-settings.json").read_text()) - assert pmb_managed["deny"] == [], "branch protection cannot be a static managed deny; must be empty" - - ss = tmp_path / ".chock-ss" / "compiled" - compile_policy( - baseline_policy("scan-secrets"), - targets=[Surface.MANAGED_SETTING.value], - output_root=ss, - agents=["claude"], - ) - ss_managed = json.loads((ss / "scan-secrets" / "managed-setting" / "managed-settings.json").read_text()) - assert ss_managed["deny"], "scan-secrets must emit managed deny patterns" - for entry in ss_managed["deny"]: - pattern = entry["pattern"] - assert "\b" not in pattern, "must not carry a U+0008 backspace byte" - re.compile(pattern) +def test_compile_writes_no_managed_setting_artifact(tmp_path: Path) -> None: + """The old stub wrote a schema Claude Code does not read; managed files now come from `chock emit --managed` only.""" + from chock.compile.compiler import EMITTERS + + assert Surface.MANAGED_SETTING not in EMITTERS + for policy_id in ("protect-main-branch", "scan-secrets"): + out = tmp_path / policy_id / ".chock" / "compiled" + result = compile_policy( + baseline_policy(policy_id), targets=[Surface.MANAGED_SETTING.value], output_root=out, agents=["claude"] + ) + assert result.artifacts == {} + assert not (out / policy_id / Surface.MANAGED_SETTING.value).exists() + assert result.coverage[policy_id]["claude"]["level"] == "none" def test_auto_compile_uses_configured_agents(tmp_path: Path) -> None: diff --git a/tests/test_generated_newlines.py b/tests/test_generated_newlines.py index 853b4872..ad800341 100644 --- a/tests/test_generated_newlines.py +++ b/tests/test_generated_newlines.py @@ -15,7 +15,7 @@ GENERATED_WRITERS = [ "src/chock/compile/emitters/ambient.py", - "src/chock/compile/emitters/claude_managed.py", + "src/chock/compile/emitters/managed/__init__.py", "src/chock/compile/emitters/in_agent.py", "src/chock/compile/emitters/git_hook.py", "src/chock/index/cli.py", diff --git a/tests/test_managed_emit_claude.py b/tests/test_managed_emit_claude.py new file mode 100644 index 00000000..5815a459 --- /dev/null +++ b/tests/test_managed_emit_claude.py @@ -0,0 +1,232 @@ +"""`chock emit --managed claude_code`: a valid drop-in, a self-contained bundle, honest labels, and nothing outside --out.""" + +from __future__ import annotations + +import json +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest +from conftest import baseline_policy + +from chock import cli +from chock.compile.emitters import managed +from chock.compile.emitters.in_agent_hooks import TIMEOUT_SECONDS +from chock.compile.levels import cap_for, level_rank +from chock.doctor.payloads import payload + +FIXTURES = Path(__file__).resolve().parent / "fixtures" / "managed_emit" +POLICIES = ("block-no-verify", "scan-secrets") +DEPLOY_ROOT = "/srv/chock-managed" +DROP_IN = Path("managed-settings.d") / "50-chock.json" +PERMISSION_RULE = re.compile(r"^[A-Za-z]+\(.*\)$") +OVERRIDE_CLAIM = re.compile(r"(cannot|can't|can not|unable to) (be )?overrid", re.IGNORECASE) + + +@pytest.fixture +def repo(tmp_path: Path) -> Path: + """A repository holding the two golden policies and one advisory policy with nothing to run.""" + root = tmp_path / "repo" + for policy_id in (*POLICIES, "yagni"): + shutil.copytree(baseline_policy(policy_id), root / ".agents" / "policies" / policy_id) + return root + + +def _emit(repo: Path, out: Path, *extra: str) -> int: + return cli.main(["emit", "--managed", "claude_code", "--out", str(out), "--repo", str(repo), *extra]) + + +def _linux(repo: Path, out: Path, *extra: str) -> int: + return _emit(repo, out, "--platform", "linux", "--deploy-root", DEPLOY_ROOT, *extra) + + +def _drop_in(out: Path) -> dict: + return json.loads((out / "claude_code" / DROP_IN).read_text(encoding="utf-8")) + + +def _handlers(doc: dict) -> list[tuple[str, dict]]: + return [(event, hook) for event, groups in doc["hooks"].items() for group in groups for hook in group["hooks"]] + + +def _tree(root: Path, skip: Path) -> dict[str, bytes]: + return { + p.relative_to(root).as_posix(): p.read_bytes() + for p in root.rglob("*") + if p.is_file() and skip not in p.parents and "__pycache__" not in p.parts + } + + +def test_nothing_outside_out_changes(repo: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """Acceptance 1: HOME and cwd in tmp; every file there but --out is byte-identical after the run.""" + home = tmp_path / "home" + home.mkdir() + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("USERPROFILE", str(home)) + monkeypatch.chdir(tmp_path) + out = tmp_path / "out" + before = _tree(tmp_path, out) + assert _linux(repo, out) == 0 + assert _tree(tmp_path, out) == before + assert sorted(p.name for p in out.iterdir()) == ["claude_code"] + + +def _system_out() -> Path: + return Path(managed.CHANNELS["system_roots"][managed.host_platform()][0]) / "chock-emit-test" + + +def test_out_under_a_system_path_is_refused(repo: Path, capsys: pytest.CaptureFixture[str]) -> None: + """Acceptance 2: no file is written and the exit is non-zero.""" + out = _system_out() + assert _emit(repo, out) == 1 + assert "resolves inside the system path" in capsys.readouterr().err + assert not out.exists() + + +@pytest.mark.skipif(sys.platform == "win32", reason="a symlink needs a privilege on Windows") +def test_out_through_a_symlink_into_a_system_path_is_refused(repo: Path, tmp_path: Path) -> None: + link = tmp_path / "innocent" + link.symlink_to(_system_out().parent) + assert _emit(repo, link) == 1 + assert not (_system_out().parent / "claude_code").exists() + + +def test_an_existing_vendor_folder_is_never_written_into(repo: Path, tmp_path: Path) -> None: + out = tmp_path / "out" + (out / "claude_code").mkdir(parents=True) + (out / "claude_code" / "mine.txt").write_text("keep", encoding="utf-8") + assert _linux(repo, out) == 1 + assert [p.name for p in (out / "claude_code").iterdir()] == ["mine.txt"] + + +@pytest.mark.parametrize("root", ["relative/dir", '/has"quote', "/has$dollar", "/has`tick"]) +def test_a_deploy_root_a_shell_would_read_is_refused(repo: Path, tmp_path: Path, root: str) -> None: + assert _emit(repo, tmp_path / "out", "--platform", "linux", "--deploy-root", root) == 1 + assert not (tmp_path / "out" / "claude_code").exists() + + +def test_a_windows_deploy_root_is_written_with_forward_slashes() -> None: + assert ( + managed.deploy_root("C:\\Program Files\\ClaudeCode\\chock\\", "windows") == "C:/Program Files/ClaudeCode/chock" + ) + + +def test_the_drop_in_follows_the_documented_schema(repo: Path, tmp_path: Path) -> None: + """Acceptance 3: JSON object; PreToolUse handlers block on failure with chock's timeout; Stop takes no onFailure.""" + out = tmp_path / "out" + assert _linux(repo, out) == 0 + doc = _drop_in(out) + assert all(PERMISSION_RULE.match(rule) for rule in doc.get("permissions", {}).get("deny", [])) + handlers = _handlers(doc) + assert {event for event, _ in handlers} == {"PreToolUse", "Stop"} + for event, hook in handlers: + assert hook["type"] == "command" + assert hook["timeout"] == TIMEOUT_SECONDS == 45 + assert hook.get("onFailure") == ("block" if event == "PreToolUse" else None) + assert sum(" --stop-check" in hook["command"] for _, hook in handlers) == 1 + + +def test_no_managed_only_key_without_lock(repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: + """Acceptance 4: no allowManaged* key by default; --lock adds the hooks key alone, never the permission-rules one.""" + assert _linux(repo, tmp_path / "plain") == 0 + assert not [key for key in _drop_in(tmp_path / "plain") if key.startswith("allowManaged")] + assert _linux(repo, tmp_path / "locked", "--lock") == 0 + assert [key for key in _drop_in(tmp_path / "locked") if key.startswith("allowManaged")] == ["allowManagedHooksOnly"] + text = capsys.readouterr().out + assert "allowManagedPermissionRulesOnly is never emitted" in text + assert "chock's own project hooks and plugins included" in text + + +def test_hook_commands_name_only_the_deploy_root(repo: Path, tmp_path: Path) -> None: + """Acceptance 5: no repo-relative path, no project launcher prefix, every path under the deploy root.""" + out = tmp_path / "out" + assert _linux(repo, out) == 0 + for _, hook in _handlers(_drop_in(out)): + command = hook["command"] + assert ".chock/bin" not in command and "launch.sh ||" not in command + paths = re.findall(r'"([^"]+)"', command) + assert paths and all(path.startswith(f"{DEPLOY_ROOT}/") for path in paths) + for path in paths: + assert (out / "claude_code" / "bundle" / path.removeprefix(f"{DEPLOY_ROOT}/")).is_file(), path + + +@pytest.mark.parametrize("policy_id", POLICIES) +def test_drop_in_golden(repo: Path, tmp_path: Path, policy_id: str) -> None: + """Acceptance 6: the file for one policy, byte for byte.""" + out = tmp_path / "out" + assert _linux(repo, out, "--policies", policy_id) == 0 + actual = (out / "claude_code" / DROP_IN).read_text(encoding="utf-8") + golden = FIXTURES / f"{policy_id}.json" + if os.environ.get("CHOCK_REGEN_GOLDENS") == "1": + golden.parent.mkdir(parents=True, exist_ok=True) + golden.write_text(actual, encoding="utf-8", newline="\n") + assert actual == golden.read_text(encoding="utf-8") + + +def test_labels_are_vendor_docs_unwitnessed_and_capped(repo: Path, tmp_path: Path) -> None: + """Acceptance 7: every emitted cell rests on vendor docs, is unwitnessed, and is at most best-effort.""" + out = tmp_path / "out" + assert _linux(repo, out) == 0 + labels = json.loads((out / "claude_code" / "labels.json").read_text(encoding="utf-8")) + assert [row["policy"] for row in labels] == list(POLICIES) + for row in labels: + assert row["basis"] == "vendor-docs" and row["witnessed"] is False + assert level_rank(row["level"]) <= level_rank(cap_for("vendor-docs")) == level_rank("best-effort") + assert row["label"] == f"{row['level']} (vendor-docs)" + assert {row["policy"]: row["halves"] for row in labels} == {"block-no-verify": ["guard"], "scan-secrets": ["gate"]} + + +def test_nothing_written_or_printed_claims_users_cannot_override( + repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str] +) -> None: + out = tmp_path / "out" + assert _linux(repo, out, "--lock") == 0 + texts = [capsys.readouterr().out, *(p.read_text(encoding="utf-8") for p in (out / "claude_code").glob("*.json"))] + texts.append((out / "claude_code" / DROP_IN).read_text(encoding="utf-8")) + texts.append(json.dumps(managed.CHANNELS)) + assert not [text for text in texts if OVERRIDE_CLAIM.search(text)] + plan = json.loads((out / "claude_code" / "deploy.json").read_text(encoding="utf-8")) + assert plan["deployed"] is False and plan["basis"] == "vendor-docs" + assert any("local administrator can edit or delete" in caveat for caveat in plan["caveats"]) + assert plan["place"][0]["to"] == "/etc/claude-code/managed-settings.d/50-chock.json" + assert plan["undo"] == [f"delete {step['to']}" for step in plan["place"]] + + +def test_a_selection_with_nothing_to_run_writes_nothing(repo: Path, tmp_path: Path) -> None: + assert _linux(repo, tmp_path / "out", "--policies", "yagni") == 1 + assert not (tmp_path / "out").exists() + + +@pytest.mark.skipif( + not (shutil.which("git") and shutil.which("sh")), reason="the hook needs git and sh, as on a client" +) +def test_the_bundle_runs_on_its_own(repo: Path, tmp_path: Path) -> None: + """The emitted command, rooted at the bundle itself, refuses the canary and allows a harmless call.""" + out = tmp_path / "out" + bundle = (out / "claude_code" / "bundle").as_posix() + assert _emit(repo, out, "--policies", "block-no-verify", "--deploy-root", bundle) == 0 + [(_, hook), *_] = _handlers(_drop_in(out)) + env = {k: v for k, v in os.environ.items() if k not in ("CLAUDECODE", "AI_AGENT", "CHOCK_AGENT_COMMIT")} + (tmp_path / "home").mkdir() + env["HOME"] = env["USERPROFILE"] = str(tmp_path / "home") + + def answer(command: str) -> str: + done = subprocess.run( + hook["command"], + shell=True, + input=json.dumps(payload("claude_code", command, tmp_path.as_posix())), + capture_output=True, + text=True, + cwd=tmp_path, + env=env, + check=False, + timeout=60, + ) + assert done.returncode == 0, done.stderr + return done.stdout + + assert '"permissionDecision": "deny"' in answer("git commit --no-verify -m x") + assert "deny" not in answer("git status") diff --git a/tests/test_witness_ledger.py b/tests/test_witness_ledger.py index 3cf50e07..8e4c9cb7 100644 --- a/tests/test_witness_ledger.py +++ b/tests/test_witness_ledger.py @@ -114,7 +114,7 @@ def test_the_in_agent_surfaces_the_ledger_accepts_are_the_compilers_own() -> Non """A surface renamed in the compiler must not leave the ledger accepting a dead token.""" in_agent = {Surface.PRE_TOOL_USE.value, Surface.AGENT_HOOKS.value} assert in_agent < set(evidence.WITNESS_SURFACES) - assert set(evidence.WITNESS_SURFACES) - in_agent == {evidence.PLUGIN_HOOK, evidence.GIT_HOOK_ENV} + assert set(evidence.WITNESS_SURFACES) - in_agent == {evidence.PLUGIN_HOOK, evidence.GIT_HOOK_ENV, evidence.MANAGED} @pytest.mark.parametrize("agent", sorted(RENDERS)) From f40081c4edbbcd696ed556b0c31969dbd2206f1d Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:42:58 +0000 Subject: [PATCH 15/57] fix(floor): close the review's bypasses in chock check --floor (W2-A1r) Rework after the adversarial review (rulings 1-10): - history pass fails closed: a truncated range or a blob it cannot read is undecided, naming commit and path; binary blobs are judged (non-utf-8 read as latin-1); content gates load from both ci-gate and git-hook - own-folder exemption only for a pack whose chock.lock entry changed, listed under floor/pack-changed with its paths - hook identity is (event, matcher, type, command); only the index pin is normalised; Claude Code's disableAllHooks and a committed .claude/settings.local.json carrying hooks are refused; other vendors are printed as not covered - every .chock/bin file and the floor's workflow must equal the merge-base or this engine's render (indexes re-rendered from the head's policy data); anything else is refused by name - at the floor a waiver counts only on a line the PR leaves unchanged - a script gate that crashes or cannot run is undecided whatever the rollout - a changed compiled gate needs a lock change for its pack - every tamper rule compares the merge-base with the head - diffs read with --text; a gate naming a path outside the repository is undecided; a base with no gates says so - the 36 review attacks are regression tests asserting the owed exit code Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2a1-check-floor.md | 23 ++- src/chock/data/tamper.json | 39 ++-- src/chock/history/gates.py | 24 ++- src/chock/history/scan.py | 37 +++- src/chock/validation/checks_floor.py | 63 ++++-- src/chock/validation/floor_context.py | 28 ++- src/chock/validation/floor_git.py | 32 +-- src/chock/validation/floor_history.py | 54 ++++- src/chock/validation/tamper.py | 267 +++++++----------------- src/chock/validation/tamper_files.py | 162 +++++++++++++++ src/chock/validation/tamper_hooks.py | 131 ++++++++++++ tests/floor_support.py | 8 +- tests/test_floor_attacks.py | 284 ++++++++++++++++++++++++++ tests/test_floor_attacks2.py | 284 ++++++++++++++++++++++++++ tests/test_floor_tamper.py | 121 ++++++++--- 15 files changed, 1269 insertions(+), 288 deletions(-) create mode 100644 src/chock/validation/tamper_files.py create mode 100644 src/chock/validation/tamper_hooks.py create mode 100644 tests/test_floor_attacks.py create mode 100644 tests/test_floor_attacks2.py diff --git a/changelog.d/w2a1-check-floor.md b/changelog.d/w2a1-check-floor.md index 4aa86b2e..7dc5b329 100644 --- a/changelog.d/w2a1-check-floor.md +++ b/changelog.d/w2a1-check-floor.md @@ -1,10 +1,19 @@ - `chock check --floor --base [--head ] [--head-ref ]` (preview): judges a pull request's `base...head` with the base's compiled ci gates, run by the installed engine, never `.chock/bin/gate.py` or any other head file. A head gate that is new or differs also runs when it is declarative; a head script gate is - reported "not judged at floor". A waiver counts only when the base already has the line (event `ci-floor`); `ask` - refuses; script gates receive deleted paths under `deleted` and an environment of `PATH`, `HOME` (and `SYSTEMROOT`) - only (script payload contract version 2, `spec/gate-dsl.md`). Built-in tamper rules from `src/chock/data/tamper.json` - refuse by name a removed hook entry, a removed or weakened compiled gate, a pack dropped from `chock.lock`, a - deleted `.chock/bin` file and a removed job, step or trigger of the guarded workflow; regeneration is silent. A - per-commit pass runs the base's content gates over `merge-base..head`. Exit 0 clean, 1 refused, 2 undecided. It is - a floor only where the repository makes it a required check, which chock does not verify. + reported "not judged at floor". A waiver counts only on a line the pull request leaves unchanged (event + `ci-floor`); `ask` refuses; a gate that crashes, times out or cannot be read is undecided whatever the rollout. + Script gates receive deleted paths under `deleted` and an environment of `PATH`, `HOME` (and `SYSTEMROOT`) only + (script payload contract version 2, `spec/gate-dsl.md`). Diffs are read with `--text`. +- Built-in tamper rules (`src/chock/data/tamper.json`) compare the merge-base with the head and refuse by name: a + removed or retyped hook entry; a hook switch-off key turned on, or a committed higher-precedence settings file + carrying hooks (Claude Code; other vendors are printed as not covered); a deleted compiled gate, or a changed one + with no `chock.lock` change for its pack; a pack dropped from `chock.lock`; any `.chock/bin` file or the floor's + own workflow that is neither the merge-base's bytes nor what this engine renders. A pack whose lock entry changed + is listed under `floor/pack-changed` with every file of it the pull request changed, and only then is its own + source folder exempt from its own gates. +- A per-commit pass runs the base's content gates (both surfaces) over `merge-base..head`, judging binary blobs too + (non-utf-8 bytes read as latin-1); a range past the commit limit or a blob it cannot read is undecided, naming the + commit and path. Exit 0 clean, 1 refused, 2 undecided. It is a floor only where the repository makes it a required + check, which chock does not verify. +- `chock history`'s `load_rules` takes the compiled surfaces to read; its own defaults are unchanged. diff --git a/src/chock/data/tamper.json b/src/chock/data/tamper.json index bfe053a8..a7150d81 100644 --- a/src/chock/data/tamper.json +++ b/src/chock/data/tamper.json @@ -1,26 +1,39 @@ { - "$comment": "Facts `chock check --floor` compares base against head: each rule refuses only a removal or weakening. Ships inside the engine, so a pull request cannot edit what judges it.", + "$comment": "Facts `chock check --floor` compares merge-base against head. Ships inside the engine, so a pull request cannot edit what judges it.", "hook_files": [ - {"glob": ".claude/settings.json", "pointer": "/hooks"}, - {"glob": ".cursor/hooks.json", "pointer": "/hooks"}, - {"glob": ".github/hooks/*.json", "pointer": "/hooks"}, - {"glob": ".codex/hooks.json", "pointer": "/hooks"}, - {"glob": ".gemini/settings.json", "pointer": "/hooks"}, - {"glob": ".windsurf/hooks.json", "pointer": "/hooks"}, - {"glob": ".devin/hooks.json", "pointer": "/hooks"}, - {"glob": ".devin/hooks.v1.json", "pointer": ""}, - {"glob": ".grok/hooks/*.json", "pointer": "/hooks"} + {"glob": ".claude/settings.json", "pointer": "/hooks", "vendor": "claude_code"}, + {"glob": ".cursor/hooks.json", "pointer": "/hooks", "vendor": "cursor"}, + {"glob": ".github/hooks/*.json", "pointer": "/hooks", "vendor": "vscode_copilot"}, + {"glob": ".codex/hooks.json", "pointer": "/hooks", "vendor": "codex_cli"}, + {"glob": ".gemini/settings.json", "pointer": "/hooks", "vendor": "gemini_cli"}, + {"glob": ".windsurf/hooks.json", "pointer": "/hooks", "vendor": "windsurf"}, + {"glob": ".devin/hooks.json", "pointer": "/hooks", "vendor": "devin"}, + {"glob": ".devin/hooks.v1.json", "pointer": "", "vendor": "devin"}, + {"glob": ".grok/hooks/*.json", "pointer": "/hooks", "vendor": "grok"} ], + "$comment_switches": "Per vendor: keys that switch every hook off (a JSON pointer into its hook file, refused when truthy at the head and not at the merge-base) and the higher-precedence files a repository can commit (refused when added or changed carrying hooks or a disable key). A vendor with no entry is printed as not covered.", + "hook_switches": { + "claude_code": { + "disable_keys": ["/disableAllHooks"], + "override_files": [".claude/settings.local.json"] + } + }, "hook_nested_key": "hooks", "hook_matcher_key": "matcher", "hook_matcher_separator": "|", - "hook_command_keys": ["command", "commandWindows", "bash", "powershell"], - "volatile_pattern": "[0-9a-f]{7,}|\\d+(?:\\.\\d+)+", - "volatile_placeholder": "", + "hook_type_key": "type", + "hook_type_default": "command", + "hook_command_keys": ["command", "commandWindows", "bash", "powershell", "prompt"], + "$comment_volatile": "Only the index pin a regeneration rewrites; the index file it pins must itself equal the merge-base or this engine's render.", + "volatile_pattern": "(?<=--index-sha256 )[0-9a-f]{64}", + "volatile_placeholder": "", "compiled_prefix": ".chock/compiled/", + "policies_prefix": ".agents/policies/", "gate_surfaces": ["ci-gate", "git-hook"], "gate_file": "gate.json", "runtime_prefix": ".chock/bin/", + "index_prefix": ".chock/bin/index/", + "launcher": ".chock/bin/launch.sh", "lock_file": "chock.lock", "workflow_default": ".github/workflows/chock.yml", "workflow_config_key": "floor_workflow" diff --git a/src/chock/history/gates.py b/src/chock/history/gates.py index e39b83fd..be73efad 100644 --- a/src/chock/history/gates.py +++ b/src/chock/history/gates.py @@ -65,10 +65,22 @@ def _rule(repo: Path, gate_path: Path, spec: dict) -> Rule: ) -def load_rules(repo: Path) -> tuple[list[Rule], list[str]]: - """(applicable rules, ids of installed commit gates this scan cannot apply), both in policy order.""" +def _same(one: Rule, other: Rule) -> bool: + shape = ("policy", "content_pattern", "path_pattern", "pragma_pattern") + same = all(getattr(one, key) == getattr(other, key) for key in shape) + return same and (one.judge.scope, one.judge.own) == (other.judge.scope, other.judge.own) + + +def load_rules(repo: Path, surfaces: tuple[str, ...] = (GATE_FILE[0],)) -> tuple[list[Rule], list[str]]: + """(applicable rules, ids of installed commit gates this scan cannot apply), both in policy order. + + `surfaces` are the compiled surfaces read (the git-hook one by default); a rule two surfaces share is kept once. + """ root = repo.joinpath(*COMPILED) - gates = sorted(root.glob("*/" + "/".join(GATE_FILE))) if root.is_dir() else [] + found = ( + {path for surface in surfaces for path in root.glob(f"*/{surface}/{GATE_FILE[1]}")} if root.is_dir() else set() + ) + gates = sorted(found) rules: list[Rule] = [] skipped: list[str] = [] for gate_path in gates: @@ -82,8 +94,10 @@ def load_rules(repo: Path) -> tuple[list[Rule], list[str]]: if EVENT not in spec.get("on", []): continue if spec.get("kind") == KIND: - rules.append(_rule(repo, gate_path, spec)) - else: + rule = _rule(repo, gate_path, spec) + if not any(_same(rule, kept) for kept in rules): + rules.append(rule) + elif name not in skipped: skipped.append(gate_path.parents[1].name) if not rules: raise HistoryError("no installed content gates found under .chock/compiled; run `chock sync` first") diff --git a/src/chock/history/scan.py b/src/chock/history/scan.py index 36cf00a7..b52e4752 100644 --- a/src/chock/history/scan.py +++ b/src/chock/history/scan.py @@ -46,6 +46,9 @@ class Report: skipped_submodules: int = 0 truncated: bool = False shallow: bool = False + #: (commit, path, why) of every introduced blob this scan did not judge; filled when Limits.judge_binary is set. + unread: list[tuple[str, str, str]] = field(default_factory=list) + _unread_oids: dict[str, str] = field(default_factory=dict) @dataclass(frozen=True) @@ -54,6 +57,9 @@ class Limits: max_blob_bytes: int = DEFAULT_MAX_BLOB_BYTES allow_shallow: bool = False time_budget: int = DEFAULT_TIME_BUDGET + #: Judge binary blobs too, decoding bytes that are not utf-8 as latin-1 (lossless); a blob still not judged + #: (over max_blob_bytes) is named in Report.unread. + judge_binary: bool = False def _say(message: str) -> None: @@ -85,9 +91,21 @@ def _judged(judge: dict, policy: str, jobs: list[tuple[str, list[str]]]) -> list return [set(found) for found in hits] -def _judge_blob(rules: list[Rule], data: bytes, judge: dict) -> tuple[list[tuple[int, int, str]], set[int]]: +def _decoded(data: bytes, *, lossless: bool) -> str: + """The blob as text: utf-8 with replacement, or, when `lossless` and it is not utf-8, latin-1 byte for byte.""" + if lossless: + try: + return data.decode("utf-8") + except UnicodeDecodeError: + return data.decode("latin-1") + return data.decode("utf-8", "replace") + + +def _judge_blob( + rules: list[Rule], data: bytes, judge: dict, *, lossless: bool = False +) -> tuple[list[tuple[int, int, str]], set[int]]: """(rule index, line number, line digest) per hit, and the rules whose waiver pragma the blob carries.""" - text = data.decode("utf-8", "replace") + text = _decoded(data, lossless=lossless) lines = text.splitlines() hits: list[tuple[int, int, str]] = [] waived: set[int] = set() @@ -109,6 +127,14 @@ def _read_judged(repo: Path, rules: list[Rule], oids: list[str], limits: Limits, if missing: raise gitlog.HistoryError(f"{len(missing)} blob(s) missing from the object database, e.g. {missing[0]}") report.skipped_oversize = sum(1 for o in oids if sizes[o] > limits.max_blob_bytes) + if limits.judge_binary: + report._unread_oids.update( + { + o: f"{sizes[o]} bytes, over the {limits.max_blob_bytes}-byte limit" + for o in oids + if sizes[o] > limits.max_blob_bytes + } + ) wanted = [o for o in oids if 0 < sizes[o] <= limits.max_blob_bytes] deadline = time.monotonic() + limits.time_budget judged: Judged = {} @@ -119,10 +145,10 @@ def _read_judged(repo: Path, rules: list[Rule], oids: list[str], limits: Limits, raise gitlog.HistoryError( f"time budget of {limits.time_budget}s exceeded; narrow with --since or --max-commits" ) - if gitlog.is_binary(data): + if gitlog.is_binary(data) and not limits.judge_binary: report.skipped_binary += 1 else: - judged[oid] = _judge_blob(rules, data, judge) + judged[oid] = _judge_blob(rules, data, judge, lossless=limits.judge_binary) report.blobs_scanned += 1 if done % _PROGRESS_EVERY == 0: _say(f"history: read {done}/{len(wanted)} blobs") @@ -187,6 +213,9 @@ def scan(repo: Path, rules: list[Rule], since: str | None, limits: Limits, head: _say(f"history: {len(log.touches)} changed paths, {len(oids)} distinct blobs") judged = _read_judged(repo, rules, oids, limits, report) report.findings = _collect(log.touches, rules, judged) + report.unread = [ + (t.commit, t.path, report._unread_oids[t.oid]) for t in log.touches if t.oid in report._unread_oids + ] _say( f"history: skipped {report.skipped_binary} binary, {report.skipped_oversize} oversize blobs " f"and {report.skipped_submodules} submodule pointers (not scanned)" diff --git a/src/chock/validation/checks_floor.py b/src/chock/validation/checks_floor.py index 820f1005..42e91a49 100644 --- a/src/chock/validation/checks_floor.py +++ b/src/chock/validation/checks_floor.py @@ -28,15 +28,19 @@ from chock.gate.runner.context import ListingError from chock.gate.runner.kinds import PatternDeadlineError from chock.gate.runner.report import _reason +from chock.gate.runner.script import _UNDECIDED from chock.validation.floor_context import FloorContext from chock.validation.floor_git import FloorError, Revision, merge_base, resolve from chock.validation.floor_history import history_findings -from chock.validation.tamper import tamper +from chock.validation.tamper import Revs, pack_notes, packs_changed, tamper +from chock.validation.tamper_hooks import not_covered EXIT_REFUSED, EXIT_UNDECIDED = 1, 2 _CI_GATE = "ci-gate/gate.json" _SCRIPT, _DEPENDENCY, _REF = "script", _DEPENDENCY_KIND, "forbidden_ref" _REFUSING = (ACTION_BLOCK, ACTION_ASK) +_NO_GATES = "floor: the base has no compiled gates; no gate judged this pull request (tamper rules still ran)" +_NOT_COVERED = "floor/hook-switches not covered: {} -- the keys that switch their hooks off are not verified here" _NOT_REQUIRED = ( "floor: a floor only where this check is a required status check; that is the repository's own " "setting, unverified by chock. It judges committed content only." @@ -103,6 +107,14 @@ def policy_set(base: Revision, head: Revision) -> tuple[list[Gate], list[str]]: return run, notes +def _relative(named: str) -> str: + """`named` when it is a path inside the repository; FloorError (undecided) for an absolute or escaping one.""" + if not named or Path(named).is_absolute() or ".." in Path(named).parts or "\\" in named or ":" in named: + msg = f"gate names {named!r}, which is not a path inside the repository" + raise FloorError(msg) + return named + + def _params(gate: Gate, tree: Path) -> dict: """The gate's params, with every file it names taken from the gate's own revision, materialised under `tree`.""" params = dict(gate.spec.get("params") or {}) @@ -111,23 +123,26 @@ def _params(gate: Gate, tree: Path) -> dict: folder = gate.path.rsplit("/", 1)[0] script = f"{folder}/{params.get('script', '')}" else: - script = str(params.get("script", "")) + script = _relative(str(params.get("script", ""))) folder = script.rsplit("/", 1)[0] if "/" in script else "" gate.rev.materialise(f"{folder}/" if folder else script, tree) params["script"] = str(tree / script) if gate.spec.get("kind") == _DEPENDENCY and params.get("allowlist_file"): - named = str(params["allowlist_file"]) + named = _relative(str(params["allowlist_file"])) gate.rev.materialise(named, tree) params["allowlist_file"] = str(tree / named) return params -def _own(gate: Gate) -> tuple[str, ...]: - """Only the policy's source folder: its compiled gate is judged like any other file at the floor.""" - return (f"{POLICIES_PREFIX}{gate.policy}/",) +def _own(gate: Gate, packs: frozenset[str]) -> tuple[str, ...]: + """The policy's source folder, and only for a pack whose lock entry the pull request changed (listed in a + `floor/pack-changed` note); otherwise a gate judges its own folders like any other file.""" + return (f"{POLICIES_PREFIX}{gate.policy}/",) if gate.policy in packs else () -def _judge(gate: Gate, repo: Path, revs: tuple[str, str, str], head_ref: str | None, tree: Path) -> GateResult | str: +def _judge( # noqa: PLR0913, PLR0917 -- the gate, where it runs, what changed + gate: Gate, repo: Path, revs: tuple[str, str, str], head_ref: str | None, tree: Path, packs: frozenset[str] +) -> GateResult | str: """The gate's result, or why it could not judge.""" spec, label = gate.spec, f"{gate.policy} [{gate.source}]" kind = KINDS.get(spec.get("kind")) @@ -137,12 +152,15 @@ def _judge(gate: Gate, repo: Path, revs: tuple[str, str, str], head_ref: str | N return f"{label}: a branch gate needs --head-ref " base, head, fork = revs ctx = FloorContext( - repo, base=base, head=head, merge_base=fork, head_ref=head_ref, scope=spec.get("paths"), own=_own(gate) + repo, base=base, head=head, merge_base=fork, head_ref=head_ref, scope=spec.get("paths"), own=_own(gate, packs) ) try: - return kind(ctx, _params(gate, tree / gate.source), FLOOR_EVENT) + result = kind(ctx, _params(gate, tree / gate.source), FLOOR_EVENT) except (ListingError, PatternDeadlineError, FloorError) as exc: return f"{label}: {inert(str(exc))}" + if not result.allowed and (result.message or "").endswith(_UNDECIDED): + return f"{label}: {inert(result.message or '')}" # could not judge: undecided whatever the rollout + return result def _verdict(result: GateResult, declared: str, level: str) -> str: @@ -151,8 +169,13 @@ def _verdict(result: GateResult, declared: str, level: str) -> str: return min(result.verdict or declared, declared, _ROLLOUT_CEILING[level], key=_ACTION_RANK.__getitem__) -def judge_gates(repo: Path, base: Revision, head: Revision, fork: str, head_ref: str | None) -> Outcome: - """Run the policy set over base...head; `ask` refuses, since nobody can be asked in CI.""" +def judge_gates( # noqa: PLR0913, PLR0917 -- the range, the branch and the changed packs + repo: Path, base: Revision, head: Revision, fork: str, head_ref: str | None, packs: frozenset[str] = frozenset() +) -> Outcome: + """Run the policy set over base...head; `ask` refuses, since nobody can be asked in CI. + + A gate that cannot judge is undecided before the rollout applies: `observe` softens refusals, never that. + """ gates, notes = policy_set(base, head) outcome = Outcome([], [], notes) level = committed_rollout(repo, base.sha) @@ -160,7 +183,7 @@ def judge_gates(repo: Path, base: Revision, head: Revision, fork: str, head_ref: for gate in gates: if "commit" not in (gate.spec.get("on") or []) and gate.spec: continue - result = _judge(gate, repo, (base.sha, head.sha, fork), head_ref, Path(scratch)) + result = _judge(gate, repo, (base.sha, head.sha, fork), head_ref, Path(scratch), packs) if isinstance(result, str): outcome.undecided.append(result) continue @@ -178,10 +201,20 @@ def floor(repo: Path, base_ref: str, head_ref_rev: str, branch: str | None) -> i base_sha, head_sha = resolve(repo, base_ref, "base"), resolve(repo, head_ref_rev, "head") fork = merge_base(repo, base_sha, head_sha) base, head = Revision(repo, base_sha), Revision(repo, head_sha) + revs = Revs(base, Revision(repo, fork), head) _out(f"== floor: base {base_sha[:12]}, head {head_sha[:12]}, merge-base {fork[:12]}") - outcome = judge_gates(repo, base, head, fork, branch) - tampered = [t.render() for t in tamper(base, head)] - history, history_notes = history_findings(repo, base, head, fork) + packs = packs_changed(revs) + outcome = judge_gates(repo, base, head, fork, branch, packs) + if not base.paths(COMPILED_PREFIX): + outcome.notes.append(_NO_GATES) + tampered = [t.render() for t in tamper(revs, packs)] + try: + history, history_notes = history_findings(repo, base, head, fork, packs) + except FloorError as exc: + history, history_notes = [], [] + outcome.undecided.append(inert(str(exc))) + uncovered = not_covered(revs) + history_notes += pack_notes(revs, packs) + ([_NOT_COVERED.format(", ".join(uncovered))] if uncovered else []) for line in outcome.notes + history_notes: _out(f"note: {line}") refusals = outcome.refusals + tampered + history diff --git a/src/chock/validation/floor_context.py b/src/chock/validation/floor_context.py index e539ef0a..c35e3e9e 100644 --- a/src/chock/validation/floor_context.py +++ b/src/chock/validation/floor_context.py @@ -2,16 +2,22 @@ from __future__ import annotations +import subprocess from collections.abc import Sequence from pathlib import Path from chock.gate.runner import GateContext +from chock.gate.runner.context import _GIT +from chock.gate.runner.gitparse import DiffError, _diff_sides class FloorContext(GateContext): """`base...head` as the change; the head's files as the proposal; the base's as what a waiver must already be in. Every read is a `git show :`, so a head file is data and never reaches a program as code. + Diffs run with `--text`, so neither a NUL byte nor a head `.gitattributes` (`-diff`) hides added lines. + A waiver counts only on a line the pull request leaves unchanged: no line it adds can carry one, even a + copy of a line the base already has. """ def __init__( # noqa: PLR0913 -- the range's three revisions beside GateContext's own @@ -32,6 +38,21 @@ def __init__( # noqa: PLR0913 -- the range's three revisions beside GateContext def _range(self) -> list[str]: return [f"{self.base}...{self.head}"] + def _diff_text(self, path: str) -> str: + args = ("diff", *self._range(), "--text", "-U0", "--no-color", "--no-ext-diff", "--no-textconv", "--", path) + try: + proc = subprocess.run( # noqa: S603 -- reading committed revisions is this class's whole job + [_GIT, "--literal-pathspecs", *args], cwd=str(self.repo_root), capture_output=True, check=False + ) + except OSError as exc: + msg = f"git could not run ({exc})" + raise DiffError(msg) from exc + if proc.returncode: + first = (proc.stderr.decode("utf-8", "replace").strip().splitlines() or [""])[0] + msg = f"git diff failed (exit {proc.returncode}): {first}" + raise DiffError(msg) + return proc.stdout.decode("utf-8", "replace") + def staged_blob(self, path: str) -> str: """The proposal: the file at the head.""" return self._git("show", f"{self.head}:{path}") @@ -41,8 +62,11 @@ def head_blob(self, path: str) -> str: return self._git("show", f"{self.merge_base}:{path}") def committed_blob(self, path: str) -> str: - """Where a waiver must already be for the floor to honour it: the file at the base.""" - return self._git("show", f"{self.base}:{path}") + """Where a waiver must already be for the floor to honour it: the base's lines, less every line this + pull request adds to `path` (an added line is changed by definition, so its waiver never counts).""" + added = set(_diff_sides(self._diff_text(path))[0]) + lines = self._git("show", f"{self.base}:{path}").splitlines() + return "\n".join(line for line in lines if line not in added) def current_branch(self) -> str: """The branch under test is the one named; the checkout's own branch is the base's, never the head's.""" diff --git a/src/chock/validation/floor_git.py b/src/chock/validation/floor_git.py index bb871ed7..37833a38 100644 --- a/src/chock/validation/floor_git.py +++ b/src/chock/validation/floor_git.py @@ -72,18 +72,26 @@ def paths(self, prefix: str = "") -> list[str]: def has(self, path: str) -> bool: return path in self.entries() + def oid(self, path: str) -> str | None: + entry = self.entries().get(path) + return entry[1] if entry else None + + def blob(self, path: str) -> bytes | None: + """The file's bytes as committed; None when the tree has no such file.""" + entry = self.entries().get(path) + if entry is None: + return None + proc = git(self.repo, "cat-file", "blob", entry[1]) + if proc.returncode: + msg = f"git cat-file {path} at {self.sha[:12]} failed" + raise FloorError(msg) + return proc.stdout + def text(self, path: str) -> str | None: """The file as committed, decoded as utf-8 with replacement; None when the tree has no such file.""" if path not in self._texts: - entry = self.entries().get(path) - if entry is None: - self._texts[path] = None - else: - proc = git(self.repo, "cat-file", "blob", entry[1]) - if proc.returncode: - msg = f"git cat-file {path} at {self.sha[:12]} failed" - raise FloorError(msg) - self._texts[path] = proc.stdout.decode("utf-8", "replace") + data = self.blob(path) + self._texts[path] = None if data is None else data.decode("utf-8", "replace") return self._texts[path] def materialise(self, prefix: str, into: Path) -> None: @@ -94,11 +102,7 @@ def materialise(self, prefix: str, into: Path) -> None: continue target = into / path target.parent.mkdir(parents=True, exist_ok=True) - proc = git(self.repo, "cat-file", "blob", self.entries()[path][1]) - if proc.returncode: - msg = f"git cat-file {path} at {self.sha[:12]} failed" - raise FloorError(msg) - target.write_bytes(proc.stdout) + target.write_bytes(self.blob(path) or b"") def merge_base(repo: Path, base: str, head: str) -> str: diff --git a/src/chock/validation/floor_history.py b/src/chock/validation/floor_history.py index 4a3383b7..4db7fc2a 100644 --- a/src/chock/validation/floor_history.py +++ b/src/chock/validation/floor_history.py @@ -3,6 +3,9 @@ The cumulative diff misses a secret added in one commit and removed in the next; this pass does not. A line the base already has is not the pull request's, and a waiver the pull request adds is not honoured, so the gates run with no waiver pattern and lines present at the base are dropped. + +It fails closed: a range longer than the commit limit, or a blob it does not judge, is undecided, +never a silent skip. Binary blobs are judged too (bytes that are not utf-8 are read as latin-1). """ from __future__ import annotations @@ -12,13 +15,19 @@ from pathlib import Path from chock.display import inert +from chock.gate.runner import GateContext from chock.history import gitlog from chock.history.gates import RULE_CONTENT, load_rules from chock.history.scan import Finding, Limits, scan from chock.validation.floor_git import FloorError, Revision _COMPILED = ".chock/compiled/" +_POLICIES = ".agents/policies/" +_SURFACES = ("ci-gate", "git-hook") _SHORT = 12 +#: The largest blob the floor reads into memory to judge; a larger one is undecided, named by commit and path. +FLOOR_MAX_BLOB_BYTES = 64 * 1024 * 1024 +NO_CONTENT_GATES = "history: the base has no content gates on either surface; per-commit pass not run" def _introduced(finding: Finding, head: Revision, at_base: tuple[Revision, ...]) -> bool: @@ -31,24 +40,55 @@ def _introduced(finding: Finding, head: Revision, at_base: tuple[Revision, ...]) return line is None or not any(line in (rev.text(finding.path) or "").splitlines() for rev in at_base) -def history_findings(repo: Path, base: Revision, head: Revision, fork: str) -> tuple[list[str], list[str]]: - """(refusals, notes) of the per-commit pass; FloorError when it cannot judge.""" +def _limits() -> Limits: + return Limits(max_blob_bytes=FLOOR_MAX_BLOB_BYTES, judge_binary=True) + + +def history_findings( + repo: Path, base: Revision, head: Revision, fork: str, exempt: frozenset[str] = frozenset() +) -> tuple[list[str], list[str]]: + """(refusals, notes) of the per-commit pass; FloorError (undecided) when it cannot judge all of it. + + `exempt` are the packs whose lock entry the pull request changed: only their source folder is skipped. + """ with tempfile.TemporaryDirectory(prefix="chock-floor-history-") as scratch: root = Path(scratch) base.materialise(_COMPILED, root) try: - rules, _skipped = load_rules(root) + rules, _skipped = load_rules(root, _SURFACES) except gitlog.HistoryError as exc: - if not root.joinpath(_COMPILED).is_dir() or "no installed content gates" in str(exc): - return [], ["history: the base has no content gates; per-commit pass not run"] + if "no installed content gates" in str(exc): + return [], [NO_CONTENT_GATES] msg = f"history: {exc}" raise FloorError(msg) from exc - rules = [dataclasses.replace(rule, pragma_pattern=None) for rule in rules] + rules = [ + dataclasses.replace( + rule, + pragma_pattern=None, + judge=GateContext( + repo_root=repo, + scope=rule.judge.scope, + own=(f"{_POLICIES}{rule.policy}/",) if rule.policy in exempt else (), + ), + ) + for rule in rules + ] + limits = _limits() try: - report = scan(repo, rules, fork, Limits(), head.sha) + report = scan(repo, rules, fork, limits, head.sha) except gitlog.HistoryError as exc: msg = f"history: {exc}" raise FloorError(msg) from exc + if report.truncated: + msg = ( + f"history: {fork[:_SHORT]}..{head.sha[:_SHORT]} holds more than {limits.max_commits} commits; " + "the commits past the limit were not judged" + ) + raise FloorError(msg) + if report.unread: + named = "; ".join(f"{c[:_SHORT]} {inert(p)} ({why})" for c, p, why in report.unread) + msg = f"history: blobs not judged: {named}" + raise FloorError(msg) at_base = (base, Revision(repo, fork)) lines = [ f"{f.policy} [history]: {f.commit[:_SHORT]} {inert(f.path)}{f':{f.line}' if f.line else ''} ({f.rule})" diff --git a/src/chock/validation/tamper.py b/src/chock/validation/tamper.py index b36366d2..3d8d0c2f 100644 --- a/src/chock/validation/tamper.py +++ b/src/chock/validation/tamper.py @@ -1,32 +1,26 @@ -"""The floor's built-in tamper rules: a guardrail the base has and the head removed or weakened, by name. +"""The floor's built-in tamper rules: a guardrail the merge-base has and the head removed or weakened, by name. -Each rule compares base against head structurally and refuses only a removal or a weakening, so an -addition or a regeneration (`chock sync`) stays silent. The facts live in `data/tamper.json`, inside -the engine, so the pull request under judgement cannot edit them. +Every rule compares the merge-base with the head (the base tip may have moved on since the pull +request forked; what it added there is not the pull request's to keep). The facts live in +`data/tamper.json`, inside the engine, so the pull request under judgement cannot edit them. """ from __future__ import annotations -import fnmatch import json -import re from dataclasses import dataclass from functools import cache from typing import Any -import yaml - -from chock import yamlio from chock.display import inert -from chock.gate.runner.constants import _ACTION_RANK from chock.resources import package_data_dir from chock.validation.floor_git import Revision -_UNIVERSAL = frozenset({"", "*"}) _SHOWN = 120 -_CONFIG = ".chock/config.yaml" -#: YAML 1.1 reads a bare `on:` key as boolean true. -_ON_KEYS = ("on", True) +PACK_LIMIT = ( + "floor/pack-changed: the floor cannot tell a policy upgrade from a policy weakening offline; it lists every " + "pack whose content or lock entry changed, and the base's gates still judge the pull request" +) @cache @@ -44,16 +38,20 @@ def render(self) -> str: return f"tamper/{self.rule} {inert(self.path)}: {inert(self.detail)}" -def _norm(text: str) -> str: - """`text` with the parts a regeneration changes (hashes, versions) replaced by one placeholder.""" - return re.sub(facts()["volatile_pattern"], facts()["volatile_placeholder"], text) +@dataclass(frozen=True) +class Revs: + """The three commits a floor run reads: the base tip (whose config binds), the merge-base and the head.""" + + base: Revision + fork: Revision + head: Revision -def _shown(text: str) -> str: +def shown(text: str) -> str: return text if len(text) <= _SHOWN else text[: _SHOWN - 3] + "..." -def _json(text: str | None) -> Any: +def as_json(text: str | None) -> Any: if text is None: return None try: @@ -62,192 +60,71 @@ def _json(text: str | None) -> Any: return None -# --- hook registrations --------------------------------------------------------------------------- +def changed(revs: Revs, prefix: str) -> list[str]: + """Paths under `prefix` whose blob differs between the merge-base and the head (added, edited or deleted).""" + paths = set(revs.fork.paths(prefix)) | set(revs.head.paths(prefix)) + return sorted(path for path in paths if revs.fork.oid(path) != revs.head.oid(path)) -@dataclass(frozen=True) -class _Hook: - event: str - matchers: frozenset[str] - command: tuple[tuple[str, str], ...] - shown: str - - -def _pointed(doc: Any, pointer: str) -> Any: - for part in [p for p in pointer.split("/") if p]: - doc = doc.get(part) if isinstance(doc, dict) else None - return doc - - -def _hook(event: str, matcher: object, item: dict) -> _Hook | None: - keys = facts()["hook_command_keys"] - command = tuple((key, _norm(str(item[key]))) for key in keys if isinstance(item.get(key), str)) - if not command: - return None - raw = str(matcher) if isinstance(matcher, str) else "" - matchers = frozenset(part.strip() for part in raw.split(facts()["hook_matcher_separator"])) - return _Hook(event, matchers, command, _shown(str(item[keys[0]] if keys[0] in item else command[0][1]))) - - -def hooks(doc: Any, pointer: str) -> list[_Hook]: - """Every (event, matcher, command) entry of a vendor hook file, nested or flat.""" - table = _pointed(doc, pointer) - found: list[_Hook] = [] - nested, matcher_key = facts()["hook_nested_key"], facts()["hook_matcher_key"] - for event, items in table.items() if isinstance(table, dict) else (): - for item in items if isinstance(items, list) else (): - if not isinstance(item, dict): - continue - inner = item.get(nested) - leaves = [h for h in inner if isinstance(h, dict)] if isinstance(inner, list) else [item] - for leaf in leaves: - entry = _hook(str(event), leaf.get(matcher_key, item.get(matcher_key)), leaf) - if entry is not None: - found.append(entry) - return found - - -def _covers(head: frozenset[str], base: frozenset[str]) -> bool: - return bool(head & _UNIVERSAL) or (not base & _UNIVERSAL and head >= base) - - -def _kept(entry: _Hook, head: list[_Hook]) -> bool: - return any( - h.event == entry.event and h.command == entry.command and _covers(h.matchers, entry.matchers) for h in head - ) - - -def hook_removed(base: Revision, head: Revision) -> list[Tamper]: - found: list[Tamper] = [] - for spec in facts()["hook_files"]: - for path in [p for p in base.paths() if fnmatch.fnmatchcase(p, spec["glob"])]: - was = _json(base.text(path)) - if was is None: - continue - now = _json(head.text(path)) - kept = hooks(now, spec["pointer"]) if now is not None else [] - for entry in hooks(was, spec["pointer"]): - if not _kept(entry, kept): - found.append(Tamper("hook-removed", path, f"{entry.event} hook removed: {entry.shown}")) - return found - - -# --- compiled gates, the lock and the vendored runtime ------------------------------------------- - - -def _gate_paths(rev: Revision) -> list[str]: - prefix, name = facts()["compiled_prefix"], facts()["gate_file"] - return [ - path - for path in rev.paths(prefix) - if any(fnmatch.fnmatchcase(path, f"{prefix}*/{surface}/{name}") for surface in facts()["gate_surfaces"]) - ] +def _lock_entries(rev: Revision) -> dict[str, str]: + doc = as_json(rev.text(facts()["lock_file"])) + packs = doc.get("packs") if isinstance(doc, dict) else None + return {str(p["id"]): json.dumps(p, sort_keys=True) for p in packs or () if isinstance(p, dict) and "id" in p} -def gate_rules(base: Revision, head: Revision) -> list[Tamper]: - found: list[Tamper] = [] - for path in _gate_paths(base): - was = _json(base.text(path)) - if not isinstance(was, dict): - continue - if not head.has(path): - found.append(Tamper("gate-removed", path, "compiled gate present at the base, deleted at the head")) - continue - now = _json(head.text(path)) - if not isinstance(now, dict): - found.append(Tamper("gate-weakened", path, "gate is not a JSON object at the head")) - continue - lost = sorted(set(was.get("on") or []) - set(now.get("on") or [])) - if lost: - found.append(Tamper("gate-weakened", path, f"no longer bound to: {', '.join(map(str, lost))}")) - was_action, now_action = str(was.get("action", "block")), str(now.get("action", "block")) - if _ACTION_RANK.get(now_action, -1) < _ACTION_RANK.get(was_action, -1): - found.append(Tamper("gate-weakened", path, f"action {was_action} -> {now_action}")) - return found - - -def _pack_ids(text: str | None) -> set[str]: - doc = _json(text) - packs = doc.get("packs") if isinstance(doc, dict) else None - return {str(p["id"]) for p in packs or () if isinstance(p, dict) and "id" in p} +def packs_changed(revs: Revs) -> frozenset[str]: + """Pack ids whose `chock.lock` entry the pull request added or changed (merge-base -> head).""" + was, now = _lock_entries(revs.fork), _lock_entries(revs.head) + return frozenset(pack for pack, entry in now.items() if was.get(pack) != entry) -def lock_pack_removed(base: Revision, head: Revision) -> list[Tamper]: +def lock_pack_removed(revs: Revs) -> list[Tamper]: lock = facts()["lock_file"] - gone = sorted(_pack_ids(base.text(lock)) - _pack_ids(head.text(lock))) + gone = sorted(set(_lock_entries(revs.fork)) - set(_lock_entries(revs.head))) return [ - Tamper("lock-pack-removed", lock, f"pack {pack!r} is locked at the base, missing at the head") for pack in gone + Tamper("lock-pack-removed", lock, f"pack {pack!r} is locked at the merge-base, missing at the head") + for pack in gone ] -def runtime_removed(base: Revision, head: Revision) -> list[Tamper]: - prefix = facts()["runtime_prefix"] - kept = {_norm(path) for path in head.paths(prefix)} +def _pack_of(path: str) -> str: + for prefix in (facts()["policies_prefix"], facts()["compiled_prefix"]): + if path.startswith(prefix): + return path.removeprefix(prefix).partition("/")[0] + return "" + + +def pack_notes(revs: Revs, packs: frozenset[str]) -> list[str]: + """One `floor/pack-changed` note per pack whose content or lock entry changed, naming every file of it the + pull request changed; only a pack with a lock change has its source exempt from its own gates.""" + touched = {_pack_of(p) for prefix in ("policies_prefix", "compiled_prefix") for p in changed(revs, facts()[prefix])} + notes: list[str] = [] + for pack in sorted((touched - {""}) | packs): + source = changed(revs, f"{facts()['policies_prefix']}{pack}/") + compiled = changed(revs, f"{facts()['compiled_prefix']}{pack}/") + if pack in packs: + detail = f"floor/pack-changed: {pack}: lock entry changed" + if source: + detail += f"; its own gates do not judge its source: {', '.join(map(inert, source))}" + else: + detail = f"floor/pack-changed: {pack}: no lock change, so every gate judged it" + if source: + detail += f"; source: {', '.join(map(inert, source))}" + if compiled: + detail += f"; compiled: {', '.join(map(inert, compiled))}" + notes.append(detail) + return [*notes, PACK_LIMIT] if notes else [] + + +def tamper(revs: Revs, packs: frozenset[str]) -> list[Tamper]: + """Every removal or weakening of a guardrail from merge-base to head, in rule order.""" + from chock.validation import tamper_files, tamper_hooks # noqa: PLC0415 -- they import this module's facts + return [ - Tamper("runtime-removed", path, "vendored runtime file deleted") - for path in base.paths(prefix) - if _norm(path) not in kept + *tamper_hooks.hook_removed(revs), + *tamper_hooks.hooks_switched_off(revs), + *tamper_files.gate_rules(revs, packs), + *lock_pack_removed(revs), + *tamper_files.runtime_rules(revs), + *tamper_files.floor_workflow(revs), ] - - -# --- the required workflow ------------------------------------------------------------------------ - - -def _yaml(text: str | None) -> Any: - if text is None: - return None - try: - return yamlio.safe_load(text) - except yaml.YAMLError: - return None - - -def workflow_path(base: Revision) -> str: - """The workflow the floor guards: the base config's `floor_workflow`, else the default.""" - config = _yaml(base.text(_CONFIG)) - named = config.get(facts()["workflow_config_key"]) if isinstance(config, dict) else None - return named if isinstance(named, str) and named.strip() else facts()["workflow_default"] - - -def _triggers(doc: dict) -> set[str]: - raw = next((doc[key] for key in _ON_KEYS if key in doc), None) - if isinstance(raw, str): - return {raw} - return {str(t) for t in raw} if isinstance(raw, list | dict) else set() - - -def _steps(job: object) -> set[tuple[str, str]]: - steps = job.get("steps") if isinstance(job, dict) else None - return { - (str(s.get("name", "")), _norm(str(s.get("run") or s.get("uses") or ""))) - for s in steps or () - if isinstance(s, dict) - } - - -def ci_step_removed(base: Revision, head: Revision) -> list[Tamper]: - path = workflow_path(base) - was = _yaml(base.text(path)) - if not isinstance(was, dict): - return [] - now = _yaml(head.text(path)) - if not isinstance(now, dict): - return [Tamper("ci-step-removed", path, "workflow deleted or unreadable at the head")] - found = [Tamper("ci-step-removed", path, f"trigger {t!r} removed") for t in sorted(_triggers(was) - _triggers(now))] - head_jobs = now.get("jobs") if isinstance(now.get("jobs"), dict) else {} - base_jobs = was.get("jobs") if isinstance(was.get("jobs"), dict) else {} - for job_id, job in base_jobs.items(): - if job_id not in head_jobs: - found.append(Tamper("ci-step-removed", path, f"job {job_id!r} removed")) - continue - for name, body in sorted(_steps(job) - _steps(head_jobs[job_id])): - found.append(Tamper("ci-step-removed", path, f"job {job_id!r}: step {_shown(name or body)!r} removed")) - return found - - -RULES = (hook_removed, gate_rules, lock_pack_removed, runtime_removed, ci_step_removed) - - -def tamper(base: Revision, head: Revision) -> list[Tamper]: - """Every removal or weakening of a guardrail from base to head, in rule order.""" - return [finding for rule in RULES for finding in rule(base, head)] diff --git a/src/chock/validation/tamper_files.py b/src/chock/validation/tamper_files.py new file mode 100644 index 00000000..8bd5cd6b --- /dev/null +++ b/src/chock/validation/tamper_files.py @@ -0,0 +1,162 @@ +"""Tamper rules for files chock writes: compiled gates, the vendored runtimes and the floor's own workflow. + +A runtime file or the floor workflow must be byte-equal to the merge-base or to what this engine +renders; anything else is refused by name. No structural diff is attempted, so `if: false`, +`continue-on-error`, a narrowed trigger or an added PATH-shadowing step are refused like any edit. +These files change only through a privileged merge: first a pull request that moves the floor +workflow's engine pin, then the regeneration, which the new engine renders byte-equal. +""" + +from __future__ import annotations + +import fnmatch +import tempfile +from pathlib import Path + +import yaml + +from chock import yamlio +from chock.gate.runner.constants import _ACTION_RANK +from chock.validation.floor_git import FloorError, Revision +from chock.validation.tamper import Revs, Tamper, as_json, changed, facts +from chock.validation.tamper_hooks import hook_files + +_CONFIG = ".chock/config.yaml" +_PRIVILEGED = "changes only through a privileged merge (first the engine pin, then a regeneration it renders)" + + +def _gate_paths(rev: Revision) -> list[str]: + prefix, name = facts()["compiled_prefix"], facts()["gate_file"] + globs = [f"{prefix}*/{surface}/{name}" for surface in facts()["gate_surfaces"]] + return [path for path in rev.paths(prefix) if any(fnmatch.fnmatchcase(path, g) for g in globs)] + + +def _how(was: dict, now: object) -> str: + if not isinstance(now, dict): + return "gate is not a JSON object at the head" + lost = sorted(set(map(str, was.get("on") or [])) - set(map(str, now.get("on") or []))) + if lost: + return f"no longer bound to: {', '.join(lost)}" + was_action, now_action = str(was.get("action", "block")), str(now.get("action", "block")) + if _ACTION_RANK.get(now_action, -1) < _ACTION_RANK.get(was_action, -1): + return f"action {was_action} -> {now_action}" + return "kind, params, paths or message changed" + + +def gate_rules(revs: Revs, packs: frozenset[str]) -> list[Tamper]: + """A compiled gate the merge-base has: deleted, or changed with no lock change for its pack.""" + found: list[Tamper] = [] + prefix = facts()["compiled_prefix"] + for path in _gate_paths(revs.fork): + if not revs.head.has(path): + found.append(Tamper("gate-removed", path, "compiled gate present at the merge-base, deleted at the head")) + continue + if revs.head.oid(path) == revs.fork.oid(path): + continue + pack = path.removeprefix(prefix).partition("/")[0] + if pack in packs: + continue # listed under floor/pack-changed + was = as_json(revs.fork.text(path)) + detail = _how(was if isinstance(was, dict) else {}, as_json(revs.head.text(path))) + found.append(Tamper("gate-changed", path, f"{detail}, and chock.lock has no change for {pack!r}")) + return found + + +# --- what this engine renders ------------------------------------------------------------------- + + +def _runtime_render(name: str) -> bytes | None: + from chock.hooks.launch import launcher_text # noqa: PLC0415 -- heavy imports, only when a runtime changed + from chock.vendored import VENDORED_RUNTIMES, expected_runtime # noqa: PLC0415 + + if f"{facts()['runtime_prefix']}{name}" == facts()["launcher"]: + return launcher_text().encode("utf-8") + if name in VENDORED_RUNTIMES: + return expected_runtime(*VENDORED_RUNTIMES[name]) + return None + + +def _vendor(index_path: str) -> str: + return index_path.removeprefix(facts()["index_prefix"]).split("-", 1)[0] + + +def rendered_indexes(head: Revision, vendors: set[str]) -> dict[str, bytes] | None: + """{path: bytes} of the pinned indexes `chock sync` writes for `vendors` from the head's committed policy + data (read as data; nothing of it runs); None when the engine cannot render them.""" + from chock.hooks.in_agent_install import install_hooks # noqa: PLC0415 -- only when an index changed + + index = facts()["index_prefix"] + with tempfile.TemporaryDirectory(prefix="chock-floor-render-") as scratch: + shadow = Path(scratch) + prefixes = [facts()["compiled_prefix"], facts()["policies_prefix"], _CONFIG] + prefixes += [p for _, p in hook_files(head.paths())] + for prefix in prefixes: + head.materialise(prefix, shadow) + try: + for vendor in sorted(vendors): + install_hooks(shadow, vendor) + except Exception: # noqa: BLE001 -- any failure to render is "not rendered", which refuses + return None + folder = shadow / index + return { + f"{index}{p.name}": p.read_bytes() + for p in (folder.glob("*.json") if folder.is_dir() else []) + if _vendor(f"{index}{p.name}") in vendors + } + + +def runtime_rules(revs: Revs) -> list[Tamper]: + """Every `.chock/bin/` file the pull request changed must be the merge-base's bytes or this engine's render.""" + prefix, index = facts()["runtime_prefix"], facts()["index_prefix"] + paths = changed(revs, prefix) + vendors = {_vendor(p) for p in paths if p.startswith(index)} + rendered = rendered_indexes(revs.head, vendors) if vendors else {} + found: list[Tamper] = [] + for path in paths: + now = revs.head.blob(path) + if path.startswith(index): + expected = None if rendered is None else rendered.get(path) + else: + expected = _runtime_render(path.removeprefix(prefix)) + if now is None and path.startswith(index) and rendered is not None and path not in rendered: + continue # a stale index the engine's own sync prunes + if now is None: + found.append(Tamper("runtime-removed", path, f"vendored runtime file deleted; it {_PRIVILEGED}")) + elif now != expected: + found.append( + Tamper("runtime-changed", path, f"neither the merge-base's bytes nor this engine's; it {_PRIVILEGED}") + ) + return found + + +# --- the floor's own workflow ------------------------------------------------------------------- + + +def workflow_path(base: Revision) -> str: + """The workflow the floor guards: the base config's `floor_workflow`, else the default.""" + try: + config = yamlio.safe_load(base.text(_CONFIG) or "") + except yaml.YAMLError as exc: + msg = f"{_CONFIG} at the base does not parse; cannot tell which workflow the floor guards" + raise FloorError(msg) from exc + named = config.get(facts()["workflow_config_key"]) if isinstance(config, dict) else None + return named if isinstance(named, str) and named.strip() else facts()["workflow_default"] + + +def _workflow_renders() -> list[bytes]: + from chock.scaffold import install_ci # noqa: PLC0415 + + return [install_ci.render().encode("utf-8")] + + +def floor_workflow(revs: Revs) -> list[Tamper]: + path = workflow_path(revs.base) + if revs.head.oid(path) == revs.fork.oid(path): + return [] + now = revs.head.blob(path) + if now is None: + return [Tamper("floor-workflow-changed", path, f"the floor's workflow was deleted; it {_PRIVILEGED}")] + if now in _workflow_renders(): + return [] + detail = f"neither the merge-base's bytes nor this engine's render; the floor's workflow {_PRIVILEGED}" + return [Tamper("floor-workflow-changed", path, detail)] diff --git a/src/chock/validation/tamper_hooks.py b/src/chock/validation/tamper_hooks.py new file mode 100644 index 00000000..87f97af2 --- /dev/null +++ b/src/chock/validation/tamper_hooks.py @@ -0,0 +1,131 @@ +"""Tamper rules for agent hook registrations: an entry removed or retyped, or every hook switched off. + +A hook entry's identity is (event, matcher, type, command). A matcher may widen, and the index pin a +regeneration rewrites (`--index-sha256 `) is normalised; nothing else may change. +""" + +from __future__ import annotations + +import fnmatch +import re +from dataclasses import dataclass +from typing import Any + +from chock.validation.tamper import Revs, Tamper, as_json, facts, shown + +_UNIVERSAL = frozenset({"", "*"}) + + +@dataclass(frozen=True) +class Hook: + event: str + matchers: frozenset[str] + type: str + command: tuple[tuple[str, str], ...] + shown: str + + +def _norm(text: str) -> str: + return re.sub(facts()["volatile_pattern"], facts()["volatile_placeholder"], text) + + +def pointed(doc: Any, pointer: str) -> Any: + for part in [p for p in pointer.split("/") if p]: + doc = doc.get(part) if isinstance(doc, dict) else None + return doc + + +def _hook(event: str, matcher: object, item: dict) -> Hook | None: + keys = facts()["hook_command_keys"] + command = tuple((key, _norm(str(item[key]))) for key in keys if isinstance(item.get(key), str)) + if not command: + return None + raw = str(matcher) if isinstance(matcher, str) else "" + matchers = frozenset(part.strip() for part in raw.split(facts()["hook_matcher_separator"])) + kind = str(item.get(facts()["hook_type_key"], facts()["hook_type_default"])) + return Hook(event, matchers, kind, command, shown(command[0][1])) + + +def hooks(doc: Any, pointer: str) -> list[Hook]: + """Every (event, matcher, type, command) entry of a vendor hook file, nested or flat.""" + table = pointed(doc, pointer) + found: list[Hook] = [] + nested, matcher_key = facts()["hook_nested_key"], facts()["hook_matcher_key"] + for event, items in table.items() if isinstance(table, dict) else (): + for item in items if isinstance(items, list) else (): + if not isinstance(item, dict): + continue + inner = item.get(nested) + leaves = [h for h in inner if isinstance(h, dict)] if isinstance(inner, list) else [item] + for leaf in leaves: + entry = _hook(str(event), leaf.get(matcher_key, item.get(matcher_key)), leaf) + if entry is not None: + found.append(entry) + return found + + +def _covers(head: frozenset[str], base: frozenset[str]) -> bool: + return bool(head & _UNIVERSAL) or (not base & _UNIVERSAL and head >= base) + + +def _kept(entry: Hook, head: list[Hook]) -> bool: + return any( + (h.event, h.type, h.command) == (entry.event, entry.type, entry.command) and _covers(h.matchers, entry.matchers) + for h in head + ) + + +def hook_files(rev_paths: list[str]) -> list[tuple[dict, str]]: + """(spec, path) of every vendor hook file among `rev_paths`.""" + return [(spec, p) for spec in facts()["hook_files"] for p in rev_paths if fnmatch.fnmatchcase(p, spec["glob"])] + + +def hook_removed(revs: Revs) -> list[Tamper]: + found: list[Tamper] = [] + for spec, path in hook_files(revs.fork.paths()): + was = as_json(revs.fork.text(path)) + if was is None: + continue + now = as_json(revs.head.text(path)) + kept = hooks(now, spec["pointer"]) if now is not None else [] + for entry in hooks(was, spec["pointer"]): + if not _kept(entry, kept): + found.append(Tamper("hook-removed", path, f"{entry.event} {entry.type} hook removed: {entry.shown}")) + return found + + +def _switched_on(doc: Any, keys: list[str]) -> list[str]: + return [key for key in keys if pointed(doc, key) not in (None, False, 0, "", [], {})] + + +def hooks_switched_off(revs: Revs) -> list[Tamper]: + """A disable key turned on in a vendor hook file, or a higher-precedence file added carrying hooks or one.""" + found: list[Tamper] = [] + switches = facts()["hook_switches"] + for spec, path in hook_files(revs.head.paths()): + keys = switches.get(spec["vendor"], {}).get("disable_keys", []) + now = _switched_on(as_json(revs.head.text(path)), keys) + was = set(_switched_on(as_json(revs.fork.text(path)), keys)) + found += [Tamper("hooks-disabled", path, f"{key} turns every hook off") for key in now if key not in was] + for vendor, switch in sorted(switches.items()): + keys = switch.get("disable_keys", []) + for path in switch.get("override_files", []): + if not revs.head.has(path) or revs.head.oid(path) == revs.fork.oid(path): + continue + doc = as_json(revs.head.text(path)) + carries = doc is None or pointed(doc, facts()["hook_nested_key"]) is not None or _switched_on(doc, keys) + if carries: + found.append( + Tamper( + "hooks-overridden", + path, + f"{vendor} reads this file over its hook file; it carries hooks or a disable key, or does not parse", + ) + ) + return found + + +def not_covered(revs: Revs) -> list[str]: + """Vendors with a hook file at the merge-base whose switch-off keys this engine has not verified.""" + switches = facts()["hook_switches"] + return sorted({spec["vendor"] for spec, _ in hook_files(revs.fork.paths())} - set(switches)) diff --git a/tests/floor_support.py b/tests/floor_support.py index f63948d5..68d4251b 100644 --- a/tests/floor_support.py +++ b/tests/floor_support.py @@ -11,6 +11,7 @@ TOKEN_PATTERN = "FLOOR-TOKEN-[0-9]+" TEST_PRAGMA = "chock: test-removal-reviewed" CHECKOUT = "actions/checkout@" + "0" * 40 +INDEX_PIN = "ab" * 32 TEST_INTEGRITY = { "kind": "test_integrity", @@ -56,7 +57,12 @@ "PreToolUse": [ { "matcher": "Bash", - "hooks": [{"type": "command", "command": "sh .chock/bin/launch.sh claude.py --sha abc1234def"}], + "hooks": [ + { + "type": "command", + "command": f"sh .chock/bin/launch.sh claude.py --multi x.json --index-sha256 {INDEX_PIN}", + } + ], }, { "matcher": "Write|Edit", diff --git a/tests/test_floor_attacks.py b/tests/test_floor_attacks.py new file mode 100644 index 00000000..47e0bd37 --- /dev/null +++ b/tests/test_floor_attacks.py @@ -0,0 +1,284 @@ +"""Regression tests for the adversarial review of `chock check --floor` (content, history, hooks, runtimes). + +Each test is one attack from the review, asserting the exit code the floor owes: 1 refused, 2 undecided, +0 for a correct pull request. Workflow, waiver, script and argument attacks are in test_floor_attacks2.py. +""" + +from __future__ import annotations + +import json +import subprocess +from pathlib import Path + +import pytest +from floor_support import CLAUDE, FLOOR_TOKEN, LOCK, base_repo, commit, git, run_floor, write + +from chock.validation import floor_history + +SECRET = "key = FLOOR-TOKEN-4242\n" + + +# ---------------- content gates (promise 1) ---------------- + + +def test_secret_inside_own_policy_folder_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".agents/policies/floor-token/notes.md", "leak FLOOR-TOKEN-4242\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "FAIL floor-token [base]" in out + assert ( + "floor/pack-changed: floor-token: no lock change, so every gate judged it; " + "source: .agents/policies/floor-token/notes.md" in out + ) + + +def test_own_folder_is_exempt_only_with_a_lock_change_and_is_listed(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".agents/policies/floor-token/evals/case.txt", "FLOOR-TOKEN-4242\n") + write(repo, "chock.lock", {**LOCK, "packs": [LOCK["packs"][0], {"id": "floor-token", "version": "2"}]}) + commit(repo, "upgrade floor-token with a new eval case") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert ( + "floor/pack-changed: floor-token: lock entry changed; its own gates do not judge its source: " + ".agents/policies/floor-token/evals/case.txt" in out + ) + + +def test_secret_in_file_with_nul_byte_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + (repo / "src").mkdir(exist_ok=True) + (repo / "src/config.txt").write_bytes(b"\x00\n" + SECRET.encode()) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "FAIL floor-token [base]" in out + assert "floor-token [history]" in out + + +def test_secret_in_non_utf8_file_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + (repo / "src").mkdir(exist_ok=True) + (repo / "src/blob.bin").write_bytes(b"\xff\xfe\x00" + SECRET.encode()) + commit(repo, "add") + (repo / "src/blob.bin").unlink() + commit(repo, "remove") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "floor-token [history]" in out + + +def test_control_plain_secret_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/config.txt", SECRET) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_head_gitattributes_minus_diff_does_not_hide_test_weakening(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".gitattributes", "tests/** -diff\n") + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "assertions removed across tests" in out + + +def test_control_test_weakening_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +# ---------------- history (promise 4) ---------------- + + +def _fast_import_empty(repo: Path, n: int) -> None: + head = git(repo, "rev-parse", "HEAD") + stream = [] + for i in range(n): + msg = f"pad {i}".encode() + stream.append(b"commit refs/heads/pr\ncommitter t 1700000000 +0000\n") + stream.append(b"data %d\n%s\n" % (len(msg), msg)) + if i == 0: + stream.append(b"from " + head.encode() + b"\n") + stream.append(b"\n") + subprocess.run(["git", "-C", str(repo), "fast-import", "--quiet", "--force"], input=b"".join(stream), check=True) + git(repo, "reset", "-q", "--hard", "pr") + + +def _added_then_removed(repo: Path, rel: str = "src/config.txt", text: str = SECRET) -> None: + write(repo, rel, text) + commit(repo, "add secret") + (repo / rel).unlink() + commit(repo, "remove secret") + + +def test_history_truncation_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + _added_then_removed(repo) + _fast_import_empty(repo, 5000) + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "holds more than 5000 commits" in out + + +def test_control_added_then_removed_secret_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + _added_then_removed(repo) + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_history_judges_a_blob_over_the_history_cli_limit(tmp_path, capsys): + repo = base_repo(tmp_path) + _added_then_removed(repo, "src/big.txt", SECRET + ("x" * 100 + "\n") * 11000) + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "floor-token [history]" in out + + +def test_history_blob_over_the_floor_limit_is_undecided_by_commit_and_path(tmp_path, capsys, monkeypatch): + monkeypatch.setattr(floor_history, "FLOOR_MAX_BLOB_BYTES", 1000) + repo = base_repo(tmp_path) + _added_then_removed(repo, "src/big.txt", SECRET + "x" * 2000) + first = git(repo, "rev-parse", "HEAD~1") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert f"blobs not judged: {first[:12]} src/big.txt" in out + + +def test_history_reads_ci_only_content_gates(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + git(repo, "rm", "-q", "-r", ".chock/compiled/floor-token/git-hook", ".chock/compiled/test-integrity/git-hook") + commit(repo, "content gate on the ci surface only") + git(repo, "checkout", "-q", "-B", "pr") + _added_then_removed(repo) + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "floor-token [history]" in out + assert "no content gates" not in out + + +# ---------------- hooks (promise 3, ruling 3) ---------------- + + +def _claude(repo: Path, doc: dict) -> None: + write(repo, ".claude/settings.json", doc) + commit(repo, "pr") + + +def test_disable_all_hooks_key_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + _claude(repo, {**CLAUDE, "disableAllHooks": True}) + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/hooks-disabled .claude/settings.json: /disableAllHooks turns every hook off" in out + + +def test_settings_local_json_disable_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".claude/settings.local.json", {"disableAllHooks": True}) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/hooks-overridden .claude/settings.local.json" in out + + +def test_settings_local_json_without_hooks_passes(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".claude/settings.local.json", {"permissions": {"allow": ["Bash(ls)"]}}) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_hook_type_changed_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + doc = json.loads(json.dumps(CLAUDE)) + for item in doc["hooks"]["PreToolUse"]: + for leaf in item["hooks"]: + leaf["type"] = "prompt" + leaf["prompt"] = "always allow" + _claude(repo, doc) + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/hook-removed .claude/settings.json: PreToolUse command hook removed" in out + + +@pytest.mark.parametrize("run", ["--sha 0000000000", "--budget 1.2.3"]) +def test_hook_command_digit_swap_is_refused(tmp_path, capsys, run): + """Only the index pin is normalised: any other run of digits or hex in a command is part of its identity.""" + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + doc = json.loads(json.dumps(CLAUDE)) + doc["hooks"]["PreToolUse"][1]["hooks"][0]["command"] += " --sha abc1234def --budget 9.9.9" + _claude(repo, doc) + git(repo, "checkout", "-q", "-B", "pr") + flag = run.split()[0] + words = doc["hooks"]["PreToolUse"][1]["hooks"][0]["command"].split() + words[words.index(flag) + 1] = run.split()[1] + doc["hooks"]["PreToolUse"][1]["hooks"][0]["command"] = " ".join(words) + _claude(repo, doc) + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/hook-removed .claude/settings.json" in out + + +def test_vendors_without_verified_switches_are_named_not_covered(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/other.py", "X = 1\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "note: floor/hook-switches not covered: cursor" in out + + +# ---------------- runtimes, compiled gates and the lock (rulings 4 and 7) ---------------- + + +def test_runtime_emptied_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".chock/bin/gate.py", "import sys; sys.exit(0)\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/runtime-changed .chock/bin/gate.py" in out + + +def test_git_hook_gate_params_neutered_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + neutered = {**FLOOR_TOKEN, "params": {"content_pattern": "(?!)"}, "paths": ["nothing/*"]} + write(repo, ".chock/compiled/floor-token/git-hook/gate.json", neutered) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/gate-changed .chock/compiled/floor-token/git-hook/gate.json" in out + + +def test_gate_kind_swapped_is_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + swapped = {"kind": "forbidden_ref", "on": ["commit", "tool_use"], "action": "block", "params": {"refs": []}} + write(repo, ".chock/compiled/floor-token/git-hook/gate.json", swapped) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/gate-changed .chock/compiled/floor-token/git-hook/gate.json" in out + + +def test_lock_pack_source_swap_is_listed_by_name(tmp_path, capsys): + """A lock entry change is the signal a pack changed; the floor names it rather than refusing (ruling 7).""" + repo = base_repo(tmp_path) + lock = {**LOCK, "packs": [{"id": "test-integrity", "source": "https://evil.example/pack"}, {"id": "floor-token"}]} + write(repo, "chock.lock", lock) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "floor/pack-changed: test-integrity: lock entry changed" in out diff --git a/tests/test_floor_attacks2.py b/tests/test_floor_attacks2.py new file mode 100644 index 00000000..bbf9720c --- /dev/null +++ b/tests/test_floor_attacks2.py @@ -0,0 +1,284 @@ +"""Regression tests for the adversarial review of `chock check --floor` (workflow, waivers, scripts, arguments). + +Each test is one attack, or one correct pull request the floor once refused, asserting the owed exit code. +""" + +from __future__ import annotations + +import json +import os +import subprocess +import sys +from pathlib import Path + +import pytest +from floor_support import ( + CLAUDE, + FLOOR_TOKEN, + INDEX_PIN, + TEST_FILE, + TEST_PRAGMA, + WORKFLOW, + base_repo, + commit, + gate, + git, + run_floor, + write, +) + +SRC = Path(__file__).resolve().parents[1] / "src" +STEP = " - name: Floor\n" +SHADOW = ( + " - name: Setup\n run: mkdir -p b && printf '#!/bin/sh\\nexit 0\\n' > b/chock && chmod +x b/chock" + ' && echo "$PWD/b" >> "$GITHUB_PATH"\n' +) +HEAD_SHA = "${{ github.event.pull_request.head.sha }}" +WORKFLOW_EDITS = { + "ci-if-false": (STEP, STEP + " if: false\n"), + "ci-continue-on-error": (STEP, STEP + " continue-on-error: true\n"), + "ci-job-if-false": (" runs-on: ubuntu-latest\n", " if: false\n runs-on: ubuntu-latest\n"), + "ci-trigger-narrowed": (" pull_request_target:\n", " pull_request_target:\n branches: [no-such-branch]\n"), + "ci-env-swap": (STEP, STEP + f" env:\n BASE_SHA: {HEAD_SHA}\n"), + "ci-checkout-with": (STEP, f" with:\n ref: {HEAD_SHA}\n" + STEP), + "ci-shadow-step": (STEP, SHADOW + STEP), + "fp-step-renamed": ("name: Floor", "name: Chock floor"), + "fp-trigger-dropped": (" merge_group:\n", ""), +} + + +# ---------------- the floor's own workflow (ruling 4: equal or refuse) ---------------- + + +@pytest.mark.parametrize("name", sorted(WORKFLOW_EDITS)) +def test_any_edit_to_the_floor_workflow_is_refused(tmp_path, capsys, name): + old, new = WORKFLOW_EDITS[name] + repo = base_repo(tmp_path) + write(repo, ".github/workflows/chock.yml", WORKFLOW.replace(old, new, 1)) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/floor-workflow-changed .github/workflows/chock.yml" in out + + +def test_an_action_bump_in_the_floor_workflow_is_refused(tmp_path, capsys): + """Ruling 4 (owner decision F2 open): even a Dependabot bump goes through the privileged two-step path.""" + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, ".github/workflows/chock.yml", WORKFLOW.replace(STEP, " - uses: actions/setup-python@v5\n" + STEP)) + commit(repo, "base uses setup-python v5") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, ".github/workflows/chock.yml", WORKFLOW.replace(STEP, " - uses: actions/setup-python@v6\n" + STEP)) + commit(repo, "bump setup-python to v6") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "tamper/floor-workflow-changed" in out + + +def test_other_workflows_are_out_of_scope(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".github/workflows/lint.yml", "on: push\njobs: {}\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +# ---------------- correct pull requests (never refused) ---------------- + + +def test_base_without_chock_says_nothing_was_judged(tmp_path, capsys): + repo = tmp_path / "repo" + repo.mkdir() + git(repo, "init", "-q", "-b", "main") + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "base") + git(repo, "checkout", "-q", "-b", "pr") + write(repo, "src/config.txt", "key = FLOOR-TOKEN-4242\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "the base has no compiled gates; no gate judged this pull request" in out + assert "history: the base has no content gates on either surface" in out + + +def test_stale_branch_after_base_adds_a_hook_passes(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr: unrelated edit") + git(repo, "checkout", "-q", "main") + doc = json.loads(json.dumps(CLAUDE)) + doc["hooks"]["Stop"] = [{"hooks": [{"type": "command", "command": "sh .chock/bin/launch.sh claude.py --stop"}]}] + write(repo, ".claude/settings.json", doc) + gate(repo, "late-policy", {**FLOOR_TOKEN, "params": {"content_pattern": "LATE-[0-9]+"}}) + commit(repo, "main adds a hook and a gate after the pr forked") + git(repo, "checkout", "-q", "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "tamper/" not in out + + +def test_ok_add_policy_hook_lock_unrelated(tmp_path, capsys): + """The review's correct pull request, less its edit to the floor workflow (refused by ruling 4, above).""" + repo = base_repo(tmp_path) + gate(repo, "new-policy", {**FLOOR_TOKEN, "params": {"content_pattern": "NEVER-[0-9]+"}}) + doc = json.loads(json.dumps(CLAUDE)) + doc["hooks"]["PreToolUse"][1]["matcher"] = "Edit|Write|MultiEdit" + command = doc["hooks"]["PreToolUse"][0]["hooks"][0]["command"] + doc["hooks"]["PreToolUse"][0]["hooks"][0]["command"] = command.replace(INDEX_PIN, "9" * 64) + write(repo, ".claude/settings.json", doc) + write( + repo, + "chock.lock", + { + "lockfile_version": "2", + "engine": ">=1.3,<2", + "packs": [{"id": "test-integrity", "version": "1.4.0"}, {"id": "floor-token"}], + }, + ) + write(repo, "src/app.py", "VALUE = 3\n") + write(repo, "tests/test_core.py", TEST_FILE + "\ndef test_b():\n assert True\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "floor/pack-changed: test-integrity: lock entry changed" in out + + +# ---------------- waivers (ruling 5) ---------------- + + +def _base_pragma_then(repo: Path, head_text: str) -> None: + git(repo, "checkout", "-q", "main") + write( + repo, + "tests/test_core.py", + f"def test_a():\n # {TEST_PRAGMA}\n assert 1 + 1 == 2\n assert 2 * 2 == 4\n", + ) + commit(repo, "base: an old reviewed removal left its pragma comment") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "tests/test_core.py", head_text) + commit(repo, "pr") + + +def test_duplicating_a_base_waiver_line_does_not_waive(tmp_path, capsys): + repo = base_repo(tmp_path) + _base_pragma_then(repo, f"def test_a():\n # {TEST_PRAGMA}\n # {TEST_PRAGMA}\n pass\n") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_control_same_without_duplicate_refused(tmp_path, capsys): + repo = base_repo(tmp_path) + _base_pragma_then(repo, f"def test_a():\n # {TEST_PRAGMA}\n pass\n") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_a_content_waiver_copied_from_the_base_does_not_waive_an_added_line(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + gate(repo, "floor-token", {**FLOOR_TOKEN, "params": {**FLOOR_TOKEN["params"], "allowlist_pragma": "floor-ok"}}) + write(repo, "src/fixture.py", "X = 'FLOOR-TOKEN-1' # floor-ok\n") + commit(repo, "base: a reviewed fixture") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/fixture.py", "X = 'FLOOR-TOKEN-1' # floor-ok\nY = 2\nX = 'FLOOR-TOKEN-1' # floor-ok\n") + commit(repo, "pr adds a copy of the waived line") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +# ---------------- script gates and rollout (ruling 6) ---------------- + + +def _crashing_probe(repo: Path, *, observe: bool) -> None: + git(repo, "checkout", "-q", "main") + write(repo, ".agents/policies/probe/implementations/probe.py", "raise RuntimeError('boom')\n") + if observe: + write(repo, ".chock/config.yaml", "rollout: observe\n") + commit(repo, "base: the script gate crashes") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + + +@pytest.mark.parametrize("observe", [False, True]) +def test_a_crashing_base_script_gate_is_undecided_whatever_the_rollout(tmp_path, capsys, observe): + repo = base_repo(tmp_path) + _crashing_probe(repo, observe=observe) + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "UNDECIDED probe [base]: script gate: probe.py crashed" in out + + +def test_a_content_waiver_on_an_unchanged_line_still_counts(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + gate(repo, "floor-token", {**FLOOR_TOKEN, "params": {**FLOOR_TOKEN["params"], "allowlist_pragma": "floor-ok"}}) + write(repo, "src/fixture.py", "X = 'FLOOR-TOKEN-1' # floor-ok\n") + commit(repo, "base: a reviewed fixture") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/fixture.py", "X = 'FLOOR-TOKEN-1' # floor-ok\nY = 2\n") + commit(repo, "pr edits around it") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_head_cannot_lower_rollout(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".chock/config.yaml", "rollout: observe\n") + write(repo, "src/config.txt", "key = FLOOR-TOKEN-4242\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_a_gate_naming_a_file_outside_the_repository_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + spec = { + "kind": "dependency_allowlist", + "on": ["commit"], + "action": "block", + "params": {"allowlist_file": "/etc/hostname"}, + } + gate(repo, "deps", spec, surfaces=("ci-gate",)) + commit(repo, "base: a dependency gate with an absolute allowlist") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "not a path inside the repository" in out + + +# ---------------- refs ---------------- + + +def test_head_not_descendant_of_base(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, "src/app.py", "VALUE = 9\n") + commit(repo, "main moves on") + git(repo, "checkout", "-q", "pr") + code, out = run_floor(repo, capsys, "--head", "pr") + assert code == 0, out + + +def _cli(repo: Path, *args: str) -> int: + env = {"PATH": os.environ.get("PATH", ""), "PYTHONPATH": str(SRC), "HOME": str(repo.parent)} + code = "import sys; from chock.lifecycle import check_main; sys.exit(check_main(sys.argv[1:]))" + proc = subprocess.run( + [sys.executable, "-c", code, "--repo", str(repo), *args], capture_output=True, text=True, env=env, check=False + ) + return proc.returncode + + +def test_ref_arguments(tmp_path): + repo = base_repo(tmp_path) + write(repo, "src/config.txt", "key = FLOOR-TOKEN-4242\n") + commit(repo, "pr") + assert _cli(repo, "--floor", "--base", "main", "--head", "pr") == 1 + assert _cli(repo, "--floor", "--base", "no-such-ref", "--head", "pr") == 2 + assert _cli(repo, "--floor", "--base=--all", "--head", "pr") == 2 + assert _cli(repo, "--floor", "--base=--since=2000-01-01", "--head", "pr") == 2 + assert _cli(repo, "--floor", "--base", "main", f"--head=--output={tmp_path / 'x'}") == 2 + assert _cli(repo, "--floor", "--base", "pr", "--head", "pr") == 0 diff --git a/tests/test_floor_tamper.py b/tests/test_floor_tamper.py index c7936711..68bf5880 100644 --- a/tests/test_floor_tamper.py +++ b/tests/test_floor_tamper.py @@ -3,8 +3,24 @@ from __future__ import annotations import json - -from floor_support import CLAUDE, CURSOR, FLOOR_TOKEN, LOCK, WORKFLOW, base_repo, commit, gate, run_floor, write +import shutil +from pathlib import Path + +from floor_support import ( + CLAUDE, + CURSOR, + FLOOR_TOKEN, + LOCK, + WORKFLOW, + base_repo, + commit, + gate, + git, + run_floor, + write, +) + +ROOT = Path(__file__).resolve().parents[1] def test_an_unrelated_change_passes_with_no_tamper_line(tmp_path, capsys) -> None: @@ -35,8 +51,8 @@ def test_removing_one_pre_tool_use_entry_names_the_event_and_command(tmp_path, c code, out = run_floor(repo, capsys) assert code == 1 assert ( - "tamper/hook-removed .claude/settings.json: PreToolUse hook removed: sh .chock/bin/launch.sh claude.py --write" - in out + "tamper/hook-removed .claude/settings.json: PreToolUse command hook removed: " + "sh .chock/bin/launch.sh claude.py --write" in out ) assert out.count("tamper/hook-removed") == 1 @@ -53,10 +69,11 @@ def test_narrowing_a_hook_matcher_is_a_removal(tmp_path, capsys) -> None: def test_a_regeneration_that_adds_a_policy_is_silent(tmp_path, capsys) -> None: - """New policy, new index hash in every command, a widened matcher, a renamed index file: no tamper line.""" + """New locked policy, a new index pin in a command, a widened matcher, a new hook: no tamper line.""" repo = base_repo(tmp_path) settings = json.loads(json.dumps(CLAUDE)) - settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"] = "sh .chock/bin/launch.sh claude.py --sha 99887766ff" + command = settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"] + settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"] = command[:-64] + "cd" * 32 settings["hooks"]["PreToolUse"][1]["matcher"] = "Write|Edit|MultiEdit" settings["hooks"]["Stop"] = [ {"hooks": [{"type": "command", "command": "sh .chock/bin/launch.sh claude.py --stop"}]} @@ -65,15 +82,13 @@ def test_a_regeneration_that_adds_a_policy_is_silent(tmp_path, capsys) -> None: cursor = json.loads(json.dumps(CURSOR)) cursor["hooks"]["preToolUse"] = [{"command": "sh .chock/bin/launch.sh cursor.py --write"}] write(repo, ".cursor/hooks.json", cursor) - (repo / ".chock/bin/index/claude-PreToolUse-3a2134a7.json").unlink() - write(repo, ".chock/bin/index/claude-PreToolUse-77fe01bc.json", "{}\n") gate(repo, "new-policy", {**FLOOR_TOKEN, "params": {"content_pattern": "NEW-POLICY-[0-9]+"}}) write(repo, "chock.lock", {**LOCK, "packs": [*LOCK["packs"], {"id": "new-policy"}]}) - write(repo, ".github/workflows/chock.yml", WORKFLOW.replace("0" * 40, "1" * 40)) commit(repo, "regenerate") code, out = run_floor(repo, capsys) assert code == 0, out assert "tamper/" not in out + assert "floor/pack-changed: new-policy: lock entry changed; compiled: .chock/compiled/new-policy/" in out def test_removing_a_pack_from_the_lock_is_refused(tmp_path, capsys) -> None: @@ -85,48 +100,60 @@ def test_removing_a_pack_from_the_lock_is_refused(tmp_path, capsys) -> None: assert "tamper/lock-pack-removed chock.lock: pack 'floor-token'" in out -def test_removing_the_floor_step_from_the_workflow_is_refused(tmp_path, capsys) -> None: +def test_any_edit_to_the_floor_workflow_is_refused(tmp_path, capsys) -> None: repo = base_repo(tmp_path) write(repo, ".github/workflows/chock.yml", WORKFLOW.split(" - name: Floor")[0]) commit(repo, "pr") code, out = run_floor(repo, capsys) assert code == 1 - assert "tamper/ci-step-removed .github/workflows/chock.yml: job 'floor': step 'Floor' removed" in out + assert "tamper/floor-workflow-changed .github/workflows/chock.yml: neither the merge-base's bytes" in out + assert "privileged merge" in out + +def test_the_floor_workflow_may_become_what_this_engine_renders(tmp_path, capsys) -> None: + from chock.scaffold import install_ci -def test_dropping_a_trigger_is_refused(tmp_path, capsys) -> None: repo = base_repo(tmp_path) - write(repo, ".github/workflows/chock.yml", WORKFLOW.replace(" merge_group:\n", "")) - commit(repo, "pr") + write(repo, ".github/workflows/chock.yml", install_ci.render()) + commit(repo, "regenerate the workflow") code, out = run_floor(repo, capsys) - assert code == 1 - assert "trigger 'merge_group' removed" in out + assert code == 0, out def test_the_guarded_workflow_is_named_by_the_base_config(tmp_path, capsys) -> None: repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") (repo / ".github/workflows/chock.yml").rename(repo / ".github/workflows/floor.yml") write(repo, ".chock/config.yaml", "floor_workflow: .github/workflows/floor.yml\n") - from floor_support import git - - git(repo, "checkout", "-q", "main") commit(repo, "move the workflow") git(repo, "checkout", "-q", "-B", "pr") (repo / ".github/workflows/floor.yml").unlink() commit(repo, "pr") code, out = run_floor(repo, capsys) assert code == 1 - assert "tamper/ci-step-removed .github/workflows/floor.yml" in out + assert "tamper/floor-workflow-changed .github/workflows/floor.yml: the floor's workflow was deleted" in out -def test_weakening_a_gate_action_or_binding_is_refused(tmp_path, capsys) -> None: +def test_weakening_a_gate_without_a_lock_change_is_refused(tmp_path, capsys) -> None: repo = base_repo(tmp_path) gate(repo, "floor-token", {**FLOOR_TOKEN, "action": "warn", "on": ["tool_use"]}, surfaces=("git-hook",)) commit(repo, "pr") code, out = run_floor(repo, capsys) assert code == 1 - assert "tamper/gate-weakened .chock/compiled/floor-token/git-hook/gate.json: no longer bound to: commit" in out - assert "action block -> warn" in out + assert "tamper/gate-changed .chock/compiled/floor-token/git-hook/gate.json: no longer bound to: commit" in out + assert "chock.lock has no change for 'floor-token'" in out + + +def test_a_gate_change_with_a_lock_change_is_listed_not_refused(tmp_path, capsys) -> None: + repo = base_repo(tmp_path) + gate(repo, "floor-token", {**FLOOR_TOKEN, "message": "Still no floor tokens."}) + write(repo, "chock.lock", {**LOCK, "packs": [LOCK["packs"][0], {"id": "floor-token", "version": "2"}]}) + commit(repo, "upgrade floor-token") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "floor/pack-changed: floor-token: lock entry changed; compiled: " in out + assert ".chock/compiled/floor-token/ci-gate/gate.json, .chock/compiled/floor-token/git-hook/gate.json" in out + assert "cannot tell a policy upgrade from a policy weakening offline" in out def test_deleting_a_compiled_gate_is_refused(tmp_path, capsys) -> None: @@ -138,7 +165,7 @@ def test_deleting_a_compiled_gate_is_refused(tmp_path, capsys) -> None: assert "tamper/gate-removed .chock/compiled/floor-token/ci-gate/gate.json" in out -def test_an_edited_runner_is_never_run_and_only_its_deletion_is_named(tmp_path, capsys) -> None: +def test_an_edited_runner_is_never_run_and_is_refused(tmp_path, capsys) -> None: repo = base_repo(tmp_path) write(repo, ".chock/bin/gate.py", "raise SystemExit(0)\n") (repo / "tests/test_core.py").unlink() @@ -146,8 +173,52 @@ def test_an_edited_runner_is_never_run_and_only_its_deletion_is_named(tmp_path, code, out = run_floor(repo, capsys) assert code == 1 assert "tests/test_core.py: test file deleted" in out - assert "tamper/runtime-removed" not in out + assert "tamper/runtime-changed .chock/bin/gate.py" in out (repo / ".chock/bin/gate.py").unlink() commit(repo, "pr 2") code, out = run_floor(repo, capsys) assert "tamper/runtime-removed .chock/bin/gate.py" in out + + +def test_a_runtime_may_become_what_this_engine_renders(tmp_path, capsys) -> None: + from chock.gate.assemble import runner_source + from chock.hooks.launch import launcher_text + + repo = base_repo(tmp_path) + write(repo, ".chock/bin/gate.py", runner_source()) + write(repo, ".chock/bin/launch.sh", launcher_text()) + commit(repo, "regenerate the runtime") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def _real_install(tmp_path: Path) -> tuple[Path, str]: + """A base carrying this repository's own compiled policies, Claude Code hooks and their pinned indexes.""" + repo = tmp_path / "real" + repo.mkdir() + git(repo, "init", "-q", "-b", "main") + for rel in (".chock/compiled", ".agents/policies"): + shutil.copytree(ROOT / rel, repo / rel) + for rel in (".claude/settings.json", ".chock/config.yaml"): + (repo / rel).parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(ROOT / rel, repo / rel) + index = sorted((ROOT / ".chock/bin/index").glob("claude_code-*.json"))[0] + rel = f".chock/bin/index/{index.name}" + (repo / rel).parent.mkdir(parents=True, exist_ok=True) + (repo / rel).write_text("{}\n", encoding="utf-8") + commit(repo, "base: a stale index") + git(repo, "checkout", "-q", "-b", "pr") + return repo, rel + + +def test_an_index_this_engine_renders_is_accepted_and_any_other_refused(tmp_path, capsys) -> None: + repo, rel = _real_install(tmp_path) + shutil.copy2(ROOT / rel, repo / rel) + commit(repo, "sync rewrites the index") + code, out = run_floor(repo, capsys, "--head-ref", "pr") + assert code == 0, out + write(repo, rel, json.loads((ROOT / rel).read_text(encoding="utf-8")) | {"members": []}) + commit(repo, "an index that runs nothing") + code, out = run_floor(repo, capsys, "--head-ref", "pr") + assert code == 1 + assert f"tamper/runtime-changed {rel}" in out From 25a1b0722214f4cf3e00247f289a8c1bf663725c Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:19:53 +0000 Subject: [PATCH 16/57] fix: managed emit takes its interpreter and failure choice from the admin, in exec form W2-B1r, the eight review rulings: - Every managed hook is exec form: `command` is the required absolute `--python`, `args` run an inline bootstrap on the bundled runtime with `-I`. No git, sh, launcher, PATH lookup or `chock.python` stands in between; the bundle no longer ships launch.sh. - `--on-failure block|allow` is required. block sets onFailure on PreToolUse and makes a missing interpreter or bundle file a refusal; allow sets neither and lets the call through with a note. deploy.json and the report state the consequence. The matcher-less repo-stop entry is no longer emitted. The Stop gate notes and exits 0 when it cannot run. - The output guard refuses filesystem roots and more system trees, folds case on macOS and Windows, and normalises \\?\, \\.\ and admin-share spellings. --out naming a file, a non-UTF-8 policy file and a missing --repo are one-line refusals. - deploy.json states admin ownership of the bundle and interpreter, the repository stop, the runtime's git use, and every documented --lock effect. Docs no longer say the compiler writes managed-settings.json or that a user cannot override managed settings; cli-reference gains emit. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 2 +- changelog.d/w2b1-managed-emit-claude.md | 26 +- docs/architecture.md | 2 +- docs/cli-reference.md | 22 +- docs/compare.md | 6 +- docs/coverage-levels.md | 2 +- docs/design/derive-from-vendor-config-map.md | 2 +- docs/enforcement-surfaces.md | 13 +- .../emitters/data/managed_bootstrap.txt | 1 + .../compile/emitters/managed/__init__.py | 86 +++- .../compile/emitters/managed/claude_code.py | 133 ++++-- src/chock/compile/emitters/managed/cli.py | 59 ++- src/chock/data/commands.json | 2 +- src/chock/data/managed_channels.json | 85 +++- tests/fixtures/cli_help_golden.txt | 2 +- .../managed_emit/block-no-verify.json | 22 +- tests/fixtures/managed_emit/scan-secrets.json | 35 +- tests/test_managed_emit_claude.py | 380 ++++++++++++++++-- 18 files changed, 718 insertions(+), 162 deletions(-) create mode 100644 src/chock/compile/emitters/data/managed_bootstrap.txt diff --git a/README.md b/README.md index f99eca38..d88463fb 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ file that wiring lives. A row here is support, not installation — coverage is a given repo once `chock sync` has actually written the file, which is why the CLI's own `chock status` always beats this table for what's true *here*. Four of the nine surfaces are absent from this page entirely because they credit no agent today — `managed-setting` is -compiled but not installed, `gateway` is modelled but not yet emitted, `mcp-gateway` credits +written only by `chock emit --managed`, for an admin to deploy, `gateway` is modelled but not yet emitted, `mcp-gateway` credits nothing until its per-client witness ships, and `stop` installs and refuses on eight vendors but is a deliberate backstop for what a pre-tool hook cannot see, so it is worth no grade of its own. Full nine-surface matrix and per-agent caveats: diff --git a/changelog.d/w2b1-managed-emit-claude.md b/changelog.d/w2b1-managed-emit-claude.md index eaf193ae..bd941a10 100644 --- a/changelog.d/w2b1-managed-emit-claude.md +++ b/changelog.d/w2b1-managed-emit-claude.md @@ -1,14 +1,18 @@ -- `chock emit --managed claude_code --out ` (preview) writes, under `/claude_code/` and - nowhere else, a Claude Code managed-settings drop-in (`managed-settings.d/50-chock.json`), the - self-contained hook bundle it runs (`bundle//scripts/`: launcher, runtime, guard or gate), - `deploy.json` (where an admin places each part, what every machine needs, what it does not - promise, the undo step) and `labels.json`. It refuses an `--out` that resolves inside a system - path, never writes into an existing `/claude_code`, elevates nothing and deploys nothing. - Hook commands name the bundle by absolute path (`--deploy-root`, default the vendor's admin - folder + `/chock`); every `PreToolUse` hook carries `onFailure: "block"` and a 45 s timeout. - `--lock` adds `allowManagedHooksOnly` (off by default: it stops every other hook on the machine); +- `chock emit --managed claude_code --python --on-failure block|allow --out ` + (preview) writes, under `/claude_code/` and nowhere else, a Claude Code managed-settings + drop-in (`managed-settings.d/50-chock.json`), the hook bundle it runs (`bundle//scripts/`: + runtime, guard or gate), `deploy.json` (where an admin places each part, what every machine needs, + what the `--on-failure` choice does, what it does not promise, the undo step) and `labels.json`. + Every hook is exec form: it starts the `--python` interpreter directly, so no shell, PATH lookup + or git config picks it. `--on-failure` has no default: `block` sets `onFailure: "block"` on the + `PreToolUse` hooks and refuses a matched call when the interpreter or a bundle file is missing; + `allow` sets neither and lets the call through with a note. The repository stop's every-tool + entry is not emitted. The command refuses an `--out` that is a filesystem root or resolves into + a system folder (any spelling, case-insensitive on macOS and Windows), names a file, or already + holds `claude_code/`; it elevates nothing and deploys nothing. `--lock` adds + `allowManagedHooksOnly` (off by default; `deploy.json` lists every effect the vendor documents); `allowManagedPermissionRulesOnly` is never emitted. Every label is `best-effort (vendor-docs)`, - unwitnessed, until a `managed` witness row exists. A local administrator can still edit or - remove the file. Native `permissions.deny` rules are not emitted yet. + unwitnessed, until a `managed` witness row exists. A local administrator can still edit or remove + the file. Native `permissions.deny` rules are not emitted yet. - `chock compile` no longer writes `managed-setting/managed-settings.json`: that file used a shape Claude Code does not read. diff --git a/docs/architecture.md b/docs/architecture.md index d477262f..6fa48d5e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -26,7 +26,7 @@ supports**, across up to nine [enforcement surfaces](enforcement-surfaces.md): - `pre-tool-use` (nine agents, Claude Code and Cursor among them) and `agent-hooks` (Copilot CLI, VS Code) — agent-native hard controls - `stop` — the end-of-turn backstop that reads what the turn wrote (nine agent names) -- `managed-setting` — compiled, not yet installed +- `managed-setting` — not compiled; `chock emit --managed` writes it for an admin to deploy, and it credits nothing - `gateway` — modeled for budgets/egress (future) It also writes a **coverage report** (`.chock/coverage.json`) mapping every policy × agent onto diff --git a/docs/cli-reference.md b/docs/cli-reference.md index c8196d85..872450b1 100644 --- a/docs/cli-reference.md +++ b/docs/cli-reference.md @@ -163,7 +163,7 @@ chock compile [--repo .] [--targets SURFACE ...] [--agents AGENT ... ``` Compiles one policy into the surfaces each target agent supports (git hook, CI gate, -pre-tool-use, agent-hooks, managed-setting, `AGENTS.md` rule) and updates the per-agent +pre-tool-use, agent-hooks, `AGENTS.md` rule; nothing for `managed-setting`, which only `emit` writes) and updates the per-agent coverage report. `--agents` takes the same comma- or space-separated names as `init` and `sync`, defaults to the repo's `supported_agents`, and rejects unknown names. `sync` runs this for every enabled policy; reach for `compile` directly only when @@ -231,6 +231,26 @@ each built plugin, and needs `--url` (the marketplace repo's own git URL — nev `git remote`). Entries derive from built manifests, never hand-listed; an empty tree exits 2; `--check` reports drift without writing. +### `emit` — write a managed-settings bundle for an admin + +```bash +chock emit --managed claude_code --python --on-failure block|allow --out [--repo .] [--policies a,b] [--platform linux|macos|windows] [--deploy-root ] [--lock] +``` + +Writes, under `/claude_code/` only: `managed-settings.d/50-chock.json` (the selected policies' guards and +gates as exec-form hooks), `bundle/` (each policy's runtime and guard or gate), `deploy.json` (where an +administrator places each part, what each machine needs, what `--on-failure` does, the caveats, the undo step) and +`labels.json`. It deploys nothing, elevates nothing, and refuses an `--out` that is a filesystem root or resolves +into a system folder (the per-OS list in `src/chock/data/managed_channels.json`), names a file, or already holds +`claude_code/`. `--python` is required: the absolute interpreter on the target machines, which every hook starts +directly, so neither PATH nor git config picks it. `--on-failure` is required: `block` sets `onFailure: "block"` on +the `PreToolUse` hooks and refuses a matched call when the interpreter or a bundle file is missing; `allow` sets +neither and lets the call through with a note on stderr. `--lock` also emits `allowManagedHooksOnly`, which blocks +every user, project and other-plugin hook on the machine (`deploy.json` lists every documented effect); +`allowManagedPermissionRulesOnly` is never emitted. Every label is vendor-docs evidence, unwitnessed, at most +best-effort; a local administrator can still edit or remove the file, and a machine that also receives +server-managed or MDM policy skips it by default. + ### `gateway run` -- the MCP gateway proxy `chock gateway run --repo . -- ` wraps one downstream MCP server, refusing matching `tools/call` requests; fails closed (no proxy, no MCP). diff --git a/docs/compare.md b/docs/compare.md index f0af9e3a..1397ec75 100644 --- a/docs/compare.md +++ b/docs/compare.md @@ -9,7 +9,7 @@ The "Chock adds" column rests on: the git hook and CI gate ([Enforcement Surface | Tool | What it is, per its own docs | Where it runs | Stronger than Chock at | Chock adds | | :--- | :--- | :--- | :--- | :--- | | Plain agent hooks ([Claude Code](https://code.claude.com/docs/en/hooks), [Cursor](https://cursor.com/docs/hooks)) | The host's own hook API | In the agent | Everything the host offers, with no layer between | One policy for many agents; a commit and CI floor; a coverage report | -| Managed settings ([Claude Code](https://code.claude.com/docs/en/settings)) | Admin-deployed settings a user cannot override | In the agent, org-wide | Precedence: a user or project cannot override a managed key | Repo-level rules that travel with a clone; commit and CI gates | +| Managed settings ([Claude Code](https://code.claude.com/docs/en/settings)) | Admin-deployed settings, applied above every other settings level | In the agent, org-wide | Precedence over a user's and a project's own settings files; a local administrator can still edit the managed file | Repo-level rules that travel with a clone; commit and CI gates | | [Cupcake](https://github.com/eqtylab/cupcake) | Policy layer for AI agents, OPA Rego compiled to Wasm | In the agent | Richer in-session decisions, Rego, LLM-as-judge, MCP governance | Commit and CI gates; agents without hooks | | [agentjail](https://github.com/LuD1161/agentjail) | Policy checks plus OS sandboxing | In the agent and the OS | An OS sandbox: Landlock, network control | Commit and CI gates; more agents | | [Kastra](https://kastra.ai) | Authorization layer for AI actions | Cloud, self-hosted, air-gap, and on the laptop | Central allow/deny across prompts, tools, shell and APIs; signed audit trail | A local, repo-committed policy and commit and CI gates | @@ -23,7 +23,7 @@ The "Chock adds" column rests on: the git hook and CI gate ([Enforcement Surface - **Chock is not a security boundary.** Guards are pattern filters that quoting, aliases and encodings can evade; Chock is a governance layer, not a sandbox ([Assurance Case](assurance-case.md), [Baseline Policies](baseline-policies.md)). - **Agents without hooks get advice in-session.** They read the compiled `AGENTS.md`; the rules are enforced at commit and in CI, not while the agent works ([Assurance Case](assurance-case.md), [Concepts](concepts.md#coverage-level)). - **The commit gate can be skipped locally** (`--no-verify`); the CI gate is the backstop, and it holds only once it is a required status check ([Enforcement Surfaces](enforcement-surfaces.md)). -- **Chock does not install managed settings.** It compiles a `managed-settings.json` that nothing reads ([Enforcement Surfaces](enforcement-surfaces.md)). +- **Chock does not install managed settings.** `chock emit --managed claude_code` writes a drop-in file and the hook bundle it runs for an administrator to deploy; chock deploys nothing, and its labels are vendor-docs evidence, unwitnessed, best-effort ([CLI reference](cli-reference.md#emit--write-a-managed-settings-bundle-for-an-admin)). - **MCP is thin.** A `tool_call` gate matches tool name and input text and does not understand what a tool does; the `mcp-gateway` credits no agent yet ([Agentic-Risk Coverage](agentic-risk-coverage.md), [Enforcement Surfaces](enforcement-surfaces.md)). ## Plain agent hooks @@ -32,7 +32,7 @@ Claude Code hooks can allow, deny, ask, defer or rewrite a tool's input, and inc ## Managed settings -In Claude Code nothing in a user's own settings files or `--settings` overrides a managed key, and managed settings arrive as a file, an MDM policy or server-managed settings ([settings](https://code.claude.com/docs/en/settings)). Cursor also has enterprise `hooks.json` paths and team hooks synced from a dashboard ([hooks](https://cursor.com/docs/hooks)). Where you control every machine, these are stronger than anything a repository can commit. Chock's rules live in the repo, so they reach contributors whose machines you do not manage ([Why](why.md)). +In Claude Code the vendor documents that a user's own settings files and `--settings` do not override a managed key (a developer with administrator rights on the machine can still edit the managed file), and managed settings arrive as a file, an MDM policy or server-managed settings ([settings](https://code.claude.com/docs/en/settings)). Cursor also has enterprise `hooks.json` paths and team hooks synced from a dashboard ([hooks](https://cursor.com/docs/hooks)). Where you control every machine, these are stronger than anything a repository can commit. Chock's rules live in the repo, so they reach contributors whose machines you do not manage ([Why](why.md)). ## Cupcake diff --git a/docs/coverage-levels.md b/docs/coverage-levels.md index 4ffb9469..a7ff6d0d 100644 --- a/docs/coverage-levels.md +++ b/docs/coverage-levels.md @@ -85,7 +85,7 @@ number comparing them would invent a scale that does not exist. > [Arming a fresh clone](adopting.md#arming-a-fresh-clone). Example for `protect-main-branch`, whose gate declares `on: [commit, push]` and so targets -git-hook + CI + ambient-rule + managed-setting, with no pre-tool surface at all. Every agent +git-hook + CI + ambient-rule + managed-setting (for which the compiler writes nothing), with no pre-tool surface at all. Every agent reaches the same grade, because the git hook is what earns it: ```json diff --git a/docs/design/derive-from-vendor-config-map.md b/docs/design/derive-from-vendor-config-map.md index b4c77838..e4600b83 100644 --- a/docs/design/derive-from-vendor-config-map.md +++ b/docs/design/derive-from-vendor-config-map.md @@ -31,7 +31,7 @@ Source-of-truth vocabulary: | VS Code shell matcher `"bash\|powershell\|pwsh\|sh\|shell"` | `agent_hooks.py:11` | DA | vendor tool vocabulary; **gap**: agentseam's `vscode_copilot` records no `SHELL_TOOLS` today **[v]** — chock's witnessed matcher is the evidence D2 should ingest | | interpreter-discovery bash/powershell templates | `agent_hooks.py:14-31` | CC | chock policy (find python, refuse silently-missing); the powershell variant should reuse agentseam `adapters/_windows.py` rather than a second copy | | VS Code entry keys `type/matcher/timeout/timeoutSec/bash/command/powershell/windows` | `agent_hooks.py:45-54` | DA | `hook_entry` `entry_extra`; **divergence** — agentseam emits `{type, command, windows}` (`adapters/vscode_copilot.py:185-193`); see M.5(4) | -| Claude managed-settings fragment schema | `claude_managed.py:14-40` | CC | claude-only surface, policy-conditional content; no config equivalent upstream | +| Claude managed-settings fragment schema | `claude_managed.py` (removed in 1.2.0; `chock emit --managed` writes the drop-in, `emitters/managed/claude_code.py`) | CC | claude-only surface, policy-conditional content; no config equivalent upstream | | `ambient` / `ci` / `git_hook` / `mcp_gateway` emitters | whole files | CC | agent-agnostic **[v]** (no vendor token in any of them) | ### M.2 `hooks/installers.py`, `hooks/*_install.py` diff --git a/docs/enforcement-surfaces.md b/docs/enforcement-surfaces.md index d820b2bb..0f6f679e 100644 --- a/docs/enforcement-surfaces.md +++ b/docs/enforcement-surfaces.md @@ -132,11 +132,12 @@ per gateway process; wrap N servers with N entries. > clone is not bricked: the launcher finds one or refuses with a fix-it message. The guard > covers shell commands (`beforeShellExecution`); other tool classes are not intercepted. -> **`managed-setting` is compiled but not installed.** The compiler writes -> `.chock/compiled//managed-setting/managed-settings.json` and nothing reads it — there is -> no installer, so it enforces nothing today. It is excluded from `INSTALLED_SURFACES` in -> `surfaces.py`, so no policy can raise a coverage claim through it. Read the row below as *what the -> agent could support*, not as something switched on. +> **`managed-setting` is neither compiled nor installed.** The compiler writes nothing for it. +> `chock emit --managed claude_code` writes a managed-settings drop-in and the hook bundle it runs +> under a folder you name, for an administrator to deploy ([CLI reference](cli-reference.md#emit--write-a-managed-settings-bundle-for-an-admin)); +> chock deploys nothing. It is excluded from `INSTALLED_SURFACES` in `surfaces.py`, so no policy can +> raise a coverage claim through it, and the emitted labels are vendor-docs evidence, unwitnessed, +> best-effort. Read the row below as *what the agent could support*, not as something switched on. > **`ci-gate` needs `chock sync --ci` to actually run.** The compiler always writes > `.chock/compiled//ci-gate/gate.json` and `step.yaml`, but nothing invokes them until @@ -195,7 +196,7 @@ evidence cap that bounds every grade -- is on its own page: **[Coverage Levels]( ## How a policy picks its surfaces A hook that must stop a command targets `git-hook` + `ci-gate` (the universal floor) and, where available, -`pre-tool-use` + `managed-setting`. A hook whose `on:` includes `tool_use` is compiled to the `pre-tool-use` +`pre-tool-use` (Claude Code's target list also names `managed-setting`, for which the compiler writes nothing). A hook whose `on:` includes `tool_use` is compiled to the `pre-tool-use` surface on agents that support it (thirteen of them, Claude Code and Cursor among them; Copilot CLI and VS Code get the same guard via `agent-hooks`, and a content gate's write check as a `PreToolUse` entry over `Edit|Write` there). A hook whose `on:` includes `tool_call` (a `content_regex` or `script` gate with `params.tools`, globs diff --git a/src/chock/compile/emitters/data/managed_bootstrap.txt b/src/chock/compile/emitters/data/managed_bootstrap.txt new file mode 100644 index 00000000..4c56ada1 --- /dev/null +++ b/src/chock/compile/emitters/data/managed_bootstrap.txt @@ -0,0 +1 @@ +import sys,os;r=sys.argv[1];m=[p for p in (r,os.path.join(os.path.dirname(r),'_chock_runtime.py')) if not os.path.isfile(p)];o=sys.version_info<(__MIN_PYTHON__,);w='chock: '+('this hook needs Python __MIN_PYTHON_TEXT__+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; __OUTCOME__\n';(o or m) and (sys.stderr.write(w),sys.exit(__EXIT__));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__') diff --git a/src/chock/compile/emitters/managed/__init__.py b/src/chock/compile/emitters/managed/__init__.py index 885b4efe..ff65f056 100644 --- a/src/chock/compile/emitters/managed/__init__.py +++ b/src/chock/compile/emitters/managed/__init__.py @@ -4,6 +4,7 @@ import json import os +import posixpath import re import sys from pathlib import Path, PurePosixPath @@ -15,7 +16,6 @@ from chock.compile.levels import Grade, capped, in_agent_level, render_grade from chock.compile.surface_kinds import Surface from chock.emit import write_generated -from chock.hooks.launch import QUOTED_UNSAFE from chock.resources import package_data_dir CHANNELS: dict[str, Any] = json.loads( @@ -26,7 +26,15 @@ #: Every managed label rests on the vendor's own docs until a `managed` witness row says otherwise. BASIS = matrix_terms.BASIS_DOCS WINDOWS = "windows" +MACOS = "macos" _WINDOWS_ROOT = re.compile(r"[A-Za-z]:/[^/].*") +#: `//host/c$/...`, an administrative share of a drive. +_ADMIN_SHARE = re.compile(r"^//[^/]+/([a-z])\$(?=/|$)", re.I) +#: `\\?\` and `\\.\` device prefixes, and their UNC form, in forward-slash spelling. +_DEVICE_UNC = re.compile(r"^//[?.]/unc/", re.I) +_DEVICE = re.compile(r"^//[?.]/") +_DRIVE_ROOT = re.compile(r"[a-z]:/?") +_CONTROL = re.compile(r"[\r\n\0]") #: Windows system folders that may sit on another drive than C:. _WINDOWS_ENV_ROOTS = ("ProgramFiles", "ProgramFiles(x86)", "ProgramData", "SystemRoot") @@ -46,7 +54,7 @@ def host_platform() -> str: """This machine's platform, in the data file's words.""" if sys.platform == "win32": return WINDOWS - return "macos" if sys.platform == "darwin" else "linux" + return MACOS if sys.platform == "darwin" else "linux" def _system_roots(platform: str) -> list[str]: @@ -56,27 +64,50 @@ def _system_roots(platform: str) -> list[str]: return roots +def spelled(text: str, platform: str) -> str: + """`text` in one comparable spelling: Windows device and admin-share prefixes removed, case folded where the OS ignores it.""" + if platform != WINDOWS: + text = re.sub(r"^/+", "/", text) + return text.casefold() if platform == MACOS else text + text = text.replace("\\", "/") + text = _DEVICE_UNC.sub("//", text) + text = _DEVICE.sub("", text) + text = _ADMIN_SHARE.sub(lambda m: f"{m.group(1)}:/", text) + return re.sub(r"/+", "/", text).casefold() + + +def _inside(text: str, root: str) -> bool: + base = root.rstrip("/") + if not base or _DRIVE_ROOT.fullmatch(base): # a filesystem root holds everything: refuse it alone + return text.rstrip("/") == base + return text == base or text.startswith(f"{base}/") + + +def root_named(text: str, platform: str) -> str | None: + """The system path the absolute, resolved `text` names or sits inside, on `platform`, else None.""" + text = spelled(text, platform) + if platform == WINDOWS and _DRIVE_ROOT.fullmatch(text): + return text + return next((root for root in _system_roots(platform) if _inside(text, spelled(root, platform))), None) + + def system_root_of(path: Path, platform: str | None = None) -> str | None: """The system path `path` resolves inside (symlinks followed), else None.""" - platform = platform or host_platform() - fold = str.casefold if platform == WINDOWS else str - text = fold(Path(path).resolve().as_posix()) - for root in _system_roots(platform): - base = fold(root.rstrip("/")) - if text == base or text.startswith(f"{base}/"): - return root - return None + return root_named(Path(path).resolve().as_posix(), platform or host_platform()) def vendor_out(out: Path, vendor: str) -> Path: - """`/`, refused when `out` resolves into a system path or the folder already exists.""" + """`/`, refused when `out` resolves into a system path, is not a folder, or the folder already exists.""" root = system_root_of(out) if root is not None: msg = ( - f"--out {out} resolves inside the system path {root}; chock emit writes only to a folder you " + f"--out {out} resolves to or inside the system path {root}; chock emit writes only to a folder you " "deploy from, never to where a client reads it" ) raise EmitRefusedError(msg) + if os.path.lexists(out) and not Path(out).is_dir(): + msg = f"--out {out} is not a folder" + raise EmitRefusedError(msg) dest = Path(out).resolve() / vendor if os.path.lexists(dest): msg = f"{dest} already exists; give a new --out or remove that folder first" @@ -84,17 +115,34 @@ def vendor_out(out: Path, vendor: str) -> Path: return dest +def _absolute(text: str, platform: str) -> str | None: + """`text` as a normalised absolute `platform` path in forward slashes, else None; a line break or NUL is refused.""" + path = (text.replace("\\", "/") if platform == WINDOWS else text).rstrip("/") + absolute = _WINDOWS_ROOT.fullmatch(path) if platform == WINDOWS else len(path) > 1 and path.startswith("/") + if not absolute or _CONTROL.search(path): + return None + return posixpath.normpath(path) if platform != WINDOWS else path[:2] + posixpath.normpath(path[2:]) + + def deploy_root(text: str, platform: str) -> str: - """The absolute folder the admin will copy the bundle to, in forward-slash form; a shell-read character is refused.""" - root = (text.replace("\\", "/") if platform == WINDOWS else text).rstrip("/") - absolute = _WINDOWS_ROOT.fullmatch(root) if platform == WINDOWS else len(root) > 1 and root.startswith("/") - if not absolute or QUOTED_UNSAFE.search(root): + """The absolute folder the admin will copy the bundle to, normalised, in forward-slash form.""" + root = _absolute(text, platform) + if root is None: + msg = f"--deploy-root {text!r} must be an absolute {platform} folder with no line break" + raise EmitRefusedError(msg) + return root + + +def interpreter(text: str, platform: str) -> str: + """The absolute Python the hooks start on `platform`; nothing on PATH or in git config is consulted.""" + path = _absolute(text, platform) + if path is None: msg = ( - f"--deploy-root {text!r} must be an absolute {platform} folder holding no quote, $, backquote, " - "backslash or line break: the hook commands name it inside double quotes" + f"--python {text!r} must be the absolute path of the Python 3.11+ interpreter on the target {platform} " + "machines: a managed hook must not take its interpreter from the user's PATH or git config" ) raise EmitRefusedError(msg) - return root + return path def write_tree(dest: Path, files: Files) -> list[Path]: diff --git a/src/chock/compile/emitters/managed/claude_code.py b/src/chock/compile/emitters/managed/claude_code.py index faffa7bc..5e591c0f 100644 --- a/src/chock/compile/emitters/managed/claude_code.py +++ b/src/chock/compile/emitters/managed/claude_code.py @@ -1,17 +1,20 @@ -"""Claude Code's managed drop-in: the plugin route's hooks, rooted at an absolute deploy folder, failing closed.""" +"""Claude Code's managed drop-in: the plugin route's hooks in exec form, run by the admin's interpreter from an absolute deploy folder.""" from __future__ import annotations import json +import shlex from pathlib import Path, PurePosixPath from typing import Any from agentseam import packaging from chock.compile.emitters import managed +from chock.hooks import launch from chock.hooks.stop_check import STOP_CHECK_FLAG from chock.plugin.claude import claude_plugin_files from chock.plugin.multi_plugin import SCRIPTS_DIR +from chock.resources import package_data_dir VENDOR = "claude_code" AGENT = "claude" @@ -19,89 +22,145 @@ _HOOKS_REL = Path(packaging.supports(VENDOR, packaging.HOOKS)) #: The plugin-root token the plugin route's commands carry, swapped for the deploy folder. ROOT_TOKEN = str(packaging.executable_ref(VENDOR, "x")).removesuffix("/x") +#: What every plugin-route command starts with: its sh launcher, which the managed file never runs. +_PLUGIN_PREFIX = launch.plugin_interpreter(f"{ROOT_TOKEN}/{SCRIPTS_DIR}/{launch.PLUGIN_LAUNCHER}") + " " _FACTS = managed.facts(VENDOR) _FAIL = _FACTS["fail_block"] -#: The halves a plugin hook runs, by the flag its command carries. -_HALF_FLAGS = {"guard": " --guard ", "gate": " --gate "} +#: --on-failure choices; `block` refuses what cannot be checked, `allow` lets it through with a note. +BLOCK, ALLOW = "block", "allow" +ON_FAILURE = (BLOCK, ALLOW) +_OUTCOME = { + BLOCK: ("refusing this call (--on-failure block)", 2), + ALLOW: ("this call goes ahead unchecked (--on-failure allow)", 0), +} +_BOOTSTRAP = ( + package_data_dir("chock", "compile", "emitters", "data") + .joinpath("managed_bootstrap.txt") + .read_text(encoding="utf-8") + .strip() + .replace("__MIN_PYTHON_TEXT__", ".".join(map(str, launch.MIN_PYTHON))) + .replace("__MIN_PYTHON__", ", ".join(map(str, launch.MIN_PYTHON))) +) +#: The halves a plugin hook runs, by the flag its argv carries. +_HALF_FLAGS = {"guard": "--guard", "gate": "--gate"} -def _is_stop_check(group: dict[str, Any]) -> bool: - return all(hook["command"].endswith(f" {STOP_CHECK_FLAG}") for hook in group["hooks"]) +def bootstrap(on_failure: str) -> str: + """The `-c` program that starts a bundled runtime, or, when it or Python 3.11+ is missing, says so and exits per `on_failure`.""" + outcome, code = _OUTCOME[on_failure] + return _BOOTSTRAP.replace("__OUTCOME__", outcome).replace("__EXIT__", str(code)) + + +def _argv(command: str) -> list[str]: + """The runtime path and flags of a plugin-route command, its sh launcher prefix dropped.""" + if not command.startswith(_PLUGIN_PREFIX): + msg = f"{command!r} does not start with the plugin launcher this emitter replaces" + raise managed.EmitRefusedError(msg) + return shlex.split(command.removeprefix(_PLUGIN_PREFIX)) + + +def _exec_hook(hook: dict[str, Any], event: str, root: str, python: str, on_failure: str) -> dict[str, Any]: + """`hook` in exec form: `python` runs the bootstrap on the runtime rooted at `root`; onFailure only where it acts.""" + argv = [arg.replace(ROOT_TOKEN, root) for arg in _argv(hook["command"])] + if any("${" in arg for arg in argv): + msg = f"{argv!r} still names a client token after rooting it at {root}" + raise managed.EmitRefusedError(msg) + blocks = on_failure == BLOCK and event in _FAIL["events"] + entry = { + "type": hook["type"], + "command": python, + "args": [*_FACTS["python_flags"], bootstrap(on_failure if blocks else ALLOW), *argv], + } + entry.update({key: value for key, value in hook.items() if key not in {"type", "command"}}) + if blocks: + entry[_FAIL["key"]] = _FAIL["value"] + return entry -def _rooted(group: dict[str, Any], event: str, root: str) -> dict[str, Any]: - """`group` with its plugin root swapped for `root` and, where the docs say it acts, onFailure set.""" - hooks = [] - for hook in group["hooks"]: - command = hook["command"].replace(ROOT_TOKEN, root) - if "${" in command: - msg = f"{command!r} still names a client token after rooting it at {root}" - raise managed.EmitRefusedError(msg) - rooted = {**hook, "command": command} - if event in _FAIL["events"]: - rooted[_FAIL["key"]] = _FAIL["value"] - hooks.append(rooted) - return {**group, "hooks": hooks} +def _is_stop_check(group: dict[str, Any]) -> bool: + return all(hook["command"].endswith(f" {STOP_CHECK_FLAG}") for hook in group["hooks"]) def _policy_parts(policy_dir: Path, manifest: dict[str, Any], repo_root: Path) -> tuple[dict[str, Any], dict] | None: - """The plugin route's hooks and scripts for one policy, or None when it runs nothing.""" + """The plugin route's hooks and scripts for one policy, or None when it runs nothing; its sh launcher is left out.""" files = claude_plugin_files(policy_dir, manifest, repo_root, folded=False) if _HOOKS_REL not in files: return None - scripts = {PurePosixPath(rel.as_posix()): text for rel, text in files.items() if rel.parts[0] == SCRIPTS_DIR} + scripts = { + PurePosixPath(rel.as_posix()): text + for rel, text in files.items() + if rel.parts[0] == SCRIPTS_DIR and rel.name != launch.PLUGIN_LAUNCHER + } return json.loads(files[_HOOKS_REL])["hooks"], scripts def build( - policies: list[tuple[str, Path, dict[str, Any]]], repo_root: Path, root: str, *, lock: bool = False + policies: list[tuple[str, Path, dict[str, Any]]], repo_root: Path, options: dict[str, Any] ) -> tuple[managed.Files, list[dict[str, Any]], list[str]]: - """(files under `/claude_code`, one label per emitted policy, the policies with nothing to run).""" + """(files under `/claude_code`, one label per emitted policy, the policies with nothing to run). + + `options`: root (deploy folder), python (absolute interpreter), on_failure, lock. The repo stop's matcher-less + stop check is never emitted: no policy needs it, and it would run on every tool call. + """ events: dict[str, list[dict[str, Any]]] = {} files: managed.Files = {} labels, skipped = [], [] - stop_check_kept = False for policy_id, policy_dir, manifest in policies: parts = _policy_parts(policy_dir, manifest, repo_root) if parts is None: skipped.append(policy_id) continue hooks, scripts = parts - commands = [] + flags = set() for event, groups in hooks.items(): - for group in groups: - if _is_stop_check(group): - if stop_check_kept: - continue - stop_check_kept = True - events.setdefault(event, []).append(_rooted(group, event, f"{root}/{policy_id}")) - commands += [hook["command"] for hook in group["hooks"]] + for group in (group for group in groups if not _is_stop_check(group)): + rooted = [ + _exec_hook(hook, event, f"{options['root']}/{policy_id}", options["python"], options["on_failure"]) + for hook in group["hooks"] + ] + events.setdefault(event, []).append({**group, "hooks": rooted}) + flags.update(arg for hook in rooted for arg in hook["args"]) for rel, text in scripts.items(): files[PurePosixPath(BUNDLE, policy_id, rel)] = text - halves = [half for half, flag in _HALF_FLAGS.items() if any(flag in command for command in commands)] - labels.append(managed.label(policy_id, VENDOR, AGENT, halves)) + labels.append( + managed.label(policy_id, VENDOR, AGENT, [half for half, flag in _HALF_FLAGS.items() if flag in flags]) + ) document: dict[str, Any] = {"hooks": events} - if lock: + if options["lock"]: document[_FACTS["lock_key"]] = True files[PurePosixPath(_FACTS["drop_in"])] = managed.as_json(document) return files, labels, skipped -def deploy_plan(platform: str, root: str, *, lock: bool) -> dict[str, Any]: - """Where an admin places each part, what it needs, what it does not promise, and how to take it out.""" +def matchers(document: dict[str, Any]) -> list[str]: + """The tool matchers of the emitted pre-tool groups, in order, each once.""" + found = (group.get("matcher") for group in document["hooks"].get("PreToolUse", [])) + return list(dict.fromkeys(matcher for matcher in found if matcher)) + + +def deploy_plan(platform: str, options: dict[str, Any], tools: list[str]) -> dict[str, Any]: + """Where an admin places each part, what it needs, what the failure choice does, what it does not promise, and the undo.""" + root, python, lock = options["root"], options["python"], options["lock"] admin = _FACTS["admin_dir"][platform] place = [ {"from": _FACTS["drop_in"], "to": f"{admin}/{_FACTS['drop_in']}"}, {"from": f"{BUNDLE}/", "to": f"{root}/"}, ] + consequence = ( + _FACTS["on_failure"][options["on_failure"]] + .replace("__PYTHON__", python) + .replace("__MATCHERS__", ", ".join(tools)) + ) plan: dict[str, Any] = { "vendor": VENDOR, "platform": platform, "deployed": False, "basis": managed.BASIS, + "python": python, + "on_failure": {"choice": options["on_failure"], "consequence": consequence}, "place": place, "undo": [f"delete {step['to']}" for step in place], - "requirements": _FACTS["requirements"], + "requirements": [line.replace("__PYTHON__", python) for line in _FACTS["requirements"]], "caveats": _FACTS["caveats"], "never_emitted": _FACTS["never_emitted"], "notes": [_FACTS["never_note"], *([_FACTS["lock_note"]] if lock else [])], diff --git a/src/chock/compile/emitters/managed/cli.py b/src/chock/compile/emitters/managed/cli.py index c366c675..c827cafd 100644 --- a/src/chock/compile/emitters/managed/cli.py +++ b/src/chock/compile/emitters/managed/cli.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse +import json import sys from pathlib import Path, PurePosixPath from typing import Any @@ -25,8 +26,10 @@ def _parser() -> argparse.ArgumentParser: prog="chock emit", description=( "Write one vendor's managed-settings file, the self-contained hook bundle it runs, deploy.json " - "(where an admin places each part, what it needs, how to undo it) and labels.json, all under --out. " - "It writes no system path, elevates nothing and deploys nothing. Every label is vendor-docs " + "(where an admin places each part, what it needs, what --on-failure does, how to undo it) and " + "labels.json, all under --out. It refuses an --out that is a filesystem root or resolves into a " + "system folder (per OS: /etc, /usr, /var, /Library, C:/Program Files, C:/Windows and the others in " + "chock's managed_channels.json); it elevates nothing and deploys nothing. Every label is vendor-docs " "evidence, unwitnessed, at most best-effort; a local administrator can still edit or remove the file." ), ) @@ -38,7 +41,21 @@ def _parser() -> argparse.ArgumentParser: "--repo", type=Path, default=Path(), help="the repository whose policies are emitted (default: .)" ) parser.add_argument( - "--policies", help="comma-separated policy ids (default: every enabled policy with a guard or gate)" + "--policies", + help="comma-separated policy ids, named ones emitted even if the config disables them " + "(default: every enabled policy with a guard or gate)", + ) + parser.add_argument( + "--python", + required=True, + help="absolute path of the Python 3.11+ interpreter on the target machines; the hooks start it directly", + ) + parser.add_argument( + "--on-failure", + required=True, + choices=claude_code.ON_FAILURE, + help="when the hook cannot run (no interpreter, a missing bundle file, a crash or timeout): block refuses " + "the matched tool call, allow lets it through with a note", ) parser.add_argument("--platform", choices=managed.PLATFORMS, help="the OS the file is for (default: this one)") parser.add_argument( @@ -55,7 +72,10 @@ def _parser() -> argparse.ArgumentParser: def _manifest(policy_dir: Path) -> dict[str, Any]: warnings: list[str] = [] - result = load_manifest(policy_dir, warnings=warnings) + try: + result = load_manifest(policy_dir, warnings=warnings) + except UnicodeDecodeError as exc: + raise managed.EmitRefusedError(_not_utf8(policy_dir, exc)) from exc if result is None: return {} data, path = result @@ -63,9 +83,19 @@ def _manifest(policy_dir: Path) -> dict[str, Any]: return data +def _not_utf8(where: Path, exc: UnicodeDecodeError) -> str: + return f"a file of {where} is not UTF-8 text (byte {exc.start}: {exc.reason})" + + def _selected(repo: Path, wanted: str | None) -> list[tuple[str, Path, dict[str, Any]]]: """(id, folder, manifest) of each policy to emit: the named ones, else every one the config leaves enabled.""" + if not repo.is_dir(): + msg = f"--repo {repo} is not a folder" + raise managed.EmitRefusedError(msg) found = {path.name: path for path in discover_policy_dirs(repo)} + if not found: + msg = f"no policy under {repo / '.agents' / 'policies'}" + raise managed.EmitRefusedError(msg) names = [name.strip() for name in wanted.split(",") if name.strip()] if wanted else sorted(found) missing = [name for name in names if name not in found] if missing: @@ -92,6 +122,7 @@ def _report(dest: Path, plan: dict[str, Any], labels: list[dict[str, Any]], skip f" {row['policy']}: {row['label']}, {'witnessed' if row['witnessed'] else 'unwitnessed'}" for row in labels ] lines += [f" skipped {policy_id}: no shell guard or write gate to run" for policy_id in skipped] + lines.append(f" {plan['on_failure']['consequence']}") lines += [f" {note}" for note in plan["notes"]] lines.append(f" {DEPLOY_FILE} lists what each machine needs, what this does not promise, and the undo step.") return "\n".join(lines) + "\n" @@ -102,15 +133,25 @@ def emit(args: argparse.Namespace) -> str: builder = BUILDERS[args.managed] facts = managed.facts(args.managed) platform = args.platform or managed.host_platform() - root = managed.deploy_root(args.deploy_root or facts["deploy_root"][platform], platform) + options = { + "root": managed.deploy_root(args.deploy_root or facts["deploy_root"][platform], platform), + "python": managed.interpreter(args.python, platform), + "on_failure": args.on_failure, + "lock": args.lock, + } dest = managed.vendor_out(args.out, args.managed) repo = args.repo.resolve() - files, labels, skipped = builder.build(_selected(repo, args.policies), repo, root, lock=args.lock) + selected = _selected(repo, args.policies) + try: + files, labels, skipped = builder.build(selected, repo, options) + except UnicodeDecodeError as exc: + raise managed.EmitRefusedError(_not_utf8(repo / ".agents" / "policies", exc)) from exc if not labels: msg = "no selected policy has a shell guard or a write gate, so there is nothing for a managed hook to run" raise managed.EmitRefusedError(msg) - plan = builder.deploy_plan(platform, root, lock=args.lock) - plan["deploy_root"] = root + tools = builder.matchers(json.loads(files[PurePosixPath(facts["drop_in"])])) + plan = builder.deploy_plan(platform, options, tools) + plan["deploy_root"] = options["root"] files[PurePosixPath(DEPLOY_FILE)] = managed.as_json(plan) files[PurePosixPath(LABELS_FILE)] = managed.as_json(labels) managed.write_tree(dest, files) @@ -121,7 +162,7 @@ def main(argv: list[str] | None = None) -> int: args = _parser().parse_args(argv) try: report = emit(args) - except managed.EmitRefusedError as exc: + except (managed.EmitRefusedError, OSError) as exc: error(f"chock emit: {exc}") return 1 sys.stdout.write(legible(report)) diff --git a/src/chock/data/commands.json b/src/chock/data/commands.json index ae49e44e..58a0f72c 100644 --- a/src/chock/data/commands.json +++ b/src/chock/data/commands.json @@ -87,7 +87,7 @@ }, "emit": { "module": "chock.compile.emitters.managed.cli", - "help": "Write a vendor's managed-settings bundle under --out for an admin to deploy (emit --managed claude_code --out ); writes no system path" + "help": "Write a vendor's managed-settings bundle under --out for an admin to deploy (emit --managed claude_code --python --on-failure block|allow --out ); refuses an --out that is a filesystem root or inside a system folder" }, "install-skills": { "module": "chock.scaffold.skills", diff --git a/src/chock/data/managed_channels.json b/src/chock/data/managed_channels.json index c32b4c1e..9be65bf8 100644 --- a/src/chock/data/managed_channels.json +++ b/src/chock/data/managed_channels.json @@ -1,8 +1,50 @@ { "system_roots": { - "linux": ["/etc", "/usr", "/bin", "/sbin", "/lib", "/boot", "/opt"], - "macos": ["/Library", "/System", "/etc", "/private/etc", "/usr", "/bin", "/sbin", "/opt"], - "windows": ["C:/Program Files", "C:/Program Files (x86)", "C:/ProgramData", "C:/Windows"] + "linux": [ + "/", + "/etc", + "/usr", + "/bin", + "/sbin", + "/lib", + "/lib32", + "/lib64", + "/libx32", + "/boot", + "/opt", + "/var", + "/srv", + "/run", + "/sys", + "/proc", + "/dev", + "/root" + ], + "macos": [ + "/", + "/Library", + "/System", + "/Applications", + "/etc", + "/private/etc", + "/usr", + "/bin", + "/sbin", + "/opt", + "/cores", + "/dev", + "/var/root", + "/private/var/root", + "/var/db", + "/private/var/db" + ], + "windows": [ + "C:/", + "C:/Program Files", + "C:/Program Files (x86)", + "C:/ProgramData", + "C:/Windows" + ] }, "vendors": { "claude_code": { @@ -23,23 +65,46 @@ "macos": "/Library/Application Support/ClaudeCode/chock", "windows": "C:/Program Files/ClaudeCode/chock" }, - "fail_block": {"key": "onFailure", "value": "block", "events": ["PreToolUse"], "min_client": "2.1.295"}, + "fail_block": { + "key": "onFailure", + "value": "block", + "events": [ + "PreToolUse" + ], + "min_client": "2.1.295" + }, + "python_flags": [ + "-I", + "-X", + "utf8", + "-c" + ], "lock_key": "allowManagedHooksOnly", - "never_emitted": ["allowManagedPermissionRulesOnly"], + "never_emitted": [ + "allowManagedPermissionRulesOnly" + ], + "on_failure": { + "block": "--on-failure block: every tool call these hooks match (__MATCHERS__) is refused on a machine where a bundle file is missing or __PYTHON__ is older than Python 3.11 (the hook exits 2, on any client version), and, on Claude Code 2.1.295 or later, where __PYTHON__ is missing or cannot start, or the hook crashes or times out.", + "allow": "--on-failure allow: every tool call these hooks match (__MATCHERS__) goes ahead unchecked on a machine where __PYTHON__ is missing, older than Python 3.11 or cannot start, or a bundle file is missing, and whenever the hook crashes or times out; the note is the hook's own stderr, or Claude Code's hook-error notice when the interpreter cannot start." + }, "requirements": [ - "Every machine needs git, a POSIX sh (git's) and Python 3.11 or later on PATH. Without them, onFailure \"block\" refuses every matched tool call on that machine.", + "Every machine needs Python 3.11 or later at __PYTHON__ exactly: the hook commands start that file directly (exec form, no shell), so neither PATH, git config nor a shell picks the interpreter.", "onFailure needs Claude Code 2.1.295 or later; an older client lets a crashed or timed-out hook through.", - "The bundle must sit at deploy_root exactly: the hook commands name it by absolute path." + "The bundle must sit at deploy_root exactly: the hook commands name it by absolute path.", + "The bundle folder and __PYTHON__ must be owned by an administrator and not writable by users: whoever can write them can replace the guard. The vendor docs state no ownership rule for the files a managed hook runs; on who can change the managed source they say \"a developer who is an administrator on the machine can edit the managed source itself\"." ], "caveats": [ "Managed settings bind Claude Code only. A local administrator can edit or delete these files.", "On WSL, /etc/claude-code is writable by the user as well.", "By default (managedSourcesBehavior \"first-wins\") a machine that also receives server-managed or MDM policy skips the managed settings files without a warning; /status names the source in use.", - "onFailure has no effect on Stop hooks, so the turn-end gate fails open when it crashes.", + "onFailure has no effect on Stop hooks, so the turn-end gate lets the turn end when it cannot run, whatever --on-failure says.", + "A repository holding .chock/stop.json (valid or not) makes these hooks refuse every tool call they match while Claude Code works in that repository: the bundled runtime honours chock's repository stop.", + "The bundled runtime runs git from PATH for repository facts (the stop files, the turn-end gate's changed files); the guards' verdicts on a command do not depend on it.", + "Hooks write their decision log under the user's home folder (~/.chock/log).", "A file in managed-settings.d that is not valid JSON stops Claude Code from starting.", - "The labels are vendor-docs evidence, unwitnessed: nobody has watched this file refuse a call yet." + "The labels are vendor-docs evidence, unwitnessed, best-effort: nobody has watched this file refuse a call yet." ], - "lock_note": "--lock adds allowManagedHooksOnly: every user, project and plugin hook on the machine stops running, chock's own project hooks and plugins included.", + "lock_note": "--lock adds allowManagedHooksOnly. The vendor docs list what it changes on the machine: only managed hooks, Agent SDK hooks and hooks of plugins the managed settings force-enable run; user, project and local hooks, hooks and mods of other installed plugins (chock's own project hooks and plugins included) and hooks in agent frontmatter are blocked; monitors of other plugins do not start; plugins with a command source are disabled and marketplace headersHelper commands are blocked unless disableCommandPluginSources is set to false; the status line and file suggestion are read from managed settings only; the /goal command cannot run.", "never_note": "allowManagedPermissionRulesOnly is never emitted: it would discard every user and project permission rule." } } diff --git a/tests/fixtures/cli_help_golden.txt b/tests/fixtures/cli_help_golden.txt index 92e8ff1a..16269cf4 100644 --- a/tests/fixtures/cli_help_golden.txt +++ b/tests/fixtures/cli_help_golden.txt @@ -22,7 +22,7 @@ Everyday: Authoring: new Create a deterministic artifact skeleton compile Low-level single-policy compile - emit Write a vendor's managed-settings bundle under --out for an admin to deploy (emit --managed claude_code --out ); writes no system path + emit Write a vendor's managed-settings bundle under --out for an admin to deploy (emit --managed claude_code --python --on-failure block|allow --out ); refuses an --out that is a filesystem root or inside a system folder install-skills Install bundled authoring skills into agent skill dirs (write mode; check via CI) registry Scan/list/resolve the artifact registry plugin Package policies as installable plugins (plugin build [--format claude] [--check]) diff --git a/tests/fixtures/managed_emit/block-no-verify.json b/tests/fixtures/managed_emit/block-no-verify.json index fc1a5471..c34902e6 100644 --- a/tests/fixtures/managed_emit/block-no-verify.json +++ b/tests/fixtures/managed_emit/block-no-verify.json @@ -6,17 +6,17 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/block-no-verify/scripts/launch.sh\" \"/srv/chock-managed/block-no-verify/scripts/claude_code.py\" --guard \"/srv/chock-managed/block-no-verify/scripts/block-no-verify.py\"", - "timeout": 45, - "onFailure": "block" - } - ] - }, - { - "hooks": [ - { - "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/block-no-verify/scripts/launch.sh\" \"/srv/chock-managed/block-no-verify/scripts/claude_code.py\" --stop-check", + "command": "/usr/bin/python3", + "args": [ + "-I", + "-X", + "utf8", + "-c", + "import sys,os;r=sys.argv[1];m=[p for p in (r,os.path.join(os.path.dirname(r),'_chock_runtime.py')) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; refusing this call (--on-failure block)\\n';(o or m) and (sys.stderr.write(w),sys.exit(2));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", + "/srv/chock-managed/block-no-verify/scripts/claude_code.py", + "--guard", + "/srv/chock-managed/block-no-verify/scripts/block-no-verify.py" + ], "timeout": 45, "onFailure": "block" } diff --git a/tests/fixtures/managed_emit/scan-secrets.json b/tests/fixtures/managed_emit/scan-secrets.json index f24bc2b9..8f6b835f 100644 --- a/tests/fixtures/managed_emit/scan-secrets.json +++ b/tests/fixtures/managed_emit/scan-secrets.json @@ -6,17 +6,17 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/scan-secrets/scripts/launch.sh\" \"/srv/chock-managed/scan-secrets/scripts/claude_code.py\" --gate \"/srv/chock-managed/scan-secrets/scripts/gate.json\"", - "timeout": 45, - "onFailure": "block" - } - ] - }, - { - "hooks": [ - { - "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/scan-secrets/scripts/launch.sh\" \"/srv/chock-managed/scan-secrets/scripts/claude_code.py\" --stop-check", + "command": "/usr/bin/python3", + "args": [ + "-I", + "-X", + "utf8", + "-c", + "import sys,os;r=sys.argv[1];m=[p for p in (r,os.path.join(os.path.dirname(r),'_chock_runtime.py')) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; refusing this call (--on-failure block)\\n';(o or m) and (sys.stderr.write(w),sys.exit(2));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", + "/srv/chock-managed/scan-secrets/scripts/claude_code.py", + "--gate", + "/srv/chock-managed/scan-secrets/scripts/gate.json" + ], "timeout": 45, "onFailure": "block" } @@ -28,7 +28,18 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"/srv/chock-managed/scan-secrets/scripts/launch.sh\" \"/srv/chock-managed/scan-secrets/scripts/claude_code.py\" --gate \"/srv/chock-managed/scan-secrets/scripts/gate.json\" --stop", + "command": "/usr/bin/python3", + "args": [ + "-I", + "-X", + "utf8", + "-c", + "import sys,os;r=sys.argv[1];m=[p for p in (r,os.path.join(os.path.dirname(r),'_chock_runtime.py')) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; this call goes ahead unchecked (--on-failure allow)\\n';(o or m) and (sys.stderr.write(w),sys.exit(0));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", + "/srv/chock-managed/scan-secrets/scripts/claude_code.py", + "--gate", + "/srv/chock-managed/scan-secrets/scripts/gate.json", + "--stop" + ], "timeout": 45 } ] diff --git a/tests/test_managed_emit_claude.py b/tests/test_managed_emit_claude.py index 5815a459..6dc6ea69 100644 --- a/tests/test_managed_emit_claude.py +++ b/tests/test_managed_emit_claude.py @@ -16,6 +16,7 @@ from chock import cli from chock.compile.emitters import managed from chock.compile.emitters.in_agent_hooks import TIMEOUT_SECONDS +from chock.compile.emitters.managed import claude_code from chock.compile.levels import cap_for, level_rank from chock.doctor.payloads import payload @@ -36,12 +37,32 @@ def repo(tmp_path: Path) -> Path: return root -def _emit(repo: Path, out: Path, *extra: str) -> int: - return cli.main(["emit", "--managed", "claude_code", "--out", str(out), "--repo", str(repo), *extra]) +PYTHON = "/usr/bin/python3" +ROOT = Path(__file__).resolve().parents[1] +FAIL_CLOSED = re.compile(r"fail(s|ing|ed)?[- ]closed|cannot be (bypassed|disabled)|\benforced\b", re.IGNORECASE) + + +def _emit(repo: Path, out: Path, *extra: str, python: str = PYTHON, on_failure: str = "block") -> int: + return cli.main( + [ + "emit", + "--managed", + "claude_code", + "--out", + str(out), + "--repo", + str(repo), + "--python", + python, + "--on-failure", + on_failure, + *extra, + ] + ) -def _linux(repo: Path, out: Path, *extra: str) -> int: - return _emit(repo, out, "--platform", "linux", "--deploy-root", DEPLOY_ROOT, *extra) +def _linux(repo: Path, out: Path, *extra: str, on_failure: str = "block") -> int: + return _emit(repo, out, "--platform", "linux", "--deploy-root", DEPLOY_ROOT, *extra, on_failure=on_failure) def _drop_in(out: Path) -> dict: @@ -75,14 +96,14 @@ def test_nothing_outside_out_changes(repo: Path, tmp_path: Path, monkeypatch: py def _system_out() -> Path: - return Path(managed.CHANNELS["system_roots"][managed.host_platform()][0]) / "chock-emit-test" + return Path(managed.CHANNELS["system_roots"][managed.host_platform()][1]) / "chock-emit-test" def test_out_under_a_system_path_is_refused(repo: Path, capsys: pytest.CaptureFixture[str]) -> None: """Acceptance 2: no file is written and the exit is non-zero.""" out = _system_out() assert _emit(repo, out) == 1 - assert "resolves inside the system path" in capsys.readouterr().err + assert "resolves to or inside the system path" in capsys.readouterr().err assert not out.exists() @@ -102,12 +123,25 @@ def test_an_existing_vendor_folder_is_never_written_into(repo: Path, tmp_path: P assert [p.name for p in (out / "claude_code").iterdir()] == ["mine.txt"] -@pytest.mark.parametrize("root", ["relative/dir", '/has"quote', "/has$dollar", "/has`tick"]) -def test_a_deploy_root_a_shell_would_read_is_refused(repo: Path, tmp_path: Path, root: str) -> None: +@pytest.mark.parametrize("root", ["relative/dir", "/has\nnewline", "/"]) +def test_a_deploy_root_that_is_not_an_absolute_folder_is_refused(repo: Path, tmp_path: Path, root: str) -> None: assert _emit(repo, tmp_path / "out", "--platform", "linux", "--deploy-root", root) == 1 assert not (tmp_path / "out" / "claude_code").exists() +def test_a_deploy_root_with_shell_characters_is_one_argument(repo: Path, tmp_path: Path) -> None: + """Finding 8: exec form, no shell, so quote, $ and backquote reach the client as written.""" + root = '/srv/a"b$c`d e' + assert _emit(repo, tmp_path / "out", "--platform", "linux", "--deploy-root", root) == 0 + args = [arg for _, hook in _handlers(_drop_in(tmp_path / "out")) for arg in hook["args"]] + assert f"{root}/block-no-verify/scripts/claude_code.py" in args + + +def test_a_deploy_root_is_normalised() -> None: + """Finding 9: deploy.json never shows a `..` target.""" + assert managed.deploy_root("/etc/claude-code/../../srv/x/", "linux") == "/srv/x" + + def test_a_windows_deploy_root_is_written_with_forward_slashes() -> None: assert ( managed.deploy_root("C:\\Program Files\\ClaudeCode\\chock\\", "windows") == "C:/Program Files/ClaudeCode/chock" @@ -126,7 +160,65 @@ def test_the_drop_in_follows_the_documented_schema(repo: Path, tmp_path: Path) - assert hook["type"] == "command" assert hook["timeout"] == TIMEOUT_SECONDS == 45 assert hook.get("onFailure") == ("block" if event == "PreToolUse" else None) - assert sum(" --stop-check" in hook["command"] for _, hook in handlers) == 1 + + +def test_hooks_are_exec_form_started_by_the_given_python(repo: Path, tmp_path: Path) -> None: + """Findings 1, 8: the command is --python itself; no git, sh, launcher or PATH lookup stands in between.""" + assert _linux(repo, tmp_path / "out") == 0 + for _, hook in _handlers(_drop_in(tmp_path / "out")): + assert hook["command"] == PYTHON + assert hook["args"][:4] == ["-I", "-X", "utf8", "-c"] + assert not [arg for arg in hook["args"] if "launch.sh" in arg or "chock.python" in arg or "git -c" in arg] + assert not list((tmp_path / "out").rglob("launch.sh")) + + +@pytest.mark.parametrize("python", ["python3", "bin/python3", ""]) +def test_python_must_be_an_absolute_path(repo: Path, tmp_path: Path, python: str, capsys) -> None: + """Ruling 1: the interpreter is never taken from PATH.""" + assert _emit(repo, tmp_path / "out", "--platform", "linux", python=python) == 1 + assert "must be the absolute path of the Python 3.11+ interpreter" in capsys.readouterr().err + assert not (tmp_path / "out").exists() + + +@pytest.mark.parametrize("missing", ["--python", "--on-failure"]) +def test_python_and_on_failure_have_no_default(repo: Path, tmp_path: Path, missing: str, capsys) -> None: + """Rulings 1, 2: the admin names both; without either the command fails and says which.""" + argv = ["emit", "--managed", "claude_code", "--out", str(tmp_path / "out"), "--repo", str(repo)] + argv += [flag for pair in (("--python", PYTHON), ("--on-failure", "block")) if pair[0] != missing for flag in pair] + with pytest.raises(SystemExit) as exc: + cli.main(argv) + assert exc.value.code == 2 + assert missing in capsys.readouterr().err + assert not (tmp_path / "out").exists() + + +def test_on_failure_allow_emits_no_failure_key(repo: Path, tmp_path: Path) -> None: + """Ruling 2: `allow` emits no onFailure anywhere.""" + assert _linux(repo, tmp_path / "out", on_failure="allow") == 0 + assert not [hook for _, hook in _handlers(_drop_in(tmp_path / "out")) if "onFailure" in hook] + + +def test_no_matcherless_stop_check_is_emitted(repo: Path, tmp_path: Path) -> None: + """Finding 2: every pre-tool group names its tools; the repo stop's every-tool entry is left out.""" + assert _linux(repo, tmp_path / "out") == 0 + doc = _drop_in(tmp_path / "out") + assert all(group.get("matcher") for group in doc["hooks"]["PreToolUse"]) + assert not [hook for _, hook in _handlers(doc) if "--stop-check" in hook["args"]] + + +@pytest.mark.parametrize("choice", ["block", "allow"]) +def test_the_failure_consequence_is_stated_in_deploy_json_and_output( + repo: Path, tmp_path: Path, choice: str, capsys +) -> None: + """Ruling 2: one sentence each, naming the interpreter and the matched tools.""" + assert _linux(repo, tmp_path / "out", on_failure=choice) == 0 + plan = json.loads((tmp_path / "out" / "claude_code" / "deploy.json").read_text(encoding="utf-8")) + sentence = plan["on_failure"]["consequence"] + assert plan["on_failure"]["choice"] == choice + assert sentence.startswith(f"--on-failure {choice}:") and PYTHON in sentence + assert "Bash|PowerShell, Write|Edit|MultiEdit|NotebookEdit" in sentence + assert ("is refused" if choice == "block" else "goes ahead unchecked") in sentence + assert sentence in capsys.readouterr().out def test_no_managed_only_key_without_lock(repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: @@ -141,13 +233,11 @@ def test_no_managed_only_key_without_lock(repo: Path, tmp_path: Path, capsys: py def test_hook_commands_name_only_the_deploy_root(repo: Path, tmp_path: Path) -> None: - """Acceptance 5: no repo-relative path, no project launcher prefix, every path under the deploy root.""" + """Acceptance 5: no repo-relative path, every path argument under the deploy root and in the bundle.""" out = tmp_path / "out" assert _linux(repo, out) == 0 for _, hook in _handlers(_drop_in(out)): - command = hook["command"] - assert ".chock/bin" not in command and "launch.sh ||" not in command - paths = re.findall(r'"([^"]+)"', command) + paths = [arg for arg in hook["args"][5:] if not arg.startswith("--")] assert paths and all(path.startswith(f"{DEPLOY_ROOT}/") for path in paths) for path in paths: assert (out / "claude_code" / "bundle" / path.removeprefix(f"{DEPLOY_ROOT}/")).is_file(), path @@ -200,33 +290,249 @@ def test_a_selection_with_nothing_to_run_writes_nothing(repo: Path, tmp_path: Pa assert not (tmp_path / "out").exists() -@pytest.mark.skipif( - not (shutil.which("git") and shutil.which("sh")), reason="the hook needs git and sh, as on a client" -) -def test_the_bundle_runs_on_its_own(repo: Path, tmp_path: Path) -> None: - """The emitted command, rooted at the bundle itself, refuses the canary and allows a harmless call.""" +def _bundle_hook(repo: Path, tmp_path: Path, on_failure: str = "block") -> tuple[Path, dict]: + """The block-no-verify guard's handler, emitted with this test's Python and rooted at the bundle itself.""" out = tmp_path / "out" bundle = (out / "claude_code" / "bundle").as_posix() - assert _emit(repo, out, "--policies", "block-no-verify", "--deploy-root", bundle) == 0 + assert ( + _emit( + repo, + out, + "--policies", + "block-no-verify", + "--deploy-root", + bundle, + python=sys.executable, + on_failure=on_failure, + ) + == 0 + ) [(_, hook), *_] = _handlers(_drop_in(out)) + return out / "claude_code" / "bundle", hook + + +def _env(tmp_path: Path, **extra: str) -> dict[str, str]: env = {k: v for k, v in os.environ.items() if k not in ("CLAUDECODE", "AI_AGENT", "CHOCK_AGENT_COMMIT")} - (tmp_path / "home").mkdir() - env["HOME"] = env["USERPROFILE"] = str(tmp_path / "home") - - def answer(command: str) -> str: - done = subprocess.run( - hook["command"], - shell=True, - input=json.dumps(payload("claude_code", command, tmp_path.as_posix())), - capture_output=True, - text=True, - cwd=tmp_path, - env=env, - check=False, - timeout=60, - ) + home = tmp_path / "home" + home.mkdir(exist_ok=True) + env["HOME"] = env["USERPROFILE"] = str(home) + return {**env, **extra} + + +def _run(hook: dict, command: str, cwd: Path, env: dict[str, str]) -> subprocess.CompletedProcess: + """Run `hook` the way the client runs exec form: `command` spawned with `args`, no shell.""" + return subprocess.run( + [hook["command"], *hook["args"]], + input=json.dumps(payload("claude_code", command, cwd.as_posix())), + capture_output=True, + text=True, + cwd=cwd, + env=env, + check=False, + timeout=60, + ) + + +def test_the_bundle_runs_on_its_own(repo: Path, tmp_path: Path) -> None: + """The emitted handler, rooted at the bundle itself, refuses the canary and allows a harmless call.""" + _, hook = _bundle_hook(repo, tmp_path) + env = _env(tmp_path) + denied, allowed = _run(hook, "git commit --no-verify -m x", tmp_path, env), _run(hook, "git status", tmp_path, env) + assert denied.returncode == allowed.returncode == 0, denied.stderr + allowed.stderr + assert '"permissionDecision": "deny"' in denied.stdout + assert "deny" not in allowed.stdout + + +@pytest.mark.skipif(sys.platform == "win32" or not shutil.which("git"), reason="POSIX stubs and git config") +def test_user_interpreter_settings_do_not_switch_the_hook_off(repo: Path, tmp_path: Path) -> None: + """Finding 1: chock.python set to a stub (local and global) and stub python3, python, sh, git first on PATH.""" + _, hook = _bundle_hook(repo, tmp_path) + stubs = tmp_path / "stubs" + stubs.mkdir() + for name in ("python3", "python", "py", "sh", "git"): + stub = stubs / name + stub.write_text("#!/bin/sh\nprintf chock-python >&9 2>/dev/null; exit 0\n", encoding="utf-8") + stub.chmod(0o755) + work = tmp_path / "work" + work.mkdir() + subprocess.run(["git", "init", "-q", str(work)], check=True) + subprocess.run(["git", "-C", str(work), "config", "chock.python", str(stubs / "python3")], check=True) + global_config = tmp_path / "gitconfig" + global_config.write_text(f"[chock]\n\tpython = {stubs / 'python3'}\n", encoding="utf-8") + env = _env( + tmp_path, + PATH=f"{stubs}{os.pathsep}{os.environ.get('PATH', '')}", + GIT_CONFIG_GLOBAL=str(global_config), + PYTHONPATH=str(stubs), + PYTHONSTARTUP=str(stubs / "python3"), + ) + for cwd in (work, tmp_path): + done = _run(hook, "git commit --no-verify -m x", cwd, env) assert done.returncode == 0, done.stderr - return done.stdout + assert '"permissionDecision": "deny"' in done.stdout + + +@pytest.mark.parametrize(("choice", "code"), [("block", 2), ("allow", 0)]) +def test_a_missing_bundle_file_follows_on_failure(repo: Path, tmp_path: Path, choice: str, code: int) -> None: + """Ruling 2: block refuses (exit 2) and allow lets the call through, each with a note on stderr.""" + bundle, hook = _bundle_hook(repo, tmp_path, on_failure=choice) + (bundle / "block-no-verify" / "scripts" / "_chock_runtime.py").unlink() + done = _run(hook, "git status", tmp_path, _env(tmp_path)) + assert done.returncode == code + assert done.stderr.startswith("chock: missing ") and f"--on-failure {choice}" in done.stderr + assert done.stdout == "" + + +@pytest.mark.parametrize(("choice", "code"), [("block", 2), ("allow", 0)]) +def test_too_old_a_python_follows_on_failure(choice: str, code: int) -> None: + """Ruling 2: the version check runs before any runtime code; here the minimum is raised past this Python.""" + program = claude_code.bootstrap(choice).replace("(3, 11,)", "(99,)") + done = subprocess.run( + [sys.executable, "-I", "-c", program, __file__], capture_output=True, text=True, check=False, timeout=60 + ) + assert done.returncode == code + assert "this hook needs Python" in done.stderr and f"--on-failure {choice}" in done.stderr + + +def test_the_stop_gate_never_refuses_for_a_missing_runtime(repo: Path, tmp_path: Path) -> None: + """onFailure has no effect on Stop, and exit 2 there sends Claude back to work: the turn-end gate notes and exits 0.""" + assert _linux(repo, tmp_path / "out", "--policies", "scan-secrets") == 0 + stop = [hook for event, hook in _handlers(_drop_in(tmp_path / "out")) if event == "Stop"] + assert stop and all("sys.exit(0)" in hook["args"][4] for hook in stop) + + +@pytest.mark.parametrize( + ("path", "platform"), + [ + ("/", "linux"), + ("//etc/x", "linux"), + ("/var/lib/x", "linux"), + ("/srv/x", "linux"), + ("/root/x", "linux"), + ("/run/x", "linux"), + ("/sys/x", "linux"), + ("/proc/x", "linux"), + ("/", "macos"), + ("/LIBRARY/Application Support/ClaudeCode/x", "macos"), + ("/system/x", "macos"), + ("/private/etc/x", "macos"), + ("C:/", "windows"), + ("D:\\", "windows"), + ("c:\\program files\\ClaudeCode", "windows"), + ("\\\\?\\C:\\Program Files\\x", "windows"), + ("\\\\.\\C:\\Windows\\x", "windows"), + ("\\\\localhost\\C$\\Program Files\\x", "windows"), + ("\\\\?\\UNC\\server\\c$\\Windows", "windows"), + ], +) +def test_every_system_spelling_is_refused(path: str, platform: str) -> None: + """Finding 4: filesystem roots, the listed trees, case-insensitive spellings, device and admin-share prefixes.""" + assert managed.root_named(path, platform) is not None + + +@pytest.mark.parametrize( + ("path", "platform"), + [("/home/me/out", "linux"), ("/tmp/out", "linux"), ("/Users/me/out", "macos"), ("C:/Users/me/out", "windows")], +) +def test_ordinary_folders_are_allowed(path: str, platform: str) -> None: + assert managed.root_named(path, platform) is None + + +def _one_line_refusal(capsys: pytest.CaptureFixture[str]) -> str: + err = capsys.readouterr().err + assert "Traceback" not in err and len(err.strip().splitlines()) == 1, err + return err + + +def test_out_naming_a_file_is_refused_cleanly(repo: Path, tmp_path: Path, capsys) -> None: + """Finding 5.""" + afile = tmp_path / "afile" + afile.write_text("x", encoding="utf-8") + assert _linux(repo, afile) == 1 + assert "is not a folder" in _one_line_refusal(capsys) + assert afile.read_text(encoding="utf-8") == "x" + + +@pytest.mark.parametrize("name", ["manifest.yaml", "block-no-verify.py"]) +def test_a_non_utf8_policy_file_is_refused_cleanly(repo: Path, tmp_path: Path, name: str, capsys) -> None: + """Finding 5: a non-UTF-8 manifest or guard.""" + [target] = list((repo / ".agents" / "policies" / "block-no-verify").rglob(name)) + target.write_bytes(target.read_bytes() + b"\xe9\n") + assert _linux(repo, tmp_path / "out", "--policies", "block-no-verify") == 1 + assert "is not UTF-8 text" in _one_line_refusal(capsys) + assert not (tmp_path / "out").exists() + - assert '"permissionDecision": "deny"' in answer("git commit --no-verify -m x") - assert "deny" not in answer("git status") +def test_a_missing_repo_is_refused_cleanly(tmp_path: Path, capsys) -> None: + """Findings 5, 9: says the folder is missing, not that no policy has a guard.""" + assert _linux(tmp_path / "does-not-exist", tmp_path / "out") == 1 + assert "is not a folder" in _one_line_refusal(capsys) + + +def _plan(out: Path) -> dict: + return json.loads((out / "claude_code" / "deploy.json").read_text(encoding="utf-8")) + + +def test_deploy_json_states_ownership_stop_file_and_lock_effects(repo: Path, tmp_path: Path) -> None: + """Ruling 6 (findings 7, 9): admin-owned bundle with the vendor's words, the repo stop, every --lock side effect.""" + assert _linux(repo, tmp_path / "out", "--lock") == 0 + plan = _plan(tmp_path / "out") + text = json.dumps(plan) + assert any("owned by an administrator and not writable by users" in line for line in plan["requirements"]) + assert "can edit the managed source itself" in text + assert any(".chock/stop.json" in line and "refuse every tool call" in line for line in plan["caveats"]) + lock = plan["notes"][-1] + for effect in ( + "user, project and local hooks", + "hooks in agent frontmatter", + "monitors of other plugins", + "command source are disabled", + "headersHelper", + "status line and file suggestion", + "/goal", + "force-enable", + ): + assert effect in lock, effect + + +@pytest.mark.skipif(sys.platform == "win32" or not shutil.which("git"), reason="POSIX paths, a git repository") +def test_a_repo_stop_file_refuses_matched_calls_as_deploy_json_says(repo: Path, tmp_path: Path) -> None: + """Finding 7: the behaviour the caveat states, proven.""" + _, hook = _bundle_hook(repo, tmp_path) + work = tmp_path / "stopped" + (work / ".chock").mkdir(parents=True) + (work / ".chock" / "stop.json").write_text("not json", encoding="utf-8") + subprocess.run(["git", "init", "-q", str(work)], check=True) + done = _run(hook, "ls", work, _env(tmp_path)) + assert done.returncode == 0 and '"permissionDecision": "deny"' in done.stdout + + +def test_no_wording_stronger_than_the_label(repo: Path, tmp_path: Path, capsys) -> None: + """Ruling 7 (finding 6): no fail-closed, enforced or cannot-override claim in output, help, data, docstrings or changelog.""" + assert _linux(repo, tmp_path / "out", "--lock") == 0 + with pytest.raises(SystemExit): + cli.main(["emit", "--help"]) + texts = [ + capsys.readouterr().out, + *(p.read_text(encoding="utf-8") for p in (tmp_path / "out" / "claude_code").rglob("*.json")), + ] + texts += [json.dumps(managed.CHANNELS), managed.__doc__ or "", claude_code.__doc__ or ""] + texts += [p.read_text(encoding="utf-8") for p in (ROOT / "changelog.d").glob("w2b1*.md")] + texts += [ + p.read_text(encoding="utf-8") + for p in (ROOT / "src" / "chock" / "compile" / "emitters" / "managed").glob("*.py") + ] + assert not [text for text in texts if FAIL_CLOSED.search(text) or OVERRIDE_CLAIM.search(text)] + assert "writes no system path" not in "".join(texts) + + +DOCS = ROOT / "docs" + + +def test_docs_say_what_the_code_does() -> None: + """Ruling 3 (finding 3): no compiled managed-settings file, `chock emit` documented, no cannot-override claim.""" + pages = {p.name: p.read_text(encoding="utf-8") for p in DOCS.rglob("*.md")} + assert not [name for name, text in pages.items() if "managed-setting/managed-settings.json" in text] + assert "chock emit --managed" in pages["cli-reference.md"] + assert not OVERRIDE_CLAIM.search(pages["compare.md"]) + assert "a user cannot override" not in pages["compare.md"].lower() From 66c677726319f201a85438f55ca5fed7fb7c826d Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:37:00 +0000 Subject: [PATCH 17/57] feat(floor): control-plane rule replaces tamper rules; judge base..head (W2-A1v2) The floor stops judging how a guardrail file changed and reports that it changed. - base must be an ancestor of head (pass the merge result), else exit 2; every read comes from the tree diff base..head (--no-renames, --no-ext-diff, --no-textconv) and the commits in it; no merge-base is computed. - src/chock/data/control_plane.json lists the guardrail paths, matched case-blind; any change to one is floor/control-plane-changed, listed first (text and SARIF). The base's floor.control_plane decides: refuse (default, exit 1) or listed (exit 0). The base may add or remove patterns; the head's config is never read. - Deleted: tamper.py, tamper_files.py, tamper_hooks.py, tamper.json, the lock-entry exemption, the engine-render exception, the head policy set. - Content gates are the base's and never judge a control-plane path. A waiver counts only on a line unchanged in both base and head. - History pass: one reader (latin-1, utf-8 when valid, utf-16 both orders and alignments for a blob with a NUL; lines split on \n only); a finding carries its line digest, so nothing is re-counted. The base's rollout and declared action apply to history findings too. - Every proof from both adversarial passes is a regression test asserting the owed exit. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2a1-check-floor.md | 44 ++- spec/gate-dsl.md | 2 +- src/chock/data/commands.json | 2 +- src/chock/data/control_plane.json | 33 ++ src/chock/data/tamper.json | 40 --- src/chock/history/scan.py | 42 ++- src/chock/validation/checks_floor.py | 206 ++++++------ src/chock/validation/control_plane.py | 90 +++++ src/chock/validation/floor_cli.py | 14 +- src/chock/validation/floor_context.py | 43 +-- src/chock/validation/floor_git.py | 28 +- src/chock/validation/floor_history.py | 84 +++-- src/chock/validation/floor_sarif.py | 51 +++ src/chock/validation/tamper.py | 130 -------- src/chock/validation/tamper_files.py | 162 --------- src/chock/validation/tamper_hooks.py | 131 -------- tests/fixtures/cli_help_golden.txt | 2 +- tests/floor_support.py | 25 +- tests/test_floor_attacks.py | 209 ++++++------ tests/test_floor_attacks2.py | 138 +++++--- tests/test_floor_attacks3.py | 457 ++++++++++++++++++++++++++ tests/test_floor_check.py | 16 +- tests/test_floor_control_plane.py | 158 +++++++++ tests/test_floor_tamper.py | 224 ------------- 24 files changed, 1262 insertions(+), 1069 deletions(-) create mode 100644 src/chock/data/control_plane.json delete mode 100644 src/chock/data/tamper.json create mode 100644 src/chock/validation/control_plane.py create mode 100644 src/chock/validation/floor_sarif.py delete mode 100644 src/chock/validation/tamper.py delete mode 100644 src/chock/validation/tamper_files.py delete mode 100644 src/chock/validation/tamper_hooks.py create mode 100644 tests/test_floor_attacks3.py create mode 100644 tests/test_floor_control_plane.py delete mode 100644 tests/test_floor_tamper.py diff --git a/changelog.d/w2a1-check-floor.md b/changelog.d/w2a1-check-floor.md index 7dc5b329..deb68ee1 100644 --- a/changelog.d/w2a1-check-floor.md +++ b/changelog.d/w2a1-check-floor.md @@ -1,19 +1,27 @@ -- `chock check --floor --base [--head ] [--head-ref ]` (preview): judges a pull request's - `base...head` with the base's compiled ci gates, run by the installed engine, never `.chock/bin/gate.py` or any - other head file. A head gate that is new or differs also runs when it is declarative; a head script gate is - reported "not judged at floor". A waiver counts only on a line the pull request leaves unchanged (event - `ci-floor`); `ask` refuses; a gate that crashes, times out or cannot be read is undecided whatever the rollout. - Script gates receive deleted paths under `deleted` and an environment of `PATH`, `HOME` (and `SYSTEMROOT`) only - (script payload contract version 2, `spec/gate-dsl.md`). Diffs are read with `--text`. -- Built-in tamper rules (`src/chock/data/tamper.json`) compare the merge-base with the head and refuse by name: a - removed or retyped hook entry; a hook switch-off key turned on, or a committed higher-precedence settings file - carrying hooks (Claude Code; other vendors are printed as not covered); a deleted compiled gate, or a changed one - with no `chock.lock` change for its pack; a pack dropped from `chock.lock`; any `.chock/bin` file or the floor's - own workflow that is neither the merge-base's bytes nor what this engine renders. A pack whose lock entry changed - is listed under `floor/pack-changed` with every file of it the pull request changed, and only then is its own - source folder exempt from its own gates. -- A per-commit pass runs the base's content gates (both surfaces) over `merge-base..head`, judging binary blobs too - (non-utf-8 bytes read as latin-1); a range past the commit limit or a blob it cannot read is undecided, naming the - commit and path. Exit 0 clean, 1 refused, 2 undecided. It is a floor only where the repository makes it a required - check, which chock does not verify. +- `chock check --floor --base [--head ] [--head-ref ] [--format sarif --output ]` + (preview): judges what a pull request will land. The base must be an ancestor of the head (pass GitHub's + `refs/pull//merge` or the merge queue's head; anything else is undecided). Everything comes from the tree + diff `base..head` (no renames, no external diff or textconv) and the commits in it; no merge-base is computed. + Exit 0 clean, 1 refused, 2 undecided. +- Control plane: a change (add, edit, delete, mode) to any path in `src/chock/data/control_plane.json` + (`.chock/**`, `chock.lock`, `.agents/policies/**`, every vendor hook or settings file chock and agentseam write, + with local and override variants, `.github/workflows/**`, `.github/actions/**`, `CODEOWNERS`, `.gitattributes`; + matched case-blind) is reported as `floor/control-plane-changed` with every path, first. These files define the + guardrails; the floor does not judge whether a change to them is safe. A person decides. The base's + `.chock/config.yaml` sets `floor.control_plane`: `refuse` (default, exit 1: every guardrail change, including a + policy upgrade, a `chock sync` regeneration or a workflow edit, needs a privileged merge) or `listed` (exit 0, + paths still listed and in the SARIF; the adopter relies on code-owner review, and content inside listed paths is + not judged). `floor.control_plane_paths: {add, remove}` edits the list. The head's config is never read. +- Everything else is judged by the base's compiled ci gates and rollout, run by this engine; no head file executes. + `ask` refuses; a gate that crashes, times out or cannot be read is undecided whatever the rollout. A waiver counts + only on a line present and unchanged in both base and head. Script gates receive deleted paths under `deleted` + and an environment of `PATH`, `HOME` (and `SYSTEMROOT`) only (script payload contract version 2, + `spec/gate-dsl.md`). +- A per-commit pass runs the base's content gates (both surfaces) over every blob a commit in `base..head` adds or + changes outside the control plane, read as latin-1, as utf-8 when valid, and, for a blob with a NUL byte, as + utf-16 in both byte orders and alignments, lines split on `\n` only. A range past the commit limit or a blob over + 64 MiB is undecided, naming the commit and path. Not seen: other encodings, compressed or encoded content, commit + messages, file names. +- It holds only as a required status check the pull request cannot redefine; that is the adopter's setting, which + chock does not verify. It judges committed content only. - `chock history`'s `load_rules` takes the compiled surfaces to read; its own defaults are unchanged. diff --git a/spec/gate-dsl.md b/spec/gate-dsl.md index 9882422f..c4c26106 100644 --- a/spec/gate-dsl.md +++ b/spec/gate-dsl.md @@ -227,7 +227,7 @@ script lives beside this gate file" instead of "under the repository root". Only plugin writes it: there the gate, the runner and the script travel together and no repository holds them. -Script payload contract **v2** (v1 was unnumbered; v2 adds `ci-floor`, from `chock check --floor`, and nothing elsewhere). `event` is `commit`, `push`, `tool_use`, `ci-floor` or (see above) `tool_call`. `session` is present only at the agent events. At `ci-floor` the payload adds `deleted` (sorted deleted paths; a deletion-only range still runs the script), the environment is only `PATH`, `HOME`, `SYSTEMROOT`, the script is the base's copy, a waiver counts only when the base has the line, and `ask` refuses. `writes` is the staged blobs at `commit` and +Script payload contract **v2** (v1 was unnumbered; v2 adds `ci-floor`, from `chock check --floor`, and nothing elsewhere). `event` is `commit`, `push`, `tool_use`, `ci-floor` or (see above) `tool_call`. `session` is present only at the agent events. At `ci-floor` the payload adds `deleted` (sorted deleted paths; a deletion-only range still runs the script), the environment is only `PATH`, `HOME`, `SYSTEMROOT`, the script is the base's copy, a waiver counts only on a line unchanged in base and head, and `ask` refuses. `writes` is the staged blobs at `commit` and `push`, and the write itself at `tool_use` -- the file a tool call is about to write, or what the turn left on disk at its end -- so one script serves every surface, and it reaches the write path and the turn's end exactly as `content_regex` does. The script answers with its diff --git a/src/chock/data/commands.json b/src/chock/data/commands.json index 59556192..9121c35a 100644 --- a/src/chock/data/commands.json +++ b/src/chock/data/commands.json @@ -29,7 +29,7 @@ "check": { "module": "chock.history.cli", "fn": "check_main", - "help": "Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor judges a pull request with its base's gates)" + "help": "Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor, a preview, judges a pull request's merge result with its base's gates)" }, "status": { "module": "chock.lifecycle", diff --git a/src/chock/data/control_plane.json b/src/chock/data/control_plane.json new file mode 100644 index 00000000..6d12a658 --- /dev/null +++ b/src/chock/data/control_plane.json @@ -0,0 +1,33 @@ +{ + "$comment": "The paths that define the guardrails (`chock check --floor`, floor/control-plane-changed). Matched with fnmatch after case folding, so `*` crosses `/`. A pull request that adds, changes, deletes or re-modes one is listed; the base's `.chock/config.yaml` decides refuse or listed. Vendor hook files are agentseam's config_path for every repo-relative vendor (tests/test_floor_control_plane.py checks it) plus each local or override variant chock knows.", + "paths": [ + ".chock/**", + "chock.lock", + ".agents/policies/**", + ".agents/hooks.json", + ".agents/plugins/**", + ".claude/settings.json", + ".claude/settings.local.json", + ".clinerules/hooks/**", + ".codex/hooks.json", + ".codex/config.toml", + ".cursor/hooks.json", + ".devin/hooks.json", + ".devin/hooks.v1.json", + ".windsurf/hooks.json", + ".factory/hooks.json", + ".gemini/settings.json", + ".grok/hooks/**", + ".openhands/hooks.json", + ".tabnine/agent/settings.json", + ".vibe/hooks.toml", + ".github/hooks/**", + ".github/workflows/**", + ".github/actions/**", + ".github/CODEOWNERS", + "CODEOWNERS", + "docs/CODEOWNERS", + ".gitattributes", + "**/.gitattributes" + ] +} diff --git a/src/chock/data/tamper.json b/src/chock/data/tamper.json deleted file mode 100644 index a7150d81..00000000 --- a/src/chock/data/tamper.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "$comment": "Facts `chock check --floor` compares merge-base against head. Ships inside the engine, so a pull request cannot edit what judges it.", - "hook_files": [ - {"glob": ".claude/settings.json", "pointer": "/hooks", "vendor": "claude_code"}, - {"glob": ".cursor/hooks.json", "pointer": "/hooks", "vendor": "cursor"}, - {"glob": ".github/hooks/*.json", "pointer": "/hooks", "vendor": "vscode_copilot"}, - {"glob": ".codex/hooks.json", "pointer": "/hooks", "vendor": "codex_cli"}, - {"glob": ".gemini/settings.json", "pointer": "/hooks", "vendor": "gemini_cli"}, - {"glob": ".windsurf/hooks.json", "pointer": "/hooks", "vendor": "windsurf"}, - {"glob": ".devin/hooks.json", "pointer": "/hooks", "vendor": "devin"}, - {"glob": ".devin/hooks.v1.json", "pointer": "", "vendor": "devin"}, - {"glob": ".grok/hooks/*.json", "pointer": "/hooks", "vendor": "grok"} - ], - "$comment_switches": "Per vendor: keys that switch every hook off (a JSON pointer into its hook file, refused when truthy at the head and not at the merge-base) and the higher-precedence files a repository can commit (refused when added or changed carrying hooks or a disable key). A vendor with no entry is printed as not covered.", - "hook_switches": { - "claude_code": { - "disable_keys": ["/disableAllHooks"], - "override_files": [".claude/settings.local.json"] - } - }, - "hook_nested_key": "hooks", - "hook_matcher_key": "matcher", - "hook_matcher_separator": "|", - "hook_type_key": "type", - "hook_type_default": "command", - "hook_command_keys": ["command", "commandWindows", "bash", "powershell", "prompt"], - "$comment_volatile": "Only the index pin a regeneration rewrites; the index file it pins must itself equal the merge-base or this engine's render.", - "volatile_pattern": "(?<=--index-sha256 )[0-9a-f]{64}", - "volatile_placeholder": "", - "compiled_prefix": ".chock/compiled/", - "policies_prefix": ".agents/policies/", - "gate_surfaces": ["ci-gate", "git-hook"], - "gate_file": "gate.json", - "runtime_prefix": ".chock/bin/", - "index_prefix": ".chock/bin/index/", - "launcher": ".chock/bin/launch.sh", - "lock_file": "chock.lock", - "workflow_default": ".github/workflows/chock.yml", - "workflow_config_key": "floor_workflow" -} diff --git a/src/chock/history/scan.py b/src/chock/history/scan.py index b52e4752..4a16727c 100644 --- a/src/chock/history/scan.py +++ b/src/chock/history/scan.py @@ -2,6 +2,7 @@ from __future__ import annotations +import contextlib import hashlib import shutil import sys @@ -31,6 +32,8 @@ class Finding: line: int | None rule: str policy: str + #: The matched line's digest (the forbidden-path rule's name for a path finding), as the scan read it. + key: str = "" def as_dict(self) -> dict[str, object]: return {"commit": self.commit, "path": self.path, "line": self.line, "rule": self.rule, "policy": self.policy} @@ -57,8 +60,8 @@ class Limits: max_blob_bytes: int = DEFAULT_MAX_BLOB_BYTES allow_shallow: bool = False time_budget: int = DEFAULT_TIME_BUDGET - #: Judge binary blobs too, decoding bytes that are not utf-8 as latin-1 (lossless); a blob still not judged - #: (over max_blob_bytes) is named in Report.unread. + #: The floor's reader: every blob is judged (binary too) in each of `floor_texts`, and a blob still not + #: judged (over max_blob_bytes) is named in Report.unread. judge_binary: bool = False @@ -91,22 +94,35 @@ def _judged(judge: dict, policy: str, jobs: list[tuple[str, list[str]]]) -> list return [set(found) for found in hits] -def _decoded(data: bytes, *, lossless: bool) -> str: - """The blob as text: utf-8 with replacement, or, when `lossless` and it is not utf-8, latin-1 byte for byte.""" - if lossless: - try: - return data.decode("utf-8") - except UnicodeDecodeError: - return data.decode("latin-1") - return data.decode("utf-8", "replace") +_UTF16 = ("utf-16-le", "utf-16-be") + + +def floor_texts(data: bytes) -> list[str]: + """Every reading the floor judges: latin-1 (lossless), utf-8 when the bytes are utf-8, and, when the blob + holds a NUL byte, utf-16 in both byte orders at both alignments (a BOM decodes to U+FEFF, a stray byte shifts).""" + texts = [data.decode("latin-1")] + with contextlib.suppress(UnicodeDecodeError): + texts.append(data.decode("utf-8")) + if b"\0" in data: + texts += [data[skip:].decode(codec, "replace") for codec in _UTF16 for skip in (0, 1)] + return texts + + +def floor_lines(data: bytes) -> list[str]: + """`floor_texts`, each split on `\\n` only: one line list, so a line number never needs a second count.""" + return [line for text in floor_texts(data) for line in text.split("\n")] def _judge_blob( rules: list[Rule], data: bytes, judge: dict, *, lossless: bool = False ) -> tuple[list[tuple[int, int, str]], set[int]]: """(rule index, line number, line digest) per hit, and the rules whose waiver pragma the blob carries.""" - text = _decoded(data, lossless=lossless) - lines = text.splitlines() + if lossless: + lines = floor_lines(data) + text = "\n".join(lines) + else: + text = data.decode("utf-8", "replace") + lines = text.splitlines() hits: list[tuple[int, int, str]] = [] waived: set[int] = set() for index, rule in enumerate(rules): @@ -181,7 +197,7 @@ def add(item: gitlog.Touch, rule: Rule, kind: str, line: int | None, key: str) - marker = (item.path, rule.policy, key) if marker not in seen: seen.add(marker) - found.append(Finding(item.commit, item.path, line, kind, rule.policy)) + found.append(Finding(item.commit, item.path, line, kind, rule.policy, key)) for item in touched: hits, waived = judged.get(item.oid, ([], set())) diff --git a/src/chock/validation/checks_floor.py b/src/chock/validation/checks_floor.py index 42e91a49..1e2f8e4c 100644 --- a/src/chock/validation/checks_floor.py +++ b/src/chock/validation/checks_floor.py @@ -1,6 +1,9 @@ -"""`chock check --floor`: judge a pull request with the base's gates, run by this engine; no head file executes. +"""`chock check --floor` (preview): judge what a pull request will land with the base's gates, run by this engine. -Exit 0 clean, 1 any refusal (each naming its policy or tamper rule and path), 2 undecided. +base must be an ancestor of head (pass the merge result); everything comes from the tree diff base..head and +the commits in it. A changed control-plane path is listed (`floor/control-plane-changed`), never read for +meaning; the base's gates judge everything else and no head file executes. +Exit 0 clean, 1 any refusal (each naming its rule or policy and path), 2 undecided. """ from __future__ import annotations @@ -9,7 +12,7 @@ import json import sys import tempfile -from dataclasses import dataclass +from dataclasses import dataclass, field from pathlib import Path from chock.display import inert @@ -19,7 +22,6 @@ COMPILED_PREFIX, FLOOR_EVENT, KINDS, - POLICIES_PREFIX, SCRIPT_BASE_GATE, GateResult, committed_rollout, @@ -29,21 +31,27 @@ from chock.gate.runner.kinds import PatternDeadlineError from chock.gate.runner.report import _reason from chock.gate.runner.script import _UNDECIDED +from chock.validation import control_plane as cp +from chock.validation import floor_sarif from chock.validation.floor_context import FloorContext -from chock.validation.floor_git import FloorError, Revision, merge_base, resolve +from chock.validation.floor_git import FloorError, Revision, changed_paths, require_ancestor, resolve from chock.validation.floor_history import history_findings -from chock.validation.tamper import Revs, pack_notes, packs_changed, tamper -from chock.validation.tamper_hooks import not_covered EXIT_REFUSED, EXIT_UNDECIDED = 1, 2 _CI_GATE = "ci-gate/gate.json" +_SURFACES = ("ci-gate", "git-hook") _SCRIPT, _DEPENDENCY, _REF = "script", _DEPENDENCY_KIND, "forbidden_ref" _REFUSING = (ACTION_BLOCK, ACTION_ASK) -_NO_GATES = "floor: the base has no compiled gates; no gate judged this pull request (tamper rules still ran)" -_NOT_COVERED = "floor/hook-switches not covered: {} -- the keys that switch their hooks off are not verified here" +_FIRST_ADOPTION = "floor: base has no chock gates; nothing was judged" +_NO_GATES = "floor: the base has no compiled ci gates; no gate judged the content (the control-plane rule still ran)" +_CONTROL_PLANE = ( + "{rule} ({mode}): {count} path(s) -- these files define the guardrails; the floor does not judge " + "whether a change to them is safe. A person decides." +) +_LISTED_GAP = "{rule}: under `listed` the content of these paths was not judged; code-owner review is the check" _NOT_REQUIRED = ( - "floor: a floor only where this check is a required status check; that is the repository's own " - "setting, unverified by chock. It judges committed content only." + "floor (preview): a floor only where this check is a required status check the pull request cannot " + "redefine; that is the repository's own setting, unverified by chock. It judges committed content only." ) @@ -57,10 +65,9 @@ def _err(line: str) -> None: @dataclass(frozen=True) class Gate: - """One compiled ci gate as one revision commits it.""" + """One compiled ci gate as the base commits it.""" policy: str - source: str rev: Revision path: str spec: dict @@ -68,45 +75,27 @@ class Gate: @dataclass class Outcome: - refusals: list[str] - undecided: list[str] - notes: list[str] + refusals: list[str] = field(default_factory=list) + undecided: list[str] = field(default_factory=list) + notes: list[str] = field(default_factory=list) + listed: list[str] = field(default_factory=list) + mode: str = cp.REFUSE -def _gates(rev: Revision, source: str) -> dict[str, Gate]: - found: dict[str, Gate] = {} - for path in rev.paths(COMPILED_PREFIX): - rest = path.removeprefix(COMPILED_PREFIX) - policy, _, tail = rest.partition("/") +def base_gates(base: Revision) -> list[Gate]: + found: list[Gate] = [] + for path in base.paths(COMPILED_PREFIX): + policy, _, tail = path.removeprefix(COMPILED_PREFIX).partition("/") if tail != _CI_GATE: continue try: - spec = json.loads(rev.text(path) or "") + spec = json.loads(base.text(path) or "") except ValueError: spec = None - found[policy] = Gate(policy, source, rev, path, spec if isinstance(spec, dict) else {}) + found.append(Gate(policy, base, path, spec if isinstance(spec, dict) else {})) return found -def policy_set(base: Revision, head: Revision) -> tuple[list[Gate], list[str]]: - """Every base gate, plus each head gate that is new or differs (both run; the stricter binds). - - A head script gate is new code and is never run here: the base set binds until it merges. - """ - at_base, at_head = _gates(base, "base"), _gates(head, "head") - run = list(at_base.values()) - notes: list[str] = [] - for policy, gate in sorted(at_head.items()): - same = policy in at_base and base.text(at_base[policy].path) == head.text(gate.path) - if same: - continue - if gate.spec.get("kind") == _SCRIPT: - notes.append(f"{policy} [head]: not judged at floor (new script code, runs after merge)") - else: - run.append(gate) - return run, notes - - def _relative(named: str) -> str: """`named` when it is a path inside the repository; FloorError (undecided) for an absolute or escaping one.""" if not named or Path(named).is_absolute() or ".." in Path(named).parts or "\\" in named or ":" in named: @@ -116,7 +105,7 @@ def _relative(named: str) -> str: def _params(gate: Gate, tree: Path) -> dict: - """The gate's params, with every file it names taken from the gate's own revision, materialised under `tree`.""" + """The gate's params, with every file it names taken from the base, materialised under `tree`.""" params = dict(gate.spec.get("params") or {}) if gate.spec.get("kind") == _SCRIPT: if gate.spec.get("script_base") == SCRIPT_BASE_GATE: @@ -134,28 +123,16 @@ def _params(gate: Gate, tree: Path) -> dict: return params -def _own(gate: Gate, packs: frozenset[str]) -> tuple[str, ...]: - """The policy's source folder, and only for a pack whose lock entry the pull request changed (listed in a - `floor/pack-changed` note); otherwise a gate judges its own folders like any other file.""" - return (f"{POLICIES_PREFIX}{gate.policy}/",) if gate.policy in packs else () - - -def _judge( # noqa: PLR0913, PLR0917 -- the gate, where it runs, what changed - gate: Gate, repo: Path, revs: tuple[str, str, str], head_ref: str | None, tree: Path, packs: frozenset[str] -) -> GateResult | str: +def _judge(gate: Gate, ctx: FloorContext, tree: Path) -> GateResult | str: """The gate's result, or why it could not judge.""" - spec, label = gate.spec, f"{gate.policy} [{gate.source}]" + spec, label = gate.spec, f"{gate.policy} [base]" kind = KINDS.get(spec.get("kind")) if not spec or kind is None or spec.get("action", ACTION_BLOCK) not in _ACTION_RANK: return f"{label}: unreadable gate, unknown kind or unknown action" - if spec.get("kind") == _REF and not head_ref: + if spec.get("kind") == _REF and not ctx.head_ref: return f"{label}: a branch gate needs --head-ref " - base, head, fork = revs - ctx = FloorContext( - repo, base=base, head=head, merge_base=fork, head_ref=head_ref, scope=spec.get("paths"), own=_own(gate, packs) - ) try: - result = kind(ctx, _params(gate, tree / gate.source), FLOOR_EVENT) + result = kind(ctx, _params(gate, tree), FLOOR_EVENT) except (ListingError, PatternDeadlineError, FloorError) as exc: return f"{label}: {inert(str(exc))}" if not result.allowed and (result.message or "").endswith(_UNDECIDED): @@ -169,76 +146,121 @@ def _verdict(result: GateResult, declared: str, level: str) -> str: return min(result.verdict or declared, declared, _ROLLOUT_CEILING[level], key=_ACTION_RANK.__getitem__) -def judge_gates( # noqa: PLR0913, PLR0917 -- the range, the branch and the changed packs - repo: Path, base: Revision, head: Revision, fork: str, head_ref: str | None, packs: frozenset[str] = frozenset() +def judge_gates( # noqa: PLR0913 -- the range, the branch, what it excludes and the level + repo: Path, base: Revision, head: Revision, head_ref: str | None, *, plane: cp.ControlPlane, level: str ) -> Outcome: - """Run the policy set over base...head; `ask` refuses, since nobody can be asked in CI. + """Run the base's ci gates over base..head; `ask` refuses, since nobody can be asked in CI. A gate that cannot judge is undecided before the rollout applies: `observe` softens refusals, never that. """ - gates, notes = policy_set(base, head) - outcome = Outcome([], [], notes) - level = committed_rollout(repo, base.sha) + outcome = Outcome() with tempfile.TemporaryDirectory(prefix="chock-floor-") as scratch: - for gate in gates: + for gate in base_gates(base): if "commit" not in (gate.spec.get("on") or []) and gate.spec: continue - result = _judge(gate, repo, (base.sha, head.sha, fork), head_ref, Path(scratch), packs) + ctx = FloorContext( + repo, base=base.sha, head=head.sha, head_ref=head_ref, scope=gate.spec.get("paths"), + control_plane=plane.covers, + ) # fmt: skip + result = _judge(gate, ctx, Path(scratch)) if isinstance(result, str): outcome.undecided.append(result) continue verdict = _verdict(result, gate.spec.get("action", ACTION_BLOCK), level) if verdict == "allow": continue - reason = _reason(result, gate.spec, FLOOR_EVENT, gate.policy) - line = f"{gate.policy} [{gate.source}] ({verdict}): {reason}" + line = f"{gate.policy} [base] ({verdict}): {_reason(result, gate.spec, FLOOR_EVENT, gate.policy)}" (outcome.refusals if verdict in _REFUSING else outcome.notes).append(line) return outcome -def floor(repo: Path, base_ref: str, head_ref_rev: str, branch: str | None) -> int: - repo = repo.resolve() - base_sha, head_sha = resolve(repo, base_ref, "base"), resolve(repo, head_ref_rev, "head") - fork = merge_base(repo, base_sha, head_sha) - base, head = Revision(repo, base_sha), Revision(repo, head_sha) - revs = Revs(base, Revision(repo, fork), head) - _out(f"== floor: base {base_sha[:12]}, head {head_sha[:12]}, merge-base {fork[:12]}") - packs = packs_changed(revs) - outcome = judge_gates(repo, base, head, fork, branch, packs) - if not base.paths(COMPILED_PREFIX): +def _declared(base: Revision, policy: str) -> str: + """The strictest action the base's compiled gates of `policy` declare, on either surface.""" + actions = [] + for surface in _SURFACES: + try: + spec = json.loads(base.text(f"{COMPILED_PREFIX}{policy}/{surface}/gate.json") or "null") + except ValueError: + spec = None + if isinstance(spec, dict) and spec.get("action", ACTION_BLOCK) in _ACTION_RANK: + actions.append(spec.get("action", ACTION_BLOCK)) + return max(actions or [ACTION_BLOCK], key=_ACTION_RANK.__getitem__) + + +def _judged(repo: Path, base: Revision, head: Revision, branch: str | None) -> Outcome: + plane = cp.from_base(base) + listed = [path for path in changed_paths(repo, base.sha, head.sha) if plane.covers(path)] + level = committed_rollout(repo, base.sha) + outcome = judge_gates(repo, base, head, branch, plane=plane, level=level) + outcome.listed, outcome.mode = listed, plane.mode + if not base_gates(base): outcome.notes.append(_NO_GATES) - tampered = [t.render() for t in tamper(revs, packs)] try: - history, history_notes = history_findings(repo, base, head, fork, packs) + history, notes = history_findings(repo, base, head, plane.covers) except FloorError as exc: - history, history_notes = [], [] + history, notes = [], [] outcome.undecided.append(inert(str(exc))) - uncovered = not_covered(revs) - history_notes += pack_notes(revs, packs) + ([_NOT_COVERED.format(", ".join(uncovered))] if uncovered else []) - for line in outcome.notes + history_notes: + for policy, finding in history: + verdict = min(_declared(base, policy), _ROLLOUT_CEILING[level], key=_ACTION_RANK.__getitem__) + (outcome.refusals if verdict in _REFUSING else outcome.notes).append( + f"{policy} [history] ({verdict}): {finding}" + ) + outcome.notes += notes + return outcome + + +def _print(outcome: Outcome) -> None: + if outcome.listed: + head = "FAIL" if outcome.mode == cp.REFUSE else "LISTED" + text = _CONTROL_PLANE.format(rule=cp.RULE, mode=outcome.mode, count=len(outcome.listed)) + _out(f"{head} {text}") + for path in outcome.listed: + _out(f" {inert(path)}") + if outcome.mode == cp.LISTED: + _out(f"note: {_LISTED_GAP.format(rule=cp.RULE)}") + for line in outcome.notes: _out(f"note: {line}") - refusals = outcome.refusals + tampered + history - for line in refusals: + for line in outcome.refusals: _out(f"FAIL {line}") for line in outcome.undecided: _err(f"UNDECIDED {line}") _out(_NOT_REQUIRED) + + +def _code(outcome: Outcome) -> int: if outcome.undecided: return EXIT_UNDECIDED - return EXIT_REFUSED if refusals else 0 + refused = outcome.refusals or (outcome.listed and outcome.mode == cp.REFUSE) + return EXIT_REFUSED if refused else 0 + + +def floor(repo: Path, base_ref: str, head_ref_rev: str, branch: str | None, sarif: str | None = None) -> int: + repo = repo.resolve() + base_sha, head_sha = resolve(repo, base_ref, "base"), resolve(repo, head_ref_rev, "head") + require_ancestor(repo, base_sha, head_sha) + _out(f"== floor (preview): base {base_sha[:12]}, head {head_sha[:12]}") + base, head = Revision(repo, base_sha), Revision(repo, head_sha) + if not base.paths(".chock/"): + _out(_FIRST_ADOPTION) + return 0 + outcome = _judged(repo, base, head, branch) + _print(outcome) + code = _code(outcome) + return floor_sarif.write(repo, sarif, outcome, code) if sarif else code def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(prog="chock check --floor") parser.add_argument("--repo", default=".") parser.add_argument("--base", required=True, help="The pull request's base: its gates, waivers and rules bind") - parser.add_argument("--head", default="HEAD", help="The commit under test, read as objects only") + parser.add_argument("--head", default="HEAD", help="The merge result under test, read as objects only") parser.add_argument("--head-ref", default=None, help="The branch under test, for branch gates") + parser.add_argument("--sarif", default=None, help="Also write the result as SARIF 2.1.0 to this file") args = parser.parse_args(argv) try: - return floor(Path(args.repo), args.base, args.head, args.head_ref) + return floor(Path(args.repo), args.base, args.head, args.head_ref, args.sarif) except FloorError as exc: - _err(f"floor: {inert(str(exc))}") + _err(f"UNDECIDED floor: {inert(str(exc))}") except Exception as exc: # noqa: BLE001 -- 1 means refused; anything undecided is 2, naming only the type - _err(f"floor: no verdict was established ({type(exc).__name__})") + _err(f"UNDECIDED floor: no verdict was established ({type(exc).__name__})") return EXIT_UNDECIDED diff --git a/src/chock/validation/control_plane.py b/src/chock/validation/control_plane.py new file mode 100644 index 00000000..a064d491 --- /dev/null +++ b/src/chock/validation/control_plane.py @@ -0,0 +1,90 @@ +"""The floor's control plane: the paths that define the guardrails, and what the base says to do when one changes. + +No file is read for meaning: a listed path that changed is reported, whatever the change. The base's +`.chock/config.yaml` alone sets the outcome and the patterns; the head's config is never read. +""" + +from __future__ import annotations + +import fnmatch +import json +import unicodedata +from dataclasses import dataclass +from importlib import resources + +import yaml + +from chock import yamlio +from chock.validation.floor_git import FloorError, Revision + +RULE = "floor/control-plane-changed" +REFUSE, LISTED = "refuse", "listed" +_MODES = (REFUSE, LISTED) +_CONFIG = ".chock/config.yaml" +_SECTION, _MODE, _PATHS = "floor", "control_plane", "control_plane_paths" +_EDITS = ("add", "remove") +_FORMAT = "Cf" + + +def default_patterns() -> tuple[str, ...]: + text = resources.files("chock").joinpath("data/control_plane.json").read_text(encoding="utf-8") + return tuple(json.loads(text)["paths"]) + + +def folded(path: str) -> str: + """`path` as a case-blind, normalising filesystem may open it: NFC, no format characters, case folded, + and no trailing dot or space on any component (Windows drops them).""" + text = "".join(c for c in unicodedata.normalize("NFC", path) if unicodedata.category(c) != _FORMAT) + return "/".join(part.rstrip(". ") for part in text.casefold().split("/")) + + +@dataclass(frozen=True) +class ControlPlane: + mode: str + patterns: tuple[str, ...] + + def covers(self, path: str) -> bool: + name = folded(path) + return any(fnmatch.fnmatchcase(name, folded(pattern)) for pattern in self.patterns) + + +def _invalid(what: str) -> FloorError: + return FloorError(f"the base's {_CONFIG} {_SECTION}.{what}") + + +def _section(base: Revision) -> dict: + text = base.text(_CONFIG) + try: + doc = yamlio.safe_load(text) if text else None + except yaml.YAMLError as exc: + msg = "could not be read as YAML" + raise _invalid(msg) from exc + section = doc.get(_SECTION) if isinstance(doc, dict) else None + if section is not None and not isinstance(section, dict): + msg = "is not a mapping" + raise _invalid(msg) + return section or {} + + +def _edits(section: dict) -> dict[str, list[str]]: + edits = section.get(_PATHS) or {} + if not isinstance(edits, dict) or set(edits) - set(_EDITS): + msg = f"{_PATHS} takes only {' and '.join(_EDITS)}" + raise _invalid(msg) + found = {key: edits.get(key) or [] for key in _EDITS} + if any(not isinstance(v, list) or not all(isinstance(p, str) and p for p in v) for v in found.values()): + msg = f"{_PATHS} lists must hold path patterns" + raise _invalid(msg) + return found + + +def from_base(base: Revision) -> ControlPlane: + """The control plane the base defines; FloorError (undecided) for a `floor:` section it cannot read.""" + section = _section(base) + mode = section.get(_MODE, REFUSE) + if mode not in _MODES: + msg = f"{_MODE} is {mode!r}; it takes {' or '.join(_MODES)}" + raise _invalid(msg) + edits = _edits(section) + kept = tuple(p for p in default_patterns() if p not in edits["remove"]) + return ControlPlane(mode, kept + tuple(edits["add"])) diff --git a/src/chock/validation/floor_cli.py b/src/chock/validation/floor_cli.py index 5516c5d6..cfcef739 100644 --- a/src/chock/validation/floor_cli.py +++ b/src/chock/validation/floor_cli.py @@ -11,17 +11,19 @@ def add_arguments(parser: argparse.ArgumentParser) -> None: parser.add_argument( "--floor", action="store_true", - help="Judge base...head with the base's gates, waivers and tamper rules, run by this engine; " - "no head file executes (needs --base; runs alone)", + help="Preview: judge base..head (base an ancestor of head: pass the merge result) with the base's gates, " + "run by this engine; a changed guardrail path is listed and, by default, refused (needs --base; runs alone)", ) - parser.add_argument("--head", default=None, help="With --floor: the commit under test (default HEAD)") + parser.add_argument("--head", default=None, help="With --floor: the merge result under test (default HEAD)") def from_check(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int: """The floor runs alone: every other check reads, and the evals run, the head's own files.""" if not args.floor: parser.error("--head goes with --floor") - if not args.base or args.only or args.mode or args.event or args.format != "text" or args.output: - parser.error("--floor needs --base and takes no --only, --mode, --event, --format or --output") + sarif = args.format == "sarif" + if not args.base or args.only or args.mode or args.event or bool(args.output) != sarif: + parser.error("--floor needs --base, takes --format sarif only with --output, and no --only, --mode or --event") argv = ["--repo", args.repo, "--base", args.base, "--head", args.head or "HEAD"] - return main(argv + (["--head-ref", args.head_ref] if args.head_ref else [])) + argv += ["--head-ref", args.head_ref] if args.head_ref else [] + return main(argv + (["--sarif", args.output] if sarif else [])) diff --git a/src/chock/validation/floor_context.py b/src/chock/validation/floor_context.py index c35e3e9e..a0802a95 100644 --- a/src/chock/validation/floor_context.py +++ b/src/chock/validation/floor_context.py @@ -1,9 +1,9 @@ -"""The material a gate judges at the floor: the base...head range, read by revision, never from the working tree.""" +"""The material a gate judges at the floor: the tree diff base..head, read by revision, never from the working tree.""" from __future__ import annotations import subprocess -from collections.abc import Sequence +from collections.abc import Callable, Sequence from pathlib import Path from chock.gate.runner import GateContext @@ -12,31 +12,32 @@ class FloorContext(GateContext): - """`base...head` as the change; the head's files as the proposal; the base's as what a waiver must already be in. + """`base..head` as the change; the head's files as the proposal; the base's as what a waiver must already be in. Every read is a `git show :`, so a head file is data and never reaches a program as code. - Diffs run with `--text`, so neither a NUL byte nor a head `.gitattributes` (`-diff`) hides added lines. - A waiver counts only on a line the pull request leaves unchanged: no line it adds can carry one, even a - copy of a line the base already has. + Diffs run with `--text` and `--no-renames`, so neither a NUL byte, a head `.gitattributes` nor a rename + hides a line or a deletion. A control-plane path is never judged here: the control-plane rule owns it. """ - def __init__( # noqa: PLR0913 -- the range's three revisions beside GateContext's own + def __init__( # noqa: PLR0913 -- the range and what it excludes, beside GateContext's own self, repo_root: Path, *, base: str, head: str, - merge_base: str, head_ref: str | None, - scope: Sequence[str] | None = None, - own: Sequence[str] = (), + scope: Sequence[str] | None, + control_plane: Callable[[str], bool], ) -> None: - super().__init__(repo_root=repo_root, base=base, head_ref=head_ref, scope=scope, own=own) + super().__init__(repo_root=repo_root, base=base, head_ref=head_ref, scope=scope) self.head = head - self.merge_base = merge_base + self.control_plane = control_plane + + def in_scope(self, path: str, *, judge_own: bool = False) -> bool: + return not self.control_plane(path) and super().in_scope(path, judge_own=judge_own) def _range(self) -> list[str]: - return [f"{self.base}...{self.head}"] + return ["--no-renames", str(self.base), self.head] def _diff_text(self, path: str) -> str: args = ("diff", *self._range(), "--text", "-U0", "--no-color", "--no-ext-diff", "--no-textconv", "--", path) @@ -58,15 +59,17 @@ def staged_blob(self, path: str) -> str: return self._git("show", f"{self.head}:{path}") def head_blob(self, path: str) -> str: - """Before the change: the file at the merge-base, the same side the three-dot diff compares against.""" - return self._git("show", f"{self.merge_base}:{path}") + """Before the change: the file at the base.""" + return self._git("show", f"{self.base}:{path}") def committed_blob(self, path: str) -> str: - """Where a waiver must already be for the floor to honour it: the base's lines, less every line this - pull request adds to `path` (an added line is changed by definition, so its waiver never counts).""" - added = set(_diff_sides(self._diff_text(path))[0]) - lines = self._git("show", f"{self.base}:{path}").splitlines() - return "\n".join(line for line in lines if line not in added) + """Where a waiver must be for the floor to honour it: the lines present and unchanged in both base and + head. A line the pull request adds, changes or removes never carries a waiver, even a copy of a base line.""" + added, removed = _diff_sides(self._diff_text(path)) + touched = set(added) | set(removed) + kept = set(self.staged_blob(path).splitlines()) + lines = self.head_blob(path).splitlines() + return "\n".join(line for line in lines if line in kept and line not in touched) def current_branch(self) -> str: """The branch under test is the one named; the checkout's own branch is the base's, never the head's.""" diff --git a/src/chock/validation/floor_git.py b/src/chock/validation/floor_git.py index 37833a38..32a633ca 100644 --- a/src/chock/validation/floor_git.py +++ b/src/chock/validation/floor_git.py @@ -72,10 +72,6 @@ def paths(self, prefix: str = "") -> list[str]: def has(self, path: str) -> bool: return path in self.entries() - def oid(self, path: str) -> str | None: - entry = self.entries().get(path) - return entry[1] if entry else None - def blob(self, path: str) -> bytes | None: """The file's bytes as committed; None when the tree has no such file.""" entry = self.entries().get(path) @@ -105,10 +101,22 @@ def materialise(self, prefix: str, into: Path) -> None: target.write_bytes(self.blob(path) or b"") -def merge_base(repo: Path, base: str, head: str) -> str: - proc = git(repo, "merge-base", base, head) - sha = proc.stdout.decode("ascii", "replace").strip() - if proc.returncode or not sha: - msg = f"base {base[:12]} and head {head[:12]} share no history; refusing to judge an unrelated range" +def require_ancestor(repo: Path, base: str, head: str) -> None: + """FloorError (undecided) unless `base` is an ancestor of `head`: the floor judges what will land, not a branch.""" + proc = git(repo, "merge-base", "--is-ancestor", base, head) + if proc.returncode: + msg = ( + f"base {base[:12]} is not an ancestor of head {head[:12]}; pass the pull request's merge result " + "as --head (refs/pull//merge, or the merge queue's head)" + ) raise FloorError(msg) - return sha + + +def changed_paths(repo: Path, base: str, head: str) -> list[str]: + """Every path the tree diff base..head adds, changes, deletes or re-modes; renames are a delete and an add.""" + args = ("--no-renames", "--no-ext-diff", "--no-textconv", "--no-relative", "--ignore-submodules=none") + proc = git(repo, "diff", "--name-only", "-z", *args, base, head, "--") + if proc.returncode: + msg = f"git diff {base[:12]} {head[:12]} failed" + raise FloorError(msg) + return sorted(os.fsdecode(raw) for raw in proc.stdout.split(b"\0") if raw) diff --git a/src/chock/validation/floor_history.py b/src/chock/validation/floor_history.py index 4db7fc2a..677d24fa 100644 --- a/src/chock/validation/floor_history.py +++ b/src/chock/validation/floor_history.py @@ -1,28 +1,28 @@ -"""The floor's per-commit pass: each commit in merge-base..head judged with the base's content gates. +"""The floor's per-commit pass: each commit in base..head judged with the base's content gates. The cumulative diff misses a secret added in one commit and removed in the next; this pass does not. -A line the base already has is not the pull request's, and a waiver the pull request adds is not -honoured, so the gates run with no waiver pattern and lines present at the base are dropped. - -It fails closed: a range longer than the commit limit, or a blob it does not judge, is undecided, -never a silent skip. Binary blobs are judged too (bytes that are not utf-8 are read as latin-1). +Each blob a commit adds or changes outside the control plane is judged whole, in every reading of +`scan.floor_texts`, with no waiver; a finding whose line (same reading, same digest) the base already +has at that path is not the pull request's. It fails closed: a range past the commit limit, or a blob +over the size limit, is undecided, naming the commit and path. +Not seen: other encodings (utf-32, compressed or encoded content), commit messages, file names. """ from __future__ import annotations import dataclasses import tempfile +from collections.abc import Callable from pathlib import Path from chock.display import inert from chock.gate.runner import GateContext from chock.history import gitlog from chock.history.gates import RULE_CONTENT, load_rules -from chock.history.scan import Finding, Limits, scan +from chock.history.scan import Finding, Limits, _digest, floor_lines, scan from chock.validation.floor_git import FloorError, Revision _COMPILED = ".chock/compiled/" -_POLICIES = ".agents/policies/" _SURFACES = ("ci-gate", "git-hook") _SHORT = 12 #: The largest blob the floor reads into memory to judge; a larger one is undecided, named by commit and path. @@ -30,27 +30,15 @@ NO_CONTENT_GATES = "history: the base has no content gates on either surface; per-commit pass not run" -def _introduced(finding: Finding, head: Revision, at_base: tuple[Revision, ...]) -> bool: - """Whether the finding is the pull request's: its line (or path) is not already at the base.""" - if finding.rule != RULE_CONTENT or not finding.line: - return not any(rev.has(finding.path) for rev in at_base) - commit = Revision(head.repo, finding.commit) - lines = (commit.text(finding.path) or "").splitlines() - line = lines[finding.line - 1] if finding.line <= len(lines) else None - return line is None or not any(line in (rev.text(finding.path) or "").splitlines() for rev in at_base) - - -def _limits() -> Limits: - return Limits(max_blob_bytes=FLOOR_MAX_BLOB_BYTES, judge_binary=True) - +def _introduced(finding: Finding, base: Revision) -> bool: + """Whether the finding is the pull request's: its line (or, for a path finding, its path) is not at the base.""" + if finding.rule != RULE_CONTENT: + return not base.has(finding.path) + data = base.blob(finding.path) + return data is None or finding.key not in {_digest(line) for line in floor_lines(data)} -def history_findings( - repo: Path, base: Revision, head: Revision, fork: str, exempt: frozenset[str] = frozenset() -) -> tuple[list[str], list[str]]: - """(refusals, notes) of the per-commit pass; FloorError (undecided) when it cannot judge all of it. - `exempt` are the packs whose lock entry the pull request changed: only their source folder is skipped. - """ +def _report(repo: Path, base: Revision, head: Revision, limits: Limits): with tempfile.TemporaryDirectory(prefix="chock-floor-history-") as scratch: root = Path(scratch) base.materialise(_COMPILED, root) @@ -58,41 +46,43 @@ def history_findings( rules, _skipped = load_rules(root, _SURFACES) except gitlog.HistoryError as exc: if "no installed content gates" in str(exc): - return [], [NO_CONTENT_GATES] + return None msg = f"history: {exc}" raise FloorError(msg) from exc rules = [ - dataclasses.replace( - rule, - pragma_pattern=None, - judge=GateContext( - repo_root=repo, - scope=rule.judge.scope, - own=(f"{_POLICIES}{rule.policy}/",) if rule.policy in exempt else (), - ), - ) + dataclasses.replace(rule, pragma_pattern=None, judge=GateContext(repo_root=repo, scope=rule.judge.scope)) for rule in rules ] - limits = _limits() try: - report = scan(repo, rules, fork, limits, head.sha) + return scan(repo, rules, base.sha, limits, head.sha) except gitlog.HistoryError as exc: msg = f"history: {exc}" raise FloorError(msg) from exc + + +def history_findings( + repo: Path, base: Revision, head: Revision, control_plane: Callable[[str], bool] +) -> tuple[list[tuple[str, str]], list[str]]: + """((policy, finding) per refusal candidate, notes) of the per-commit pass; FloorError (undecided) when it + cannot judge all of it.""" + limits = Limits(max_blob_bytes=FLOOR_MAX_BLOB_BYTES, judge_binary=True) + report = _report(repo, base, head, limits) + if report is None: + return [], [NO_CONTENT_GATES] if report.truncated: msg = ( - f"history: {fork[:_SHORT]}..{head.sha[:_SHORT]} holds more than {limits.max_commits} commits; " + f"history: {base.sha[:_SHORT]}..{head.sha[:_SHORT]} holds more than {limits.max_commits} commits; " "the commits past the limit were not judged" ) raise FloorError(msg) - if report.unread: - named = "; ".join(f"{c[:_SHORT]} {inert(p)} ({why})" for c, p, why in report.unread) + unread = [(c, p, why) for c, p, why in report.unread if not control_plane(p)] + if unread: + named = "; ".join(f"{c[:_SHORT]} {inert(p)} ({why})" for c, p, why in unread) msg = f"history: blobs not judged: {named}" raise FloorError(msg) - at_base = (base, Revision(repo, fork)) - lines = [ - f"{f.policy} [history]: {f.commit[:_SHORT]} {inert(f.path)}{f':{f.line}' if f.line else ''} ({f.rule})" + found = [ + (f.policy, f"{f.commit[:_SHORT]} {inert(f.path)}{f':{f.line}' if f.line else ''} ({f.rule})") for f in report.findings - if _introduced(f, head, at_base) + if not control_plane(f.path) and _introduced(f, base) ] - return lines, [] + return found, [] diff --git a/src/chock/validation/floor_sarif.py b/src/chock/validation/floor_sarif.py new file mode 100644 index 00000000..ae72d7b5 --- /dev/null +++ b/src/chock/validation/floor_sarif.py @@ -0,0 +1,51 @@ +"""`chock check --floor --sarif `: the floor's result as one SARIF 2.1.0 log; the exit code is unchanged.""" + +from __future__ import annotations + +import json +import sys +from pathlib import Path +from typing import TYPE_CHECKING + +from chock import __version__ +from chock.gate.sarif import INFORMATION_URI, SARIF_SCHEMA, safe_text +from chock.gate.sarif_output import write_output +from chock.validation import control_plane as cp + +if TYPE_CHECKING: + from chock.validation.checks_floor import Outcome + +_REFUSED = "floor/refused" +_UNDECIDED_EXIT = 2 + + +def _result(rule: str, level: str, text: str, path: str | None = None) -> dict: + result: dict = {"ruleId": rule, "level": level, "message": {"text": safe_text(text)}} + if path is not None: + result["locations"] = [{"physicalLocation": {"artifactLocation": {"uri": safe_text(path)}}}] + return result + + +def log(outcome: Outcome) -> dict: + """Control-plane paths first, one result each; then every refusal; undecided lines as notifications.""" + level = "error" if outcome.mode == cp.REFUSE else "note" + why = f"control-plane path changed ({outcome.mode}); the floor does not judge whether this is safe" + results = [_result(cp.RULE, level, why, path) for path in outcome.listed] + results += [_result(_REFUSED, "error", line) for line in outcome.refusals] + notes = [{"level": "error", "message": {"text": safe_text(line)}} for line in outcome.undecided] + driver = {"name": "chock", "version": __version__, "informationUri": INFORMATION_URI} + run = { + "tool": {"driver": driver}, + "invocations": [{"executionSuccessful": not outcome.undecided, "toolExecutionNotifications": notes}], + "results": results, + } + return {"$schema": SARIF_SCHEMA, "version": "2.1.0", "runs": [run]} + + +def write(repo: Path, output: str, outcome: Outcome, code: int) -> int: + """Write the log; `code` stands unless the file cannot be written, which is undecided.""" + text = json.dumps(log(outcome), indent=2) + "\n" + if failure := write_output(Path(output), text, repo): + sys.stderr.write(f"UNDECIDED floor: cannot write --sarif: {failure}\n") + return _UNDECIDED_EXIT + return code diff --git a/src/chock/validation/tamper.py b/src/chock/validation/tamper.py deleted file mode 100644 index 3d8d0c2f..00000000 --- a/src/chock/validation/tamper.py +++ /dev/null @@ -1,130 +0,0 @@ -"""The floor's built-in tamper rules: a guardrail the merge-base has and the head removed or weakened, by name. - -Every rule compares the merge-base with the head (the base tip may have moved on since the pull -request forked; what it added there is not the pull request's to keep). The facts live in -`data/tamper.json`, inside the engine, so the pull request under judgement cannot edit them. -""" - -from __future__ import annotations - -import json -from dataclasses import dataclass -from functools import cache -from typing import Any - -from chock.display import inert -from chock.resources import package_data_dir -from chock.validation.floor_git import Revision - -_SHOWN = 120 -PACK_LIMIT = ( - "floor/pack-changed: the floor cannot tell a policy upgrade from a policy weakening offline; it lists every " - "pack whose content or lock entry changed, and the base's gates still judge the pull request" -) - - -@cache -def facts() -> dict[str, Any]: - return json.loads(package_data_dir("chock", "data").joinpath("tamper.json").read_text(encoding="utf-8")) - - -@dataclass(frozen=True) -class Tamper: - rule: str - path: str - detail: str - - def render(self) -> str: - return f"tamper/{self.rule} {inert(self.path)}: {inert(self.detail)}" - - -@dataclass(frozen=True) -class Revs: - """The three commits a floor run reads: the base tip (whose config binds), the merge-base and the head.""" - - base: Revision - fork: Revision - head: Revision - - -def shown(text: str) -> str: - return text if len(text) <= _SHOWN else text[: _SHOWN - 3] + "..." - - -def as_json(text: str | None) -> Any: - if text is None: - return None - try: - return json.loads(text) - except ValueError: - return None - - -def changed(revs: Revs, prefix: str) -> list[str]: - """Paths under `prefix` whose blob differs between the merge-base and the head (added, edited or deleted).""" - paths = set(revs.fork.paths(prefix)) | set(revs.head.paths(prefix)) - return sorted(path for path in paths if revs.fork.oid(path) != revs.head.oid(path)) - - -def _lock_entries(rev: Revision) -> dict[str, str]: - doc = as_json(rev.text(facts()["lock_file"])) - packs = doc.get("packs") if isinstance(doc, dict) else None - return {str(p["id"]): json.dumps(p, sort_keys=True) for p in packs or () if isinstance(p, dict) and "id" in p} - - -def packs_changed(revs: Revs) -> frozenset[str]: - """Pack ids whose `chock.lock` entry the pull request added or changed (merge-base -> head).""" - was, now = _lock_entries(revs.fork), _lock_entries(revs.head) - return frozenset(pack for pack, entry in now.items() if was.get(pack) != entry) - - -def lock_pack_removed(revs: Revs) -> list[Tamper]: - lock = facts()["lock_file"] - gone = sorted(set(_lock_entries(revs.fork)) - set(_lock_entries(revs.head))) - return [ - Tamper("lock-pack-removed", lock, f"pack {pack!r} is locked at the merge-base, missing at the head") - for pack in gone - ] - - -def _pack_of(path: str) -> str: - for prefix in (facts()["policies_prefix"], facts()["compiled_prefix"]): - if path.startswith(prefix): - return path.removeprefix(prefix).partition("/")[0] - return "" - - -def pack_notes(revs: Revs, packs: frozenset[str]) -> list[str]: - """One `floor/pack-changed` note per pack whose content or lock entry changed, naming every file of it the - pull request changed; only a pack with a lock change has its source exempt from its own gates.""" - touched = {_pack_of(p) for prefix in ("policies_prefix", "compiled_prefix") for p in changed(revs, facts()[prefix])} - notes: list[str] = [] - for pack in sorted((touched - {""}) | packs): - source = changed(revs, f"{facts()['policies_prefix']}{pack}/") - compiled = changed(revs, f"{facts()['compiled_prefix']}{pack}/") - if pack in packs: - detail = f"floor/pack-changed: {pack}: lock entry changed" - if source: - detail += f"; its own gates do not judge its source: {', '.join(map(inert, source))}" - else: - detail = f"floor/pack-changed: {pack}: no lock change, so every gate judged it" - if source: - detail += f"; source: {', '.join(map(inert, source))}" - if compiled: - detail += f"; compiled: {', '.join(map(inert, compiled))}" - notes.append(detail) - return [*notes, PACK_LIMIT] if notes else [] - - -def tamper(revs: Revs, packs: frozenset[str]) -> list[Tamper]: - """Every removal or weakening of a guardrail from merge-base to head, in rule order.""" - from chock.validation import tamper_files, tamper_hooks # noqa: PLC0415 -- they import this module's facts - - return [ - *tamper_hooks.hook_removed(revs), - *tamper_hooks.hooks_switched_off(revs), - *tamper_files.gate_rules(revs, packs), - *lock_pack_removed(revs), - *tamper_files.runtime_rules(revs), - *tamper_files.floor_workflow(revs), - ] diff --git a/src/chock/validation/tamper_files.py b/src/chock/validation/tamper_files.py deleted file mode 100644 index 8bd5cd6b..00000000 --- a/src/chock/validation/tamper_files.py +++ /dev/null @@ -1,162 +0,0 @@ -"""Tamper rules for files chock writes: compiled gates, the vendored runtimes and the floor's own workflow. - -A runtime file or the floor workflow must be byte-equal to the merge-base or to what this engine -renders; anything else is refused by name. No structural diff is attempted, so `if: false`, -`continue-on-error`, a narrowed trigger or an added PATH-shadowing step are refused like any edit. -These files change only through a privileged merge: first a pull request that moves the floor -workflow's engine pin, then the regeneration, which the new engine renders byte-equal. -""" - -from __future__ import annotations - -import fnmatch -import tempfile -from pathlib import Path - -import yaml - -from chock import yamlio -from chock.gate.runner.constants import _ACTION_RANK -from chock.validation.floor_git import FloorError, Revision -from chock.validation.tamper import Revs, Tamper, as_json, changed, facts -from chock.validation.tamper_hooks import hook_files - -_CONFIG = ".chock/config.yaml" -_PRIVILEGED = "changes only through a privileged merge (first the engine pin, then a regeneration it renders)" - - -def _gate_paths(rev: Revision) -> list[str]: - prefix, name = facts()["compiled_prefix"], facts()["gate_file"] - globs = [f"{prefix}*/{surface}/{name}" for surface in facts()["gate_surfaces"]] - return [path for path in rev.paths(prefix) if any(fnmatch.fnmatchcase(path, g) for g in globs)] - - -def _how(was: dict, now: object) -> str: - if not isinstance(now, dict): - return "gate is not a JSON object at the head" - lost = sorted(set(map(str, was.get("on") or [])) - set(map(str, now.get("on") or []))) - if lost: - return f"no longer bound to: {', '.join(lost)}" - was_action, now_action = str(was.get("action", "block")), str(now.get("action", "block")) - if _ACTION_RANK.get(now_action, -1) < _ACTION_RANK.get(was_action, -1): - return f"action {was_action} -> {now_action}" - return "kind, params, paths or message changed" - - -def gate_rules(revs: Revs, packs: frozenset[str]) -> list[Tamper]: - """A compiled gate the merge-base has: deleted, or changed with no lock change for its pack.""" - found: list[Tamper] = [] - prefix = facts()["compiled_prefix"] - for path in _gate_paths(revs.fork): - if not revs.head.has(path): - found.append(Tamper("gate-removed", path, "compiled gate present at the merge-base, deleted at the head")) - continue - if revs.head.oid(path) == revs.fork.oid(path): - continue - pack = path.removeprefix(prefix).partition("/")[0] - if pack in packs: - continue # listed under floor/pack-changed - was = as_json(revs.fork.text(path)) - detail = _how(was if isinstance(was, dict) else {}, as_json(revs.head.text(path))) - found.append(Tamper("gate-changed", path, f"{detail}, and chock.lock has no change for {pack!r}")) - return found - - -# --- what this engine renders ------------------------------------------------------------------- - - -def _runtime_render(name: str) -> bytes | None: - from chock.hooks.launch import launcher_text # noqa: PLC0415 -- heavy imports, only when a runtime changed - from chock.vendored import VENDORED_RUNTIMES, expected_runtime # noqa: PLC0415 - - if f"{facts()['runtime_prefix']}{name}" == facts()["launcher"]: - return launcher_text().encode("utf-8") - if name in VENDORED_RUNTIMES: - return expected_runtime(*VENDORED_RUNTIMES[name]) - return None - - -def _vendor(index_path: str) -> str: - return index_path.removeprefix(facts()["index_prefix"]).split("-", 1)[0] - - -def rendered_indexes(head: Revision, vendors: set[str]) -> dict[str, bytes] | None: - """{path: bytes} of the pinned indexes `chock sync` writes for `vendors` from the head's committed policy - data (read as data; nothing of it runs); None when the engine cannot render them.""" - from chock.hooks.in_agent_install import install_hooks # noqa: PLC0415 -- only when an index changed - - index = facts()["index_prefix"] - with tempfile.TemporaryDirectory(prefix="chock-floor-render-") as scratch: - shadow = Path(scratch) - prefixes = [facts()["compiled_prefix"], facts()["policies_prefix"], _CONFIG] - prefixes += [p for _, p in hook_files(head.paths())] - for prefix in prefixes: - head.materialise(prefix, shadow) - try: - for vendor in sorted(vendors): - install_hooks(shadow, vendor) - except Exception: # noqa: BLE001 -- any failure to render is "not rendered", which refuses - return None - folder = shadow / index - return { - f"{index}{p.name}": p.read_bytes() - for p in (folder.glob("*.json") if folder.is_dir() else []) - if _vendor(f"{index}{p.name}") in vendors - } - - -def runtime_rules(revs: Revs) -> list[Tamper]: - """Every `.chock/bin/` file the pull request changed must be the merge-base's bytes or this engine's render.""" - prefix, index = facts()["runtime_prefix"], facts()["index_prefix"] - paths = changed(revs, prefix) - vendors = {_vendor(p) for p in paths if p.startswith(index)} - rendered = rendered_indexes(revs.head, vendors) if vendors else {} - found: list[Tamper] = [] - for path in paths: - now = revs.head.blob(path) - if path.startswith(index): - expected = None if rendered is None else rendered.get(path) - else: - expected = _runtime_render(path.removeprefix(prefix)) - if now is None and path.startswith(index) and rendered is not None and path not in rendered: - continue # a stale index the engine's own sync prunes - if now is None: - found.append(Tamper("runtime-removed", path, f"vendored runtime file deleted; it {_PRIVILEGED}")) - elif now != expected: - found.append( - Tamper("runtime-changed", path, f"neither the merge-base's bytes nor this engine's; it {_PRIVILEGED}") - ) - return found - - -# --- the floor's own workflow ------------------------------------------------------------------- - - -def workflow_path(base: Revision) -> str: - """The workflow the floor guards: the base config's `floor_workflow`, else the default.""" - try: - config = yamlio.safe_load(base.text(_CONFIG) or "") - except yaml.YAMLError as exc: - msg = f"{_CONFIG} at the base does not parse; cannot tell which workflow the floor guards" - raise FloorError(msg) from exc - named = config.get(facts()["workflow_config_key"]) if isinstance(config, dict) else None - return named if isinstance(named, str) and named.strip() else facts()["workflow_default"] - - -def _workflow_renders() -> list[bytes]: - from chock.scaffold import install_ci # noqa: PLC0415 - - return [install_ci.render().encode("utf-8")] - - -def floor_workflow(revs: Revs) -> list[Tamper]: - path = workflow_path(revs.base) - if revs.head.oid(path) == revs.fork.oid(path): - return [] - now = revs.head.blob(path) - if now is None: - return [Tamper("floor-workflow-changed", path, f"the floor's workflow was deleted; it {_PRIVILEGED}")] - if now in _workflow_renders(): - return [] - detail = f"neither the merge-base's bytes nor this engine's render; the floor's workflow {_PRIVILEGED}" - return [Tamper("floor-workflow-changed", path, detail)] diff --git a/src/chock/validation/tamper_hooks.py b/src/chock/validation/tamper_hooks.py deleted file mode 100644 index 87f97af2..00000000 --- a/src/chock/validation/tamper_hooks.py +++ /dev/null @@ -1,131 +0,0 @@ -"""Tamper rules for agent hook registrations: an entry removed or retyped, or every hook switched off. - -A hook entry's identity is (event, matcher, type, command). A matcher may widen, and the index pin a -regeneration rewrites (`--index-sha256 `) is normalised; nothing else may change. -""" - -from __future__ import annotations - -import fnmatch -import re -from dataclasses import dataclass -from typing import Any - -from chock.validation.tamper import Revs, Tamper, as_json, facts, shown - -_UNIVERSAL = frozenset({"", "*"}) - - -@dataclass(frozen=True) -class Hook: - event: str - matchers: frozenset[str] - type: str - command: tuple[tuple[str, str], ...] - shown: str - - -def _norm(text: str) -> str: - return re.sub(facts()["volatile_pattern"], facts()["volatile_placeholder"], text) - - -def pointed(doc: Any, pointer: str) -> Any: - for part in [p for p in pointer.split("/") if p]: - doc = doc.get(part) if isinstance(doc, dict) else None - return doc - - -def _hook(event: str, matcher: object, item: dict) -> Hook | None: - keys = facts()["hook_command_keys"] - command = tuple((key, _norm(str(item[key]))) for key in keys if isinstance(item.get(key), str)) - if not command: - return None - raw = str(matcher) if isinstance(matcher, str) else "" - matchers = frozenset(part.strip() for part in raw.split(facts()["hook_matcher_separator"])) - kind = str(item.get(facts()["hook_type_key"], facts()["hook_type_default"])) - return Hook(event, matchers, kind, command, shown(command[0][1])) - - -def hooks(doc: Any, pointer: str) -> list[Hook]: - """Every (event, matcher, type, command) entry of a vendor hook file, nested or flat.""" - table = pointed(doc, pointer) - found: list[Hook] = [] - nested, matcher_key = facts()["hook_nested_key"], facts()["hook_matcher_key"] - for event, items in table.items() if isinstance(table, dict) else (): - for item in items if isinstance(items, list) else (): - if not isinstance(item, dict): - continue - inner = item.get(nested) - leaves = [h for h in inner if isinstance(h, dict)] if isinstance(inner, list) else [item] - for leaf in leaves: - entry = _hook(str(event), leaf.get(matcher_key, item.get(matcher_key)), leaf) - if entry is not None: - found.append(entry) - return found - - -def _covers(head: frozenset[str], base: frozenset[str]) -> bool: - return bool(head & _UNIVERSAL) or (not base & _UNIVERSAL and head >= base) - - -def _kept(entry: Hook, head: list[Hook]) -> bool: - return any( - (h.event, h.type, h.command) == (entry.event, entry.type, entry.command) and _covers(h.matchers, entry.matchers) - for h in head - ) - - -def hook_files(rev_paths: list[str]) -> list[tuple[dict, str]]: - """(spec, path) of every vendor hook file among `rev_paths`.""" - return [(spec, p) for spec in facts()["hook_files"] for p in rev_paths if fnmatch.fnmatchcase(p, spec["glob"])] - - -def hook_removed(revs: Revs) -> list[Tamper]: - found: list[Tamper] = [] - for spec, path in hook_files(revs.fork.paths()): - was = as_json(revs.fork.text(path)) - if was is None: - continue - now = as_json(revs.head.text(path)) - kept = hooks(now, spec["pointer"]) if now is not None else [] - for entry in hooks(was, spec["pointer"]): - if not _kept(entry, kept): - found.append(Tamper("hook-removed", path, f"{entry.event} {entry.type} hook removed: {entry.shown}")) - return found - - -def _switched_on(doc: Any, keys: list[str]) -> list[str]: - return [key for key in keys if pointed(doc, key) not in (None, False, 0, "", [], {})] - - -def hooks_switched_off(revs: Revs) -> list[Tamper]: - """A disable key turned on in a vendor hook file, or a higher-precedence file added carrying hooks or one.""" - found: list[Tamper] = [] - switches = facts()["hook_switches"] - for spec, path in hook_files(revs.head.paths()): - keys = switches.get(spec["vendor"], {}).get("disable_keys", []) - now = _switched_on(as_json(revs.head.text(path)), keys) - was = set(_switched_on(as_json(revs.fork.text(path)), keys)) - found += [Tamper("hooks-disabled", path, f"{key} turns every hook off") for key in now if key not in was] - for vendor, switch in sorted(switches.items()): - keys = switch.get("disable_keys", []) - for path in switch.get("override_files", []): - if not revs.head.has(path) or revs.head.oid(path) == revs.fork.oid(path): - continue - doc = as_json(revs.head.text(path)) - carries = doc is None or pointed(doc, facts()["hook_nested_key"]) is not None or _switched_on(doc, keys) - if carries: - found.append( - Tamper( - "hooks-overridden", - path, - f"{vendor} reads this file over its hook file; it carries hooks or a disable key, or does not parse", - ) - ) - return found - - -def not_covered(revs: Revs) -> list[str]: - """Vendors with a hook file at the merge-base whose switch-off keys this engine has not verified.""" - switches = facts()["hook_switches"] - return sorted({spec["vendor"] for spec, _ in hook_files(revs.fork.paths())} - set(switches)) diff --git a/tests/fixtures/cli_help_golden.txt b/tests/fixtures/cli_help_golden.txt index 579ede22..deebc2cf 100644 --- a/tests/fixtures/cli_help_golden.txt +++ b/tests/fixtures/cli_help_golden.txt @@ -8,7 +8,7 @@ Everyday: install Build a chock.sh selection into one plugin for one agent (install --selection [--client C]) remove Remove an installed policy and resync sync Recompile + rewire so the repo matches its policies (--ci/--skills for extras) - check Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor judges a pull request with its base's gates) + check Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor, a preview, judges a pull request's merge result with its base's gates) status Policy states and coverage (--only registry,log for more) stop Refuse every agent tool call, commit and push here until resume (--agent C for one client) resume Lift a stop: agents are judged by the policies again diff --git a/tests/floor_support.py b/tests/floor_support.py index 68d4251b..52bac271 100644 --- a/tests/floor_support.py +++ b/tests/floor_support.py @@ -111,10 +111,14 @@ def commit(repo: Path, message: str) -> str: return git(repo, "rev-parse", "HEAD") -def base_repo(tmp_path: Path) -> Path: - """`main` carries two gates, a probe script gate, hook files, a lock, a runtime, a workflow and a test; `pr` is checked out.""" +CONTROL_PLANE = "floor/control-plane-changed" + + +def base_repo(tmp_path: Path, mode: str = "refuse") -> Path: + """`main` carries two gates, a probe script gate, hook files, a lock, a runtime, a workflow and a test; `pr` is + checked out. `mode` is the base's floor.control_plane (refuse is the default and writes no config).""" repo = tmp_path / "repo" - repo.mkdir() + repo.mkdir(parents=True) git(repo, "init", "-q", "-b", "main") gate(repo, "test-integrity", TEST_INTEGRITY) gate(repo, "floor-token", FLOOR_TOKEN) @@ -128,6 +132,8 @@ def base_repo(tmp_path: Path) -> Path: write(repo, ".github/workflows/chock.yml", WORKFLOW) write(repo, "tests/test_core.py", TEST_FILE) write(repo, "src/app.py", "VALUE = 1\n") + if mode != "refuse": + write(repo, ".chock/config.yaml", f"floor:\n control_plane: {mode}\n") commit(repo, "base") git(repo, "checkout", "-q", "-b", "pr") return repo @@ -138,3 +144,16 @@ def run_floor(repo: Path, capsys, *extra: str) -> tuple[int, str]: code = floor_main(["--repo", str(repo), "--base", "main", *extra]) out = capsys.readouterr() return code, out.out + out.err + + +def control_plane_change(tmp_path: Path, capsys, change, *paths: str, extra: tuple[str, ...] = ()) -> None: + """`change(repo)` as the pull request: exit 1 under `refuse`, exit 0 under `listed`, each listing every path.""" + for mode, owed in (("refuse", 1), ("listed", 0)): + repo = base_repo(tmp_path / mode, mode) + change(repo) + code, out = run_floor(repo, capsys, *extra) + assert code == owed, f"{mode}: {out}" + assert f"{CONTROL_PLANE} ({mode})" in out, out + assert out.index(CONTROL_PLANE) < out.index("floor (preview): a floor only"), out + for path in paths: + assert f"\n {path}\n" in out, f"{mode}: {path} not listed\n{out}" diff --git a/tests/test_floor_attacks.py b/tests/test_floor_attacks.py index 47e0bd37..fbe91276 100644 --- a/tests/test_floor_attacks.py +++ b/tests/test_floor_attacks.py @@ -1,7 +1,9 @@ -"""Regression tests for the adversarial review of `chock check --floor` (content, history, hooks, runtimes). +"""Regression tests for the first adversarial review of `chock check --floor` (content, history, hooks, runtimes). -Each test is one attack from the review, asserting the exit code the floor owes: 1 refused, 2 undecided, -0 for a correct pull request. Workflow, waiver, script and argument attacks are in test_floor_attacks2.py. +Each test is one attack from the review, asserting the exit code the floor owes (W2-A1v2): a content or +history attack is refused (1) or undecided (2); a change to a control-plane path is refused under the base's +`refuse` (the default) and listed with exit 0 under `listed`. Workflow, waiver, script and argument attacks +are in test_floor_attacks2.py; the second review's are in test_floor_attacks3.py. """ from __future__ import annotations @@ -11,7 +13,17 @@ from pathlib import Path import pytest -from floor_support import CLAUDE, FLOOR_TOKEN, LOCK, base_repo, commit, git, run_floor, write +from floor_support import ( + CLAUDE, + FLOOR_TOKEN, + LOCK, + base_repo, + commit, + control_plane_change, + git, + run_floor, + write, +) from chock.validation import floor_history @@ -21,30 +33,27 @@ # ---------------- content gates (promise 1) ---------------- -def test_secret_inside_own_policy_folder_is_refused(tmp_path, capsys): - repo = base_repo(tmp_path) - write(repo, ".agents/policies/floor-token/notes.md", "leak FLOOR-TOKEN-4242\n") - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "FAIL floor-token [base]" in out - assert ( - "floor/pack-changed: floor-token: no lock change, so every gate judged it; " - "source: .agents/policies/floor-token/notes.md" in out - ) +def test_secret_inside_own_policy_folder_is_a_control_plane_change(tmp_path, capsys): + """The policy's own folder is control plane: listed, never judged by its own gate, never exempted silently.""" + path = ".agents/policies/floor-token/notes.md" + def change(repo): + write(repo, path, "leak FLOOR-TOKEN-4242\n") + commit(repo, "pr") -def test_own_folder_is_exempt_only_with_a_lock_change_and_is_listed(tmp_path, capsys): - repo = base_repo(tmp_path) + control_plane_change(tmp_path, capsys, change, path) + + +def test_listed_mode_states_that_control_plane_content_was_not_judged(tmp_path, capsys): + repo = base_repo(tmp_path, "listed") write(repo, ".agents/policies/floor-token/evals/case.txt", "FLOOR-TOKEN-4242\n") write(repo, "chock.lock", {**LOCK, "packs": [LOCK["packs"][0], {"id": "floor-token", "version": "2"}]}) commit(repo, "upgrade floor-token with a new eval case") code, out = run_floor(repo, capsys) assert code == 0, out - assert ( - "floor/pack-changed: floor-token: lock entry changed; its own gates do not judge its source: " - ".agents/policies/floor-token/evals/case.txt" in out - ) + assert "LISTED floor/control-plane-changed (listed): 2 path(s)" in out + assert "the content of these paths was not judged" in out + assert "FAIL" not in out def test_secret_in_file_with_nul_byte_is_refused(tmp_path, capsys): @@ -78,14 +87,16 @@ def test_control_plain_secret_refused(tmp_path, capsys): assert code == 1, out -def test_head_gitattributes_minus_diff_does_not_hide_test_weakening(tmp_path, capsys): - repo = base_repo(tmp_path) +@pytest.mark.parametrize("mode", ["refuse", "listed"]) +def test_head_gitattributes_minus_diff_does_not_hide_test_weakening(tmp_path, capsys, mode): + repo = base_repo(tmp_path, mode) write(repo, ".gitattributes", "tests/** -diff\n") write(repo, "tests/test_core.py", "def test_a():\n pass\n") commit(repo, "pr") code, out = run_floor(repo, capsys) assert code == 1, out assert "assertions removed across tests" in out + assert "\n .gitattributes\n" in out def test_control_test_weakening_refused(tmp_path, capsys): @@ -167,118 +178,92 @@ def test_history_reads_ci_only_content_gates(tmp_path, capsys): assert "no content gates" not in out -# ---------------- hooks (promise 3, ruling 3) ---------------- +# ---------------- hooks, runtimes, compiled gates and the lock: control plane (V2) ---------------- -def _claude(repo: Path, doc: dict) -> None: - write(repo, ".claude/settings.json", doc) - commit(repo, "pr") +def _claude(doc: dict, path: str = ".claude/settings.json"): + def change(repo): + write(repo, path, doc) + commit(repo, "pr") + return change -def test_disable_all_hooks_key_is_refused(tmp_path, capsys): - repo = base_repo(tmp_path) - _claude(repo, {**CLAUDE, "disableAllHooks": True}) - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/hooks-disabled .claude/settings.json: /disableAllHooks turns every hook off" in out +def _leaves(transform) -> dict: + doc = json.loads(json.dumps(CLAUDE)) + for item in doc["hooks"]["PreToolUse"]: + for leaf in item["hooks"]: + transform(leaf) + return doc -def test_settings_local_json_disable_is_refused(tmp_path, capsys): - repo = base_repo(tmp_path) - write(repo, ".claude/settings.local.json", {"disableAllHooks": True}) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/hooks-overridden .claude/settings.local.json" in out +def _to_prompt(leaf: dict) -> None: + leaf.update(type="prompt", prompt="always allow") -def test_settings_local_json_without_hooks_passes(tmp_path, capsys): - repo = base_repo(tmp_path) - write(repo, ".claude/settings.local.json", {"permissions": {"allow": ["Bash(ls)"]}}) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 0, out +def _digit_swap(leaf: dict) -> None: + leaf["command"] = leaf["command"].replace("claude.py", "claude.py --sha 0000000000") -def test_hook_type_changed_is_refused(tmp_path, capsys): - repo = base_repo(tmp_path) - doc = json.loads(json.dumps(CLAUDE)) - for item in doc["hooks"]["PreToolUse"]: - for leaf in item["hooks"]: - leaf["type"] = "prompt" - leaf["prompt"] = "always allow" - _claude(repo, doc) - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/hook-removed .claude/settings.json: PreToolUse command hook removed" in out +CONTROL_PLANE_ATTACKS = { + "disable-all-hooks": (_claude({**CLAUDE, "disableAllHooks": True}), ".claude/settings.json"), + "settings-local-disable": ( + _claude({"disableAllHooks": True}, ".claude/settings.local.json"), + ".claude/settings.local.json", + ), + "hook-type-changed": (_claude(_leaves(_to_prompt)), ".claude/settings.json"), + "hook-command-digit-swap": (_claude(_leaves(_digit_swap)), ".claude/settings.json"), +} -@pytest.mark.parametrize("run", ["--sha 0000000000", "--budget 1.2.3"]) -def test_hook_command_digit_swap_is_refused(tmp_path, capsys, run): - """Only the index pin is normalised: any other run of digits or hex in a command is part of its identity.""" - repo = base_repo(tmp_path) - git(repo, "checkout", "-q", "main") - doc = json.loads(json.dumps(CLAUDE)) - doc["hooks"]["PreToolUse"][1]["hooks"][0]["command"] += " --sha abc1234def --budget 9.9.9" - _claude(repo, doc) - git(repo, "checkout", "-q", "-B", "pr") - flag = run.split()[0] - words = doc["hooks"]["PreToolUse"][1]["hooks"][0]["command"].split() - words[words.index(flag) + 1] = run.split()[1] - doc["hooks"]["PreToolUse"][1]["hooks"][0]["command"] = " ".join(words) - _claude(repo, doc) - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/hook-removed .claude/settings.json" in out +@pytest.mark.parametrize("name", sorted(CONTROL_PLANE_ATTACKS)) +def test_hook_attacks_are_control_plane_changes(tmp_path, capsys, name): + change, path = CONTROL_PLANE_ATTACKS[name] + control_plane_change(tmp_path, capsys, change, path) -def test_vendors_without_verified_switches_are_named_not_covered(tmp_path, capsys): - repo = base_repo(tmp_path) - write(repo, "src/other.py", "X = 1\n") - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 0, out - assert "note: floor/hook-switches not covered: cursor" in out +def test_settings_local_json_without_hooks_is_still_listed(tmp_path, capsys): + """No file is read for meaning: a permissions-only override file is a guardrail change like any other.""" + path = ".claude/settings.local.json" + control_plane_change(tmp_path, capsys, _claude({"permissions": {"allow": ["Bash(ls)"]}}, path), path) -# ---------------- runtimes, compiled gates and the lock (rulings 4 and 7) ---------------- +def _writes(rel: str, content: object): + def change(repo): + write(repo, rel, content) + commit(repo, "pr") -def test_runtime_emptied_is_refused(tmp_path, capsys): - repo = base_repo(tmp_path) - write(repo, ".chock/bin/gate.py", "import sys; sys.exit(0)\n") - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/runtime-changed .chock/bin/gate.py" in out + return change -def test_git_hook_gate_params_neutered_is_refused(tmp_path, capsys): - repo = base_repo(tmp_path) - neutered = {**FLOOR_TOKEN, "params": {"content_pattern": "(?!)"}, "paths": ["nothing/*"]} - write(repo, ".chock/compiled/floor-token/git-hook/gate.json", neutered) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/gate-changed .chock/compiled/floor-token/git-hook/gate.json" in out +GATE = ".chock/compiled/floor-token/git-hook/gate.json" +SWAPPED = {"kind": "forbidden_ref", "on": ["commit", "tool_use"], "action": "block", "params": {"refs": []}} +NEUTERED = {**FLOOR_TOKEN, "params": {"content_pattern": "(?!)"}, "paths": ["nothing/*"]} +SOURCE_SWAP = { + **LOCK, + "packs": [{"id": "test-integrity", "source": "https://evil.example/pack"}, {"id": "floor-token"}], +} +STATE_ATTACKS = { + "runtime-emptied": (".chock/bin/gate.py", "import sys; sys.exit(0)\n"), + "gate-params-neutered": (GATE, NEUTERED), + "gate-kind-swapped": (GATE, SWAPPED), + "lock-source-swap": ("chock.lock", SOURCE_SWAP), +} -def test_gate_kind_swapped_is_refused(tmp_path, capsys): - repo = base_repo(tmp_path) - swapped = {"kind": "forbidden_ref", "on": ["commit", "tool_use"], "action": "block", "params": {"refs": []}} - write(repo, ".chock/compiled/floor-token/git-hook/gate.json", swapped) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/gate-changed .chock/compiled/floor-token/git-hook/gate.json" in out +@pytest.mark.parametrize("name", sorted(STATE_ATTACKS)) +def test_runtime_gate_and_lock_attacks_are_control_plane_changes(tmp_path, capsys, name): + rel, content = STATE_ATTACKS[name] + control_plane_change(tmp_path, capsys, _writes(rel, content), rel) -def test_lock_pack_source_swap_is_listed_by_name(tmp_path, capsys): - """A lock entry change is the signal a pack changed; the floor names it rather than refusing (ruling 7).""" - repo = base_repo(tmp_path) - lock = {**LOCK, "packs": [{"id": "test-integrity", "source": "https://evil.example/pack"}, {"id": "floor-token"}]} - write(repo, "chock.lock", lock) +def test_a_neutered_head_gate_does_not_stop_the_base_gate(tmp_path, capsys): + """Under `listed` the change is let through for a person, but the base's gate still judges the content.""" + repo = base_repo(tmp_path, "listed") + write(repo, GATE, NEUTERED) + write(repo, ".chock/compiled/floor-token/ci-gate/gate.json", NEUTERED) + write(repo, "src/config.txt", SECRET) commit(repo, "pr") code, out = run_floor(repo, capsys) - assert code == 0, out - assert "floor/pack-changed: test-integrity: lock entry changed" in out + assert code == 1, out + assert "FAIL floor-token [base] (block)" in out diff --git a/tests/test_floor_attacks2.py b/tests/test_floor_attacks2.py index bbf9720c..a087d300 100644 --- a/tests/test_floor_attacks2.py +++ b/tests/test_floor_attacks2.py @@ -1,6 +1,7 @@ -"""Regression tests for the adversarial review of `chock check --floor` (workflow, waivers, scripts, arguments). +"""Regression tests for the first adversarial review of `chock check --floor` (workflow, waivers, scripts, refs). -Each test is one attack, or one correct pull request the floor once refused, asserting the owed exit code. +Each test is one attack, or one correct pull request, asserting the exit code W2-A1v2 owes. Every workflow +edit is a control-plane change: refused under `refuse`, listed with exit 0 under `listed`, never read. """ from __future__ import annotations @@ -21,6 +22,7 @@ WORKFLOW, base_repo, commit, + control_plane_change, gate, git, run_floor, @@ -47,43 +49,46 @@ } -# ---------------- the floor's own workflow (ruling 4: equal or refuse) ---------------- +# ---------------- workflows are control plane (V2: no structural reading) ---------------- + +FLOOR_WORKFLOW = ".github/workflows/chock.yml" + + +def _workflow(text: str, rel: str = FLOOR_WORKFLOW): + def change(repo): + write(repo, rel, text) + commit(repo, "pr") + + return change @pytest.mark.parametrize("name", sorted(WORKFLOW_EDITS)) -def test_any_edit_to_the_floor_workflow_is_refused(tmp_path, capsys, name): +def test_any_edit_to_the_floor_workflow_is_a_control_plane_change(tmp_path, capsys, name): old, new = WORKFLOW_EDITS[name] - repo = base_repo(tmp_path) - write(repo, ".github/workflows/chock.yml", WORKFLOW.replace(old, new, 1)) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/floor-workflow-changed .github/workflows/chock.yml" in out + control_plane_change(tmp_path, capsys, _workflow(WORKFLOW.replace(old, new, 1)), FLOOR_WORKFLOW) -def test_an_action_bump_in_the_floor_workflow_is_refused(tmp_path, capsys): - """Ruling 4 (owner decision F2 open): even a Dependabot bump goes through the privileged two-step path.""" - repo = base_repo(tmp_path) - git(repo, "checkout", "-q", "main") - write(repo, ".github/workflows/chock.yml", WORKFLOW.replace(STEP, " - uses: actions/setup-python@v5\n" + STEP)) - commit(repo, "base uses setup-python v5") - git(repo, "checkout", "-q", "-B", "pr") - write(repo, ".github/workflows/chock.yml", WORKFLOW.replace(STEP, " - uses: actions/setup-python@v6\n" + STEP)) - commit(repo, "bump setup-python to v6") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "tamper/floor-workflow-changed" in out +def test_an_action_bump_in_the_floor_workflow_is_a_control_plane_change(tmp_path, capsys): + """By design under `refuse`: a Dependabot bump needs a privileged merge, or the base sets `listed`.""" + def change(repo): + git(repo, "checkout", "-q", "main") + write(repo, FLOOR_WORKFLOW, WORKFLOW.replace(STEP, " - uses: actions/setup-python@v5\n" + STEP)) + commit(repo, "base uses setup-python v5") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, FLOOR_WORKFLOW, WORKFLOW.replace(STEP, " - uses: actions/setup-python@v6\n" + STEP)) + commit(repo, "bump setup-python to v6") -def test_other_workflows_are_out_of_scope(tmp_path, capsys): - repo = base_repo(tmp_path) - write(repo, ".github/workflows/lint.yml", "on: push\njobs: {}\n") - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 0, out + control_plane_change(tmp_path, capsys, change, FLOOR_WORKFLOW) + + +def test_an_added_workflow_is_a_control_plane_change(tmp_path, capsys): + """A workflow the head adds can report a check under the floor's name (F3: in the default list).""" + rel = ".github/workflows/lint.yml" + control_plane_change(tmp_path, capsys, _workflow("on: push\njobs: {}\n", rel), rel) -# ---------------- correct pull requests (never refused) ---------------- +# ---------------- first adoption, stale branches, correct pull requests ---------------- def test_base_without_chock_says_nothing_was_judged(tmp_path, capsys): @@ -94,15 +99,14 @@ def test_base_without_chock_says_nothing_was_judged(tmp_path, capsys): commit(repo, "base") git(repo, "checkout", "-q", "-b", "pr") write(repo, "src/config.txt", "key = FLOOR-TOKEN-4242\n") + write(repo, ".claude/settings.json", CLAUDE) commit(repo, "pr") code, out = run_floor(repo, capsys) assert code == 0, out - assert "the base has no compiled gates; no gate judged this pull request" in out - assert "history: the base has no content gates on either surface" in out + assert "floor: base has no chock gates; nothing was judged" in out -def test_stale_branch_after_base_adds_a_hook_passes(tmp_path, capsys): - repo = base_repo(tmp_path) +def _stale_branch(repo: Path) -> None: write(repo, "src/app.py", "VALUE = 2\n") commit(repo, "pr: unrelated edit") git(repo, "checkout", "-q", "main") @@ -112,35 +116,64 @@ def test_stale_branch_after_base_adds_a_hook_passes(tmp_path, capsys): gate(repo, "late-policy", {**FLOOR_TOKEN, "params": {"content_pattern": "LATE-[0-9]+"}}) commit(repo, "main adds a hook and a gate after the pr forked") git(repo, "checkout", "-q", "pr") + + +def test_a_stale_branch_is_undecided_and_its_merge_result_passes(tmp_path, capsys): + """V1: the floor judges what will land; a branch behind its base is not that, its merge result is.""" + repo = base_repo(tmp_path) + _stale_branch(repo) + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "is not an ancestor of head" in out + assert "refs/pull//merge" in out + git(repo, "checkout", "-q", "--detach", "main") + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "merge", "-q", "--no-ff", "--no-edit", "pr") code, out = run_floor(repo, capsys) assert code == 0, out - assert "tamper/" not in out + assert "control-plane" not in out def test_ok_add_policy_hook_lock_unrelated(tmp_path, capsys): - """The review's correct pull request, less its edit to the floor workflow (refused by ruling 4, above).""" - repo = base_repo(tmp_path) - gate(repo, "new-policy", {**FLOOR_TOKEN, "params": {"content_pattern": "NEVER-[0-9]+"}}) - doc = json.loads(json.dumps(CLAUDE)) - doc["hooks"]["PreToolUse"][1]["matcher"] = "Edit|Write|MultiEdit" - command = doc["hooks"]["PreToolUse"][0]["hooks"][0]["command"] - doc["hooks"]["PreToolUse"][0]["hooks"][0]["command"] = command.replace(INDEX_PIN, "9" * 64) - write(repo, ".claude/settings.json", doc) - write( - repo, + """The review's correct pull request: a policy upgrade and a regeneration. By design: refused under + `refuse` (a privileged merge), listed under `listed`.""" + lock = { + "lockfile_version": "2", + "engine": ">=1.3,<2", + "packs": [{"id": "test-integrity", "version": "1.4.0"}, {"id": "floor-token"}], + } + + def change(repo): + gate(repo, "new-policy", {**FLOOR_TOKEN, "params": {"content_pattern": "NEVER-[0-9]+"}}) + doc = json.loads(json.dumps(CLAUDE)) + doc["hooks"]["PreToolUse"][1]["matcher"] = "Edit|Write|MultiEdit" + command = doc["hooks"]["PreToolUse"][0]["hooks"][0]["command"] + doc["hooks"]["PreToolUse"][0]["hooks"][0]["command"] = command.replace(INDEX_PIN, "9" * 64) + write(repo, ".claude/settings.json", doc) + write(repo, "chock.lock", lock) + write(repo, "src/app.py", "VALUE = 3\n") + write(repo, "tests/test_core.py", TEST_FILE + "\ndef test_b():\n assert True\n") + commit(repo, "pr") + + control_plane_change( + tmp_path, + capsys, + change, + ".chock/compiled/new-policy/ci-gate/gate.json", + ".chock/compiled/new-policy/git-hook/gate.json", + ".claude/settings.json", "chock.lock", - { - "lockfile_version": "2", - "engine": ">=1.3,<2", - "packs": [{"id": "test-integrity", "version": "1.4.0"}, {"id": "floor-token"}], - }, ) + + +def test_ok_ordinary_pull_request_passes(tmp_path, capsys): + repo = base_repo(tmp_path) write(repo, "src/app.py", "VALUE = 3\n") write(repo, "tests/test_core.py", TEST_FILE + "\ndef test_b():\n assert True\n") + write(repo, "docs/guide.md", "# Guide\n") commit(repo, "pr") code, out = run_floor(repo, capsys) assert code == 0, out - assert "floor/pack-changed: test-integrity: lock entry changed" in out + assert "control-plane" not in out # ---------------- waivers (ruling 5) ---------------- @@ -253,14 +286,15 @@ def test_a_gate_naming_a_file_outside_the_repository_is_undecided(tmp_path, caps # ---------------- refs ---------------- -def test_head_not_descendant_of_base(tmp_path, capsys): +def test_head_not_descendant_of_base_is_undecided(tmp_path, capsys): repo = base_repo(tmp_path) git(repo, "checkout", "-q", "main") write(repo, "src/app.py", "VALUE = 9\n") commit(repo, "main moves on") git(repo, "checkout", "-q", "pr") code, out = run_floor(repo, capsys, "--head", "pr") - assert code == 0, out + assert code == 2, out + assert "is not an ancestor of head" in out def _cli(repo: Path, *args: str) -> int: diff --git a/tests/test_floor_attacks3.py b/tests/test_floor_attacks3.py new file mode 100644 index 00000000..f6d45878 --- /dev/null +++ b/tests/test_floor_attacks3.py @@ -0,0 +1,457 @@ +"""Regression tests for the second adversarial review of `chock check --floor` (W2-A1r, every proof ported). + +Each asserts the exit W2-A1v2 owes: a control-plane attack exits 1 under `refuse` and 0, with the path +listed, under `listed`; content, waiver and history attacks exit 1 or 2; correct pull requests exit 0. +""" + +from __future__ import annotations + +import json +import os +import subprocess +import time +from pathlib import Path + +import pytest +from floor_support import ( + CLAUDE, + FLOOR_TOKEN, + LOCK, + TEST_FILE, + TEST_PRAGMA, + WORKFLOW, + base_repo, + commit, + control_plane_change, + gate, + git, + run_floor, + write, +) + +SECRET = "FLOOR-TOKEN-424242" +SETTINGS = ".claude/settings.json" +FLOOR_WORKFLOW = ".github/workflows/chock.yml" + + +def _merge(repo: Path, branch: str = "pr") -> None: + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "merge", "-q", "--no-ff", "--no-edit", branch) + + +def _land(repo: Path, branch: str) -> None: + """Merge the pull request into main and start the next one from it.""" + git(repo, "checkout", "-q", "main") + git(repo, "merge", "-q", "--ff-only", "pr") + git(repo, "checkout", "-q", "-b", branch) + + +# ---------- X1, X2: the floor workflow replaced by a render, or retargeted by config, then edited ---------- + + +def test_floor_workflow_replaced_by_engine_render(tmp_path, capsys): + from chock.scaffold import install_ci + + def change(repo): + write(repo, FLOOR_WORKFLOW, install_ci.render()) + commit(repo, "regenerate ci") + + control_plane_change(tmp_path, capsys, change, FLOOR_WORKFLOW) + + +def test_floor_workflow_retargeted_by_config(tmp_path, capsys): + decoy = ".github/workflows/decoy.yml" + + def change(repo): + write(repo, ".chock/config.yaml", "floor_workflow: .github/workflows/decoy.yml\n") + write(repo, decoy, "name: decoy\non: [workflow_dispatch]\njobs: {}\n") + commit(repo, "pr1: config") + + control_plane_change(tmp_path, capsys, change, ".chock/config.yaml", decoy) + + +def test_the_head_config_never_sets_the_mode_or_the_patterns(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".chock/config.yaml", "floor:\n control_plane: listed\n control_plane_paths:\n remove: ['**']\n") + write(repo, FLOOR_WORKFLOW, WORKFLOW.replace(" - name: Floor\n", " - name: Floor\n if: false\n")) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "FAIL floor/control-plane-changed (refuse): 2 path(s)" in out + + +# ---------- X7: rollout lowered for the next pull request ---------- + + +@pytest.mark.parametrize("next_change", ["leak", "weaken"]) +def test_rollout_lowered_to_observe(tmp_path, capsys, next_change): + def change(repo): + write(repo, ".chock/config.yaml", "rollout: observe\n") + commit(repo, "pr1: observe") + + control_plane_change(tmp_path, capsys, change, ".chock/config.yaml") + repo = base_repo(tmp_path / "second") + change(repo) + _land(repo, "pr2") + if next_change == "leak": + write(repo, "src/leak.py", f'KEY = "{SECRET}"\n') + else: + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + commit(repo, "pr2") + code, out = run_floor(repo, capsys) + assert code == 0, out # a person merged `observe`: the base's level now binds, refusals are notes + assert "(warn)" in out + + +# ---------- X5: a lock field edit with a weakened gate and a secret in the pack's folder ---------- + + +def _bogus_lock(repo: Path, *, weaken: bool) -> None: + lock = json.loads(json.dumps(LOCK)) + lock["packs"][1]["note"] = "x" + write(repo, "chock.lock", lock) + if weaken: + gate(repo, "floor-token", dict(FLOOR_TOKEN, params={"content_pattern": "(?!)"})) + write(repo, ".agents/policies/floor-token/fixtures/creds.txt", f"{SECRET}\n") + commit(repo, "pr") + + +@pytest.mark.parametrize("weaken", [True, False]) +def test_bogus_lock_field(tmp_path, capsys, weaken): + paths = ["chock.lock", ".agents/policies/floor-token/fixtures/creds.txt"] + if weaken: + paths += [f".chock/compiled/floor-token/{s}/gate.json" for s in ("ci-gate", "git-hook")] + control_plane_change(tmp_path, capsys, lambda repo: _bogus_lock(repo, weaken=weaken), *paths) + + +# ---------- X4, M-c, M-d: Claude Code hooks neutralised without removal, added, or overridden ---------- + + +def _claude_with(extra: dict | None = None, leaf: dict | None = None, matcher: str | None = None) -> dict: + doc = json.loads(json.dumps(CLAUDE)) + doc.update(extra or {}) + first = doc["hooks"]["PreToolUse"][0] + first["hooks"][0].update(leaf or {}) + if matcher is not None: + first["matcher"] = matcher + return doc + + +ENV_REDIRECT = {"env": {"GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "chock.python", "GIT_CONFIG_VALUE_0": "tools/py"}} +SESSION_START = {"SessionStart": [{"hooks": [{"type": "command", "command": "sh tools/warm-cache.sh"}]}]} +HOOK_ATTACKS = { + "env-redirects-interpreter": (_claude_with(extra=ENV_REDIRECT), {"tools/py": "#!/bin/sh\nexit 0\n"}), + "env-path-shadow": (_claude_with(extra={"env": {"PATH": "tools:/usr/bin:/bin"}}), {"tools/python3": "exit 0\n"}), + "matcher-never-matches": (_claude_with(matcher="(?=x)(|Bash|)"), {}), + "async": (_claude_with(leaf={"async": True}), {}), + "timeout-tiny": (_claude_with(leaf={"timeout": 0.001}), {}), + "if-condition": (_claude_with(leaf={"if": "Bash(no-such-command-ever)"}), {}), + "exec-form-args": (_claude_with(leaf={"args": []}), {}), + "shell-powershell": (_claude_with(leaf={"shell": "powershell"}), {}), + "hook-removed": ({"hooks": {"PreToolUse": CLAUDE["hooks"]["PreToolUse"][1:]}}, {}), + "session-start-rewrites-launcher": ( + {"hooks": {**CLAUDE["hooks"], **SESSION_START}}, + {"tools/warm-cache.sh": "#!/bin/sh\nprintf 'exit 0\\n' > .chock/bin/launch.sh\n"}, + ), +} + + +@pytest.mark.parametrize("name", sorted(HOOK_ATTACKS)) +def test_hook_neutralised_without_removal(tmp_path, capsys, name): + """Also the launcher demo (test_launcher_runs_head_stub_via_env): its `env` change is this listed path.""" + doc, extra = HOOK_ATTACKS[name] + + def change(repo): + write(repo, SETTINGS, doc) + for rel, text in extra.items(): + write(repo, rel, text) + commit(repo, "pr") + + control_plane_change(tmp_path, capsys, change, SETTINGS) + + +def test_hook_onfailure_block_removed(tmp_path, capsys): + def change(repo): + git(repo, "checkout", "-q", "main") + write(repo, SETTINGS, _claude_with(leaf={"onFailure": "block"})) + commit(repo, "base: hooks") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, SETTINGS, _claude_with(leaf={"onFailure": "continue"})) + commit(repo, "pr") + + control_plane_change(tmp_path, capsys, change, SETTINGS) + + +@pytest.mark.parametrize( + "name", [".claude/settings.local.json", ".claude/Settings.local.json", ".CLAUDE/settings.json"] +) +def test_override_file_any_case(tmp_path, capsys, name): + def change(repo): + write(repo, name, {"disableAllHooks": True}) + commit(repo, "pr") + + control_plane_change(tmp_path, capsys, change, name) + + +ACTION = """name: floor +runs: + using: composite + steps: + - shell: bash + run: chock check --floor --base "$BASE_SHA" --head "$HEAD_SHA" +""" + + +def test_floor_workflow_local_action_neutered(tmp_path, capsys): + rel = ".github/actions/floor/action.yml" + + def change(repo): + git(repo, "checkout", "-q", "main") + write(repo, rel, ACTION) + commit(repo, "base: floor via a local action") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, rel, ACTION.replace("chock check --floor", "true ||")) + commit(repo, "pr") + + control_plane_change(tmp_path, capsys, change, rel) + + +# ---------- X3: a criss-cross merge-base; V1 judges the merge result instead ---------- + + +def _criss_cross(repo: Path, *, branch_first: bool) -> None: + """A Stop hook is added on main; criss-cross merges; the side branch then drops it.""" + git(repo, "checkout", "-q", "main") + git(repo, "branch", "-f", "side") + doc = json.loads(json.dumps(CLAUDE)) + doc["hooks"]["Stop"] = [{"hooks": [{"type": "command", "command": "sh .chock/bin/launch.sh claude.py --stop"}]}] + + def on_main(): + git(repo, "checkout", "-q", "main") + write(repo, SETTINGS, doc) + return commit(repo, "main: add the Stop guardrail") + + def on_side(): + git(repo, "checkout", "-q", "side") + write(repo, "src/side.py", "S = 1\n") + return commit(repo, "side: unrelated") + + m1, p1 = (on_side(), on_main())[::-1] if branch_first else (on_main(), on_side()) + git(repo, "checkout", "-q", "main") + _merge(repo, p1) + git(repo, "checkout", "-q", "side") + _merge(repo, m1) + write(repo, SETTINGS, CLAUDE) + commit(repo, "side: drop Stop") + + +@pytest.mark.parametrize("branch_first", [False, True]) +def test_criss_cross_drop_guardrail(tmp_path, capsys, branch_first): + repo = base_repo(tmp_path) + _criss_cross(repo, branch_first=branch_first) + code, out = run_floor(repo, capsys, "--head", "side") + assert code == 2, out # main is not an ancestor of side: no merge-base is ever picked + git(repo, "checkout", "-q", "--detach", "main") + _merge(repo, "side") + landed = git(repo, "show", f"HEAD:{SETTINGS}") != git(repo, "show", f"main:{SETTINGS}") + code, out = run_floor(repo, capsys) + assert code == (1 if landed else 0), out + assert (f"\n {SETTINGS}\n" in out) == landed, out + + +# ---------- shallow clone and unrelated history ---------- + + +def test_shallow_clone_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr1") + write(repo, "src/app.py", "VALUE = 3\n") + commit(repo, "pr2") + clone = tmp_path / "clone" + subprocess.run( + ["git", "clone", "-q", "--depth", "1", "--no-single-branch", f"file://{repo}", str(clone)], check=True + ) + code, out = run_floor(clone, capsys, "--base", "origin/main", "--head", "origin/pr") + assert code == 2, out + + +def test_unrelated_history_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "--orphan", "alien") + git(repo, "rm", "-rq", "--cached", ".") + write(repo, "x.txt", f"{SECRET}\n") + git(repo, "add", "x.txt") + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "commit", "-q", "-m", "alien") + code, out = run_floor(repo, capsys, "--head", "alien") + assert code == 2, out + + +# ---------- M-f, X6: encodings and line counting in the history pass ---------- + + +def test_utf16_secret(tmp_path, capsys): + repo = base_repo(tmp_path) + (repo / "src/notes.txt").write_bytes(f"key = {SECRET}\n".encode("utf-16")) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "floor-token [history] (block)" in out + + +def test_utf16_secret_shifted_by_one_byte(tmp_path, capsys): + repo = base_repo(tmp_path) + (repo / "src/notes.bin").write_bytes(b"\x01" + f"key = {SECRET}\n".encode("utf-16-le")) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_secret_as_symlink_target_added_then_removed(tmp_path, capsys): + repo = base_repo(tmp_path) + os.symlink(SECRET, repo / "link") + commit(repo, "add link") + (repo / "link").unlink() + commit(repo, "remove link") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +@pytest.mark.parametrize("prefix", [b"\x85\x85", b"\xff\xff", b"\x1c\x1d\x1e"]) +def test_history_line_number_mismatch_drops_no_secret(tmp_path, capsys, prefix): + """U+0085 and the separators splitlines() knows are not line breaks to the floor: one reader, one count.""" + repo = base_repo(tmp_path) + (repo / "src/app.py").write_bytes(prefix + b"FLOOR-TOKEN-777\nVALUE = 1\nVALUE = 1\n") + commit(repo, "pr: add (hidden)") + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "pr: restore") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "src/app.py:1 (content-pattern)" in out + + +# ---------- M-a, M-b: waivers on removed lines, renames hiding deletions ---------- + + +def _base_then_pr(repo: Path, base_text: str, head_text: str) -> None: + git(repo, "checkout", "-q", "main") + write(repo, "tests/test_core.py", base_text) + commit(repo, "base") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "tests/test_core.py", head_text) + commit(repo, "pr") + + +def test_removed_assertion_carrying_base_pragma_is_not_waived(tmp_path, capsys): + repo = base_repo(tmp_path) + pragma = TEST_FILE.replace("assert 2 * 2 == 4", f"assert 2 * 2 == 4 # {TEST_PRAGMA}") + _base_then_pr(repo, pragma, "def test_a():\n assert 1 + 1 == 2\n") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_removed_pragma_assertions_two_lines(tmp_path, capsys): + repo = base_repo(tmp_path) + body = TEST_FILE.replace("== 2\n", f"== 2 # {TEST_PRAGMA}\n").replace("== 4\n", f"== 4 # {TEST_PRAGMA}\n") + _base_then_pr(repo, body, "def test_a():\n pass\n") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +@pytest.mark.parametrize("how", ["rename", "delete", "weaken"]) +def test_test_file_moved_deleted_or_weakened_is_refused(tmp_path, capsys, how): + repo = base_repo(tmp_path) + if how == "rename": + git(repo, "mv", "tests/test_core.py", "attic_core.py") + elif how == "delete": + git(repo, "rm", "-q", "tests/test_core.py") + else: + write(repo, "tests/test_core.py", "def test_a():\n assert 1 + 1 == 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +# ---------- first adoption ---------- + + +def test_first_adoption_output(tmp_path, capsys): + repo = tmp_path / "repo" + repo.mkdir() + git(repo, "init", "-q", "-b", "main") + write(repo, "src/app.py", "V = 1\n") + commit(repo, "base") + git(repo, "checkout", "-q", "-b", "pr") + write(repo, "src/leak.py", "K = 'FLOOR-TOKEN-1'\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "base has no chock gates; nothing was judged" in out + + +# ---------- correct pull requests and wall time ---------- + + +@pytest.mark.parametrize("megabytes", [8, 40]) +def test_large_correct_blob(tmp_path, capsys, megabytes): + repo = base_repo(tmp_path) + line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0123456789\n" + (repo / "data.csv").write_text(line * (megabytes * 1024 * 1024 // len(line))) + commit(repo, "pr: data") + started = time.monotonic() + code, out = run_floor(repo, capsys) + print(f"{megabytes} MB: exit {code} in {time.monotonic() - started:.1f}s") + assert code == 0, out + + +def test_ok_merge_base_into_branch_and_revert(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/feature.py", "F = 1\n") + commit(repo, "pr: feature") + git(repo, "checkout", "-q", "main") + write(repo, "src/other.py", "O = 1\n") + commit(repo, "main moves on") + git(repo, "checkout", "-q", "pr") + _merge(repo, "main") + write(repo, "src/feature.py", "F = 2\n") + commit(repo, "pr: change") + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "revert", "--no-edit", "HEAD") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_policy_added_with_lock_entry_is_a_control_plane_change(tmp_path, capsys): + """By design under `refuse` (a privileged merge); under `listed` its own gate never judges its own source.""" + + def change(repo): + lock = json.loads(json.dumps(LOCK)) + lock["packs"].append({"id": "no-todo"}) + write(repo, "chock.lock", lock) + gate(repo, "no-todo", dict(FLOOR_TOKEN, message="no TODO", params={"content_pattern": "TODO-NEVER"})) + write(repo, ".agents/policies/no-todo/policy.md", "# no-todo\n") + commit(repo, "pr: add policy") + + control_plane_change(tmp_path, capsys, change, "chock.lock", ".agents/policies/no-todo/policy.md") + + +def test_docs_quoting_a_policy_example_in_its_own_folder(tmp_path, capsys): + rel = ".agents/policies/floor-token/README.md" + + def change(repo): + write(repo, rel, "Example that is refused: FLOOR-TOKEN-1\n") + commit(repo, "pr: docs") + + control_plane_change(tmp_path, capsys, change, rel) + + +def test_ok_rename_file_with_base_waiver(tmp_path, capsys): + """A rename is a delete and an add (V1); the fixture's probe gate refuses every delete, so it goes first.""" + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + git(repo, "rm", "-q", "-r", ".chock/compiled/probe") + write(repo, "src/legacy.py", "X = 1\n") + commit(repo, "base") + git(repo, "checkout", "-q", "-B", "pr") + git(repo, "mv", "src/legacy.py", "src/legacy2.py") + commit(repo, "pr: rename") + code, out = run_floor(repo, capsys) + assert code == 0, out diff --git a/tests/test_floor_check.py b/tests/test_floor_check.py index 793cee47..23adae55 100644 --- a/tests/test_floor_check.py +++ b/tests/test_floor_check.py @@ -42,7 +42,8 @@ def test_a_waiver_the_pull_request_adds_is_not_honoured(tmp_path, capsys) -> Non assert "assertions removed across tests" in out -def test_a_waiver_already_at_the_base_is_honoured(tmp_path, capsys) -> None: +def test_a_base_waiver_on_a_line_the_pull_request_removes_is_not_honoured(tmp_path, capsys) -> None: + """V4: a waiver counts only on a line present and unchanged in both base and head.""" repo = base_repo(tmp_path) git(repo, "checkout", "-q", "main") write(repo, "tests/test_core.py", TEST_FILE.replace("== 4\n", f"== 4 # {TEST_PRAGMA}\n")) @@ -51,7 +52,8 @@ def test_a_waiver_already_at_the_base_is_honoured(tmp_path, capsys) -> None: write(repo, "tests/test_core.py", "def test_a():\n assert 1 + 1 == 2\n") commit(repo, "pr") code, out = run_floor(repo, capsys) - assert code == 0, out + assert code == 1, out + assert "assertions removed across tests" in out def test_ask_counts_as_a_refusal(tmp_path, capsys) -> None: @@ -75,7 +77,7 @@ def test_a_secret_added_then_removed_is_found_by_the_history_pass(tmp_path, caps commit(repo, "remove") code, out = run_floor(repo, capsys) assert code == 1 - assert f"floor-token [history]: {first[:12]} src/app.py:1 (content-pattern)" in out + assert f"FAIL floor-token [history] (block): {first[:12]} src/app.py:1 (content-pattern)" in out def test_a_line_already_at_the_base_is_not_the_pull_requests(tmp_path, capsys) -> None: @@ -113,8 +115,9 @@ def test_no_head_file_is_executed(tmp_path, capsys) -> None: commit(repo, "pr") code, out = run_floor(repo, capsys) assert not (repo / "MARKER").exists() - assert "new-probe [head]: not judged at floor" in out - assert code == 0, out + assert "new-probe" not in out.replace(".chock/compiled/new-probe/", "") + assert code == 1, out + assert f"\n {PROBE['params']['script']}\n" in out def test_the_head_is_read_as_objects_from_a_base_checkout(tmp_path, capsys) -> None: @@ -124,7 +127,8 @@ def test_the_head_is_read_as_objects_from_a_base_checkout(tmp_path, capsys) -> N git(repo, "checkout", "-q", "main") code, out = run_floor(repo, capsys, "--head", "pr") assert code == 1 - assert "tamper/hook-removed .cursor/hooks.json" in out + assert "FAIL floor/control-plane-changed (refuse): 1 path(s)" in out + assert "\n .cursor/hooks.json\n" in out def test_an_unresolvable_base_is_undecided(tmp_path, capsys) -> None: diff --git a/tests/test_floor_control_plane.py b/tests/test_floor_control_plane.py new file mode 100644 index 00000000..53df4cf0 --- /dev/null +++ b/tests/test_floor_control_plane.py @@ -0,0 +1,158 @@ +"""The floor's control-plane rule (W2-A1v2 V2): which paths, how they match, what the base's config decides.""" + +from __future__ import annotations + +import json +import os + +import pytest +from agentseam.vendor_config import VENDOR_CONFIG +from floor_support import WORKFLOW, base_repo, commit, git, run_floor, write + +from chock import vendors +from chock.validation import control_plane as cp + +FLOOR_WORKFLOW = ".github/workflows/chock.yml" +DEFAULT = cp.ControlPlane(cp.REFUSE, cp.default_patterns()) + + +@pytest.mark.parametrize("vendor", sorted(v for v in VENDOR_CONFIG if not vendors.config_path(v).startswith("~"))) +def test_every_repo_relative_vendor_hook_file_is_control_plane(vendor): + path = vendors.config_path(vendor) + assert DEFAULT.covers(path) or DEFAULT.covers(f"{path}/hook.sh"), path + + +@pytest.mark.parametrize( + "path", + [ + ".chock/config.yaml", + ".chock/bin/launch.sh", + "chock.lock", + ".agents/policies/x/manifest.yaml", + ".claude/settings.local.json", + ".github/hooks/chock.json", + ".github/workflows/any.yml", + ".github/actions/floor/action.yml", + ".github/CODEOWNERS", + "CODEOWNERS", + "docs/CODEOWNERS", + ".gitattributes", + "src/.gitattributes", + ".Chock/Config.yaml", + ".claude/SETTINGS.json", + ".claude/settings.json.", + ".claude/settings​.json", + ], +) +def test_guardrail_paths_match_in_any_spelling_a_filesystem_may_open(path): + assert DEFAULT.covers(path) + + +@pytest.mark.parametrize("path", ["src/app.py", ".claude/commands/x.md", "docs/guide.md", "chock.lock.bak", "tests/x"]) +def test_ordinary_paths_are_not_control_plane(path): + assert not DEFAULT.covers(path) + + +def test_mode_and_deletion_changes_are_listed(tmp_path, capsys): + repo = base_repo(tmp_path) + (repo / ".chock/bin/gate.py").chmod(0o755) + git(repo, "rm", "-q", ".cursor/hooks.json") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n .chock/bin/gate.py\n .cursor/hooks.json\n" in out + + +def test_the_base_config_adds_and_removes_patterns(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + config = "floor:\n control_plane_paths:\n add: ['infra/**']\n remove: ['.github/workflows/**']\n" + write(repo, ".chock/config.yaml", config) + commit(repo, "base: own patterns") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, FLOOR_WORKFLOW, WORKFLOW + "# edited\n") + write(repo, "infra/main.tf", "x = 1\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "FAIL floor/control-plane-changed (refuse): 1 path(s)" in out + assert "\n infra/main.tf\n" in out + + +@pytest.mark.parametrize( + "config", + [ + "floor:\n control_plane: off\n", + "floor: listed\n", + "floor:\n control_plane_paths: ['x']\n", + "floor:\n control_plane_paths:\n add: [1]\n", + "floor: [\n", + ], +) +def test_a_base_config_the_floor_cannot_read_is_undecided(tmp_path, capsys, config): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, ".chock/config.yaml", config) + commit(repo, "base: bad floor config") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "UNDECIDED floor: the base's .chock/config.yaml floor" in out + + +@pytest.mark.parametrize(("mode", "owed", "level"), [("refuse", 1, "error"), ("listed", 0, "note")]) +def test_sarif_lists_control_plane_paths_first(tmp_path, capsys, mode, owed, level): + repo = base_repo(tmp_path, mode) + write(repo, FLOOR_WORKFLOW, WORKFLOW + "# edited\n") + write(repo, "src/config.txt", "key = FLOOR-TOKEN-1\n") + commit(repo, "pr") + out = tmp_path / "floor.sarif" + code, text = run_floor(repo, capsys, "--sarif", str(out)) + assert code == 1, text # the secret refuses in both modes + results = json.loads(out.read_text(encoding="utf-8"))["runs"][0]["results"] + assert results[0]["ruleId"] == cp.RULE + assert results[0]["level"] == level + assert results[0]["locations"][0]["physicalLocation"]["artifactLocation"]["uri"] == FLOOR_WORKFLOW + assert {r["ruleId"] for r in results[1:]} == {"floor/refused"} + assert owed in (0, 1) + + +def test_check_routes_format_sarif_to_the_floor(tmp_path, capsys): + from chock.lifecycle import check_main + + repo = base_repo(tmp_path, "listed") + write(repo, FLOOR_WORKFLOW, WORKFLOW + "# edited\n") + commit(repo, "pr") + out = tmp_path / "floor.sarif" + argv = ["--repo", str(repo), "--floor", "--base", "main", "--format", "sarif", "--output", str(out)] + assert check_main(argv) == 0, capsys.readouterr().out + assert json.loads(out.read_text(encoding="utf-8"))["runs"][0]["results"][0]["ruleId"] == cp.RULE + + +def test_a_base_observe_rollout_softens_history_and_a_warn_gate_never_refuses(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, ".chock/config.yaml", "rollout: observe\n") + commit(repo, "base: observe") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-9'\n") + commit(repo, "add") + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "remove") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "note: floor-token [history] (warn)" in out + + +def test_no_head_file_reaches_the_working_tree(tmp_path, capsys): + """Read as objects: nothing under the head's control plane is written or run, even under `listed`.""" + repo = base_repo(tmp_path, "listed") + write(repo, ".chock/bin/gate.py", "import pathlib; pathlib.Path('MARKER').write_text('ran')\n") + commit(repo, "pr") + git(repo, "checkout", "-q", "main") + before = sorted(os.listdir(repo)) + code, out = run_floor(repo, capsys, "--head", "pr") + assert code == 0, out + assert sorted(os.listdir(repo)) == before diff --git a/tests/test_floor_tamper.py b/tests/test_floor_tamper.py deleted file mode 100644 index 68bf5880..00000000 --- a/tests/test_floor_tamper.py +++ /dev/null @@ -1,224 +0,0 @@ -"""`chock check --floor` refuses, by name, a removed or weakened guardrail, and stays silent on a regeneration.""" - -from __future__ import annotations - -import json -import shutil -from pathlib import Path - -from floor_support import ( - CLAUDE, - CURSOR, - FLOOR_TOKEN, - LOCK, - WORKFLOW, - base_repo, - commit, - gate, - git, - run_floor, - write, -) - -ROOT = Path(__file__).resolve().parents[1] - - -def test_an_unrelated_change_passes_with_no_tamper_line(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - write(repo, "src/other.py", "OTHER = 2\n") - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 0, out - assert "tamper/" not in out - assert "unverified by chock" in out - - -def test_deleting_a_vendor_hook_file_is_refused_by_name(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - (repo / ".cursor/hooks.json").unlink() - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert "tamper/hook-removed .cursor/hooks.json" in out - - -def test_removing_one_pre_tool_use_entry_names_the_event_and_command(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - settings = json.loads(json.dumps(CLAUDE)) - settings["hooks"]["PreToolUse"].pop(1) - write(repo, ".claude/settings.json", settings) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert ( - "tamper/hook-removed .claude/settings.json: PreToolUse command hook removed: " - "sh .chock/bin/launch.sh claude.py --write" in out - ) - assert out.count("tamper/hook-removed") == 1 - - -def test_narrowing_a_hook_matcher_is_a_removal(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - settings = json.loads(json.dumps(CLAUDE)) - settings["hooks"]["PreToolUse"][1]["matcher"] = "Write" - write(repo, ".claude/settings.json", settings) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert "tamper/hook-removed .claude/settings.json" in out - - -def test_a_regeneration_that_adds_a_policy_is_silent(tmp_path, capsys) -> None: - """New locked policy, a new index pin in a command, a widened matcher, a new hook: no tamper line.""" - repo = base_repo(tmp_path) - settings = json.loads(json.dumps(CLAUDE)) - command = settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"] - settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"] = command[:-64] + "cd" * 32 - settings["hooks"]["PreToolUse"][1]["matcher"] = "Write|Edit|MultiEdit" - settings["hooks"]["Stop"] = [ - {"hooks": [{"type": "command", "command": "sh .chock/bin/launch.sh claude.py --stop"}]} - ] - write(repo, ".claude/settings.json", settings) - cursor = json.loads(json.dumps(CURSOR)) - cursor["hooks"]["preToolUse"] = [{"command": "sh .chock/bin/launch.sh cursor.py --write"}] - write(repo, ".cursor/hooks.json", cursor) - gate(repo, "new-policy", {**FLOOR_TOKEN, "params": {"content_pattern": "NEW-POLICY-[0-9]+"}}) - write(repo, "chock.lock", {**LOCK, "packs": [*LOCK["packs"], {"id": "new-policy"}]}) - commit(repo, "regenerate") - code, out = run_floor(repo, capsys) - assert code == 0, out - assert "tamper/" not in out - assert "floor/pack-changed: new-policy: lock entry changed; compiled: .chock/compiled/new-policy/" in out - - -def test_removing_a_pack_from_the_lock_is_refused(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - write(repo, "chock.lock", {**LOCK, "packs": LOCK["packs"][:1]}) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert "tamper/lock-pack-removed chock.lock: pack 'floor-token'" in out - - -def test_any_edit_to_the_floor_workflow_is_refused(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - write(repo, ".github/workflows/chock.yml", WORKFLOW.split(" - name: Floor")[0]) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert "tamper/floor-workflow-changed .github/workflows/chock.yml: neither the merge-base's bytes" in out - assert "privileged merge" in out - - -def test_the_floor_workflow_may_become_what_this_engine_renders(tmp_path, capsys) -> None: - from chock.scaffold import install_ci - - repo = base_repo(tmp_path) - write(repo, ".github/workflows/chock.yml", install_ci.render()) - commit(repo, "regenerate the workflow") - code, out = run_floor(repo, capsys) - assert code == 0, out - - -def test_the_guarded_workflow_is_named_by_the_base_config(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - git(repo, "checkout", "-q", "main") - (repo / ".github/workflows/chock.yml").rename(repo / ".github/workflows/floor.yml") - write(repo, ".chock/config.yaml", "floor_workflow: .github/workflows/floor.yml\n") - commit(repo, "move the workflow") - git(repo, "checkout", "-q", "-B", "pr") - (repo / ".github/workflows/floor.yml").unlink() - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert "tamper/floor-workflow-changed .github/workflows/floor.yml: the floor's workflow was deleted" in out - - -def test_weakening_a_gate_without_a_lock_change_is_refused(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - gate(repo, "floor-token", {**FLOOR_TOKEN, "action": "warn", "on": ["tool_use"]}, surfaces=("git-hook",)) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert "tamper/gate-changed .chock/compiled/floor-token/git-hook/gate.json: no longer bound to: commit" in out - assert "chock.lock has no change for 'floor-token'" in out - - -def test_a_gate_change_with_a_lock_change_is_listed_not_refused(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - gate(repo, "floor-token", {**FLOOR_TOKEN, "message": "Still no floor tokens."}) - write(repo, "chock.lock", {**LOCK, "packs": [LOCK["packs"][0], {"id": "floor-token", "version": "2"}]}) - commit(repo, "upgrade floor-token") - code, out = run_floor(repo, capsys) - assert code == 0, out - assert "floor/pack-changed: floor-token: lock entry changed; compiled: " in out - assert ".chock/compiled/floor-token/ci-gate/gate.json, .chock/compiled/floor-token/git-hook/gate.json" in out - assert "cannot tell a policy upgrade from a policy weakening offline" in out - - -def test_deleting_a_compiled_gate_is_refused(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - (repo / ".chock/compiled/floor-token/ci-gate/gate.json").unlink() - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert "tamper/gate-removed .chock/compiled/floor-token/ci-gate/gate.json" in out - - -def test_an_edited_runner_is_never_run_and_is_refused(tmp_path, capsys) -> None: - repo = base_repo(tmp_path) - write(repo, ".chock/bin/gate.py", "raise SystemExit(0)\n") - (repo / "tests/test_core.py").unlink() - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1 - assert "tests/test_core.py: test file deleted" in out - assert "tamper/runtime-changed .chock/bin/gate.py" in out - (repo / ".chock/bin/gate.py").unlink() - commit(repo, "pr 2") - code, out = run_floor(repo, capsys) - assert "tamper/runtime-removed .chock/bin/gate.py" in out - - -def test_a_runtime_may_become_what_this_engine_renders(tmp_path, capsys) -> None: - from chock.gate.assemble import runner_source - from chock.hooks.launch import launcher_text - - repo = base_repo(tmp_path) - write(repo, ".chock/bin/gate.py", runner_source()) - write(repo, ".chock/bin/launch.sh", launcher_text()) - commit(repo, "regenerate the runtime") - code, out = run_floor(repo, capsys) - assert code == 0, out - - -def _real_install(tmp_path: Path) -> tuple[Path, str]: - """A base carrying this repository's own compiled policies, Claude Code hooks and their pinned indexes.""" - repo = tmp_path / "real" - repo.mkdir() - git(repo, "init", "-q", "-b", "main") - for rel in (".chock/compiled", ".agents/policies"): - shutil.copytree(ROOT / rel, repo / rel) - for rel in (".claude/settings.json", ".chock/config.yaml"): - (repo / rel).parent.mkdir(parents=True, exist_ok=True) - shutil.copy2(ROOT / rel, repo / rel) - index = sorted((ROOT / ".chock/bin/index").glob("claude_code-*.json"))[0] - rel = f".chock/bin/index/{index.name}" - (repo / rel).parent.mkdir(parents=True, exist_ok=True) - (repo / rel).write_text("{}\n", encoding="utf-8") - commit(repo, "base: a stale index") - git(repo, "checkout", "-q", "-b", "pr") - return repo, rel - - -def test_an_index_this_engine_renders_is_accepted_and_any_other_refused(tmp_path, capsys) -> None: - repo, rel = _real_install(tmp_path) - shutil.copy2(ROOT / rel, repo / rel) - commit(repo, "sync rewrites the index") - code, out = run_floor(repo, capsys, "--head-ref", "pr") - assert code == 0, out - write(repo, rel, json.loads((ROOT / rel).read_text(encoding="utf-8")) | {"members": []}) - commit(repo, "an index that runs nothing") - code, out = run_floor(repo, capsys, "--head-ref", "pr") - assert code == 1 - assert f"tamper/runtime-changed {rel}" in out From 2c98067e9f27e46deab62cc0d732801ce03521cf Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:37:54 +0000 Subject: [PATCH 18/57] test: split the managed emit tests and keep the docs within their budgets The managed emit tests move into two files over a shared helper module, each under the 300-line budget; the two doc pages are reflowed to fit theirs. The surface-doc tests pin the corrected wording ("neither compiled nor installed", "not installed by chock") and now also check that the compiler has no managed-setting emitter. The README stays in its word budget. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 2 +- docs/cli-reference.md | 42 +-- docs/enforcement-surfaces.md | 11 +- tests/managed_emit_helpers.py | 130 +++++++++ tests/test_managed_emit_claude.py | 370 ++++--------------------- tests/test_managed_emit_runs.py | 198 +++++++++++++ tests/test_surface_doc_matches_code.py | 8 +- 7 files changed, 399 insertions(+), 362 deletions(-) create mode 100644 tests/managed_emit_helpers.py create mode 100644 tests/test_managed_emit_runs.py diff --git a/README.md b/README.md index d88463fb..77a900d7 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ file that wiring lives. A row here is support, not installation — coverage is a given repo once `chock sync` has actually written the file, which is why the CLI's own `chock status` always beats this table for what's true *here*. Four of the nine surfaces are absent from this page entirely because they credit no agent today — `managed-setting` is -written only by `chock emit --managed`, for an admin to deploy, `gateway` is modelled but not yet emitted, `mcp-gateway` credits +not installed by chock, `gateway` is modelled but not yet emitted, `mcp-gateway` credits nothing until its per-client witness ships, and `stop` installs and refuses on eight vendors but is a deliberate backstop for what a pre-tool hook cannot see, so it is worth no grade of its own. Full nine-surface matrix and per-agent caveats: diff --git a/docs/cli-reference.md b/docs/cli-reference.md index 872450b1..983049c8 100644 --- a/docs/cli-reference.md +++ b/docs/cli-reference.md @@ -162,12 +162,7 @@ frontmatter *is* its manifest; `new subagent` writes `subagent.yaml`. `new polic chock compile [--repo .] [--targets SURFACE ...] [--agents AGENT ...] [--policy-dir DIR] [--output-root DIR] ``` -Compiles one policy into the surfaces each target agent supports (git hook, CI gate, -pre-tool-use, agent-hooks, `AGENTS.md` rule; nothing for `managed-setting`, which only `emit` writes) and updates the per-agent -coverage report. `--agents` takes the same comma- or space-separated names as `init` -and `sync`, defaults to the repo's `supported_agents`, and rejects unknown names. -`sync` runs this for every enabled policy; reach for `compile` directly only when -iterating on a single policy's emitted output. +Compiles one policy into the surfaces each target agent supports (git hook, CI gate, pre-tool-use, agent-hooks, `AGENTS.md` rule; nothing for `managed-setting`, which only `emit` writes) and updates the per-agent coverage report. `--agents` takes the same comma- or space-separated names as `init` and `sync`, defaults to the repo's `supported_agents`, and rejects unknown names. `sync` runs this for every enabled policy; reach for `compile` directly only when iterating on a single policy's emitted output. ### `install-skills` — refresh the bundled authoring skills @@ -197,24 +192,9 @@ diffs the result to catch a stale registry. See [Registry & Lockfile](registry-a chock plugin build [--repo .] [--policies-dir base] [--format agent-plugins|claude|copilot|cursor|codex|devin|all] [--out-dir DIST] [--policy ID ...] [--out PATH] [--check] ``` -Renders each policy as a plugin. The default `agent-plugins` format writes an -[Agent Plugins 1.0.0](https://agent-plugins.org) package into each policy folder — additive, -`manifest.yaml` stays the source of truth, and a packaged policy is `advisory` wherever it is -read: v1 defines no enforcement semantics, so packaging changes no value in `coverage.json`. - -The five hook formats ship a byte-identical guard and adapter; since 0.11.0 the policy's own gate rides along too -(`scripts/gate.json`, the stdlib runner, a script gate's `implementations/` tree), as does its `skill/` folder. Only -the envelope differs. `claude` (`.claude-plugin/`, `PreToolUse` + `Stop`) is read natively by Claude Code, Copilot -CLI, VS Code and Grok Build; `copilot` is the Agent Plugins 1.0 layout under `com.github.copilot/hooks/` (`Stop`); -`cursor` (`.cursor-plugin/`) takes `beforeShellExecution` per guard and, per gate, `preToolUse` on the write plus -`stop`; `codex` (`.codex-plugin/`, `PreToolUse` per guard, `Stop` per gate) reaches a hook engine no other package -can, both failing **open** without `git`; `devin` (`.devin-plugin/plugin.json` + `hooks.json`, same two events) -is best-effort by the vendor's own design, fail-open, not enforced. They require `--out-dir` (or `--out`); in-place output is -refused so a policy folder is never mistaken for a published plugin. `--policies-dir` packages -a published directory; `--check` judges without writing. `--policy ID` (repeatable; manifest -`id` or directory name, else a named error) narrows the build and skips `--out-dir` -stale-package cleanup. `--out PATH` (exactly one `--policy`) writes straight to `PATH` instead -of `///`. +Renders each policy as a plugin. The default `agent-plugins` format writes an [Agent Plugins 1.0.0](https://agent-plugins.org) package into each policy folder — additive, `manifest.yaml` stays the source of truth, and a packaged policy is `advisory` wherever it is read: v1 defines no enforcement semantics, so packaging changes no value in `coverage.json`. + +The five hook formats ship a byte-identical guard and adapter; since 0.11.0 the policy's own gate rides along too (`scripts/gate.json`, the stdlib runner, a script gate's `implementations/` tree), as does its `skill/` folder. Only the envelope differs. `claude` (`.claude-plugin/`, `PreToolUse` + `Stop`) is read natively by Claude Code, Copilot CLI, VS Code and Grok Build; `copilot` is the Agent Plugins 1.0 layout under `com.github.copilot/hooks/` (`Stop`); `cursor` (`.cursor-plugin/`) takes `beforeShellExecution` per guard and, per gate, `preToolUse` on the write plus `stop`; `codex` (`.codex-plugin/`, `PreToolUse` per guard, `Stop` per gate) reaches a hook engine no other package can, both failing **open** without `git`; `devin` (`.devin-plugin/plugin.json` + `hooks.json`, same two events) is best-effort by the vendor's own design, fail-open, not enforced. They require `--out-dir` (or `--out`); in-place output is refused so a policy folder is never mistaken for a published plugin. `--policies-dir` packages a published directory; `--check` judges without writing. `--policy ID` (repeatable; manifest `id` or directory name, else a named error) narrows the build and skips `--out-dir` stale-package cleanup. `--out PATH` (exactly one `--policy`) writes straight to `PATH` instead of `///`. ### `marketplace build` — index a built plugin tree @@ -237,19 +217,7 @@ each built plugin, and needs `--url` (the marketplace repo's own git URL — nev chock emit --managed claude_code --python --on-failure block|allow --out [--repo .] [--policies a,b] [--platform linux|macos|windows] [--deploy-root ] [--lock] ``` -Writes, under `/claude_code/` only: `managed-settings.d/50-chock.json` (the selected policies' guards and -gates as exec-form hooks), `bundle/` (each policy's runtime and guard or gate), `deploy.json` (where an -administrator places each part, what each machine needs, what `--on-failure` does, the caveats, the undo step) and -`labels.json`. It deploys nothing, elevates nothing, and refuses an `--out` that is a filesystem root or resolves -into a system folder (the per-OS list in `src/chock/data/managed_channels.json`), names a file, or already holds -`claude_code/`. `--python` is required: the absolute interpreter on the target machines, which every hook starts -directly, so neither PATH nor git config picks it. `--on-failure` is required: `block` sets `onFailure: "block"` on -the `PreToolUse` hooks and refuses a matched call when the interpreter or a bundle file is missing; `allow` sets -neither and lets the call through with a note on stderr. `--lock` also emits `allowManagedHooksOnly`, which blocks -every user, project and other-plugin hook on the machine (`deploy.json` lists every documented effect); -`allowManagedPermissionRulesOnly` is never emitted. Every label is vendor-docs evidence, unwitnessed, at most -best-effort; a local administrator can still edit or remove the file, and a machine that also receives -server-managed or MDM policy skips it by default. +Writes, under `/claude_code/` only: `managed-settings.d/50-chock.json` (the selected policies' guards and gates as exec-form hooks), `bundle/` (each policy's runtime and guard or gate), `deploy.json` (where an administrator places each part, what each machine needs, what `--on-failure` does, the caveats, the undo step) and `labels.json`. It deploys nothing, elevates nothing, and refuses an `--out` that is a filesystem root or resolves into a system folder (the per-OS list in `src/chock/data/managed_channels.json`), names a file, or already holds `claude_code/`. `--python` is required: the absolute interpreter on the target machines, which every hook starts directly, so neither PATH nor git config picks it. `--on-failure` is required: `block` sets `onFailure: "block"` on the `PreToolUse` hooks and refuses a matched call when the interpreter or a bundle file is missing; `allow` sets neither and lets the call through with a note on stderr. `--lock` also emits `allowManagedHooksOnly`, which blocks every user, project and other-plugin hook on the machine (`deploy.json` lists every documented effect); `allowManagedPermissionRulesOnly` is never emitted. Every label is vendor-docs evidence, unwitnessed, at most best-effort; a local administrator can still edit or remove the file, and a machine that also receives server-managed or MDM policy skips it by default. ### `gateway run` -- the MCP gateway proxy diff --git a/docs/enforcement-surfaces.md b/docs/enforcement-surfaces.md index 0f6f679e..f6ec53eb 100644 --- a/docs/enforcement-surfaces.md +++ b/docs/enforcement-surfaces.md @@ -132,12 +132,11 @@ per gateway process; wrap N servers with N entries. > clone is not bricked: the launcher finds one or refuses with a fix-it message. The guard > covers shell commands (`beforeShellExecution`); other tool classes are not intercepted. -> **`managed-setting` is neither compiled nor installed.** The compiler writes nothing for it. -> `chock emit --managed claude_code` writes a managed-settings drop-in and the hook bundle it runs -> under a folder you name, for an administrator to deploy ([CLI reference](cli-reference.md#emit--write-a-managed-settings-bundle-for-an-admin)); -> chock deploys nothing. It is excluded from `INSTALLED_SURFACES` in `surfaces.py`, so no policy can -> raise a coverage claim through it, and the emitted labels are vendor-docs evidence, unwitnessed, -> best-effort. Read the row below as *what the agent could support*, not as something switched on. +> **`managed-setting` is neither compiled nor installed.** The compiler writes nothing for it; `chock emit --managed claude_code` +> writes a drop-in and its hook bundle under a folder you name, for an administrator to deploy ([CLI reference](cli-reference.md#emit--write-a-managed-settings-bundle-for-an-admin)). +> chock deploys nothing. It is excluded from `INSTALLED_SURFACES` in `surfaces.py`, so no policy can raise a coverage claim +> through it, and the emitted labels are vendor-docs evidence, unwitnessed, best-effort. Read the row below as *what the +> agent could support*, not as something switched on. > **`ci-gate` needs `chock sync --ci` to actually run.** The compiler always writes > `.chock/compiled//ci-gate/gate.json` and `step.yaml`, but nothing invokes them until diff --git a/tests/managed_emit_helpers.py b/tests/managed_emit_helpers.py new file mode 100644 index 00000000..7e8d39f7 --- /dev/null +++ b/tests/managed_emit_helpers.py @@ -0,0 +1,130 @@ +"""Shared fixture and helpers for the `chock emit --managed claude_code` tests.""" + +from __future__ import annotations + +import json +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest +from conftest import baseline_policy + +from chock import cli +from chock.doctor.payloads import payload + +FIXTURES = Path(__file__).resolve().parent / "fixtures" / "managed_emit" +POLICIES = ("block-no-verify", "scan-secrets") +DEPLOY_ROOT = "/srv/chock-managed" +DROP_IN = Path("managed-settings.d") / "50-chock.json" +PERMISSION_RULE = re.compile(r"^[A-Za-z]+\(.*\)$") +OVERRIDE_CLAIM = re.compile(r"(cannot|can't|can not|unable to) (be )?overrid", re.IGNORECASE) + + +def make_repo(tmp_path: Path) -> Path: + """A repository holding the two golden policies and one advisory policy with nothing to run.""" + root = tmp_path / "repo" + for policy_id in (*POLICIES, "yagni"): + shutil.copytree(baseline_policy(policy_id), root / ".agents" / "policies" / policy_id) + return root + + +PYTHON = "/usr/bin/python3" +ROOT = Path(__file__).resolve().parents[1] +DOCS = ROOT / "docs" +FAIL_CLOSED = re.compile(r"fail(s|ing|ed)?[- ]closed|cannot be (bypassed|disabled)|\benforced\b", re.IGNORECASE) + + +def emit(repo: Path, out: Path, *extra: str, python: str = PYTHON, on_failure: str = "block") -> int: + return cli.main( + [ + "emit", + "--managed", + "claude_code", + "--out", + str(out), + "--repo", + str(repo), + "--python", + python, + "--on-failure", + on_failure, + *extra, + ] + ) + + +def linux(repo: Path, out: Path, *extra: str, on_failure: str = "block") -> int: + return emit(repo, out, "--platform", "linux", "--deploy-root", DEPLOY_ROOT, *extra, on_failure=on_failure) + + +def drop_in(out: Path) -> dict: + return json.loads((out / "claude_code" / DROP_IN).read_text(encoding="utf-8")) + + +def handlers(doc: dict) -> list[tuple[str, dict]]: + return [(event, hook) for event, groups in doc["hooks"].items() for group in groups for hook in group["hooks"]] + + +def tree(root: Path, skip: Path) -> dict[str, bytes]: + return { + p.relative_to(root).as_posix(): p.read_bytes() + for p in root.rglob("*") + if p.is_file() and skip not in p.parents and "__pycache__" not in p.parts + } + + +def bundle_hook(repo: Path, tmp_path: Path, on_failure: str = "block") -> tuple[Path, dict]: + """The block-no-verify guard's handler, emitted with this test's Python and rooted at the bundle itself.""" + out = tmp_path / "out" + bundle = (out / "claude_code" / "bundle").as_posix() + assert ( + emit( + repo, + out, + "--policies", + "block-no-verify", + "--deploy-root", + bundle, + python=sys.executable, + on_failure=on_failure, + ) + == 0 + ) + [(_, hook), *_] = handlers(drop_in(out)) + return out / "claude_code" / "bundle", hook + + +def env_for(tmp_path: Path, **extra: str) -> dict[str, str]: + env = {k: v for k, v in os.environ.items() if k not in ("CLAUDECODE", "AI_AGENT", "CHOCK_AGENT_COMMIT")} + home = tmp_path / "home" + home.mkdir(exist_ok=True) + env["HOME"] = env["USERPROFILE"] = str(home) + return {**env, **extra} + + +def run_hook(hook: dict, command: str, cwd: Path, env: dict[str, str]) -> subprocess.CompletedProcess: + """Run `hook` the way the client runs exec form: `command` spawned with `args`, no shell.""" + return subprocess.run( + [hook["command"], *hook["args"]], + input=json.dumps(payload("claude_code", command, cwd.as_posix())), + capture_output=True, + text=True, + cwd=cwd, + env=env, + check=False, + timeout=60, + ) + + +def one_line_refusal(capsys: pytest.CaptureFixture[str]) -> str: + err = capsys.readouterr().err + assert "Traceback" not in err and len(err.strip().splitlines()) == 1, err + return err + + +def plan_of(out: Path) -> dict: + return json.loads((out / "claude_code" / "deploy.json").read_text(encoding="utf-8")) diff --git a/tests/test_managed_emit_claude.py b/tests/test_managed_emit_claude.py index 6dc6ea69..a8fa1373 100644 --- a/tests/test_managed_emit_claude.py +++ b/tests/test_managed_emit_claude.py @@ -1,84 +1,42 @@ -"""`chock emit --managed claude_code`: a valid drop-in, a self-contained bundle, honest labels, and nothing outside --out.""" +"""`chock emit --managed claude_code`: a valid drop-in, honest labels, docs and wording, and nothing outside --out.""" from __future__ import annotations import json import os -import re -import shutil -import subprocess import sys from pathlib import Path import pytest -from conftest import baseline_policy +from managed_emit_helpers import ( + DEPLOY_ROOT, + DOCS, + DROP_IN, + FAIL_CLOSED, + FIXTURES, + OVERRIDE_CLAIM, + PERMISSION_RULE, + POLICIES, + PYTHON, + ROOT, + drop_in, + emit, + handlers, + linux, + make_repo, + tree, +) from chock import cli from chock.compile.emitters import managed from chock.compile.emitters.in_agent_hooks import TIMEOUT_SECONDS from chock.compile.emitters.managed import claude_code from chock.compile.levels import cap_for, level_rank -from chock.doctor.payloads import payload - -FIXTURES = Path(__file__).resolve().parent / "fixtures" / "managed_emit" -POLICIES = ("block-no-verify", "scan-secrets") -DEPLOY_ROOT = "/srv/chock-managed" -DROP_IN = Path("managed-settings.d") / "50-chock.json" -PERMISSION_RULE = re.compile(r"^[A-Za-z]+\(.*\)$") -OVERRIDE_CLAIM = re.compile(r"(cannot|can't|can not|unable to) (be )?overrid", re.IGNORECASE) @pytest.fixture def repo(tmp_path: Path) -> Path: - """A repository holding the two golden policies and one advisory policy with nothing to run.""" - root = tmp_path / "repo" - for policy_id in (*POLICIES, "yagni"): - shutil.copytree(baseline_policy(policy_id), root / ".agents" / "policies" / policy_id) - return root - - -PYTHON = "/usr/bin/python3" -ROOT = Path(__file__).resolve().parents[1] -FAIL_CLOSED = re.compile(r"fail(s|ing|ed)?[- ]closed|cannot be (bypassed|disabled)|\benforced\b", re.IGNORECASE) - - -def _emit(repo: Path, out: Path, *extra: str, python: str = PYTHON, on_failure: str = "block") -> int: - return cli.main( - [ - "emit", - "--managed", - "claude_code", - "--out", - str(out), - "--repo", - str(repo), - "--python", - python, - "--on-failure", - on_failure, - *extra, - ] - ) - - -def _linux(repo: Path, out: Path, *extra: str, on_failure: str = "block") -> int: - return _emit(repo, out, "--platform", "linux", "--deploy-root", DEPLOY_ROOT, *extra, on_failure=on_failure) - - -def _drop_in(out: Path) -> dict: - return json.loads((out / "claude_code" / DROP_IN).read_text(encoding="utf-8")) - - -def _handlers(doc: dict) -> list[tuple[str, dict]]: - return [(event, hook) for event, groups in doc["hooks"].items() for group in groups for hook in group["hooks"]] - - -def _tree(root: Path, skip: Path) -> dict[str, bytes]: - return { - p.relative_to(root).as_posix(): p.read_bytes() - for p in root.rglob("*") - if p.is_file() and skip not in p.parents and "__pycache__" not in p.parts - } + return make_repo(tmp_path) def test_nothing_outside_out_changes(repo: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: @@ -89,9 +47,9 @@ def test_nothing_outside_out_changes(repo: Path, tmp_path: Path, monkeypatch: py monkeypatch.setenv("USERPROFILE", str(home)) monkeypatch.chdir(tmp_path) out = tmp_path / "out" - before = _tree(tmp_path, out) - assert _linux(repo, out) == 0 - assert _tree(tmp_path, out) == before + before = tree(tmp_path, out) + assert linux(repo, out) == 0 + assert tree(tmp_path, out) == before assert sorted(p.name for p in out.iterdir()) == ["claude_code"] @@ -102,7 +60,7 @@ def _system_out() -> Path: def test_out_under_a_system_path_is_refused(repo: Path, capsys: pytest.CaptureFixture[str]) -> None: """Acceptance 2: no file is written and the exit is non-zero.""" out = _system_out() - assert _emit(repo, out) == 1 + assert emit(repo, out) == 1 assert "resolves to or inside the system path" in capsys.readouterr().err assert not out.exists() @@ -111,7 +69,7 @@ def test_out_under_a_system_path_is_refused(repo: Path, capsys: pytest.CaptureFi def test_out_through_a_symlink_into_a_system_path_is_refused(repo: Path, tmp_path: Path) -> None: link = tmp_path / "innocent" link.symlink_to(_system_out().parent) - assert _emit(repo, link) == 1 + assert emit(repo, link) == 1 assert not (_system_out().parent / "claude_code").exists() @@ -119,21 +77,21 @@ def test_an_existing_vendor_folder_is_never_written_into(repo: Path, tmp_path: P out = tmp_path / "out" (out / "claude_code").mkdir(parents=True) (out / "claude_code" / "mine.txt").write_text("keep", encoding="utf-8") - assert _linux(repo, out) == 1 + assert linux(repo, out) == 1 assert [p.name for p in (out / "claude_code").iterdir()] == ["mine.txt"] @pytest.mark.parametrize("root", ["relative/dir", "/has\nnewline", "/"]) def test_a_deploy_root_that_is_not_an_absolute_folder_is_refused(repo: Path, tmp_path: Path, root: str) -> None: - assert _emit(repo, tmp_path / "out", "--platform", "linux", "--deploy-root", root) == 1 + assert emit(repo, tmp_path / "out", "--platform", "linux", "--deploy-root", root) == 1 assert not (tmp_path / "out" / "claude_code").exists() def test_a_deploy_root_with_shell_characters_is_one_argument(repo: Path, tmp_path: Path) -> None: """Finding 8: exec form, no shell, so quote, $ and backquote reach the client as written.""" root = '/srv/a"b$c`d e' - assert _emit(repo, tmp_path / "out", "--platform", "linux", "--deploy-root", root) == 0 - args = [arg for _, hook in _handlers(_drop_in(tmp_path / "out")) for arg in hook["args"]] + assert emit(repo, tmp_path / "out", "--platform", "linux", "--deploy-root", root) == 0 + args = [arg for _, hook in handlers(drop_in(tmp_path / "out")) for arg in hook["args"]] assert f"{root}/block-no-verify/scripts/claude_code.py" in args @@ -151,12 +109,12 @@ def test_a_windows_deploy_root_is_written_with_forward_slashes() -> None: def test_the_drop_in_follows_the_documented_schema(repo: Path, tmp_path: Path) -> None: """Acceptance 3: JSON object; PreToolUse handlers block on failure with chock's timeout; Stop takes no onFailure.""" out = tmp_path / "out" - assert _linux(repo, out) == 0 - doc = _drop_in(out) + assert linux(repo, out) == 0 + doc = drop_in(out) assert all(PERMISSION_RULE.match(rule) for rule in doc.get("permissions", {}).get("deny", [])) - handlers = _handlers(doc) - assert {event for event, _ in handlers} == {"PreToolUse", "Stop"} - for event, hook in handlers: + found = handlers(doc) + assert {event for event, _ in found} == {"PreToolUse", "Stop"} + for event, hook in found: assert hook["type"] == "command" assert hook["timeout"] == TIMEOUT_SECONDS == 45 assert hook.get("onFailure") == ("block" if event == "PreToolUse" else None) @@ -164,8 +122,8 @@ def test_the_drop_in_follows_the_documented_schema(repo: Path, tmp_path: Path) - def test_hooks_are_exec_form_started_by_the_given_python(repo: Path, tmp_path: Path) -> None: """Findings 1, 8: the command is --python itself; no git, sh, launcher or PATH lookup stands in between.""" - assert _linux(repo, tmp_path / "out") == 0 - for _, hook in _handlers(_drop_in(tmp_path / "out")): + assert linux(repo, tmp_path / "out") == 0 + for _, hook in handlers(drop_in(tmp_path / "out")): assert hook["command"] == PYTHON assert hook["args"][:4] == ["-I", "-X", "utf8", "-c"] assert not [arg for arg in hook["args"] if "launch.sh" in arg or "chock.python" in arg or "git -c" in arg] @@ -175,7 +133,7 @@ def test_hooks_are_exec_form_started_by_the_given_python(repo: Path, tmp_path: P @pytest.mark.parametrize("python", ["python3", "bin/python3", ""]) def test_python_must_be_an_absolute_path(repo: Path, tmp_path: Path, python: str, capsys) -> None: """Ruling 1: the interpreter is never taken from PATH.""" - assert _emit(repo, tmp_path / "out", "--platform", "linux", python=python) == 1 + assert emit(repo, tmp_path / "out", "--platform", "linux", python=python) == 1 assert "must be the absolute path of the Python 3.11+ interpreter" in capsys.readouterr().err assert not (tmp_path / "out").exists() @@ -194,16 +152,16 @@ def test_python_and_on_failure_have_no_default(repo: Path, tmp_path: Path, missi def test_on_failure_allow_emits_no_failure_key(repo: Path, tmp_path: Path) -> None: """Ruling 2: `allow` emits no onFailure anywhere.""" - assert _linux(repo, tmp_path / "out", on_failure="allow") == 0 - assert not [hook for _, hook in _handlers(_drop_in(tmp_path / "out")) if "onFailure" in hook] + assert linux(repo, tmp_path / "out", on_failure="allow") == 0 + assert not [hook for _, hook in handlers(drop_in(tmp_path / "out")) if "onFailure" in hook] def test_no_matcherless_stop_check_is_emitted(repo: Path, tmp_path: Path) -> None: """Finding 2: every pre-tool group names its tools; the repo stop's every-tool entry is left out.""" - assert _linux(repo, tmp_path / "out") == 0 - doc = _drop_in(tmp_path / "out") + assert linux(repo, tmp_path / "out") == 0 + doc = drop_in(tmp_path / "out") assert all(group.get("matcher") for group in doc["hooks"]["PreToolUse"]) - assert not [hook for _, hook in _handlers(doc) if "--stop-check" in hook["args"]] + assert not [hook for _, hook in handlers(doc) if "--stop-check" in hook["args"]] @pytest.mark.parametrize("choice", ["block", "allow"]) @@ -211,7 +169,7 @@ def test_the_failure_consequence_is_stated_in_deploy_json_and_output( repo: Path, tmp_path: Path, choice: str, capsys ) -> None: """Ruling 2: one sentence each, naming the interpreter and the matched tools.""" - assert _linux(repo, tmp_path / "out", on_failure=choice) == 0 + assert linux(repo, tmp_path / "out", on_failure=choice) == 0 plan = json.loads((tmp_path / "out" / "claude_code" / "deploy.json").read_text(encoding="utf-8")) sentence = plan["on_failure"]["consequence"] assert plan["on_failure"]["choice"] == choice @@ -223,10 +181,10 @@ def test_the_failure_consequence_is_stated_in_deploy_json_and_output( def test_no_managed_only_key_without_lock(repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: """Acceptance 4: no allowManaged* key by default; --lock adds the hooks key alone, never the permission-rules one.""" - assert _linux(repo, tmp_path / "plain") == 0 - assert not [key for key in _drop_in(tmp_path / "plain") if key.startswith("allowManaged")] - assert _linux(repo, tmp_path / "locked", "--lock") == 0 - assert [key for key in _drop_in(tmp_path / "locked") if key.startswith("allowManaged")] == ["allowManagedHooksOnly"] + assert linux(repo, tmp_path / "plain") == 0 + assert not [key for key in drop_in(tmp_path / "plain") if key.startswith("allowManaged")] + assert linux(repo, tmp_path / "locked", "--lock") == 0 + assert [key for key in drop_in(tmp_path / "locked") if key.startswith("allowManaged")] == ["allowManagedHooksOnly"] text = capsys.readouterr().out assert "allowManagedPermissionRulesOnly is never emitted" in text assert "chock's own project hooks and plugins included" in text @@ -235,8 +193,8 @@ def test_no_managed_only_key_without_lock(repo: Path, tmp_path: Path, capsys: py def test_hook_commands_name_only_the_deploy_root(repo: Path, tmp_path: Path) -> None: """Acceptance 5: no repo-relative path, every path argument under the deploy root and in the bundle.""" out = tmp_path / "out" - assert _linux(repo, out) == 0 - for _, hook in _handlers(_drop_in(out)): + assert linux(repo, out) == 0 + for _, hook in handlers(drop_in(out)): paths = [arg for arg in hook["args"][5:] if not arg.startswith("--")] assert paths and all(path.startswith(f"{DEPLOY_ROOT}/") for path in paths) for path in paths: @@ -247,7 +205,7 @@ def test_hook_commands_name_only_the_deploy_root(repo: Path, tmp_path: Path) -> def test_drop_in_golden(repo: Path, tmp_path: Path, policy_id: str) -> None: """Acceptance 6: the file for one policy, byte for byte.""" out = tmp_path / "out" - assert _linux(repo, out, "--policies", policy_id) == 0 + assert linux(repo, out, "--policies", policy_id) == 0 actual = (out / "claude_code" / DROP_IN).read_text(encoding="utf-8") golden = FIXTURES / f"{policy_id}.json" if os.environ.get("CHOCK_REGEN_GOLDENS") == "1": @@ -259,7 +217,7 @@ def test_drop_in_golden(repo: Path, tmp_path: Path, policy_id: str) -> None: def test_labels_are_vendor_docs_unwitnessed_and_capped(repo: Path, tmp_path: Path) -> None: """Acceptance 7: every emitted cell rests on vendor docs, is unwitnessed, and is at most best-effort.""" out = tmp_path / "out" - assert _linux(repo, out) == 0 + assert linux(repo, out) == 0 labels = json.loads((out / "claude_code" / "labels.json").read_text(encoding="utf-8")) assert [row["policy"] for row in labels] == list(POLICIES) for row in labels: @@ -273,7 +231,7 @@ def test_nothing_written_or_printed_claims_users_cannot_override( repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str] ) -> None: out = tmp_path / "out" - assert _linux(repo, out, "--lock") == 0 + assert linux(repo, out, "--lock") == 0 texts = [capsys.readouterr().out, *(p.read_text(encoding="utf-8") for p in (out / "claude_code").glob("*.json"))] texts.append((out / "claude_code" / DROP_IN).read_text(encoding="utf-8")) texts.append(json.dumps(managed.CHANNELS)) @@ -286,230 +244,13 @@ def test_nothing_written_or_printed_claims_users_cannot_override( def test_a_selection_with_nothing_to_run_writes_nothing(repo: Path, tmp_path: Path) -> None: - assert _linux(repo, tmp_path / "out", "--policies", "yagni") == 1 + assert linux(repo, tmp_path / "out", "--policies", "yagni") == 1 assert not (tmp_path / "out").exists() -def _bundle_hook(repo: Path, tmp_path: Path, on_failure: str = "block") -> tuple[Path, dict]: - """The block-no-verify guard's handler, emitted with this test's Python and rooted at the bundle itself.""" - out = tmp_path / "out" - bundle = (out / "claude_code" / "bundle").as_posix() - assert ( - _emit( - repo, - out, - "--policies", - "block-no-verify", - "--deploy-root", - bundle, - python=sys.executable, - on_failure=on_failure, - ) - == 0 - ) - [(_, hook), *_] = _handlers(_drop_in(out)) - return out / "claude_code" / "bundle", hook - - -def _env(tmp_path: Path, **extra: str) -> dict[str, str]: - env = {k: v for k, v in os.environ.items() if k not in ("CLAUDECODE", "AI_AGENT", "CHOCK_AGENT_COMMIT")} - home = tmp_path / "home" - home.mkdir(exist_ok=True) - env["HOME"] = env["USERPROFILE"] = str(home) - return {**env, **extra} - - -def _run(hook: dict, command: str, cwd: Path, env: dict[str, str]) -> subprocess.CompletedProcess: - """Run `hook` the way the client runs exec form: `command` spawned with `args`, no shell.""" - return subprocess.run( - [hook["command"], *hook["args"]], - input=json.dumps(payload("claude_code", command, cwd.as_posix())), - capture_output=True, - text=True, - cwd=cwd, - env=env, - check=False, - timeout=60, - ) - - -def test_the_bundle_runs_on_its_own(repo: Path, tmp_path: Path) -> None: - """The emitted handler, rooted at the bundle itself, refuses the canary and allows a harmless call.""" - _, hook = _bundle_hook(repo, tmp_path) - env = _env(tmp_path) - denied, allowed = _run(hook, "git commit --no-verify -m x", tmp_path, env), _run(hook, "git status", tmp_path, env) - assert denied.returncode == allowed.returncode == 0, denied.stderr + allowed.stderr - assert '"permissionDecision": "deny"' in denied.stdout - assert "deny" not in allowed.stdout - - -@pytest.mark.skipif(sys.platform == "win32" or not shutil.which("git"), reason="POSIX stubs and git config") -def test_user_interpreter_settings_do_not_switch_the_hook_off(repo: Path, tmp_path: Path) -> None: - """Finding 1: chock.python set to a stub (local and global) and stub python3, python, sh, git first on PATH.""" - _, hook = _bundle_hook(repo, tmp_path) - stubs = tmp_path / "stubs" - stubs.mkdir() - for name in ("python3", "python", "py", "sh", "git"): - stub = stubs / name - stub.write_text("#!/bin/sh\nprintf chock-python >&9 2>/dev/null; exit 0\n", encoding="utf-8") - stub.chmod(0o755) - work = tmp_path / "work" - work.mkdir() - subprocess.run(["git", "init", "-q", str(work)], check=True) - subprocess.run(["git", "-C", str(work), "config", "chock.python", str(stubs / "python3")], check=True) - global_config = tmp_path / "gitconfig" - global_config.write_text(f"[chock]\n\tpython = {stubs / 'python3'}\n", encoding="utf-8") - env = _env( - tmp_path, - PATH=f"{stubs}{os.pathsep}{os.environ.get('PATH', '')}", - GIT_CONFIG_GLOBAL=str(global_config), - PYTHONPATH=str(stubs), - PYTHONSTARTUP=str(stubs / "python3"), - ) - for cwd in (work, tmp_path): - done = _run(hook, "git commit --no-verify -m x", cwd, env) - assert done.returncode == 0, done.stderr - assert '"permissionDecision": "deny"' in done.stdout - - -@pytest.mark.parametrize(("choice", "code"), [("block", 2), ("allow", 0)]) -def test_a_missing_bundle_file_follows_on_failure(repo: Path, tmp_path: Path, choice: str, code: int) -> None: - """Ruling 2: block refuses (exit 2) and allow lets the call through, each with a note on stderr.""" - bundle, hook = _bundle_hook(repo, tmp_path, on_failure=choice) - (bundle / "block-no-verify" / "scripts" / "_chock_runtime.py").unlink() - done = _run(hook, "git status", tmp_path, _env(tmp_path)) - assert done.returncode == code - assert done.stderr.startswith("chock: missing ") and f"--on-failure {choice}" in done.stderr - assert done.stdout == "" - - -@pytest.mark.parametrize(("choice", "code"), [("block", 2), ("allow", 0)]) -def test_too_old_a_python_follows_on_failure(choice: str, code: int) -> None: - """Ruling 2: the version check runs before any runtime code; here the minimum is raised past this Python.""" - program = claude_code.bootstrap(choice).replace("(3, 11,)", "(99,)") - done = subprocess.run( - [sys.executable, "-I", "-c", program, __file__], capture_output=True, text=True, check=False, timeout=60 - ) - assert done.returncode == code - assert "this hook needs Python" in done.stderr and f"--on-failure {choice}" in done.stderr - - -def test_the_stop_gate_never_refuses_for_a_missing_runtime(repo: Path, tmp_path: Path) -> None: - """onFailure has no effect on Stop, and exit 2 there sends Claude back to work: the turn-end gate notes and exits 0.""" - assert _linux(repo, tmp_path / "out", "--policies", "scan-secrets") == 0 - stop = [hook for event, hook in _handlers(_drop_in(tmp_path / "out")) if event == "Stop"] - assert stop and all("sys.exit(0)" in hook["args"][4] for hook in stop) - - -@pytest.mark.parametrize( - ("path", "platform"), - [ - ("/", "linux"), - ("//etc/x", "linux"), - ("/var/lib/x", "linux"), - ("/srv/x", "linux"), - ("/root/x", "linux"), - ("/run/x", "linux"), - ("/sys/x", "linux"), - ("/proc/x", "linux"), - ("/", "macos"), - ("/LIBRARY/Application Support/ClaudeCode/x", "macos"), - ("/system/x", "macos"), - ("/private/etc/x", "macos"), - ("C:/", "windows"), - ("D:\\", "windows"), - ("c:\\program files\\ClaudeCode", "windows"), - ("\\\\?\\C:\\Program Files\\x", "windows"), - ("\\\\.\\C:\\Windows\\x", "windows"), - ("\\\\localhost\\C$\\Program Files\\x", "windows"), - ("\\\\?\\UNC\\server\\c$\\Windows", "windows"), - ], -) -def test_every_system_spelling_is_refused(path: str, platform: str) -> None: - """Finding 4: filesystem roots, the listed trees, case-insensitive spellings, device and admin-share prefixes.""" - assert managed.root_named(path, platform) is not None - - -@pytest.mark.parametrize( - ("path", "platform"), - [("/home/me/out", "linux"), ("/tmp/out", "linux"), ("/Users/me/out", "macos"), ("C:/Users/me/out", "windows")], -) -def test_ordinary_folders_are_allowed(path: str, platform: str) -> None: - assert managed.root_named(path, platform) is None - - -def _one_line_refusal(capsys: pytest.CaptureFixture[str]) -> str: - err = capsys.readouterr().err - assert "Traceback" not in err and len(err.strip().splitlines()) == 1, err - return err - - -def test_out_naming_a_file_is_refused_cleanly(repo: Path, tmp_path: Path, capsys) -> None: - """Finding 5.""" - afile = tmp_path / "afile" - afile.write_text("x", encoding="utf-8") - assert _linux(repo, afile) == 1 - assert "is not a folder" in _one_line_refusal(capsys) - assert afile.read_text(encoding="utf-8") == "x" - - -@pytest.mark.parametrize("name", ["manifest.yaml", "block-no-verify.py"]) -def test_a_non_utf8_policy_file_is_refused_cleanly(repo: Path, tmp_path: Path, name: str, capsys) -> None: - """Finding 5: a non-UTF-8 manifest or guard.""" - [target] = list((repo / ".agents" / "policies" / "block-no-verify").rglob(name)) - target.write_bytes(target.read_bytes() + b"\xe9\n") - assert _linux(repo, tmp_path / "out", "--policies", "block-no-verify") == 1 - assert "is not UTF-8 text" in _one_line_refusal(capsys) - assert not (tmp_path / "out").exists() - - -def test_a_missing_repo_is_refused_cleanly(tmp_path: Path, capsys) -> None: - """Findings 5, 9: says the folder is missing, not that no policy has a guard.""" - assert _linux(tmp_path / "does-not-exist", tmp_path / "out") == 1 - assert "is not a folder" in _one_line_refusal(capsys) - - -def _plan(out: Path) -> dict: - return json.loads((out / "claude_code" / "deploy.json").read_text(encoding="utf-8")) - - -def test_deploy_json_states_ownership_stop_file_and_lock_effects(repo: Path, tmp_path: Path) -> None: - """Ruling 6 (findings 7, 9): admin-owned bundle with the vendor's words, the repo stop, every --lock side effect.""" - assert _linux(repo, tmp_path / "out", "--lock") == 0 - plan = _plan(tmp_path / "out") - text = json.dumps(plan) - assert any("owned by an administrator and not writable by users" in line for line in plan["requirements"]) - assert "can edit the managed source itself" in text - assert any(".chock/stop.json" in line and "refuse every tool call" in line for line in plan["caveats"]) - lock = plan["notes"][-1] - for effect in ( - "user, project and local hooks", - "hooks in agent frontmatter", - "monitors of other plugins", - "command source are disabled", - "headersHelper", - "status line and file suggestion", - "/goal", - "force-enable", - ): - assert effect in lock, effect - - -@pytest.mark.skipif(sys.platform == "win32" or not shutil.which("git"), reason="POSIX paths, a git repository") -def test_a_repo_stop_file_refuses_matched_calls_as_deploy_json_says(repo: Path, tmp_path: Path) -> None: - """Finding 7: the behaviour the caveat states, proven.""" - _, hook = _bundle_hook(repo, tmp_path) - work = tmp_path / "stopped" - (work / ".chock").mkdir(parents=True) - (work / ".chock" / "stop.json").write_text("not json", encoding="utf-8") - subprocess.run(["git", "init", "-q", str(work)], check=True) - done = _run(hook, "ls", work, _env(tmp_path)) - assert done.returncode == 0 and '"permissionDecision": "deny"' in done.stdout - - def test_no_wording_stronger_than_the_label(repo: Path, tmp_path: Path, capsys) -> None: """Ruling 7 (finding 6): no fail-closed, enforced or cannot-override claim in output, help, data, docstrings or changelog.""" - assert _linux(repo, tmp_path / "out", "--lock") == 0 + assert linux(repo, tmp_path / "out", "--lock") == 0 with pytest.raises(SystemExit): cli.main(["emit", "--help"]) texts = [ @@ -526,9 +267,6 @@ def test_no_wording_stronger_than_the_label(repo: Path, tmp_path: Path, capsys) assert "writes no system path" not in "".join(texts) -DOCS = ROOT / "docs" - - def test_docs_say_what_the_code_does() -> None: """Ruling 3 (finding 3): no compiled managed-settings file, `chock emit` documented, no cannot-override claim.""" pages = {p.name: p.read_text(encoding="utf-8") for p in DOCS.rglob("*.md")} diff --git a/tests/test_managed_emit_runs.py b/tests/test_managed_emit_runs.py new file mode 100644 index 00000000..c8a7719b --- /dev/null +++ b/tests/test_managed_emit_runs.py @@ -0,0 +1,198 @@ +"""`chock emit --managed claude_code`: the emitted hooks run as stated, the guard refuses system paths, refusals are clean.""" + +from __future__ import annotations + +import json +import os +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest +from managed_emit_helpers import ( + bundle_hook, + drop_in, + env_for, + handlers, + linux, + make_repo, + one_line_refusal, + plan_of, + run_hook, +) + +from chock.compile.emitters import managed +from chock.compile.emitters.managed import claude_code + + +@pytest.fixture +def repo(tmp_path: Path) -> Path: + return make_repo(tmp_path) + + +def test_the_bundle_runs_on_its_own(repo: Path, tmp_path: Path) -> None: + """The emitted handler, rooted at the bundle itself, refuses the canary and allows a harmless call.""" + _, hook = bundle_hook(repo, tmp_path) + env = env_for(tmp_path) + denied, allowed = ( + run_hook(hook, "git commit --no-verify -m x", tmp_path, env), + run_hook(hook, "git status", tmp_path, env), + ) + assert denied.returncode == allowed.returncode == 0, denied.stderr + allowed.stderr + assert '"permissionDecision": "deny"' in denied.stdout + assert "deny" not in allowed.stdout + + +@pytest.mark.skipif(sys.platform == "win32" or not shutil.which("git"), reason="POSIX stubs and git config") +def test_user_interpreter_settings_do_not_switch_the_hook_off(repo: Path, tmp_path: Path) -> None: + """Finding 1: chock.python set to a stub (local and global) and stub python3, python, sh, git first on PATH.""" + _, hook = bundle_hook(repo, tmp_path) + stubs = tmp_path / "stubs" + stubs.mkdir() + for name in ("python3", "python", "py", "sh", "git"): + stub = stubs / name + stub.write_text("#!/bin/sh\nprintf chock-python >&9 2>/dev/null; exit 0\n", encoding="utf-8") + stub.chmod(0o755) + work = tmp_path / "work" + work.mkdir() + subprocess.run(["git", "init", "-q", str(work)], check=True) + subprocess.run(["git", "-C", str(work), "config", "chock.python", str(stubs / "python3")], check=True) + global_config = tmp_path / "gitconfig" + global_config.write_text(f"[chock]\n\tpython = {stubs / 'python3'}\n", encoding="utf-8") + env = env_for( + tmp_path, + PATH=f"{stubs}{os.pathsep}{os.environ.get('PATH', '')}", + GIT_CONFIG_GLOBAL=str(global_config), + PYTHONPATH=str(stubs), + PYTHONSTARTUP=str(stubs / "python3"), + ) + for cwd in (work, tmp_path): + done = run_hook(hook, "git commit --no-verify -m x", cwd, env) + assert done.returncode == 0, done.stderr + assert '"permissionDecision": "deny"' in done.stdout + + +@pytest.mark.parametrize(("choice", "code"), [("block", 2), ("allow", 0)]) +def test_a_missing_bundle_file_follows_on_failure(repo: Path, tmp_path: Path, choice: str, code: int) -> None: + """Ruling 2: block refuses (exit 2) and allow lets the call through, each with a note on stderr.""" + bundle, hook = bundle_hook(repo, tmp_path, on_failure=choice) + (bundle / "block-no-verify" / "scripts" / "_chock_runtime.py").unlink() + done = run_hook(hook, "git status", tmp_path, env_for(tmp_path)) + assert done.returncode == code + assert done.stderr.startswith("chock: missing ") and f"--on-failure {choice}" in done.stderr + assert done.stdout == "" + + +@pytest.mark.parametrize(("choice", "code"), [("block", 2), ("allow", 0)]) +def test_too_old_a_python_follows_on_failure(choice: str, code: int) -> None: + """Ruling 2: the version check runs before any runtime code; here the minimum is raised past this Python.""" + program = claude_code.bootstrap(choice).replace("(3, 11,)", "(99,)") + done = subprocess.run( + [sys.executable, "-I", "-c", program, __file__], capture_output=True, text=True, check=False, timeout=60 + ) + assert done.returncode == code + assert "this hook needs Python" in done.stderr and f"--on-failure {choice}" in done.stderr + + +def test_the_stop_gate_never_refuses_for_a_missing_runtime(repo: Path, tmp_path: Path) -> None: + """onFailure has no effect on Stop, and exit 2 there sends Claude back to work: the turn-end gate notes and exits 0.""" + assert linux(repo, tmp_path / "out", "--policies", "scan-secrets") == 0 + stop = [hook for event, hook in handlers(drop_in(tmp_path / "out")) if event == "Stop"] + assert stop and all("sys.exit(0)" in hook["args"][4] for hook in stop) + + +@pytest.mark.parametrize( + ("path", "platform"), + [ + ("/", "linux"), + ("//etc/x", "linux"), + ("/var/lib/x", "linux"), + ("/srv/x", "linux"), + ("/root/x", "linux"), + ("/run/x", "linux"), + ("/sys/x", "linux"), + ("/proc/x", "linux"), + ("/", "macos"), + ("/LIBRARY/Application Support/ClaudeCode/x", "macos"), + ("/system/x", "macos"), + ("/private/etc/x", "macos"), + ("C:/", "windows"), + ("D:\\", "windows"), + ("c:\\program files\\ClaudeCode", "windows"), + ("\\\\?\\C:\\Program Files\\x", "windows"), + ("\\\\.\\C:\\Windows\\x", "windows"), + ("\\\\localhost\\C$\\Program Files\\x", "windows"), + ("\\\\?\\UNC\\server\\c$\\Windows", "windows"), + ], +) +def test_every_system_spelling_is_refused(path: str, platform: str) -> None: + """Finding 4: filesystem roots, the listed trees, case-insensitive spellings, device and admin-share prefixes.""" + assert managed.root_named(path, platform) is not None + + +@pytest.mark.parametrize( + ("path", "platform"), + [("/home/me/out", "linux"), ("/tmp/out", "linux"), ("/Users/me/out", "macos"), ("C:/Users/me/out", "windows")], +) +def test_ordinary_folders_are_allowed(path: str, platform: str) -> None: + assert managed.root_named(path, platform) is None + + +def test_out_naming_a_file_is_refused_cleanly(repo: Path, tmp_path: Path, capsys) -> None: + """Finding 5.""" + afile = tmp_path / "afile" + afile.write_text("x", encoding="utf-8") + assert linux(repo, afile) == 1 + assert "is not a folder" in one_line_refusal(capsys) + assert afile.read_text(encoding="utf-8") == "x" + + +@pytest.mark.parametrize("name", ["manifest.yaml", "block-no-verify.py"]) +def test_a_non_utf8_policy_file_is_refused_cleanly(repo: Path, tmp_path: Path, name: str, capsys) -> None: + """Finding 5: a non-UTF-8 manifest or guard.""" + [target] = list((repo / ".agents" / "policies" / "block-no-verify").rglob(name)) + target.write_bytes(target.read_bytes() + b"\xe9\n") + assert linux(repo, tmp_path / "out", "--policies", "block-no-verify") == 1 + assert "is not UTF-8 text" in one_line_refusal(capsys) + assert not (tmp_path / "out").exists() + + +def test_a_missing_repo_is_refused_cleanly(tmp_path: Path, capsys) -> None: + """Findings 5, 9: says the folder is missing, not that no policy has a guard.""" + assert linux(tmp_path / "does-not-exist", tmp_path / "out") == 1 + assert "is not a folder" in one_line_refusal(capsys) + + +def test_deploy_json_states_ownership_stop_file_and_lock_effects(repo: Path, tmp_path: Path) -> None: + """Ruling 6 (findings 7, 9): admin-owned bundle with the vendor's words, the repo stop, every --lock side effect.""" + assert linux(repo, tmp_path / "out", "--lock") == 0 + plan = plan_of(tmp_path / "out") + text = json.dumps(plan) + assert any("owned by an administrator and not writable by users" in line for line in plan["requirements"]) + assert "can edit the managed source itself" in text + assert any(".chock/stop.json" in line and "refuse every tool call" in line for line in plan["caveats"]) + lock = plan["notes"][-1] + for effect in ( + "user, project and local hooks", + "hooks in agent frontmatter", + "monitors of other plugins", + "command source are disabled", + "headersHelper", + "status line and file suggestion", + "/goal", + "force-enable", + ): + assert effect in lock, effect + + +@pytest.mark.skipif(sys.platform == "win32" or not shutil.which("git"), reason="POSIX paths, a git repository") +def test_a_repo_stop_file_refuses_matched_calls_as_deploy_json_says(repo: Path, tmp_path: Path) -> None: + """Finding 7: the behaviour the caveat states, proven.""" + _, hook = bundle_hook(repo, tmp_path) + work = tmp_path / "stopped" + (work / ".chock").mkdir(parents=True) + (work / ".chock" / "stop.json").write_text("not json", encoding="utf-8") + subprocess.run(["git", "init", "-q", str(work)], check=True) + done = run_hook(hook, "ls", work, env_for(tmp_path)) + assert done.returncode == 0 and '"permissionDecision": "deny"' in done.stdout diff --git a/tests/test_surface_doc_matches_code.py b/tests/test_surface_doc_matches_code.py index 31c7edc8..c8fa2784 100644 --- a/tests/test_surface_doc_matches_code.py +++ b/tests/test_surface_doc_matches_code.py @@ -148,7 +148,7 @@ def test_readme_enforces_column_matches_the_code() -> None: #: What README's one omission paragraph owes each surface it drops. Ordered by where the #: names appear in that paragraph, which `_reason_for` slices on. README_OMITTED = { - Surface.MANAGED_SETTING: "compiled but not installed", + Surface.MANAGED_SETTING: "not installed by chock", Surface.GATEWAY: "modelled but not yet emitted", Surface.MCP_GATEWAY: "per-client witness", Surface.STOP: "worth no grade of its own", @@ -186,6 +186,7 @@ def test_readme_says_why_the_absent_surfaces_are_absent() -> None: assert not crediting, f"{surface.value} now credits {crediting}; the README owes it a column" assert Surface.MANAGED_SETTING not in INSTALLED_SURFACES, "an installer landed; 'not installed' is now false" + assert Surface.MANAGED_SETTING not in EMITTERS, "the compiler writes it again; the README wording is stale" assert Surface.GATEWAY not in EMITTERS, "gateway emits now; 'not yet emitted' is now false" supporting = [a for a, s in SURFACE_AGENTS.items() if Surface.MCP_GATEWAY in s] assert not supporting, f"mcp-gateway is now supported by {supporting}; the per-client wording is stale" @@ -262,7 +263,10 @@ def test_managed_setting_is_disclosed_as_not_installed() -> None: from chock.compile.surfaces import INSTALLED_SURFACES assert Surface.MANAGED_SETTING not in INSTALLED_SURFACES, "an installer landed; drop the caveat" - assert "compiled but not installed" in DOC.read_text(encoding="utf-8") + from chock.compile.compiler import EMITTERS + + assert Surface.MANAGED_SETTING not in EMITTERS, "the compiler writes it again; 'neither compiled' is now false" + assert "neither compiled nor installed" in DOC.read_text(encoding="utf-8") def test_ci_gate_is_disclosed_as_needing_its_installer() -> None: From 365f7ed449a6e625f92834e4093e61c254ab940a Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:55:09 +0000 Subject: [PATCH 19/57] test(floor): split the floor attack tests to fit the 300-line budget Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- tests/test_floor_attacks2.py | 38 ------ tests/test_floor_attacks3.py | 206 +------------------------------ tests/test_floor_attacks4.py | 232 +++++++++++++++++++++++++++++++++++ tests/test_floor_check.py | 39 ++++++ 4 files changed, 272 insertions(+), 243 deletions(-) create mode 100644 tests/test_floor_attacks4.py diff --git a/tests/test_floor_attacks2.py b/tests/test_floor_attacks2.py index a087d300..84276360 100644 --- a/tests/test_floor_attacks2.py +++ b/tests/test_floor_attacks2.py @@ -7,9 +7,6 @@ from __future__ import annotations import json -import os -import subprocess -import sys from pathlib import Path import pytest @@ -281,38 +278,3 @@ def test_a_gate_naming_a_file_outside_the_repository_is_undecided(tmp_path, caps code, out = run_floor(repo, capsys) assert code == 2, out assert "not a path inside the repository" in out - - -# ---------------- refs ---------------- - - -def test_head_not_descendant_of_base_is_undecided(tmp_path, capsys): - repo = base_repo(tmp_path) - git(repo, "checkout", "-q", "main") - write(repo, "src/app.py", "VALUE = 9\n") - commit(repo, "main moves on") - git(repo, "checkout", "-q", "pr") - code, out = run_floor(repo, capsys, "--head", "pr") - assert code == 2, out - assert "is not an ancestor of head" in out - - -def _cli(repo: Path, *args: str) -> int: - env = {"PATH": os.environ.get("PATH", ""), "PYTHONPATH": str(SRC), "HOME": str(repo.parent)} - code = "import sys; from chock.lifecycle import check_main; sys.exit(check_main(sys.argv[1:]))" - proc = subprocess.run( - [sys.executable, "-c", code, "--repo", str(repo), *args], capture_output=True, text=True, env=env, check=False - ) - return proc.returncode - - -def test_ref_arguments(tmp_path): - repo = base_repo(tmp_path) - write(repo, "src/config.txt", "key = FLOOR-TOKEN-4242\n") - commit(repo, "pr") - assert _cli(repo, "--floor", "--base", "main", "--head", "pr") == 1 - assert _cli(repo, "--floor", "--base", "no-such-ref", "--head", "pr") == 2 - assert _cli(repo, "--floor", "--base=--all", "--head", "pr") == 2 - assert _cli(repo, "--floor", "--base=--since=2000-01-01", "--head", "pr") == 2 - assert _cli(repo, "--floor", "--base", "main", f"--head=--output={tmp_path / 'x'}") == 2 - assert _cli(repo, "--floor", "--base", "pr", "--head", "pr") == 0 diff --git a/tests/test_floor_attacks3.py b/tests/test_floor_attacks3.py index f6d45878..ec5f8320 100644 --- a/tests/test_floor_attacks3.py +++ b/tests/test_floor_attacks3.py @@ -1,15 +1,12 @@ """Regression tests for the second adversarial review of `chock check --floor` (W2-A1r, every proof ported). Each asserts the exit W2-A1v2 owes: a control-plane attack exits 1 under `refuse` and 0, with the path -listed, under `listed`; content, waiver and history attacks exit 1 or 2; correct pull requests exit 0. +listed, under `listed`. Part 2 (refs, encodings, waivers, history, correct PRs) is test_floor_attacks4.py. """ from __future__ import annotations import json -import os -import subprocess -import time from pathlib import Path import pytest @@ -17,8 +14,6 @@ CLAUDE, FLOOR_TOKEN, LOCK, - TEST_FILE, - TEST_PRAGMA, WORKFLOW, base_repo, commit, @@ -256,202 +251,3 @@ def test_criss_cross_drop_guardrail(tmp_path, capsys, branch_first): code, out = run_floor(repo, capsys) assert code == (1 if landed else 0), out assert (f"\n {SETTINGS}\n" in out) == landed, out - - -# ---------- shallow clone and unrelated history ---------- - - -def test_shallow_clone_is_undecided(tmp_path, capsys): - repo = base_repo(tmp_path) - write(repo, "src/app.py", "VALUE = 2\n") - commit(repo, "pr1") - write(repo, "src/app.py", "VALUE = 3\n") - commit(repo, "pr2") - clone = tmp_path / "clone" - subprocess.run( - ["git", "clone", "-q", "--depth", "1", "--no-single-branch", f"file://{repo}", str(clone)], check=True - ) - code, out = run_floor(clone, capsys, "--base", "origin/main", "--head", "origin/pr") - assert code == 2, out - - -def test_unrelated_history_is_undecided(tmp_path, capsys): - repo = base_repo(tmp_path) - git(repo, "checkout", "-q", "--orphan", "alien") - git(repo, "rm", "-rq", "--cached", ".") - write(repo, "x.txt", f"{SECRET}\n") - git(repo, "add", "x.txt") - git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "commit", "-q", "-m", "alien") - code, out = run_floor(repo, capsys, "--head", "alien") - assert code == 2, out - - -# ---------- M-f, X6: encodings and line counting in the history pass ---------- - - -def test_utf16_secret(tmp_path, capsys): - repo = base_repo(tmp_path) - (repo / "src/notes.txt").write_bytes(f"key = {SECRET}\n".encode("utf-16")) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "floor-token [history] (block)" in out - - -def test_utf16_secret_shifted_by_one_byte(tmp_path, capsys): - repo = base_repo(tmp_path) - (repo / "src/notes.bin").write_bytes(b"\x01" + f"key = {SECRET}\n".encode("utf-16-le")) - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - - -def test_secret_as_symlink_target_added_then_removed(tmp_path, capsys): - repo = base_repo(tmp_path) - os.symlink(SECRET, repo / "link") - commit(repo, "add link") - (repo / "link").unlink() - commit(repo, "remove link") - code, out = run_floor(repo, capsys) - assert code == 1, out - - -@pytest.mark.parametrize("prefix", [b"\x85\x85", b"\xff\xff", b"\x1c\x1d\x1e"]) -def test_history_line_number_mismatch_drops_no_secret(tmp_path, capsys, prefix): - """U+0085 and the separators splitlines() knows are not line breaks to the floor: one reader, one count.""" - repo = base_repo(tmp_path) - (repo / "src/app.py").write_bytes(prefix + b"FLOOR-TOKEN-777\nVALUE = 1\nVALUE = 1\n") - commit(repo, "pr: add (hidden)") - write(repo, "src/app.py", "VALUE = 1\n") - commit(repo, "pr: restore") - code, out = run_floor(repo, capsys) - assert code == 1, out - assert "src/app.py:1 (content-pattern)" in out - - -# ---------- M-a, M-b: waivers on removed lines, renames hiding deletions ---------- - - -def _base_then_pr(repo: Path, base_text: str, head_text: str) -> None: - git(repo, "checkout", "-q", "main") - write(repo, "tests/test_core.py", base_text) - commit(repo, "base") - git(repo, "checkout", "-q", "-B", "pr") - write(repo, "tests/test_core.py", head_text) - commit(repo, "pr") - - -def test_removed_assertion_carrying_base_pragma_is_not_waived(tmp_path, capsys): - repo = base_repo(tmp_path) - pragma = TEST_FILE.replace("assert 2 * 2 == 4", f"assert 2 * 2 == 4 # {TEST_PRAGMA}") - _base_then_pr(repo, pragma, "def test_a():\n assert 1 + 1 == 2\n") - code, out = run_floor(repo, capsys) - assert code == 1, out - - -def test_removed_pragma_assertions_two_lines(tmp_path, capsys): - repo = base_repo(tmp_path) - body = TEST_FILE.replace("== 2\n", f"== 2 # {TEST_PRAGMA}\n").replace("== 4\n", f"== 4 # {TEST_PRAGMA}\n") - _base_then_pr(repo, body, "def test_a():\n pass\n") - code, out = run_floor(repo, capsys) - assert code == 1, out - - -@pytest.mark.parametrize("how", ["rename", "delete", "weaken"]) -def test_test_file_moved_deleted_or_weakened_is_refused(tmp_path, capsys, how): - repo = base_repo(tmp_path) - if how == "rename": - git(repo, "mv", "tests/test_core.py", "attic_core.py") - elif how == "delete": - git(repo, "rm", "-q", "tests/test_core.py") - else: - write(repo, "tests/test_core.py", "def test_a():\n assert 1 + 1 == 2\n") - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 1, out - - -# ---------- first adoption ---------- - - -def test_first_adoption_output(tmp_path, capsys): - repo = tmp_path / "repo" - repo.mkdir() - git(repo, "init", "-q", "-b", "main") - write(repo, "src/app.py", "V = 1\n") - commit(repo, "base") - git(repo, "checkout", "-q", "-b", "pr") - write(repo, "src/leak.py", "K = 'FLOOR-TOKEN-1'\n") - commit(repo, "pr") - code, out = run_floor(repo, capsys) - assert code == 0, out - assert "base has no chock gates; nothing was judged" in out - - -# ---------- correct pull requests and wall time ---------- - - -@pytest.mark.parametrize("megabytes", [8, 40]) -def test_large_correct_blob(tmp_path, capsys, megabytes): - repo = base_repo(tmp_path) - line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0123456789\n" - (repo / "data.csv").write_text(line * (megabytes * 1024 * 1024 // len(line))) - commit(repo, "pr: data") - started = time.monotonic() - code, out = run_floor(repo, capsys) - print(f"{megabytes} MB: exit {code} in {time.monotonic() - started:.1f}s") - assert code == 0, out - - -def test_ok_merge_base_into_branch_and_revert(tmp_path, capsys): - repo = base_repo(tmp_path) - write(repo, "src/feature.py", "F = 1\n") - commit(repo, "pr: feature") - git(repo, "checkout", "-q", "main") - write(repo, "src/other.py", "O = 1\n") - commit(repo, "main moves on") - git(repo, "checkout", "-q", "pr") - _merge(repo, "main") - write(repo, "src/feature.py", "F = 2\n") - commit(repo, "pr: change") - git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "revert", "--no-edit", "HEAD") - code, out = run_floor(repo, capsys) - assert code == 0, out - - -def test_policy_added_with_lock_entry_is_a_control_plane_change(tmp_path, capsys): - """By design under `refuse` (a privileged merge); under `listed` its own gate never judges its own source.""" - - def change(repo): - lock = json.loads(json.dumps(LOCK)) - lock["packs"].append({"id": "no-todo"}) - write(repo, "chock.lock", lock) - gate(repo, "no-todo", dict(FLOOR_TOKEN, message="no TODO", params={"content_pattern": "TODO-NEVER"})) - write(repo, ".agents/policies/no-todo/policy.md", "# no-todo\n") - commit(repo, "pr: add policy") - - control_plane_change(tmp_path, capsys, change, "chock.lock", ".agents/policies/no-todo/policy.md") - - -def test_docs_quoting_a_policy_example_in_its_own_folder(tmp_path, capsys): - rel = ".agents/policies/floor-token/README.md" - - def change(repo): - write(repo, rel, "Example that is refused: FLOOR-TOKEN-1\n") - commit(repo, "pr: docs") - - control_plane_change(tmp_path, capsys, change, rel) - - -def test_ok_rename_file_with_base_waiver(tmp_path, capsys): - """A rename is a delete and an add (V1); the fixture's probe gate refuses every delete, so it goes first.""" - repo = base_repo(tmp_path) - git(repo, "checkout", "-q", "main") - git(repo, "rm", "-q", "-r", ".chock/compiled/probe") - write(repo, "src/legacy.py", "X = 1\n") - commit(repo, "base") - git(repo, "checkout", "-q", "-B", "pr") - git(repo, "mv", "src/legacy.py", "src/legacy2.py") - commit(repo, "pr: rename") - code, out = run_floor(repo, capsys) - assert code == 0, out diff --git a/tests/test_floor_attacks4.py b/tests/test_floor_attacks4.py new file mode 100644 index 00000000..684632b5 --- /dev/null +++ b/tests/test_floor_attacks4.py @@ -0,0 +1,232 @@ +"""Second adversarial review of `chock check --floor`, part 2: refs, encodings, waivers, history, correct PRs. + +Each asserts the exit W2-A1v2 owes. Control-plane proofs of the same review are in test_floor_attacks3.py. +""" + +from __future__ import annotations + +import json +import os +import subprocess +import time +from pathlib import Path + +import pytest +from floor_support import ( + FLOOR_TOKEN, + LOCK, + TEST_FILE, + TEST_PRAGMA, + base_repo, + commit, + control_plane_change, + gate, + git, + run_floor, + write, +) + +SECRET = "FLOOR-TOKEN-424242" + + +def _merge(repo: Path, branch: str = "pr") -> None: + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "merge", "-q", "--no-ff", "--no-edit", branch) + + +# ---------- shallow clone and unrelated history ---------- + + +def test_shallow_clone_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr1") + write(repo, "src/app.py", "VALUE = 3\n") + commit(repo, "pr2") + clone = tmp_path / "clone" + subprocess.run( + ["git", "clone", "-q", "--depth", "1", "--no-single-branch", f"file://{repo}", str(clone)], check=True + ) + code, out = run_floor(clone, capsys, "--base", "origin/main", "--head", "origin/pr") + assert code == 2, out + + +def test_unrelated_history_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "--orphan", "alien") + git(repo, "rm", "-rq", "--cached", ".") + write(repo, "x.txt", f"{SECRET}\n") + git(repo, "add", "x.txt") + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "commit", "-q", "-m", "alien") + code, out = run_floor(repo, capsys, "--head", "alien") + assert code == 2, out + + +# ---------- M-f, X6: encodings and line counting in the history pass ---------- + + +def test_utf16_secret(tmp_path, capsys): + repo = base_repo(tmp_path) + (repo / "src/notes.txt").write_bytes(f"key = {SECRET}\n".encode("utf-16")) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "floor-token [history] (block)" in out + + +def test_utf16_secret_shifted_by_one_byte(tmp_path, capsys): + repo = base_repo(tmp_path) + (repo / "src/notes.bin").write_bytes(b"\x01" + f"key = {SECRET}\n".encode("utf-16-le")) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_secret_as_symlink_target_added_then_removed(tmp_path, capsys): + repo = base_repo(tmp_path) + os.symlink(SECRET, repo / "link") + commit(repo, "add link") + (repo / "link").unlink() + commit(repo, "remove link") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +@pytest.mark.parametrize("prefix", [b"\x85\x85", b"\xff\xff", b"\x1c\x1d\x1e"]) +def test_history_line_number_mismatch_drops_no_secret(tmp_path, capsys, prefix): + """U+0085 and the separators splitlines() knows are not line breaks to the floor: one reader, one count.""" + repo = base_repo(tmp_path) + (repo / "src/app.py").write_bytes(prefix + b"FLOOR-TOKEN-777\nVALUE = 1\nVALUE = 1\n") + commit(repo, "pr: add (hidden)") + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "pr: restore") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "src/app.py:1 (content-pattern)" in out + + +# ---------- M-a, M-b: waivers on removed lines, renames hiding deletions ---------- + + +def _base_then_pr(repo: Path, base_text: str, head_text: str) -> None: + git(repo, "checkout", "-q", "main") + write(repo, "tests/test_core.py", base_text) + commit(repo, "base") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "tests/test_core.py", head_text) + commit(repo, "pr") + + +def test_removed_assertion_carrying_base_pragma_is_not_waived(tmp_path, capsys): + repo = base_repo(tmp_path) + pragma = TEST_FILE.replace("assert 2 * 2 == 4", f"assert 2 * 2 == 4 # {TEST_PRAGMA}") + _base_then_pr(repo, pragma, "def test_a():\n assert 1 + 1 == 2\n") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_removed_pragma_assertions_two_lines(tmp_path, capsys): + repo = base_repo(tmp_path) + body = TEST_FILE.replace("== 2\n", f"== 2 # {TEST_PRAGMA}\n").replace("== 4\n", f"== 4 # {TEST_PRAGMA}\n") + _base_then_pr(repo, body, "def test_a():\n pass\n") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +@pytest.mark.parametrize("how", ["rename", "delete", "weaken"]) +def test_test_file_moved_deleted_or_weakened_is_refused(tmp_path, capsys, how): + repo = base_repo(tmp_path) + if how == "rename": + git(repo, "mv", "tests/test_core.py", "attic_core.py") + elif how == "delete": + git(repo, "rm", "-q", "tests/test_core.py") + else: + write(repo, "tests/test_core.py", "def test_a():\n assert 1 + 1 == 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +# ---------- first adoption ---------- + + +def test_first_adoption_output(tmp_path, capsys): + repo = tmp_path / "repo" + repo.mkdir() + git(repo, "init", "-q", "-b", "main") + write(repo, "src/app.py", "V = 1\n") + commit(repo, "base") + git(repo, "checkout", "-q", "-b", "pr") + write(repo, "src/leak.py", "K = 'FLOOR-TOKEN-1'\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "base has no chock gates; nothing was judged" in out + + +# ---------- correct pull requests and wall time ---------- + + +@pytest.mark.parametrize("megabytes", [8, 40]) +def test_large_correct_blob(tmp_path, capsys, megabytes): + repo = base_repo(tmp_path) + line = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0123456789\n" + (repo / "data.csv").write_text(line * (megabytes * 1024 * 1024 // len(line))) + commit(repo, "pr: data") + started = time.monotonic() + code, out = run_floor(repo, capsys) + print(f"{megabytes} MB: exit {code} in {time.monotonic() - started:.1f}s") + assert code == 0, out + + +def test_ok_merge_base_into_branch_and_revert(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/feature.py", "F = 1\n") + commit(repo, "pr: feature") + git(repo, "checkout", "-q", "main") + write(repo, "src/other.py", "O = 1\n") + commit(repo, "main moves on") + git(repo, "checkout", "-q", "pr") + _merge(repo, "main") + write(repo, "src/feature.py", "F = 2\n") + commit(repo, "pr: change") + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "revert", "--no-edit", "HEAD") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_policy_added_with_lock_entry_is_a_control_plane_change(tmp_path, capsys): + """By design under `refuse` (a privileged merge); under `listed` its own gate never judges its own source.""" + + def change(repo): + lock = json.loads(json.dumps(LOCK)) + lock["packs"].append({"id": "no-todo"}) + write(repo, "chock.lock", lock) + gate(repo, "no-todo", dict(FLOOR_TOKEN, message="no TODO", params={"content_pattern": "TODO-NEVER"})) + write(repo, ".agents/policies/no-todo/policy.md", "# no-todo\n") + commit(repo, "pr: add policy") + + control_plane_change(tmp_path, capsys, change, "chock.lock", ".agents/policies/no-todo/policy.md") + + +def test_docs_quoting_a_policy_example_in_its_own_folder(tmp_path, capsys): + rel = ".agents/policies/floor-token/README.md" + + def change(repo): + write(repo, rel, "Example that is refused: FLOOR-TOKEN-1\n") + commit(repo, "pr: docs") + + control_plane_change(tmp_path, capsys, change, rel) + + +def test_ok_rename_file_with_base_waiver(tmp_path, capsys): + """A rename is a delete and an add (V1); the fixture's probe gate refuses every delete, so it goes first.""" + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + git(repo, "rm", "-q", "-r", ".chock/compiled/probe") + write(repo, "src/legacy.py", "X = 1\n") + commit(repo, "base") + git(repo, "checkout", "-q", "-B", "pr") + git(repo, "mv", "src/legacy.py", "src/legacy2.py") + commit(repo, "pr: rename") + code, out = run_floor(repo, capsys) + assert code == 0, out diff --git a/tests/test_floor_check.py b/tests/test_floor_check.py index 23adae55..4ee3b842 100644 --- a/tests/test_floor_check.py +++ b/tests/test_floor_check.py @@ -2,6 +2,11 @@ from __future__ import annotations +import os +import subprocess +import sys +from pathlib import Path + import pytest from floor_support import ( FLOOR_TOKEN, @@ -19,6 +24,8 @@ from chock.lifecycle import check_main +SRC = Path(__file__).resolve().parents[1] / "src" + NO_OP = {"kind": "content_regex", "on": ["commit"], "action": "warn", "params": {"content_pattern": "(?!)"}} @@ -182,3 +189,35 @@ def test_no_coverage_tier_is_credited_for_the_floor() -> None: from chock.compile import surfaces assert not [name for name in vars(surfaces) if "floor" in name.lower()] + + +def test_head_not_descendant_of_base_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, "src/app.py", "VALUE = 9\n") + commit(repo, "main moves on") + git(repo, "checkout", "-q", "pr") + code, out = run_floor(repo, capsys, "--head", "pr") + assert code == 2, out + assert "is not an ancestor of head" in out + + +def _cli(repo: Path, *args: str) -> int: + env = {"PATH": os.environ.get("PATH", ""), "PYTHONPATH": str(SRC), "HOME": str(repo.parent)} + code = "import sys; from chock.lifecycle import check_main; sys.exit(check_main(sys.argv[1:]))" + proc = subprocess.run( + [sys.executable, "-c", code, "--repo", str(repo), *args], capture_output=True, text=True, env=env, check=False + ) + return proc.returncode + + +def test_ref_arguments(tmp_path): + repo = base_repo(tmp_path) + write(repo, "src/config.txt", "key = FLOOR-TOKEN-4242\n") + commit(repo, "pr") + assert _cli(repo, "--floor", "--base", "main", "--head", "pr") == 1 + assert _cli(repo, "--floor", "--base", "no-such-ref", "--head", "pr") == 2 + assert _cli(repo, "--floor", "--base=--all", "--head", "pr") == 2 + assert _cli(repo, "--floor", "--base=--since=2000-01-01", "--head", "pr") == 2 + assert _cli(repo, "--floor", "--base", "main", f"--head=--output={tmp_path / 'x'}") == 2 + assert _cli(repo, "--floor", "--base", "pr", "--head", "pr") == 0 From ee4ccc2f4bc5e897b34870fbebdd55e31e11f936 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:16:55 +0000 Subject: [PATCH 20/57] fix: isolate every child interpreter the hook runtime starts; deploy.json says what happens Ruling 9: a Python guard, the gate runner and a script gate now start under -I -B -X utf8 through budget.isolated_child, with the script's own folder put on sys.path explicitly and every PYTHON* variable removed from the child's environment. A hostile PYTHONPATH, PYTHONHOME, PYTHONPLATLIBDIR or PYTHONSAFEPATH no longer passes the canary or turns its refusal into an ask. Rulings 10-12: deploy.json gains stop_files, client_versions (quoted, dated) and not_covered; the managed bootstrap checks every bundled file, so a missing guard or chock_shellparse follows --on-failure. Ruling 13: odd deploy-root and python paths, and a normalisation table. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2b1-managed-emit-claude.md | 10 + .../emitters/data/managed_bootstrap.txt | 2 +- .../compile/emitters/managed/claude_code.py | 24 ++- src/chock/data/managed_channels.json | 49 ++++- src/chock/gate/budget.py | 21 ++ src/chock/gate/guard_runner.py | 14 +- src/chock/gate/tool_call_gate.py | 7 +- src/chock/gate/write_gate.py | 5 +- .../managed_emit/block-no-verify.json | 2 +- tests/fixtures/managed_emit/scan-secrets.json | 4 +- tests/test_managed_emit_deploy_text.py | 186 ++++++++++++++++++ tests/test_managed_emit_isolation.py | 104 ++++++++++ 12 files changed, 405 insertions(+), 23 deletions(-) create mode 100644 tests/test_managed_emit_deploy_text.py create mode 100644 tests/test_managed_emit_isolation.py diff --git a/changelog.d/w2b1-managed-emit-claude.md b/changelog.d/w2b1-managed-emit-claude.md index bd941a10..8b2be99a 100644 --- a/changelog.d/w2b1-managed-emit-claude.md +++ b/changelog.d/w2b1-managed-emit-claude.md @@ -16,3 +16,13 @@ the file. Native `permissions.deny` rules are not emitted yet. - `chock compile` no longer writes `managed-setting/managed-settings.json`: that file used a shape Claude Code does not read. +- Every child interpreter a hook runtime starts (a Python guard, the gate runner, a script gate) now + runs isolated (`-I`), with its own folder put on the import path explicitly and every `PYTHON*` + variable removed from its environment, on every route. A hostile `PYTHONPATH`, `PYTHONHOME`, + `PYTHONPLATLIBDIR` or `PYTHONSAFEPATH` no longer lets a refused command through or turns a refusal + into a prompt. +- `chock emit --managed claude_code`: the bootstrap now checks every bundled file, so a missing guard + or `chock_shellparse` follows `--on-failure` like any other missing file. `deploy.json` gains + `stop_files` (the repository's `.chock/stop.json` and the `~/.chock/stops/` files, each with its + effect), `client_versions` (2.1.295 for `onFailure`, 2.1.314 for Monitor commands, each quoted with + the date read) and `not_covered`. diff --git a/src/chock/compile/emitters/data/managed_bootstrap.txt b/src/chock/compile/emitters/data/managed_bootstrap.txt index 4c56ada1..33b46bbb 100644 --- a/src/chock/compile/emitters/data/managed_bootstrap.txt +++ b/src/chock/compile/emitters/data/managed_bootstrap.txt @@ -1 +1 @@ -import sys,os;r=sys.argv[1];m=[p for p in (r,os.path.join(os.path.dirname(r),'_chock_runtime.py')) if not os.path.isfile(p)];o=sys.version_info<(__MIN_PYTHON__,);w='chock: '+('this hook needs Python __MIN_PYTHON_TEXT__+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; __OUTCOME__\n';(o or m) and (sys.stderr.write(w),sys.exit(__EXIT__));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__') +import sys,os;r=sys.argv[1];m=[p for p in (r,*(os.path.join(os.path.dirname(r),*f.split('/')) for f in __NEEDED__)) if not os.path.isfile(p)];o=sys.version_info<(__MIN_PYTHON__,);w='chock: '+('this hook needs Python __MIN_PYTHON_TEXT__+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; __OUTCOME__\n';(o or m) and (sys.stderr.write(w),sys.exit(__EXIT__));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__') diff --git a/src/chock/compile/emitters/managed/claude_code.py b/src/chock/compile/emitters/managed/claude_code.py index 5e591c0f..423d517a 100644 --- a/src/chock/compile/emitters/managed/claude_code.py +++ b/src/chock/compile/emitters/managed/claude_code.py @@ -45,10 +45,11 @@ _HALF_FLAGS = {"guard": "--guard", "gate": "--gate"} -def bootstrap(on_failure: str) -> str: - """The `-c` program that starts a bundled runtime, or, when it or Python 3.11+ is missing, says so and exits per `on_failure`.""" +def bootstrap(on_failure: str, needed: tuple[str, ...] = ()) -> str: + """The `-c` program that starts a bundled runtime, or, when it, a `needed` file beside it (relative, `/`-separated) + or Python 3.11+ is missing, says so and exits per `on_failure`.""" outcome, code = _OUTCOME[on_failure] - return _BOOTSTRAP.replace("__OUTCOME__", outcome).replace("__EXIT__", str(code)) + return _BOOTSTRAP.replace("__OUTCOME__", outcome).replace("__EXIT__", str(code)).replace("__NEEDED__", repr(needed)) def _argv(command: str) -> list[str]: @@ -59,8 +60,11 @@ def _argv(command: str) -> list[str]: return shlex.split(command.removeprefix(_PLUGIN_PREFIX)) -def _exec_hook(hook: dict[str, Any], event: str, root: str, python: str, on_failure: str) -> dict[str, Any]: - """`hook` in exec form: `python` runs the bootstrap on the runtime rooted at `root`; onFailure only where it acts.""" +def _exec_hook(hook: dict[str, Any], event: str, root: str, options: dict[str, Any]) -> dict[str, Any]: + """`hook` in exec form: `python` runs the bootstrap on the runtime rooted at `root`; onFailure only where it acts. + + `options`: python, on_failure, needed (every bundled file beside the runtime, which the bootstrap checks).""" + python, on_failure = options["python"], options["on_failure"] argv = [arg.replace(ROOT_TOKEN, root) for arg in _argv(hook["command"])] if any("${" in arg for arg in argv): msg = f"{argv!r} still names a client token after rooting it at {root}" @@ -69,7 +73,7 @@ def _exec_hook(hook: dict[str, Any], event: str, root: str, python: str, on_fail entry = { "type": hook["type"], "command": python, - "args": [*_FACTS["python_flags"], bootstrap(on_failure if blocks else ALLOW), *argv], + "args": [*_FACTS["python_flags"], bootstrap(on_failure if blocks else ALLOW, options["needed"]), *argv], } entry.update({key: value for key, value in hook.items() if key not in {"type", "command"}}) if blocks: @@ -112,11 +116,12 @@ def build( continue hooks, scripts = parts flags = set() + needed = tuple(sorted(rel.relative_to(SCRIPTS_DIR).as_posix() for rel in scripts)) + hook_options = {**options, "needed": needed} for event, groups in hooks.items(): for group in (group for group in groups if not _is_stop_check(group)): rooted = [ - _exec_hook(hook, event, f"{options['root']}/{policy_id}", options["python"], options["on_failure"]) - for hook in group["hooks"] + _exec_hook(hook, event, f"{options['root']}/{policy_id}", hook_options) for hook in group["hooks"] ] events.setdefault(event, []).append({**group, "hooks": rooted}) flags.update(arg for hook in rooted for arg in hook["args"]) @@ -162,6 +167,9 @@ def deploy_plan(platform: str, options: dict[str, Any], tools: list[str]) -> dic "undo": [f"delete {step['to']}" for step in place], "requirements": [line.replace("__PYTHON__", python) for line in _FACTS["requirements"]], "caveats": _FACTS["caveats"], + "stop_files": _FACTS["stop_files"], + "client_versions": _FACTS["client_versions"], + "not_covered": _FACTS["not_covered"], "never_emitted": _FACTS["never_emitted"], "notes": [_FACTS["never_note"], *([_FACTS["lock_note"]] if lock else [])], "sources": _FACTS["sources"], diff --git a/src/chock/data/managed_channels.json b/src/chock/data/managed_channels.json index 9be65bf8..98bdd19b 100644 --- a/src/chock/data/managed_channels.json +++ b/src/chock/data/managed_channels.json @@ -89,7 +89,8 @@ }, "requirements": [ "Every machine needs Python 3.11 or later at __PYTHON__ exactly: the hook commands start that file directly (exec form, no shell), so neither PATH, git config nor a shell picks the interpreter.", - "onFailure needs Claude Code 2.1.295 or later; an older client lets a crashed or timed-out hook through.", + "onFailure needs Claude Code 2.1.295 or later (client_versions); an older client lets a crashed or timed-out hook through.", + "Monitor commands are checked only on Claude Code 2.1.314 or later, and WebSocket watches never (client_versions, not_covered).", "The bundle must sit at deploy_root exactly: the hook commands name it by absolute path.", "The bundle folder and __PYTHON__ must be owned by an administrator and not writable by users: whoever can write them can replace the guard. The vendor docs state no ownership rule for the files a managed hook runs; on who can change the managed source they say \"a developer who is an administrator on the machine can edit the managed source itself\"." ], @@ -99,13 +100,57 @@ "By default (managedSourcesBehavior \"first-wins\") a machine that also receives server-managed or MDM policy skips the managed settings files without a warning; /status names the source in use.", "onFailure has no effect on Stop hooks, so the turn-end gate lets the turn end when it cannot run, whatever --on-failure says.", "A repository holding .chock/stop.json (valid or not) makes these hooks refuse every tool call they match while Claude Code works in that repository: the bundled runtime honours chock's repository stop.", + "Files under the user's home folder can also stop these hooks: ~/.chock/stops/_everywhere.json (chock stop --everywhere) refuses every tool call they match, in every repository; the other files there refuse calls in their repository; an unreadable ~/.chock/stops refuses every call. The user can create these files, and so can the agent with any tool these hooks do not match (stop_files).", "The bundled runtime runs git from PATH for repository facts (the stop files, the turn-end gate's changed files); the guards' verdicts on a command do not depend on it.", "Hooks write their decision log under the user's home folder (~/.chock/log).", "A file in managed-settings.d that is not valid JSON stops Claude Code from starting.", "The labels are vendor-docs evidence, unwitnessed, best-effort: nobody has watched this file refuse a call yet." ], "lock_note": "--lock adds allowManagedHooksOnly. The vendor docs list what it changes on the machine: only managed hooks, Agent SDK hooks and hooks of plugins the managed settings force-enable run; user, project and local hooks, hooks and mods of other installed plugins (chock's own project hooks and plugins included) and hooks in agent frontmatter are blocked; monitors of other plugins do not start; plugins with a command source are disabled and marketplace headersHelper commands are blocked unless disableCommandPluginSources is set to false; the status line and file suggestion are read from managed settings only; the /goal command cannot run.", - "never_note": "allowManagedPermissionRulesOnly is never emitted: it would discard every user and project permission rule." + "never_note": "allowManagedPermissionRulesOnly is never emitted: it would discard every user and project permission rule.", + "client_versions": [ + { + "needed_for": "onFailure (--on-failure block refuses a call when the hook cannot start, crashes or times out)", + "min_client": "2.1.295", + "quote": "Requires Claude Code v2.1.295 or later", + "source": "https://code.claude.com/docs/en/hooks", + "read": "2026-11-13" + }, + { + "needed_for": "the Bash|PowerShell matcher also checking commands Claude starts with the Monitor tool", + "min_client": "2.1.314", + "quote": "A `PreToolUse` or `PermissionRequest` hook with a `Bash` matcher also runs on shell commands Claude starts with the Monitor tool. Requires Claude Code v2.1.314 or later.", + "source": "https://code.claude.com/docs/en/hooks", + "read": "2026-11-13" + } + ], + "not_covered": [ + "Monitor commands on a client older than 2.1.314: the hook never sees them.", + "WebSocket watches started with the Monitor tool, on every client version (vendor docs, read 2026-11-13: \"A hook with a `Bash` matcher doesn't run for a WebSocket watch\").", + "MCP tools and any other tool that no emitted matcher names (the shell matcher is Bash|PowerShell), even when it runs a command." + ], + "stop_files": [ + { + "path": ".chock/stop.json in any repository the call works in or names", + "written_by": "chock stop", + "effect": "every tool call these hooks match is refused; a stop file that is not valid JSON refuses as well." + }, + { + "path": "~/.chock/stops/_everywhere.json in the user's home folder", + "written_by": "chock stop --everywhere", + "effect": "every tool call these hooks match is refused, whatever the repository or session; any content, {} included, counts." + }, + { + "path": "~/.chock/stops/.json in the user's home folder", + "written_by": "chock stop (its machine registry entry)", + "effect": "tool calls that work in or name that repository are refused, and so is every call while Claude Code names no project folder." + }, + { + "path": "~/.chock/stops itself", + "written_by": "chock stop", + "effect": "if it exists but cannot be listed, or an entry in it cannot be read, every tool call these hooks match is refused." + } + ] } } } diff --git a/src/chock/gate/budget.py b/src/chock/gate/budget.py index e821fd43..332b11ed 100644 --- a/src/chock/gate/budget.py +++ b/src/chock/gate/budget.py @@ -30,6 +30,14 @@ #: Added to the environment of every policy script, so running a gate never writes bytecode into its pack. BYTECODE_OFF = {"PYTHONDONTWRITEBYTECODE": "1"} +#: Starts a child script under `-I` with only the script's own folder on sys.path, as running it directly would. +ISOLATED_START = ( + "import os, runpy, sys\n" + "sys.argv = sys.argv[1:]\n" + "sys.path.insert(0, os.path.dirname(os.path.abspath(sys.argv[0])))\n" + "runpy.run_path(sys.argv[0], run_name='__main__')\n" +) + #: Seconds a gate's patterns may spend on one judgement (one commit's lines, one write, one blob) #: before the gate refuses naming itself: a repo-supplied pattern can be catastrophic. PATTERN_SECONDS = 10 @@ -142,6 +150,19 @@ def run_tree(argv, *, timeout=None, **options): return subprocess.CompletedProcess(argv, proc.returncode, out, err) +def python_free_env(env=None): + """`env` (this process's by default) without any `PYTHON*` variable, plus bytecode off for grandchildren.""" + base = os.environ if env is None else env + return {**{k: v for k, v in base.items() if not k.upper().startswith("PYTHON")}, **BYTECODE_OFF} + + +def isolated_child(argv, env=None): + """(argv, env) that run the Python child `[interpreter, script, *args]` isolated: no PYTHON* variable, + user site or current folder can change what it imports; its own folder is put on sys.path explicitly.""" + interpreter, script, *args = argv + return [interpreter, "-I", "-B", "-X", "utf8", "-c", ISOLATED_START, str(script), *args], python_free_env(env) + + def engine_remaining(deadline): """Seconds left before `deadline`, never negative.""" return max(deadline - time.monotonic(), 0.0) diff --git a/src/chock/gate/guard_runner.py b/src/chock/gate/guard_runner.py index a34be713..7adf565e 100644 --- a/src/chock/gate/guard_runner.py +++ b/src/chock/gate/guard_runner.py @@ -12,11 +12,12 @@ from pathlib import Path from .budget import ( - BYTECODE_OFF, ENGINE_BUDGET_SECONDS, engine_deadline, engine_remaining, + isolated_child, last_state, + python_free_env, run_tree, time_left, ) @@ -101,7 +102,7 @@ def bash_candidates() -> list[str]: def interpreter_env(interpreter: str) -> dict[str, str]: """The environment a guard runs in: on Windows, Git's usr/bin ahead on PATH for sed and grep.""" - env = {**os.environ, **BYTECODE_OFF} + env = python_free_env() env.pop(CAN_ASK_ENV, None) # only a caller that knows the client may say; never inherited if os.name != _WINDOWS: return env @@ -167,6 +168,12 @@ def split_command(command: str) -> tuple[list[str], bool]: return command.split(), True +def _guard_start(interpreter: str, guard: Path, args: list[str], env: dict[str, str]): + """(argv, env) for the guard: a Python guard starts isolated, a shell guard as given.""" + argv = [interpreter, str(guard), *args] + return isolated_child(argv, env) if guard.suffix == PYTHON_SUFFIX else (argv, env) + + def run_guard(guard: Path, command: str, tool: str = "") -> str: """`GUARD_BLOCKED` / `GUARD_ASKED` / `GUARD_CLEAN` when the guard ran, otherwise why it did not.""" return run_guard_detailed(guard, command, tool)[0] @@ -198,8 +205,9 @@ def run_guard_detailed(guard: Path, command: str, tool: str = "", *, can_ask: bo if fallback: env[ARGV_FALLBACK_ENV] = "1" env.update({} if can_ask is None else {CAN_ASK_ENV: "1" if can_ask else "0"}) + argv, env = _guard_start(interpreter, guard, args, env) proc = run_tree( - [interpreter, str(guard), *args], + argv, capture_output=True, text=True, encoding="utf-8", diff --git a/src/chock/gate/tool_call_gate.py b/src/chock/gate/tool_call_gate.py index 9a8c63a8..2fdd473a 100644 --- a/src/chock/gate/tool_call_gate.py +++ b/src/chock/gate/tool_call_gate.py @@ -11,18 +11,17 @@ import fnmatch import json -import os import subprocess import sys from pathlib import Path from .budget import ( - BYTECODE_OFF, PATTERN_SECONDS, allowed_seconds, close_judge, engine_deadline, engine_remaining, + isolated_child, judge_patterns, pattern_judge, run_tree, @@ -98,7 +97,7 @@ def _tool_call_script(spec, root, payload, deadline=None): if not script.is_file(): return ("deny", f"script gate: {named!r} is not installed{_TOOL_CALL_UNDECIDED}") deadline = engine_deadline() if deadline is None else deadline - argv = [sys.executable, str(script)] + argv, env = isolated_child([sys.executable, str(script)]) try: proc = run_tree( argv, @@ -108,7 +107,7 @@ def _tool_call_script(spec, root, payload, deadline=None): encoding="utf-8", errors="replace", cwd=str(root), - env={**os.environ, **BYTECODE_OFF}, + env=env, timeout=time_left(deadline, argv), check=False, ) diff --git a/src/chock/gate/write_gate.py b/src/chock/gate/write_gate.py index f6aaef80..3879fa09 100644 --- a/src/chock/gate/write_gate.py +++ b/src/chock/gate/write_gate.py @@ -16,7 +16,7 @@ import sys from pathlib import Path -from .budget import allowed_seconds, engine_deadline, last_state, run_tree, time_left +from .budget import allowed_seconds, engine_deadline, isolated_child, last_state, run_tree, time_left from .decision_log import GATE_LAST from .decision_target import dlog_fields from .edit_image import added_from_event, created_text, edited_text @@ -167,10 +167,11 @@ def run_gate(gate, writes, event, run_in, deadline): runner = runner_for(gate) if runner is None: return GATE_ERRORED, "the vendored gate runner is not installed beside this gate" - argv = [sys.executable, str(runner), "run", "--gate", str(gate), "--event", event] + argv, env = isolated_child([sys.executable, str(runner), "run", "--gate", str(gate), "--event", event]) try: proc = run_tree( argv, + env=env, input=json.dumps({"writes": writes, **(extra or {})}), capture_output=True, text=True, diff --git a/tests/fixtures/managed_emit/block-no-verify.json b/tests/fixtures/managed_emit/block-no-verify.json index c34902e6..ce9a2c20 100644 --- a/tests/fixtures/managed_emit/block-no-verify.json +++ b/tests/fixtures/managed_emit/block-no-verify.json @@ -12,7 +12,7 @@ "-X", "utf8", "-c", - "import sys,os;r=sys.argv[1];m=[p for p in (r,os.path.join(os.path.dirname(r),'_chock_runtime.py')) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; refusing this call (--on-failure block)\\n';(o or m) and (sys.stderr.write(w),sys.exit(2));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", + "import sys,os;r=sys.argv[1];m=[p for p in (r,*(os.path.join(os.path.dirname(r),*f.split('/')) for f in ('_chock_runtime.py', 'block-no-verify.py', 'chock_shellparse/__init__.py', 'chock_shellparse/args.py', 'chock_shellparse/parse.py', 'chock_shellparse/targets.py', 'chock_shellparse/writes.py', 'claude_code.py'))) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; refusing this call (--on-failure block)\\n';(o or m) and (sys.stderr.write(w),sys.exit(2));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", "/srv/chock-managed/block-no-verify/scripts/claude_code.py", "--guard", "/srv/chock-managed/block-no-verify/scripts/block-no-verify.py" diff --git a/tests/fixtures/managed_emit/scan-secrets.json b/tests/fixtures/managed_emit/scan-secrets.json index 8f6b835f..e918e589 100644 --- a/tests/fixtures/managed_emit/scan-secrets.json +++ b/tests/fixtures/managed_emit/scan-secrets.json @@ -12,7 +12,7 @@ "-X", "utf8", "-c", - "import sys,os;r=sys.argv[1];m=[p for p in (r,os.path.join(os.path.dirname(r),'_chock_runtime.py')) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; refusing this call (--on-failure block)\\n';(o or m) and (sys.stderr.write(w),sys.exit(2));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", + "import sys,os;r=sys.argv[1];m=[p for p in (r,*(os.path.join(os.path.dirname(r),*f.split('/')) for f in ('_chock_runtime.py', 'claude_code.py', 'gate.json', 'gate.py'))) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; refusing this call (--on-failure block)\\n';(o or m) and (sys.stderr.write(w),sys.exit(2));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", "/srv/chock-managed/scan-secrets/scripts/claude_code.py", "--gate", "/srv/chock-managed/scan-secrets/scripts/gate.json" @@ -34,7 +34,7 @@ "-X", "utf8", "-c", - "import sys,os;r=sys.argv[1];m=[p for p in (r,os.path.join(os.path.dirname(r),'_chock_runtime.py')) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; this call goes ahead unchecked (--on-failure allow)\\n';(o or m) and (sys.stderr.write(w),sys.exit(0));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", + "import sys,os;r=sys.argv[1];m=[p for p in (r,*(os.path.join(os.path.dirname(r),*f.split('/')) for f in ('_chock_runtime.py', 'claude_code.py', 'gate.json', 'gate.py'))) if not os.path.isfile(p)];o=sys.version_info<(3, 11,);w='chock: '+('this hook needs Python 3.11+, '+sys.executable+' is older' if o else 'missing '+', '.join(m))+'; this call goes ahead unchecked (--on-failure allow)\\n';(o or m) and (sys.stderr.write(w),sys.exit(0));sys.argv=sys.argv[1:];import runpy;runpy.run_path(r,run_name='__main__')", "/srv/chock-managed/scan-secrets/scripts/claude_code.py", "--gate", "/srv/chock-managed/scan-secrets/scripts/gate.json", diff --git a/tests/test_managed_emit_deploy_text.py b/tests/test_managed_emit_deploy_text.py new file mode 100644 index 00000000..a85f8fc9 --- /dev/null +++ b/tests/test_managed_emit_deploy_text.py @@ -0,0 +1,186 @@ +"""Rulings 10-13: deploy.json names every stop file and client version, says what happens, and odd paths still run.""" + +from __future__ import annotations + +import hashlib +import os +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest +from managed_emit_helpers import bundle_hook, drop_in, emit, env_for, handlers, linux, make_repo, plan_of, run_hook + +from chock.compile.emitters import managed + +CANARY = "git commit --no-verify -m x" +DENY = '"permissionDecision": "deny"' +HOOKS_DOC = "https://code.claude.com/docs/en/hooks" +POSIX_GIT = pytest.mark.skipif(sys.platform == "win32" or not shutil.which("git"), reason="POSIX paths, git") + +#: Every deploy.json sentence that states a behaviour (by its opening words), and the test that proves it. A +#: sentence about the vendor's own behaviour is proven by its quote: "vendor docs". +SENTENCE_PROOF = { + "--on-failure block:": "test_a_missing_bundle_file_follows_on_failure, test_a_missing_guard_or_parser_follows_on_failure", + "--on-failure allow:": "test_a_missing_bundle_file_follows_on_failure, test_a_missing_guard_or_parser_follows_on_failure", + "Every machine needs Python 3.11": "test_too_old_a_python_follows_on_failure, test_user_interpreter_settings_do_not_switch_the_hook_off", + "onFailure needs Claude Code 2.1.295": "test_client_versions_are_quoted_with_the_date_read", + "Monitor commands are checked only": "test_client_versions_are_quoted_with_the_date_read", + "The bundle must sit at deploy_root": "test_odd_deploy_root_and_python_paths_still_start_the_bundle", + "The bundle folder and __PYTHON__": "vendor docs", + "Managed settings bind Claude Code only.": "vendor docs", + "On WSL,": "vendor docs", + "By default (managedSourcesBehavior": "vendor docs", + "onFailure has no effect on Stop hooks": "test_the_stop_gate_never_refuses_for_a_missing_runtime", + "A repository holding .chock/stop.json": "test_a_repo_stop_file_refuses_matched_calls_as_deploy_json_says", + "Files under the user's home folder": "test_each_user_stop_file_refuses_as_deploy_json_says", + "The bundled runtime runs git from PATH": "test_user_interpreter_settings_do_not_switch_the_hook_off", + "Hooks write their decision log": "test_the_decision_log_lands_under_the_users_home", + "A file in managed-settings.d": "vendor docs", + "The labels are vendor-docs evidence": "test_labels_are_vendor_docs_unwitnessed_and_capped", +} +SUITE = Path(__file__).parent + + +def stated(plan: dict) -> list[str]: + return [plan["on_failure"]["consequence"], *plan["requirements"], *plan["caveats"]] + + +@pytest.fixture +def repo(tmp_path: Path) -> Path: + return make_repo(tmp_path) + + +@pytest.mark.parametrize("choice", ["block", "allow"]) +def test_every_behaviour_sentence_has_a_proof(repo: Path, tmp_path: Path, choice: str) -> None: + """Ruling 12: a new sentence fails here until it names its test, and each named test exists.""" + assert linux(repo, tmp_path / "out", on_failure=choice) == 0 + sources = "".join(path.read_text(encoding="utf-8") for path in SUITE.glob("test_managed_emit_*.py")) + for sentence in stated(plan_of(tmp_path / "out")): + [proof] = [proof for opening, proof in SENTENCE_PROOF.items() if sentence.startswith(opening.split("__")[0])] + for name in (part.strip() for part in proof.split(",") if proof != "vendor docs"): + assert f"def {name}(" in sources, (sentence, name) + + +@pytest.mark.parametrize(("choice", "code"), [("block", 2), ("allow", 0)]) +@pytest.mark.parametrize("missing", ["block-no-verify.py", "chock_shellparse"]) +def test_a_missing_guard_or_parser_follows_on_failure(repo, tmp_path, choice, code, missing) -> None: + """Ruling 12: allow lets the call through with a note, block refuses (exit 2), never an ask or a guard deny.""" + bundle, hook = bundle_hook(repo, tmp_path, on_failure=choice) + target = bundle / "block-no-verify" / "scripts" / missing + shutil.rmtree(target) if target.is_dir() else target.unlink() + for command in (CANARY, "git status"): + done = run_hook(hook, command, tmp_path, env_for(tmp_path)) + assert done.returncode == code and "permissionDecision" not in done.stdout, done.stdout + done.stderr + assert f"missing {target.as_posix()}" in done.stderr.replace("\\", "/") or missing in done.stderr + assert f"(--on-failure {choice})" in done.stderr + + +def test_deploy_json_names_every_stop_file_with_its_effect(repo: Path, tmp_path: Path) -> None: + """Ruling 10: the repository's stop, the machine-wide stop, each registry entry, the unreadable registry.""" + assert linux(repo, tmp_path / "out") == 0 + stops = plan_of(tmp_path / "out")["stop_files"] + by_writer = {entry["path"].split(" ")[0]: entry for entry in stops} + assert set(by_writer) == { + ".chock/stop.json", + "~/.chock/stops/_everywhere.json", + "~/.chock/stops/.json", + "~/.chock/stops", + } + assert by_writer["~/.chock/stops/_everywhere.json"]["written_by"] == "chock stop --everywhere" + assert all(entry["effect"] and "refused" in entry["effect"] for entry in stops) + + +@POSIX_GIT +@pytest.mark.parametrize("which", ["everywhere", "registry", "unreadable"]) +def test_each_user_stop_file_refuses_as_deploy_json_says(repo: Path, tmp_path: Path, which: str) -> None: + """Ruling 10 / finding 3: each file under ~/.chock/stops refuses a harmless call; nothing there allows it.""" + _, hook = bundle_hook(repo, tmp_path) + work = tmp_path / "work" + subprocess.run(["git", "init", "-q", str(work)], check=True) + env = env_for(tmp_path) + stops = Path(env["HOME"]) / ".chock" / "stops" + stops.parent.mkdir(parents=True, exist_ok=True) + if which == "unreadable": + stops.write_text("a file where the folder should be", encoding="utf-8") + else: + stops.mkdir() + key = hashlib.sha256(os.path.realpath(work).encode("utf-8")).hexdigest() + name = "_everywhere.json" if which == "everywhere" else f"{key}.json" + (stops / name).write_text("{}" if which == "everywhere" else '{"scope": "all"}', encoding="utf-8") + assert run_hook(hook, "git status", work, {**env, "CLAUDE_PROJECT_DIR": str(work)}).stdout.count(DENY) == 1 + + +@POSIX_GIT +def test_the_decision_log_lands_under_the_users_home(repo: Path, tmp_path: Path) -> None: + _, hook = bundle_hook(repo, tmp_path) + env = {k: v for k, v in env_for(tmp_path).items() if not k.startswith("CHOCK_")} # the suite turns logging off + assert DENY in run_hook(hook, CANARY, tmp_path, env).stdout + assert any((Path(env["HOME"]) / ".chock" / "log").rglob("*")), "no decision record under ~/.chock/log" + + +def test_client_versions_are_quoted_with_the_date_read(repo: Path, tmp_path: Path) -> None: + """Ruling 11: the failure key's and the Monitor matcher's client versions, quoted and dated; what is not covered.""" + assert linux(repo, tmp_path / "out") == 0 + plan = plan_of(tmp_path / "out") + versions = {entry["min_client"]: entry for entry in plan["client_versions"]} + assert set(versions) == {"2.1.295", "2.1.314"} + for version, entry in versions.items(): + assert f"Claude Code v{version} or later" in entry["quote"] and entry["source"] == HOOKS_DOC + assert entry["read"] == "2026-11-13" + assert "Monitor" in versions["2.1.314"]["quote"] + text = " ".join(plan["not_covered"]) + for gap in ("older than 2.1.314", "WebSocket watches", "MCP tools"): + assert gap in text, gap + matchers = "|".join(group["matcher"] for group in drop_in(tmp_path / "out")["hooks"]["PreToolUse"]) + assert "Bash|PowerShell" in matchers and "mcp__" not in matchers and "Monitor" not in matchers + + +ODD = "odd dir 'q' \"dq\" $HOME `tick` ünï" + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX symlink and file names") +def test_odd_deploy_root_and_python_paths_still_start_the_bundle(repo: Path, tmp_path: Path) -> None: + """Ruling 13: spaces, quotes, `$`, a backquote and non-ASCII in both paths; the emitted argv still runs.""" + base = tmp_path / ODD + base.mkdir() + python = base / f"py {ODD}" + python.symlink_to(sys.executable) + out = base / "out" + root = (out / "claude_code" / "bundle").as_posix() + assert emit(repo, out, "--policies", "block-no-verify", "--deploy-root", root, python=str(python)) == 0 + [(_, hook), *_] = handlers(drop_in(out)) + assert hook["command"] == str(python) and any(arg.startswith(root) for arg in hook["args"]) + env = env_for(tmp_path) + assert DENY in run_hook(hook, CANARY, tmp_path, env).stdout + allowed = run_hook(hook, "git status", tmp_path, env) + assert allowed.returncode == 0 and "permissionDecision" not in allowed.stdout, allowed.stderr + + +#: (path as given, platform, its comparable spelling, the system root it names or None for an ordinary folder). +SPELLINGS = [ + ("//etc//x", "linux", "/etc//x", "/etc"), + ("/Etc/x", "linux", "/Etc/x", None), + ("/etcetera/x", "linux", "/etcetera/x", None), + ("/home/me/etc", "linux", "/home/me/etc", None), + ("/srv-data/x", "linux", "/srv-data/x", None), + ("/LIBRARY/x", "macos", "/library/x", "/Library"), + ("/Users/me/Library/x", "macos", "/users/me/library/x", None), + ("/Volumes/Data/out", "macos", "/volumes/data/out", None), + ("C:\\Program Files\\x", "windows", "c:/program files/x", "C:/Program Files"), + ("\\\\?\\C:\\Windows\\x", "windows", "c:/windows/x", "C:/Windows"), + ("\\\\host\\D$\\x", "windows", "d:/x", None), + ("\\\\?\\UNC\\host\\c$\\ProgramData", "windows", "c:/programdata", "C:/ProgramData"), + ("C:/Users//Me/out", "windows", "c:/users/me/out", None), + ("C:/Users/me/Program Files/x", "windows", "c:/users/me/program files/x", None), + ("C:/ProgramDataX/x", "windows", "c:/programdatax/x", None), + ("D:/out", "windows", "d:/out", None), +] + + +@pytest.mark.parametrize(("given", "platform", "spelling", "root"), SPELLINGS) +def test_output_guard_normalisation_table(given: str, platform: str, spelling: str, root: str | None) -> None: + """Ruling 13: each spelling the guard compares, and whether it is refused; ordinary folders stay allowed.""" + assert managed.spelled(given, platform) == spelling + assert managed.root_named(given, platform) == root diff --git a/tests/test_managed_emit_isolation.py b/tests/test_managed_emit_isolation.py new file mode 100644 index 00000000..5e586f50 --- /dev/null +++ b/tests/test_managed_emit_isolation.py @@ -0,0 +1,104 @@ +"""Ruling 9: every child interpreter the shared runtime starts is isolated; hostile PYTHON* values change no verdict.""" + +from __future__ import annotations + +import json +import os +import subprocess +import sys +from pathlib import Path + +import pytest +from managed_emit_helpers import bundle_hook, env_for, make_repo, run_hook + +from chock.doctor.payloads import payload +from chock.gate import budget + +CANARY = "git commit --no-verify -m x" +HARMLESS = "git status" +#: Each variable the review proved or suspected: ends the guard early, breaks its start-up, or drops its folder. +HOSTILE = ("PYTHONPATH", "PYTHONHOME", "PYTHONPLATLIBDIR", "PYTHONSAFEPATH", "PYTHONSTARTUP") + + +def hostile_value(name: str, evil: Path) -> str: + return { + "PYTHONPATH": str(evil), + "PYTHONHOME": str(evil / "no-such-home"), + "PYTHONPLATLIBDIR": "nope", + "PYTHONSAFEPATH": "1", + "PYTHONSTARTUP": str(evil / "sitecustomize.py"), + }[name] + + +@pytest.fixture +def evil(tmp_path: Path) -> Path: + """A folder whose sitecustomize ends any interpreter that loads it with exit 0 and no decision.""" + folder = tmp_path / "evil" + folder.mkdir() + (folder / "sitecustomize.py").write_text("import os\nos._exit(0)\n", encoding="utf-8") + return folder + + +@pytest.fixture +def bundle(tmp_path: Path) -> tuple[Path, dict]: + return bundle_hook(make_repo(tmp_path), tmp_path) + + +def project_hook(scripts: Path, command: str, cwd: Path, env: dict[str, str]) -> subprocess.CompletedProcess: + """The project entry (`claude_code.py --guard ...`) started isolated, so only its children see `env`'s PYTHON*.""" + return subprocess.run( + [sys.executable, "-I", str(scripts / "claude_code.py"), "--guard", str(scripts / "block-no-verify.py")], + input=json.dumps(payload("claude_code", command, cwd.as_posix())), + capture_output=True, + text=True, + cwd=cwd, + env=env, + check=False, + timeout=60, + ) + + +def assert_verdicts(denied: subprocess.CompletedProcess, allowed: subprocess.CompletedProcess) -> None: + assert denied.returncode == allowed.returncode == 0, denied.stderr + allowed.stderr + assert '"permissionDecision": "deny"' in denied.stdout, denied.stdout + denied.stderr + assert "permissionDecision" not in allowed.stdout, allowed.stdout + allowed.stderr + + +@pytest.mark.parametrize("name", HOSTILE) +def test_managed_route_ignores_hostile_python_variables(name, bundle, evil, tmp_path) -> None: + """The reviewer's proof (t_batch1.py section 5): the canary is still refused, a harmless call still allowed.""" + _, hook = bundle + env = env_for(tmp_path, **{name: hostile_value(name, evil)}) + assert_verdicts(run_hook(hook, CANARY, tmp_path, env), run_hook(hook, HARMLESS, tmp_path, env)) + + +@pytest.mark.parametrize("name", HOSTILE) +def test_project_route_children_ignore_hostile_python_variables(name, bundle, evil, tmp_path) -> None: + """The project entry hands the guard child the same hostile environment; the child is isolated all the same.""" + root, _ = bundle + scripts = root / "block-no-verify" / "scripts" + env = env_for(tmp_path, **{name: hostile_value(name, evil)}) + assert_verdicts(project_hook(scripts, CANARY, tmp_path, env), project_hook(scripts, HARMLESS, tmp_path, env)) + + +def test_isolated_child_drops_every_python_variable_and_keeps_bytecode_off(tmp_path: Path) -> None: + script = tmp_path / "s.py" + argv, env = budget.isolated_child( + [sys.executable, script, "a"], {"PYTHONPATH": "x", "pythonhome": "y", "PATH": "/bin", "HOME": "/h"} + ) + assert argv[:2] == [sys.executable, "-I"] and argv[-2:] == [str(script), "a"] + assert env == {"PATH": "/bin", "HOME": "/h", **budget.BYTECODE_OFF} + + +def test_an_isolated_child_imports_from_its_own_folder_only(tmp_path: Path, evil: Path) -> None: + """Its own folder is on sys.path (a guard's chock_shellparse), the current folder and PYTHONPATH are not.""" + own = tmp_path / "own" + own.mkdir() + (own / "helper.py").write_text("WHO = 'own'\n", encoding="utf-8") + (evil / "helper.py").write_text("WHO = 'evil'\n", encoding="utf-8") + (own / "main.py").write_text("import sys, helper\nprint(helper.WHO, sys.argv[1:])\n", encoding="utf-8") + argv, env = budget.isolated_child([sys.executable, own / "main.py", "x y"], dict(os.environ)) + done = subprocess.run( + argv, env={**env, "PYTHONPATH": str(evil)}, cwd=evil, capture_output=True, text=True, check=False + ) + assert done.stdout.strip() == "own ['x y']", done.stderr From 0cf04b3521a92ffe4df679c2179996194cfc1c1e Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:23:57 +0000 Subject: [PATCH 21/57] fix: the hook launcher starts its runtime with -E -s Ruling 9 on the project and plugin routes: the outer interpreter launch.sh starts no longer reads PYTHONPATH, PYTHONHOME, PYTHONPLATLIBDIR or PYTHONSAFEPATH, or the user site; the probe runs under -I. -E -s rather than -I: the launcher also starts scripts (chock_bundle.py) that import from their own folder, which -I's safe path drops. The `-m chock` form is left as it was: it needs site-packages. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2b1-managed-emit-claude.md | 4 ++- src/chock/hooks/data/launch.sh | 9 ++++-- tests/test_managed_emit_isolation.py | 38 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/changelog.d/w2b1-managed-emit-claude.md b/changelog.d/w2b1-managed-emit-claude.md index 8b2be99a..eb0aaca5 100644 --- a/changelog.d/w2b1-managed-emit-claude.md +++ b/changelog.d/w2b1-managed-emit-claude.md @@ -18,7 +18,9 @@ Claude Code does not read. - Every child interpreter a hook runtime starts (a Python guard, the gate runner, a script gate) now runs isolated (`-I`), with its own folder put on the import path explicitly and every `PYTHON*` - variable removed from its environment, on every route. A hostile `PYTHONPATH`, `PYTHONHOME`, + variable removed from its environment, on every route. The hook launcher (`launch.sh`) starts a + runtime script with `-E -s`, so no `PYTHON*` variable or user site reaches the outer interpreter + either (its `-m chock` form keeps the environment: it needs the installed package). A hostile `PYTHONPATH`, `PYTHONHOME`, `PYTHONPLATLIBDIR` or `PYTHONSAFEPATH` no longer lets a refused command through or turns a refusal into a prompt. - `chock emit --managed claude_code`: the bootstrap now checks every bundled file, so a missing guard diff --git a/src/chock/hooks/data/launch.sh b/src/chock/hooks/data/launch.sh index e3cd4948..fe887da2 100644 --- a/src/chock/hooks/data/launch.sh +++ b/src/chock/hooks/data/launch.sh @@ -21,17 +21,20 @@ configured="$(git config --get chock.python 2>/dev/null)" legacy= case "${OS:-}|${MSYSTEM:-}" in *Windows_NT*|*"|"?*) legacy=1 ;; esac [ "$runtime" = "-m" ] && legacy=1 +# A script starts with -E -s (-I less -P): no PYTHON* variable or user site changes it; its own folder stays on the path. +isolate=-Es +[ "$runtime" = "-m" ] && isolate= # Only the marker path sets it (to 9); everywhere else an inherited value is cleared, so it never names an fd. [ -n "$legacy" ] && { CHOCK_START_FD=; export CHOCK_START_FD; } for candidate in "$configured" python3 python py; do [ -n "$candidate" ] || continue if [ -n "$legacy" ]; then - if probed="$("$candidate" -c 'import sys; print("chock-python"); sys.exit(sys.version_info < (__MIN_PYTHON__,))' /dev/null)" && [ "$probed" = "chock-python" ]; then - exec "$candidate" -X utf8 "$runtime" "$@" + if probed="$("$candidate" -I -c 'import sys; print("chock-python"); sys.exit(sys.version_info < (__MIN_PYTHON__,))' /dev/null)" && [ "$probed" = "chock-python" ]; then + exec "$candidate" ${isolate:+"$isolate"} -X utf8 "$runtime" "$@" fi elif command -v "$candidate" >/dev/null 2>&1; then exec 3>&1 - marker="$(CHOCK_START_FD=9 "$candidate" -X utf8 "$runtime" "$@" 9>&1 1>&3 3>&-)" + marker="$(CHOCK_START_FD=9 "$candidate" ${isolate:+"$isolate"} -X utf8 "$runtime" "$@" 9>&1 1>&3 3>&-)" status=$? exec 3>&- [ "$marker" = "chock-python" ] && exit "$status" diff --git a/tests/test_managed_emit_isolation.py b/tests/test_managed_emit_isolation.py index 5e586f50..e60a04e6 100644 --- a/tests/test_managed_emit_isolation.py +++ b/tests/test_managed_emit_isolation.py @@ -4,6 +4,7 @@ import json import os +import shutil import subprocess import sys from pathlib import Path @@ -13,10 +14,12 @@ from chock.doctor.payloads import payload from chock.gate import budget +from chock.hooks import launch CANARY = "git commit --no-verify -m x" HARMLESS = "git status" #: Each variable the review proved or suspected: ends the guard early, breaks its start-up, or drops its folder. +POSIX_GIT = pytest.mark.skipif(sys.platform == "win32" or not shutil.which("git"), reason="POSIX sh and git") HOSTILE = ("PYTHONPATH", "PYTHONHOME", "PYTHONPLATLIBDIR", "PYTHONSAFEPATH", "PYTHONSTARTUP") @@ -102,3 +105,38 @@ def test_an_isolated_child_imports_from_its_own_folder_only(tmp_path: Path, evil argv, env={**env, "PYTHONPATH": str(evil)}, cwd=evil, capture_output=True, text=True, check=False ) assert done.stdout.strip() == "own ['x y']", done.stderr + + +def launched(folder: Path, command: str, cwd: Path, env: dict[str, str]) -> subprocess.CompletedProcess: + """The runtime in `folder` started through chock's launcher, as a project or plugin hook command starts it.""" + return subprocess.run( + [ + "sh", + str(folder / "launch.sh"), + str(folder / "claude_code.py"), + "--guard", + str(folder / "block-no-verify.py"), + ], + input=json.dumps(payload("claude_code", command, cwd.as_posix())), + capture_output=True, + text=True, + cwd=cwd, + env=env, + check=False, + timeout=60, + ) + + +@POSIX_GIT +@pytest.mark.parametrize("route", ["project", "plugin"]) +@pytest.mark.parametrize("name", [None, *HOSTILE]) +def test_the_launcher_starts_the_runtime_isolated(route, name, bundle, evil, tmp_path) -> None: + """Ruling 9 on the launcher routes: the outer interpreter is isolated too, before and with each hostile value.""" + root, _ = bundle + work = tmp_path / "work" + subprocess.run(["git", "init", "-q", str(work)], check=True) + folder = work / ".chock" / "bin" if route == "project" else tmp_path / "plugin" / "scripts" + shutil.copytree(root / "block-no-verify" / "scripts", folder) + (folder / "launch.sh").write_text(launch.launcher_text(), encoding="utf-8", newline="\n") + env = env_for(tmp_path, **({} if name is None else {name: hostile_value(name, evil)})) + assert_verdicts(launched(folder, CANARY, work, env), launched(folder, HARMLESS, work, env)) From 8e288911214fc264731894cd4af2010541c91db4 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:40:59 +0000 Subject: [PATCH 22/57] fix(floor): whole agent folders, symlink-aware matching, undecided reads (W2-A1v2.1) L1: the default control-plane list covers the whole folder of every agent chock or agentseam writes into, plugin folders, .mcp.json, .vscode/settings.json, .vscode/mcp.json, .env and .env.*; a test checks it against the engine's own data (vendor hook files, permission files, mcp_clients.json, plugin stores, the CI installer). Root instruction files stay out. L2: a diff entry at a parent folder of a pattern matches (symlink, gitlink); a symlinked control-plane folder's in-repo target, at base or head, is control plane. Tree data only. L3: a blob git cannot read at base or head is undecided, never "". L4: --repo resolves to the top level. L5: every run states the mode, pattern count and the base's additions and removals; a removal naming no default pattern is undecided; the history pass uses --no-renames; --floor help carries the V8 caveat. L6: help and changelog state the list's limits and the rename cost. L7: the review-3 proofs are regression tests asserting the owed exit. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2a1-check-floor.md | 31 ++-- src/chock/data/commands.json | 2 +- src/chock/data/control_plane.json | 47 +++--- src/chock/history/gitlog.py | 4 +- src/chock/history/scan.py | 4 +- src/chock/validation/checks_floor.py | 12 +- src/chock/validation/control_plane.py | 83 +++++++++- src/chock/validation/floor_cli.py | 6 +- src/chock/validation/floor_context.py | 19 +-- src/chock/validation/floor_git.py | 10 ++ src/chock/validation/floor_history.py | 2 +- tests/test_floor_control_plane.py | 6 +- tests/test_floor_control_plane_list.py | 93 ++++++++++++ tests/test_floor_review3_exit.py | 200 +++++++++++++++++++++++++ tests/test_floor_review3_plane.py | 168 +++++++++++++++++++++ 15 files changed, 632 insertions(+), 55 deletions(-) create mode 100644 tests/test_floor_control_plane_list.py create mode 100644 tests/test_floor_review3_exit.py create mode 100644 tests/test_floor_review3_plane.py diff --git a/changelog.d/w2a1-check-floor.md b/changelog.d/w2a1-check-floor.md index deb68ee1..a7656b49 100644 --- a/changelog.d/w2a1-check-floor.md +++ b/changelog.d/w2a1-check-floor.md @@ -3,22 +3,33 @@ `refs/pull//merge` or the merge queue's head; anything else is undecided). Everything comes from the tree diff `base..head` (no renames, no external diff or textconv) and the commits in it; no merge-base is computed. Exit 0 clean, 1 refused, 2 undecided. -- Control plane: a change (add, edit, delete, mode) to any path in `src/chock/data/control_plane.json` - (`.chock/**`, `chock.lock`, `.agents/policies/**`, every vendor hook or settings file chock and agentseam write, - with local and override variants, `.github/workflows/**`, `.github/actions/**`, `CODEOWNERS`, `.gitattributes`; - matched case-blind) is reported as `floor/control-plane-changed` with every path, first. These files define the - guardrails; the floor does not judge whether a change to them is safe. A person decides. The base's - `.chock/config.yaml` sets `floor.control_plane`: `refuse` (default, exit 1: every guardrail change, including a - policy upgrade, a `chock sync` regeneration or a workflow edit, needs a privileged merge) or `listed` (exit 0, - paths still listed and in the SARIF; the adopter relies on code-owner review, and content inside listed paths is - not judged). `floor.control_plane_paths: {add, remove}` edits the list. The head's config is never read. +- Control plane: a change (add, edit, delete, mode) to any path in `src/chock/data/control_plane.json` is + reported as `floor/control-plane-changed` with every path, first. The list: `.chock/**`, `chock.lock`, the whole + folder of every agent chock or agentseam writes into (`.agents/**`, `.claude/**`, `.codex/**`, `.cursor/**`, + `.gemini/**`, `.windsurf/**`, `.github/hooks/**` and the rest, plugin folders such as `.claude-plugin/**`), + `.mcp.json`, `.vscode/settings.json`, `.vscode/mcp.json`, `.env`, `.env.*`, `.github/workflows/**`, + `.github/actions/**`, `CODEOWNERS`, `.gitattributes`; matched case-blind. A symlink or gitlink at a parent + folder of a pattern (`.gemini -> cfg`) matches, and a symlinked control-plane folder's in-repo target is + control plane (read from the trees at base and head). Root instruction files (`AGENTS.md`, `CLAUDE.md`) are + not on it. These files define the guardrails; the floor does not judge whether a change to them is safe. A + person decides. The base's `.chock/config.yaml` sets `floor.control_plane`: `refuse` (default, exit 1: every + guardrail change, including a policy upgrade, a `chock sync` regeneration or a workflow edit, needs a + privileged merge) or `listed` (exit 0, paths still listed and in the SARIF; the adopter relies on code-owner + review, and content inside listed paths is not judged). `floor.control_plane_paths: {add, remove}` edits the + list; a `remove` entry that names no default pattern is undecided (exit 2). Every run prints the mode, the + number of patterns and the base's additions and removals. The head's config is never read. +- The list is what chock knows about, not a proof of completeness. If your workflow installs chock from a file in + the repository (`requirements*.txt`, `pyproject.toml`, a lockfile, a local action), add that file to the list. + Renaming or moving a test file reads as a deletion and is refused by test-integrity. +- `--repo` may name a folder below the top level; every diff is computed from the top level. A file git cannot + read at base or head is undecided (exit 2), never handed to a gate as empty text. - Everything else is judged by the base's compiled ci gates and rollout, run by this engine; no head file executes. `ask` refuses; a gate that crashes, times out or cannot be read is undecided whatever the rollout. A waiver counts only on a line present and unchanged in both base and head. Script gates receive deleted paths under `deleted` and an environment of `PATH`, `HOME` (and `SYSTEMROOT`) only (script payload contract version 2, `spec/gate-dsl.md`). - A per-commit pass runs the base's content gates (both surfaces) over every blob a commit in `base..head` adds or - changes outside the control plane, read as latin-1, as utf-8 when valid, and, for a blob with a NUL byte, as + changes outside the control plane (renames read as a delete and an add), read as latin-1, as utf-8 when valid, and, for a blob with a NUL byte, as utf-16 in both byte orders and alignments, lines split on `\n` only. A range past the commit limit or a blob over 64 MiB is undecided, naming the commit and path. Not seen: other encodings, compressed or encoded content, commit messages, file names. diff --git a/src/chock/data/commands.json b/src/chock/data/commands.json index 9121c35a..8b00d73b 100644 --- a/src/chock/data/commands.json +++ b/src/chock/data/commands.json @@ -29,7 +29,7 @@ "check": { "module": "chock.history.cli", "fn": "check_main", - "help": "Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor, a preview, judges a pull request's merge result with its base's gates)" + "help": "Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor, a preview, judges a pull request's merge result with its base's gates; it holds only as a required check the pull request cannot redefine, and judges committed content only)" }, "status": { "module": "chock.lifecycle", diff --git a/src/chock/data/control_plane.json b/src/chock/data/control_plane.json index 6d12a658..7618164c 100644 --- a/src/chock/data/control_plane.json +++ b/src/chock/data/control_plane.json @@ -1,27 +1,36 @@ { - "$comment": "The paths that define the guardrails (`chock check --floor`, floor/control-plane-changed). Matched with fnmatch after case folding, so `*` crosses `/`. A pull request that adds, changes, deletes or re-modes one is listed; the base's `.chock/config.yaml` decides refuse or listed. Vendor hook files are agentseam's config_path for every repo-relative vendor (tests/test_floor_control_plane.py checks it) plus each local or override variant chock knows.", + "$comment": "The paths that define the guardrails (`chock check --floor`, floor/control-plane-changed). Matched with fnmatch after case folding, so `*` crosses `/`; a symlink or gitlink at a parent folder of a pattern matches too, and a symlinked control-plane folder's in-repo target is control plane. A pull request that adds, changes, deletes or re-modes one is listed; the base's `.chock/config.yaml` decides refuse or listed. Whole folder for every agent chock or agentseam writes into (hooks, permissions, MCP, plugins, skills, subagents), plus the engine's CI workflow and store files; tests/test_floor_control_plane_list.py checks the list against the engine's own data. Instruction files at the root (AGENTS.md, CLAUDE.md) are left out: they steer an agent, they are not guardrails. What chock knows about, not a proof of completeness.", "paths": [ ".chock/**", "chock.lock", - ".agents/policies/**", - ".agents/hooks.json", - ".agents/plugins/**", - ".claude/settings.json", - ".claude/settings.local.json", - ".clinerules/hooks/**", - ".codex/hooks.json", - ".codex/config.toml", - ".cursor/hooks.json", - ".devin/hooks.json", - ".devin/hooks.v1.json", - ".windsurf/hooks.json", - ".factory/hooks.json", - ".gemini/settings.json", - ".grok/hooks/**", - ".openhands/hooks.json", - ".tabnine/agent/settings.json", - ".vibe/hooks.toml", + ".agents/**", + ".claude/**", + ".clinerules/**", + ".codex/**", + ".cursor/**", + ".devin/**", + ".factory/**", + ".gemini/**", + ".grok/**", + ".junie/**", + ".kimi-code/**", + ".openhands/**", + ".tabnine/**", + ".vibe/**", + ".windsurf/**", + ".claude-plugin/**", + ".cursor-plugin/**", + ".devin-plugin/**", + ".factory-plugin/**", + ".plugin/**", ".github/hooks/**", + ".github/agents/**", + ".github/plugin/**", + ".mcp.json", + ".vscode/settings.json", + ".vscode/mcp.json", + ".env", + ".env.*", ".github/workflows/**", ".github/actions/**", ".github/CODEOWNERS", diff --git a/src/chock/history/gitlog.py b/src/chock/history/gitlog.py index 37a00dc7..d0dea963 100644 --- a/src/chock/history/gitlog.py +++ b/src/chock/history/gitlog.py @@ -152,10 +152,10 @@ def _records(tokens: Iterator[bytes], log: Log) -> Iterator[tuple[str, str, str] yield commit, fields[3], path -def touches(repo: Path, since: str | None, max_commits: int, head: str = "HEAD") -> Log: +def touches(repo: Path, since: str | None, max_commits: int, head: str = "HEAD", *, renames: bool = True) -> Log: """Blobs added or changed per commit, oldest commit first (the newest `max_commits` of the range).""" args = [ - "log", "-z", "--raw", "-m", "-M", "--topo-order", "--reverse", "--no-abbrev", + "log", "-z", "--raw", "-m", "-M" if renames else "--no-renames", "--topo-order", "--reverse", "--no-abbrev", "--no-ext-diff", "--no-textconv", "--no-show-signature", "--diff-filter=ACMRT", "-n", str(max_commits), "--format=%x01%H", *_range(since, head), "--", ] # fmt: skip diff --git a/src/chock/history/scan.py b/src/chock/history/scan.py index 4a16727c..b67254e5 100644 --- a/src/chock/history/scan.py +++ b/src/chock/history/scan.py @@ -63,6 +63,8 @@ class Limits: #: The floor's reader: every blob is judged (binary too) in each of `floor_texts`, and a blob still not #: judged (over max_blob_bytes) is named in Report.unread. judge_binary: bool = False + #: False reads a rename as a delete and an add (`--no-renames`), as the floor's tree diff does. + renames: bool = True def _say(message: str) -> None: @@ -223,7 +225,7 @@ def scan(repo: Path, rules: list[Rule], since: str | None, limits: Limits, head: report.truncated = in_range > limits.max_commits report.commits = min(in_range, limits.max_commits) _say(f"history: listing up to {report.commits} of {in_range} commits") - log = gitlog.touches(repo, since, limits.max_commits, head) + log = gitlog.touches(repo, since, limits.max_commits, head, renames=limits.renames) report.skipped_submodules = log.submodules oids = sorted({t.oid for t in log.touches}) _say(f"history: {len(log.touches)} changed paths, {len(oids)} distinct blobs") diff --git a/src/chock/validation/checks_floor.py b/src/chock/validation/checks_floor.py index 1e2f8e4c..19412833 100644 --- a/src/chock/validation/checks_floor.py +++ b/src/chock/validation/checks_floor.py @@ -34,7 +34,7 @@ from chock.validation import control_plane as cp from chock.validation import floor_sarif from chock.validation.floor_context import FloorContext -from chock.validation.floor_git import FloorError, Revision, changed_paths, require_ancestor, resolve +from chock.validation.floor_git import FloorError, Revision, changed_paths, require_ancestor, resolve, toplevel from chock.validation.floor_history import history_findings EXIT_REFUSED, EXIT_UNDECIDED = 1, 2 @@ -80,6 +80,7 @@ class Outcome: notes: list[str] = field(default_factory=list) listed: list[str] = field(default_factory=list) mode: str = cp.REFUSE + in_force: str = "" def base_gates(base: Revision) -> list[Gate]: @@ -159,7 +160,7 @@ def judge_gates( # noqa: PLR0913 -- the range, the branch, what it excludes and if "commit" not in (gate.spec.get("on") or []) and gate.spec: continue ctx = FloorContext( - repo, base=base.sha, head=head.sha, head_ref=head_ref, scope=gate.spec.get("paths"), + repo, base=base, head=head, head_ref=head_ref, scope=gate.spec.get("paths"), control_plane=plane.covers, ) # fmt: skip result = _judge(gate, ctx, Path(scratch)) @@ -188,11 +189,11 @@ def _declared(base: Revision, policy: str) -> str: def _judged(repo: Path, base: Revision, head: Revision, branch: str | None) -> Outcome: - plane = cp.from_base(base) + plane = cp.through_links(cp.from_base(base), base, head) listed = [path for path in changed_paths(repo, base.sha, head.sha) if plane.covers(path)] level = committed_rollout(repo, base.sha) outcome = judge_gates(repo, base, head, branch, plane=plane, level=level) - outcome.listed, outcome.mode = listed, plane.mode + outcome.listed, outcome.mode, outcome.in_force = listed, plane.mode, plane.summary() if not base_gates(base): outcome.notes.append(_NO_GATES) try: @@ -210,6 +211,7 @@ def _judged(repo: Path, base: Revision, head: Revision, branch: str | None) -> O def _print(outcome: Outcome) -> None: + _out(outcome.in_force) if outcome.listed: head = "FAIL" if outcome.mode == cp.REFUSE else "LISTED" text = _CONTROL_PLANE.format(rule=cp.RULE, mode=outcome.mode, count=len(outcome.listed)) @@ -235,7 +237,7 @@ def _code(outcome: Outcome) -> int: def floor(repo: Path, base_ref: str, head_ref_rev: str, branch: str | None, sarif: str | None = None) -> int: - repo = repo.resolve() + repo = toplevel(repo) base_sha, head_sha = resolve(repo, base_ref, "base"), resolve(repo, head_ref_rev, "head") require_ancestor(repo, base_sha, head_sha) _out(f"== floor (preview): base {base_sha[:12]}, head {head_sha[:12]}") diff --git a/src/chock/validation/control_plane.py b/src/chock/validation/control_plane.py index a064d491..fc3624a8 100644 --- a/src/chock/validation/control_plane.py +++ b/src/chock/validation/control_plane.py @@ -6,8 +6,11 @@ from __future__ import annotations +import dataclasses import fnmatch +import functools import json +import posixpath import unicodedata from dataclasses import dataclass from importlib import resources @@ -24,6 +27,8 @@ _SECTION, _MODE, _PATHS = "floor", "control_plane", "control_plane_paths" _EDITS = ("add", "remove") _FORMAT = "Cf" +_SYMLINK = "120000" +_GLOB = frozenset("*?[") def default_patterns() -> tuple[str, ...]: @@ -31,6 +36,7 @@ def default_patterns() -> tuple[str, ...]: return tuple(json.loads(text)["paths"]) +@functools.lru_cache(maxsize=8192) def folded(path: str) -> str: """`path` as a case-blind, normalising filesystem may open it: NFC, no format characters, case folded, and no trailing dot or space on any component (Windows drops them).""" @@ -38,14 +44,77 @@ def folded(path: str) -> str: return "/".join(part.rstrip(". ") for part in text.casefold().split("/")) +@functools.cache +def _parents(pattern: str) -> set[str]: + """The folders a pattern lies under, up to its first glob: a symlink or gitlink there redirects the pattern.""" + parts = folded(pattern).split("/") + literal = parts[: next((i for i, p in enumerate(parts) if _GLOB & set(p)), len(parts))] + return {"/".join(literal[:k]) for k in range(1, len(literal) + 1) if k < len(parts)} + + @dataclass(frozen=True) class ControlPlane: mode: str patterns: tuple[str, ...] + added: tuple[str, ...] = () + removed: tuple[str, ...] = () + #: (folded in-repo target, link path) for each symlink the control plane reaches through, at base or head. + links: tuple[tuple[str, str], ...] = () - def covers(self, path: str) -> bool: + def _direct(self, name: str) -> bool: + return any(fnmatch.fnmatchcase(name, folded(p)) or name in _parents(p) for p in self.patterns) + + def covers(self, path: str, _depth: int = 0) -> bool: + """Whether `path`, or the path a client opens through a symlinked control-plane folder, is control plane.""" name = folded(path) - return any(fnmatch.fnmatchcase(name, folded(pattern)) for pattern in self.patterns) + if self._direct(name): + return True + if _depth > len(self.links): + return False + rests = ((link, _under(name, target)) for target, link in self.links) + return any(self.covers(link + rest, _depth + 1) for link, rest in rests if rest is not None) + + def summary(self) -> str: + """The list in force: mode, size, and the base's own edits to the defaults.""" + added, removed = ", ".join(self.added) or "none", ", ".join(self.removed) or "none" + return ( + f"control plane: {self.mode}, {len(self.patterns)} pattern(s); base added: {added}; base removed: {removed}" + ) + + +def _under(name: str, target: str) -> str | None: + """`name` relative to `target` ("" when equal, "/rest" below it; the root is ""), None when outside it.""" + if not target: + return f"/{name}" + if name == target or name.startswith(f"{target}/"): + return name[len(target) :] + return None + + +def _target(link: str, data: bytes | None) -> str | None: + """The in-repo folder or file a committed symlink names; None when it leaves the repository.""" + if not data: + return None + joined = posixpath.normpath(posixpath.join(posixpath.dirname(link), data.decode("utf-8", "replace"))) + if posixpath.isabs(joined) or joined.split("/")[0] == "..": + return None + return "" if joined == "." else folded(joined) + + +def through_links(plane: ControlPlane, *revisions: Revision) -> ControlPlane: + """`plane` plus every in-repo target of a symlink it covers at any of `revisions`, followed to a fixed point.""" + symlinks = [(rev, path) for rev in revisions for path, (mode, _) in rev.entries().items() if mode == _SYMLINK] + links: list[tuple[str, str]] = [] + grew = True + while grew: + grew = False + current = dataclasses.replace(plane, links=tuple(links)) + for rev, path in symlinks: + target = _target(path, rev.blob(path)) if current.covers(path) else None + if target is not None and (target, folded(path)) not in links: + links.append((target, folded(path))) + grew = True + return dataclasses.replace(plane, links=tuple(links)) def _invalid(what: str) -> FloorError: @@ -85,6 +154,10 @@ def from_base(base: Revision) -> ControlPlane: if mode not in _MODES: msg = f"{_MODE} is {mode!r}; it takes {' or '.join(_MODES)}" raise _invalid(msg) - edits = _edits(section) - kept = tuple(p for p in default_patterns() if p not in edits["remove"]) - return ControlPlane(mode, kept + tuple(edits["add"])) + edits, defaults = _edits(section), default_patterns() + unknown = [p for p in edits["remove"] if p not in defaults] + if unknown: + msg = f"{_PATHS}.remove names {unknown!r}, which is no default pattern" + raise _invalid(msg) + kept = tuple(p for p in defaults if p not in edits["remove"]) + return ControlPlane(mode, kept + tuple(edits["add"]), tuple(edits["add"]), tuple(edits["remove"])) diff --git a/src/chock/validation/floor_cli.py b/src/chock/validation/floor_cli.py index cfcef739..1c59aaf5 100644 --- a/src/chock/validation/floor_cli.py +++ b/src/chock/validation/floor_cli.py @@ -12,7 +12,11 @@ def add_arguments(parser: argparse.ArgumentParser) -> None: "--floor", action="store_true", help="Preview: judge base..head (base an ancestor of head: pass the merge result) with the base's gates, " - "run by this engine; a changed guardrail path is listed and, by default, refused (needs --base; runs alone)", + "run by this engine; a changed guardrail path is listed and, by default, refused (needs --base; runs alone). " + "It holds only as a required check the pull request cannot redefine (your setting, unverified by chock) and " + "judges committed content only; under `listed` you rely on code-owner review of the listed paths. The list is " + "what chock knows about, not a proof of completeness (a file your workflow installs chock from belongs on it: " + "floor.control_plane_paths.add); a renamed or moved test file reads as a deletion and is refused", ) parser.add_argument("--head", default=None, help="With --floor: the merge result under test (default HEAD)") diff --git a/src/chock/validation/floor_context.py b/src/chock/validation/floor_context.py index a0802a95..a1daefd6 100644 --- a/src/chock/validation/floor_context.py +++ b/src/chock/validation/floor_context.py @@ -9,12 +9,13 @@ from chock.gate.runner import GateContext from chock.gate.runner.context import _GIT from chock.gate.runner.gitparse import DiffError, _diff_sides +from chock.validation.floor_git import Revision class FloorContext(GateContext): """`base..head` as the change; the head's files as the proposal; the base's as what a waiver must already be in. - Every read is a `git show :`, so a head file is data and never reaches a program as code. + Every read is a blob by revision (unreadable is undecided), so a head file is data and never reaches a program as code. Diffs run with `--text` and `--no-renames`, so neither a NUL byte, a head `.gitattributes` nor a rename hides a line or a deletion. A control-plane path is never judged here: the control-plane rule owns it. """ @@ -23,14 +24,14 @@ def __init__( # noqa: PLR0913 -- the range and what it excludes, beside GateCon self, repo_root: Path, *, - base: str, - head: str, + base: Revision, + head: Revision, head_ref: str | None, scope: Sequence[str] | None, control_plane: Callable[[str], bool], ) -> None: - super().__init__(repo_root=repo_root, base=base, head_ref=head_ref, scope=scope) - self.head = head + super().__init__(repo_root=repo_root, base=base.sha, head_ref=head_ref, scope=scope) + self.head, self._revisions = head.sha, (base, head) self.control_plane = control_plane def in_scope(self, path: str, *, judge_own: bool = False) -> bool: @@ -55,12 +56,12 @@ def _diff_text(self, path: str) -> str: return proc.stdout.decode("utf-8", "replace") def staged_blob(self, path: str) -> str: - """The proposal: the file at the head.""" - return self._git("show", f"{self.head}:{path}") + """The proposal: the file at the head; "" only when the head has no such file, FloorError when unreadable.""" + return self._revisions[1].text(path) or "" def head_blob(self, path: str) -> str: - """Before the change: the file at the base.""" - return self._git("show", f"{self.base}:{path}") + """Before the change: the file at the base; "" only when the base has no such file, FloorError when unreadable.""" + return self._revisions[0].text(path) or "" def committed_blob(self, path: str) -> str: """Where a waiver must be for the floor to honour it: the lines present and unchanged in both base and diff --git a/src/chock/validation/floor_git.py b/src/chock/validation/floor_git.py index 32a633ca..06d031fd 100644 --- a/src/chock/validation/floor_git.py +++ b/src/chock/validation/floor_git.py @@ -31,6 +31,16 @@ def git(repo: Path, *args: str, data: bytes | None = None) -> subprocess.Complet raise FloorError(msg) from exc +def toplevel(repo: Path) -> Path: + """The repository's top level, so a `--repo` below it sees every path; FloorError when `repo` is in none.""" + proc = git(repo.resolve(), "rev-parse", "--show-toplevel") + top = os.fsdecode(proc.stdout).strip() + if proc.returncode or not top: + msg = f"{repo} is not inside a git repository" + raise FloorError(msg) + return Path(top).resolve() + + def resolve(repo: Path, rev: str, what: str) -> str: """The commit `rev` names; FloorError when it names none.""" proc = git(repo, "rev-parse", "--verify", "--quiet", f"{rev}^{{commit}}") diff --git a/src/chock/validation/floor_history.py b/src/chock/validation/floor_history.py index 677d24fa..ff650211 100644 --- a/src/chock/validation/floor_history.py +++ b/src/chock/validation/floor_history.py @@ -65,7 +65,7 @@ def history_findings( ) -> tuple[list[tuple[str, str]], list[str]]: """((policy, finding) per refusal candidate, notes) of the per-commit pass; FloorError (undecided) when it cannot judge all of it.""" - limits = Limits(max_blob_bytes=FLOOR_MAX_BLOB_BYTES, judge_binary=True) + limits = Limits(max_blob_bytes=FLOOR_MAX_BLOB_BYTES, judge_binary=True, renames=False) report = _report(repo, base, head, limits) if report is None: return [], [NO_CONTENT_GATES] diff --git a/tests/test_floor_control_plane.py b/tests/test_floor_control_plane.py index 53df4cf0..fa5940a7 100644 --- a/tests/test_floor_control_plane.py +++ b/tests/test_floor_control_plane.py @@ -30,6 +30,8 @@ def test_every_repo_relative_vendor_hook_file_is_control_plane(vendor): "chock.lock", ".agents/policies/x/manifest.yaml", ".claude/settings.local.json", + ".claude/commands/x.md", + ".claude/skills/x/SKILL.md", ".github/hooks/chock.json", ".github/workflows/any.yml", ".github/actions/floor/action.yml", @@ -48,7 +50,9 @@ def test_guardrail_paths_match_in_any_spelling_a_filesystem_may_open(path): assert DEFAULT.covers(path) -@pytest.mark.parametrize("path", ["src/app.py", ".claude/commands/x.md", "docs/guide.md", "chock.lock.bak", "tests/x"]) +@pytest.mark.parametrize( + "path", ["src/app.py", "AGENTS.md", "CLAUDE.md", "docs/guide.md", "chock.lock.bak", "tests/x", ".envrc"] +) def test_ordinary_paths_are_not_control_plane(path): assert not DEFAULT.covers(path) diff --git a/tests/test_floor_control_plane_list.py b/tests/test_floor_control_plane_list.py new file mode 100644 index 00000000..862d2fe5 --- /dev/null +++ b/tests/test_floor_control_plane_list.py @@ -0,0 +1,93 @@ +"""The floor's default control-plane list (W2-A1v2.1 L1) against every repo-relative path the engine writes.""" + +from __future__ import annotations + +import json +from importlib import resources + +import pytest +from agentseam import instructions, permissions_data +from agentseam.vendor_config import VENDOR_CONFIG + +from chock import vendors +from chock.config import CONFIG_DIR +from chock.gate.runner.constants import COMPILED_PREFIX, POLICIES_PREFIX +from chock.guardrails.toggle import FILENAME as GUARDRAILS_TOGGLE +from chock.hooks.launch import LAUNCHER_REL +from chock.plugin.marketplace_core import TREES +from chock.plugin.marketplace_devin import DEVIN_ROOT_MANIFEST_REL +from chock.scaffold.install_ci import DEFAULT_PATH as CI_WORKFLOW +from chock.validation import control_plane as cp + +DEFAULT = cp.ControlPlane(cp.REFUSE, cp.default_patterns()) +_REPO_SCOPES = ("project", "local") + + +def _data(package: str, rel: str) -> dict: + return json.loads(resources.files(package).joinpath(rel).read_text(encoding="utf-8")) + + +def _hook_files() -> list[str]: + return sorted({p for p in map(vendors.config_path, VENDOR_CONFIG) if not p.startswith("~")}) + + +def _engine_paths() -> list[str]: + """Every repo-relative file or folder chock or agentseam writes hooks, permissions, MCP servers, plugins, + CI or its own state into. A folder is given with a trailing `/`.""" + found = set(_hook_files()) + found |= { + e["path"] for files in permissions_data.CONFIG_FILES.values() for e in files if e["scope"] in _REPO_SCOPES + } + found |= {c["path"] for c in _data("chock", "data/mcp_clients.json").values()} + found |= {p.as_posix() for tree in TREES.values() for p in tree["index_paths"]} | { + DEVIN_ROOT_MANIFEST_REL.as_posix() + } + install = _data("chock.install", "data/install.json") + found |= {i["path"] for client in install["clients"].values() for i in client.get("index", [])} + stores = resources.files("chock.plugin").joinpath("data/stores") + layouts = [json.loads(f.read_text(encoding="utf-8")).get("layout", {}) for f in stores.iterdir()] + # A plugin manifest in its own folder (`.factory-plugin/plugin.json`); a bare `plugin.json` is the package root's. + found |= {m for layout in layouts if "/" in (m := layout.get("manifest") or "")} + found |= {CI_WORKFLOW, LAUNCHER_REL, GUARDRAILS_TOGGLE, COMPILED_PREFIX, POLICIES_PREFIX, f"{CONFIG_DIR}/"} + return sorted(found) + + +def _agent_folder(path: str) -> str: + parts = path.split("/") + return "/".join(parts[:2] if parts[0] == ".github" else parts[:1]) + + +@pytest.mark.parametrize("path", _engine_paths()) +def test_every_path_the_engine_writes_is_control_plane(path): + assert path.startswith(".") or "/" not in path, path # repo-relative, never a home path + assert DEFAULT.covers(path) or DEFAULT.covers(f"{path.rstrip('/')}/x"), path + + +@pytest.mark.parametrize("folder", sorted({_agent_folder(p) for p in _hook_files() if "/" in p})) +def test_every_agent_folder_is_control_plane_whole(folder): + for rel in ("skills/x/SKILL.md", "agents/x.md", "cli.json", ".env", "rules/x.md", "x"): + assert DEFAULT.covers(f"{folder}/{rel}"), f"{folder}/{rel}" + assert DEFAULT.covers(folder), f"a symlink or gitlink at {folder} is not matched" + + +@pytest.mark.parametrize( + "path", + sorted({f for e in instructions.INSTRUCTION_FILES.values() for f in e["files"] if "/" in f}), +) +def test_instruction_files_inside_an_agent_folder_are_covered_by_it(path): + """Whole folders: an instruction file inside one is listed with it; one beside them (`.github/x.md`) is not.""" + assert DEFAULT.covers(path) == DEFAULT.covers(f"{_agent_folder(path)}/x"), path + + +@pytest.mark.parametrize( + "path", sorted({f for e in instructions.INSTRUCTION_FILES.values() for f in e["files"] if "/" not in f}) +) +def test_root_instruction_files_stay_out(path): + assert not DEFAULT.covers(path), path + + +@pytest.mark.parametrize( + "path", [".mcp.json", ".vscode/settings.json", ".vscode/mcp.json", ".env", ".env.local", ".cursor/cli.json"] +) +def test_client_config_and_env_files_named_by_the_review_are_control_plane(path): + assert DEFAULT.covers(path) diff --git a/tests/test_floor_review3_exit.py b/tests/test_floor_review3_exit.py new file mode 100644 index 00000000..a526c645 --- /dev/null +++ b/tests/test_floor_review3_exit.py @@ -0,0 +1,200 @@ +"""Review 3 of the floor (W2-A1v2.1 L3, L4, L6, L7): fault paths, ordinary pull requests, no head file runs.""" + +from __future__ import annotations + +import os +import shutil +from pathlib import Path + +import pytest +from floor_support import PROBE, TEST_FILE, base_repo, commit, git, run_floor, write + +from chock.validation import checks_floor, floor_git +from chock.validation.checks_floor import main as floor_main + +_IDENT = ("-c", "user.name=t", "-c", "user.email=t@example.com") + + +def _drop_object(repo: Path, oid: str) -> None: + (repo / ".git/objects" / oid[:2] / oid[2:]).unlink() + + +def _base_without(tmp_path: Path, *policies: str) -> Path: + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + for policy in policies: + shutil.rmtree(repo / ".chock/compiled" / policy) + commit(repo, f"base: without {', '.join(policies)}") + git(repo, "checkout", "-q", "-B", "pr") + return repo + + +@pytest.mark.parametrize("without", [("floor-token", "test-integrity"), ()]) +def test_an_unreadable_head_blob_is_undecided(tmp_path, capsys, without): + """L3: with a script gate only (no history pass) as with content gates, never '' handed to a gate.""" + repo = _base_without(tmp_path, *without) if without else base_repo(tmp_path) + write(repo, "src/new.py", "print('x')\n") + commit(repo, "pr") + _drop_object(repo, git(repo, "rev-parse", "HEAD:src/new.py")) + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "src/new.py" in out, out + + +@pytest.mark.parametrize("below", ["src", "tests"]) +def test_repo_given_as_a_subdirectory_still_sees_removed_assertions(tmp_path, capsys, below): + """L4: a workflow with `working-directory:` set, or --repo below the top level.""" + repo = base_repo(tmp_path) + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + commit(repo, "pr") + code = checks_floor.main(["--repo", str(repo / below), "--base", "main"]) + out = capsys.readouterr() + assert code == 1, out.out + out.err + assert "test-integrity" in out.out, out.out + + +def test_repo_outside_any_repository_is_undecided(tmp_path, capsys): + (tmp_path / "plain").mkdir() + code = checks_floor.main(["--repo", str(tmp_path / "plain"), "--base", "main"]) + assert code == 2, capsys.readouterr() + + +def test_keyboard_interrupt_never_returns_zero(tmp_path, monkeypatch): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + + def boom(*_a, **_k): + raise KeyboardInterrupt + + monkeypatch.setattr(floor_git, "changed_paths", boom) + monkeypatch.setattr(checks_floor, "changed_paths", boom) + with pytest.raises(KeyboardInterrupt): + checks_floor.main(["--repo", str(repo), "--base", "main"]) + + +def _ordinary(repo: Path, change: str) -> None: + if change == "source": + write(repo, "src/app.py", "VALUE = 2\n") + elif change == "test-added": + write(repo, "tests/test_more.py", "def test_b():\n assert True\n") + elif change == "docs": + write(repo, "docs/guide.md", "# Guide\n") + elif change == "dependency": + write(repo, "requirements.txt", "requests==2.32.3\n") + elif change == "instructions": + write(repo, "AGENTS.md", "# Rules\n") + elif change == "rename-source": + git(repo, "mv", "src/app.py", "src/core.py") + commit(repo, change) + + +@pytest.mark.parametrize("change", ["source", "test-added", "docs", "dependency", "instructions", "rename-source"]) +def test_ordinary_pull_request_passes(tmp_path, capsys, change): + """V7. rename-source runs without the fixture's probe gate, which refuses every deletion by design.""" + repo = _base_without(tmp_path, "probe") if change == "rename-source" else base_repo(tmp_path) + _ordinary(repo, change) + code, out = run_floor(repo, capsys) + assert code == 0, out + + +@pytest.mark.parametrize("dest", ["tests/test_main.py", "tests/unit/test_core.py"]) +def test_renaming_or_moving_a_test_file_is_refused_as_a_deletion(tmp_path, capsys, dest): + """L6, a stated cost: --no-renames reads the move as a deleted test file.""" + repo = _base_without(tmp_path, "probe") + (repo / dest).parent.mkdir(parents=True, exist_ok=True) + git(repo, "mv", "tests/test_core.py", dest) + commit(repo, "move") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "test-integrity" in out, out + + +def test_history_pass_reads_a_rename_as_a_delete_and_an_add(tmp_path, capsys): + """L5: a secret moved in one commit and removed in the next is judged at the path it was added under.""" + repo = _base_without(tmp_path, "probe", "test-integrity") + write(repo, "src/a.txt", "key = FLOOR-TOKEN-7\n") + commit(repo, "add") + git(repo, "mv", "src/a.txt", "src/b.txt") + commit(repo, "move") + git(repo, "rm", "-q", "src/b.txt") + commit(repo, "drop") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "src/a.txt" in out, out + + +def test_branch_that_merged_an_advanced_base_passes(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/feature.py", "X = 1\n") + commit(repo, "pr work") + git(repo, "checkout", "-q", "main") + write(repo, ".github/workflows/chock.yml", "name: changed by a maintainer\n") + commit(repo, "base advanced") + git(repo, "checkout", "-q", "pr") + git(repo, *_IDENT, "merge", "-q", "--no-edit", "main") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_merge_result_against_advanced_base_passes(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/feature.py", "X = 1\n") + commit(repo, "pr work") + git(repo, "checkout", "-q", "main") + write(repo, ".github/workflows/chock.yml", "name: changed by a maintainer\n") + write(repo, "tests/test_core.py", TEST_FILE.replace(" assert 2 * 2 == 4\n", "")) + commit(repo, "base advanced (a maintainer trimmed a test)") + git(repo, "checkout", "-q", "-b", "merge-ref") + git(repo, *_IDENT, "merge", "-q", "--no-edit", "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def _marker_py(marks: Path, name: str) -> str: + return f"import pathlib\npathlib.Path({str(marks / name)!r}).write_text('ran')\n" + + +def _marker_sh(marks: Path, name: str) -> str: + return f"#!/bin/sh\necho ran > {marks / name}\ncat\n" + + +@pytest.mark.parametrize("checkout", ["head", "base"]) +def test_no_head_file_runs(tmp_path, capsys, checkout): + marks = tmp_path / "marks" + marks.mkdir() + repo = base_repo(tmp_path) + write(repo, PROBE["params"]["script"], _marker_py(marks, "head-probe")) + write(repo, ".agents/policies/probe/implementations/sitecustomize.py", _marker_py(marks, "head-site-impl")) + for rel, name in (("sitecustomize.py", "site"), ("usercustomize.py", "usersite"), ("json.py", "json")): + write(repo, rel, _marker_py(marks, name)) + write(repo, ".chock/bin/gate.py", _marker_py(marks, "head-runner")) + write(repo, ".chock/compiled/evil/ci-gate/gate.json", + {"kind": "script", "on": ["commit"], "action": "block", "message": "m", + "params": {"script": "evil.py"}, "script_base": "gate"}) # fmt: skip + write(repo, ".chock/compiled/evil/ci-gate/evil.py", _marker_py(marks, "head-gate")) + write(repo, ".gitattributes", "* diff=evil filter=evil\n") + write(repo, "src/app.py", "VALUE = 2\n") + write(repo, ".githooks/post-checkout", _marker_sh(marks, "hook")) + commit(repo, "pr") + args = ["--repo", str(repo), "--base", "main"] + if checkout == "base": + git(repo, "checkout", "-q", "main") + args += ["--head", "pr"] + for name in ("textconv", "diffcmd", "clean", "smudge", "process", "fsmonitor"): + script = tmp_path / f"{name}.sh" + script.write_text(_marker_sh(marks, name)) + script.chmod(0o755) + for key, name in (("diff.evil.textconv", "textconv"), ("diff.evil.command", "diffcmd"), + ("filter.evil.clean", "clean"), ("filter.evil.smudge", "smudge"), + ("filter.evil.process", "process"), ("core.fsmonitor", "fsmonitor")): # fmt: skip + git(repo, "config", key, str(tmp_path / f"{name}.sh")) + cwd = os.getcwd() + os.chdir(repo) + try: + code = floor_main(args) + finally: + os.chdir(cwd) + out = capsys.readouterr() + assert sorted(p.name for p in marks.iterdir()) == [], out.out + out.err + assert code == 1, out.out + out.err diff --git a/tests/test_floor_review3_plane.py b/tests/test_floor_review3_plane.py new file mode 100644 index 00000000..964fd48c --- /dev/null +++ b/tests/test_floor_review3_plane.py @@ -0,0 +1,168 @@ +"""Review 3 of the floor (W2-A1v2.1 L1, L2, L5, L7): guardrail files the list missed, symlinks, the list in force.""" + +from __future__ import annotations + +import os + +import pytest +from floor_support import CONTROL_PLANE, base_repo, commit, git, run_floor, write + +GATEWAY_MCP = { + "mcpServers": {"fetch": {"command": "chock", "args": ["gateway", "run", "--repo", ".", "--", "fetch-mcp"]}} +} +UNWRAPPED_MCP = {"mcpServers": {"fetch": {"command": "fetch-mcp"}}} +SKILL_HOOK = ( + "---\nname: x\ndescription: x\nhooks:\n PreToolUse:\n - matcher: Bash\n hooks:\n" + " - type: command\n command: ./rewrite-input.sh\n---\nbody\n" +) +GEMINI_OFF = {"hooksConfig": {"enabled": False}, "tools": {"allowed": ["run_shell_command"]}} + + +def _pr(repo, files): + for rel, content in files.items(): + write(repo, rel, content) + commit(repo, "pr") + + +def _refused(repo, capsys, *paths: str) -> str: + code, out = run_floor(repo, capsys) + assert code == 1, out + assert f"{CONTROL_PLANE} (refuse)" in out, out + for path in paths: + assert f"\n {path}\n" in out, f"{path} not listed\n{out}" + return out + + +@pytest.mark.parametrize("rel", [".mcp.json", ".cursor/mcp.json", ".vscode/mcp.json"]) +def test_unwrapping_the_mcp_gateway_is_named(tmp_path, capsys, rel): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + _pr(repo, {rel: GATEWAY_MCP}) + git(repo, "checkout", "-q", "-B", "pr") + _pr(repo, {rel: UNWRAPPED_MCP}) + _refused(repo, capsys, rel) + + +@pytest.mark.parametrize( + ("rel", "content"), + [ + (".cursor/cli.json", {"permissions": {"allow": ["Shell(*)"], "deny": []}}), + (".vscode/settings.json", {"chat.hookFilesLocations": {"elsewhere": True}, "chat.useClaudeHooks": False}), + (".gemini/.env", "PATH=/tmp/evil:/usr/bin\n"), + (".env", "PYTHONPATH=/tmp/evil\n"), + (".env.local", "PYTHONPATH=/tmp/evil\n"), + (".claude/skills/x/SKILL.md", SKILL_HOOK), + (".claude/agents/x.md", SKILL_HOOK), + (".github/agents/x.agent.md", SKILL_HOOK), + ], +) +def test_other_hook_permission_or_env_files_are_named(tmp_path, capsys, rel, content): + repo = base_repo(tmp_path) + _pr(repo, {rel: content}) + _refused(repo, capsys, rel) + + +def test_symlinked_vendor_folder_adds_a_settings_file_named(tmp_path, capsys): + """`.gemini -> cfg` then `cfg/settings.json`: the client opens `.gemini/settings.json`.""" + repo = base_repo(tmp_path) + write(repo, "cfg/settings.json", GEMINI_OFF) + os.symlink("cfg", repo / ".gemini") + commit(repo, "pr") + _refused(repo, capsys, ".gemini", "cfg/settings.json") + + +def test_a_base_symlinked_vendor_folder_makes_its_target_control_plane(tmp_path, capsys): + """The link is at the base and unchanged; the pull request edits only the target.""" + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, "cfg/settings.json", {"hooksConfig": {"enabled": True}}) + os.symlink("cfg", repo / ".gemini") + commit(repo, "base: linked settings") + git(repo, "checkout", "-q", "-B", "pr") + _pr(repo, {"cfg/settings.json": GEMINI_OFF}) + out = _refused(repo, capsys, "cfg/settings.json") + assert "\n .gemini\n" not in out, out + + +def test_a_chain_of_symlinks_is_followed(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + os.symlink("a", repo / ".codex") + os.symlink("b", repo / "a") + write(repo, "b/keep.txt", "x\n") + commit(repo, "base: links") + git(repo, "checkout", "-q", "-B", "pr") + _pr(repo, {"b/hooks.json": {"hooks": {}}}) + _refused(repo, capsys, "b/hooks.json") + + +def test_a_vendor_folder_linked_to_the_root_makes_the_root_control_plane(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + os.symlink(".", repo / ".windsurf") + commit(repo, "base: link to the root") + git(repo, "checkout", "-q", "-B", "pr") + _pr(repo, {"hooks.json": {"hooks": {}}}) + _refused(repo, capsys, "hooks.json") + + +def test_a_symlink_out_of_the_repository_adds_nothing(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + os.symlink("../elsewhere", repo / ".gemini") + commit(repo, "base: link out") + git(repo, "checkout", "-q", "-B", "pr") + _pr(repo, {"src/app.py": "VALUE = 2\n"}) + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_gitlink_at_a_vendor_folder_is_named(tmp_path, capsys): + """A submodule mounted at `.codex` supplies `.codex/hooks.json` on a recursive clone.""" + repo = base_repo(tmp_path) + sha = git(repo, "rev-parse", "HEAD") + git(repo, "update-index", "--add", "--cacheinfo", f"160000,{sha},.codex") + write(repo, ".gitmodules", '[submodule "c"]\n\tpath = .codex\n\turl = https://example.com/x.git\n') + git(repo, "add", ".gitmodules") + git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "--no-gpg-sign", "-m", "pr") + _refused(repo, capsys, ".codex") + + +def test_listed_mode_states_a_narrowed_list(tmp_path, capsys): + """A base that removed a default pattern: a later workflow edit passes, and the output says the list was narrowed.""" + repo = base_repo(tmp_path, "listed") + git(repo, "checkout", "-q", "main") + write( + repo, + ".chock/config.yaml", + "floor:\n control_plane: refuse\n control_plane_paths:\n remove: ['.github/workflows/**']\n", + ) + commit(repo, "base narrows") + git(repo, "checkout", "-q", "-B", "pr") + _pr(repo, {".github/workflows/chock.yml": "name: x\non: push\njobs: {}\n"}) + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "control plane: refuse," in out, out + assert "base removed: .github/workflows/**" in out, out + + +@pytest.mark.parametrize("mode", ["refuse", "listed"]) +def test_mode_and_size_are_stated_when_nothing_is_listed(tmp_path, capsys, mode): + repo = base_repo(tmp_path, mode) + _pr(repo, {"src/app.py": "VALUE = 2\n"}) + code, out = run_floor(repo, capsys) + assert code == 0, out + assert f"control plane: {mode}, " in out, out + assert "pattern(s); base added: none; base removed: none" in out, out + + +def test_a_removal_that_names_no_default_pattern_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, ".chock/config.yaml", "floor:\n control_plane_paths:\n remove: ['.github/workflow/**']\n") + commit(repo, "base: typo") + git(repo, "checkout", "-q", "-B", "pr") + _pr(repo, {"src/app.py": "VALUE = 2\n"}) + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "which is no default pattern" in out, out From e66db5ccd35ce0ecab6b2a973c4eaa872a251b94 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Fri, 13 Nov 2026 01:00:04 +0000 Subject: [PATCH 23/57] test(cli): refresh the help golden for the --floor caveat Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- tests/fixtures/cli_help_golden.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/cli_help_golden.txt b/tests/fixtures/cli_help_golden.txt index deebc2cf..39858248 100644 --- a/tests/fixtures/cli_help_golden.txt +++ b/tests/fixtures/cli_help_golden.txt @@ -8,7 +8,7 @@ Everyday: install Build a chock.sh selection into one plugin for one agent (install --selection [--client C]) remove Remove an installed policy and resync sync Recompile + rewire so the repo matches its policies (--ci/--skills for extras) - check Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor, a preview, judges a pull request's merge result with its base's gates) + check Run every truth check: validate, verify, evals, matrix (--only to narrow; --history scans past commits; --floor, a preview, judges a pull request's merge result with its base's gates; it holds only as a required check the pull request cannot redefine, and judges committed content only) status Policy states and coverage (--only registry,log for more) stop Refuse every agent tool call, commit and push here until resume (--agent C for one client) resume Lift a stop: agents are judged by the policies again From 7a29d71227d4d6f733bfeca916482684562913d2 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Fri, 13 Nov 2026 01:29:00 +0000 Subject: [PATCH 24/57] refactor(floor): move Outcome into floor_outcome to break the import cycle checks_floor imported floor_sarif at module level while floor_sarif imported Outcome back from checks_floor under TYPE_CHECKING, which CodeQL reports as a module-level cyclic import. Outcome now lives in its own module that both import; no behaviour change. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- src/chock/validation/checks_floor.py | 13 ++----------- src/chock/validation/floor_outcome.py | 17 +++++++++++++++++ src/chock/validation/floor_sarif.py | 5 +---- 3 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 src/chock/validation/floor_outcome.py diff --git a/src/chock/validation/checks_floor.py b/src/chock/validation/checks_floor.py index 19412833..85adaeca 100644 --- a/src/chock/validation/checks_floor.py +++ b/src/chock/validation/checks_floor.py @@ -12,7 +12,7 @@ import json import sys import tempfile -from dataclasses import dataclass, field +from dataclasses import dataclass from pathlib import Path from chock.display import inert @@ -36,6 +36,7 @@ from chock.validation.floor_context import FloorContext from chock.validation.floor_git import FloorError, Revision, changed_paths, require_ancestor, resolve, toplevel from chock.validation.floor_history import history_findings +from chock.validation.floor_outcome import Outcome EXIT_REFUSED, EXIT_UNDECIDED = 1, 2 _CI_GATE = "ci-gate/gate.json" @@ -73,16 +74,6 @@ class Gate: spec: dict -@dataclass -class Outcome: - refusals: list[str] = field(default_factory=list) - undecided: list[str] = field(default_factory=list) - notes: list[str] = field(default_factory=list) - listed: list[str] = field(default_factory=list) - mode: str = cp.REFUSE - in_force: str = "" - - def base_gates(base: Revision) -> list[Gate]: found: list[Gate] = [] for path in base.paths(COMPILED_PREFIX): diff --git a/src/chock/validation/floor_outcome.py b/src/chock/validation/floor_outcome.py new file mode 100644 index 00000000..b68f5ada --- /dev/null +++ b/src/chock/validation/floor_outcome.py @@ -0,0 +1,17 @@ +"""What `chock check --floor` found: the refusals, undecided lines, notes and listed control-plane paths.""" + +from __future__ import annotations + +from dataclasses import dataclass, field + +from chock.validation import control_plane as cp + + +@dataclass +class Outcome: + refusals: list[str] = field(default_factory=list) + undecided: list[str] = field(default_factory=list) + notes: list[str] = field(default_factory=list) + listed: list[str] = field(default_factory=list) + mode: str = cp.REFUSE + in_force: str = "" diff --git a/src/chock/validation/floor_sarif.py b/src/chock/validation/floor_sarif.py index ae72d7b5..6b219e57 100644 --- a/src/chock/validation/floor_sarif.py +++ b/src/chock/validation/floor_sarif.py @@ -5,15 +5,12 @@ import json import sys from pathlib import Path -from typing import TYPE_CHECKING from chock import __version__ from chock.gate.sarif import INFORMATION_URI, SARIF_SCHEMA, safe_text from chock.gate.sarif_output import write_output from chock.validation import control_plane as cp - -if TYPE_CHECKING: - from chock.validation.checks_floor import Outcome +from chock.validation.floor_outcome import Outcome _REFUSED = "floor/refused" _UNDECIDED_EXIT = 2 From 4a60c43da9a9086e8ebba368435656306ba67b8a Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 08:29:20 -0500 Subject: [PATCH 25/57] chore: sync the compiled runtime, goldens, lockfile and hook configs on release/1.2.0 Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- .chock/bin/_chock_runtime.py | 31 ++++++++++--- .chock/bin/claude_code.py | 2 +- .chock/bin/codex_cli.py | 2 +- .chock/bin/cursor.py | 2 +- .chock/bin/devin.py | 2 +- .chock/bin/gate.py | 46 +++++++++++++++++-- .chock/bin/gemini_cli.py | 2 +- .chock/bin/grok.py | 2 +- .../claude_code-PreToolUse-185f9750.json | 1 - .../claude_code-PreToolUse-3a2134a7.json | 4 -- .../claude_code-PreToolUse-4acdeb9e.json | 5 -- .../index/codex_cli-PreToolUse-14533f34.json | 4 -- .../index/codex_cli-PreToolUse-186bd88e.json | 1 - .../index/codex_cli-PreToolUse-d2160b00.json | 5 -- .../cursor-beforeShellExecution-b6c825df.json | 4 -- .../bin/index/cursor-preToolUse-b6c825df.json | 6 --- .../bin/index/devin-PreToolUse-185f9750.json | 4 -- .../index/gemini_cli-BeforeTool-09cc5246.json | 4 -- .../index/gemini_cli-BeforeTool-c8f6602b.json | 1 - .../index/gemini_cli-BeforeTool-e86eddda.json | 5 -- .../bin/index/grok-PreToolUse-185f9750.json | 4 -- .../index/tabnine-BeforeTool-c8f6602b.json | 4 -- .../vscode_copilot-PreToolUse-2b90d191.json | 1 - .../vscode_copilot-PreToolUse-38a36ef3.json | 5 -- .../vscode_copilot-preToolUse-3ed8896e.json | 4 -- .chock/bin/launch.sh | 9 ++-- .chock/bin/tabnine.py | 2 +- .chock/bin/vscode_copilot.py | 2 +- .chock/bin/windsurf.py | 2 +- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 15 ------ .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .../managed-setting/managed-settings.json | 4 -- .claude/settings.json | 6 +-- .codex/hooks.json | 12 ++--- .cursor/hooks.json | 4 +- .devin/hooks.v1.json | 2 +- .gemini/settings.json | 6 +-- .github/hooks/chock.json | 24 +++++----- .grok/hooks/agentseam.json | 2 +- .tabnine/agent/settings.json | 2 +- chock.lock | 46 +++++++++---------- .../fixtures/plugin_targets/antigravity.json | 4 +- tests/fixtures/plugin_targets/droid.json | 6 +-- tests/fixtures/plugin_targets/goose.json | 6 +-- tests/fixtures/plugin_targets/kimi.json | 8 ++-- tests/fixtures/plugin_targets/openhands.json | 6 +-- .../runtime_goldens/_chock_runtime.py | 31 ++++++++++--- tests/fixtures/runtime_goldens/antigravity.py | 2 +- tests/fixtures/runtime_goldens/claude_code.py | 2 +- tests/fixtures/runtime_goldens/codex_cli.py | 2 +- tests/fixtures/runtime_goldens/cursor.py | 2 +- tests/fixtures/runtime_goldens/devin.py | 2 +- .../fixtures/runtime_goldens/factory_droid.py | 2 +- tests/fixtures/runtime_goldens/gemini_cli.py | 2 +- tests/fixtures/runtime_goldens/goose.py | 2 +- tests/fixtures/runtime_goldens/grok.py | 2 +- .../fixtures/runtime_goldens/mistral_vibe.py | 2 +- tests/fixtures/runtime_goldens/openhands.py | 2 +- tests/fixtures/runtime_goldens/tabnine.py | 2 +- .../runtime_goldens/vscode_copilot.py | 2 +- tests/fixtures/runtime_goldens/windsurf.py | 2 +- 80 files changed, 188 insertions(+), 270 deletions(-) delete mode 100644 .chock/compiled/agent-discipline/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/block-destructive-commands/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/block-no-verify/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/code-safety/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/context-hygiene/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/git-safety/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/injection-defense/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/memory-discipline/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/minimal-content/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/pin-github-actions/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/pre-generated-scripts/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/protect-agent-config/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/protect-main-branch/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/require-review-evidence/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/scan-secrets/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/test-integrity/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/token-efficiency/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/verify-framework-conformance/managed-setting/managed-settings.json delete mode 100644 .chock/compiled/yagni/managed-setting/managed-settings.json diff --git a/.chock/bin/_chock_runtime.py b/.chock/bin/_chock_runtime.py index 36926292..233ffb4c 100644 --- a/.chock/bin/_chock_runtime.py +++ b/.chock/bin/_chock_runtime.py @@ -32,6 +32,8 @@ BYTECODE_OFF = {'PYTHONDONTWRITEBYTECODE': '1'} +ISOLATED_START = "import os, runpy, sys\nsys.argv = sys.argv[1:]\nsys.path.insert(0, os.path.dirname(os.path.abspath(sys.argv[0])))\nrunpy.run_path(sys.argv[0], run_name='__main__')\n" + PATTERN_SECONDS = 10 PATTERN_START_SECONDS = 10 @@ -111,6 +113,17 @@ def run_tree(argv, *, timeout=None, **options): untrack_tree(proc) return _chock_subprocess.CompletedProcess(argv, proc.returncode, out, err) +def python_free_env(env=None): + """`env` (this process's by default) without any `PYTHON*` variable, plus bytecode off for grandchildren.""" + base = _chock_os.environ if env is None else env + return {**{k: v for k, v in base.items() if not k.upper().startswith('PYTHON')}, **BYTECODE_OFF} + +def isolated_child(argv, env=None): + """(argv, env) that run the Python child `[interpreter, script, *args]` isolated: no PYTHON* variable, + user site or current folder can change what it imports; its own folder is put on sys.path explicitly.""" + interpreter, script, *args = argv + return ([interpreter, '-I', '-B', '-X', 'utf8', '-c', ISOLATED_START, str(script), *args], python_free_env(env)) + def engine_remaining(deadline): """Seconds left before `deadline`, never negative.""" return max(deadline - _chock_time.monotonic(), 0.0) @@ -803,7 +816,7 @@ def bash_candidates() -> list[str]: def interpreter_env(interpreter: str) -> dict[str, str]: """The environment a guard runs in: on Windows, Git's usr/bin ahead on PATH for sed and grep.""" - env = {**_chock_os.environ, **BYTECODE_OFF} + env = python_free_env() env.pop(CAN_ASK_ENV, None) if _chock_os.name != _WINDOWS: return env @@ -861,6 +874,11 @@ def split_command(command: str) -> tuple[list[str], bool]: except ValueError: return (command.split(), True) +def _guard_start(interpreter: str, guard: _chock_Path, args: list[str], env: dict[str, str]): + """(argv, env) for the guard: a Python guard starts isolated, a shell guard as given.""" + argv = [interpreter, str(guard), *args] + return isolated_child(argv, env) if guard.suffix == PYTHON_SUFFIX else (argv, env) + def run_guard(guard: _chock_Path, command: str, tool: str='') -> str: """`GUARD_BLOCKED` / `GUARD_ASKED` / `GUARD_CLEAN` when the guard ran, otherwise why it did not.""" return run_guard_detailed(guard, command, tool)[0] @@ -888,7 +906,8 @@ def run_guard_detailed(guard: _chock_Path, command: str, tool: str='', *, can_as if fallback: env[ARGV_FALLBACK_ENV] = '1' env.update({} if can_ask is None else {CAN_ASK_ENV: '1' if can_ask else '0'}) - proc = run_tree([interpreter, str(guard), *args], capture_output=True, text=True, encoding='utf-8', errors='replace', env=env, timeout=time_left(deadline, [interpreter, str(guard)]), check=False) + argv, env = _guard_start(interpreter, guard, args, env) + proc = run_tree(argv, capture_output=True, text=True, encoding='utf-8', errors='replace', env=env, timeout=time_left(deadline, [interpreter, str(guard)]), check=False) except _chock_subprocess.TimeoutExpired: print(f'chock: guard timed out after {ENGINE_BUDGET_SECONDS}s, not checked', file=sys.stderr) return (GUARD_ERRORED, '') @@ -2624,9 +2643,9 @@ def run_gate(gate, writes, event, run_in, deadline): runner = runner_for(gate) if runner is None: return (GATE_ERRORED, 'the vendored gate runner is not installed beside this gate') - argv = [sys.executable, str(runner), 'run', '--gate', str(gate), '--event', event] + argv, env = isolated_child([sys.executable, str(runner), 'run', '--gate', str(gate), '--event', event]) try: - proc = run_tree(argv, input=json.dumps({'writes': writes, **(extra or {})}), capture_output=True, text=True, encoding=_UTF8, errors='replace', timeout=time_left(deadline, argv), check=False, cwd=str(root) if root is not None else None) + proc = run_tree(argv, env=env, input=json.dumps({'writes': writes, **(extra or {})}), capture_output=True, text=True, encoding=_UTF8, errors='replace', timeout=time_left(deadline, argv), check=False, cwd=str(root) if root is not None else None) except _chock_subprocess.TimeoutExpired as exc: return (GATE_ERRORED, f'the gate runner gave no verdict within {allowed_seconds(exc)}') except (OSError, _chock_subprocess.SubprocessError) as exc: @@ -2767,9 +2786,9 @@ def _tool_call_script(spec, root, payload, deadline=None): if not script.is_file(): return ('deny', f'script gate: {named!r} is not installed{_TOOL_CALL_UNDECIDED}') deadline = engine_deadline() if deadline is None else deadline - argv = [sys.executable, str(script)] + argv, env = isolated_child([sys.executable, str(script)]) try: - proc = run_tree(argv, input=json.dumps(payload), capture_output=True, text=True, encoding='utf-8', errors='replace', cwd=str(root), env={**_chock_os.environ, **BYTECODE_OFF}, timeout=time_left(deadline, argv), check=False) + proc = run_tree(argv, input=json.dumps(payload), capture_output=True, text=True, encoding='utf-8', errors='replace', cwd=str(root), env=env, timeout=time_left(deadline, argv), check=False) except _chock_subprocess.TimeoutExpired as exc: budget = f'gave no verdict within {allowed_seconds(exc)}' return ('deny', f'script gate: {script.name} {budget}{_TOOL_CALL_UNDECIDED}') diff --git a/.chock/bin/claude_code.py b/.chock/bin/claude_code.py index 558e7fbc..86885f6d 100755 --- a/.chock/bin/claude_code.py +++ b/.chock/bin/claude_code.py @@ -855,7 +855,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/bin/codex_cli.py b/.chock/bin/codex_cli.py index 3cbb9f52..0a9db955 100755 --- a/.chock/bin/codex_cli.py +++ b/.chock/bin/codex_cli.py @@ -823,7 +823,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/bin/cursor.py b/.chock/bin/cursor.py index 6db1a9cd..42fab5ed 100755 --- a/.chock/bin/cursor.py +++ b/.chock/bin/cursor.py @@ -721,7 +721,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/bin/devin.py b/.chock/bin/devin.py index cccf63ea..19b17565 100755 --- a/.chock/bin/devin.py +++ b/.chock/bin/devin.py @@ -826,7 +826,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/bin/gate.py b/.chock/bin/gate.py index 41abf266..0cf99ede 100755 --- a/.chock/bin/gate.py +++ b/.chock/bin/gate.py @@ -34,6 +34,14 @@ BYTECODE_OFF = {"PYTHONDONTWRITEBYTECODE": "1"} +ISOLATED_START = ( + "import os, runpy, sys\n" + "sys.argv = sys.argv[1:]\n" + "sys.path.insert(0, os.path.dirname(os.path.abspath(sys.argv[0])))\n" + "runpy.run_path(sys.argv[0], run_name='__main__')\n" +) + + PATTERN_SECONDS = 10 @@ -137,6 +145,19 @@ def run_tree(argv, *, timeout=None, **options): return subprocess.CompletedProcess(argv, proc.returncode, out, err) +def python_free_env(env=None): + """`env` (this process's by default) without any `PYTHON*` variable, plus bytecode off for grandchildren.""" + base = os.environ if env is None else env + return {**{k: v for k, v in base.items() if not k.upper().startswith("PYTHON")}, **BYTECODE_OFF} + + +def isolated_child(argv, env=None): + """(argv, env) that run the Python child `[interpreter, script, *args]` isolated: no PYTHON* variable, + user site or current folder can change what it imports; its own folder is put on sys.path explicitly.""" + interpreter, script, *args = argv + return [interpreter, "-I", "-B", "-X", "utf8", "-c", ISOLATED_START, str(script), *args], python_free_env(env) + + def engine_remaining(deadline): """Seconds left before `deadline`, never negative.""" return max(deadline - time.monotonic(), 0.0) @@ -550,8 +571,12 @@ def head_blob(self, path: str) -> str: #: adds is not honoured. Coverage (`on: [commit]`) is still decided by the name `commit`. AGENT_COMMIT_EVENT = "agent-commit" +#: The event `chock check --floor` judges at: the change under judgement may be the refused party's, so a +#: waiver counts only when the base already has that line (the floor's `committed_blob` reads the base). +FLOOR_EVENT = "ci-floor" + #: Events where the actor may be the refused agent: a waiver counts only when HEAD already has that line. -HEAD_WAIVER_EVENTS = frozenset({AGENT_COMMIT_EVENT, TOOL_USE_EVENT}) +HEAD_WAIVER_EVENTS = frozenset({AGENT_COMMIT_EVENT, TOOL_USE_EVENT, FLOOR_EVENT}) _HUMAN_ENV = frozenset({"0", "false", "no", "off"}) @@ -959,6 +984,15 @@ def _kind_test_integrity(ctx: GateContext, params: dict, event: str) -> GateResu _FINDINGS_KEY = "findings" +#: All a script gate's environment holds at the floor: no token, secret or runner variable reaches it. +_FLOOR_ENV_KEYS = ("PATH", "HOME", "SYSTEMROOT") + + +def _env(event: object) -> dict[str, str]: + if event == FLOOR_EVENT: + return {key: os.environ[key] for key in _FLOOR_ENV_KEYS if key in os.environ} | _BYTECODE_OFF + return {**os.environ, **_BYTECODE_OFF} + def _spawn(script: Path, ctx: GateContext, payload: dict, timeout: float) -> subprocess.CompletedProcess[str]: return run_tree( @@ -969,7 +1003,7 @@ def _spawn(script: Path, ctx: GateContext, payload: dict, timeout: float) -> sub encoding="utf-8", errors="replace", cwd=ctx.repo_root, - env={**os.environ, **_BYTECODE_OFF}, + env=_env(payload.get("event")), timeout=timeout, check=False, ) @@ -1084,7 +1118,7 @@ def _judge_findings( def _kind_script(ctx: GateContext, params: dict, event: str) -> GateResult: """Hand the material to the policy's own script and carry back its verdict. - The script reads `{"event", "repo_root", "writes": {path: text}}` on stdin -- the staged + The script reads `{"event", "repo_root", "writes": {path: text}}` on stdin (plus `deleted` at the floor) -- the staged blobs at commit and push, the write itself at tool use and at the turn's end -- so one script serves every surface the declarative kinds do, and reads them the same way. It answers with an exit code: 0 allows; 1 refuses, 3 asks, 4 warns, with its own words on stderr. @@ -1100,9 +1134,13 @@ def _kind_script(ctx: GateContext, params: dict, event: str) -> GateResult: if not script.is_file(): return GateResult(allowed=False, message=f"script gate: {named!r} is not installed{_UNDECIDED}") writes = {path: ctx.staged_blob(path) for path in ctx.staged_paths()} - if not writes: + # Only the floor hands a script the deleted paths: the contract every other event has is unchanged. + deleted = sorted(ctx.staged_paths("D")) if event == FLOOR_EVENT else [] + if not writes and not deleted: return GateResult(allowed=True) material = {"event": event, "repo_root": str(ctx.repo_root), "writes": writes} + if event == FLOOR_EVENT: + material["deleted"] = deleted if ctx.session: material["session"] = ctx.session started = time.monotonic() diff --git a/.chock/bin/gemini_cli.py b/.chock/bin/gemini_cli.py index 964ae941..9a290d83 100755 --- a/.chock/bin/gemini_cli.py +++ b/.chock/bin/gemini_cli.py @@ -826,7 +826,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/bin/grok.py b/.chock/bin/grok.py index f262f001..39616f2a 100755 --- a/.chock/bin/grok.py +++ b/.chock/bin/grok.py @@ -826,7 +826,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/bin/index/claude_code-PreToolUse-185f9750.json b/.chock/bin/index/claude_code-PreToolUse-185f9750.json index a5aefb93..2f066730 100644 --- a/.chock/bin/index/claude_code-PreToolUse-185f9750.json +++ b/.chock/bin/index/claude_code-PreToolUse-185f9750.json @@ -14,7 +14,6 @@ ".chock/compiled/token-efficiency/agent-hooks/tool-call-gate.json": "0689304b5bacd25e9a371674d0faa8f9b8d9d35132d2faf7917c2b165a1a93fa", ".chock/compiled/token-efficiency/agent-hooks/tool-call-hooks.json": "7300181bc92f845f81e316813adc9eb6cfd58d8a94525126455203df7fb08e6a", ".chock/compiled/token-efficiency/ambient-rule/ambient.md": "6d94b590c79d340fe25c48dc119b41ece181c5c605b4a38a853e54b54f4e9156", - ".chock/compiled/token-efficiency/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/token-efficiency/pre-tool-use/codex_cli-tool-call-hooks.json": "a80d3ca758064cba6407f75213a659ec24321eddd85b2adc444cf9f367c5e741", ".chock/compiled/token-efficiency/pre-tool-use/cursor-post-hooks.json": "4348e99148692cafca9417a6dba71e01bda26a404733926415313428bb7f2d93", ".chock/compiled/token-efficiency/pre-tool-use/cursor-postfailure-hooks.json": "9c1e9f0f10255027115cfe5f74ac955be9f66f29973905f8eae319293285f2ab", diff --git a/.chock/bin/index/claude_code-PreToolUse-3a2134a7.json b/.chock/bin/index/claude_code-PreToolUse-3a2134a7.json index 6f3c0aa2..53fe6ac3 100644 --- a/.chock/bin/index/claude_code-PreToolUse-3a2134a7.json +++ b/.chock/bin/index/claude_code-PreToolUse-3a2134a7.json @@ -20,7 +20,6 @@ ".chock/compiled/block-destructive-commands/agent-hooks/agent-hooks.json": "166952d2add28842185387ca8602d36f511968c7e666e04c87708dc9dfe3054e", ".chock/compiled/block-destructive-commands/ambient-rule/ambient.md": "67f05c884bf417822000ebac578a7090a3ead6e07f0dea03c46eaf695214f190", ".chock/compiled/block-destructive-commands/git-hook/git-pre-push.sh": "ecb69d79165216412f646fb2ef8d53646a4c6d39fde77bfcc987adff7c76149e", - ".chock/compiled/block-destructive-commands/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-destructive-commands/pre-tool-use/antigravity-hooks.json": "9848d7c4f5be4c7ca30d401ceffe1ad4ec30775a0383552232aed9a91499b002", ".chock/compiled/block-destructive-commands/pre-tool-use/codex_cli-hooks.json": "02d8a824208df11dfd45e452e684e0779d8f164c4b6a8164bae9f9ca2cbf0865", ".chock/compiled/block-destructive-commands/pre-tool-use/cursor-hooks.json": "7091a6caea23d8794ef4e26af62e77d9f3a3bc3a38d8de9a941277e053ca1d97", @@ -55,7 +54,6 @@ ".agents/policies/block-no-verify/skills/block-no-verify/SKILL.md": "4e40b8cb58619728bdfc8dd4de2143c77bfa8c744f1f44d34904b88ae85ac023", ".chock/compiled/block-no-verify/agent-hooks/agent-hooks.json": "82b2e539bb812058a0c6cf3298ae9b72762eec28ede995ebe730b5d76f99c02b", ".chock/compiled/block-no-verify/ambient-rule/ambient.md": "371f90ab55eea13626e972c05883f2c1c427c9ca21a6d4aff23efa4513469431", - ".chock/compiled/block-no-verify/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-no-verify/pre-tool-use/antigravity-hooks.json": "1d5445dda7ade581edf55dc5e2d9adfde0c2ec9efd9cede936b8b99fd1fb0a9b", ".chock/compiled/block-no-verify/pre-tool-use/codex_cli-hooks.json": "a9df710f0b597cf83be795e5f7e68e0b9a641199765f4c7870de0be60b9ce8f0", ".chock/compiled/block-no-verify/pre-tool-use/cursor-hooks.json": "b7d84e25198f56302446e5a3738bfc1328a1e144c69d4d5b8cacdc83b9c829fa", @@ -92,7 +90,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -152,7 +149,6 @@ ".chock/compiled/protect-commit-privacy/agent-hooks/agent-hooks.json": "c532957fe3efd114386ced897aec8fffdfc41e8f281a39efb4620f4e6a7d2747", ".chock/compiled/protect-commit-privacy/ambient-rule/ambient.md": "0d747282d92f843a14974765e038a69a41c93c70e1e6e8100a6546d5b48865c4", ".chock/compiled/protect-commit-privacy/git-hook/git-commit-msg.sh": "c4d9679ba23a73ab2fad594b5a3c088fd1a29d2b2444483da6d08023e0fef98a", - ".chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-commit-privacy/pre-tool-use/antigravity-hooks.json": "762b527f13d0924e9fdd48b748d1027a1232a25225189e763cabcee208e9055d", ".chock/compiled/protect-commit-privacy/pre-tool-use/codex_cli-hooks.json": "85f228d8d50290015ef3204a73e9ddf3470929ed7fbb0f0ce8bd2d2eb8460f05", ".chock/compiled/protect-commit-privacy/pre-tool-use/cursor-hooks.json": "28d17b33eacbc0e331fc588ee9a129e66f9ad29414dad360066990d1c739e4db", diff --git a/.chock/bin/index/claude_code-PreToolUse-4acdeb9e.json b/.chock/bin/index/claude_code-PreToolUse-4acdeb9e.json index 2653147e..95ad602b 100644 --- a/.chock/bin/index/claude_code-PreToolUse-4acdeb9e.json +++ b/.chock/bin/index/claude_code-PreToolUse-4acdeb9e.json @@ -17,7 +17,6 @@ ".chock/compiled/block-invisible-unicode/ci-gate/step.yaml": "84e201486169ad4c016579f744c77b88b3b722628c01018d6afa7239fc9bec63", ".chock/compiled/block-invisible-unicode/git-hook/gate.json": "351fb35fffc9aba4882fe53ebbd3713b69aca6b4eb75798538a327b1cb641e1e", ".chock/compiled/block-invisible-unicode/git-hook/git-pre-commit.sh": "454c869cf2aa5123164f732ab28ffb79cfbf57c4e4a2b73597ef592bf11e59c6", - ".chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-invisible-unicode/mcp-gateway/gateway-gate.json": "83a400812189da3cb5ba7150d969698ab2400aa6c558348499f8745b5bf17ae0", ".chock/compiled/block-invisible-unicode/pre-tool-use/codex_cli-write-hooks.json": "fbad2af5a4a0e25f8a2704865641310f88042fe3c36ef3f5184b023d7b23af09", ".chock/compiled/block-invisible-unicode/pre-tool-use/cursor-write-hooks.json": "5a8a3215d75707c045b803c913edae75beaf9554cf4fa237d757ad773dfea2f4", @@ -61,7 +60,6 @@ ".chock/compiled/block-wildcard-agent-permissions/ci-gate/step.yaml": "f173704ceb69af71a971c081d7219f205fc176b5970dd4a32e429bbf4f6cd929", ".chock/compiled/block-wildcard-agent-permissions/git-hook/gate.json": "758636f7d17326652db0cac493c4fb59dcd7eaa922824a6e43bc49bfb616f762", ".chock/compiled/block-wildcard-agent-permissions/git-hook/git-pre-commit.sh": "8ad425c111c1dbf52a48ad2ba68b645d4d716befa8dfea575817ecea752421ea", - ".chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-wildcard-agent-permissions/mcp-gateway/gateway-gate.json": "9bf2c6b2447a2227f303b44afd50771db2cfa3a3ee40edc1280ac43209060c55", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/codex_cli-write-hooks.json": "8af817d29d040a474bbd5aa4c090c6084ab153f985882588e6b753ad6df1aad1", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/cursor-write-hooks.json": "691bdbeb577970ffdbc416b828781b74c39bcc9abe6eeda09e0d0069d0a04578", @@ -105,7 +103,6 @@ ".chock/compiled/pin-github-actions/ci-gate/step.yaml": "91ed82dfc00043421971fc4f480e800682fe9f8b8c0e6aa2cfecbb1e8de8b01d", ".chock/compiled/pin-github-actions/git-hook/gate.json": "65b5bc9554fa8852cf94ceaaced461755d7694c8b87b37fe7a8434d8642c666b", ".chock/compiled/pin-github-actions/git-hook/git-pre-commit.sh": "e6e47fcbbce3525c47b7ea69deee273ce95442ef3dac7bcd43a897b553769bcd", - ".chock/compiled/pin-github-actions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/pin-github-actions/mcp-gateway/gateway-gate.json": "308905df356acf7194c529bf0a418fd219e083ebd8545ce1aabd45c2dfbc2650", ".chock/compiled/pin-github-actions/pre-tool-use/codex_cli-write-hooks.json": "14238725b34a2e01cda395bc90e6ac5c914b4450b3b33ab81934df6324c1210d", ".chock/compiled/pin-github-actions/pre-tool-use/cursor-write-hooks.json": "68283be362683ad10a6a4542ecc4d59cb52bfcfb02483587262b5c5e6997d6e1", @@ -152,7 +149,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -209,7 +205,6 @@ ".chock/compiled/scan-secrets/ci-gate/step.yaml": "e2392603980559fbc57a1be4e0243cfa3d620fddbc6c8be7b2114fd4bbad9e07", ".chock/compiled/scan-secrets/git-hook/gate.json": "04d7a7705ced24c5c341ab4dc9806c3f488e2b1ac7a6c0b995e45b2e810cfd1b", ".chock/compiled/scan-secrets/git-hook/git-pre-commit.sh": "65c9db2f5b615a079cf92b63d3c739bcd4427ed4db33811b13d8922c43bb2b50", - ".chock/compiled/scan-secrets/managed-setting/managed-settings.json": "650436873f2a8da403515b6a205bf0006fbd2799dd8ff6c16a46b80847beafe5", ".chock/compiled/scan-secrets/mcp-gateway/gateway-gate.json": "520fd73b5d80023c68619e7ff56539c74ed0ef672ba8ce4686cf6f34972c7b25", ".chock/compiled/scan-secrets/pre-tool-use/codex_cli-write-hooks.json": "804cedc96a9809cb84f27e5d9f0da8989b32dc5173fdd37a35bfd204771a3edc", ".chock/compiled/scan-secrets/pre-tool-use/cursor-write-hooks.json": "a81dd0ba1d107bc551c51bbd7084a272c2a0b7bcc0e0b02411c36af15db60da8", diff --git a/.chock/bin/index/codex_cli-PreToolUse-14533f34.json b/.chock/bin/index/codex_cli-PreToolUse-14533f34.json index 6f3c0aa2..53fe6ac3 100644 --- a/.chock/bin/index/codex_cli-PreToolUse-14533f34.json +++ b/.chock/bin/index/codex_cli-PreToolUse-14533f34.json @@ -20,7 +20,6 @@ ".chock/compiled/block-destructive-commands/agent-hooks/agent-hooks.json": "166952d2add28842185387ca8602d36f511968c7e666e04c87708dc9dfe3054e", ".chock/compiled/block-destructive-commands/ambient-rule/ambient.md": "67f05c884bf417822000ebac578a7090a3ead6e07f0dea03c46eaf695214f190", ".chock/compiled/block-destructive-commands/git-hook/git-pre-push.sh": "ecb69d79165216412f646fb2ef8d53646a4c6d39fde77bfcc987adff7c76149e", - ".chock/compiled/block-destructive-commands/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-destructive-commands/pre-tool-use/antigravity-hooks.json": "9848d7c4f5be4c7ca30d401ceffe1ad4ec30775a0383552232aed9a91499b002", ".chock/compiled/block-destructive-commands/pre-tool-use/codex_cli-hooks.json": "02d8a824208df11dfd45e452e684e0779d8f164c4b6a8164bae9f9ca2cbf0865", ".chock/compiled/block-destructive-commands/pre-tool-use/cursor-hooks.json": "7091a6caea23d8794ef4e26af62e77d9f3a3bc3a38d8de9a941277e053ca1d97", @@ -55,7 +54,6 @@ ".agents/policies/block-no-verify/skills/block-no-verify/SKILL.md": "4e40b8cb58619728bdfc8dd4de2143c77bfa8c744f1f44d34904b88ae85ac023", ".chock/compiled/block-no-verify/agent-hooks/agent-hooks.json": "82b2e539bb812058a0c6cf3298ae9b72762eec28ede995ebe730b5d76f99c02b", ".chock/compiled/block-no-verify/ambient-rule/ambient.md": "371f90ab55eea13626e972c05883f2c1c427c9ca21a6d4aff23efa4513469431", - ".chock/compiled/block-no-verify/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-no-verify/pre-tool-use/antigravity-hooks.json": "1d5445dda7ade581edf55dc5e2d9adfde0c2ec9efd9cede936b8b99fd1fb0a9b", ".chock/compiled/block-no-verify/pre-tool-use/codex_cli-hooks.json": "a9df710f0b597cf83be795e5f7e68e0b9a641199765f4c7870de0be60b9ce8f0", ".chock/compiled/block-no-verify/pre-tool-use/cursor-hooks.json": "b7d84e25198f56302446e5a3738bfc1328a1e144c69d4d5b8cacdc83b9c829fa", @@ -92,7 +90,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -152,7 +149,6 @@ ".chock/compiled/protect-commit-privacy/agent-hooks/agent-hooks.json": "c532957fe3efd114386ced897aec8fffdfc41e8f281a39efb4620f4e6a7d2747", ".chock/compiled/protect-commit-privacy/ambient-rule/ambient.md": "0d747282d92f843a14974765e038a69a41c93c70e1e6e8100a6546d5b48865c4", ".chock/compiled/protect-commit-privacy/git-hook/git-commit-msg.sh": "c4d9679ba23a73ab2fad594b5a3c088fd1a29d2b2444483da6d08023e0fef98a", - ".chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-commit-privacy/pre-tool-use/antigravity-hooks.json": "762b527f13d0924e9fdd48b748d1027a1232a25225189e763cabcee208e9055d", ".chock/compiled/protect-commit-privacy/pre-tool-use/codex_cli-hooks.json": "85f228d8d50290015ef3204a73e9ddf3470929ed7fbb0f0ce8bd2d2eb8460f05", ".chock/compiled/protect-commit-privacy/pre-tool-use/cursor-hooks.json": "28d17b33eacbc0e331fc588ee9a129e66f9ad29414dad360066990d1c739e4db", diff --git a/.chock/bin/index/codex_cli-PreToolUse-186bd88e.json b/.chock/bin/index/codex_cli-PreToolUse-186bd88e.json index a5aefb93..2f066730 100644 --- a/.chock/bin/index/codex_cli-PreToolUse-186bd88e.json +++ b/.chock/bin/index/codex_cli-PreToolUse-186bd88e.json @@ -14,7 +14,6 @@ ".chock/compiled/token-efficiency/agent-hooks/tool-call-gate.json": "0689304b5bacd25e9a371674d0faa8f9b8d9d35132d2faf7917c2b165a1a93fa", ".chock/compiled/token-efficiency/agent-hooks/tool-call-hooks.json": "7300181bc92f845f81e316813adc9eb6cfd58d8a94525126455203df7fb08e6a", ".chock/compiled/token-efficiency/ambient-rule/ambient.md": "6d94b590c79d340fe25c48dc119b41ece181c5c605b4a38a853e54b54f4e9156", - ".chock/compiled/token-efficiency/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/token-efficiency/pre-tool-use/codex_cli-tool-call-hooks.json": "a80d3ca758064cba6407f75213a659ec24321eddd85b2adc444cf9f367c5e741", ".chock/compiled/token-efficiency/pre-tool-use/cursor-post-hooks.json": "4348e99148692cafca9417a6dba71e01bda26a404733926415313428bb7f2d93", ".chock/compiled/token-efficiency/pre-tool-use/cursor-postfailure-hooks.json": "9c1e9f0f10255027115cfe5f74ac955be9f66f29973905f8eae319293285f2ab", diff --git a/.chock/bin/index/codex_cli-PreToolUse-d2160b00.json b/.chock/bin/index/codex_cli-PreToolUse-d2160b00.json index 2653147e..95ad602b 100644 --- a/.chock/bin/index/codex_cli-PreToolUse-d2160b00.json +++ b/.chock/bin/index/codex_cli-PreToolUse-d2160b00.json @@ -17,7 +17,6 @@ ".chock/compiled/block-invisible-unicode/ci-gate/step.yaml": "84e201486169ad4c016579f744c77b88b3b722628c01018d6afa7239fc9bec63", ".chock/compiled/block-invisible-unicode/git-hook/gate.json": "351fb35fffc9aba4882fe53ebbd3713b69aca6b4eb75798538a327b1cb641e1e", ".chock/compiled/block-invisible-unicode/git-hook/git-pre-commit.sh": "454c869cf2aa5123164f732ab28ffb79cfbf57c4e4a2b73597ef592bf11e59c6", - ".chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-invisible-unicode/mcp-gateway/gateway-gate.json": "83a400812189da3cb5ba7150d969698ab2400aa6c558348499f8745b5bf17ae0", ".chock/compiled/block-invisible-unicode/pre-tool-use/codex_cli-write-hooks.json": "fbad2af5a4a0e25f8a2704865641310f88042fe3c36ef3f5184b023d7b23af09", ".chock/compiled/block-invisible-unicode/pre-tool-use/cursor-write-hooks.json": "5a8a3215d75707c045b803c913edae75beaf9554cf4fa237d757ad773dfea2f4", @@ -61,7 +60,6 @@ ".chock/compiled/block-wildcard-agent-permissions/ci-gate/step.yaml": "f173704ceb69af71a971c081d7219f205fc176b5970dd4a32e429bbf4f6cd929", ".chock/compiled/block-wildcard-agent-permissions/git-hook/gate.json": "758636f7d17326652db0cac493c4fb59dcd7eaa922824a6e43bc49bfb616f762", ".chock/compiled/block-wildcard-agent-permissions/git-hook/git-pre-commit.sh": "8ad425c111c1dbf52a48ad2ba68b645d4d716befa8dfea575817ecea752421ea", - ".chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-wildcard-agent-permissions/mcp-gateway/gateway-gate.json": "9bf2c6b2447a2227f303b44afd50771db2cfa3a3ee40edc1280ac43209060c55", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/codex_cli-write-hooks.json": "8af817d29d040a474bbd5aa4c090c6084ab153f985882588e6b753ad6df1aad1", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/cursor-write-hooks.json": "691bdbeb577970ffdbc416b828781b74c39bcc9abe6eeda09e0d0069d0a04578", @@ -105,7 +103,6 @@ ".chock/compiled/pin-github-actions/ci-gate/step.yaml": "91ed82dfc00043421971fc4f480e800682fe9f8b8c0e6aa2cfecbb1e8de8b01d", ".chock/compiled/pin-github-actions/git-hook/gate.json": "65b5bc9554fa8852cf94ceaaced461755d7694c8b87b37fe7a8434d8642c666b", ".chock/compiled/pin-github-actions/git-hook/git-pre-commit.sh": "e6e47fcbbce3525c47b7ea69deee273ce95442ef3dac7bcd43a897b553769bcd", - ".chock/compiled/pin-github-actions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/pin-github-actions/mcp-gateway/gateway-gate.json": "308905df356acf7194c529bf0a418fd219e083ebd8545ce1aabd45c2dfbc2650", ".chock/compiled/pin-github-actions/pre-tool-use/codex_cli-write-hooks.json": "14238725b34a2e01cda395bc90e6ac5c914b4450b3b33ab81934df6324c1210d", ".chock/compiled/pin-github-actions/pre-tool-use/cursor-write-hooks.json": "68283be362683ad10a6a4542ecc4d59cb52bfcfb02483587262b5c5e6997d6e1", @@ -152,7 +149,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -209,7 +205,6 @@ ".chock/compiled/scan-secrets/ci-gate/step.yaml": "e2392603980559fbc57a1be4e0243cfa3d620fddbc6c8be7b2114fd4bbad9e07", ".chock/compiled/scan-secrets/git-hook/gate.json": "04d7a7705ced24c5c341ab4dc9806c3f488e2b1ac7a6c0b995e45b2e810cfd1b", ".chock/compiled/scan-secrets/git-hook/git-pre-commit.sh": "65c9db2f5b615a079cf92b63d3c739bcd4427ed4db33811b13d8922c43bb2b50", - ".chock/compiled/scan-secrets/managed-setting/managed-settings.json": "650436873f2a8da403515b6a205bf0006fbd2799dd8ff6c16a46b80847beafe5", ".chock/compiled/scan-secrets/mcp-gateway/gateway-gate.json": "520fd73b5d80023c68619e7ff56539c74ed0ef672ba8ce4686cf6f34972c7b25", ".chock/compiled/scan-secrets/pre-tool-use/codex_cli-write-hooks.json": "804cedc96a9809cb84f27e5d9f0da8989b32dc5173fdd37a35bfd204771a3edc", ".chock/compiled/scan-secrets/pre-tool-use/cursor-write-hooks.json": "a81dd0ba1d107bc551c51bbd7084a272c2a0b7bcc0e0b02411c36af15db60da8", diff --git a/.chock/bin/index/cursor-beforeShellExecution-b6c825df.json b/.chock/bin/index/cursor-beforeShellExecution-b6c825df.json index 6f3c0aa2..53fe6ac3 100644 --- a/.chock/bin/index/cursor-beforeShellExecution-b6c825df.json +++ b/.chock/bin/index/cursor-beforeShellExecution-b6c825df.json @@ -20,7 +20,6 @@ ".chock/compiled/block-destructive-commands/agent-hooks/agent-hooks.json": "166952d2add28842185387ca8602d36f511968c7e666e04c87708dc9dfe3054e", ".chock/compiled/block-destructive-commands/ambient-rule/ambient.md": "67f05c884bf417822000ebac578a7090a3ead6e07f0dea03c46eaf695214f190", ".chock/compiled/block-destructive-commands/git-hook/git-pre-push.sh": "ecb69d79165216412f646fb2ef8d53646a4c6d39fde77bfcc987adff7c76149e", - ".chock/compiled/block-destructive-commands/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-destructive-commands/pre-tool-use/antigravity-hooks.json": "9848d7c4f5be4c7ca30d401ceffe1ad4ec30775a0383552232aed9a91499b002", ".chock/compiled/block-destructive-commands/pre-tool-use/codex_cli-hooks.json": "02d8a824208df11dfd45e452e684e0779d8f164c4b6a8164bae9f9ca2cbf0865", ".chock/compiled/block-destructive-commands/pre-tool-use/cursor-hooks.json": "7091a6caea23d8794ef4e26af62e77d9f3a3bc3a38d8de9a941277e053ca1d97", @@ -55,7 +54,6 @@ ".agents/policies/block-no-verify/skills/block-no-verify/SKILL.md": "4e40b8cb58619728bdfc8dd4de2143c77bfa8c744f1f44d34904b88ae85ac023", ".chock/compiled/block-no-verify/agent-hooks/agent-hooks.json": "82b2e539bb812058a0c6cf3298ae9b72762eec28ede995ebe730b5d76f99c02b", ".chock/compiled/block-no-verify/ambient-rule/ambient.md": "371f90ab55eea13626e972c05883f2c1c427c9ca21a6d4aff23efa4513469431", - ".chock/compiled/block-no-verify/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-no-verify/pre-tool-use/antigravity-hooks.json": "1d5445dda7ade581edf55dc5e2d9adfde0c2ec9efd9cede936b8b99fd1fb0a9b", ".chock/compiled/block-no-verify/pre-tool-use/codex_cli-hooks.json": "a9df710f0b597cf83be795e5f7e68e0b9a641199765f4c7870de0be60b9ce8f0", ".chock/compiled/block-no-verify/pre-tool-use/cursor-hooks.json": "b7d84e25198f56302446e5a3738bfc1328a1e144c69d4d5b8cacdc83b9c829fa", @@ -92,7 +90,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -152,7 +149,6 @@ ".chock/compiled/protect-commit-privacy/agent-hooks/agent-hooks.json": "c532957fe3efd114386ced897aec8fffdfc41e8f281a39efb4620f4e6a7d2747", ".chock/compiled/protect-commit-privacy/ambient-rule/ambient.md": "0d747282d92f843a14974765e038a69a41c93c70e1e6e8100a6546d5b48865c4", ".chock/compiled/protect-commit-privacy/git-hook/git-commit-msg.sh": "c4d9679ba23a73ab2fad594b5a3c088fd1a29d2b2444483da6d08023e0fef98a", - ".chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-commit-privacy/pre-tool-use/antigravity-hooks.json": "762b527f13d0924e9fdd48b748d1027a1232a25225189e763cabcee208e9055d", ".chock/compiled/protect-commit-privacy/pre-tool-use/codex_cli-hooks.json": "85f228d8d50290015ef3204a73e9ddf3470929ed7fbb0f0ce8bd2d2eb8460f05", ".chock/compiled/protect-commit-privacy/pre-tool-use/cursor-hooks.json": "28d17b33eacbc0e331fc588ee9a129e66f9ad29414dad360066990d1c739e4db", diff --git a/.chock/bin/index/cursor-preToolUse-b6c825df.json b/.chock/bin/index/cursor-preToolUse-b6c825df.json index ee5b6120..e2017f6d 100644 --- a/.chock/bin/index/cursor-preToolUse-b6c825df.json +++ b/.chock/bin/index/cursor-preToolUse-b6c825df.json @@ -17,7 +17,6 @@ ".chock/compiled/block-invisible-unicode/ci-gate/step.yaml": "84e201486169ad4c016579f744c77b88b3b722628c01018d6afa7239fc9bec63", ".chock/compiled/block-invisible-unicode/git-hook/gate.json": "351fb35fffc9aba4882fe53ebbd3713b69aca6b4eb75798538a327b1cb641e1e", ".chock/compiled/block-invisible-unicode/git-hook/git-pre-commit.sh": "454c869cf2aa5123164f732ab28ffb79cfbf57c4e4a2b73597ef592bf11e59c6", - ".chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-invisible-unicode/mcp-gateway/gateway-gate.json": "83a400812189da3cb5ba7150d969698ab2400aa6c558348499f8745b5bf17ae0", ".chock/compiled/block-invisible-unicode/pre-tool-use/codex_cli-write-hooks.json": "fbad2af5a4a0e25f8a2704865641310f88042fe3c36ef3f5184b023d7b23af09", ".chock/compiled/block-invisible-unicode/pre-tool-use/cursor-write-hooks.json": "5a8a3215d75707c045b803c913edae75beaf9554cf4fa237d757ad773dfea2f4", @@ -61,7 +60,6 @@ ".chock/compiled/block-wildcard-agent-permissions/ci-gate/step.yaml": "f173704ceb69af71a971c081d7219f205fc176b5970dd4a32e429bbf4f6cd929", ".chock/compiled/block-wildcard-agent-permissions/git-hook/gate.json": "758636f7d17326652db0cac493c4fb59dcd7eaa922824a6e43bc49bfb616f762", ".chock/compiled/block-wildcard-agent-permissions/git-hook/git-pre-commit.sh": "8ad425c111c1dbf52a48ad2ba68b645d4d716befa8dfea575817ecea752421ea", - ".chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-wildcard-agent-permissions/mcp-gateway/gateway-gate.json": "9bf2c6b2447a2227f303b44afd50771db2cfa3a3ee40edc1280ac43209060c55", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/codex_cli-write-hooks.json": "8af817d29d040a474bbd5aa4c090c6084ab153f985882588e6b753ad6df1aad1", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/cursor-write-hooks.json": "691bdbeb577970ffdbc416b828781b74c39bcc9abe6eeda09e0d0069d0a04578", @@ -105,7 +103,6 @@ ".chock/compiled/pin-github-actions/ci-gate/step.yaml": "91ed82dfc00043421971fc4f480e800682fe9f8b8c0e6aa2cfecbb1e8de8b01d", ".chock/compiled/pin-github-actions/git-hook/gate.json": "65b5bc9554fa8852cf94ceaaced461755d7694c8b87b37fe7a8434d8642c666b", ".chock/compiled/pin-github-actions/git-hook/git-pre-commit.sh": "e6e47fcbbce3525c47b7ea69deee273ce95442ef3dac7bcd43a897b553769bcd", - ".chock/compiled/pin-github-actions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/pin-github-actions/mcp-gateway/gateway-gate.json": "308905df356acf7194c529bf0a418fd219e083ebd8545ce1aabd45c2dfbc2650", ".chock/compiled/pin-github-actions/pre-tool-use/codex_cli-write-hooks.json": "14238725b34a2e01cda395bc90e6ac5c914b4450b3b33ab81934df6324c1210d", ".chock/compiled/pin-github-actions/pre-tool-use/cursor-write-hooks.json": "68283be362683ad10a6a4542ecc4d59cb52bfcfb02483587262b5c5e6997d6e1", @@ -152,7 +149,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -209,7 +205,6 @@ ".chock/compiled/scan-secrets/ci-gate/step.yaml": "e2392603980559fbc57a1be4e0243cfa3d620fddbc6c8be7b2114fd4bbad9e07", ".chock/compiled/scan-secrets/git-hook/gate.json": "04d7a7705ced24c5c341ab4dc9806c3f488e2b1ac7a6c0b995e45b2e810cfd1b", ".chock/compiled/scan-secrets/git-hook/git-pre-commit.sh": "65c9db2f5b615a079cf92b63d3c739bcd4427ed4db33811b13d8922c43bb2b50", - ".chock/compiled/scan-secrets/managed-setting/managed-settings.json": "650436873f2a8da403515b6a205bf0006fbd2799dd8ff6c16a46b80847beafe5", ".chock/compiled/scan-secrets/mcp-gateway/gateway-gate.json": "520fd73b5d80023c68619e7ff56539c74ed0ef672ba8ce4686cf6f34972c7b25", ".chock/compiled/scan-secrets/pre-tool-use/codex_cli-write-hooks.json": "804cedc96a9809cb84f27e5d9f0da8989b32dc5173fdd37a35bfd204771a3edc", ".chock/compiled/scan-secrets/pre-tool-use/cursor-write-hooks.json": "a81dd0ba1d107bc551c51bbd7084a272c2a0b7bcc0e0b02411c36af15db60da8", @@ -250,7 +245,6 @@ ".chock/compiled/token-efficiency/agent-hooks/tool-call-gate.json": "0689304b5bacd25e9a371674d0faa8f9b8d9d35132d2faf7917c2b165a1a93fa", ".chock/compiled/token-efficiency/agent-hooks/tool-call-hooks.json": "7300181bc92f845f81e316813adc9eb6cfd58d8a94525126455203df7fb08e6a", ".chock/compiled/token-efficiency/ambient-rule/ambient.md": "6d94b590c79d340fe25c48dc119b41ece181c5c605b4a38a853e54b54f4e9156", - ".chock/compiled/token-efficiency/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/token-efficiency/pre-tool-use/codex_cli-tool-call-hooks.json": "a80d3ca758064cba6407f75213a659ec24321eddd85b2adc444cf9f367c5e741", ".chock/compiled/token-efficiency/pre-tool-use/cursor-post-hooks.json": "4348e99148692cafca9417a6dba71e01bda26a404733926415313428bb7f2d93", ".chock/compiled/token-efficiency/pre-tool-use/cursor-postfailure-hooks.json": "9c1e9f0f10255027115cfe5f74ac955be9f66f29973905f8eae319293285f2ab", diff --git a/.chock/bin/index/devin-PreToolUse-185f9750.json b/.chock/bin/index/devin-PreToolUse-185f9750.json index 6f3c0aa2..53fe6ac3 100644 --- a/.chock/bin/index/devin-PreToolUse-185f9750.json +++ b/.chock/bin/index/devin-PreToolUse-185f9750.json @@ -20,7 +20,6 @@ ".chock/compiled/block-destructive-commands/agent-hooks/agent-hooks.json": "166952d2add28842185387ca8602d36f511968c7e666e04c87708dc9dfe3054e", ".chock/compiled/block-destructive-commands/ambient-rule/ambient.md": "67f05c884bf417822000ebac578a7090a3ead6e07f0dea03c46eaf695214f190", ".chock/compiled/block-destructive-commands/git-hook/git-pre-push.sh": "ecb69d79165216412f646fb2ef8d53646a4c6d39fde77bfcc987adff7c76149e", - ".chock/compiled/block-destructive-commands/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-destructive-commands/pre-tool-use/antigravity-hooks.json": "9848d7c4f5be4c7ca30d401ceffe1ad4ec30775a0383552232aed9a91499b002", ".chock/compiled/block-destructive-commands/pre-tool-use/codex_cli-hooks.json": "02d8a824208df11dfd45e452e684e0779d8f164c4b6a8164bae9f9ca2cbf0865", ".chock/compiled/block-destructive-commands/pre-tool-use/cursor-hooks.json": "7091a6caea23d8794ef4e26af62e77d9f3a3bc3a38d8de9a941277e053ca1d97", @@ -55,7 +54,6 @@ ".agents/policies/block-no-verify/skills/block-no-verify/SKILL.md": "4e40b8cb58619728bdfc8dd4de2143c77bfa8c744f1f44d34904b88ae85ac023", ".chock/compiled/block-no-verify/agent-hooks/agent-hooks.json": "82b2e539bb812058a0c6cf3298ae9b72762eec28ede995ebe730b5d76f99c02b", ".chock/compiled/block-no-verify/ambient-rule/ambient.md": "371f90ab55eea13626e972c05883f2c1c427c9ca21a6d4aff23efa4513469431", - ".chock/compiled/block-no-verify/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-no-verify/pre-tool-use/antigravity-hooks.json": "1d5445dda7ade581edf55dc5e2d9adfde0c2ec9efd9cede936b8b99fd1fb0a9b", ".chock/compiled/block-no-verify/pre-tool-use/codex_cli-hooks.json": "a9df710f0b597cf83be795e5f7e68e0b9a641199765f4c7870de0be60b9ce8f0", ".chock/compiled/block-no-verify/pre-tool-use/cursor-hooks.json": "b7d84e25198f56302446e5a3738bfc1328a1e144c69d4d5b8cacdc83b9c829fa", @@ -92,7 +90,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -152,7 +149,6 @@ ".chock/compiled/protect-commit-privacy/agent-hooks/agent-hooks.json": "c532957fe3efd114386ced897aec8fffdfc41e8f281a39efb4620f4e6a7d2747", ".chock/compiled/protect-commit-privacy/ambient-rule/ambient.md": "0d747282d92f843a14974765e038a69a41c93c70e1e6e8100a6546d5b48865c4", ".chock/compiled/protect-commit-privacy/git-hook/git-commit-msg.sh": "c4d9679ba23a73ab2fad594b5a3c088fd1a29d2b2444483da6d08023e0fef98a", - ".chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-commit-privacy/pre-tool-use/antigravity-hooks.json": "762b527f13d0924e9fdd48b748d1027a1232a25225189e763cabcee208e9055d", ".chock/compiled/protect-commit-privacy/pre-tool-use/codex_cli-hooks.json": "85f228d8d50290015ef3204a73e9ddf3470929ed7fbb0f0ce8bd2d2eb8460f05", ".chock/compiled/protect-commit-privacy/pre-tool-use/cursor-hooks.json": "28d17b33eacbc0e331fc588ee9a129e66f9ad29414dad360066990d1c739e4db", diff --git a/.chock/bin/index/gemini_cli-BeforeTool-09cc5246.json b/.chock/bin/index/gemini_cli-BeforeTool-09cc5246.json index 6f3c0aa2..53fe6ac3 100644 --- a/.chock/bin/index/gemini_cli-BeforeTool-09cc5246.json +++ b/.chock/bin/index/gemini_cli-BeforeTool-09cc5246.json @@ -20,7 +20,6 @@ ".chock/compiled/block-destructive-commands/agent-hooks/agent-hooks.json": "166952d2add28842185387ca8602d36f511968c7e666e04c87708dc9dfe3054e", ".chock/compiled/block-destructive-commands/ambient-rule/ambient.md": "67f05c884bf417822000ebac578a7090a3ead6e07f0dea03c46eaf695214f190", ".chock/compiled/block-destructive-commands/git-hook/git-pre-push.sh": "ecb69d79165216412f646fb2ef8d53646a4c6d39fde77bfcc987adff7c76149e", - ".chock/compiled/block-destructive-commands/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-destructive-commands/pre-tool-use/antigravity-hooks.json": "9848d7c4f5be4c7ca30d401ceffe1ad4ec30775a0383552232aed9a91499b002", ".chock/compiled/block-destructive-commands/pre-tool-use/codex_cli-hooks.json": "02d8a824208df11dfd45e452e684e0779d8f164c4b6a8164bae9f9ca2cbf0865", ".chock/compiled/block-destructive-commands/pre-tool-use/cursor-hooks.json": "7091a6caea23d8794ef4e26af62e77d9f3a3bc3a38d8de9a941277e053ca1d97", @@ -55,7 +54,6 @@ ".agents/policies/block-no-verify/skills/block-no-verify/SKILL.md": "4e40b8cb58619728bdfc8dd4de2143c77bfa8c744f1f44d34904b88ae85ac023", ".chock/compiled/block-no-verify/agent-hooks/agent-hooks.json": "82b2e539bb812058a0c6cf3298ae9b72762eec28ede995ebe730b5d76f99c02b", ".chock/compiled/block-no-verify/ambient-rule/ambient.md": "371f90ab55eea13626e972c05883f2c1c427c9ca21a6d4aff23efa4513469431", - ".chock/compiled/block-no-verify/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-no-verify/pre-tool-use/antigravity-hooks.json": "1d5445dda7ade581edf55dc5e2d9adfde0c2ec9efd9cede936b8b99fd1fb0a9b", ".chock/compiled/block-no-verify/pre-tool-use/codex_cli-hooks.json": "a9df710f0b597cf83be795e5f7e68e0b9a641199765f4c7870de0be60b9ce8f0", ".chock/compiled/block-no-verify/pre-tool-use/cursor-hooks.json": "b7d84e25198f56302446e5a3738bfc1328a1e144c69d4d5b8cacdc83b9c829fa", @@ -92,7 +90,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -152,7 +149,6 @@ ".chock/compiled/protect-commit-privacy/agent-hooks/agent-hooks.json": "c532957fe3efd114386ced897aec8fffdfc41e8f281a39efb4620f4e6a7d2747", ".chock/compiled/protect-commit-privacy/ambient-rule/ambient.md": "0d747282d92f843a14974765e038a69a41c93c70e1e6e8100a6546d5b48865c4", ".chock/compiled/protect-commit-privacy/git-hook/git-commit-msg.sh": "c4d9679ba23a73ab2fad594b5a3c088fd1a29d2b2444483da6d08023e0fef98a", - ".chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-commit-privacy/pre-tool-use/antigravity-hooks.json": "762b527f13d0924e9fdd48b748d1027a1232a25225189e763cabcee208e9055d", ".chock/compiled/protect-commit-privacy/pre-tool-use/codex_cli-hooks.json": "85f228d8d50290015ef3204a73e9ddf3470929ed7fbb0f0ce8bd2d2eb8460f05", ".chock/compiled/protect-commit-privacy/pre-tool-use/cursor-hooks.json": "28d17b33eacbc0e331fc588ee9a129e66f9ad29414dad360066990d1c739e4db", diff --git a/.chock/bin/index/gemini_cli-BeforeTool-c8f6602b.json b/.chock/bin/index/gemini_cli-BeforeTool-c8f6602b.json index a5aefb93..2f066730 100644 --- a/.chock/bin/index/gemini_cli-BeforeTool-c8f6602b.json +++ b/.chock/bin/index/gemini_cli-BeforeTool-c8f6602b.json @@ -14,7 +14,6 @@ ".chock/compiled/token-efficiency/agent-hooks/tool-call-gate.json": "0689304b5bacd25e9a371674d0faa8f9b8d9d35132d2faf7917c2b165a1a93fa", ".chock/compiled/token-efficiency/agent-hooks/tool-call-hooks.json": "7300181bc92f845f81e316813adc9eb6cfd58d8a94525126455203df7fb08e6a", ".chock/compiled/token-efficiency/ambient-rule/ambient.md": "6d94b590c79d340fe25c48dc119b41ece181c5c605b4a38a853e54b54f4e9156", - ".chock/compiled/token-efficiency/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/token-efficiency/pre-tool-use/codex_cli-tool-call-hooks.json": "a80d3ca758064cba6407f75213a659ec24321eddd85b2adc444cf9f367c5e741", ".chock/compiled/token-efficiency/pre-tool-use/cursor-post-hooks.json": "4348e99148692cafca9417a6dba71e01bda26a404733926415313428bb7f2d93", ".chock/compiled/token-efficiency/pre-tool-use/cursor-postfailure-hooks.json": "9c1e9f0f10255027115cfe5f74ac955be9f66f29973905f8eae319293285f2ab", diff --git a/.chock/bin/index/gemini_cli-BeforeTool-e86eddda.json b/.chock/bin/index/gemini_cli-BeforeTool-e86eddda.json index 2653147e..95ad602b 100644 --- a/.chock/bin/index/gemini_cli-BeforeTool-e86eddda.json +++ b/.chock/bin/index/gemini_cli-BeforeTool-e86eddda.json @@ -17,7 +17,6 @@ ".chock/compiled/block-invisible-unicode/ci-gate/step.yaml": "84e201486169ad4c016579f744c77b88b3b722628c01018d6afa7239fc9bec63", ".chock/compiled/block-invisible-unicode/git-hook/gate.json": "351fb35fffc9aba4882fe53ebbd3713b69aca6b4eb75798538a327b1cb641e1e", ".chock/compiled/block-invisible-unicode/git-hook/git-pre-commit.sh": "454c869cf2aa5123164f732ab28ffb79cfbf57c4e4a2b73597ef592bf11e59c6", - ".chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-invisible-unicode/mcp-gateway/gateway-gate.json": "83a400812189da3cb5ba7150d969698ab2400aa6c558348499f8745b5bf17ae0", ".chock/compiled/block-invisible-unicode/pre-tool-use/codex_cli-write-hooks.json": "fbad2af5a4a0e25f8a2704865641310f88042fe3c36ef3f5184b023d7b23af09", ".chock/compiled/block-invisible-unicode/pre-tool-use/cursor-write-hooks.json": "5a8a3215d75707c045b803c913edae75beaf9554cf4fa237d757ad773dfea2f4", @@ -61,7 +60,6 @@ ".chock/compiled/block-wildcard-agent-permissions/ci-gate/step.yaml": "f173704ceb69af71a971c081d7219f205fc176b5970dd4a32e429bbf4f6cd929", ".chock/compiled/block-wildcard-agent-permissions/git-hook/gate.json": "758636f7d17326652db0cac493c4fb59dcd7eaa922824a6e43bc49bfb616f762", ".chock/compiled/block-wildcard-agent-permissions/git-hook/git-pre-commit.sh": "8ad425c111c1dbf52a48ad2ba68b645d4d716befa8dfea575817ecea752421ea", - ".chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-wildcard-agent-permissions/mcp-gateway/gateway-gate.json": "9bf2c6b2447a2227f303b44afd50771db2cfa3a3ee40edc1280ac43209060c55", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/codex_cli-write-hooks.json": "8af817d29d040a474bbd5aa4c090c6084ab153f985882588e6b753ad6df1aad1", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/cursor-write-hooks.json": "691bdbeb577970ffdbc416b828781b74c39bcc9abe6eeda09e0d0069d0a04578", @@ -105,7 +103,6 @@ ".chock/compiled/pin-github-actions/ci-gate/step.yaml": "91ed82dfc00043421971fc4f480e800682fe9f8b8c0e6aa2cfecbb1e8de8b01d", ".chock/compiled/pin-github-actions/git-hook/gate.json": "65b5bc9554fa8852cf94ceaaced461755d7694c8b87b37fe7a8434d8642c666b", ".chock/compiled/pin-github-actions/git-hook/git-pre-commit.sh": "e6e47fcbbce3525c47b7ea69deee273ce95442ef3dac7bcd43a897b553769bcd", - ".chock/compiled/pin-github-actions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/pin-github-actions/mcp-gateway/gateway-gate.json": "308905df356acf7194c529bf0a418fd219e083ebd8545ce1aabd45c2dfbc2650", ".chock/compiled/pin-github-actions/pre-tool-use/codex_cli-write-hooks.json": "14238725b34a2e01cda395bc90e6ac5c914b4450b3b33ab81934df6324c1210d", ".chock/compiled/pin-github-actions/pre-tool-use/cursor-write-hooks.json": "68283be362683ad10a6a4542ecc4d59cb52bfcfb02483587262b5c5e6997d6e1", @@ -152,7 +149,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -209,7 +205,6 @@ ".chock/compiled/scan-secrets/ci-gate/step.yaml": "e2392603980559fbc57a1be4e0243cfa3d620fddbc6c8be7b2114fd4bbad9e07", ".chock/compiled/scan-secrets/git-hook/gate.json": "04d7a7705ced24c5c341ab4dc9806c3f488e2b1ac7a6c0b995e45b2e810cfd1b", ".chock/compiled/scan-secrets/git-hook/git-pre-commit.sh": "65c9db2f5b615a079cf92b63d3c739bcd4427ed4db33811b13d8922c43bb2b50", - ".chock/compiled/scan-secrets/managed-setting/managed-settings.json": "650436873f2a8da403515b6a205bf0006fbd2799dd8ff6c16a46b80847beafe5", ".chock/compiled/scan-secrets/mcp-gateway/gateway-gate.json": "520fd73b5d80023c68619e7ff56539c74ed0ef672ba8ce4686cf6f34972c7b25", ".chock/compiled/scan-secrets/pre-tool-use/codex_cli-write-hooks.json": "804cedc96a9809cb84f27e5d9f0da8989b32dc5173fdd37a35bfd204771a3edc", ".chock/compiled/scan-secrets/pre-tool-use/cursor-write-hooks.json": "a81dd0ba1d107bc551c51bbd7084a272c2a0b7bcc0e0b02411c36af15db60da8", diff --git a/.chock/bin/index/grok-PreToolUse-185f9750.json b/.chock/bin/index/grok-PreToolUse-185f9750.json index 6f3c0aa2..53fe6ac3 100644 --- a/.chock/bin/index/grok-PreToolUse-185f9750.json +++ b/.chock/bin/index/grok-PreToolUse-185f9750.json @@ -20,7 +20,6 @@ ".chock/compiled/block-destructive-commands/agent-hooks/agent-hooks.json": "166952d2add28842185387ca8602d36f511968c7e666e04c87708dc9dfe3054e", ".chock/compiled/block-destructive-commands/ambient-rule/ambient.md": "67f05c884bf417822000ebac578a7090a3ead6e07f0dea03c46eaf695214f190", ".chock/compiled/block-destructive-commands/git-hook/git-pre-push.sh": "ecb69d79165216412f646fb2ef8d53646a4c6d39fde77bfcc987adff7c76149e", - ".chock/compiled/block-destructive-commands/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-destructive-commands/pre-tool-use/antigravity-hooks.json": "9848d7c4f5be4c7ca30d401ceffe1ad4ec30775a0383552232aed9a91499b002", ".chock/compiled/block-destructive-commands/pre-tool-use/codex_cli-hooks.json": "02d8a824208df11dfd45e452e684e0779d8f164c4b6a8164bae9f9ca2cbf0865", ".chock/compiled/block-destructive-commands/pre-tool-use/cursor-hooks.json": "7091a6caea23d8794ef4e26af62e77d9f3a3bc3a38d8de9a941277e053ca1d97", @@ -55,7 +54,6 @@ ".agents/policies/block-no-verify/skills/block-no-verify/SKILL.md": "4e40b8cb58619728bdfc8dd4de2143c77bfa8c744f1f44d34904b88ae85ac023", ".chock/compiled/block-no-verify/agent-hooks/agent-hooks.json": "82b2e539bb812058a0c6cf3298ae9b72762eec28ede995ebe730b5d76f99c02b", ".chock/compiled/block-no-verify/ambient-rule/ambient.md": "371f90ab55eea13626e972c05883f2c1c427c9ca21a6d4aff23efa4513469431", - ".chock/compiled/block-no-verify/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-no-verify/pre-tool-use/antigravity-hooks.json": "1d5445dda7ade581edf55dc5e2d9adfde0c2ec9efd9cede936b8b99fd1fb0a9b", ".chock/compiled/block-no-verify/pre-tool-use/codex_cli-hooks.json": "a9df710f0b597cf83be795e5f7e68e0b9a641199765f4c7870de0be60b9ce8f0", ".chock/compiled/block-no-verify/pre-tool-use/cursor-hooks.json": "b7d84e25198f56302446e5a3738bfc1328a1e144c69d4d5b8cacdc83b9c829fa", @@ -92,7 +90,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -152,7 +149,6 @@ ".chock/compiled/protect-commit-privacy/agent-hooks/agent-hooks.json": "c532957fe3efd114386ced897aec8fffdfc41e8f281a39efb4620f4e6a7d2747", ".chock/compiled/protect-commit-privacy/ambient-rule/ambient.md": "0d747282d92f843a14974765e038a69a41c93c70e1e6e8100a6546d5b48865c4", ".chock/compiled/protect-commit-privacy/git-hook/git-commit-msg.sh": "c4d9679ba23a73ab2fad594b5a3c088fd1a29d2b2444483da6d08023e0fef98a", - ".chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-commit-privacy/pre-tool-use/antigravity-hooks.json": "762b527f13d0924e9fdd48b748d1027a1232a25225189e763cabcee208e9055d", ".chock/compiled/protect-commit-privacy/pre-tool-use/codex_cli-hooks.json": "85f228d8d50290015ef3204a73e9ddf3470929ed7fbb0f0ce8bd2d2eb8460f05", ".chock/compiled/protect-commit-privacy/pre-tool-use/cursor-hooks.json": "28d17b33eacbc0e331fc588ee9a129e66f9ad29414dad360066990d1c739e4db", diff --git a/.chock/bin/index/tabnine-BeforeTool-c8f6602b.json b/.chock/bin/index/tabnine-BeforeTool-c8f6602b.json index 6f3c0aa2..53fe6ac3 100644 --- a/.chock/bin/index/tabnine-BeforeTool-c8f6602b.json +++ b/.chock/bin/index/tabnine-BeforeTool-c8f6602b.json @@ -20,7 +20,6 @@ ".chock/compiled/block-destructive-commands/agent-hooks/agent-hooks.json": "166952d2add28842185387ca8602d36f511968c7e666e04c87708dc9dfe3054e", ".chock/compiled/block-destructive-commands/ambient-rule/ambient.md": "67f05c884bf417822000ebac578a7090a3ead6e07f0dea03c46eaf695214f190", ".chock/compiled/block-destructive-commands/git-hook/git-pre-push.sh": "ecb69d79165216412f646fb2ef8d53646a4c6d39fde77bfcc987adff7c76149e", - ".chock/compiled/block-destructive-commands/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-destructive-commands/pre-tool-use/antigravity-hooks.json": "9848d7c4f5be4c7ca30d401ceffe1ad4ec30775a0383552232aed9a91499b002", ".chock/compiled/block-destructive-commands/pre-tool-use/codex_cli-hooks.json": "02d8a824208df11dfd45e452e684e0779d8f164c4b6a8164bae9f9ca2cbf0865", ".chock/compiled/block-destructive-commands/pre-tool-use/cursor-hooks.json": "7091a6caea23d8794ef4e26af62e77d9f3a3bc3a38d8de9a941277e053ca1d97", @@ -55,7 +54,6 @@ ".agents/policies/block-no-verify/skills/block-no-verify/SKILL.md": "4e40b8cb58619728bdfc8dd4de2143c77bfa8c744f1f44d34904b88ae85ac023", ".chock/compiled/block-no-verify/agent-hooks/agent-hooks.json": "82b2e539bb812058a0c6cf3298ae9b72762eec28ede995ebe730b5d76f99c02b", ".chock/compiled/block-no-verify/ambient-rule/ambient.md": "371f90ab55eea13626e972c05883f2c1c427c9ca21a6d4aff23efa4513469431", - ".chock/compiled/block-no-verify/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-no-verify/pre-tool-use/antigravity-hooks.json": "1d5445dda7ade581edf55dc5e2d9adfde0c2ec9efd9cede936b8b99fd1fb0a9b", ".chock/compiled/block-no-verify/pre-tool-use/codex_cli-hooks.json": "a9df710f0b597cf83be795e5f7e68e0b9a641199765f4c7870de0be60b9ce8f0", ".chock/compiled/block-no-verify/pre-tool-use/cursor-hooks.json": "b7d84e25198f56302446e5a3738bfc1328a1e144c69d4d5b8cacdc83b9c829fa", @@ -92,7 +90,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -152,7 +149,6 @@ ".chock/compiled/protect-commit-privacy/agent-hooks/agent-hooks.json": "c532957fe3efd114386ced897aec8fffdfc41e8f281a39efb4620f4e6a7d2747", ".chock/compiled/protect-commit-privacy/ambient-rule/ambient.md": "0d747282d92f843a14974765e038a69a41c93c70e1e6e8100a6546d5b48865c4", ".chock/compiled/protect-commit-privacy/git-hook/git-commit-msg.sh": "c4d9679ba23a73ab2fad594b5a3c088fd1a29d2b2444483da6d08023e0fef98a", - ".chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-commit-privacy/pre-tool-use/antigravity-hooks.json": "762b527f13d0924e9fdd48b748d1027a1232a25225189e763cabcee208e9055d", ".chock/compiled/protect-commit-privacy/pre-tool-use/codex_cli-hooks.json": "85f228d8d50290015ef3204a73e9ddf3470929ed7fbb0f0ce8bd2d2eb8460f05", ".chock/compiled/protect-commit-privacy/pre-tool-use/cursor-hooks.json": "28d17b33eacbc0e331fc588ee9a129e66f9ad29414dad360066990d1c739e4db", diff --git a/.chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json b/.chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json index 9df57bb7..2c3be7d4 100644 --- a/.chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json +++ b/.chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json @@ -14,7 +14,6 @@ ".chock/compiled/token-efficiency/agent-hooks/tool-call-gate.json": "0689304b5bacd25e9a371674d0faa8f9b8d9d35132d2faf7917c2b165a1a93fa", ".chock/compiled/token-efficiency/agent-hooks/tool-call-hooks.json": "7300181bc92f845f81e316813adc9eb6cfd58d8a94525126455203df7fb08e6a", ".chock/compiled/token-efficiency/ambient-rule/ambient.md": "6d94b590c79d340fe25c48dc119b41ece181c5c605b4a38a853e54b54f4e9156", - ".chock/compiled/token-efficiency/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/token-efficiency/pre-tool-use/codex_cli-tool-call-hooks.json": "a80d3ca758064cba6407f75213a659ec24321eddd85b2adc444cf9f367c5e741", ".chock/compiled/token-efficiency/pre-tool-use/cursor-post-hooks.json": "4348e99148692cafca9417a6dba71e01bda26a404733926415313428bb7f2d93", ".chock/compiled/token-efficiency/pre-tool-use/cursor-postfailure-hooks.json": "9c1e9f0f10255027115cfe5f74ac955be9f66f29973905f8eae319293285f2ab", diff --git a/.chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json b/.chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json index 2ae882c8..79336a01 100644 --- a/.chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json +++ b/.chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json @@ -17,7 +17,6 @@ ".chock/compiled/block-invisible-unicode/ci-gate/step.yaml": "84e201486169ad4c016579f744c77b88b3b722628c01018d6afa7239fc9bec63", ".chock/compiled/block-invisible-unicode/git-hook/gate.json": "351fb35fffc9aba4882fe53ebbd3713b69aca6b4eb75798538a327b1cb641e1e", ".chock/compiled/block-invisible-unicode/git-hook/git-pre-commit.sh": "454c869cf2aa5123164f732ab28ffb79cfbf57c4e4a2b73597ef592bf11e59c6", - ".chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-invisible-unicode/mcp-gateway/gateway-gate.json": "83a400812189da3cb5ba7150d969698ab2400aa6c558348499f8745b5bf17ae0", ".chock/compiled/block-invisible-unicode/pre-tool-use/codex_cli-write-hooks.json": "fbad2af5a4a0e25f8a2704865641310f88042fe3c36ef3f5184b023d7b23af09", ".chock/compiled/block-invisible-unicode/pre-tool-use/cursor-write-hooks.json": "5a8a3215d75707c045b803c913edae75beaf9554cf4fa237d757ad773dfea2f4", @@ -61,7 +60,6 @@ ".chock/compiled/block-wildcard-agent-permissions/ci-gate/step.yaml": "f173704ceb69af71a971c081d7219f205fc176b5970dd4a32e429bbf4f6cd929", ".chock/compiled/block-wildcard-agent-permissions/git-hook/gate.json": "758636f7d17326652db0cac493c4fb59dcd7eaa922824a6e43bc49bfb616f762", ".chock/compiled/block-wildcard-agent-permissions/git-hook/git-pre-commit.sh": "8ad425c111c1dbf52a48ad2ba68b645d4d716befa8dfea575817ecea752421ea", - ".chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-wildcard-agent-permissions/mcp-gateway/gateway-gate.json": "9bf2c6b2447a2227f303b44afd50771db2cfa3a3ee40edc1280ac43209060c55", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/codex_cli-write-hooks.json": "8af817d29d040a474bbd5aa4c090c6084ab153f985882588e6b753ad6df1aad1", ".chock/compiled/block-wildcard-agent-permissions/pre-tool-use/cursor-write-hooks.json": "691bdbeb577970ffdbc416b828781b74c39bcc9abe6eeda09e0d0069d0a04578", @@ -105,7 +103,6 @@ ".chock/compiled/pin-github-actions/ci-gate/step.yaml": "91ed82dfc00043421971fc4f480e800682fe9f8b8c0e6aa2cfecbb1e8de8b01d", ".chock/compiled/pin-github-actions/git-hook/gate.json": "65b5bc9554fa8852cf94ceaaced461755d7694c8b87b37fe7a8434d8642c666b", ".chock/compiled/pin-github-actions/git-hook/git-pre-commit.sh": "e6e47fcbbce3525c47b7ea69deee273ce95442ef3dac7bcd43a897b553769bcd", - ".chock/compiled/pin-github-actions/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/pin-github-actions/mcp-gateway/gateway-gate.json": "308905df356acf7194c529bf0a418fd219e083ebd8545ce1aabd45c2dfbc2650", ".chock/compiled/pin-github-actions/pre-tool-use/codex_cli-write-hooks.json": "14238725b34a2e01cda395bc90e6ac5c914b4450b3b33ab81934df6324c1210d", ".chock/compiled/pin-github-actions/pre-tool-use/cursor-write-hooks.json": "68283be362683ad10a6a4542ecc4d59cb52bfcfb02483587262b5c5e6997d6e1", @@ -152,7 +149,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -209,7 +205,6 @@ ".chock/compiled/scan-secrets/ci-gate/step.yaml": "e2392603980559fbc57a1be4e0243cfa3d620fddbc6c8be7b2114fd4bbad9e07", ".chock/compiled/scan-secrets/git-hook/gate.json": "04d7a7705ced24c5c341ab4dc9806c3f488e2b1ac7a6c0b995e45b2e810cfd1b", ".chock/compiled/scan-secrets/git-hook/git-pre-commit.sh": "65c9db2f5b615a079cf92b63d3c739bcd4427ed4db33811b13d8922c43bb2b50", - ".chock/compiled/scan-secrets/managed-setting/managed-settings.json": "650436873f2a8da403515b6a205bf0006fbd2799dd8ff6c16a46b80847beafe5", ".chock/compiled/scan-secrets/mcp-gateway/gateway-gate.json": "520fd73b5d80023c68619e7ff56539c74ed0ef672ba8ce4686cf6f34972c7b25", ".chock/compiled/scan-secrets/pre-tool-use/codex_cli-write-hooks.json": "804cedc96a9809cb84f27e5d9f0da8989b32dc5173fdd37a35bfd204771a3edc", ".chock/compiled/scan-secrets/pre-tool-use/cursor-write-hooks.json": "a81dd0ba1d107bc551c51bbd7084a272c2a0b7bcc0e0b02411c36af15db60da8", diff --git a/.chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json b/.chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json index 6f3c0aa2..53fe6ac3 100644 --- a/.chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json +++ b/.chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json @@ -20,7 +20,6 @@ ".chock/compiled/block-destructive-commands/agent-hooks/agent-hooks.json": "166952d2add28842185387ca8602d36f511968c7e666e04c87708dc9dfe3054e", ".chock/compiled/block-destructive-commands/ambient-rule/ambient.md": "67f05c884bf417822000ebac578a7090a3ead6e07f0dea03c46eaf695214f190", ".chock/compiled/block-destructive-commands/git-hook/git-pre-push.sh": "ecb69d79165216412f646fb2ef8d53646a4c6d39fde77bfcc987adff7c76149e", - ".chock/compiled/block-destructive-commands/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-destructive-commands/pre-tool-use/antigravity-hooks.json": "9848d7c4f5be4c7ca30d401ceffe1ad4ec30775a0383552232aed9a91499b002", ".chock/compiled/block-destructive-commands/pre-tool-use/codex_cli-hooks.json": "02d8a824208df11dfd45e452e684e0779d8f164c4b6a8164bae9f9ca2cbf0865", ".chock/compiled/block-destructive-commands/pre-tool-use/cursor-hooks.json": "7091a6caea23d8794ef4e26af62e77d9f3a3bc3a38d8de9a941277e053ca1d97", @@ -55,7 +54,6 @@ ".agents/policies/block-no-verify/skills/block-no-verify/SKILL.md": "4e40b8cb58619728bdfc8dd4de2143c77bfa8c744f1f44d34904b88ae85ac023", ".chock/compiled/block-no-verify/agent-hooks/agent-hooks.json": "82b2e539bb812058a0c6cf3298ae9b72762eec28ede995ebe730b5d76f99c02b", ".chock/compiled/block-no-verify/ambient-rule/ambient.md": "371f90ab55eea13626e972c05883f2c1c427c9ca21a6d4aff23efa4513469431", - ".chock/compiled/block-no-verify/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/block-no-verify/pre-tool-use/antigravity-hooks.json": "1d5445dda7ade581edf55dc5e2d9adfde0c2ec9efd9cede936b8b99fd1fb0a9b", ".chock/compiled/block-no-verify/pre-tool-use/codex_cli-hooks.json": "a9df710f0b597cf83be795e5f7e68e0b9a641199765f4c7870de0be60b9ce8f0", ".chock/compiled/block-no-verify/pre-tool-use/cursor-hooks.json": "b7d84e25198f56302446e5a3738bfc1328a1e144c69d4d5b8cacdc83b9c829fa", @@ -92,7 +90,6 @@ ".chock/compiled/protect-agent-config/agent-hooks/gate-hooks.json": "d7a2a9b67c712958415981eb0cb7b6dccff19f3162c079c5cfa445afe82e4f9a", ".chock/compiled/protect-agent-config/agent-hooks/gate.json": "15fef0cfe7fbbd34e8b8833fdf9b45f14630cc3b202ea29256a5dd8a3b3d8b33", ".chock/compiled/protect-agent-config/ambient-rule/ambient.md": "933e699a70fa72f4ea3eedc8e21078455785ede5a46c291ee57814b1d16d0cd0", - ".chock/compiled/protect-agent-config/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-agent-config/mcp-gateway/gateway-gate.json": "81f440f9de61c0e15e73f0f35a319f224e773f1d8f6b92dfe95cc65562407ce5", ".chock/compiled/protect-agent-config/pre-tool-use/antigravity-hooks.json": "3fc5c848ba68835a54b756374c6abb73a285b6f0c9586911385e6a00f3ed79a3", ".chock/compiled/protect-agent-config/pre-tool-use/codex_cli-hooks.json": "ba95952f9d8b552175ea360f46c6a4d76670de1033eb7123f6e3513073913e16", @@ -152,7 +149,6 @@ ".chock/compiled/protect-commit-privacy/agent-hooks/agent-hooks.json": "c532957fe3efd114386ced897aec8fffdfc41e8f281a39efb4620f4e6a7d2747", ".chock/compiled/protect-commit-privacy/ambient-rule/ambient.md": "0d747282d92f843a14974765e038a69a41c93c70e1e6e8100a6546d5b48865c4", ".chock/compiled/protect-commit-privacy/git-hook/git-commit-msg.sh": "c4d9679ba23a73ab2fad594b5a3c088fd1a29d2b2444483da6d08023e0fef98a", - ".chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json": "859d05e0265611518ac5329c0a52141581f55956fd86e952206d6651820f018b", ".chock/compiled/protect-commit-privacy/pre-tool-use/antigravity-hooks.json": "762b527f13d0924e9fdd48b748d1027a1232a25225189e763cabcee208e9055d", ".chock/compiled/protect-commit-privacy/pre-tool-use/codex_cli-hooks.json": "85f228d8d50290015ef3204a73e9ddf3470929ed7fbb0f0ce8bd2d2eb8460f05", ".chock/compiled/protect-commit-privacy/pre-tool-use/cursor-hooks.json": "28d17b33eacbc0e331fc588ee9a129e66f9ad29414dad360066990d1c739e4db", diff --git a/.chock/bin/launch.sh b/.chock/bin/launch.sh index 0b9fb10e..22981835 100755 --- a/.chock/bin/launch.sh +++ b/.chock/bin/launch.sh @@ -21,17 +21,20 @@ configured="$(git config --get chock.python 2>/dev/null)" legacy= case "${OS:-}|${MSYSTEM:-}" in *Windows_NT*|*"|"?*) legacy=1 ;; esac [ "$runtime" = "-m" ] && legacy=1 +# A script starts with -E -s (-I less -P): no PYTHON* variable or user site changes it; its own folder stays on the path. +isolate=-Es +[ "$runtime" = "-m" ] && isolate= # Only the marker path sets it (to 9); everywhere else an inherited value is cleared, so it never names an fd. [ -n "$legacy" ] && { CHOCK_START_FD=; export CHOCK_START_FD; } for candidate in "$configured" python3 python py; do [ -n "$candidate" ] || continue if [ -n "$legacy" ]; then - if probed="$("$candidate" -c 'import sys; print("chock-python"); sys.exit(sys.version_info < (3, 11,))' /dev/null)" && [ "$probed" = "chock-python" ]; then - exec "$candidate" -X utf8 "$runtime" "$@" + if probed="$("$candidate" -I -c 'import sys; print("chock-python"); sys.exit(sys.version_info < (3, 11,))' /dev/null)" && [ "$probed" = "chock-python" ]; then + exec "$candidate" ${isolate:+"$isolate"} -X utf8 "$runtime" "$@" fi elif command -v "$candidate" >/dev/null 2>&1; then exec 3>&1 - marker="$(CHOCK_START_FD=9 "$candidate" -X utf8 "$runtime" "$@" 9>&1 1>&3 3>&-)" + marker="$(CHOCK_START_FD=9 "$candidate" ${isolate:+"$isolate"} -X utf8 "$runtime" "$@" 9>&1 1>&3 3>&-)" status=$? exec 3>&- [ "$marker" = "chock-python" ] && exit "$status" diff --git a/.chock/bin/tabnine.py b/.chock/bin/tabnine.py index 8fd2cb93..29141032 100755 --- a/.chock/bin/tabnine.py +++ b/.chock/bin/tabnine.py @@ -782,7 +782,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/bin/vscode_copilot.py b/.chock/bin/vscode_copilot.py index d62f569f..bab53915 100755 --- a/.chock/bin/vscode_copilot.py +++ b/.chock/bin/vscode_copilot.py @@ -717,7 +717,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/bin/windsurf.py b/.chock/bin/windsurf.py index a8672de9..74cee413 100755 --- a/.chock/bin/windsurf.py +++ b/.chock/bin/windsurf.py @@ -680,7 +680,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/.chock/compiled/agent-discipline/managed-setting/managed-settings.json b/.chock/compiled/agent-discipline/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/agent-discipline/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/block-destructive-commands/managed-setting/managed-settings.json b/.chock/compiled/block-destructive-commands/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/block-destructive-commands/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json b/.chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/block-invisible-unicode/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/block-no-verify/managed-setting/managed-settings.json b/.chock/compiled/block-no-verify/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/block-no-verify/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json b/.chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/block-wildcard-agent-permissions/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/code-safety/managed-setting/managed-settings.json b/.chock/compiled/code-safety/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/code-safety/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/context-hygiene/managed-setting/managed-settings.json b/.chock/compiled/context-hygiene/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/context-hygiene/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/git-safety/managed-setting/managed-settings.json b/.chock/compiled/git-safety/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/git-safety/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/injection-defense/managed-setting/managed-settings.json b/.chock/compiled/injection-defense/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/injection-defense/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/memory-discipline/managed-setting/managed-settings.json b/.chock/compiled/memory-discipline/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/memory-discipline/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/minimal-content/managed-setting/managed-settings.json b/.chock/compiled/minimal-content/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/minimal-content/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/pin-github-actions/managed-setting/managed-settings.json b/.chock/compiled/pin-github-actions/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/pin-github-actions/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/pre-generated-scripts/managed-setting/managed-settings.json b/.chock/compiled/pre-generated-scripts/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/pre-generated-scripts/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/protect-agent-config/managed-setting/managed-settings.json b/.chock/compiled/protect-agent-config/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/protect-agent-config/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json b/.chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/protect-commit-privacy/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/protect-main-branch/managed-setting/managed-settings.json b/.chock/compiled/protect-main-branch/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/protect-main-branch/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/require-review-evidence/managed-setting/managed-settings.json b/.chock/compiled/require-review-evidence/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/require-review-evidence/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/scan-secrets/managed-setting/managed-settings.json b/.chock/compiled/scan-secrets/managed-setting/managed-settings.json deleted file mode 100644 index 30afb96f..00000000 --- a/.chock/compiled/scan-secrets/managed-setting/managed-settings.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "deny": [ - { - "type": "file", - "pattern": "\\.(env|pem|key|p12|pfx|jks|keystore)$", - "message": "Credential files are denied in the workspace." - }, - { - "type": "text", - "pattern": "(?i)(AKIA[0-9A-Z]{16}|ghp_[a-zA-Z0-9]{36}|xox[bpas]-[0-9A-Za-z-]{10,}|(sk|rk)_live_[0-9A-Za-z]{16,}|sk-ant-[0-9A-Za-z_-]{20,}|AIza[0-9A-Za-z_-]{35}|npm_[0-9A-Za-z]{36}|eyJ[A-Za-z0-9_-]*\\.[A-Za-z0-9_-]*\\.[A-Za-z0-9_-]*|-----BEGIN .* PRIVATE KEY-----)", - "message": "Credential-like values are denied in generated or edited files." - } - ], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/test-integrity/managed-setting/managed-settings.json b/.chock/compiled/test-integrity/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/test-integrity/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/token-efficiency/managed-setting/managed-settings.json b/.chock/compiled/token-efficiency/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/token-efficiency/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/verify-framework-conformance/managed-setting/managed-settings.json b/.chock/compiled/verify-framework-conformance/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/verify-framework-conformance/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.chock/compiled/yagni/managed-setting/managed-settings.json b/.chock/compiled/yagni/managed-setting/managed-settings.json deleted file mode 100644 index 8efb266a..00000000 --- a/.chock/compiled/yagni/managed-setting/managed-settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "deny": [], - "ask": [] -} \ No newline at end of file diff --git a/.claude/settings.json b/.claude/settings.json index 9017c332..66d547ee 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -6,7 +6,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/claude_code.py --multi \".chock/bin/index/claude_code-PreToolUse-3a2134a7.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/claude_code.py --multi \".chock/bin/index/claude_code-PreToolUse-3a2134a7.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c", "timeout": 45 } ] @@ -16,7 +16,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/claude_code.py --multi \".chock/bin/index/claude_code-PreToolUse-4acdeb9e.json\" --index-sha256 2ff022397098e742808806927b5c3194e3f0aa0c162d50d22cd98eef4756f022", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/claude_code.py --multi \".chock/bin/index/claude_code-PreToolUse-4acdeb9e.json\" --index-sha256 8083526dfaf2ae5ef67edf44b86db346ca93eaa1808cc9e406c00571d9c97829", "timeout": 45 } ] @@ -25,7 +25,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/claude_code.py --multi \".chock/bin/index/claude_code-PreToolUse-185f9750.json\" --index-sha256 8acaa1f45d082758ffa5270a722263da96ccfd24038713be8e13a11220bd4d2d --stop-check", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/claude_code.py --multi \".chock/bin/index/claude_code-PreToolUse-185f9750.json\" --index-sha256 c016a88140574f28f7b73f92fbff992d34969db9af603a7147673d4cceb9133c --stop-check", "timeout": 45 } ] diff --git a/.codex/hooks.json b/.codex/hooks.json index 3b11e557..133886e5 100644 --- a/.codex/hooks.json +++ b/.codex/hooks.json @@ -5,8 +5,8 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-14533f34.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a", - "commandWindows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-14533f34.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-14533f34.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c", + "commandWindows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-14533f34.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", "timeout": 45 } ], @@ -16,8 +16,8 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-d2160b00.json\" --index-sha256 2ff022397098e742808806927b5c3194e3f0aa0c162d50d22cd98eef4756f022", - "commandWindows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-d2160b00.json\" --index-sha256 2ff022397098e742808806927b5c3194e3f0aa0c162d50d22cd98eef4756f022; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-d2160b00.json\" --index-sha256 8083526dfaf2ae5ef67edf44b86db346ca93eaa1808cc9e406c00571d9c97829", + "commandWindows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-d2160b00.json\" --index-sha256 8083526dfaf2ae5ef67edf44b86db346ca93eaa1808cc9e406c00571d9c97829; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", "timeout": 45 } ], @@ -27,8 +27,8 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-186bd88e.json\" --index-sha256 8acaa1f45d082758ffa5270a722263da96ccfd24038713be8e13a11220bd4d2d --stop-check", - "commandWindows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-186bd88e.json\" --index-sha256 8acaa1f45d082758ffa5270a722263da96ccfd24038713be8e13a11220bd4d2d --stop-check; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-186bd88e.json\" --index-sha256 c016a88140574f28f7b73f92fbff992d34969db9af603a7147673d4cceb9133c --stop-check", + "commandWindows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/codex_cli.py --multi \".chock/bin/index/codex_cli-PreToolUse-186bd88e.json\" --index-sha256 c016a88140574f28f7b73f92fbff992d34969db9af603a7147673d4cceb9133c --stop-check; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", "timeout": 45 } ] diff --git a/.cursor/hooks.json b/.cursor/hooks.json index 225f9f54..21039954 100644 --- a/.cursor/hooks.json +++ b/.cursor/hooks.json @@ -2,14 +2,14 @@ "hooks": { "beforeShellExecution": [ { - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/cursor.py --multi \".chock/bin/index/cursor-beforeShellExecution-b6c825df.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/cursor.py --multi \".chock/bin/index/cursor-beforeShellExecution-b6c825df.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c", "timeout": 45, "failClosed": true } ], "preToolUse": [ { - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/cursor.py --multi \".chock/bin/index/cursor-preToolUse-b6c825df.json\" --index-sha256 5b6164207fceb0f8784ce211241353a3c843037e90d9c5720f4a6f94c721c196 --stop-check", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/cursor.py --multi \".chock/bin/index/cursor-preToolUse-b6c825df.json\" --index-sha256 44e2f370e3ea951657950c54134d86a43b002dfded0c6af5483cade09a21881c --stop-check", "timeout": 45, "failClosed": true } diff --git a/.devin/hooks.v1.json b/.devin/hooks.v1.json index 68b3d5fe..10ef28ec 100644 --- a/.devin/hooks.v1.json +++ b/.devin/hooks.v1.json @@ -4,7 +4,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/devin.py --multi \".chock/bin/index/devin-PreToolUse-185f9750.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a --stop-check", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/devin.py --multi \".chock/bin/index/devin-PreToolUse-185f9750.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c --stop-check", "timeout": 45 } ] diff --git a/.gemini/settings.json b/.gemini/settings.json index d8e3887b..a4cee683 100644 --- a/.gemini/settings.json +++ b/.gemini/settings.json @@ -5,7 +5,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/gemini_cli.py --multi \".chock/bin/index/gemini_cli-BeforeTool-09cc5246.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/gemini_cli.py --multi \".chock/bin/index/gemini_cli-BeforeTool-09cc5246.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c", "timeout": 45000 } ], @@ -15,7 +15,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/gemini_cli.py --multi \".chock/bin/index/gemini_cli-BeforeTool-e86eddda.json\" --index-sha256 2ff022397098e742808806927b5c3194e3f0aa0c162d50d22cd98eef4756f022", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/gemini_cli.py --multi \".chock/bin/index/gemini_cli-BeforeTool-e86eddda.json\" --index-sha256 8083526dfaf2ae5ef67edf44b86db346ca93eaa1808cc9e406c00571d9c97829", "timeout": 45000 } ], @@ -25,7 +25,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/gemini_cli.py --multi \".chock/bin/index/gemini_cli-BeforeTool-c8f6602b.json\" --index-sha256 8acaa1f45d082758ffa5270a722263da96ccfd24038713be8e13a11220bd4d2d --stop-check", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/gemini_cli.py --multi \".chock/bin/index/gemini_cli-BeforeTool-c8f6602b.json\" --index-sha256 c016a88140574f28f7b73f92fbff992d34969db9af603a7147673d4cceb9133c --stop-check", "timeout": 45000 } ] diff --git a/.github/hooks/chock.json b/.github/hooks/chock.json index d31e9828..6ded7e07 100644 --- a/.github/hooks/chock.json +++ b/.github/hooks/chock.json @@ -7,19 +7,19 @@ "matcher": "Edit|Write", "timeout": 45, "timeoutSec": 45, - "bash": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json\" --index-sha256 1693abeab7a1aad4522981978246b0a8c72bc9a6dca83e067f145190d0cda85c", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json\" --index-sha256 1693abeab7a1aad4522981978246b0a8c72bc9a6dca83e067f145190d0cda85c", - "powershell": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json\" --index-sha256 1693abeab7a1aad4522981978246b0a8c72bc9a6dca83e067f145190d0cda85c; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", - "windows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json\" --index-sha256 1693abeab7a1aad4522981978246b0a8c72bc9a6dca83e067f145190d0cda85c; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE" + "bash": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json\" --index-sha256 f18958baa97e9bb3f981bbad57d5475a0d341c6c767c8e25dcba5b0114be88f5", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json\" --index-sha256 f18958baa97e9bb3f981bbad57d5475a0d341c6c767c8e25dcba5b0114be88f5", + "powershell": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json\" --index-sha256 f18958baa97e9bb3f981bbad57d5475a0d341c6c767c8e25dcba5b0114be88f5; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", + "windows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-38a36ef3.json\" --index-sha256 f18958baa97e9bb3f981bbad57d5475a0d341c6c767c8e25dcba5b0114be88f5; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE" }, { "type": "command", "timeout": 45, "timeoutSec": 45, - "bash": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json\" --index-sha256 0d7dc4f8d23079d317442722244ae470a1494e9ea81a01267d57d18ac053fe38 --stop-check", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json\" --index-sha256 0d7dc4f8d23079d317442722244ae470a1494e9ea81a01267d57d18ac053fe38 --stop-check", - "powershell": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json\" --index-sha256 0d7dc4f8d23079d317442722244ae470a1494e9ea81a01267d57d18ac053fe38 --stop-check; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", - "windows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json\" --index-sha256 0d7dc4f8d23079d317442722244ae470a1494e9ea81a01267d57d18ac053fe38 --stop-check; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE" + "bash": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json\" --index-sha256 8aee92efb0e0a8adba27114ac3a4be297bbce9df7111dac107bac79ad5b6ece4 --stop-check", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json\" --index-sha256 8aee92efb0e0a8adba27114ac3a4be297bbce9df7111dac107bac79ad5b6ece4 --stop-check", + "powershell": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json\" --index-sha256 8aee92efb0e0a8adba27114ac3a4be297bbce9df7111dac107bac79ad5b6ece4 --stop-check; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", + "windows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-PreToolUse-2b90d191.json\" --index-sha256 8aee92efb0e0a8adba27114ac3a4be297bbce9df7111dac107bac79ad5b6ece4 --stop-check; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE" } ], "SessionStart": [ @@ -86,10 +86,10 @@ "matcher": "bash|powershell|pwsh|sh|shell", "timeout": 45, "timeoutSec": 45, - "bash": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a", - "powershell": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", - "windows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE" + "bash": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c", + "powershell": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE", + "windows": "& git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/vscode_copilot.py --multi \".chock/bin/index/vscode_copilot-preToolUse-3ed8896e.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c; if ($null -eq $LASTEXITCODE) { exit 2 }; exit $LASTEXITCODE" } ] } diff --git a/.grok/hooks/agentseam.json b/.grok/hooks/agentseam.json index ac168698..384b85c9 100644 --- a/.grok/hooks/agentseam.json +++ b/.grok/hooks/agentseam.json @@ -5,7 +5,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/grok.py --multi \".chock/bin/index/grok-PreToolUse-185f9750.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a --stop-check", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/grok.py --multi \".chock/bin/index/grok-PreToolUse-185f9750.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c --stop-check", "timeout": 45 } ] diff --git a/.tabnine/agent/settings.json b/.tabnine/agent/settings.json index a261cb93..93ae5116 100644 --- a/.tabnine/agent/settings.json +++ b/.tabnine/agent/settings.json @@ -5,7 +5,7 @@ "hooks": [ { "type": "command", - "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/tabnine.py --multi \".chock/bin/index/tabnine-BeforeTool-c8f6602b.json\" --index-sha256 ebfc8620b51af8d70e3d11b25aeab3c58c3f13fedc2dc2c8d07541f2e0bf911a --stop-check", + "command": "git -c \"alias.chock-hook=!test -f .chock/bin/launch.sh || { echo chock: no .chock/bin/launch.sh here, run chock sync --repo . >&2; exit 2; }; sh .chock/bin/launch.sh\" chock-hook .chock/bin/tabnine.py --multi \".chock/bin/index/tabnine-BeforeTool-c8f6602b.json\" --index-sha256 5b90d9a49d0efb9369d980cbfb916a6fae927a36c977ae02181a0460f409767c --stop-check", "name": "agentseam", "timeout": 45000 } diff --git a/chock.lock b/chock.lock index 22c0800b..5bfb7bea 100644 --- a/chock.lock +++ b/chock.lock @@ -1,6 +1,6 @@ { "lockfile_version": "2", - "engine": ">=0.1,<0.2", + "engine": ">=1.1,<2", "packs": [ { "id": "agent-discipline", @@ -8,7 +8,7 @@ "managed": false, "sha256": "e41a849bc2b80f5c3c10fa4ea9883795a3e2ef9a626322c97ce7db41bab71bda", "source": "local", - "artifacts_sha256": "aa8906f9de0aa4a597077410094b8f3cb5cef5e0e50e438f45a1c689160cf3db" + "artifacts_sha256": "17ed5f45decf46868b440f82d698b0c11b8f3e8c8616d20ccb16907d3c692f8b" }, { "id": "block-destructive-commands", @@ -16,7 +16,7 @@ "managed": false, "sha256": "a5c10785be81bb4a391f4e629359454b2e6212772b8475a2ce2f08b03214bab6", "source": "local", - "artifacts_sha256": "606e3ef7e43d9050b317103ae0a0652e2b8548ee8fc2d18aa390539077f8210a" + "artifacts_sha256": "9b988a9717063a7ae7698f79d01fbb53b0177fddbcf7194e0db36d573860f1d4" }, { "id": "block-invisible-unicode", @@ -24,7 +24,7 @@ "managed": false, "sha256": "e34c93313ca3a6352c1d13715c60e8191ad6cdf04542d9f9d6a78cb210e34594", "source": "local", - "artifacts_sha256": "33845353b14a3413b48dce1deeabecc9451858702adc4c12f3ad68dfe560be46" + "artifacts_sha256": "0c8d0bb624227d674c309b342e8240b2b6be9cba4204d566cc2e8ed5291f3001" }, { "id": "block-no-verify", @@ -32,7 +32,7 @@ "managed": false, "sha256": "4e4fe3bd5a2b1947851bb390f56701c8129619b5a74a745806684f95d2e2105a", "source": "local", - "artifacts_sha256": "8d9fb089051fba248a366e851b77448117bdf54026fb3fdab1abe645bb479861" + "artifacts_sha256": "888852399360d4eab3fcc2191ca0406c8e94ff59f23dfa83adf3fc39bc4cb537" }, { "id": "block-wildcard-agent-permissions", @@ -40,7 +40,7 @@ "managed": false, "sha256": "5a6018554c19e90993f20e5d8a02fd1288c3ebb8431ddcee2967f009f6fab129", "source": "local", - "artifacts_sha256": "395d89ad34af4bdb0ed4f0e7a0dce55ebf8ee3d9eada8e90196ffcb190bf1767" + "artifacts_sha256": "1731c28f2dd34b569111e3ff0448c218803116ef895927542709fb297c7f4518" }, { "id": "code-safety", @@ -48,7 +48,7 @@ "managed": false, "sha256": "07a34b6e60452d78f8df0f853d17097e931f5e2e5e4b3fc492277bb99fd651f7", "source": "local", - "artifacts_sha256": "2a3465baf6bc0da2f3ae44e01cf1ddcdd400135438c14836368c6bdc7a29a405" + "artifacts_sha256": "c4028cdd8092fb374b8efe271b785d2228718264f4e04fc755e966af472248a6" }, { "id": "context-hygiene", @@ -56,7 +56,7 @@ "managed": false, "sha256": "7d77f634a5b13e199a4e6744d3fc2dadffce3891c7bc6a6a538639e37ae1505d", "source": "local", - "artifacts_sha256": "1cbaddfc2bd8e401ac218252c755d5ef9be607f0b88359f2af59c5323992b285" + "artifacts_sha256": "5f74ee647666735c5b32974c0d59ab485e2c47c570d3c98d1854d1e1dcb95e68" }, { "id": "git-safety", @@ -64,7 +64,7 @@ "managed": false, "sha256": "d57e89caab2d9c0608beb4c6acad049c1dc76150fa2caae17f43daa2d77264c4", "source": "local", - "artifacts_sha256": "b9db0e16e1009cb85d051c939ff47093497f8540d648adc33b90b6d1a39f08d8" + "artifacts_sha256": "c9f84f4d2faad9345307bcae87ef0ed6c2f15173367f1564da144a55a49a4be2" }, { "id": "injection-defense", @@ -72,7 +72,7 @@ "managed": false, "sha256": "722c6235b5366a0916625a2acb4f7383b07b04f2691a99b3d5cb8203d922fa45", "source": "local", - "artifacts_sha256": "a6f82e74b77c04a051dbc1058b1e1124eb8b78d12427af3b947b6d42686331be" + "artifacts_sha256": "e272fd293c18788f453b4cd4a6f774483b59b49b7a362a59b7b3d575d16bb881" }, { "id": "memory-discipline", @@ -80,7 +80,7 @@ "managed": false, "sha256": "e795ef722ee5eb0fdc9af872666d24b90a7a020dff50e56bd8f418a9a868a11a", "source": "local", - "artifacts_sha256": "906287d93337b6080f68621350512cbf8a73c381657c08faeeddab8a2ec45a6b" + "artifacts_sha256": "30ff9ba99409ecc720a8087341962b9c934070d56c21e34260af180c87643ae0" }, { "id": "minimal-content", @@ -88,7 +88,7 @@ "managed": false, "sha256": "00bb15c96c603ff92fdb7c7fd9fcc610c36f05fdcd5147763931acbd96818853", "source": "local", - "artifacts_sha256": "a6d131a5036887ab769dbdcbe01c761f5825b656a6cae30607f900639cb29ec7" + "artifacts_sha256": "bf5246fd47343838f0e6eac62a6a67928641e472b5a0689db101ddd4b1a14cb4" }, { "id": "pin-github-actions", @@ -96,7 +96,7 @@ "managed": false, "sha256": "a3afaef1791bf3b84cfd48b91c8dd5553a6a2fbd99d0e9df6423cb100a4e68d7", "source": "local", - "artifacts_sha256": "99d58c2e84918370cac3b75a6fa2cc8d530c63dfa8189c5a5e4835b06859dfdd" + "artifacts_sha256": "df233c2dbcbe2a93c2cdbbcc610c568b5b0c1d32f380b0f7d70d9f2200e19234" }, { "id": "pre-generated-scripts", @@ -104,7 +104,7 @@ "managed": false, "sha256": "17227f0f6e007ddd445f0ace6457b5d3ffc66ed23f2d52b75af9f9eb669191c5", "source": "local", - "artifacts_sha256": "ef6d558f78d7f86bc66b7c755b3eac5324a44db9e240ef29dc24a7d6e27f5380" + "artifacts_sha256": "f43a190e1338a7d0eeb219b9625e51f7292e1df84e81933751080901fb30756d" }, { "id": "protect-agent-config", @@ -112,7 +112,7 @@ "managed": false, "sha256": "dc20b58f82ae4c5c77759d32d15a51a431c76039c3cc34b1d4a892cb2f264aef", "source": "local", - "artifacts_sha256": "aa0f584a3da1309770c9fbda7d082970e7d279d298f06ca625520a80d85e278f" + "artifacts_sha256": "a7525f05e9b0f287a0eea727e3d2e041228832fdd242553c0df1a039164a7a9b" }, { "id": "protect-commit-privacy", @@ -120,7 +120,7 @@ "managed": false, "sha256": "07d12989cb3a4a20a2d48579f2b51c5c5d6e14e4367412250d2d3fe682759fdd", "source": "local", - "artifacts_sha256": "0aab76e19d6744ed014507bd15d6ed15e2db88d1777cd5bfe2295d557c7bf39b" + "artifacts_sha256": "8bf045a0a7d83fafc4f64576cab3aedc418b9dd0ebef196452db1836747846ac" }, { "id": "protect-main-branch", @@ -128,7 +128,7 @@ "managed": false, "sha256": "d1709293d7ee4ef502457e2e65eb65516daebd6ba5f7172db744bfdd8db63002", "source": "local", - "artifacts_sha256": "dd31407d52e400f6f74e515cdacb8b3e188b97133db255f88bf73791bbee2eb4" + "artifacts_sha256": "a7d7c8dc2439782632437eee5c407ec95fa5a8b5ac78385a699183d3510c58e2" }, { "id": "require-review-evidence", @@ -136,7 +136,7 @@ "managed": false, "sha256": "67b76ca30e7e9add2d284b62da210344a828e6de3d28e5f410ff81c5842beaa4", "source": "local", - "artifacts_sha256": "b16de533c1019379d5f5c7420bd0934f1430e6716f09faa8677763e20bab5a02" + "artifacts_sha256": "d75b06e96f0e001e72a565a1450527b72fb33ff00bbe520cc4244d8aa7d983d6" }, { "id": "scan-secrets", @@ -144,7 +144,7 @@ "managed": false, "sha256": "d04914f3b1ff3d4a2983ec8bc9fd3465556d5744ec41d03faca61ddd8c4917f0", "source": "local", - "artifacts_sha256": "5c9540a8b0ca8ca8bb88dde396adb71fe725eace2e932dc90c18d8c69d6b67f5" + "artifacts_sha256": "0c421836338483c280671ad720e58162e737f4ba822bc3c60f49c91ee6aa391a" }, { "id": "test-integrity", @@ -152,7 +152,7 @@ "managed": false, "sha256": "965b849688591738ddb1b6a7f5d20deaa0483e33c6daa117d37a592126512fca", "source": "local", - "artifacts_sha256": "b013983cfca823013ac954817d053c7ce8b7accdaa6b0484a2f7105a655550e8" + "artifacts_sha256": "f8516f28439131aa8b5c52c4c08b563425ac1a4336c8dacab17bf2ac8ba716d5" }, { "id": "token-efficiency", @@ -160,7 +160,7 @@ "managed": false, "sha256": "771cbb776e8b86602d330b878073046bd7becbc44827e47f745bb5970973ceb4", "source": "local", - "artifacts_sha256": "c14412edb8abd9e633198f2fc08bc09af74e682939e1a076da1fea32c3fab97a" + "artifacts_sha256": "01171e6cefe124c56339d1ec84788c4d82e1e4ef4049e2192ef978cda49fd7df" }, { "id": "verify-dependency-exists", @@ -175,7 +175,7 @@ "managed": false, "sha256": "8cf8ec272df2d224b264ca662e7ca6798130369a4d70a14d454566654ada9917", "source": "local", - "artifacts_sha256": "3c4f69d24ba8acbe41c9c7a1a334dc3fb9d722d39c85f601e6f214a92917210c" + "artifacts_sha256": "1c272c78ac73c7014a44eca6a2c5b5df8ef4a84f886d995c51d01c7d363dfe28" }, { "id": "yagni", @@ -183,7 +183,7 @@ "managed": false, "sha256": "58954cf1e9d3634f67b759d65fc16bb2f97782b1815a71ea558b4dfd47b66628", "source": "local", - "artifacts_sha256": "12038a1b15c829fc3326b2a9667e0ea7276278be51620a58b2efdb4b60a019b5" + "artifacts_sha256": "d911cbf1dd92b800b121719f306d2cf1964259f71bb35aa2e078e85b76def2a2" } ] } diff --git a/tests/fixtures/plugin_targets/antigravity.json b/tests/fixtures/plugin_targets/antigravity.json index 51a128b7..95b251a3 100644 --- a/tests/fixtures/plugin_targets/antigravity.json +++ b/tests/fixtures/plugin_targets/antigravity.json @@ -41,7 +41,7 @@ "Stop": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-501fc3f8.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-501fc3f8.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 }, { @@ -55,7 +55,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b4ab2f9e.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"__CHOCK_PLUGIN_ROOT__\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b4ab2f9e.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"__CHOCK_PLUGIN_ROOT__\" --stop-check", "timeout": 45 } ], diff --git a/tests/fixtures/plugin_targets/droid.json b/tests/fixtures/plugin_targets/droid.json index f5492d97..ece3f350 100644 --- a/tests/fixtures/plugin_targets/droid.json +++ b/tests/fixtures/plugin_targets/droid.json @@ -44,7 +44,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-f8a1fdfc.json\" --index-sha256 1558e2ef98f7a3b8cf1b8c8aa00e9dcc168f65154abfeec52db142123edfc615 --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-f8a1fdfc.json\" --index-sha256 701603c13fd36de20f91005e93e586186f4dd8f6aa0f0a37cffabb1f58189f71 --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ], @@ -54,7 +54,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-91dc9fe9.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-91dc9fe9.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ], @@ -75,7 +75,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/Stop-185f9750.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/Stop-185f9750.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ] diff --git a/tests/fixtures/plugin_targets/goose.json b/tests/fixtures/plugin_targets/goose.json index 19a2e919..f79fa93f 100644 --- a/tests/fixtures/plugin_targets/goose.json +++ b/tests/fixtures/plugin_targets/goose.json @@ -40,7 +40,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-b6718bef.json\" --index-sha256 1558e2ef98f7a3b8cf1b8c8aa00e9dcc168f65154abfeec52db142123edfc615 --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-b6718bef.json\" --index-sha256 701603c13fd36de20f91005e93e586186f4dd8f6aa0f0a37cffabb1f58189f71 --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } @@ -51,7 +51,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-031b12f7.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-031b12f7.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } @@ -74,7 +74,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/Stop-4cd5d275.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/Stop-4cd5d275.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } diff --git a/tests/fixtures/plugin_targets/kimi.json b/tests/fixtures/plugin_targets/kimi.json index f7141e5f..5bbd7994 100644 --- a/tests/fixtures/plugin_targets/kimi.json +++ b/tests/fixtures/plugin_targets/kimi.json @@ -39,12 +39,12 @@ "hooks": [ { "event": "Stop", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"${KIMI_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"${KIMI_PLUGIN_ROOT}\"", "timeout": 45 }, { "event": "PreToolUse", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", "timeout": 45 }, { @@ -62,12 +62,12 @@ "hooks": [ { "event": "Stop", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"${KIMI_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"${KIMI_PLUGIN_ROOT}\"", "timeout": 45 }, { "event": "PreToolUse", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", "timeout": 45 }, { diff --git a/tests/fixtures/plugin_targets/openhands.json b/tests/fixtures/plugin_targets/openhands.json index ff591ed2..570533a0 100644 --- a/tests/fixtures/plugin_targets/openhands.json +++ b/tests/fixtures/plugin_targets/openhands.json @@ -45,7 +45,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b310004d.json\" --index-sha256 1558e2ef98f7a3b8cf1b8c8aa00e9dcc168f65154abfeec52db142123edfc615 --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b310004d.json\" --index-sha256 701603c13fd36de20f91005e93e586186f4dd8f6aa0f0a37cffabb1f58189f71 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ], @@ -55,7 +55,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-81fe7457.json\" --index-sha256 2dff4567d546a7ee68a3a6259616766beee48f2529d4a708bcfa14c1c5b84662 --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-81fe7457.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ], @@ -76,7 +76,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-185f9750.json\" --index-sha256 cfe0407ea48e39552d82f5d09a52c462c9690e5dc6986ded27db464c1781aff9 --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-185f9750.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ] diff --git a/tests/fixtures/runtime_goldens/_chock_runtime.py b/tests/fixtures/runtime_goldens/_chock_runtime.py index 36926292..233ffb4c 100644 --- a/tests/fixtures/runtime_goldens/_chock_runtime.py +++ b/tests/fixtures/runtime_goldens/_chock_runtime.py @@ -32,6 +32,8 @@ BYTECODE_OFF = {'PYTHONDONTWRITEBYTECODE': '1'} +ISOLATED_START = "import os, runpy, sys\nsys.argv = sys.argv[1:]\nsys.path.insert(0, os.path.dirname(os.path.abspath(sys.argv[0])))\nrunpy.run_path(sys.argv[0], run_name='__main__')\n" + PATTERN_SECONDS = 10 PATTERN_START_SECONDS = 10 @@ -111,6 +113,17 @@ def run_tree(argv, *, timeout=None, **options): untrack_tree(proc) return _chock_subprocess.CompletedProcess(argv, proc.returncode, out, err) +def python_free_env(env=None): + """`env` (this process's by default) without any `PYTHON*` variable, plus bytecode off for grandchildren.""" + base = _chock_os.environ if env is None else env + return {**{k: v for k, v in base.items() if not k.upper().startswith('PYTHON')}, **BYTECODE_OFF} + +def isolated_child(argv, env=None): + """(argv, env) that run the Python child `[interpreter, script, *args]` isolated: no PYTHON* variable, + user site or current folder can change what it imports; its own folder is put on sys.path explicitly.""" + interpreter, script, *args = argv + return ([interpreter, '-I', '-B', '-X', 'utf8', '-c', ISOLATED_START, str(script), *args], python_free_env(env)) + def engine_remaining(deadline): """Seconds left before `deadline`, never negative.""" return max(deadline - _chock_time.monotonic(), 0.0) @@ -803,7 +816,7 @@ def bash_candidates() -> list[str]: def interpreter_env(interpreter: str) -> dict[str, str]: """The environment a guard runs in: on Windows, Git's usr/bin ahead on PATH for sed and grep.""" - env = {**_chock_os.environ, **BYTECODE_OFF} + env = python_free_env() env.pop(CAN_ASK_ENV, None) if _chock_os.name != _WINDOWS: return env @@ -861,6 +874,11 @@ def split_command(command: str) -> tuple[list[str], bool]: except ValueError: return (command.split(), True) +def _guard_start(interpreter: str, guard: _chock_Path, args: list[str], env: dict[str, str]): + """(argv, env) for the guard: a Python guard starts isolated, a shell guard as given.""" + argv = [interpreter, str(guard), *args] + return isolated_child(argv, env) if guard.suffix == PYTHON_SUFFIX else (argv, env) + def run_guard(guard: _chock_Path, command: str, tool: str='') -> str: """`GUARD_BLOCKED` / `GUARD_ASKED` / `GUARD_CLEAN` when the guard ran, otherwise why it did not.""" return run_guard_detailed(guard, command, tool)[0] @@ -888,7 +906,8 @@ def run_guard_detailed(guard: _chock_Path, command: str, tool: str='', *, can_as if fallback: env[ARGV_FALLBACK_ENV] = '1' env.update({} if can_ask is None else {CAN_ASK_ENV: '1' if can_ask else '0'}) - proc = run_tree([interpreter, str(guard), *args], capture_output=True, text=True, encoding='utf-8', errors='replace', env=env, timeout=time_left(deadline, [interpreter, str(guard)]), check=False) + argv, env = _guard_start(interpreter, guard, args, env) + proc = run_tree(argv, capture_output=True, text=True, encoding='utf-8', errors='replace', env=env, timeout=time_left(deadline, [interpreter, str(guard)]), check=False) except _chock_subprocess.TimeoutExpired: print(f'chock: guard timed out after {ENGINE_BUDGET_SECONDS}s, not checked', file=sys.stderr) return (GUARD_ERRORED, '') @@ -2624,9 +2643,9 @@ def run_gate(gate, writes, event, run_in, deadline): runner = runner_for(gate) if runner is None: return (GATE_ERRORED, 'the vendored gate runner is not installed beside this gate') - argv = [sys.executable, str(runner), 'run', '--gate', str(gate), '--event', event] + argv, env = isolated_child([sys.executable, str(runner), 'run', '--gate', str(gate), '--event', event]) try: - proc = run_tree(argv, input=json.dumps({'writes': writes, **(extra or {})}), capture_output=True, text=True, encoding=_UTF8, errors='replace', timeout=time_left(deadline, argv), check=False, cwd=str(root) if root is not None else None) + proc = run_tree(argv, env=env, input=json.dumps({'writes': writes, **(extra or {})}), capture_output=True, text=True, encoding=_UTF8, errors='replace', timeout=time_left(deadline, argv), check=False, cwd=str(root) if root is not None else None) except _chock_subprocess.TimeoutExpired as exc: return (GATE_ERRORED, f'the gate runner gave no verdict within {allowed_seconds(exc)}') except (OSError, _chock_subprocess.SubprocessError) as exc: @@ -2767,9 +2786,9 @@ def _tool_call_script(spec, root, payload, deadline=None): if not script.is_file(): return ('deny', f'script gate: {named!r} is not installed{_TOOL_CALL_UNDECIDED}') deadline = engine_deadline() if deadline is None else deadline - argv = [sys.executable, str(script)] + argv, env = isolated_child([sys.executable, str(script)]) try: - proc = run_tree(argv, input=json.dumps(payload), capture_output=True, text=True, encoding='utf-8', errors='replace', cwd=str(root), env={**_chock_os.environ, **BYTECODE_OFF}, timeout=time_left(deadline, argv), check=False) + proc = run_tree(argv, input=json.dumps(payload), capture_output=True, text=True, encoding='utf-8', errors='replace', cwd=str(root), env=env, timeout=time_left(deadline, argv), check=False) except _chock_subprocess.TimeoutExpired as exc: budget = f'gave no verdict within {allowed_seconds(exc)}' return ('deny', f'script gate: {script.name} {budget}{_TOOL_CALL_UNDECIDED}') diff --git a/tests/fixtures/runtime_goldens/antigravity.py b/tests/fixtures/runtime_goldens/antigravity.py index 87741342..f9e41494 100644 --- a/tests/fixtures/runtime_goldens/antigravity.py +++ b/tests/fixtures/runtime_goldens/antigravity.py @@ -748,7 +748,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/claude_code.py b/tests/fixtures/runtime_goldens/claude_code.py index 558e7fbc..86885f6d 100644 --- a/tests/fixtures/runtime_goldens/claude_code.py +++ b/tests/fixtures/runtime_goldens/claude_code.py @@ -855,7 +855,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/codex_cli.py b/tests/fixtures/runtime_goldens/codex_cli.py index 3cbb9f52..0a9db955 100644 --- a/tests/fixtures/runtime_goldens/codex_cli.py +++ b/tests/fixtures/runtime_goldens/codex_cli.py @@ -823,7 +823,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/cursor.py b/tests/fixtures/runtime_goldens/cursor.py index 6db1a9cd..42fab5ed 100644 --- a/tests/fixtures/runtime_goldens/cursor.py +++ b/tests/fixtures/runtime_goldens/cursor.py @@ -721,7 +721,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/devin.py b/tests/fixtures/runtime_goldens/devin.py index cccf63ea..19b17565 100644 --- a/tests/fixtures/runtime_goldens/devin.py +++ b/tests/fixtures/runtime_goldens/devin.py @@ -826,7 +826,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/factory_droid.py b/tests/fixtures/runtime_goldens/factory_droid.py index bc4eb977..c78e01c2 100644 --- a/tests/fixtures/runtime_goldens/factory_droid.py +++ b/tests/fixtures/runtime_goldens/factory_droid.py @@ -809,7 +809,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/gemini_cli.py b/tests/fixtures/runtime_goldens/gemini_cli.py index 964ae941..9a290d83 100644 --- a/tests/fixtures/runtime_goldens/gemini_cli.py +++ b/tests/fixtures/runtime_goldens/gemini_cli.py @@ -826,7 +826,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/goose.py b/tests/fixtures/runtime_goldens/goose.py index ba78ed46..81e9e529 100644 --- a/tests/fixtures/runtime_goldens/goose.py +++ b/tests/fixtures/runtime_goldens/goose.py @@ -782,7 +782,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/grok.py b/tests/fixtures/runtime_goldens/grok.py index f262f001..39616f2a 100644 --- a/tests/fixtures/runtime_goldens/grok.py +++ b/tests/fixtures/runtime_goldens/grok.py @@ -826,7 +826,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/mistral_vibe.py b/tests/fixtures/runtime_goldens/mistral_vibe.py index 3b865f11..e6fe07a3 100644 --- a/tests/fixtures/runtime_goldens/mistral_vibe.py +++ b/tests/fixtures/runtime_goldens/mistral_vibe.py @@ -809,7 +809,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/openhands.py b/tests/fixtures/runtime_goldens/openhands.py index f852c40f..c6f71ac7 100644 --- a/tests/fixtures/runtime_goldens/openhands.py +++ b/tests/fixtures/runtime_goldens/openhands.py @@ -782,7 +782,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/tabnine.py b/tests/fixtures/runtime_goldens/tabnine.py index 8fd2cb93..29141032 100644 --- a/tests/fixtures/runtime_goldens/tabnine.py +++ b/tests/fixtures/runtime_goldens/tabnine.py @@ -782,7 +782,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/vscode_copilot.py b/tests/fixtures/runtime_goldens/vscode_copilot.py index d62f569f..bab53915 100644 --- a/tests/fixtures/runtime_goldens/vscode_copilot.py +++ b/tests/fixtures/runtime_goldens/vscode_copilot.py @@ -717,7 +717,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): diff --git a/tests/fixtures/runtime_goldens/windsurf.py b/tests/fixtures/runtime_goldens/windsurf.py index a8672de9..74cee413 100644 --- a/tests/fixtures/runtime_goldens/windsurf.py +++ b/tests/fixtures/runtime_goldens/windsurf.py @@ -680,7 +680,7 @@ def degrade(decision, event): # Not self-contained: chock's guard logic lives in the sibling _chock_runtime.py, loaded from this # file's own folder and pinned to the sha256 of that file's bytes (CRLF read as LF), hashed before they are executed. # Missing, unreadable, another version, or failing to import: every call refuses (_CHOCK_RUNTIME_FAULT), never allows. -_CHOCK_RUNTIME_PIN = '8e3b4b73f866de5887946c8afe0dee895548a5d67146470d63350fa84b08b5f2' +_CHOCK_RUNTIME_PIN = '791b92ce3802e2b71819589bced3f7c5fd079bf535cf469a785480a3a31c91f7' def _chock_load_runtime(): From 02c713fddc768edb4a593adc8ffa740c381a7568 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:29:55 +0000 Subject: [PATCH 26/57] fix(floor): close the control plane under links; history, attributes, list, config, SARIF (W2-A1v2.2) Rulings S1-S8 of the floor's fourth pass: - S1: every symlink at, inside or above the control plane is resolved link by link from the trees at base and head; it must open a control-plane path or nothing, else exit 2 naming the link and the fix (a loop, an absolute or out-of-repository target, or a path through a submodule is a failure). The links passed on the way are control plane. A base `.chock` that is a link or a submodule is exit 2, never first adoption. Replaces through_links. - S2: a control-plane path any commit in base..head leaves in a state the base lacks is listed, even when the tree diff is empty. - S3: every git call reads attributes from the base (GIT_ATTR_SOURCE; a git that ignores it is exit 2); .gitattributes leaves the default list. - S4: agent folders match at any depth; .env by exact name at any depth; .env.* dropped; .devcontainer/** and .devcontainer.json added. - S5: add `name/` means name/**; a bare folder is exit 2; an entry that matches nothing is marked so on the in-force line. - S6: every run writes the SARIF asked for; an undecided run says so. - S7: docs/floor.md; the two "cannot bypass" lines and the changelog corrected. - S8: the pass-4 proofs ported as regression tests asserting the owed exit. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2a1-check-floor.md | 19 +- docs/README.md | 1 + docs/assurance-case.md | 3 +- docs/enforcement-surfaces.md | 4 +- docs/floor.md | 67 +++++++ src/chock/data/control_plane.json | 41 +++-- src/chock/validation/checks_floor.py | 74 +++++--- src/chock/validation/control_plane.py | 189 +++++++++++++++----- src/chock/validation/floor_cli.py | 3 +- src/chock/validation/floor_git.py | 59 +++++- src/chock/validation/floor_outcome.py | 6 + tests/floor_support4.py | 60 +++++++ tests/test_floor_attacks.py | 2 +- tests/test_floor_control_plane.py | 21 ++- tests/test_floor_control_plane_list.py | 4 +- tests/test_floor_review3_exit.py | 4 +- tests/test_floor_review3_plane.py | 36 +++- tests/test_floor_review4_config.py | 109 +++++++++++ tests/test_floor_review4_exit.py | 238 +++++++++++++++++++++++++ tests/test_floor_review4_interact.py | 120 +++++++++++++ tests/test_floor_review4_links.py | 202 +++++++++++++++++++++ tests/test_floor_review4_list.py | 34 ++++ tests/test_floor_review4_ordinary.py | 86 +++++++++ tests/test_floor_review4_rulings.py | 201 +++++++++++++++++++++ 24 files changed, 1466 insertions(+), 117 deletions(-) create mode 100644 docs/floor.md create mode 100644 tests/floor_support4.py create mode 100644 tests/test_floor_review4_config.py create mode 100644 tests/test_floor_review4_exit.py create mode 100644 tests/test_floor_review4_interact.py create mode 100644 tests/test_floor_review4_links.py create mode 100644 tests/test_floor_review4_list.py create mode 100644 tests/test_floor_review4_ordinary.py create mode 100644 tests/test_floor_review4_rulings.py diff --git a/changelog.d/w2a1-check-floor.md b/changelog.d/w2a1-check-floor.md index a7656b49..a047059d 100644 --- a/changelog.d/w2a1-check-floor.md +++ b/changelog.d/w2a1-check-floor.md @@ -7,16 +7,23 @@ reported as `floor/control-plane-changed` with every path, first. The list: `.chock/**`, `chock.lock`, the whole folder of every agent chock or agentseam writes into (`.agents/**`, `.claude/**`, `.codex/**`, `.cursor/**`, `.gemini/**`, `.windsurf/**`, `.github/hooks/**` and the rest, plugin folders such as `.claude-plugin/**`), - `.mcp.json`, `.vscode/settings.json`, `.vscode/mcp.json`, `.env`, `.env.*`, `.github/workflows/**`, - `.github/actions/**`, `CODEOWNERS`, `.gitattributes`; matched case-blind. A symlink or gitlink at a parent - folder of a pattern (`.gemini -> cfg`) matches, and a symlinked control-plane folder's in-repo target is - control plane (read from the trees at base and head). Root instruction files (`AGENTS.md`, `CLAUDE.md`) are + `.mcp.json`, `.vscode/settings.json`, `.vscode/mcp.json`, `.env` (at any depth), `.devcontainer/**`, + `.devcontainer.json`, `.github/workflows/**`, `.github/actions/**`, `CODEOWNERS`; agent folders match at any + depth (`**/.claude/**`); matched case-blind. A symlink or gitlink at a parent folder of a pattern + (`.gemini -> cfg`) matches. Every symlink at, inside or above the control plane must resolve, link by link + from the trees at base and head, to a control-plane path or to nothing; otherwise (and for a loop, an absolute + or out-of-repository target, or one through a submodule) the run is undecided (exit 2), naming the link and the + fix. A base whose `.chock` is a link or a submodule is undecided, never first adoption. A control-plane path a + commit in the range changed is named even when the tree diff leaves it as at the base. Every git call reads + attributes from the base (`GIT_ATTR_SOURCE`; git 2.42 or newer, else undecided), so `.gitattributes` is off + the list. Root instruction files (`AGENTS.md`, `CLAUDE.md`) are not on it. These files define the guardrails; the floor does not judge whether a change to them is safe. A person decides. The base's `.chock/config.yaml` sets `floor.control_plane`: `refuse` (default, exit 1: every guardrail change, including a policy upgrade, a `chock sync` regeneration or a workflow edit, needs a privileged merge) or `listed` (exit 0, paths still listed and in the SARIF; the adopter relies on code-owner review, and content inside listed paths is not judged). `floor.control_plane_paths: {add, remove}` edits the - list; a `remove` entry that names no default pattern is undecided (exit 2). Every run prints the mode, the + list; a `remove` entry that names no default pattern is undecided (exit 2); an `add` entry `name/` means + `name/**`, a bare folder name is undecided, and an entry matching nothing at base or head is marked so. Every run prints the mode, the number of patterns and the base's additions and removals. The head's config is never read. - The list is what chock knows about, not a proof of completeness. If your workflow installs chock from a file in the repository (`requirements*.txt`, `pyproject.toml`, a lockfile, a local action), add that file to the list. @@ -33,6 +40,8 @@ utf-16 in both byte orders and alignments, lines split on `\n` only. A range past the commit limit or a blob over 64 MiB is undecided, naming the commit and path. Not seen: other encodings, compressed or encoded content, commit messages, file names. +- `--sarif` writes a log on every run; an undecided run's says `executionSuccessful: false`. Not covered: a + Windows short name (`CLAUDE~1/`), `*.code-workspace`, submodule content (docs/floor.md). - It holds only as a required status check the pull request cannot redefine; that is the adopter's setting, which chock does not verify. It judges committed content only. - `chock history`'s `load_rules` takes the compiled surfaces to read; its own defaults are unchanged. diff --git a/docs/README.md b/docs/README.md index 2e186dfc..00a100f6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -24,6 +24,7 @@ Everything you need to understand, use, and extend Chock. New here? Start with | [Agentic-Risk Coverage](agentic-risk-coverage.md) | Problem-first index: what Chock stops, at which honest tier — and what it doesn't | | [Agent Plugins](agent-plugins.md) | Package policies for the open standard — and what that does not buy you | | [Validation](validation.md) | Understand what `chock check` checks | +| [CI Floor](floor.md) | What `chock check --floor` (preview) claims, the conditions it holds under, and what it does not cover | | [Registry & Lockfile](registry-and-lockfile.md) | Reproducible, hash-pinned distribution | | [Compatibility](compatibility.md) | What may change between versions, and what pinning guarantees | | [Reviewer Evidence](reviewer-evidence.md) | Recording what a review rests on, so the next reader can re-derive it | diff --git a/docs/assurance-case.md b/docs/assurance-case.md index 0c68ed96..c4000b52 100644 --- a/docs/assurance-case.md +++ b/docs/assurance-case.md @@ -26,7 +26,8 @@ discredits every honest number. Boundaries: (1) the adopter repository (trusted content, versioned); (2) the agent session (untrusted executor — it may ignore, misread, or attempt to modify guidance); (3) the catalog (semi-trusted remote — executable content, trust-on-first-use, hash -pinned); (4) CI (the enforcement floor — the one surface a local actor cannot bypass). +pinned); (4) CI (the enforcement floor, and only where its check is a required status check the pull request cannot +redefine — the repository's setting, which chock does not verify; see [the CI floor](floor.md)). Threat classes defended against: diff --git a/docs/enforcement-surfaces.md b/docs/enforcement-surfaces.md index f6ec53eb..af380fb1 100644 --- a/docs/enforcement-surfaces.md +++ b/docs/enforcement-surfaces.md @@ -88,8 +88,8 @@ each guarantee holds. > nested repository, or a clone never synced) the command exits 2 the same way. Installed entries equal compiled ones exactly, so `chock sync` on any machine > is a zero diff; entries in the old baked-interpreter form are recognised and replaced. -**`git-hook` + `ci-gate` are the universal hard floor** every agent shares. `pre-tool-use` and -`agent-hooks` are the premium tier on agents that expose native controls; membership +**`git-hook` + `ci-gate` are the universal floor** every agent shares (`ci-gate` holds only as a required check the +PR cannot redefine: [the CI floor](floor.md)). `pre-tool-use` and `agent-hooks` are the premium tier on agents that expose native controls; membership derives from agentseam's matrix (see the coverage matrix below). `gateway` is reserved for cost/egress controls on the roadmap. `mcp-gateway` ([#32](https://github.com/open-coder-ai/chock/issues/32)) governs exactly the MCP slice: diff --git a/docs/floor.md b/docs/floor.md new file mode 100644 index 00000000..a021fff1 --- /dev/null +++ b/docs/floor.md @@ -0,0 +1,67 @@ +# The CI floor: `chock check --floor` (preview) + +`chock check --floor --base --head ` judges what a pull request will land, using the +**base's** gates, run by this engine. It is a preview, and it holds only under the conditions below. + +## What it claims + +- **Judge what will land.** The base must be an ancestor of the head; pass GitHub's `refs/pull//merge`, or the + merge queue's head. Everything comes from the tree diff `base..head` (no renames, no external diff or textconv) + and from the commits in it. Every git call reads attributes from the base (`GIT_ATTR_SOURCE`, git 2.42 or newer), + so a `.gitattributes` the pull request adds changes nothing the floor sees. +- **Control plane.** A change (add, edit, delete, mode) to a path on the list in + `src/chock/data/control_plane.json` is reported as `floor/control-plane-changed`, every path, one per line. A + path that a commit in the range changed, even if a later commit changed it back, is named too. These files + define the guardrails; the floor does not judge whether a change to them is safe. A person decides. +- **Links.** Every symbolic link at, inside or above a control-plane path is resolved from the trees at base and + head, link by link. If it opens a path that is itself control plane, or nothing, the run carries on, and each + link passed on the way is control plane too. Otherwise the run is undecided (exit 2), naming the link and its + target. So is a loop, an absolute target, a target outside the repository, or one that passes through a + submodule. The fix: put the target on `floor.control_plane_paths.add` in the base's config, or replace the link + with real files. A base whose `.chock` is a link or a submodule is undecided too. +- **Everything else** is judged by the base's compiled ci gates and rollout. No file from the head executes. + `ask` refuses. A gate that crashes, times out or cannot be read is undecided, whatever the rollout. A waiver + counts only on a line present and unchanged in both base and head. +- **History.** The base's content gates read every blob a commit in the range adds or changes outside the control + plane. +- **Exit codes:** 0 clean, 1 refused, 2 undecided. `--sarif ` writes a log on every run, with + `executionSuccessful: false` when the run is undecided. + +## Configuration (the base's `.chock/config.yaml`; the head's is never read) + +```yaml +floor: + control_plane: refuse # default: exit 1 on any listed path. `listed`: exit 0, paths still listed + control_plane_paths: + add: [infra/] # `name/` or `name/**` is the folder and all in it; a bare folder name is exit 2 + remove: ["**/.env"] # must name a default pattern exactly, or the run is exit 2 +``` + +Each run prints the mode, the number of patterns and the base's additions and removals. An added entry that +matches no path at base or head is marked as matching nothing. + +## Conditions and limits + +- **A required status check the pull request cannot redefine.** The floor holds only as one. That is the + repository's own setting, and chock does not verify it. +- **Committed content only.** +- **Under `refuse`,** every guardrail change needs a privileged merge: a policy upgrade, a `chock sync` + regeneration or a workflow edit. +- **Under `listed`,** you rely on code-owner review of the listed paths. The content inside them is not judged. +- **The list is what chock knows about,** not a proof of completeness. If your workflow installs chock from a file + in the repository (`requirements*.txt`, `pyproject.toml`, a lockfile, a local action), add that file to the + list. +- **Renames.** Renaming or moving a test file reads as a deletion, and test-integrity refuses it. + +## Not covered + +- **Windows short names.** The 8.3 alias of a listed folder (`CLAUDE~1/...` for `.claude/`) is not matched. This + is untested on Windows. +- **`*.code-workspace` files.** They are not on the list: it is not verified that an editor honours hook settings + from one. +- **Submodule content.** Only the pointer is seen. A pointer at or inside a control-plane folder is listed, but a + submodule's own agent folders are not read. +- **What the history pass does not see:** other encodings (utf-32, compressed or encoded content), commit + messages and file names. +- **Back-merges.** A path is named from history when a commit leaves it in a state the base does not have. A + branch that merged an older base, from before a later change to that path, is named for it. diff --git a/src/chock/data/control_plane.json b/src/chock/data/control_plane.json index 7618164c..426a702d 100644 --- a/src/chock/data/control_plane.json +++ b/src/chock/data/control_plane.json @@ -1,23 +1,23 @@ { - "$comment": "The paths that define the guardrails (`chock check --floor`, floor/control-plane-changed). Matched with fnmatch after case folding, so `*` crosses `/`; a symlink or gitlink at a parent folder of a pattern matches too, and a symlinked control-plane folder's in-repo target is control plane. A pull request that adds, changes, deletes or re-modes one is listed; the base's `.chock/config.yaml` decides refuse or listed. Whole folder for every agent chock or agentseam writes into (hooks, permissions, MCP, plugins, skills, subagents), plus the engine's CI workflow and store files; tests/test_floor_control_plane_list.py checks the list against the engine's own data. Instruction files at the root (AGENTS.md, CLAUDE.md) are left out: they steer an agent, they are not guardrails. What chock knows about, not a proof of completeness.", + "$comment": "The paths that define the guardrails (`chock check --floor`, floor/control-plane-changed). Matched with fnmatch after case folding, so `*` crosses `/`; a leading `**/` also matches at the top level; a symlink or gitlink at a parent folder of a pattern matches too. Every symlink the control plane reaches must resolve, through the trees at base and head, to a control-plane path, or the run is undecided. A pull request that adds, changes, deletes or re-modes one, in the tree diff or in any of its commits, is listed; the base's `.chock/config.yaml` decides refuse or listed. Whole folder, at any depth, for every agent chock or agentseam writes into (hooks, permissions, MCP, plugins, skills, subagents), plus the engine's CI workflow and store files; tests/test_floor_control_plane_list.py checks the list against the engine's own data. Instruction files (AGENTS.md, CLAUDE.md) are left out: they steer an agent, they are not guardrails. `.gitattributes` is left out: the floor reads attributes from the base. What chock knows about, not a proof of completeness.", "paths": [ ".chock/**", "chock.lock", - ".agents/**", - ".claude/**", - ".clinerules/**", - ".codex/**", - ".cursor/**", - ".devin/**", - ".factory/**", - ".gemini/**", - ".grok/**", - ".junie/**", - ".kimi-code/**", - ".openhands/**", - ".tabnine/**", - ".vibe/**", - ".windsurf/**", + "**/.agents/**", + "**/.claude/**", + "**/.clinerules/**", + "**/.codex/**", + "**/.cursor/**", + "**/.devin/**", + "**/.factory/**", + "**/.gemini/**", + "**/.grok/**", + "**/.junie/**", + "**/.kimi-code/**", + "**/.openhands/**", + "**/.tabnine/**", + "**/.vibe/**", + "**/.windsurf/**", ".claude-plugin/**", ".cursor-plugin/**", ".devin-plugin/**", @@ -29,14 +29,13 @@ ".mcp.json", ".vscode/settings.json", ".vscode/mcp.json", - ".env", - ".env.*", + "**/.env", + ".devcontainer/**", + ".devcontainer.json", ".github/workflows/**", ".github/actions/**", ".github/CODEOWNERS", "CODEOWNERS", - "docs/CODEOWNERS", - ".gitattributes", - "**/.gitattributes" + "docs/CODEOWNERS" ] } diff --git a/src/chock/validation/checks_floor.py b/src/chock/validation/checks_floor.py index 85adaeca..653c02ff 100644 --- a/src/chock/validation/checks_floor.py +++ b/src/chock/validation/checks_floor.py @@ -34,7 +34,14 @@ from chock.validation import control_plane as cp from chock.validation import floor_sarif from chock.validation.floor_context import FloorContext -from chock.validation.floor_git import FloorError, Revision, changed_paths, require_ancestor, resolve, toplevel +from chock.validation.floor_git import ( + FloorError, + Revision, + attributes_from, + require_ancestor, + resolve, + toplevel, +) from chock.validation.floor_history import history_findings from chock.validation.floor_outcome import Outcome @@ -43,6 +50,8 @@ _SURFACES = ("ci-gate", "git-hook") _SCRIPT, _DEPENDENCY, _REF = "script", _DEPENDENCY_KIND, "forbidden_ref" _REFUSING = (ACTION_BLOCK, ACTION_ASK) +_CHOCK = ".chock" +_HISTORY_ONLY = " (in a commit of the range only; the tree diff leaves it as at the base)" _FIRST_ADOPTION = "floor: base has no chock gates; nothing was judged" _NO_GATES = "floor: the base has no compiled ci gates; no gate judged the content (the control-plane rule still ran)" _CONTROL_PLANE = ( @@ -180,11 +189,16 @@ def _declared(base: Revision, policy: str) -> str: def _judged(repo: Path, base: Revision, head: Revision, branch: str | None) -> Outcome: - plane = cp.through_links(cp.from_base(base), base, head) - listed = [path for path in changed_paths(repo, base.sha, head.sha) if plane.covers(path)] + plane = cp.closed_under_links(cp.from_base(base, head), base, head) + listed, history_only = plane.changes(repo, base, head) level = committed_rollout(repo, base.sha) outcome = judge_gates(repo, base, head, branch, plane=plane, level=level) - outcome.listed, outcome.mode, outcome.in_force = listed, plane.mode, plane.summary() + outcome.listed, outcome.history_only, outcome.mode, outcome.in_force = ( + listed, + history_only, + plane.mode, + plane.summary(), + ) if not base_gates(base): outcome.notes.append(_NO_GATES) try: @@ -208,7 +222,7 @@ def _print(outcome: Outcome) -> None: text = _CONTROL_PLANE.format(rule=cp.RULE, mode=outcome.mode, count=len(outcome.listed)) _out(f"{head} {text}") for path in outcome.listed: - _out(f" {inert(path)}") + _out(f" {inert(path)}{_HISTORY_ONLY if path in outcome.history_only else ''}") if outcome.mode == cp.LISTED: _out(f"note: {_LISTED_GAP.format(rule=cp.RULE)}") for line in outcome.notes: @@ -227,19 +241,35 @@ def _code(outcome: Outcome) -> int: return EXIT_REFUSED if refused else 0 -def floor(repo: Path, base_ref: str, head_ref_rev: str, branch: str | None, sarif: str | None = None) -> int: - repo = toplevel(repo) +def _base_chock(base: Revision) -> bool: + """Whether the base commits chock gates; FloorError (undecided) when its `.chock` is a link or a submodule.""" + if base.state(_CHOCK) is not None: + msg = f"the base's {_CHOCK} is a symbolic link or a submodule; the floor reads gates only from files under {_CHOCK}/" + raise FloorError(msg) + return bool(base.paths(f"{_CHOCK}/")) + + +def floor(repo: Path, base_ref: str, head_ref_rev: str, branch: str | None) -> Outcome: base_sha, head_sha = resolve(repo, base_ref, "base"), resolve(repo, head_ref_rev, "head") require_ancestor(repo, base_sha, head_sha) _out(f"== floor (preview): base {base_sha[:12]}, head {head_sha[:12]}") base, head = Revision(repo, base_sha), Revision(repo, head_sha) - if not base.paths(".chock/"): - _out(_FIRST_ADOPTION) - return 0 - outcome = _judged(repo, base, head, branch) - _print(outcome) - code = _code(outcome) - return floor_sarif.write(repo, sarif, outcome, code) if sarif else code + if not _base_chock(base): + return Outcome(first_adoption=True) + return _judged(repo, base, head, branch) + + +def _run(repo: Path, args: argparse.Namespace) -> tuple[Outcome, Path]: + """(outcome, top level), every attribute read from the base; an error that stops the run is one undecided line.""" + try: + repo = toplevel(repo) + with attributes_from(repo, args.base): + return floor(repo, args.base, args.head, args.head_ref), repo + except FloorError as exc: + why = inert(str(exc)) + except Exception as exc: # noqa: BLE001 -- 1 means refused; anything undecided is 2, naming only the type + why = f"no verdict was established ({type(exc).__name__})" + return Outcome(undecided=[f"floor: {why}"], stopped=True), repo def main(argv: list[str] | None = None) -> int: @@ -250,10 +280,12 @@ def main(argv: list[str] | None = None) -> int: parser.add_argument("--head-ref", default=None, help="The branch under test, for branch gates") parser.add_argument("--sarif", default=None, help="Also write the result as SARIF 2.1.0 to this file") args = parser.parse_args(argv) - try: - return floor(Path(args.repo), args.base, args.head, args.head_ref, args.sarif) - except FloorError as exc: - _err(f"UNDECIDED floor: {inert(str(exc))}") - except Exception as exc: # noqa: BLE001 -- 1 means refused; anything undecided is 2, naming only the type - _err(f"UNDECIDED floor: no verdict was established ({type(exc).__name__})") - return EXIT_UNDECIDED + outcome, repo = _run(Path(args.repo), args) + if outcome.first_adoption: + _out(_FIRST_ADOPTION) + elif outcome.stopped: + _err(f"UNDECIDED {outcome.undecided[0]}") + else: + _print(outcome) + code = _code(outcome) + return floor_sarif.write(repo, args.sarif, outcome, code) if args.sarif else code diff --git a/src/chock/validation/control_plane.py b/src/chock/validation/control_plane.py index fc3624a8..d0762d67 100644 --- a/src/chock/validation/control_plane.py +++ b/src/chock/validation/control_plane.py @@ -6,19 +6,20 @@ from __future__ import annotations +import bisect import dataclasses import fnmatch import functools import json -import posixpath import unicodedata from dataclasses import dataclass from importlib import resources +from pathlib import Path import yaml from chock import yamlio -from chock.validation.floor_git import FloorError, Revision +from chock.validation.floor_git import FloorError, Revision, changed_paths, history_states RULE = "floor/control-plane-changed" REFUSE, LISTED = "refuse", "listed" @@ -29,6 +30,8 @@ _FORMAT = "Cf" _SYMLINK = "120000" _GLOB = frozenset("*?[") +_ANY_DEPTH = "**/" +_MAX_HOPS = 32 def default_patterns() -> tuple[str, ...]: @@ -52,69 +55,139 @@ def _parents(pattern: str) -> set[str]: return {"/".join(literal[:k]) for k in range(1, len(literal) + 1) if k < len(parts)} +def _match(name: str, pattern: str) -> bool: + """fnmatch on folded text; a leading `**/` also matches at the top level.""" + pat = folded(pattern) + return fnmatch.fnmatchcase(name, pat) or (pat.startswith(_ANY_DEPTH) and fnmatch.fnmatchcase(name, pat[3:])) + + @dataclass(frozen=True) class ControlPlane: mode: str patterns: tuple[str, ...] added: tuple[str, ...] = () removed: tuple[str, ...] = () - #: (folded in-repo target, link path) for each symlink the control plane reaches through, at base or head. - links: tuple[tuple[str, str], ...] = () + #: Entries of `added` that match no path at base or head, named as such in the summary. + unmatched: tuple[str, ...] = () + #: Folded paths of the symlinks resolving a control-plane link passes through, at base or head. + hops: frozenset[str] = frozenset() def _direct(self, name: str) -> bool: - return any(fnmatch.fnmatchcase(name, folded(p)) or name in _parents(p) for p in self.patterns) + return any(_match(name, p) or _match(f"{name}/", p) or name in _parents(p) for p in self.patterns) - def covers(self, path: str, _depth: int = 0) -> bool: - """Whether `path`, or the path a client opens through a symlinked control-plane folder, is control plane.""" + def covers(self, path: str) -> bool: + """Whether `path`, a folder above it that a client opens it through, or a link on the way, is control plane.""" name = folded(path) - if self._direct(name): - return True - if _depth > len(self.links): - return False - rests = ((link, _under(name, target)) for target, link in self.links) - return any(self.covers(link + rest, _depth + 1) for link, rest in rests if rest is not None) + return name in self.hops or self._direct(name) + + def changes(self, repo: Path, base: Revision, head: Revision) -> tuple[list[str], set[str]]: + """(paths of the plane the tree diff changes or any commit leaves in a state the base lacks, those only + in history).""" + diff = {path for path in changed_paths(repo, base.sha, head.sha) if self.covers(path)} + states = history_states(repo, base.sha, head.sha) + history = {p for p, state in states if p not in diff and self.covers(p) and state != base.state(p)} + return sorted(diff | history), history def summary(self) -> str: """The list in force: mode, size, and the base's own edits to the defaults.""" - added, removed = ", ".join(self.added) or "none", ", ".join(self.removed) or "none" + shown = [f"{p} (matches nothing at base or head)" if p in self.unmatched else p for p in self.added] + added, removed = ", ".join(shown) or "none", ", ".join(self.removed) or "none" return ( f"control plane: {self.mode}, {len(self.patterns)} pattern(s); base added: {added}; base removed: {removed}" ) -def _under(name: str, target: str) -> str | None: - """`name` relative to `target` ("" when equal, "/rest" below it; the root is ""), None when outside it.""" - if not target: - return f"/{name}" - if name == target or name.startswith(f"{target}/"): - return name[len(target) :] - return None - - -def _target(link: str, data: bytes | None) -> str | None: - """The in-repo folder or file a committed symlink names; None when it leaves the repository.""" - if not data: - return None - joined = posixpath.normpath(posixpath.join(posixpath.dirname(link), data.decode("utf-8", "replace"))) - if posixpath.isabs(joined) or joined.split("/")[0] == "..": - return None - return "" if joined == "." else folded(joined) - - -def through_links(plane: ControlPlane, *revisions: Revision) -> ControlPlane: - """`plane` plus every in-repo target of a symlink it covers at any of `revisions`, followed to a fixed point.""" - symlinks = [(rev, path) for rev in revisions for path, (mode, _) in rev.entries().items() if mode == _SYMLINK] - links: list[tuple[str, str]] = [] - grew = True - while grew: - grew = False - current = dataclasses.replace(plane, links=tuple(links)) - for rev, path in symlinks: - target = _target(path, rev.blob(path)) if current.covers(path) else None - if target is not None and (target, folded(path)) not in links: - links.append((target, folded(path))) - grew = True - return dataclasses.replace(plane, links=tuple(links)) +class _Tree: + """One revision's symlinks, gitlinks and paths, folded, for resolving links from tree data alone.""" + + def __init__(self, rev: Revision) -> None: + entries = rev.entries() + self.rev = rev + self.links = {folded(p): p for p, (mode, _) in entries.items() if mode == _SYMLINK} + self.gitlinks = {folded(p) for p in rev.gitlinks()} + self.files = {folded(p) for p in entries} | self.gitlinks + self.folders = {f.rsplit("/", k)[0] for f in self.files for k in range(1, f.count("/") + 1)} + self._sorted = sorted(self.files) + + def resolve(self, link: str) -> tuple[str, list[str]]: + """(folded in-repo path `link` opens, links passed); FloorError naming the link when it cannot resolve.""" + todo, done, hops = link.split("/"), [], [] + while todo: + part = todo.pop(0) + part = part if part == ".." else folded(part) + if not part: + continue + if part == "..": + if not done: + raise _unresolved(link, self.rev, "its target leaves the repository") + done.pop() + continue + here = "/".join([*done, part]) + if here in self.gitlinks and todo: + raise _unresolved(link, self.rev, f"its target passes through the submodule {here}") + if here not in self.links: + done.append(part) + continue + hops.append(here) + if len(hops) > _MAX_HOPS: + raise _unresolved(link, self.rev, f"more than {_MAX_HOPS} links (a loop?)") + target = (self.rev.blob(self.links[here]) or b"").decode("utf-8", "replace") + if not target or target.startswith("/") or "\\" in target or ":" in target.split("/")[0]: + raise _unresolved(link, self.rev, f"{here} has the absolute or empty target {target!r}") + todo[:0] = target.split("/") + return "/".join(done), hops + + def exists(self, name: str) -> bool: + return not name or name in self.files or name in self.folders + + def below(self, folder: str) -> list[str]: + """The folded paths under `folder` (the root when "").""" + if not folder: + return self._sorted + start = bisect.bisect_left(self._sorted, f"{folder}/") + return self._sorted[start : bisect.bisect_left(self._sorted, f"{folder}0")] # "0" sorts right after "/" + + +def _unresolved(link: str, rev: Revision, why: str) -> FloorError: + return FloorError( + f"control plane: the symbolic link {link} at {rev.sha[:12]} does not resolve inside the repository: {why}; " + "replace the link with real files" + ) + + +def _opens_control_plane(plane: ControlPlane, tree: _Tree, name: str) -> tuple[str, list[str]] | None: + """(target, links passed) when the link `name` is a control-plane root, inside one, or above one; else None.""" + if plane.covers(name): + return tree.resolve(tree.links[name]) + try: + target, passed = tree.resolve(tree.links[name]) + except FloorError: + return None # leaves the repository: nothing committed lies under it + rests = (f[len(target) :] if target else f"/{f}" for f in tree.below(target)) + return (target, passed) if any(plane.covers(name + rest) for rest in rests) else None + + +def closed_under_links(plane: ControlPlane, *revisions: Revision) -> ControlPlane: + """`plane` plus every link a control-plane link passes through, once each link at, inside or above the control + plane at each of `revisions` opens a control-plane path or nothing; FloorError (undecided) naming one that does not.""" + hops: set[str] = set() + for rev in revisions: + tree = _Tree(rev) + for name, link in sorted(tree.links.items()): + found = _opens_control_plane(plane, tree, name) + if found is None: + continue + target, passed = found + hops.update(passed) + if tree.exists(target) and not plane.covers(target): + shown = target or "." + msg = ( + f"control plane: the symbolic link {link} at {rev.sha[:12]} opens {shown}, which is not control " + f"plane; put {shown}/** on floor.{_PATHS}.add in the base's {_CONFIG}, or replace the link with " + "real files" + ) + raise FloorError(msg) + return dataclasses.replace(plane, hops=frozenset(hops)) def _invalid(what: str) -> FloorError: @@ -147,7 +220,24 @@ def _edits(section: dict) -> dict[str, list[str]]: return found -def from_base(base: Revision) -> ControlPlane: +def _added(entries: list[str], revisions: tuple[Revision, ...]) -> tuple[tuple[str, ...], tuple[str, ...]]: + """(patterns, entries matching no path at any of `revisions`) for the base's `add:`; `name/` means `name/**`. + FloorError (undecided) for a bare entry naming a folder, which as written would match only the folder itself.""" + trees = [_Tree(rev) for rev in revisions] if entries else [] + patterns, unmatched = [], [] + for entry in entries: + if not _GLOB & set(entry) and not entry.endswith("/") and any(folded(entry) in t.folders for t in trees): + msg = f"{_PATHS}.add names {entry!r}, a folder; write {entry + '/**'!r} for the folder and everything in it" + raise _invalid(msg) + pattern = f"{entry}**" if entry.endswith("/") else entry + alone = ControlPlane(REFUSE, (pattern,)) + if not any(alone.covers(f) for t in trees for f in t.files | t.folders): + unmatched.append(entry) + patterns.append(pattern) + return tuple(patterns), tuple(unmatched) + + +def from_base(base: Revision, head: Revision) -> ControlPlane: """The control plane the base defines; FloorError (undecided) for a `floor:` section it cannot read.""" section = _section(base) mode = section.get(_MODE, REFUSE) @@ -160,4 +250,5 @@ def from_base(base: Revision) -> ControlPlane: msg = f"{_PATHS}.remove names {unknown!r}, which is no default pattern" raise _invalid(msg) kept = tuple(p for p in defaults if p not in edits["remove"]) - return ControlPlane(mode, kept + tuple(edits["add"]), tuple(edits["add"]), tuple(edits["remove"])) + added, unmatched = _added(edits["add"], (base, head)) + return ControlPlane(mode, kept + added, tuple(edits["add"]), tuple(edits["remove"]), unmatched) diff --git a/src/chock/validation/floor_cli.py b/src/chock/validation/floor_cli.py index 1c59aaf5..693b5a30 100644 --- a/src/chock/validation/floor_cli.py +++ b/src/chock/validation/floor_cli.py @@ -16,7 +16,8 @@ def add_arguments(parser: argparse.ArgumentParser) -> None: "It holds only as a required check the pull request cannot redefine (your setting, unverified by chock) and " "judges committed content only; under `listed` you rely on code-owner review of the listed paths. The list is " "what chock knows about, not a proof of completeness (a file your workflow installs chock from belongs on it: " - "floor.control_plane_paths.add); a renamed or moved test file reads as a deletion and is refused", + "floor.control_plane_paths.add); a symlink at, inside or above the list must open a listed path or the run is " + "undecided; a renamed or moved test file reads as a deletion and is refused (docs/floor.md)", ) parser.add_argument("--head", default=None, help="With --floor: the merge result under test (default HEAD)") diff --git a/src/chock/validation/floor_git.py b/src/chock/validation/floor_git.py index 06d031fd..de90dad5 100644 --- a/src/chock/validation/floor_git.py +++ b/src/chock/validation/floor_git.py @@ -2,14 +2,20 @@ from __future__ import annotations +import contextlib import os import subprocess +from collections.abc import Iterator from dataclasses import dataclass, field from pathlib import Path _GIT_TIMEOUT = 120 +_ATTR_SOURCE = "GIT_ATTR_SOURCE" +_ATTR_GIT = "2.42" +_NO_SUCH_TREE = "chock-floor-no-such-tree" _TREE_FIELDS = 2 -_BLOB = "blob" +_BLOB, _COMMIT, _GITLINK = "blob", "commit", "160000" +_ABSENT = "000000" _REGULAR_MODES = ("100644", "100755") @@ -58,6 +64,7 @@ class Revision: repo: Path sha: str _entries: dict[str, tuple[str, str]] | None = None + _gitlinks: dict[str, str] = field(default_factory=dict) _texts: dict[str, str | None] = field(default_factory=dict) def entries(self) -> dict[str, tuple[str, str]]: @@ -73,9 +80,22 @@ def entries(self) -> dict[str, tuple[str, str]]: parts = meta.decode("ascii", "replace").split() if len(parts) > _TREE_FIELDS and parts[1] == _BLOB: found[os.fsdecode(raw_path)] = (parts[0], parts[2]) + elif len(parts) > _TREE_FIELDS and parts[1] == _COMMIT: + self._gitlinks[os.fsdecode(raw_path)] = parts[2] self._entries = found return self._entries + def gitlinks(self) -> dict[str, str]: + """path -> commit oid for every submodule pointer in the tree.""" + self.entries() + return self._gitlinks + + def state(self, path: str) -> tuple[str, str] | None: + """(mode, oid) of `path` here, a submodule pointer included; None when absent.""" + if path in self.gitlinks(): + return _GITLINK, self._gitlinks[path] + return self.entries().get(path) + def paths(self, prefix: str = "") -> list[str]: return sorted(path for path in self.entries() if path.startswith(prefix)) @@ -130,3 +150,40 @@ def changed_paths(repo: Path, base: str, head: str) -> list[str]: msg = f"git diff {base[:12]} {head[:12]} failed" raise FloorError(msg) return sorted(os.fsdecode(raw) for raw in proc.stdout.split(b"\0") if raw) + + +def history_states(repo: Path, base: str, head: str) -> list[tuple[str, tuple[str, str] | None]]: + """(path, (mode, oid) or None when deleted) for every entry each commit in base..head writes, a merge read + against each parent, as the history pass reads them.""" + args = ("log", "-z", "--raw", "-m", "--no-renames", "--no-abbrev", "--no-ext-diff", "--no-textconv") + proc = git(repo, *args, "--format=", f"{base}..{head}", "--") + if proc.returncode: + msg = f"git log {base[:12]}..{head[:12]} failed" + raise FloorError(msg) + tokens, found = iter(proc.stdout.split(b"\0")), [] + for raw in tokens: + meta = raw.lstrip(b"\n").decode("ascii", "replace") + if not meta.startswith(":"): + continue + mode, oid = meta[1:].split(" ")[1], meta[1:].split(" ")[3] + found.append((os.fsdecode(next(tokens, b"")), None if mode == _ABSENT else (mode, oid))) + return found + + +@contextlib.contextmanager +def attributes_from(repo: Path, base_ref: str) -> Iterator[None]: + """Every git the floor runs reads attributes from the base's tree, never the checkout's or the head's; + FloorError (undecided) when this git ignores `GIT_ATTR_SOURCE`.""" + saved = os.environ.get(_ATTR_SOURCE) + os.environ[_ATTR_SOURCE] = _NO_SUCH_TREE + try: + if git(repo, "-c", "core.fsmonitor=false", "check-attr", "-a", "--", "probe").returncode == 0: + msg = f"this git ignores {_ATTR_SOURCE}; the floor needs git {_ATTR_GIT} or newer to read attributes from the base" + raise FloorError(msg) + os.environ[_ATTR_SOURCE] = resolve(repo, base_ref, "base") + yield + finally: + if saved is None: + os.environ.pop(_ATTR_SOURCE, None) + else: + os.environ[_ATTR_SOURCE] = saved diff --git a/src/chock/validation/floor_outcome.py b/src/chock/validation/floor_outcome.py index b68f5ada..eec56bf0 100644 --- a/src/chock/validation/floor_outcome.py +++ b/src/chock/validation/floor_outcome.py @@ -15,3 +15,9 @@ class Outcome: listed: list[str] = field(default_factory=list) mode: str = cp.REFUSE in_force: str = "" + #: Listed paths that only a commit of the range changed; the tree diff leaves them as at the base. + history_only: set[str] = field(default_factory=set) + #: The base has no `.chock`: nothing was judged (V6). + first_adoption: bool = False + #: An error stopped the run before any verdict; `undecided` holds its one line. + stopped: bool = False diff --git a/tests/floor_support4.py b/tests/floor_support4.py new file mode 100644 index 00000000..67920fae --- /dev/null +++ b/tests/floor_support4.py @@ -0,0 +1,60 @@ +"""Review 4 helpers (pass-4 proofs): rewrite the base on `main`, then branch `pr` from it.""" + +from __future__ import annotations + +import os +import shutil +from pathlib import Path + +from floor_support import base_repo, commit, git, run_floor, write # noqa: F401 + +FAKE_A = "1" * 40 +FAKE_B = "2" * 40 + + +def rebase_main(repo: Path, change) -> None: + """Apply `change(repo)` on main as a new base commit, then put `pr` back on it.""" + git(repo, "checkout", "-q", "main") + change(repo) + commit(repo, "base edit") + git(repo, "checkout", "-q", "-B", "pr") + + +def raw_commit(repo: Path, message: str) -> str: + """Commit the index as it stands (no `add -A`), so a hand-made gitlink survives.""" + git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "--no-gpg-sign", "-m", message) + return git(repo, "rev-parse", "HEAD") + + +def gitlink(repo: Path, path: str, sha: str) -> None: + git(repo, "update-index", "--add", "--cacheinfo", f"160000,{sha},{path}") + + +def symlink(repo: Path, path: str, target: str) -> None: + link = repo / path + link.parent.mkdir(parents=True, exist_ok=True) + if link.is_symlink() or link.is_file(): + link.unlink() + elif link.is_dir(): + shutil.rmtree(link) + os.symlink(target, link) + + +def rm(repo: Path, path: str) -> None: + p = repo / path + if p.is_symlink() or p.is_file(): + p.unlink() + elif p.is_dir(): + shutil.rmtree(p) + + +HOOKLESS = {"hooks": {}} + + +def undecided(repo: Path, capsys, link: str) -> str: + """S1: exit 2, naming the link that does not stay on the control plane, and the fix.""" + code, out = run_floor(repo, capsys) + assert code == 2, out + assert f"symbolic link {link} at" in out, out + assert "replace the link with real files" in out, out + return out diff --git a/tests/test_floor_attacks.py b/tests/test_floor_attacks.py index fbe91276..1a3229f0 100644 --- a/tests/test_floor_attacks.py +++ b/tests/test_floor_attacks.py @@ -96,7 +96,7 @@ def test_head_gitattributes_minus_diff_does_not_hide_test_weakening(tmp_path, ca code, out = run_floor(repo, capsys) assert code == 1, out assert "assertions removed across tests" in out - assert "\n .gitattributes\n" in out + assert "\n .gitattributes\n" not in out # S3: attributes come from the base, so the file is no guardrail def test_control_test_weakening_refused(tmp_path, capsys): diff --git a/tests/test_floor_control_plane.py b/tests/test_floor_control_plane.py index fa5940a7..575f06ef 100644 --- a/tests/test_floor_control_plane.py +++ b/tests/test_floor_control_plane.py @@ -38,8 +38,10 @@ def test_every_repo_relative_vendor_hook_file_is_control_plane(vendor): ".github/CODEOWNERS", "CODEOWNERS", "docs/CODEOWNERS", - ".gitattributes", - "src/.gitattributes", + "pkg/.claude/settings.json", + "a/b/.codex/config.toml", + "pkg/.env", + ".devcontainer/devcontainer.json", ".Chock/Config.yaml", ".claude/SETTINGS.json", ".claude/settings.json.", @@ -51,7 +53,20 @@ def test_guardrail_paths_match_in_any_spelling_a_filesystem_may_open(path): @pytest.mark.parametrize( - "path", ["src/app.py", "AGENTS.md", "CLAUDE.md", "docs/guide.md", "chock.lock.bak", "tests/x", ".envrc"] + "path", + [ + "src/app.py", + "AGENTS.md", + "CLAUDE.md", + "docs/guide.md", + "chock.lock.bak", + "tests/x", + ".envrc", + ".gitattributes", + "src/.gitattributes", + ".env.example", + "x.env", + ], ) def test_ordinary_paths_are_not_control_plane(path): assert not DEFAULT.covers(path) diff --git a/tests/test_floor_control_plane_list.py b/tests/test_floor_control_plane_list.py index 862d2fe5..62c9a118 100644 --- a/tests/test_floor_control_plane_list.py +++ b/tests/test_floor_control_plane_list.py @@ -86,8 +86,6 @@ def test_root_instruction_files_stay_out(path): assert not DEFAULT.covers(path), path -@pytest.mark.parametrize( - "path", [".mcp.json", ".vscode/settings.json", ".vscode/mcp.json", ".env", ".env.local", ".cursor/cli.json"] -) +@pytest.mark.parametrize("path", [".mcp.json", ".vscode/settings.json", ".vscode/mcp.json", ".env", ".cursor/cli.json"]) def test_client_config_and_env_files_named_by_the_review_are_control_plane(path): assert DEFAULT.covers(path) diff --git a/tests/test_floor_review3_exit.py b/tests/test_floor_review3_exit.py index a526c645..3cf110a0 100644 --- a/tests/test_floor_review3_exit.py +++ b/tests/test_floor_review3_exit.py @@ -9,7 +9,7 @@ import pytest from floor_support import PROBE, TEST_FILE, base_repo, commit, git, run_floor, write -from chock.validation import checks_floor, floor_git +from chock.validation import checks_floor, control_plane, floor_git from chock.validation.checks_floor import main as floor_main _IDENT = ("-c", "user.name=t", "-c", "user.email=t@example.com") @@ -68,7 +68,7 @@ def boom(*_a, **_k): raise KeyboardInterrupt monkeypatch.setattr(floor_git, "changed_paths", boom) - monkeypatch.setattr(checks_floor, "changed_paths", boom) + monkeypatch.setattr(control_plane, "changed_paths", boom) with pytest.raises(KeyboardInterrupt): checks_floor.main(["--repo", str(repo), "--base", "main"]) diff --git a/tests/test_floor_review3_plane.py b/tests/test_floor_review3_plane.py index 964fd48c..ebe54179 100644 --- a/tests/test_floor_review3_plane.py +++ b/tests/test_floor_review3_plane.py @@ -33,6 +33,15 @@ def _refused(repo, capsys, *paths: str) -> str: return out +def _undecided(repo, capsys, link: str) -> str: + """S1: a control-plane link that opens a path off the control plane is undecided, naming the link and the fix.""" + code, out = run_floor(repo, capsys) + assert code == 2, out + assert f"symbolic link {link} at" in out, out + assert "floor.control_plane_paths.add" in out or "replace the link with real files" in out, out + return out + + @pytest.mark.parametrize("rel", [".mcp.json", ".cursor/mcp.json", ".vscode/mcp.json"]) def test_unwrapping_the_mcp_gateway_is_named(tmp_path, capsys, rel): repo = base_repo(tmp_path) @@ -50,7 +59,7 @@ def test_unwrapping_the_mcp_gateway_is_named(tmp_path, capsys, rel): (".vscode/settings.json", {"chat.hookFilesLocations": {"elsewhere": True}, "chat.useClaudeHooks": False}), (".gemini/.env", "PATH=/tmp/evil:/usr/bin\n"), (".env", "PYTHONPATH=/tmp/evil\n"), - (".env.local", "PYTHONPATH=/tmp/evil\n"), + ("pkg/.env", "PYTHONPATH=/tmp/evil\n"), (".claude/skills/x/SKILL.md", SKILL_HOOK), (".claude/agents/x.md", SKILL_HOOK), (".github/agents/x.agent.md", SKILL_HOOK), @@ -68,7 +77,7 @@ def test_symlinked_vendor_folder_adds_a_settings_file_named(tmp_path, capsys): write(repo, "cfg/settings.json", GEMINI_OFF) os.symlink("cfg", repo / ".gemini") commit(repo, "pr") - _refused(repo, capsys, ".gemini", "cfg/settings.json") + _undecided(repo, capsys, ".gemini") def test_a_base_symlinked_vendor_folder_makes_its_target_control_plane(tmp_path, capsys): @@ -80,6 +89,19 @@ def test_a_base_symlinked_vendor_folder_makes_its_target_control_plane(tmp_path, commit(repo, "base: linked settings") git(repo, "checkout", "-q", "-B", "pr") _pr(repo, {"cfg/settings.json": GEMINI_OFF}) + _undecided(repo, capsys, ".gemini") + + +def test_a_base_link_whose_target_the_base_adds_to_the_plane_lists_the_target(tmp_path, capsys): + """The fix S1 names: the base puts the link's target on its own list, so an edit there is listed.""" + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + write(repo, "cfg/settings.json", {"hooksConfig": {"enabled": True}}) + os.symlink("cfg", repo / ".gemini") + write(repo, ".chock/config.yaml", "floor:\n control_plane_paths:\n add: [cfg/]\n") + commit(repo, "base: linked settings, target listed") + git(repo, "checkout", "-q", "-B", "pr") + _pr(repo, {"cfg/settings.json": GEMINI_OFF}) out = _refused(repo, capsys, "cfg/settings.json") assert "\n .gemini\n" not in out, out @@ -93,7 +115,7 @@ def test_a_chain_of_symlinks_is_followed(tmp_path, capsys): commit(repo, "base: links") git(repo, "checkout", "-q", "-B", "pr") _pr(repo, {"b/hooks.json": {"hooks": {}}}) - _refused(repo, capsys, "b/hooks.json") + _undecided(repo, capsys, ".codex") def test_a_vendor_folder_linked_to_the_root_makes_the_root_control_plane(tmp_path, capsys): @@ -103,18 +125,18 @@ def test_a_vendor_folder_linked_to_the_root_makes_the_root_control_plane(tmp_pat commit(repo, "base: link to the root") git(repo, "checkout", "-q", "-B", "pr") _pr(repo, {"hooks.json": {"hooks": {}}}) - _refused(repo, capsys, "hooks.json") + _undecided(repo, capsys, ".windsurf") -def test_a_symlink_out_of_the_repository_adds_nothing(tmp_path, capsys): +def test_a_symlink_out_of_the_repository_is_undecided(tmp_path, capsys): repo = base_repo(tmp_path) git(repo, "checkout", "-q", "main") os.symlink("../elsewhere", repo / ".gemini") commit(repo, "base: link out") git(repo, "checkout", "-q", "-B", "pr") _pr(repo, {"src/app.py": "VALUE = 2\n"}) - code, out = run_floor(repo, capsys) - assert code == 0, out + out = _undecided(repo, capsys, ".gemini") + assert "leaves the repository" in out, out def test_gitlink_at_a_vendor_folder_is_named(tmp_path, capsys): diff --git a/tests/test_floor_review4_config.py b/tests/test_floor_review4_config.py new file mode 100644 index 00000000..92088083 --- /dev/null +++ b/tests/test_floor_review4_config.py @@ -0,0 +1,109 @@ +"""Review 4, L5: the base's floor config. Each case asserts the owed exit and checks the 'in force' line.""" + +from __future__ import annotations + +import pytest +from floor_support import base_repo, commit, run_floor, write +from floor_support4 import HOOKLESS, rebase_main + + +def _with_config(tmp_path, text): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: write(r, ".chock/config.yaml", text)) + return repo + + +CASES = [ + # (id, config text, change, owed exit, text the in-force line must hold or None) + ("remove-other-case", "floor:\n control_plane_paths:\n remove: ['.CLAUDE/**']\n", "claude", 2, None), + ("remove-trailing-slash", "floor:\n control_plane_paths:\n remove: ['.claude/**/']\n", "claude", 2, None), + ( + "remove-exact", + "floor:\n control_plane_paths:\n remove: ['**/.claude/**']\n", + "claude", + 0, + "base removed: **/.claude/**", + ), + ("add-everything", "floor:\n control_plane_paths:\n add: ['*']\n", "src", 1, "base added: *"), + ("add-empty-string", "floor:\n control_plane_paths:\n add: ['']\n", "src", 2, None), + ("add-non-string", "floor:\n control_plane_paths:\n add: [1]\n", "src", 2, None), + ("mode-capitalised", "floor:\n control_plane: Listed\n", "claude", 2, None), + ("mode-bool", "floor:\n control_plane: yes\n", "claude", 2, None), + ("mode-null", "floor:\n control_plane:\n", "claude", 2, None), + ("mode-list", "floor:\n control_plane: [listed]\n", "claude", 2, None), + ( + "dup-key-last-wins", + "floor:\n control_plane: refuse\n control_plane: listed\n", + "claude", + 0, + "control plane: listed", + ), + ( + "dup-section", + "floor:\n control_plane: listed\nfloor:\n control_plane: refuse\n", + "claude", + 1, + "control plane: refuse", + ), + ("anchor-merge", "x: &m\n control_plane: listed\nfloor:\n <<: *m\n", "claude", 0, "control plane: listed"), + ("not-yaml", "floor: [\n", "claude", 2, None), + ("floor-is-list", "floor: [1]\n", "claude", 2, None), + ("paths-is-list", "floor:\n control_plane_paths: ['x']\n", "claude", 2, None), + ("paths-unknown-key", "floor:\n control_plane_paths:\n drop: ['x']\n", "claude", 2, None), + ("bom", "floor:\n control_plane: listed\n", "claude", 0, "control plane: listed"), + ("tab-indent", "floor:\n\tcontrol_plane: listed\n", "claude", 2, None), + ("top-level-list", "- floor\n", "claude", 1, "control plane: refuse"), + ("upper-section", "Floor:\n control_plane: listed\n", "claude", 1, "control plane: refuse"), + ("deep-nesting", "floor:\n x: " + "[" * 20000 + "]" * 20000 + "\n", "claude", 2, None), +] + + +@pytest.mark.parametrize(("config", "change", "owed", "line"), [c[1:] for c in CASES], ids=[c[0] for c in CASES]) +def test_base_config(tmp_path, capsys, config, change, owed, line): + repo = _with_config(tmp_path, config) + if change == "claude": + write(repo, ".claude/settings.json", HOOKLESS) + else: + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == owed, out + if line: + assert line in out, out + + +@pytest.mark.parametrize( + ("pat", "owed", "said"), + [ + ("infra", 2, "write 'infra/**' for the folder"), # S5: a bare folder name asks for name/** + ("infra/", 1, "base added: infra/;"), # S5: a trailing slash is the folder and everything in it + ("/infra/**", 0, "base added: /infra/** (matches nothing at base or head)"), # S5: never shown as matching + ], +) +def test_add_folder_forms(tmp_path, capsys, pat, owed, said): + """m1: `add: [infra]`, `[infra/]` or `[/infra/**]`; each is either the folder, a question, or shown as matching nothing.""" + repo = _with_config(tmp_path, f"floor:\n control_plane_paths:\n add: ['{pat}']\n") + write(repo, "infra/deploy.sh", "rm -rf /\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == owed, out + assert said in out, out + + +def test_listed_mode_content_gap_is_stated(tmp_path, capsys): + """V3: under listed, a floor token inside a control-plane file passes, and the output says it was not judged.""" + repo = _with_config(tmp_path, "floor:\n control_plane: listed\n") + write(repo, ".claude/settings.local.json", '{"x": "FLOOR-TOKEN-1"}\n') + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "not judged" in out, out + + +def test_base_add_pattern_judged_by_content_gate_under_listed(tmp_path, capsys): + repo = _with_config(tmp_path, "floor:\n control_plane: listed\n control_plane_paths:\n add: ['src/**']\n") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-7'\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "not judged" in out and "src/app.py" in out, out diff --git a/tests/test_floor_review4_exit.py b/tests/test_floor_review4_exit.py new file mode 100644 index 00000000..7f20673f --- /dev/null +++ b/tests/test_floor_review4_exit.py @@ -0,0 +1,238 @@ +"""Review 4: abnormal paths (L3, L4, item 5). None may end as 0; SARIF and text must agree.""" + +from __future__ import annotations + +import json +import os +import shutil +import subprocess +import sys +import tempfile +import zlib +from pathlib import Path + +from floor_support import base_repo, commit, git, run_floor, write +from floor_support4 import rebase_main + +from chock.validation import checks_floor + +W = Path(__file__).resolve().parents[1] / "floor-review" + + +def _obj(repo: Path, oid: str) -> Path: + return repo / ".git/objects" / oid[:2] / oid[2:] + + +def _corrupt(repo: Path, oid: str, how: str) -> None: + path = _obj(repo, oid) + os.chmod(path, 0o644) + raw = zlib.decompress(path.read_bytes()) + if how == "garbage": + path.write_bytes(b"not zlib at all") + elif how == "truncated": + path.write_bytes(zlib.compress(raw)[:-6]) + elif how == "wrong-type": + body = raw.split(b"\0", 1)[1] + path.write_bytes(zlib.compress(b"tree %d\0" % len(body) + body)) + elif how == "wrong-size": + body = raw.split(b"\0", 1)[1] + path.write_bytes(zlib.compress(b"blob %d\0" % (len(body) + 50) + body)) + + +def _script_only(tmp_path: Path) -> Path: + repo = base_repo(tmp_path) + + def base(r): + for policy in ("floor-token", "test-integrity"): + shutil.rmtree(r / ".chock/compiled" / policy) + + rebase_main(repo, base) + return repo + + +def test_corrupt_head_blob_every_way(tmp_path, capsys): + for how in ("garbage", "truncated", "wrong-type"): # wrong-size: git returns the true bytes (rc 0); not a finding + for shape in ("content", "script"): + repo = (base_repo if shape == "content" else _script_only)(tmp_path / f"{how}-{shape}") + write(repo, "src/new.py", "print('x')\n") + commit(repo, "pr") + _corrupt(repo, git(repo, "rev-parse", "HEAD:src/new.py"), how) + code, out = run_floor(repo, capsys) + assert code == 2, (how, shape, out) + + +def test_corrupt_head_tree(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/new.py", "print('x')\n") + commit(repo, "pr") + _corrupt(repo, git(repo, "rev-parse", "HEAD:src"), "garbage") + code, out = run_floor(repo, capsys) + assert code == 2, out + + +def test_corrupt_base_config_blob(tmp_path, capsys): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: write(r, ".chock/config.yaml", "floor:\n control_plane: listed\n")) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + _corrupt(repo, git(repo, "rev-parse", "main:.chock/config.yaml"), "garbage") + code, out = run_floor(repo, capsys) + assert code == 2, out + + +def test_invalid_json_gate_at_base(tmp_path, capsys): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: write(r, ".chock/compiled/floor-token/ci-gate/gate.json", "{not json")) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 2, out + + +def test_git_missing_from_path(tmp_path): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + env = {"PATH": str(tmp_path / "empty"), "HOME": os.environ["HOME"], "PYTHONPATH": os.environ.get("PYTHONPATH", "")} + proc = subprocess.run( + [ + sys.executable, + "-c", + "import sys; from chock.validation.checks_floor import main; sys.exit(main(sys.argv[1:]))", + "--repo", + str(repo), + "--base", + "main", + ], + env=env, + capture_output=True, + text=True, + check=False, + ) + assert proc.returncode == 2, proc.stdout + proc.stderr + + +def test_temp_dir_full(tmp_path, capsys, monkeypatch): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + + def full(*_a, **_k): + raise OSError(28, "No space left on device") + + monkeypatch.setattr(tempfile, "TemporaryDirectory", full) + code, out = run_floor(repo, capsys) + assert code == 2, out + + +def test_unusable_tmpdir_env(tmp_path): + repo = base_repo(tmp_path) + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + commit(repo, "pr") + bad = tmp_path / "ro" + bad.mkdir() + bad.chmod(0o500) + env = {**os.environ, "TMPDIR": str(bad / "missing")} + proc = subprocess.run( + [ + sys.executable, + "-c", + "import sys; from chock.validation.checks_floor import main; sys.exit(main(sys.argv[1:]))", + "--repo", + str(repo), + "--base", + "main", + ], + env=env, + capture_output=True, + text=True, + check=False, + ) + assert proc.returncode in (1, 2), proc.stdout + proc.stderr + + +def test_sarif_unwritable_targets(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + (tmp_path / "adir").mkdir() + for target in (tmp_path / "missing/x.sarif", tmp_path / "adir"): + code, out = run_floor(repo, capsys, "--sarif", str(target)) + assert code == 2, (target, out) + + +def test_sarif_agrees_with_exit_on_every_outcome(tmp_path, capsys): + """refuse+listed -> error results; listed -> note results and exit 0; undecided -> executionSuccessful false.""" + repo = base_repo(tmp_path) + write(repo, ".claude/settings.json", "{}\n") + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + commit(repo, "pr") + sarif = tmp_path / "o.sarif" + code, _ = run_floor(repo, capsys, "--sarif", str(sarif)) + doc = json.loads(sarif.read_text()) + levels = {r["level"] for r in doc["runs"][0]["results"]} + assert code == 1 and levels == {"error"}, (code, levels) + + +def test_an_undecided_run_replaces_a_stale_sarif(tmp_path, capsys): + """S6: the output path holds a clean SARIF (as a head checkout could); an undecided run overwrites it.""" + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + sarif = tmp_path / "o.sarif" + sarif.write_text(json.dumps({"version": "2.1.0", "runs": [{"results": []}]})) + code = checks_floor.main(["--repo", str(repo), "--base", "no-such-ref", "--sarif", str(sarif)]) + capsys.readouterr() + assert code == 2 + doc = json.loads(sarif.read_text()) + invocation = doc["runs"][0]["invocations"][0] + assert invocation["executionSuccessful"] is False, doc + assert "does not resolve" in invocation["toolExecutionNotifications"][0]["message"]["text"], doc + + +def test_repo_forms(tmp_path, capsys): + repo = base_repo(tmp_path / "with space") + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + commit(repo, "pr") + link = tmp_path / "link" + os.symlink(repo, link) + wt = tmp_path / "wt dir" + git(repo, "worktree", "add", "-q", "--detach", str(wt), "pr") + (repo / "src/deep/er").mkdir(parents=True) + for form in (repo, link, link / "src", wt, wt / "tests", repo / "src/deep/er"): + code = checks_floor.main(["--repo", str(form), "--base", "main", "--head", "pr"]) + out = capsys.readouterr() + assert code == 1, (form, out.out + out.err) + bare = tmp_path / "bare.git" + git(repo, "clone", "-q", "--bare", str(repo), str(bare)) + code = checks_floor.main(["--repo", str(bare), "--base", "main", "--head", "pr"]) + out = capsys.readouterr() + assert code in (1, 2), out.out + out.err + + +def test_ref_forms(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "tag", "-a", "-m", "t", "v1", "main") + git(repo, "tag", "light", "main") + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + commit(repo, "pr") + short = git(repo, "rev-parse", "--short=7", "main") + git(repo, "branch", "we/ird-n@me", "main") + for base in ("v1", "light", short, "HEAD~1", "pr~1", "we/ird-n@me", "refs/heads/main", "main^{commit}"): + code = checks_floor.main(["--repo", str(repo), "--base", base]) + out = capsys.readouterr() + assert code == 1, (base, out.out + out.err) + for base in ("--all", "-h", "main..pr", "main...pr", "", "@{-1}", ":/base"): + try: + code = checks_floor.main(["--repo", str(repo), f"--base={base}"]) + except SystemExit as exc: + code = exc.code + out = capsys.readouterr() + assert code in (1, 2), (base, code, out.out + out.err) + + +def test_head_equal_to_base_is_clean(tmp_path, capsys): + repo = base_repo(tmp_path) + code = checks_floor.main(["--repo", str(repo), "--base", "main", "--head", "main"]) + out = capsys.readouterr() + assert code == 0, out.out diff --git a/tests/test_floor_review4_interact.py b/tests/test_floor_review4_interact.py new file mode 100644 index 00000000..1a78bd61 --- /dev/null +++ b/tests/test_floor_review4_interact.py @@ -0,0 +1,120 @@ +"""Review 4, item 4: interactions. Each asserts the exit the spec owes (or, where marked, the exit observed by design).""" + +from __future__ import annotations + +import contextlib +import os +import shutil +import time + +from floor_support import base_repo, commit, git, run_floor, write +from floor_support4 import raw_commit + + +def test_head_deletes_whole_chock_tree(tmp_path, capsys): + repo = base_repo(tmp_path) + shutil.rmtree(repo / ".chock") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_mode_only_changes(tmp_path, capsys): + for i, (path, owed) in enumerate( + (("src/app.py", 0), (".claude/settings.json", 1), (".agents/policies/probe/implementations/probe.py", 1)) + ): + repo = base_repo(tmp_path / str(i)) + (repo / path).chmod(0o755) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == owed, (path, out) + + +def test_merge_resolution_introduces_a_token_neither_parent_has(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 'left'\n") + commit(repo, "left") + git(repo, "checkout", "-q", "-b", "right", "main") + write(repo, "src/app.py", "VALUE = 'right'\n") + commit(repo, "right") + git(repo, "checkout", "-q", "pr") + with contextlib.suppress(Exception): # a conflict is expected + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "merge", "-q", "--no-edit", "right") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-99'\n") + git(repo, "add", "-A") + raw_commit(repo, "merge with an evil resolution") + assert len(git(repo, "rev-list", "--parents", "-n1", "HEAD").split()) == 3 + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_clean_merge_resolution_with_token_in_resolution_only_history(tmp_path, capsys): + """Token added by the merge commit and removed in the next commit: only the history pass can see it.""" + repo = base_repo(tmp_path) + write(repo, "src/a.py", "A = 1\n") + commit(repo, "left") + git(repo, "checkout", "-q", "-b", "right", "main") + write(repo, "src/b.py", "B = 1\n") + commit(repo, "right") + git(repo, "checkout", "-q", "pr") + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "merge", "-q", "--no-commit", "right") + write(repo, "src/c.py", "C = 'FLOOR-TOKEN-5'\n") + git(repo, "add", "-A") + raw_commit(repo, "merge") + os.remove(repo / "src/c.py") + commit(repo, "remove it") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_control_plane_change_and_revert_secret_in_history(tmp_path, capsys): + """Commit 1 puts a token in .claude/notes.json, commit 2 removes it: the tree diff is empty. S2: still named.""" + repo = base_repo(tmp_path) + write(repo, ".claude/notes.json", '{"k": "FLOOR-TOKEN-42"}\n') + commit(repo, "add") + os.remove(repo / ".claude/notes.json") + commit(repo, "remove") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n .claude/notes.json (in a commit of the range only" in out, out + + +def test_same_outside_the_control_plane_for_contrast(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/notes.json", '{"k": "FLOOR-TOKEN-42"}\n') + commit(repo, "add") + os.remove(repo / "src/notes.json") + commit(repo, "remove") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_first_adoption_with_violation(tmp_path, capsys): + """V6: base without .chock exits 0 even when the head adds .chock and a token.""" + repo = tmp_path / "repo" + repo.mkdir() + git(repo, "init", "-q", "-b", "main") + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "base") + git(repo, "checkout", "-q", "-b", "pr") + write(repo, ".chock/config.yaml", "rollout: enforce\n") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-1'\n") + write(repo, ".claude/settings.json", "{}\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0 and "nothing was judged" in out, out + + +def test_many_files(tmp_path, capsys): + repo = base_repo(tmp_path) + for i in range(3000): + write(repo, f"gen/f{i:05d}.py", f"V{i} = {i}\n") + commit(repo, "pr") + start = time.monotonic() + code, out = run_floor(repo, capsys) + print(f"3000 files: exit {code} in {time.monotonic() - start:.1f}s") + assert code == 0, out[-800:] + write(repo, "gen/f01234.py", "V = 'FLOOR-TOKEN-3'\n") + commit(repo, "pr2") + code, out = run_floor(repo, capsys) + assert code == 1, out[-800:] diff --git a/tests/test_floor_review4_links.py b/tests/test_floor_review4_links.py new file mode 100644 index 00000000..b4e78eaf --- /dev/null +++ b/tests/test_floor_review4_links.py @@ -0,0 +1,202 @@ +"""Review 4, L2 matching: symlink chains, gitlink targets, base/head asymmetry, spellings. Each asserts the owed exit. + +S1: a link at, inside or above the control plane must open a control-plane path (or nothing) at base and head, +else the run is undecided (2) naming the link. The suspected Windows short-name case (`CLAUDE~1/`) is not ported: +docs list it as not covered until it is tested on Windows.""" + +from __future__ import annotations + +from floor_support import CLAUDE, TEST_FILE, base_repo, commit, git, run_floor, write +from floor_support4 import FAKE_A, FAKE_B, HOOKLESS, gitlink, raw_commit, rebase_main, rm, symlink, undecided + + +def _chain_base(repo): + rm(repo, ".claude") + symlink(repo, ".claude", "cfg/claude") + symlink(repo, "cfg", "real") + write(repo, "real/claude/settings.json", CLAUDE) + + +def test_two_hop_symlink_chain_target_is_named(tmp_path, capsys): + """L2: `.claude -> cfg/claude`, `cfg -> real`. The client reads real/claude/settings.json as .claude/settings.json.""" + repo = base_repo(tmp_path) + rebase_main(repo, _chain_base) + write(repo, "real/claude/settings.json", HOOKLESS) + commit(repo, "pr: drop every hook through the chain") + undecided(repo, capsys, ".claude") + + +def test_one_hop_symlink_target_is_named_for_contrast(tmp_path, capsys): + repo = base_repo(tmp_path) + + def base(r): + rm(r, ".claude") + symlink(r, ".claude", "real/claude") + write(r, "real/claude/settings.json", CLAUDE) + + rebase_main(repo, base) + write(repo, "real/claude/settings.json", HOOKLESS) + commit(repo, "pr") + undecided(repo, capsys, ".claude") + + +def test_hooks_folder_symlinked_through_a_symlinked_parent(tmp_path, capsys): + """`.claude/hooks -> ../tools/hooks` and `tools -> vendor/tools`: editing the hook script the client runs.""" + repo = base_repo(tmp_path) + + def base(r): + symlink(r, ".claude/hooks", "../tools/hooks") + symlink(r, "tools", "vendor/tools") + write(r, "vendor/tools/hooks/guard.sh", "#!/bin/sh\nexit 2\n") + + rebase_main(repo, base) + write(repo, "vendor/tools/hooks/guard.sh", "#!/bin/sh\nexit 0\n") + commit(repo, "pr: guard now allows everything") + undecided(repo, capsys, ".claude/hooks") + + +def _submodule_base(repo): + rm(repo, ".claude") + symlink(repo, ".claude", "vendor/agentcfg/.claude") + git(repo, "add", "-A") + gitlink(repo, "vendor/agentcfg", FAKE_A) + + +def test_symlinked_agent_folder_into_a_submodule_bump_is_named(tmp_path, capsys): + """L2: the control-plane folder's target lies inside a gitlink; the PR moves the gitlink.""" + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + _submodule_base(repo) + raw_commit(repo, "base: shared agent config from a submodule") + git(repo, "checkout", "-q", "-B", "pr") + gitlink(repo, "vendor/agentcfg", FAKE_B) + raw_commit(repo, "pr: bump the agent-config submodule") + out = undecided(repo, capsys, ".claude") + assert "passes through the submodule vendor/agentcfg" in out, out + + +def test_symlinked_chock_folder_at_base_is_not_first_adoption(tmp_path, capsys): + """V6 says 'B has no .chock'. Here B has .chock, as a symlink to the real folder.""" + repo = base_repo(tmp_path) + + def base(r): + (r / ".chock").rename(r / "chockcfg") + symlink(r, ".chock", "chockcfg") + + rebase_main(repo, base) + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + write(repo, ".claude/settings.json", HOOKLESS) + commit(repo, "pr: hooks off and assertions gone") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "base has no chock gates" not in out, out + assert "the base's .chock is a symbolic link" in out, out + + +def test_symlink_replaced_by_directory_is_named(tmp_path, capsys): + repo = base_repo(tmp_path) + + def base(r): + rm(r, ".claude") + symlink(r, ".claude", "real/claude") + write(r, "real/claude/settings.json", CLAUDE) + + rebase_main(repo, base) + rm(repo, ".claude") + write(repo, ".claude/settings.json", HOOKLESS) + commit(repo, "pr") + undecided(repo, capsys, ".claude") + + +def test_directory_replaced_by_symlink_is_named(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "elsewhere/settings.json", HOOKLESS) + rm(repo, ".claude") + symlink(repo, ".claude", "elsewhere") + commit(repo, "pr") + undecided(repo, capsys, ".claude") + + +def test_rename_away_and_replace_in_one_pr_is_named(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "mv", ".claude", "old-claude") + write(repo, ".claude/settings.json", HOOKLESS) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_retargeted_link_is_named(tmp_path, capsys): + repo = base_repo(tmp_path) + + def base(r): + rm(r, ".claude") + symlink(r, ".claude", "a") + write(r, "a/settings.json", CLAUDE) + write(r, "b/settings.json", HOOKLESS) + + rebase_main(repo, base) + symlink(repo, ".claude", "b") + commit(repo, "pr") + undecided(repo, capsys, ".claude") + + +def test_link_only_at_head_into_existing_folder(tmp_path, capsys): + repo = base_repo(tmp_path) + + def base(r): + write(r, "b/settings.json", HOOKLESS) + + rebase_main(repo, base) + symlink(repo, ".gemini", "b") + commit(repo, "pr") + undecided(repo, capsys, ".gemini") + + +def test_case_and_trailing_dot_space_spellings_are_named(tmp_path, capsys): + for i, name in enumerate( + ( + ".Claude/settings.json", + ".claude./settings.local.json", + ".claude /x.json", + ".CHOCK/compiled/x/ci-gate/gate.json", + ".Vscode/Settings.JSON.", + ) + ): + repo = base_repo(tmp_path / str(i)) + write(repo, name, "{}\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, (name, out) + + +def test_zero_width_and_nfd_spellings_are_named(tmp_path, capsys): + for i, name in enumerate((".cl​aude/settings.json", ".ćlaude-x/y", ".vscode/se‍ttings.json")): + repo = base_repo(tmp_path / str(i)) + write(repo, name, "{}\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + # .ćlaude-x is a different name on every filesystem: owed 0; the others fold to listed paths. + assert code == (0 if "laude-x" in name else 1), (name, out) + + +def test_gitlink_at_github_workflows_is_named(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "rm", "-q", "-r", "--cached", ".github/workflows") + rm(repo, ".github/workflows") + gitlink(repo, ".github/workflows", FAKE_A) + raw_commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + + +def test_test_file_replaced_by_gitlink_is_refused(tmp_path, capsys): + """L3-ish: a gitlink where a test was. The head 'blob' is a commit git cannot show as text.""" + repo = base_repo(tmp_path) + git(repo, "rm", "-q", "--cached", "tests/test_core.py") + rm(repo, "tests/test_core.py") + gitlink(repo, "tests/test_core.py", FAKE_A) + raw_commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code != 0, out + assert TEST_FILE diff --git a/tests/test_floor_review4_list.py b/tests/test_floor_review4_list.py new file mode 100644 index 00000000..75bef275 --- /dev/null +++ b/tests/test_floor_review4_list.py @@ -0,0 +1,34 @@ +"""Review 4, item 6: files vendor docs say can disable or pre-empt guardrails; S4 puts each on the default list. +`*.code-workspace` is not listed (not verified that an editor honours hook settings from it; docs, not covered).""" + +from __future__ import annotations + +import pytest +from floor_support import base_repo, commit, run_floor, write + +GAPS = { + "nested-claude-skill-hooks": ( + "pkg/.claude/skills/x/SKILL.md", + "---\nname: x\nallowed-tools: Bash(*)\nhooks: {}\n---\nx\n", + ), + "nested-claude-settings": ("pkg/.claude/settings.json", '{"permissions": {"allow": ["Bash(*)"]}}\n'), + "nested-codex-config": ( + "pkg/.codex/config.toml", + 'approval_policy = "never"\nsandbox_mode = "danger-full-access"\n', + ), + "nested-dotenv": ("pkg/.env", "PATH=./bin:/usr/bin\n"), + "devcontainer": ( + ".devcontainer/devcontainer.json", + '{"postCreateCommand": "git config chock.python ./bin/fakepy", "remoteEnv": {"PATH": "./bin:${containerEnv:PATH}"}}\n', + ), +} + + +@pytest.mark.parametrize("name", list(GAPS)) +def test_listed(tmp_path, capsys, name): + path, text = GAPS[name] + repo = base_repo(tmp_path) + write(repo, path, text) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out diff --git a/tests/test_floor_review4_ordinary.py b/tests/test_floor_review4_ordinary.py new file mode 100644 index 00000000..3d310ba4 --- /dev/null +++ b/tests/test_floor_review4_ordinary.py @@ -0,0 +1,86 @@ +"""Review 4, item 7: ordinary pull requests touching no control-plane path. Owed exit 0 (V7).""" + +from __future__ import annotations + +import random +import time + +import pytest +from floor_support import TEST_FILE, base_repo, commit, run_floor, write + + +def _bytes(repo, rel, data): + p = repo / rel + p.parent.mkdir(parents=True, exist_ok=True) + p.write_bytes(data) + + +def _rand(n, seed=7): + return random.Random(seed).randbytes(n) # noqa: S311 -- incompressible filler, not a secret + + +CASES = { + "source-edit": lambda r: write(r, "src/app.py", "VALUE = 2\n"), + "new-directory": lambda r: write(r, "pkg/sub/mod.py", "X = 1\n"), + "docs": lambda r: write(r, "docs/guide.md", "# Guide\n"), + "readme": lambda r: write(r, "README.md", "# Project\n"), + "package-lock": lambda r: write(r, "package-lock.json", '{"lockfileVersion": 3, "packages": {"": {}}}\n'), + "uv-lock": lambda r: write(r, "uv.lock", 'version = 1\n[[package]]\nname = "x"\nversion = "1.0"\n'), + "test-added": lambda r: write(r, "tests/test_new.py", "def test_b():\n assert True\n"), + "test-edited-values": lambda r: write(r, "tests/test_core.py", TEST_FILE.replace("== 4", "== 4.0")), + "test-assert-moved": lambda r: write( + r, "tests/test_core.py", "def test_a():\n assert 2 * 2 == 4\n x = 1\n assert 1 + 1 == 2\n" + ), + "crlf-file": lambda r: _bytes(r, "src/win.py", b"A = 1\r\nB = 2\r\n"), + "crlf-normalised-test": lambda r: _bytes(r, "tests/test_core.py", TEST_FILE.replace("\n", "\r\n").encode()), + "utf8-bom": lambda r: _bytes(r, "src/bom.py", b"\xef\xbb\xbfA = 1\n"), + "utf16-bom-text": lambda r: _bytes(r, "src/u16.txt", "hello\nworld\n".encode("utf-16")), + "png": lambda r: _bytes(r, "assets/logo.png", b"\x89PNG\r\n\x1a\n" + _rand(200_000)), + "env-example": lambda r: write(r, ".env.example", "DATABASE_URL=\n"), + "nested-gitattributes": lambda r: write(r, "assets/.gitattributes", "*.png binary\n"), + "docs-codeowners-word": lambda r: write(r, "docs/codeowners.md", "Who owns what\n"), + "agents-md": lambda r: write(r, "AGENTS.md", "Be nice.\n"), + "exec-bit-script": lambda r: (write(r, "scripts/run.sh", "#!/bin/sh\n"), (r / "scripts/run.sh").chmod(0o755)), +} + + +@pytest.mark.parametrize("name", list(CASES)) +def test_ordinary(tmp_path, capsys, name): + repo = base_repo(tmp_path) + CASES[name](repo) + commit(repo, f"pr: {name}") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +@pytest.mark.parametrize( + ("name", "size", "binary"), + [ + ("text-20MiB", 20 * 2**20, False), + ("binary-8MiB", 8 * 2**20, True), + ("binary-40MiB", 40 * 2**20, True), + ], +) +def test_large_file_under_the_limit(tmp_path, capsys, name, size, binary): + repo = base_repo(tmp_path) + if binary: + _bytes(repo, f"assets/{name}.bin", _rand(size)) + else: + line = b"generated_value_0123456789 = 'abcdefghijklmnopqrstuvwxyz'\n" + _bytes(repo, f"src/{name}.py", line * (size // len(line))) + commit(repo, "pr") + start = time.monotonic() + code, out = run_floor(repo, capsys) + took = time.monotonic() - start + print(f"{name}: exit {code} in {took:.1f}s") + assert code == 0, (took, out[-1500:]) + + +def test_binary_60mib_under_the_64mib_limit(tmp_path, capsys): + repo = base_repo(tmp_path) + _bytes(repo, "assets/model.bin", _rand(60 * 2**20)) + commit(repo, "pr") + start = time.monotonic() + code, out = run_floor(repo, capsys) + print(f"binary-60MiB: exit {code} in {time.monotonic() - start:.1f}s") + assert code == 0, out[-1500:] diff --git a/tests/test_floor_review4_rulings.py b/tests/test_floor_review4_rulings.py new file mode 100644 index 00000000..8b71b03b --- /dev/null +++ b/tests/test_floor_review4_rulings.py @@ -0,0 +1,201 @@ +"""Rulings S1-S6 of the floor's fourth pass (W2-A1v2.2), beyond the ported proofs. Each asserts the owed exit.""" + +from __future__ import annotations + +import json +import os +import subprocess + +import pytest +from floor_support import CLAUDE, TEST_FILE, base_repo, commit, git, run_floor, write +from floor_support4 import FAKE_A, HOOKLESS, gitlink, raw_commit, rebase_main, rm, symlink, undecided + +from chock.validation import checks_floor, floor_git + +LISTED = "floor/control-plane-changed (refuse)" + + +def _base_link(repo, path, target, *files): + def change(r): + if path == ".claude": + rm(r, ".claude") + for rel in files: + write(r, rel, CLAUDE) + symlink(r, path, target) + + rebase_main(repo, change) + + +@pytest.mark.parametrize( + ("link", "target", "why"), + [ + (".claude", ".claude", "more than 32 links"), + (".claude", "/etc/claude", "absolute or empty target"), + (".claude", "../../outside", "leaves the repository"), + (".claude/hooks", "../x/../../up", "leaves the repository"), + ], +) +def test_a_control_plane_link_that_cannot_resolve_is_undecided(tmp_path, capsys, link, target, why): + """S1: a loop, an absolute target or a target outside the repository is a failure to resolve.""" + repo = base_repo(tmp_path) + _base_link(repo, link, target) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr: an ordinary change") + out = undecided(repo, capsys, link) + assert why in out, out + + +def test_a_link_to_another_control_plane_folder_carries_on(tmp_path, capsys): + """S1: `.claude -> .codex` stays on the control plane; an ordinary change passes, an edit there is listed.""" + repo = base_repo(tmp_path) + _base_link(repo, ".claude", ".codex", ".codex/settings.json") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + write(repo, ".codex/settings.json", HOOKLESS) + commit(repo, "pr: through the link") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n .codex/settings.json\n" in out, out + + +def test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it(tmp_path, capsys): + """S1: a link to nothing is fine; once the head creates its target off the plane, it is undecided.""" + repo = base_repo(tmp_path) + _base_link(repo, ".gemini", "cfg") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + assert run_floor(repo, capsys)[0] == 0 + write(repo, "cfg/settings.json", HOOKLESS) + commit(repo, "pr: fill the target") + undecided(repo, capsys, ".gemini") + + +def test_a_non_control_plane_hop_is_listed_when_retargeted(tmp_path, capsys): + """`.claude -> cfg`, `cfg -> .codex`: moving `cfg` to `.cursor` changes what `.claude` opens, so `cfg` is listed.""" + repo = base_repo(tmp_path) + + def change(r): + rm(r, ".claude") + write(r, ".codex/settings.json", CLAUDE) + symlink(r, "cfg", ".codex") + symlink(r, ".claude", "cfg") + + rebase_main(repo, change) + symlink(repo, "cfg", ".cursor") + commit(repo, "pr: retarget the middle link") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n cfg\n" in out, out + + +def test_a_link_above_a_nested_agent_folder_is_undecided(tmp_path, capsys): + """S1, ancestor: `pkg -> vendor` with `vendor/.claude/`: the client opens pkg/.claude through `pkg`.""" + repo = base_repo(tmp_path) + _base_link(repo, "pkg", "vendor", "vendor/.claude/settings.json") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + undecided(repo, capsys, "pkg") + + +def test_an_unrelated_link_out_of_the_repository_is_not_judged(tmp_path, capsys): + """S1 asks only about links at, inside or above the control plane; others owe no refusal.""" + repo = base_repo(tmp_path) + _base_link(repo, "docs/shared", "/usr/share/doc") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_a_gitlink_inside_a_control_plane_folder_is_listed(tmp_path, capsys): + """S1: a submodule at or inside a control-plane folder is named like any other change to it.""" + repo = base_repo(tmp_path) + gitlink(repo, ".claude/shared", FAKE_A) + raw_commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n .claude/shared\n" in out, out + + +def test_a_guardrail_changed_and_changed_back_is_named(tmp_path, capsys): + """S2: hooks off in one commit, back on in the next; the tree diff is empty, the path is still named.""" + repo = base_repo(tmp_path) + write(repo, ".claude/settings.json", HOOKLESS) + commit(repo, "off") + write(repo, ".claude/settings.json", CLAUDE) + commit(repo, "on again") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n .claude/settings.json (in a commit of the range only" in out, out + + +def test_a_pull_request_merge_of_a_behind_branch_names_nothing_from_the_base(tmp_path, capsys): + """S2 reads states the base does not have: main's own workflow change, merged into the PR ref, is not the PR's.""" + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr: ordinary") + git(repo, "checkout", "-q", "main") + write(repo, ".github/workflows/chock.yml", "name: changed on main\n") + commit(repo, "main: workflow") + git(repo, "checkout", "-q", "-b", "merge-ref") + git(repo, "-c", "user.name=t", "-c", "user.email=t@e", "merge", "-q", "--no-edit", "pr") + code, out = run_floor(repo, capsys, "--head", "merge-ref") + assert code == 0, out + + +def test_attributes_come_from_the_base(tmp_path, capsys): + """S3: a head `.gitattributes` marking tests binary neither hides a removed assertion nor is itself listed.""" + repo = base_repo(tmp_path) + write(repo, ".gitattributes", "tests/** binary -diff -text\n") + write(repo, "tests/test_core.py", TEST_FILE.replace(" assert", " pass #")) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n .gitattributes\n" not in out, out + assert "GIT_ATTR_SOURCE" not in os.environ + + +def test_a_git_that_ignores_attr_source_is_undecided(tmp_path, capsys, monkeypatch): + """S3: a git too old to read attributes from the base is exit 2, naming the version needed.""" + repo = base_repo(tmp_path) + real = floor_git.git + + def old_git(repo_, *args, data=None): + if "check-attr" in args: + return subprocess.CompletedProcess(args, 0, b"", b"") + return real(repo_, *args, data=data) + + monkeypatch.setattr(floor_git, "git", old_git) + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "git 2.42 or newer" in out, out + + +def test_first_adoption_writes_a_clean_sarif(tmp_path, capsys): + """S6: every run writes the file it was asked for, so a stale one never stands in for this run.""" + repo = tmp_path / "repo" + repo.mkdir() + git(repo, "init", "-q", "-b", "main") + write(repo, "src/app.py", "VALUE = 1\n") + commit(repo, "base") + sarif = tmp_path / "o.sarif" + sarif.write_text('{"stale": true}\n') + code = checks_floor.main(["--repo", str(repo), "--base", "main", "--sarif", str(sarif)]) + assert code == 0, capsys.readouterr() + doc = json.loads(sarif.read_text()) + assert doc["runs"][0]["invocations"][0]["executionSuccessful"] is True, doc + assert doc["runs"][0]["results"] == [], doc + + +def test_an_undecided_link_writes_an_undecided_sarif(tmp_path, capsys): + """S6: text and SARIF agree on an undecided run.""" + repo = base_repo(tmp_path) + _base_link(repo, ".claude", "cfg/claude", "cfg/claude/settings.json") + sarif = tmp_path / "o.sarif" + code = checks_floor.main(["--repo", str(repo), "--base", "main", "--sarif", str(sarif)]) + assert code == 2, capsys.readouterr() + invocation = json.loads(sarif.read_text())["runs"][0]["invocations"][0] + assert invocation["executionSuccessful"] is False + assert "symbolic link .claude" in invocation["toolExecutionNotifications"][0]["message"]["text"] From 15c29079a54113f6c61fc3b063ba51e654c213f6 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:34:19 +0000 Subject: [PATCH 27/57] test(floor): drop unused imports and a world-readable chmod in the pass-4 proofs Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- tests/floor_support4.py | 2 +- tests/test_floor_review4_exit.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/floor_support4.py b/tests/floor_support4.py index 67920fae..e4245208 100644 --- a/tests/floor_support4.py +++ b/tests/floor_support4.py @@ -6,7 +6,7 @@ import shutil from pathlib import Path -from floor_support import base_repo, commit, git, run_floor, write # noqa: F401 +from floor_support import commit, git, run_floor FAKE_A = "1" * 40 FAKE_B = "2" * 40 diff --git a/tests/test_floor_review4_exit.py b/tests/test_floor_review4_exit.py index 7f20673f..43ad24d8 100644 --- a/tests/test_floor_review4_exit.py +++ b/tests/test_floor_review4_exit.py @@ -25,7 +25,7 @@ def _obj(repo: Path, oid: str) -> Path: def _corrupt(repo: Path, oid: str, how: str) -> None: path = _obj(repo, oid) - os.chmod(path, 0o644) + os.chmod(path, 0o600) # writable by the owner, so the object can be corrupted raw = zlib.decompress(path.read_bytes()) if how == "garbage": path.write_bytes(b"not zlib at all") From d23260322e4c40eff3474c96611b99e7bba3bfec Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:51:03 +0000 Subject: [PATCH 28/57] Floor gate G1, GT-1: regression tests for links, history and attributes Scenarios A1-A11 (links), B1-B8 (history) and C1-C3 (attributes) from the release-1.2.0 plan, each asserting the exit the plan owes. A7's second half (`pkg -> real` with `real/.claude/`, owed 0) fails: the code gives 2, as an existing ruling-S1 test asserts for the same shape; reported to the orchestrator, not changed. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- tests/test_floor_gate_attrs.py | 62 ++++++++++++ tests/test_floor_gate_history.py | 135 ++++++++++++++++++++++++++ tests/test_floor_gate_links.py | 161 +++++++++++++++++++++++++++++++ tests/test_floor_gate_links2.py | 110 +++++++++++++++++++++ 4 files changed, 468 insertions(+) create mode 100644 tests/test_floor_gate_attrs.py create mode 100644 tests/test_floor_gate_history.py create mode 100644 tests/test_floor_gate_links.py create mode 100644 tests/test_floor_gate_links2.py diff --git a/tests/test_floor_gate_attrs.py b/tests/test_floor_gate_attrs.py new file mode 100644 index 00000000..a5689327 --- /dev/null +++ b/tests/test_floor_gate_attrs.py @@ -0,0 +1,62 @@ +"""Release gate G1, GT-1 attributes (S3), scenarios C1-C3: each test asserts the exit the plan owes. + +Every git call the floor makes reads attributes from the base, so a `.gitattributes` the head adds changes nothing +the floor sees, and is not itself listed.""" + +from __future__ import annotations + +import os +import shutil + +import pytest +from floor_support import base_repo, commit, git, run_floor, write +from floor_support4 import raw_commit, rebase_main + +VIOLATION = "VALUE = 'FLOOR-TOKEN-7'\n" +C1_ATTRIBUTES = ("-diff", "binary", "filter=x", "diff=x", "eol=crlf", "working-tree-encoding=UTF-16", "export-ignore") +C1_PLACES = {"root": (".gitattributes", "src/bad.py"), "nested": ("src/.gitattributes", "bad.py")} + + +@pytest.mark.parametrize("place", list(C1_PLACES)) +@pytest.mark.parametrize("attribute", C1_ATTRIBUTES) +def test_c1_head_gitattributes_cannot_hide_a_content_violation(tmp_path, capsys, attribute, place): + attrs, pattern = C1_PLACES[place] + repo = base_repo(tmp_path) + write(repo, "src/bad.py", VIOLATION) + commit(repo, "pr: a violation") # staged before the attributes exist, so no filter rewrites the blob + write(repo, attrs, f"{pattern} {attribute}\n") + git(repo, "add", attrs) # by path: `add -A` would re-read bad.py through the new attribute + raw_commit(repo, f"pr: mark it {attribute}") + code, out = run_floor(repo, capsys) + assert code == 1, (attribute, place, out) + assert "floor-token" in out, out + assert f"\n {attrs}\n" not in out, out + + +@pytest.mark.parametrize("attribute", ["-diff", "binary"]) +def test_c2_base_gitattributes_marking_a_path_never_hides_a_violation_there(tmp_path, capsys, attribute): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: write(r, ".gitattributes", f"src/** {attribute}\n")) + write(repo, "src/bad.py", VIOLATION) + commit(repo, "pr: a violation under a base attribute") + code, out = run_floor(repo, capsys) + assert code == 1, (attribute, out) + assert "floor-token" in out, out + + +def test_c3_git_without_the_attribute_source_option_is_2_naming_the_version(tmp_path, capsys, monkeypatch): + """A `git` wrapper earlier on PATH that drops GIT_ATTR_SOURCE, as a git older than 2.42 ignores it.""" + repo = base_repo(tmp_path) + real = shutil.which("git") + bin_dir = tmp_path / "old-git" + bin_dir.mkdir() + wrapper = bin_dir / "git" + wrapper.write_text(f'#!/bin/sh\nunset GIT_ATTR_SOURCE\nexec "{real}" "$@"\n', encoding="utf-8") + wrapper.chmod(0o755) + monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ['PATH']}") + assert shutil.which("git") == str(wrapper) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr: ordinary") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "git 2.42 or newer" in out, out diff --git a/tests/test_floor_gate_history.py b/tests/test_floor_gate_history.py new file mode 100644 index 00000000..c6fca847 --- /dev/null +++ b/tests/test_floor_gate_history.py @@ -0,0 +1,135 @@ +"""Release gate G1, GT-1 history (S2), scenarios B1-B8: each test asserts the exit the plan owes. + +A commit is named when it changes a plane path to a state that differs from the base's, even if the tree diff +base..head is empty for it.""" + +from __future__ import annotations + +from floor_support import CLAUDE, base_repo, commit, git, run_floor, write +from floor_support4 import HOOKLESS, rebase_main + +SETTINGS = ".claude/settings.json" +HISTORY_ONLY = " (in a commit of the range only" +USER = ("-c", "user.name=t", "-c", "user.email=t@example.com") + + +def _named(repo, capsys, path, *extra): + code, out = run_floor(repo, capsys, *extra) + assert code == 1, out + assert f"\n {path}{HISTORY_ONLY}" in out, out + return out + + +def _merge(repo, *branches): + git(repo, *USER, "merge", "-q", "--no-ff", "--no-edit", *branches) + + +def _off_and_on(repo): + write(repo, SETTINGS, HOOKLESS) + commit(repo, "hooks off") + write(repo, SETTINGS, CLAUDE) + commit(repo, "hooks back on") + + +def test_b1_plane_file_changed_and_restored_is_1_named(tmp_path, capsys): + repo = base_repo(tmp_path) + _off_and_on(repo) + _named(repo, capsys, SETTINGS) + + +def test_b2_change_and_restore_on_a_merged_side_branch_is_1(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr: ordinary") + git(repo, "checkout", "-q", "-b", "side") + _off_and_on(repo) + git(repo, "checkout", "-q", "pr") + _merge(repo, "side") + _named(repo, capsys, SETTINGS) + + +def test_b3_plane_file_created_and_deleted_is_1(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, ".claude/agents/helper.md", "# helper\n") + commit(repo, "add an agent") + git(repo, "rm", "-q", ".claude/agents/helper.md") + commit(repo, "remove it") + _named(repo, capsys, ".claude/agents/helper.md") + + +def test_b4_mode_only_change_and_its_revert_is_1(tmp_path, capsys): + repo = base_repo(tmp_path) + (repo / SETTINGS).chmod(0o755) + commit(repo, "make it executable") + (repo / SETTINGS).chmod(0o644) + commit(repo, "and back") + _named(repo, capsys, SETTINGS) + + +def test_b5_rename_into_a_plane_folder_and_back_out_is_1(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "mv", "src/app.py", ".claude/app.py") + commit(repo, "move into the agent folder") + git(repo, "mv", ".claude/app.py", "src/app.py") + commit(repo, "move back") + _named(repo, capsys, ".claude/app.py") + + +def _base_moves_on(repo): + """main changes a plane file after `pr` branched; the pull request's own commits never touch the plane.""" + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr: ordinary") + git(repo, "checkout", "-q", "main") + write(repo, ".github/workflows/chock.yml", "name: changed on main\n") + commit(repo, "main: workflow") + + +def test_b6_branch_that_merged_a_newer_base_is_0(tmp_path, capsys): + repo = base_repo(tmp_path) + _base_moves_on(repo) + git(repo, "checkout", "-q", "pr") + _merge(repo, "main") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_b6_branch_made_before_the_base_changed_judged_as_merge_result_is_0(tmp_path, capsys): + repo = base_repo(tmp_path) + _base_moves_on(repo) + git(repo, "checkout", "-q", "-b", "merge-ref") + _merge(repo, "pr") + code, out = run_floor(repo, capsys, "--head", "merge-ref") + assert code == 0, out + + +def test_b7_revert_of_an_unrelated_commit_is_0(tmp_path, capsys): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: write(r, "src/app.py", "VALUE = 5\n")) + git(repo, *USER, "revert", "--no-edit", "HEAD") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_b7_cherry_pick_of_a_commit_outside_the_plane_is_0(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "-b", "other") + write(repo, "src/other.py", "OTHER = 1\n") + picked = commit(repo, "other: source only") + git(repo, "checkout", "-q", "pr") + git(repo, *USER, "cherry-pick", picked) + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_b8_octopus_merge_bringing_a_plane_change_reverted_later_is_1(tmp_path, capsys): + repo = base_repo(tmp_path) + for name, rel, content in (("a", SETTINGS, HOOKLESS), ("b", "src/b.py", "B = 1\n"), ("c", "src/c.py", "C = 1\n")): + git(repo, "checkout", "-q", "-b", name, "pr") + write(repo, rel, content) + commit(repo, f"{name}: change") + git(repo, "checkout", "-q", "pr") + _merge(repo, "a", "b", "c") + assert len(git(repo, "rev-parse", "HEAD^@").split()) == 4 # an octopus: pr and three branches + write(repo, SETTINGS, CLAUDE) + commit(repo, "revert the hooks change the octopus brought") + _named(repo, capsys, SETTINGS) diff --git a/tests/test_floor_gate_links.py b/tests/test_floor_gate_links.py new file mode 100644 index 00000000..e00e01e0 --- /dev/null +++ b/tests/test_floor_gate_links.py @@ -0,0 +1,161 @@ +"""Release gate G1, GT-1 links (S1), scenarios A1-A6: each test asserts the exit the plan owes. + +A link at or inside a control-plane folder must open a control-plane path, or nothing, at base and head; else the +run is undecided (2) naming the link. A7-A11 are in test_floor_gate_links2.py.""" + +from __future__ import annotations + +import pytest +from floor_support import CLAUDE, base_repo, commit, git, run_floor, write +from floor_support4 import FAKE_A, gitlink, raw_commit, rebase_main, rm, symlink, undecided + +PLANE_FILE = ".codex/settings.json" +LIMIT = 32 # control_plane._MAX_HOPS: the most links one resolution may pass + + +def pr_source(repo): + """A pull request touching only source, staged by path so a hand-made index entry survives.""" + write(repo, "src/app.py", "VALUE = 2\n") + git(repo, "add", "src/app.py") + raw_commit(repo, "pr: source only") + + +def raw_base(repo, change): + """`change(repo)` stages the base by hand (gitlinks, empty links); commit it on main, put `pr` on it.""" + git(repo, "checkout", "-q", "main") + change(repo) + raw_commit(repo, "base edit") + git(repo, "checkout", "-q", "-B", "pr") + + +def test_a1_link_between_plane_folders_with_source_only_pr_is_0(tmp_path, capsys): + repo = base_repo(tmp_path) + + def base(r): + write(r, ".agents/skills/x/SKILL.md", "# x\n") + symlink(r, ".claude/skills", "../.agents/skills") + + rebase_main(repo, base) + pr_source(repo) + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_a2_plane_link_to_ordinary_folder_at_base_is_2_naming_link_and_target(tmp_path, capsys): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: symlink(r, ".claude/x", "../src")) + pr_source(repo) + out = undecided(repo, capsys, ".claude/x") + assert "opens src, which is not control plane" in out, out + + +def test_a3_plane_link_to_ordinary_folder_added_at_head_is_nonzero_naming_link(tmp_path, capsys): + repo = base_repo(tmp_path) + symlink(repo, ".claude/x", "../src") + commit(repo, "pr: link the agent folder to source") + code, out = run_floor(repo, capsys) + assert code != 0, out + assert ".claude/x" in out, out + + +def _link(r, link, target): + if link == ".claude": + rm(r, ".claude") + git(r, "rm", "-q", "-r", "--cached", "--ignore-unmatch", ".claude") + symlink(r, link, target) + git(r, "add", "-A") + + +def _chain(r, link, up, count): + """`link` -> c1 -> ... -> c -> .codex: `count` links in all, ending at a real plane folder.""" + write(r, PLANE_FILE, CLAUDE) + _link(r, link, f"{up}c1" if count > 1 else f"{up}.codex") + for i in range(1, count): + symlink(r, f"c{i}", f"c{i + 1}" if i < count - 1 else ".codex") + git(r, "add", "-A") + + +def _empty(r, link, _up): + _link(r, link, "placeholder") + empty = r.parent / "empty" + empty.write_bytes(b"") + oid = git(r, "hash-object", "-w", str(empty)) + git(r, "update-index", "--cacheinfo", f"120000,{oid},{link}") + + +def _submodule(r, link, up): + _link(r, link, f"{up}vendor/sub/x") + gitlink(r, "vendor/sub", FAKE_A) + + +A4_UNRESOLVED = { + "absolute": (lambda r, link, _up: _link(r, link, "/etc/agent"), "absolute or empty target '/etc/agent'"), + "climbs-out": (lambda r, link, up: _link(r, link, f"{up}../outside"), "leaves the repository"), + "loop": (lambda r, link, _up: _link(r, link, link.rsplit("/", 1)[-1]), f"more than {LIMIT} links"), + "empty": (_empty, "absolute or empty target ''"), + "submodule": (_submodule, "passes through the submodule vendor/sub"), + "chain-over-limit": (lambda r, link, up: _chain(r, link, up, LIMIT + 1), f"more than {LIMIT} links"), +} +WHERE = {"at": (".claude", ""), "inside": (".claude/hooks", "../")} + + +@pytest.mark.parametrize("where", list(WHERE)) +@pytest.mark.parametrize("case", list(A4_UNRESOLVED)) +def test_a4_plane_link_that_cannot_resolve_is_2(tmp_path, capsys, case, where): + build, why = A4_UNRESOLVED[case] + link, up = WHERE[where] + repo = base_repo(tmp_path) + raw_base(repo, lambda r: build(r, link, up)) + pr_source(repo) + out = undecided(repo, capsys, link) + assert why in out, out + + +@pytest.mark.parametrize("where", list(WHERE)) +def test_a4_chain_at_the_limit_ending_in_a_plane_path_is_0(tmp_path, capsys, where): + link, up = WHERE[where] + repo = base_repo(tmp_path) + raw_base(repo, lambda r: _chain(r, link, up, LIMIT)) + pr_source(repo) + code, out = run_floor(repo, capsys) + assert code == 0, out + + +A5_SPELLINGS = { + "dot-slash": "./../.agents/skills", + "doubled-slashes": "..//.agents//skills", + "trailing-slash": "../.agents/skills/", + "climb-and-return": "../src/../.agents/skills", + "case-only": "../.AGENTS/Skills", +} + + +@pytest.mark.parametrize("case", list(A5_SPELLINGS)) +def test_a5_odd_spelling_of_a_plane_target_is_0(tmp_path, capsys, case): + repo = base_repo(tmp_path) + + def base(r): + write(r, ".agents/skills/x/SKILL.md", "# x\n") + symlink(r, ".claude/skills", A5_SPELLINGS[case]) + + rebase_main(repo, base) + pr_source(repo) + code, out = run_floor(repo, capsys) + assert code == 0, (case, out) + + +def test_a6_dangling_plane_link_is_0(tmp_path, capsys): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: symlink(r, ".claude/hooks", "../cfg")) + pr_source(repo) + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_a6_target_absent_at_base_and_ordinary_at_head_is_2(tmp_path, capsys): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: symlink(r, ".claude/hooks", "../cfg")) + write(repo, "cfg/guard.sh", "#!/bin/sh\nexit 0\n") + commit(repo, "pr: fill the dangling target off the plane") + out = undecided(repo, capsys, ".claude/hooks") + assert "opens cfg, which is not control plane" in out, out diff --git a/tests/test_floor_gate_links2.py b/tests/test_floor_gate_links2.py new file mode 100644 index 00000000..d9699a07 --- /dev/null +++ b/tests/test_floor_gate_links2.py @@ -0,0 +1,110 @@ +"""Release gate G1, GT-1 links (S1), scenarios A7-A11: each test asserts the exit the plan owes.""" + +from __future__ import annotations + +import time + +from floor_support import CLAUDE, base_repo, commit, git, run_floor, write +from floor_support4 import FAKE_A, FAKE_B, gitlink, raw_commit, rebase_main, rm, symlink, undecided +from test_floor_gate_links import pr_source, raw_base + +A11_LINKS = 2000 +A11_SECONDS = 60 # an explicit bound in place of a suite-wide pytest timeout, which this repository does not set + + +def test_a7_link_at_an_ancestor_of_a_root_anchored_pattern_is_2(tmp_path, capsys): + """`.github -> gh`: the client opens gh/workflows/** as .github/workflows/**, and gh/ is not plane.""" + repo = base_repo(tmp_path) + + def base(r): + (r / ".github").rename(r / "gh") + symlink(r, ".github", "gh") + + rebase_main(repo, base) + pr_source(repo) + out = undecided(repo, capsys, ".github") + assert "opens gh, which is not control plane" in out, out + + +def test_a7_link_whose_target_tree_holds_an_agent_folder_is_0(tmp_path, capsys): + """`pkg -> real` with `real/.claude/`: real/.claude/** is plane by itself, so the link opens only plane paths.""" + repo = base_repo(tmp_path) + + def base(r): + write(r, "real/.claude/settings.json", CLAUDE) + symlink(r, "pkg", "real") + + rebase_main(repo, base) + pr_source(repo) + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_a8_plane_folder_replaced_by_a_link_to_an_identical_copy_is_nonzero(tmp_path, capsys): + repo = base_repo(tmp_path) + write(repo, "copy/claude/settings.json", CLAUDE) + rm(repo, ".claude") + symlink(repo, ".claude", "copy/claude") + commit(repo, "pr: the agent folder becomes a link to the same bytes") + code, out = run_floor(repo, capsys) + assert code != 0, out + assert ".claude" in out, out + + +def test_a9_ordinary_link_to_a_plane_file_is_0(tmp_path, capsys): + repo = base_repo(tmp_path) + symlink(repo, "docs/s", "../.claude/settings.json") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr: a docs link to the settings file") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_a10_submodule_added_at_a_plane_path_is_1_named(tmp_path, capsys): + repo = base_repo(tmp_path) + gitlink(repo, ".claude/shared", FAKE_A) + raw_commit(repo, "pr: add a submodule in the agent folder") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n .claude/shared\n" in out, out + + +def _base_submodule(repo): + raw_base(repo, lambda r: gitlink(r, ".claude/shared", FAKE_A)) + + +def test_a10_unchanged_submodule_inside_the_plane_is_0(tmp_path, capsys): + repo = base_repo(tmp_path) + _base_submodule(repo) + pr_source(repo) + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_a10_moved_submodule_pointer_inside_the_plane_is_1_named(tmp_path, capsys): + repo = base_repo(tmp_path) + _base_submodule(repo) + gitlink(repo, ".claude/shared", FAKE_B) + raw_commit(repo, "pr: bump the submodule") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert "\n .claude/shared\n" in out, out + + +def test_a11_two_thousand_plane_links_within_the_plane_is_0_in_bounded_time(tmp_path, capsys): + repo = base_repo(tmp_path) + + def base(r): + write(r, ".codex/settings.json", CLAUDE) + for i in range(A11_LINKS): + symlink(r, f".claude/links/l{i:04d}", "../../.codex/settings.json") + + rebase_main(repo, base) + assert len(git(repo, "ls-files", ".claude/links").splitlines()) == A11_LINKS + pr_source(repo) + start = time.monotonic() + code, out = run_floor(repo, capsys) + elapsed = time.monotonic() - start + print(f"A11: {A11_LINKS} links, exit {code} in {elapsed:.1f}s") + assert code == 0, out[-800:] + assert elapsed < A11_SECONDS, elapsed From de7cfa74144d37971e659300bca725fffedec9fc Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:55:09 +0000 Subject: [PATCH 29/57] Floor gate GT-2: pattern, config, exit, ordinary and docs tests Release gate G1 (option a), GT-2: regression tests for patterns D1-D6, config and SARIF E1-E4, abnormal paths F1-F2 and ordinary pull requests, and a map from every behaviour sentence in docs/floor.md and the --floor help to the test that proves it. Four scenarios did not give the owed result on the first run, and each fix is small and inside src/chock/validation/: - D2: a path matched below the top level by an any-depth pattern is now followed by the pattern that matched it. - D5: removing the old top-level form of a default names the pattern to write instead. - E3: a base config that is a folder or is not UTF-8 is undecided. - F1: base equal to head says nothing changed. The --floor help now says a link may also open nothing, which is what the code does and what docs/floor.md already says. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/w2a1-gate-patterns-exits.md | 8 ++ src/chock/validation/checks_floor.py | 5 + src/chock/validation/control_plane.py | 23 +++- src/chock/validation/floor_cli.py | 4 +- src/chock/validation/floor_outcome.py | 2 + tests/test_floor_gate_config.py | 158 ++++++++++++++++++++++ tests/test_floor_gate_docs.py | 155 ++++++++++++++++++++++ tests/test_floor_gate_docs_proofs.py | 168 ++++++++++++++++++++++++ tests/test_floor_gate_exits.py | 109 +++++++++++++++ tests/test_floor_gate_ordinary.py | 72 ++++++++++ tests/test_floor_gate_patterns.py | 87 ++++++++++++ 11 files changed, 787 insertions(+), 4 deletions(-) create mode 100644 changelog.d/w2a1-gate-patterns-exits.md create mode 100644 tests/test_floor_gate_config.py create mode 100644 tests/test_floor_gate_docs.py create mode 100644 tests/test_floor_gate_docs_proofs.py create mode 100644 tests/test_floor_gate_exits.py create mode 100644 tests/test_floor_gate_ordinary.py create mode 100644 tests/test_floor_gate_patterns.py diff --git a/changelog.d/w2a1-gate-patterns-exits.md b/changelog.d/w2a1-gate-patterns-exits.md new file mode 100644 index 00000000..92409ffe --- /dev/null +++ b/changelog.d/w2a1-gate-patterns-exits.md @@ -0,0 +1,8 @@ +- `chock check --floor`: a path listed only because an any-depth pattern matched it below the top level + (`node_modules/x/.claude/...`) is followed by the pattern that matched it (`matched **/.claude/**`). A base + `remove:` entry that is the old top-level form of a default (`.claude/**`) names the pattern to write + (`**/.claude/**`). A base `.chock/config.yaml` that is a folder, a symbolic link, a submodule or not UTF-8 is + undecided (exit 2). When base and head are the same commit the run says that nothing changed. The `--floor` + help says a link may also open nothing. Release gate G1 (GT-2): regression tests for patterns, config, SARIF, + abnormal paths and ordinary pull requests, and a map from every behaviour sentence in `docs/floor.md` and the + `--floor` help to the test that proves it. diff --git a/src/chock/validation/checks_floor.py b/src/chock/validation/checks_floor.py index 653c02ff..16a44c7a 100644 --- a/src/chock/validation/checks_floor.py +++ b/src/chock/validation/checks_floor.py @@ -199,6 +199,7 @@ def _judged(repo: Path, base: Revision, head: Revision, branch: str | None) -> O plane.mode, plane.summary(), ) + outcome.deep = {path: why for path in listed if (why := plane.deep_match(path))} if not base_gates(base): outcome.notes.append(_NO_GATES) try: @@ -223,6 +224,8 @@ def _print(outcome: Outcome) -> None: _out(f"{head} {text}") for path in outcome.listed: _out(f" {inert(path)}{_HISTORY_ONLY if path in outcome.history_only else ''}") + if path in outcome.deep: + _out(f" matched {inert(outcome.deep[path])}") if outcome.mode == cp.LISTED: _out(f"note: {_LISTED_GAP.format(rule=cp.RULE)}") for line in outcome.notes: @@ -253,6 +256,8 @@ def floor(repo: Path, base_ref: str, head_ref_rev: str, branch: str | None) -> O base_sha, head_sha = resolve(repo, base_ref, "base"), resolve(repo, head_ref_rev, "head") require_ancestor(repo, base_sha, head_sha) _out(f"== floor (preview): base {base_sha[:12]}, head {head_sha[:12]}") + if base_sha == head_sha: + _out("floor: base and head are the same commit; nothing changed") base, head = Revision(repo, base_sha), Revision(repo, head_sha) if not _base_chock(base): return Outcome(first_adoption=True) diff --git a/src/chock/validation/control_plane.py b/src/chock/validation/control_plane.py index d0762d67..b7bdfbd9 100644 --- a/src/chock/validation/control_plane.py +++ b/src/chock/validation/control_plane.py @@ -29,6 +29,7 @@ _EDITS = ("add", "remove") _FORMAT = "Cf" _SYMLINK = "120000" +_REGULAR = ("100644", "100755") _GLOB = frozenset("*?[") _ANY_DEPTH = "**/" _MAX_HOPS = 32 @@ -80,6 +81,12 @@ def covers(self, path: str) -> bool: name = folded(path) return name in self.hops or self._direct(name) + def deep_match(self, path: str) -> str | None: + """The any-depth pattern that matches `path` only below the top level, so the output can say why.""" + name = folded(path) + deep = (p for p in self.patterns if p.startswith(_ANY_DEPTH) and not fnmatch.fnmatchcase(name, folded(p)[3:])) + return next((p for p in deep if _match(name, p) or _match(f"{name}/", p)), None) + def changes(self, repo: Path, base: Revision, head: Revision) -> tuple[list[str], set[str]]: """(paths of the plane the tree diff changes or any commit leaves in a state the base lacks, those only in history).""" @@ -195,9 +202,18 @@ def _invalid(what: str) -> FloorError: def _section(base: Revision) -> dict: - text = base.text(_CONFIG) + state = base.state(_CONFIG) + if base.paths(f"{_CONFIG}/") or (state is not None and state[0] not in _REGULAR): + msg = ( + f"the base's {_CONFIG} is a folder, a symbolic link or a submodule, not a file; the floor reads only a file" + ) + raise FloorError(msg) try: + text = (base.blob(_CONFIG) or b"").decode("utf-8") doc = yamlio.safe_load(text) if text else None + except UnicodeDecodeError as exc: + msg = f"the base's {_CONFIG} is not UTF-8" + raise FloorError(msg) from exc except yaml.YAMLError as exc: msg = "could not be read as YAML" raise _invalid(msg) from exc @@ -247,7 +263,10 @@ def from_base(base: Revision, head: Revision) -> ControlPlane: edits, defaults = _edits(section), default_patterns() unknown = [p for p in edits["remove"] if p not in defaults] if unknown: - msg = f"{_PATHS}.remove names {unknown!r}, which is no default pattern" + hint = [f"{_ANY_DEPTH}{p}" for p in unknown if f"{_ANY_DEPTH}{p}" in defaults] + msg = f"{_PATHS}.remove names {unknown!r}, which is no default pattern" + ( + f"; write {hint[0]!r}" if hint else "" + ) raise _invalid(msg) kept = tuple(p for p in defaults if p not in edits["remove"]) added, unmatched = _added(edits["add"], (base, head)) diff --git a/src/chock/validation/floor_cli.py b/src/chock/validation/floor_cli.py index 693b5a30..ecbcbb92 100644 --- a/src/chock/validation/floor_cli.py +++ b/src/chock/validation/floor_cli.py @@ -16,8 +16,8 @@ def add_arguments(parser: argparse.ArgumentParser) -> None: "It holds only as a required check the pull request cannot redefine (your setting, unverified by chock) and " "judges committed content only; under `listed` you rely on code-owner review of the listed paths. The list is " "what chock knows about, not a proof of completeness (a file your workflow installs chock from belongs on it: " - "floor.control_plane_paths.add); a symlink at, inside or above the list must open a listed path or the run is " - "undecided; a renamed or moved test file reads as a deletion and is refused (docs/floor.md)", + "floor.control_plane_paths.add); a symlink at, inside or above the list must open a listed path or nothing, or " + "the run is undecided; a renamed or moved test file reads as a deletion and is refused (docs/floor.md)", ) parser.add_argument("--head", default=None, help="With --floor: the merge result under test (default HEAD)") diff --git a/src/chock/validation/floor_outcome.py b/src/chock/validation/floor_outcome.py index eec56bf0..7cda70cd 100644 --- a/src/chock/validation/floor_outcome.py +++ b/src/chock/validation/floor_outcome.py @@ -17,6 +17,8 @@ class Outcome: in_force: str = "" #: Listed paths that only a commit of the range changed; the tree diff leaves them as at the base. history_only: set[str] = field(default_factory=set) + #: Listed path -> the any-depth pattern that matched it below the top level (the cost of matching at depth). + deep: dict[str, str] = field(default_factory=dict) #: The base has no `.chock`: nothing was judged (V6). first_adoption: bool = False #: An error stopped the run before any verdict; `undecided` holds its one line. diff --git a/tests/test_floor_gate_config.py b/tests/test_floor_gate_config.py new file mode 100644 index 00000000..c8527cdb --- /dev/null +++ b/tests/test_floor_gate_config.py @@ -0,0 +1,158 @@ +"""Release gate G1, GT-2 config (S5) and SARIF (S6): E1-E4, each from the base's `.chock/config.yaml`.""" + +from __future__ import annotations + +import itertools +import json +import os +import shutil + +import pytest +from floor_support import CONTROL_PLANE, base_repo, commit, git, run_floor, write +from floor_support4 import rebase_main, rm + +_NOT_REQUIRED = "floor (preview): a floor only" + + +def _config(tmp_path, text): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: write(r, ".chock/config.yaml", text)) + return repo + + +@pytest.mark.parametrize( + ("entry", "change", "owed", "said"), + [ + ("infra/", "infra/deploy.sh", 1, "base added: infra/;"), + ("infra", "infra/deploy.sh", 2, "write 'infra/**' for the folder"), + ("infra/**", "infra/deploy.sh", 1, "base added: infra/**;"), + ("Makefile", "Makefile", 1, "base added: Makefile;"), + ], + ids=["E1-trailing-slash", "E1-bare-folder", "E1-glob", "E1-file"], +) +def test_e1_added_entries(tmp_path, capsys, entry, change, owed, said): + repo = _config(tmp_path, f"floor:\n control_plane_paths:\n add: ['{entry}']\n") + write(repo, "infra/deploy.sh", "echo base\n") if change == "Makefile" else None + write(repo, change, "echo changed\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == owed, out + assert said in out, out + if owed == 1: + assert f"\n {change}\n" in out, out + + +def test_e2_listed_mode_is_clean_and_lists_first(tmp_path, capsys): + repo = _config(tmp_path, "floor:\n control_plane: listed\n") + write(repo, ".claude/settings.json", "{}\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + first, listing = out.index("control plane: listed, "), out.index(f"LISTED {CONTROL_PLANE} (listed)") + assert first < listing < out.index("\n .claude/settings.json\n") < out.index(_NOT_REQUIRED), out + + +@pytest.mark.parametrize( + "value", ["Listed", "yes", "", "''", "3"], ids=["E2-capital", "E2-yes", "E2-empty", "E2-empty-string", "E2-number"] +) +def test_e2_a_mode_that_is_not_refuse_or_listed_is_undecided(tmp_path, capsys, value): + repo = _config(tmp_path, f"floor:\n control_plane: {value}\n") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "control_plane is " in out and "it takes refuse or listed" in out, out + + +def _directory(r): + write(r, ".chock/config.yaml/floor.yaml", "floor:\n control_plane: listed\n") + + +def _symlink(r): + write(r, "cfg/config.yaml", "floor:\n control_plane: listed\n") + os.symlink("../cfg/config.yaml", r / ".chock/config.yaml") + + +def _not_utf8(r): + (r / ".chock/config.yaml").write_bytes(b"floor:\n control_plane: listed\n# \xff\xfe\n") + + +@pytest.mark.parametrize("shape", [_directory, _symlink, _not_utf8], ids=["E3-directory", "E3-symlink", "E3-not-utf8"]) +def test_e3_a_base_config_that_is_not_a_utf8_file_is_undecided(tmp_path, capsys, shape): + repo = base_repo(tmp_path) + rebase_main(repo, shape) + write(repo, ".claude/settings.json", "{}\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert ".chock/config.yaml" in out, out + + +def _clean(tmp_path): + repo = base_repo(tmp_path) + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + return repo + + +def _refused(tmp_path): + repo = base_repo(tmp_path) + write(repo, ".claude/settings.json", "{}\n") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-1'\n") + commit(repo, "pr") + return repo + + +def _listed(tmp_path): + repo = base_repo(tmp_path, "listed") + write(repo, ".cursor/hooks.json", "{}\n") + commit(repo, "pr") + return repo + + +def _undecided(tmp_path): + repo = _config(tmp_path, "floor:\n control_plane: Listed\n") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + return repo + + +def _first_adoption(tmp_path): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + rebase_main(repo, lambda r: (shutil.rmtree(r / ".chock"), rm(r, "chock.lock"))) + write(repo, ".claude/settings.json", "{}\n") + commit(repo, "pr") + return repo + + +@pytest.mark.parametrize( + ("build", "owed"), + [(_clean, 0), (_refused, 1), (_listed, 0), (_undecided, 2), (_first_adoption, 0)], + ids=["E4-clean", "E4-refusal", "E4-listed", "E4-undecided", "E4-first-adoption"], +) +def test_e4_text_and_sarif_agree_and_sarif_never_changes_the_exit(tmp_path, capsys, build, owed): + repo = build(tmp_path) + plain, text = run_floor(repo, capsys) + sarif = tmp_path / "floor.sarif" + code, with_sarif = run_floor(repo, capsys, "--sarif", str(sarif)) + assert plain == code == owed, (plain, code, text) + assert with_sarif == text + run = json.loads(sarif.read_text(encoding="utf-8"))["runs"][0] + assert run["invocations"][0]["executionSuccessful"] is (code != 2), run + uris = [ + r["locations"][0]["physicalLocation"]["artifactLocation"]["uri"] for r in run["results"] if "locations" in r + ] + lines = text.splitlines() + start = next((i + 1 for i, line in enumerate(lines) if CONTROL_PLANE + " (" in line), len(lines)) + listed = [line[2:] for line in itertools.takewhile(lambda line: line.startswith(" "), lines[start:])] + listed = [path for path in listed if not path.startswith(" ")] + assert uris == listed, (uris, text) + levels = {r["level"] for r in run["results"]} + assert levels == ({"error"} if code == 1 else {"note"} if uris else set()), (levels, text) + refusals = [r["message"]["text"] for r in run["results"] if r["ruleId"] == "floor/refused"] + fails = [line for line in text.splitlines() if line.startswith("FAIL ") and CONTROL_PLANE not in line] + # SARIF carries a refusal on one line; the text puts each finding under its FAIL line. + assert len(fails) == len(refusals) and all(r.startswith(f[5:]) for f, r in zip(fails, refusals)), refusals + notes = [n["message"]["text"] for n in run["invocations"][0]["toolExecutionNotifications"]] + assert [f"UNDECIDED {n}" for n in notes] == [line for line in text.splitlines() if line.startswith("UNDECIDED ")] diff --git a/tests/test_floor_gate_docs.py b/tests/test_floor_gate_docs.py new file mode 100644 index 00000000..4f2755ab --- /dev/null +++ b/tests/test_floor_gate_docs.py @@ -0,0 +1,155 @@ +"""Release gate G1, GT-2 docs: every sentence of `docs/floor.md` and of the `--floor` help that states a behaviour +names the test that proves it, as test_managed_emit_deploy_text.py does for deploy.json. A new sentence fails here +until it names its proof; a proof that no longer exists fails too.""" + +from __future__ import annotations + +import argparse +import re +from pathlib import Path + +import pytest + +from chock.validation import floor_cli + +ROOT = Path(__file__).resolve().parents[1] +SUITE = Path(__file__).parent +#: A sentence that states no behaviour of the floor (a disclaimer or a note on testing), so no test can prove it. +NO_BEHAVIOUR = "states no behaviour" + +#: docs/floor.md, by each sentence's opening words (bold markers dropped, whitespace collapsed). +DOC_PROOF = { + "`chock check --floor --base --head ` judges": "test_a_head_rewrite_of_the_gate_does_not_stop_the_base_gate, test_head_cannot_lower_rollout", + "It is a preview, and it holds only": "test_docs_every_run_says_it_is_a_preview_and_states_its_conditions", + "Judge what will land. The base must be an ancestor": "test_head_not_descendant_of_base_is_undecided, test_a_stale_branch_is_undecided_and_its_merge_result_passes", + "Everything comes from the tree diff": "test_renaming_or_moving_a_test_file_is_refused_as_a_deletion, test_docs_external_diff_and_textconv_never_reach_the_floor, test_a_secret_added_then_removed_is_found_by_the_history_pass", + "Every git call reads attributes from the base": "test_attributes_come_from_the_base, test_a_git_that_ignores_attr_source_is_undecided", + "Control plane. A change (add, edit, delete, mode)": "test_mode_and_deletion_changes_are_listed, test_d1_agent_config_at_any_depth_is_refused_and_named", + "A path that a commit in the range changed": "test_a_guardrail_changed_and_changed_back_is_named", + "These files define the guardrails": "test_docs_a_listing_says_the_floor_does_not_judge_safety_and_a_person_decides", + "A person decides.": "test_docs_a_listing_says_the_floor_does_not_judge_safety_and_a_person_decides", + "Links. Every symbolic link at, inside or above": "test_a_link_above_a_nested_agent_folder_is_undecided, test_a_chain_of_symlinks_is_followed", + "If it opens a path that is itself control plane, or nothing": "test_a_link_to_another_control_plane_folder_carries_on, test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it, test_a_non_control_plane_hop_is_listed_when_retargeted", + "Otherwise the run is undecided (exit 2), naming the link": "test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it, test_a_link_above_a_nested_agent_folder_is_undecided", + "So is a loop, an absolute target": "test_a_control_plane_link_that_cannot_resolve_is_undecided, test_symlinked_agent_folder_into_a_submodule_bump_is_named", + "The fix: put the target on": "test_a_base_link_whose_target_the_base_adds_to_the_plane_lists_the_target, test_a_link_above_a_nested_agent_folder_is_undecided", + "A base whose `.chock` is a link or a submodule": "test_symlinked_chock_folder_at_base_is_not_first_adoption, test_docs_a_base_whose_chock_is_a_submodule_is_undecided", + "Everything else is judged by the base's compiled ci gates and rollout": "test_a_head_rewrite_of_the_gate_does_not_stop_the_base_gate, test_head_cannot_lower_rollout, test_a_base_observe_rollout_softens_history_and_a_warn_gate_never_refuses", + "No file from the head executes.": "test_no_head_file_is_executed, test_no_head_file_runs", + "`ask` refuses.": "test_ask_counts_as_a_refusal", + "A gate that crashes, times out or cannot be read": "test_a_crashing_base_script_gate_is_undecided_whatever_the_rollout, test_docs_a_gate_that_times_out_is_undecided_whatever_the_rollout, test_invalid_json_gate_at_base", + "A waiver counts only on a line present and unchanged": "test_a_base_waiver_on_a_line_the_pull_request_removes_is_not_honoured, test_a_waiver_the_pull_request_adds_is_not_honoured, test_a_content_waiver_on_an_unchanged_line_still_counts", + "History. The base's content gates read every blob": "test_a_secret_added_then_removed_is_found_by_the_history_pass, test_control_plane_change_and_revert_secret_in_history", + "Exit codes: 0 clean, 1 refused, 2 undecided.": "test_e4_text_and_sarif_agree_and_sarif_never_changes_the_exit", + "`--sarif ` writes a log on every run": "test_e4_text_and_sarif_agree_and_sarif_never_changes_the_exit, test_an_undecided_run_replaces_a_stale_sarif", + "default: exit 1 on any listed path.": "test_d1_agent_config_at_any_depth_is_refused_and_named", + "`listed`: exit 0, paths still listed": "test_e2_listed_mode_is_clean_and_lists_first", + "`name/` or `name/**` is the folder and all in it": "test_e1_added_entries", + "must name a default pattern exactly": "test_d5_removing_the_old_style_pattern_is_undecided_and_names_the_one_to_write, test_a_removal_that_names_no_default_pattern_is_undecided", + "Each run prints the mode, the number of patterns": "test_mode_and_size_are_stated_when_nothing_is_listed, test_d6_removing_the_any_depth_pattern_lets_the_change_through_and_says_so", + "An added entry that matches no path at base or head": "test_add_folder_forms", + "A required status check the pull request cannot redefine. The floor holds only as one.": "test_docs_every_run_says_it_is_a_preview_and_states_its_conditions", + "That is the repository's own setting, and chock does not verify it.": "test_docs_every_run_says_it_is_a_preview_and_states_its_conditions, test_no_coverage_tier_is_credited_for_the_floor", + "Committed content only.": "test_docs_only_committed_content_is_judged, test_no_head_file_reaches_the_working_tree", + "Under `refuse`, every guardrail change needs a privileged merge": "test_ok_add_policy_hook_lock_unrelated, test_an_action_bump_in_the_floor_workflow_is_a_control_plane_change", + "Under `listed`, you rely on code-owner review": "test_listed_mode_content_gap_is_stated", + "The content inside them is not judged.": "test_listed_mode_content_gap_is_stated", + "The list is what chock knows about, not a proof": NO_BEHAVIOUR, + "If your workflow installs chock from a file": "test_docs_a_file_the_workflow_installs_chock_from_can_be_put_on_the_list", + "Renames. Renaming or moving a test file": "test_renaming_or_moving_a_test_file_is_refused_as_a_deletion", + "Windows short names. The 8.3 alias": "test_docs_paths_the_list_does_not_cover", + "This is untested on Windows.": NO_BEHAVIOUR, + "`*.code-workspace` files. They are not on the list": "test_docs_paths_the_list_does_not_cover", + "Submodule content. Only the pointer is seen.": "test_docs_only_a_submodule_pointer_is_seen", + "A pointer at or inside a control-plane folder is listed": "test_a_gitlink_inside_a_control_plane_folder_is_listed, test_gitlink_at_a_vendor_folder_is_named, test_docs_only_a_submodule_pointer_is_seen", + "What the history pass does not see:": "test_docs_the_history_pass_does_not_see_other_encodings_commit_messages_or_file_names", + "Back-merges. A path is named from history": "test_a_guardrail_changed_and_changed_back_is_named, test_a_pull_request_merge_of_a_behind_branch_names_nothing_from_the_base", + "A branch that merged an older base": "test_docs_a_branch_that_merged_an_older_base_is_named_for_a_later_base_change", +} + +#: The `--floor` help, by each clause's opening words (clauses split at `; ` and `. ` outside parentheses). +HELP_PROOF = { + "Preview: judge base..head": "test_head_not_descendant_of_base_is_undecided, test_a_head_rewrite_of_the_gate_does_not_stop_the_base_gate", + "a changed guardrail path is listed and, by default, refused": "test_d1_agent_config_at_any_depth_is_refused_and_named, test_the_floor_runs_alone_and_needs_a_base", + "It holds only as a required check": "test_docs_every_run_says_it_is_a_preview_and_states_its_conditions, test_docs_only_committed_content_is_judged", + "under `listed` you rely on code-owner review": "test_listed_mode_content_gap_is_stated", + "The list is what chock knows about": "test_docs_a_file_the_workflow_installs_chock_from_can_be_put_on_the_list", + "a symlink at, inside or above the list must open a listed path or nothing": "test_a_link_to_another_control_plane_folder_carries_on, test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it, test_a_link_above_a_nested_agent_folder_is_undecided", + "a renamed or moved test file reads as a deletion": "test_renaming_or_moving_a_test_file_is_refused_as_a_deletion", +} + + +def _flat(text: str) -> str: + return re.sub(r"\s+", " ", re.sub(r"(? list[str]: + """Every sentence of docs/floor.md outside headings, with each YAML comment in its code block as one sentence.""" + blocks, fence, current = [], False, [] + for line in (ROOT / "docs/floor.md").read_text(encoding="utf-8").splitlines(): + if line.startswith("```"): + fence = not fence + elif fence and "#" in line: + blocks.append(line.split("#", 1)[1].strip()) + elif not fence and (line.startswith(("#", "- ")) or not line.strip()): + blocks.append(" ".join(current)) + current = [line[2:]] if line.startswith("- ") else [] + elif not fence: + current.append(line) + blocks.append(" ".join(current)) + found: list[str] = [] + for block in blocks: + start = len(found) + for part in re.split(r"(?<=\.|\*)\s+(?=[A-Z`*])", re.sub(r"\s+", " ", block).strip()): + # A bold lead ("**Renames.**") opens the sentence after it rather than standing alone. + joined = found.pop() + " " + part if len(found) > start and found[-1].endswith(".**") else part + found.append(joined) + return [_flat(sentence) for sentence in found if sentence] + + +def help_clauses() -> list[str]: + parser = argparse.ArgumentParser() + floor_cli.add_arguments(parser) + [text] = [a.help for a in parser._actions if "--floor" in a.option_strings] + clauses, depth, current = [], 0, "" + for i, char in enumerate(text): + depth += {"(": 1, ")": -1}.get(char, 0) + if char in ";." and depth == 0 and text[i + 1 : i + 2] in (" ", ""): + clauses.append(current.strip()) + current = "" + else: + current += char + return [c for c in [*clauses, current.strip()] if c] + + +def _proof_for(sentence: str, proofs: dict[str, str]) -> str: + owned = [proof for opening, proof in proofs.items() if sentence.startswith(opening)] + assert len(owned) == 1, f"{len(owned)} proofs for: {sentence}" + return owned[0] + + +def _sources() -> str: + return "".join(path.read_text(encoding="utf-8") for path in sorted(SUITE.glob("test_floor_*.py"))) + + +@pytest.mark.parametrize( + ("source", "proofs"), [(doc_sentences, DOC_PROOF), (help_clauses, HELP_PROOF)], ids=["docs", "help"] +) +def test_every_behaviour_sentence_has_a_proof_that_exists(source, proofs): + sources, used = _sources(), set() + for sentence in source(): + proof = _proof_for(sentence, proofs) + used.add(proof) + for name in (part.strip() for part in proof.split(",") if proof != NO_BEHAVIOUR): + assert f"def {name}(" in sources, (sentence, name) + stale = [ + opening + for opening, proof in proofs.items() + if proof not in used or not any(s.startswith(opening) for s in source()) + ] + assert not stale, f"no sentence opens with: {stale}" + + +def test_the_splitters_see_every_sentence(): + """The maps are only as good as the split: the docs give 46 sentences and the help 7 clauses.""" + assert (len(doc_sentences()), len(help_clauses())) == (len(DOC_PROOF), len(HELP_PROOF)) diff --git a/tests/test_floor_gate_docs_proofs.py b/tests/test_floor_gate_docs_proofs.py new file mode 100644 index 00000000..39ffb1b1 --- /dev/null +++ b/tests/test_floor_gate_docs_proofs.py @@ -0,0 +1,168 @@ +"""Release gate G1, GT-2 docs: the proofs for `docs/floor.md` and `--floor` help sentences no other floor test proves. +The map from each sentence to its proof is in test_floor_gate_docs.py.""" + +from __future__ import annotations + +import shutil + +import pytest +from floor_support import CONTROL_PLANE, PROBE, TEST_FILE, base_repo, commit, git, run_floor, write +from floor_support4 import FAKE_A, FAKE_B, gitlink, raw_commit, rebase_main + +from chock.gate.runner import script + +NOT_REQUIRED = ( + "floor (preview): a floor only where this check is a required status check the pull request cannot " + "redefine; that is the repository's own setting, unverified by chock. It judges committed content only." +) +A_PERSON_DECIDES = ( + "these files define the guardrails; the floor does not judge whether a change to them is safe. A person decides." +) + + +def _pr(repo, path, content): + write(repo, path, content) + commit(repo, "pr") + return repo + + +def test_docs_every_run_says_it_is_a_preview_and_states_its_conditions(tmp_path, capsys): + code, out = run_floor(_pr(base_repo(tmp_path), "src/app.py", "VALUE = 2\n"), capsys) + assert code == 0, out + assert out.startswith("== floor (preview): base "), out + assert NOT_REQUIRED in out, out + + +def test_docs_a_listing_says_the_floor_does_not_judge_safety_and_a_person_decides(tmp_path, capsys): + code, out = run_floor(_pr(base_repo(tmp_path), ".cursor/hooks.json", "{}\n"), capsys) + assert code == 1, out + assert f"FAIL {CONTROL_PLANE} (refuse): 1 path(s) -- {A_PERSON_DECIDES}" in out, out + + +def test_docs_external_diff_and_textconv_never_reach_the_floor(tmp_path, capsys): + """The base's attributes point tests at a textconv that drops asserts, and the clone sets an external diff.""" + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: write(r, ".gitattributes", "tests/*.py diff=strip\n")) + git(repo, "config", "diff.strip.textconv", "sed /assert/d") + git(repo, "config", "diff.external", "true") + code, out = run_floor(_pr(repo, "tests/test_core.py", "def test_a():\n pass\n"), capsys) + assert code == 1, out + assert "FAIL test-integrity [base] (block): Tests were weakened, not fixed." in out, out + + +@pytest.mark.parametrize("observe", [False, True], ids=["enforce", "observe"]) +def test_docs_a_gate_that_times_out_is_undecided_whatever_the_rollout(tmp_path, capsys, monkeypatch, observe): + repo = base_repo(tmp_path) + rebase_main( + repo, + lambda r: ( + write(r, PROBE["params"]["script"], "import time\ntime.sleep(30)\n"), + write(r, ".chock/config.yaml", "rollout: observe\n") if observe else None, + ), + ) + monkeypatch.setattr(script, "_SCRIPT_TIMEOUT_SECONDS", 1) + code, out = run_floor(_pr(repo, "src/app.py", "VALUE = 2\n"), capsys) + assert code == 2, out + assert "UNDECIDED probe [base]:" in out, out + + +def test_docs_only_committed_content_is_judged(tmp_path, capsys): + """The working tree holds a token and a weakened test; the committed head holds neither.""" + repo = _pr(base_repo(tmp_path), "src/app.py", "VALUE = 2\n") + write(repo, "src/app.py", "VALUE = 'FLOOR-TOKEN-9'\n") + write(repo, "tests/test_core.py", "def test_a():\n pass\n") + write(repo, ".claude/settings.json", "{}\n") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +@pytest.mark.parametrize(("listed", "owed"), [(False, 0), (True, 1)], ids=["default-list", "added-by-the-base"]) +def test_docs_a_file_the_workflow_installs_chock_from_can_be_put_on_the_list(tmp_path, capsys, listed, owed): + repo = base_repo(tmp_path) + config = "floor:\n control_plane_paths:\n add: ['requirements-ci.txt']\n" + rebase_main( + repo, + lambda r: ( + write(r, "requirements-ci.txt", "chock==1.1.0\n"), + listed and write(r, ".chock/config.yaml", config), + ), + ) + code, out = run_floor(_pr(repo, "requirements-ci.txt", "chock @ git+https://example.invalid/fork\n"), capsys) + assert code == owed, out + assert ("\n requirements-ci.txt\n" in out) is listed, out + + +@pytest.mark.parametrize("path", ["CLAUDE~1/settings.json", "project.code-workspace"], ids=["short-name", "workspace"]) +def test_docs_paths_the_list_does_not_cover(tmp_path, capsys, path): + """Not covered: a Windows 8.3 alias of a listed folder, and `*.code-workspace` files.""" + code, out = run_floor(_pr(base_repo(tmp_path), path, '{"settings": {"hooks": {}}}\n'), capsys) + assert code == 0, out + assert CONTROL_PLANE not in out, out + + +def test_docs_only_a_submodule_pointer_is_seen(tmp_path, capsys): + """A submodule outside the plane moves (it may hold agent folders of its own): nothing is listed or read.""" + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + gitlink(repo, "vendor/agents", FAKE_A) + raw_commit(repo, "base: a submodule") + git(repo, "checkout", "-q", "-B", "pr") + gitlink(repo, "vendor/agents", FAKE_B) + raw_commit(repo, "pr: bump it") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "vendor/agents" not in out.split("history:")[0], out + + +@pytest.mark.parametrize("shape", ["utf-32", "commit-message", "file-name"]) +def test_docs_the_history_pass_does_not_see_other_encodings_commit_messages_or_file_names(tmp_path, capsys, shape): + """Each commit pair adds, then removes, a token the history pass cannot see; the tree diff is clean.""" + repo = base_repo(tmp_path) + if shape == "utf-32": + (repo / "src/u32.txt").write_bytes("X = 'FLOOR-TOKEN-3'\n".encode("utf-32")) + elif shape == "file-name": + write(repo, "src/FLOOR-TOKEN-4.txt", "x\n") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "FLOOR-TOKEN-5 in the message" if shape == "commit-message" else "pr: add") + for path in ("src/u32.txt", "src/FLOOR-TOKEN-4.txt"): + (repo / path).unlink(missing_ok=True) + write(repo, "src/app.py", "VALUE = 3\n") + commit(repo, "pr: remove") + code, out = run_floor(repo, capsys) + assert code == 0, out + + +def test_docs_a_branch_that_merged_an_older_base_is_named_for_a_later_base_change(tmp_path, capsys): + """The branch merges main at M1 (workflow X), main moves to M2 (workflow Y); the merge left X, which M2 lacks.""" + repo = base_repo(tmp_path) + wf = ".github/workflows/chock.yml" + git(repo, "checkout", "-q", "main") + write(repo, wf, (repo / wf).read_text(encoding="utf-8") + "# X\n") + commit(repo, "M1") + git(repo, "checkout", "-q", "pr") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr: work") + git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "merge", "-q", "--no-edit", "main") + git(repo, "checkout", "-q", "main") + write(repo, wf, (repo / wf).read_text(encoding="utf-8") + "# Y\n") + commit(repo, "M2") + git(repo, "checkout", "-q", "--detach", "main") + git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "merge", "-q", "--no-edit", "pr") + code, out = run_floor(repo, capsys) + assert code == 1, out + assert f"\n {wf} (in a commit of the range only" in out, out + + +def test_docs_a_base_whose_chock_is_a_submodule_is_undecided(tmp_path, capsys): + repo = base_repo(tmp_path) + git(repo, "checkout", "-q", "main") + git(repo, "rm", "-r", "-q", "--cached", ".chock") + shutil.rmtree(repo / ".chock") + gitlink(repo, ".chock", FAKE_A) + raw_commit(repo, "base: .chock is a submodule") + git(repo, "checkout", "-q", "-B", "pr") + write(repo, "tests/test_core.py", TEST_FILE.replace("== 4", "== 4.0")) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "the base's .chock is a symbolic link or a submodule" in out, out diff --git a/tests/test_floor_gate_exits.py b/tests/test_floor_gate_exits.py new file mode 100644 index 00000000..ea892054 --- /dev/null +++ b/tests/test_floor_gate_exits.py @@ -0,0 +1,109 @@ +"""Release gate G1, GT-2 abnormal paths: F1-F2. None may end as 0 unless the scenario owes it.""" + +from __future__ import annotations + +import os +import subprocess +import sys + +import pytest +from floor_support import base_repo, commit, git, run_floor, write + +from chock.validation import checks_floor + + +def _main(capsys, *argv: str) -> tuple[int, str]: + code = checks_floor.main(list(argv)) + out = capsys.readouterr() + return code, out.out + out.err + + +def _pr(tmp_path, path="src/app.py", content="VALUE = 2\n"): + repo = base_repo(tmp_path) + write(repo, path, content) + commit(repo, "pr") + return repo + + +def test_f1_base_equal_to_head_is_clean_and_says_nothing_changed(tmp_path, capsys): + repo = base_repo(tmp_path) + code, out = _main(capsys, "--repo", str(repo), "--base", "main", "--head", "main") + assert code == 0, out + assert "base and head are the same commit; nothing changed" in out, out + + +def test_f1_a_repository_with_no_commits_is_undecided(tmp_path, capsys): + repo = tmp_path / "empty" + repo.mkdir() + git(repo, "init", "-q", "-b", "main") + code, out = _main(capsys, "--repo", str(repo), "--base", "main", "--head", "HEAD") + assert code == 2, out + assert "does not resolve" in out, out + + +@pytest.mark.parametrize("which", ["--base", "--head"]) +def test_f1_a_ref_that_does_not_exist_is_undecided(tmp_path, capsys, which): + repo = _pr(tmp_path) + refs = {"--base": "main", "--head": "pr", which: "no-such-ref"} + code, out = _main(capsys, "--repo", str(repo), "--base", refs["--base"], "--head", refs["--head"]) + assert code == 2, out + assert "'no-such-ref' does not resolve" in out, out + + +@pytest.mark.parametrize("obj", ["pr^{tree}", "pr:src/app.py"], ids=["F1-tree", "F1-blob"]) +def test_f1_a_head_that_is_a_tree_or_blob_is_undecided(tmp_path, capsys, obj): + repo = _pr(tmp_path) + code, out = _main(capsys, "--repo", str(repo), "--base", "main", "--head", git(repo, "rev-parse", obj)) + assert code == 2, out + assert "does not resolve" in out, out + + +def test_f1_a_head_that_is_an_annotated_tag_is_judged_as_its_commit(tmp_path, capsys): + repo = _pr(tmp_path, ".claude/settings.json", "{}\n") + git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "tag", "-a", "-m", "t", "v-pr", "pr") + by_commit = _main(capsys, "--repo", str(repo), "--base", "main", "--head", "pr") + by_tag = _main(capsys, "--repo", str(repo), "--base", "main", "--head", "v-pr") + assert by_tag == by_commit and by_tag[0] == 1, by_tag + assert f"head {git(repo, 'rev-parse', 'pr')[:12]}" in by_tag[1], by_tag + + +def test_f2_a_missing_object_is_undecided(tmp_path, capsys): + """A loose blob of the head deleted from a scratch copy: the floor cannot read it, so it cannot pass it.""" + repo = _pr(tmp_path, "src/new.py", "print('new')\n") + oid = git(repo, "rev-parse", "pr:src/new.py") + loose = repo / ".git/objects" / oid[:2] / oid[2:] + loose.unlink() + code, out = run_floor(repo, capsys) + assert code == 2, out + + +def test_f2_repo_that_is_not_a_repository_is_undecided(tmp_path, capsys): + plain = tmp_path / "plain" + plain.mkdir() + code, out = _main(capsys, "--repo", str(plain), "--base", "main") + assert code == 2, out + assert "is not inside a git repository" in out, out + + +def test_f2_git_absent_from_path_is_undecided(tmp_path): + repo = _pr(tmp_path) + env = {"PATH": str(tmp_path / "empty"), "HOME": os.environ["HOME"], "PYTHONPATH": os.pathsep.join(sys.path)} + script = "import sys; from chock.validation.checks_floor import main; sys.exit(main(sys.argv[1:]))" + proc = subprocess.run( + [sys.executable, "-c", script, "--repo", str(repo), "--base", "main"], + env=env, + capture_output=True, + text=True, + check=False, + ) + assert proc.returncode == 2, proc.stdout + proc.stderr + assert "git could not run" in proc.stderr, proc.stderr + + +@pytest.mark.parametrize("target", ["missing/x.sarif", "adir"], ids=["F2-no-parent", "F2-a-directory"]) +def test_f2_an_unwritable_sarif_path_is_non_zero_and_says_why(tmp_path, capsys, target): + repo = _pr(tmp_path) + (tmp_path / "adir").mkdir() + code, out = run_floor(repo, capsys, "--sarif", str(tmp_path / target)) + assert code != 0, out + assert "UNDECIDED floor: cannot write --sarif: " in out, out diff --git a/tests/test_floor_gate_ordinary.py b/tests/test_floor_gate_ordinary.py new file mode 100644 index 00000000..aa89ea0b --- /dev/null +++ b/tests/test_floor_gate_ordinary.py @@ -0,0 +1,72 @@ +"""Release gate G1, GT-2 ordinary pull requests: each touches no control-plane path and is owed exit 0.""" + +from __future__ import annotations + +import random + +import pytest +from floor_support import TEST_FILE, base_repo, commit, git, run_floor, write + +from chock.validation.floor_history import FLOOR_MAX_BLOB_BYTES + + +def _bytes(repo, rel, data): + path = repo / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) + + +def _merge_base_into_branch(repo): + """main moves on (a source edit), the branch edits a test, then merges main in: the merge is the head.""" + write(repo, "src/feature.py", "FEATURE = 1\n") + commit(repo, "pr: feature") + git(repo, "checkout", "-q", "main") + write(repo, "src/other.py", "OTHER = 1\n") + commit(repo, "main moves") + git(repo, "checkout", "-q", "pr") + git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "merge", "-q", "--no-ff", "--no-edit", "main") + + +def _revert(repo): + write(repo, "src/app.py", "VALUE = 3\n") + commit(repo, "pr: change") + git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "revert", "--no-edit", "HEAD") + + +GENERATED = b"generated_value_0123456789 = 'abcdefghijklmnopqrstuvwxyz'\n" * (4 * 2**20 // 58) + +CASES = { + "source-edit": lambda r: write(r, "src/app.py", "VALUE = 2\n"), + "new-test-file": lambda r: write(r, "tests/test_new.py", "def test_b():\n assert 3 > 2\n"), + "test-gains-assertions": lambda r: write(r, "tests/test_core.py", TEST_FILE + " assert 3 * 3 == 9\n"), + "docs-edit": lambda r: write(r, "docs/guide.md", "# Guide\n\nHow to run it.\n"), + "lockfile-bump": lambda r: write(r, "uv.lock", 'version = 1\n[[package]]\nname = "x"\nversion = "1.1"\n'), + "new-directory": lambda r: write(r, "pkg/sub/mod.py", "X = 1\n"), + "binary-asset": lambda r: _bytes(r, "assets/logo.png", b"\x89PNG\r\n\x1a\n" + random.Random(7).randbytes(65536)), # noqa: S311 + "large-generated-file": lambda r: _bytes(r, "src/generated_table.py", GENERATED), + "crlf-file": lambda r: _bytes(r, "src/win.py", b"A = 1\r\nB = 2\r\n"), + "bom-file": lambda r: _bytes(r, "src/bom.py", b"\xef\xbb\xbfA = 1\n"), +} + + +@pytest.mark.parametrize("name", list(CASES)) +def test_ordinary_pull_request_is_clean(tmp_path, capsys, name): + repo = base_repo(tmp_path) + CASES[name](repo) + commit(repo, f"pr: {name}") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "floor/control-plane-changed" not in out, out + + +def test_ordinary_large_generated_file_is_under_the_limit(): + assert 2**20 < len(GENERATED) < FLOOR_MAX_BLOB_BYTES # large, and judged rather than skipped + + +@pytest.mark.parametrize("build", [_merge_base_into_branch, _revert], ids=["merge-of-base", "revert-commit"]) +def test_ordinary_history_shapes_are_clean(tmp_path, capsys, build): + repo = base_repo(tmp_path) + build(repo) + code, out = run_floor(repo, capsys, "--head", "pr") + assert code == 0, out + assert "floor/control-plane-changed" not in out, out diff --git a/tests/test_floor_gate_patterns.py b/tests/test_floor_gate_patterns.py new file mode 100644 index 00000000..0f2a10a7 --- /dev/null +++ b/tests/test_floor_gate_patterns.py @@ -0,0 +1,87 @@ +"""Release gate G1, GT-2 patterns (S4): D1-D6. Each case builds B and H with the floor fixtures and asserts the owed exit.""" + +from __future__ import annotations + +import pytest +from floor_support import CONTROL_PLANE, base_repo, commit, run_floor, write +from floor_support4 import HOOKLESS, rebase_main + + +def _changed(tmp_path, capsys, path, content="{}\n"): + repo = base_repo(tmp_path) + write(repo, path, content) + commit(repo, "pr") + return run_floor(repo, capsys) + + +@pytest.mark.parametrize("path", [".claude/settings.json", "pkg/.claude/settings.json", "a/b/c/.codex/config.toml"]) +def test_d1_agent_config_at_any_depth_is_refused_and_named(tmp_path, capsys, path): + code, out = _changed(tmp_path, capsys, path, '{"hooks": {}}\n') + assert code == 1, out + assert f"FAIL {CONTROL_PLANE} (refuse)" in out, out + assert f"\n {path}\n" in out, out + + +@pytest.mark.parametrize( + ("path", "pattern"), + [("node_modules/x/.claude/settings.json", "**/.claude/**"), ("tests/fixtures/.cursor/hooks.json", "**/.cursor/**")], +) +def test_d2_a_deep_match_names_the_path_and_the_pattern(tmp_path, capsys, path, pattern): + """The stated cost of matching at any depth: a vendored or fixture agent folder is refused, and says why.""" + code, out = _changed(tmp_path, capsys, path) + assert code == 1, out + assert f"\n {path}\n" in out, out + assert f" matched {pattern}\n" in out, out + + +@pytest.mark.parametrize( + ("path", "owed"), + [ + (".env", 1), + ("sub/.env", 1), + (".ENV", 1), # matched case-blind + (".env.local", 0), + (".env.example", 0), + # The list folds every name as Windows opens it, dropping a trailing dot or space (control_plane.folded), + # so `.env.` and `.env ` are `.env`: 1. + (".env.", 1), + (".env ", 1), + ], +) +def test_d3_env_files(tmp_path, capsys, path, owed): + code, out = _changed(tmp_path, capsys, path, "TOKEN=x\n") + assert code == owed, out + if owed: + assert f"\n {path}\n" in out, out + + +@pytest.mark.parametrize("path", [".devcontainer.json", ".devcontainer/devcontainer.json", ".devcontainer/sub/x"]) +def test_d4_devcontainer(tmp_path, capsys, path): + code, out = _changed(tmp_path, capsys, path) + assert code == 1, out + assert f"\n {path}\n" in out, out + + +def _config(tmp_path, text): + repo = base_repo(tmp_path) + rebase_main(repo, lambda r: write(r, ".chock/config.yaml", text)) + return repo + + +def test_d5_removing_the_old_style_pattern_is_undecided_and_names_the_one_to_write(tmp_path, capsys): + repo = _config(tmp_path, "floor:\n control_plane_paths:\n remove: ['.claude/**']\n") + write(repo, "src/app.py", "VALUE = 2\n") + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 2, out + assert "'.claude/**'" in out and "write '**/.claude/**'" in out, out + + +def test_d6_removing_the_any_depth_pattern_lets_the_change_through_and_says_so(tmp_path, capsys): + repo = _config(tmp_path, "floor:\n control_plane_paths:\n remove: ['**/.claude/**']\n") + write(repo, ".claude/settings.json", HOOKLESS) + commit(repo, "pr") + code, out = run_floor(repo, capsys) + assert code == 0, out + assert "control plane: refuse, " in out and "base removed: **/.claude/**" in out, out + assert ".claude/settings.json" not in out, out From eaa9a77c0d385baa4697909e69cdfaa6e7e4e5b3 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:58:16 +0000 Subject: [PATCH 30/57] Floor: a link above the plane carries on when it opens only plane paths Orchestrator ruling on G1 A7: `pkg -> real` with `real/.claude/` is 0, because every control-plane path under the link opens a control-plane path. The link is then a hop, so adding, removing or retargeting it is named. A link above a plane path whose target is not plane stays 2, as do links at or inside the plane (S1 otherwise unchanged). The review-4 S1 test for this shape now owes 0; docs/floor.md says so. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- docs/floor.md | 4 ++- src/chock/validation/control_plane.py | 11 +++++-- tests/test_floor_gate_links2.py | 42 +++++++++++++++++++++++++++ tests/test_floor_review4_rulings.py | 8 +++-- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/docs/floor.md b/docs/floor.md index a021fff1..088f1cc3 100644 --- a/docs/floor.md +++ b/docs/floor.md @@ -18,7 +18,9 @@ link passed on the way is control plane too. Otherwise the run is undecided (exit 2), naming the link and its target. So is a loop, an absolute target, a target outside the repository, or one that passes through a submodule. The fix: put the target on `floor.control_plane_paths.add` in the base's config, or replace the link - with real files. A base whose `.chock` is a link or a submodule is undecided too. + with real files. A link above a control-plane path (`pkg -> real`, with `real/.claude/`) carries on when every + control-plane path under it opens a control-plane path, and the link is then control plane too. A base whose + `.chock` is a link or a submodule is undecided too. - **Everything else** is judged by the base's compiled ci gates and rollout. No file from the head executes. `ask` refuses. A gate that crashes, times out or cannot be read is undecided, whatever the rollout. A waiver counts only on a line present and unchanged in both base and head. diff --git a/src/chock/validation/control_plane.py b/src/chock/validation/control_plane.py index d0762d67..2a9aee58 100644 --- a/src/chock/validation/control_plane.py +++ b/src/chock/validation/control_plane.py @@ -156,15 +156,20 @@ def _unresolved(link: str, rev: Revision, why: str) -> FloorError: def _opens_control_plane(plane: ControlPlane, tree: _Tree, name: str) -> tuple[str, list[str]] | None: - """(target, links passed) when the link `name` is a control-plane root, inside one, or above one; else None.""" + """(what must be control plane, links passed) when the link `name` is a control-plane root, inside one, or above + one; else None. Above one, every control-plane path under it must open one: else its target is returned.""" if plane.covers(name): return tree.resolve(tree.links[name]) try: target, passed = tree.resolve(tree.links[name]) except FloorError: return None # leaves the repository: nothing committed lies under it - rests = (f[len(target) :] if target else f"/{f}" for f in tree.below(target)) - return (target, passed) if any(plane.covers(name + rest) for rest in rests) else None + rests = [f[len(target) :] if target else f"/{f}" for f in tree.below(target)] + reached = [rest for rest in rests if plane.covers(name + rest)] + if not reached: + return None + opened = [f"{target}{rest}" if target else rest[1:] for rest in reached] + return next((target for path in opened if not plane.covers(path)), opened[0]), passed def closed_under_links(plane: ControlPlane, *revisions: Revision) -> ControlPlane: diff --git a/tests/test_floor_gate_links2.py b/tests/test_floor_gate_links2.py index d9699a07..1d2cd29c 100644 --- a/tests/test_floor_gate_links2.py +++ b/tests/test_floor_gate_links2.py @@ -4,6 +4,7 @@ import time +import pytest from floor_support import CLAUDE, base_repo, commit, git, run_floor, write from floor_support4 import FAKE_A, FAKE_B, gitlink, raw_commit, rebase_main, rm, symlink, undecided from test_floor_gate_links import pr_source, raw_base @@ -108,3 +109,44 @@ def base(r): print(f"A11: {A11_LINKS} links, exit {code} in {elapsed:.1f}s") assert code == 0, out[-800:] assert elapsed < A11_SECONDS, elapsed + + +def _real_and_other(r): + write(r, "real/.claude/settings.json", CLAUDE) + write(r, "other/.claude/settings.json", CLAUDE) + + +A7_LINK_CHANGES = { + "added": (_real_and_other, lambda r: symlink(r, "pkg", "real")), + "removed": (lambda r: (_real_and_other(r), symlink(r, "pkg", "real")), lambda r: rm(r, "pkg")), + "retargeted": (lambda r: (_real_and_other(r), symlink(r, "pkg", "real")), lambda r: symlink(r, "pkg", "other")), +} + + +@pytest.mark.parametrize("case", list(A7_LINK_CHANGES)) +def test_a7_link_above_the_plane_is_itself_plane_when_changed(tmp_path, capsys, case): + """Orchestrator ruling on A7: the link above the plane counts as a hop, so adding, removing or retargeting it + (even to another folder holding an agent folder) is named.""" + base, change = A7_LINK_CHANGES[case] + repo = base_repo(tmp_path) + rebase_main(repo, base) + change(repo) + commit(repo, f"pr: link {case}") + code, out = run_floor(repo, capsys) + assert code == 1, (case, out) + assert "\n pkg\n" in out, (case, out) + + +def test_a7_link_above_a_plane_path_whose_target_is_not_plane_is_2(tmp_path, capsys): + """Ruling on A7, condition 1: `p*/secret/**` covers pkg/secret/x but not real/secret/x, which pkg opens.""" + repo = base_repo(tmp_path) + + def base(r): + write(r, ".chock/config.yaml", "floor:\n control_plane_paths:\n add: ['p*/secret/**']\n") + write(r, "real/secret/x", "x\n") + symlink(r, "pkg", "real") + + rebase_main(repo, base) + pr_source(repo) + out = undecided(repo, capsys, "pkg") + assert "opens real, which is not control plane" in out, out diff --git a/tests/test_floor_review4_rulings.py b/tests/test_floor_review4_rulings.py index 8b71b03b..71801b90 100644 --- a/tests/test_floor_review4_rulings.py +++ b/tests/test_floor_review4_rulings.py @@ -90,13 +90,15 @@ def change(r): assert "\n cfg\n" in out, out -def test_a_link_above_a_nested_agent_folder_is_undecided(tmp_path, capsys): - """S1, ancestor: `pkg -> vendor` with `vendor/.claude/`: the client opens pkg/.claude through `pkg`.""" +def test_a_link_above_a_nested_agent_folder_that_opens_only_plane_paths_carries_on(tmp_path, capsys): + """S1 as amended by G1 A7: `pkg -> vendor` with `vendor/.claude/`: pkg/.claude opens vendor/.claude, which is + control plane by itself, so an ordinary change is 0.""" repo = base_repo(tmp_path) _base_link(repo, "pkg", "vendor", "vendor/.claude/settings.json") write(repo, "src/app.py", "VALUE = 2\n") commit(repo, "pr") - undecided(repo, capsys, "pkg") + code, out = run_floor(repo, capsys) + assert code == 0, out def test_an_unrelated_link_out_of_the_repository_is_not_judged(tmp_path, capsys): From b8498799dc349e5e794459c72e8311f9f495c6e2 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:32:45 +0000 Subject: [PATCH 31/57] test(floor): repair the gate docs map after GT-1 and GT-2 merge Name the renamed link tests in three DOC_PROOF entries and one HELP_PROOF entry, add the proof entry for the sentence opening "A link above a control-plane path", and correct the sentence count from 46 to 47. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- tests/test_floor_gate_docs.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/test_floor_gate_docs.py b/tests/test_floor_gate_docs.py index 4f2755ab..e5a72728 100644 --- a/tests/test_floor_gate_docs.py +++ b/tests/test_floor_gate_docs.py @@ -28,11 +28,12 @@ "A path that a commit in the range changed": "test_a_guardrail_changed_and_changed_back_is_named", "These files define the guardrails": "test_docs_a_listing_says_the_floor_does_not_judge_safety_and_a_person_decides", "A person decides.": "test_docs_a_listing_says_the_floor_does_not_judge_safety_and_a_person_decides", - "Links. Every symbolic link at, inside or above": "test_a_link_above_a_nested_agent_folder_is_undecided, test_a_chain_of_symlinks_is_followed", + "Links. Every symbolic link at, inside or above": "test_a7_link_at_an_ancestor_of_a_root_anchored_pattern_is_2, test_a7_link_above_a_plane_path_whose_target_is_not_plane_is_2, test_a_chain_of_symlinks_is_followed", + "A link above a control-plane path": "test_a7_link_whose_target_tree_holds_an_agent_folder_is_0, test_a7_link_above_the_plane_is_itself_plane_when_changed", "If it opens a path that is itself control plane, or nothing": "test_a_link_to_another_control_plane_folder_carries_on, test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it, test_a_non_control_plane_hop_is_listed_when_retargeted", - "Otherwise the run is undecided (exit 2), naming the link": "test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it, test_a_link_above_a_nested_agent_folder_is_undecided", + "Otherwise the run is undecided (exit 2), naming the link": "test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it, test_a7_link_at_an_ancestor_of_a_root_anchored_pattern_is_2, test_a7_link_above_a_plane_path_whose_target_is_not_plane_is_2", "So is a loop, an absolute target": "test_a_control_plane_link_that_cannot_resolve_is_undecided, test_symlinked_agent_folder_into_a_submodule_bump_is_named", - "The fix: put the target on": "test_a_base_link_whose_target_the_base_adds_to_the_plane_lists_the_target, test_a_link_above_a_nested_agent_folder_is_undecided", + "The fix: put the target on": "test_a_base_link_whose_target_the_base_adds_to_the_plane_lists_the_target, test_a7_link_at_an_ancestor_of_a_root_anchored_pattern_is_2, test_a7_link_above_a_plane_path_whose_target_is_not_plane_is_2", "A base whose `.chock` is a link or a submodule": "test_symlinked_chock_folder_at_base_is_not_first_adoption, test_docs_a_base_whose_chock_is_a_submodule_is_undecided", "Everything else is judged by the base's compiled ci gates and rollout": "test_a_head_rewrite_of_the_gate_does_not_stop_the_base_gate, test_head_cannot_lower_rollout, test_a_base_observe_rollout_softens_history_and_a_warn_gate_never_refuses", "No file from the head executes.": "test_no_head_file_is_executed, test_no_head_file_runs", @@ -74,7 +75,7 @@ "It holds only as a required check": "test_docs_every_run_says_it_is_a_preview_and_states_its_conditions, test_docs_only_committed_content_is_judged", "under `listed` you rely on code-owner review": "test_listed_mode_content_gap_is_stated", "The list is what chock knows about": "test_docs_a_file_the_workflow_installs_chock_from_can_be_put_on_the_list", - "a symlink at, inside or above the list must open a listed path or nothing": "test_a_link_to_another_control_plane_folder_carries_on, test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it, test_a_link_above_a_nested_agent_folder_is_undecided", + "a symlink at, inside or above the list must open a listed path or nothing": "test_a_link_to_another_control_plane_folder_carries_on, test_a_dangling_control_plane_link_carries_on_until_the_head_fills_it, test_a7_link_at_an_ancestor_of_a_root_anchored_pattern_is_2, test_a7_link_above_a_plane_path_whose_target_is_not_plane_is_2", "a renamed or moved test file reads as a deletion": "test_renaming_or_moving_a_test_file_is_refused_as_a_deletion", } @@ -151,5 +152,5 @@ def test_every_behaviour_sentence_has_a_proof_that_exists(source, proofs): def test_the_splitters_see_every_sentence(): - """The maps are only as good as the split: the docs give 46 sentences and the help 7 clauses.""" + """The maps are only as good as the split: the docs give 47 sentences and the help 7 clauses.""" assert (len(doc_sentences()), len(help_clauses())) == (len(DOC_PROOF), len(HELP_PROOF)) From 30a5136a9cf7ef783405be8be72d28bdf35e560b Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:01:32 +0000 Subject: [PATCH 32/57] docs: 1.2.0 release docs and evidence (REL-D1) Quick start drops --ref and --verify-sha and says what chock add pins by default. A short README section names chock doctor, chock doctor --live and the rollout line. The witness ledger gains the 2026-11-11 Claude Code repo hook row; the derived README sentence and the one test that assumed no Claude Code repo-hook witness are updated. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 14 ++++++++++---- changelog.d/rel-d1-docs-evidence.md | 9 +++++++++ docs/assets/readme/render.mjs | 4 ++-- docs/quickstart.sh | 2 +- src/chock/data/witnesses.json | 7 +++++++ tests/test_sync_every_adapter.py | 11 ++++++----- 6 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 changelog.d/rel-d1-docs-evidence.md diff --git a/README.md b/README.md index 77a900d7..195f9fa6 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ pipx install chock==1.1.0 ### Two ways to adopt it -1. **In your repository, for teams.** Run `chock init .`, then `chock add --ref --verify-sha --skip-compile` for each policy, then `chock sync --repo . --ci`. Commit the result. A fresh clone arms itself from what is committed; a person runs `chock sync` to change it. The commit gates are enforced at commit and in CI. +1. **In your repository, for teams.** Run `chock init .`, then `chock add --skip-compile` for each policy, then `chock sync --repo . --ci`. Commit the result. A fresh clone arms itself from what is committed; a person runs `chock sync` to change it. The commit gates are enforced at commit and in CI. 2. **In your coding agent, as one plugin.** Pick policies in the [chock.sh builder](https://chock.sh/coding/builder/) and run `chock install --selection --apply`. It builds one plugin for Claude Code, GitHub Copilot, Codex, Cursor, Devin, goose, Grok or Antigravity, tested live, and, untested by chock, for OpenHands, Kimi Code or Factory Droid. Best-effort, and it fails open. A selection can add [your own policies](docs/custom-policies.md): install shows their code and asks first. No chock? Add a per-policy marketplace (`chock-*-plugins`; needs Python 3.11+, git, a bash: Git for Windows has one), never both. ## Application security for the code your agents write @@ -83,7 +83,7 @@ pip install chock==1.1.0 git init -q demo && cd demo chock init . # wiring only, no policies -chock add scan-secrets --ref 1e60d211c1bb767152c6ab0167932da06ab3a10f --verify-sha c7cf363c6eb8a8ed47287beef9c06fe76dfd2d9c0ccc203fe74e32fba0e92a94 --skip-compile +chock add scan-secrets --skip-compile chock sync --repo . --ci # compile, install hooks, add the CI gate ``` @@ -104,7 +104,13 @@ git commit -m "read key from env" # passes alt="Terminal recording of the Quick start above, typed in real time, ending with the refused commit and the clean one.">

-`--ref` pins a catalog commit, `--verify-sha` refuses a policy whose hash differs, and `chock add --ref` refuses a commit that was never published. The allowlist pragma on the key line is for documented test fixtures only. It is honoured at commit, push and in CI, and deliberately not at tool-use, where the scanned text is a live tool argument. +With no `--ref`, `chock add` pins the catalog tag `v`, prints the commit it resolved to and the sha256 of the policy, and installs nothing when that tag does not exist. A tag can move: `--ref <40-hex commit>` pins a catalog commit by hand and refuses a commit that was never published, and `--verify-sha` refuses a policy whose hash differs. The allowlist pragma on the key line is for documented test fixtures only. It is honoured at commit, push and in CI, and deliberately not at tool-use, where the scanned text is a live tool argument. + +### Check it, or try it without blocking + +`chock doctor` checks the hooks `chock sync` wrote for each client and prints PASS, WARN, FAIL or INFO with a fix; it exits 1 on a FAIL ([Doctor](docs/doctor.md)). `chock doctor --live` also runs chock's own pre-tool hook commands from those files, not the client: once on a command a policy refuses, which must be refused, and once on `echo chock-doctor-live`, which must pass. It skips plugin installs and counts, but never runs, a hook command that is not byte-equal to what this chock renders. + +To record instead of refusing, add a top-level `rollout:` line to `.chock/config.yaml`. `observe` turns a refusal into a warning and records what `enforce` would have stopped in `.chock/log/gate-events.jsonl`; `ask` caps a block at ask; `enforce` is the default, and a missing, unknown or repeated value means `enforce`. Your own uncommitted edit counts for your own commit; an agent is held to the level committed at HEAD, and CI never takes a pull request below its base's level. `chock stop` ([Stop](docs/stop.md)) stays in force at every level. ### Shift left, with no model and no tokens @@ -163,7 +169,7 @@ Full nine-surface matrix and per-agent caveats: | **Kimi Code** | git hook + CI gate only — home-anchored config, outside chock's repo-scoped install | `~/.kimi-code/config.toml` | | **Replit** | git hook + CI gate only — no native hook API yet | — | -Chock tested its repo hooks live in Copilot CLI and VS Code agent mode only; other native hooks follow agentseam's record, untested. +Chock tested its repo hooks live in Copilot CLI and VS Code agent mode; Claude Code v2.1.317, Windows only; other native hooks follow agentseam's record, untested. ## What it stops diff --git a/changelog.d/rel-d1-docs-evidence.md b/changelog.d/rel-d1-docs-evidence.md new file mode 100644 index 00000000..55b4c137 --- /dev/null +++ b/changelog.d/rel-d1-docs-evidence.md @@ -0,0 +1,9 @@ +- README: the quick start's `chock add` line no longer carries `--ref` and `--verify-sha`; the text under + it says what the command pins by default (the catalog tag `v`) and that `--ref` and + `--verify-sha` still pin by hand. `docs/quickstart.sh` and the panel sources in + `docs/assets/readme/render.mjs` follow. +- README: a short section after the quick start names `chock doctor`, `chock doctor --live` and the + `rollout:` line in `.chock/config.yaml`. +- Witness ledger: one row, Claude Code v2.1.317 on Windows, 2026-11-11: `PreToolUse` refused + `git commit --no-verify` on the PowerShell tool and no commit was made (chock `295b38c`). The README + sentence on where repo hooks were tested now names it. diff --git a/docs/assets/readme/render.mjs b/docs/assets/readme/render.mjs index 4a40a163..5a758364 100644 --- a/docs/assets/readme/render.mjs +++ b/docs/assets/readme/render.mjs @@ -111,7 +111,7 @@ const ADOPT = page(`
Route 1for teams
In your repository
-${term([cmd('chock init .'), cmd('chock add <id> --ref <sha> \\'), '
  --verify-sha <sha256> --skip-compile
', cmd('chock sync --repo . --ci')])} +${term([cmd('chock init .'), cmd('chock add <id> --skip-compile'), cmd('chock sync --repo . --ci')])}
Everyone and every agent on the repo. Enforced at commit and in CI. Travels with every clone.
Route 2no repo changes
@@ -187,7 +187,7 @@ ${layer('Templates', 'chock-quickstart is what chock init leaves behind. chock-e const QUICKSTART = SECTION('See it work', 'Two commands. Real hooks. Real refusals.', `
-${term(['
# set up once
', cmd('chock init .'), cmd('chock add scan-secrets --ref <sha> \\'), '
  --verify-sha <sha256> --skip-compile
', cmd('chock sync --repo . --ci')], 'git never clones hooks. Every clone runs chock sync --repo . once.')} +${term(['
# set up once
', cmd('chock init .'), cmd('chock add scan-secrets --skip-compile'), cmd('chock sync --repo . --ci')], 'git never clones hooks. Every clone runs chock sync --repo . once.')} ${term(['
# the next commit with a credential
', cmd('git commit -m "add config"'), '
✗ Potential secret detected in this change.
', '
  - config.py: content pattern
', cmd('git commit -m "read key from env"'), '
✓ passes
'])}
${note('The refusal text is the gate message of scan-secrets in chock-catalog. The recording is in the text version.')}`); diff --git a/docs/quickstart.sh b/docs/quickstart.sh index 27cb1407..a3b7b4fc 100755 --- a/docs/quickstart.sh +++ b/docs/quickstart.sh @@ -8,5 +8,5 @@ pip install chock==1.1.0 git init -q demo && cd demo chock init . # wiring only, no policies -chock add scan-secrets --ref 1e60d211c1bb767152c6ab0167932da06ab3a10f --verify-sha c7cf363c6eb8a8ed47287beef9c06fe76dfd2d9c0ccc203fe74e32fba0e92a94 --skip-compile +chock add scan-secrets --skip-compile chock sync --repo . --ci # compile, install hooks, add the CI gate diff --git a/src/chock/data/witnesses.json b/src/chock/data/witnesses.json index 16f05d48..0571b974 100644 --- a/src/chock/data/witnesses.json +++ b/src/chock/data/witnesses.json @@ -68,5 +68,12 @@ "client": "Claude Code, Bash tool (CLAUDECODE=1, AI_AGENT=claude-code__agent)", "date": "2026-09-29", "method": "live probe: both variables were in the environment of a pre-commit hook run by `git commit` from the Bash tool (chock 1b0bcdd)" + }, + { + "agent": "claude_code", + "surface": "agent-hooks", + "client": "Claude Code v2.1.317, Windows", + "date": "2026-11-11", + "method": "live probe: PreToolUse refused `git commit --no-verify` on the PowerShell tool and no commit was made (chock 295b38c)" } ] diff --git a/tests/test_sync_every_adapter.py b/tests/test_sync_every_adapter.py index 44ef2cb0..a40fb9a2 100644 --- a/tests/test_sync_every_adapter.py +++ b/tests/test_sync_every_adapter.py @@ -157,13 +157,14 @@ def test_sync_says_agentseams_row_and_that_the_agent_is_untested(vendor: str) -> assert any("Untested" in line for line in lines) -def test_a_witnessed_agent_is_not_called_untested() -> None: - assert evidence.witness("vscode_copilot", "agent-hooks"), "the ledger's repo-hook row this test rests on" - assert sync_notes.witnessed("vscode_copilot") - assert not any("Untested" in line for line in sync_notes.notes("vscode_copilot")) +@pytest.mark.parametrize("vendor", ("vscode_copilot", "claude_code")) +def test_a_witnessed_agent_is_not_called_untested(vendor: str) -> None: + assert evidence.witness(vendor, "agent-hooks"), "the ledger's repo-hook row this test rests on" + assert sync_notes.witnessed(vendor) + assert not any("Untested" in line for line in sync_notes.notes(vendor)) -@pytest.mark.parametrize("vendor", ("claude_code", "codex_cli", "cursor", "devin")) +@pytest.mark.parametrize("vendor", ("codex_cli", "cursor", "devin")) def test_a_plugin_witness_does_not_make_sync_say_tested(vendor: str) -> None: """A plugin-hook row witnesses `chock install`; sync wires repo hooks nobody saw run there.""" assert evidence.witness(vendor, evidence.PLUGIN_HOOK) From 1e09bac46acf0bfadac04df99798f836cf099d61 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:17:18 +0000 Subject: [PATCH 33/57] docs: witness ledger row for Cursor repo hooks (REL-D1) Cursor 3.30.14 on Windows, 2026-11-14: the repo's beforeShellExecution hook refused git commit --no-verify. The derived README sentence is regenerated; the sync-notes test moves cursor to the witnessed case. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 2 +- changelog.d/rel-d1-docs-evidence.md | 3 +++ src/chock/data/witnesses.json | 7 +++++++ tests/test_sync_every_adapter.py | 4 ++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 195f9fa6..e05a2aaa 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ Full nine-surface matrix and per-agent caveats: | **Kimi Code** | git hook + CI gate only — home-anchored config, outside chock's repo-scoped install | `~/.kimi-code/config.toml` | | **Replit** | git hook + CI gate only — no native hook API yet | — | -Chock tested its repo hooks live in Copilot CLI and VS Code agent mode; Claude Code v2.1.317, Windows only; other native hooks follow agentseam's record, untested. +Chock tested its repo hooks live in Copilot CLI and VS Code agent mode; Claude Code v2.1.317, Windows; Cursor 3.30.14, Windows only; other native hooks follow agentseam's record, untested. ## What it stops diff --git a/changelog.d/rel-d1-docs-evidence.md b/changelog.d/rel-d1-docs-evidence.md index 55b4c137..afb98ecc 100644 --- a/changelog.d/rel-d1-docs-evidence.md +++ b/changelog.d/rel-d1-docs-evidence.md @@ -7,3 +7,6 @@ - Witness ledger: one row, Claude Code v2.1.317 on Windows, 2026-11-11: `PreToolUse` refused `git commit --no-verify` on the PowerShell tool and no commit was made (chock `295b38c`). The README sentence on where repo hooks were tested now names it. +- Witness ledger: a second row, Cursor 3.30.14 on Windows, 2026-11-14: the repo's `beforeShellExecution` hook refused + `git commit --no-verify` and no commit was made (chock `cc127e4`). Surface `agent-hooks`, the name the ledger's + readers (`sync_notes`, `derive.py`) key on for repo hooks. diff --git a/src/chock/data/witnesses.json b/src/chock/data/witnesses.json index 0571b974..a3b92207 100644 --- a/src/chock/data/witnesses.json +++ b/src/chock/data/witnesses.json @@ -75,5 +75,12 @@ "client": "Claude Code v2.1.317, Windows", "date": "2026-11-11", "method": "live probe: PreToolUse refused `git commit --no-verify` on the PowerShell tool and no commit was made (chock 295b38c)" + }, + { + "agent": "cursor", + "surface": "agent-hooks", + "client": "Cursor 3.30.14, Windows", + "date": "2026-11-14", + "method": "live probe: the repo's beforeShellExecution hook refused `git commit --no-verify` and no commit was made (chock cc127e4)" } ] diff --git a/tests/test_sync_every_adapter.py b/tests/test_sync_every_adapter.py index a40fb9a2..985efd29 100644 --- a/tests/test_sync_every_adapter.py +++ b/tests/test_sync_every_adapter.py @@ -157,14 +157,14 @@ def test_sync_says_agentseams_row_and_that_the_agent_is_untested(vendor: str) -> assert any("Untested" in line for line in lines) -@pytest.mark.parametrize("vendor", ("vscode_copilot", "claude_code")) +@pytest.mark.parametrize("vendor", ("vscode_copilot", "claude_code", "cursor")) def test_a_witnessed_agent_is_not_called_untested(vendor: str) -> None: assert evidence.witness(vendor, "agent-hooks"), "the ledger's repo-hook row this test rests on" assert sync_notes.witnessed(vendor) assert not any("Untested" in line for line in sync_notes.notes(vendor)) -@pytest.mark.parametrize("vendor", ("codex_cli", "cursor", "devin")) +@pytest.mark.parametrize("vendor", ("codex_cli", "devin")) def test_a_plugin_witness_does_not_make_sync_say_tested(vendor: str) -> None: """A plugin-hook row witnesses `chock install`; sync wires repo hooks nobody saw run there.""" assert evidence.witness(vendor, evidence.PLUGIN_HOOK) From ec13f287342638396d919c17c1864dc016532d38 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:31:48 +0000 Subject: [PATCH 34/57] docs: keep the README inside its word and line budgets (REL-D1) The new section took the README past 1,600 words and 300 lines. Shorten it and remove the cost-estimate sentence and six FAQ entries that repeat statements made elsewhere. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 32 +++-------------------------- changelog.d/rel-d1-docs-evidence.md | 3 +++ 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index e05a2aaa..b734ae00 100644 --- a/README.md +++ b/README.md @@ -104,13 +104,13 @@ git commit -m "read key from env" # passes alt="Terminal recording of the Quick start above, typed in real time, ending with the refused commit and the clean one.">

-With no `--ref`, `chock add` pins the catalog tag `v`, prints the commit it resolved to and the sha256 of the policy, and installs nothing when that tag does not exist. A tag can move: `--ref <40-hex commit>` pins a catalog commit by hand and refuses a commit that was never published, and `--verify-sha` refuses a policy whose hash differs. The allowlist pragma on the key line is for documented test fixtures only. It is honoured at commit, push and in CI, and deliberately not at tool-use, where the scanned text is a live tool argument. +With no `--ref`, `chock add` pins the catalog tag `v`, prints the commit and sha256, and installs nothing if that tag is missing. `--ref <40-hex commit>` and `--verify-sha ` pin by hand; `--ref` refuses a commit that was never published. The allowlist pragma on the key line is for documented test fixtures only. It is honoured at commit, push and in CI, and deliberately not at tool-use, where the scanned text is a live tool argument. ### Check it, or try it without blocking -`chock doctor` checks the hooks `chock sync` wrote for each client and prints PASS, WARN, FAIL or INFO with a fix; it exits 1 on a FAIL ([Doctor](docs/doctor.md)). `chock doctor --live` also runs chock's own pre-tool hook commands from those files, not the client: once on a command a policy refuses, which must be refused, and once on `echo chock-doctor-live`, which must pass. It skips plugin installs and counts, but never runs, a hook command that is not byte-equal to what this chock renders. +`chock doctor` prints PASS, WARN, FAIL or INFO with a fix for each client's hooks, and exits 1 on a FAIL ([Doctor](docs/doctor.md)). `chock doctor --live` also runs chock's own hook commands, not the client: once on a command a policy refuses, once on `echo chock-doctor-live`. It skips plugin installs and never runs a hook command chock did not render. -To record instead of refusing, add a top-level `rollout:` line to `.chock/config.yaml`. `observe` turns a refusal into a warning and records what `enforce` would have stopped in `.chock/log/gate-events.jsonl`; `ask` caps a block at ask; `enforce` is the default, and a missing, unknown or repeated value means `enforce`. Your own uncommitted edit counts for your own commit; an agent is held to the level committed at HEAD, and CI never takes a pull request below its base's level. `chock stop` ([Stop](docs/stop.md)) stays in force at every level. +A top-level `rollout:` line in `.chock/config.yaml` records instead of refusing: `observe` warns and logs what `enforce` would have stopped, `ask` caps a block at ask, and `enforce` (the default, also for a missing or unknown value) blocks. An agent is held to the level committed at HEAD. `chock stop` ([Stop](docs/stop.md)) stays in force at every level. ### Shift left, with no model and no tokens @@ -120,8 +120,6 @@ To record instead of refusing, add a top-level `rollout:` line to `.chock/config - Chock adds no new place your code goes: checks run where the agent writes, on the laptop, in the dev container or in the cloud agent's environment. The agent still sends context to its own model provider. Installing fetches policies once; enforcement makes no network calls. - Chock does not replace code review, your SAST suite or a penetration test. It refuses known classes while the agent writes, so they are fixed before review. -To estimate what a late finding costs you, fill in your own numbers: findings per release x share Chock covers x minutes to triage and fix x hourly rate, plus the days a security loop adds. - | | Agent defaults | With Chock | | :--- | :--- | :--- | | What it knows | Generic shell prompts | Your stack | @@ -224,34 +222,10 @@ OWASP mappings are partial and the engine is frozen at the commit above. Chock d ## FAQ for people and agents -### Does Chock use an LLM? - -No. Each check is a deterministic script (a parser plus rules) with no model call and no network access at enforcement time. - -### Does my code leave my machine? - -Chock adds no new place your code goes. Your agent still sends context to its own model provider. - ### Which agents does it work with? All 20 above get a git hook, a CI gate and an `AGENTS.md` rule; 15 also get a native hook. As one plugin: 8 tested, 3 untested. -### How do I install it? - -See [Install](#install): in your repo, or as one plugin from the builder. - -### What does it cost? - -Nothing: Apache-2.0, and a check costs no tokens. - -### How does it compare with other tools? - -See [docs/compare.md](docs/compare.md). - -### Does it replace SAST or code review? - -No. It refuses known classes while the agent writes, so they are fixed before review. - ### Which OWASP and CWE items does it cover? Partially. The OWASP Agentic mapping is above; Java rules name their CWE in each refusal. diff --git a/changelog.d/rel-d1-docs-evidence.md b/changelog.d/rel-d1-docs-evidence.md index afb98ecc..1150ca9b 100644 --- a/changelog.d/rel-d1-docs-evidence.md +++ b/changelog.d/rel-d1-docs-evidence.md @@ -10,3 +10,6 @@ - Witness ledger: a second row, Cursor 3.30.14 on Windows, 2026-11-14: the repo's `beforeShellExecution` hook refused `git commit --no-verify` and no commit was made (chock `cc127e4`). Surface `agent-hooks`, the name the ledger's readers (`sync_notes`, `derive.py`) key on for repo hooks. +- README: made room for the new section within the README budgets (1,600 words, 300 lines): the cost-estimate + sentence and the FAQ entries on LLM use, data leaving the machine, SAST, install, comparison and cost were removed; + each repeats a statement made elsewhere in the README or in `docs/`. From 6a39e584c450a4ebcae7a2f990b351e3927cab68 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:32:20 +0000 Subject: [PATCH 35/57] docs: keep the comparison link in the README (REL-D1) Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 2 +- changelog.d/rel-d1-docs-evidence.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b734ae00..eb1c79f1 100644 --- a/README.md +++ b/README.md @@ -218,7 +218,7 @@ OWASP Top 10 for Agentic Applications, from each manifest's `compliance.owasp_as Tiers: `commit` is a git hook or CI gate that exits non-zero. `in-agent` is the agent's pre-tool hook: best-effort, and it fails open. `advisory` is rule text the agent reads. No agent reaches `enforced` today. -OWASP mappings are partial and the engine is frozen at the commit above. Chock does not stop every attack: it closes common, known entry points before they ship. +OWASP mappings are partial and the engine is frozen at the commit above. Chock does not stop every attack: it closes common, known entry points before they ship. How it compares with other tools: [docs/compare.md](docs/compare.md). ## FAQ for people and agents diff --git a/changelog.d/rel-d1-docs-evidence.md b/changelog.d/rel-d1-docs-evidence.md index 1150ca9b..df3e4de6 100644 --- a/changelog.d/rel-d1-docs-evidence.md +++ b/changelog.d/rel-d1-docs-evidence.md @@ -12,4 +12,4 @@ readers (`sync_notes`, `derive.py`) key on for repo hooks. - README: made room for the new section within the README budgets (1,600 words, 300 lines): the cost-estimate sentence and the FAQ entries on LLM use, data leaving the machine, SAST, install, comparison and cost were removed; - each repeats a statement made elsewhere in the README or in `docs/`. + each is answered elsewhere in the README or in `docs/`; the comparison link moved to "Guardrails, not guarantees". From 67ca416149cfd4bcc33be99a95d836c04165dd57 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:36:51 +0000 Subject: [PATCH 36/57] docs: Codex repo-hook witness row; the README sentence joins the ledger plainly (REL-D1b) Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 2 +- changelog.d/rel-d1-docs-evidence.md | 3 +++ docs/assets/readme/derive.py | 29 +++++++++++++++++++++++---- docs/assets/readme/readme-blocks.json | 2 +- src/chock/data/witnesses.json | 7 +++++++ tests/test_readme_derived.py | 18 +++++++++++++++++ tests/test_sync_every_adapter.py | 4 ++-- 7 files changed, 57 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index eb1c79f1..dd66010c 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ Full nine-surface matrix and per-agent caveats: | **Kimi Code** | git hook + CI gate only — home-anchored config, outside chock's repo-scoped install | `~/.kimi-code/config.toml` | | **Replit** | git hook + CI gate only — no native hook API yet | — | -Chock tested its repo hooks live in Copilot CLI and VS Code agent mode; Claude Code v2.1.317, Windows; Cursor 3.30.14, Windows only; other native hooks follow agentseam's record, untested. +Chock saw its repo hooks refuse a command live in Copilot CLI and VS Code agent mode, Claude Code, Cursor and Codex CLI (on Windows; Codex after the project's hooks were trusted); other native hooks follow agentseam's record, untested. ## What it stops diff --git a/changelog.d/rel-d1-docs-evidence.md b/changelog.d/rel-d1-docs-evidence.md index df3e4de6..6c8237f3 100644 --- a/changelog.d/rel-d1-docs-evidence.md +++ b/changelog.d/rel-d1-docs-evidence.md @@ -13,3 +13,6 @@ - README: made room for the new section within the README budgets (1,600 words, 300 lines): the cost-estimate sentence and the FAQ entries on LLM use, data leaving the machine, SAST, install, comparison and cost were removed; each is answered elsewhere in the README or in `docs/`; the comparison link moved to "Guardrails, not guarantees". +- Witness ledger: a third row, Codex CLI 0.169.0 on Windows, 2026-11-14: the repo's `PreToolUse` hook refused + `git commit --no-verify` and no commit was made, after the project's hooks were trusted (chock `cc127e4`). + `derive.py` now joins the ledger's clients plainly, without versions, and carries the platform and the trust condition. diff --git a/docs/assets/readme/derive.py b/docs/assets/readme/derive.py index 06010bf1..dde72522 100644 --- a/docs/assets/readme/derive.py +++ b/docs/assets/readme/derive.py @@ -28,6 +28,9 @@ BLOCK = re.compile(r"()(?P.*?)()", re.DOTALL) UNTESTED_PREFIX = "Untested:" DEPRECATED = "deprecated" +TRUST_CONDITION = "after the project's hooks were trusted" +CLIENT_VERSION = re.compile(r"\s+v?\d[\d.]*") +CLIENT_PLATFORM = re.compile(r",\s*(?P[^,]+)$") ASI = [f"ASI{n:02d}" for n in range(1, 11)] INCIDENTS = [ ("Log4Shell (CVE-2021-44228)", "java-security"), @@ -128,6 +131,10 @@ def _either(names: list[str]) -> str: return names[0] if len(names) == 1 else ", ".join(names[:-1]) + " or " + names[-1] +def _either_and(names: list[str]) -> str: + return names[0] if len(names) == 1 else ", ".join(names[:-1]) + " and " + names[-1] + + def _table(head: tuple[str, ...], align: tuple[str, ...], rows: list[tuple[object, ...]]) -> str: lines = ["| " + " | ".join(head) + " |", "| " + " | ".join(align) + " |"] lines += ["| " + " | ".join(str(c) for c in row) + " |" for row in rows] @@ -148,14 +155,28 @@ def plugin_clients() -> tuple[list[str], list[str]]: return [n for cid, n in names if cid not in untested], [n for cid, n in names if cid in untested] -def repo_agents() -> tuple[int, int, list[str]]: - """(agents chock sync wires, those with a native hook, clients its repo hooks were witnessed in).""" +def witnessed_clients(rows: list) -> tuple[str, str]: + """(client names joined plainly, the platform and trust conditions) from the ledger rows; versions stay in the ledger.""" + names, platforms, notes = [], [], [] + for row in rows: + platform = CLIENT_PLATFORM.search(row.client) + name = CLIENT_VERSION.sub("", CLIENT_PLATFORM.sub("", row.client)).strip() + names.append(name) + if platform and platform["platform"] not in platforms: + platforms.append(platform["platform"]) + if TRUST_CONDITION in row.method: + notes.append(f"{name.split()[0]} {TRUST_CONDITION}") + return _either_and(names), "; ".join([f"on {' and '.join(platforms)}"] * bool(platforms) + notes) + + +def repo_agents() -> tuple[int, int, list]: + """(agents chock sync wires, those with a native hook, ledger rows of its repo hooks witnessed live).""" from chock import evidence # noqa: PLC0415 from chock.compile.surfaces import SURFACE_AGENTS, Surface # noqa: PLC0415 from chock.hooks import sync_notes # noqa: PLC0415 native = sum(1 for s in SURFACE_AGENTS.values() if {Surface.PRE_TOOL_USE, Surface.AGENT_HOOKS} & set(s)) - seen = [w.client for w in evidence.witnesses() if w.surface in sync_notes._HOOK_SURFACES] + seen = [w for w in evidence.witnesses() if w.surface in sync_notes._HOOK_SURFACES] return len(SURFACE_AGENTS), native, seen @@ -190,7 +211,7 @@ def readme_blocks(data: dict) -> dict[str, str]: + _table(("Class of flaw behind", "Policy", "Tier", "Eval cases"), (":---", ":---", ":---", "---:"), incidents) + "\n", "plugins": _fill(t["plugins"], TESTED=_either(tested), UNTESTED=_either(untested)), - "repo_agents": _fill(t["repo_agents"], WITNESSED="; ".join(seen)), + "repo_agents": _fill(t["repo_agents"], WITNESSED=witnessed_clients(seen)[0], NOTES=witnessed_clients(seen)[1]), "faq_agents": _fill(t["faq_agents"], ALL=agents, NATIVE=native, TESTED_N=len(tested), UNTESTED_N=len(untested)), "repos": f"\n{_fill(t['repos'], COUNT=len(repos))}\n\n" + _table(("Repository", "What it is"), (":---", ":---"), repos) diff --git a/docs/assets/readme/readme-blocks.json b/docs/assets/readme/readme-blocks.json index eeca89cd..4c588e53 100644 --- a/docs/assets/readme/readme-blocks.json +++ b/docs/assets/readme/readme-blocks.json @@ -2,7 +2,7 @@ "_": "Templates for derive.py readme: README.md text between and is rewritten from data.json and chock's own data. __TOKEN__ placeholders only.", "stamp": "From `registry.yaml` in [chock-catalog](https://github.com/open-coder-ai/chock-catalog) at `__SHA__`:", "plugins": "It builds one plugin for __TESTED__, tested live, and, untested by chock, for __UNTESTED__.", - "repo_agents": "Chock tested its repo hooks live in __WITNESSED__ only; other native hooks follow agentseam's record, untested.", + "repo_agents": "Chock saw its repo hooks refuse a command live in __WITNESSED__ (__NOTES__); other native hooks follow agentseam's record, untested.", "faq_agents": "All __ALL__ above get a git hook, a CI gate and an `AGENTS.md` rule; __NATIVE__ also get a native hook. As one plugin: __TESTED_N__ tested, __UNTESTED_N__ untested.", "repos": "The __COUNT__ public repositories:", "numbers": [ diff --git a/src/chock/data/witnesses.json b/src/chock/data/witnesses.json index a3b92207..f790bbad 100644 --- a/src/chock/data/witnesses.json +++ b/src/chock/data/witnesses.json @@ -82,5 +82,12 @@ "client": "Cursor 3.30.14, Windows", "date": "2026-11-14", "method": "live probe: the repo's beforeShellExecution hook refused `git commit --no-verify` and no commit was made (chock cc127e4)" + }, + { + "agent": "codex_cli", + "surface": "agent-hooks", + "client": "Codex CLI 0.169.0, Windows", + "date": "2026-11-14", + "method": "live probe: the repo's PreToolUse hook refused `git commit --no-verify` and no commit was made, after the project's hooks were trusted (chock cc127e4)" } ] diff --git a/tests/test_readme_derived.py b/tests/test_readme_derived.py index 7cdbcd0c..3ef77a1b 100644 --- a/tests/test_readme_derived.py +++ b/tests/test_readme_derived.py @@ -76,3 +76,21 @@ def entry(pid: str, status: str) -> dict: assert data["eval_cases"] == 3 * live assert data["eval_executed"] == 2 * live assert sum(data["tiers"].values()) == live + + +def test_witnessed_clients_read_plainly_without_versions() -> None: + from chock.evidence import Witness + + rows = [ + Witness("claude_code", "agent-hooks", "Claude Code v2.1.317, Windows", "2026-11-11", "live probe"), + Witness( + "codex_cli", + "agent-hooks", + "Codex CLI 0.169.0, Windows", + "2026-11-14", + "x, after the project's hooks were trusted (y)", + ), + ] + names, notes = _derive().witnessed_clients(rows) + assert names == "Claude Code and Codex CLI" + assert notes == "on Windows; Codex after the project's hooks were trusted" diff --git a/tests/test_sync_every_adapter.py b/tests/test_sync_every_adapter.py index 985efd29..18324e74 100644 --- a/tests/test_sync_every_adapter.py +++ b/tests/test_sync_every_adapter.py @@ -157,14 +157,14 @@ def test_sync_says_agentseams_row_and_that_the_agent_is_untested(vendor: str) -> assert any("Untested" in line for line in lines) -@pytest.mark.parametrize("vendor", ("vscode_copilot", "claude_code", "cursor")) +@pytest.mark.parametrize("vendor", ("vscode_copilot", "claude_code", "cursor", "codex_cli")) def test_a_witnessed_agent_is_not_called_untested(vendor: str) -> None: assert evidence.witness(vendor, "agent-hooks"), "the ledger's repo-hook row this test rests on" assert sync_notes.witnessed(vendor) assert not any("Untested" in line for line in sync_notes.notes(vendor)) -@pytest.mark.parametrize("vendor", ("codex_cli", "devin")) +@pytest.mark.parametrize("vendor", ("devin",)) def test_a_plugin_witness_does_not_make_sync_say_tested(vendor: str) -> None: """A plugin-hook row witnesses `chock install`; sync wires repo hooks nobody saw run there.""" assert evidence.witness(vendor, evidence.PLUGIN_HOOK) From 65a4c177453d51559630b30e3b7ff2c1d5b85a16 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:54:10 +0000 Subject: [PATCH 37/57] docs: shorten the derived repo-hook sentence to fit the README word budget (REL-D1b) Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 2 +- docs/assets/readme/derive.py | 2 +- docs/assets/readme/readme-blocks.json | 2 +- tests/test_readme_derived.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dd66010c..8c75fc94 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ Full nine-surface matrix and per-agent caveats: | **Kimi Code** | git hook + CI gate only — home-anchored config, outside chock's repo-scoped install | `~/.kimi-code/config.toml` | | **Replit** | git hook + CI gate only — no native hook API yet | — | -Chock saw its repo hooks refuse a command live in Copilot CLI and VS Code agent mode, Claude Code, Cursor and Codex CLI (on Windows; Codex after the project's hooks were trusted); other native hooks follow agentseam's record, untested. +Repo hooks refused commands live in Copilot CLI and VS Code agent mode, Claude Code, Cursor and Codex CLI (Windows; Codex after the project's hooks were trusted); other hooks follow agentseam's record, untested. ## What it stops diff --git a/docs/assets/readme/derive.py b/docs/assets/readme/derive.py index dde72522..4d53c392 100644 --- a/docs/assets/readme/derive.py +++ b/docs/assets/readme/derive.py @@ -166,7 +166,7 @@ def witnessed_clients(rows: list) -> tuple[str, str]: platforms.append(platform["platform"]) if TRUST_CONDITION in row.method: notes.append(f"{name.split()[0]} {TRUST_CONDITION}") - return _either_and(names), "; ".join([f"on {' and '.join(platforms)}"] * bool(platforms) + notes) + return _either_and(names), "; ".join([" and ".join(platforms)] * bool(platforms) + notes) def repo_agents() -> tuple[int, int, list]: diff --git a/docs/assets/readme/readme-blocks.json b/docs/assets/readme/readme-blocks.json index 4c588e53..b2096de1 100644 --- a/docs/assets/readme/readme-blocks.json +++ b/docs/assets/readme/readme-blocks.json @@ -2,7 +2,7 @@ "_": "Templates for derive.py readme: README.md text between and is rewritten from data.json and chock's own data. __TOKEN__ placeholders only.", "stamp": "From `registry.yaml` in [chock-catalog](https://github.com/open-coder-ai/chock-catalog) at `__SHA__`:", "plugins": "It builds one plugin for __TESTED__, tested live, and, untested by chock, for __UNTESTED__.", - "repo_agents": "Chock saw its repo hooks refuse a command live in __WITNESSED__ (__NOTES__); other native hooks follow agentseam's record, untested.", + "repo_agents": "Repo hooks refused commands live in __WITNESSED__ (__NOTES__); other hooks follow agentseam's record, untested.", "faq_agents": "All __ALL__ above get a git hook, a CI gate and an `AGENTS.md` rule; __NATIVE__ also get a native hook. As one plugin: __TESTED_N__ tested, __UNTESTED_N__ untested.", "repos": "The __COUNT__ public repositories:", "numbers": [ diff --git a/tests/test_readme_derived.py b/tests/test_readme_derived.py index 3ef77a1b..7aaa8926 100644 --- a/tests/test_readme_derived.py +++ b/tests/test_readme_derived.py @@ -93,4 +93,4 @@ def test_witnessed_clients_read_plainly_without_versions() -> None: ] names, notes = _derive().witnessed_clients(rows) assert names == "Claude Code and Codex CLI" - assert notes == "on Windows; Codex after the project's hooks were trusted" + assert notes == "Windows; Codex after the project's hooks were trusted" From 323ceb52265abfb8aa6dc27d3198f44da7cb5900 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 02:19:31 +0000 Subject: [PATCH 38/57] docs: group the derived repo-hook clients by the platform the ledger records (REL-D1b) Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 6 +++--- docs/assets/readme/derive.py | 20 ++++++++++++-------- docs/assets/readme/readme-blocks.json | 2 +- tests/test_readme_derived.py | 22 +++++++++++----------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 8c75fc94..fe066f25 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ Every agent in this table gets the same floor: a git hook, a CI gate and an ambi rule, which is why those three columns aren't repeated below. What varies is whether the agent also exposes a native hook chock can wire directly into its own tool-call loop, and where the file that wiring lives. A row here is support, not installation — coverage is only credited on -a given repo once `chock sync` has actually written the file, which is why the CLI's own +a given repo once `chock sync` has written the file, which is why the CLI's own `chock status` always beats this table for what's true *here*. Four of the nine surfaces are absent from this page entirely because they credit no agent today — `managed-setting` is not installed by chock, `gateway` is modelled but not yet emitted, `mcp-gateway` credits @@ -167,7 +167,7 @@ Full nine-surface matrix and per-agent caveats: | **Kimi Code** | git hook + CI gate only — home-anchored config, outside chock's repo-scoped install | `~/.kimi-code/config.toml` | | **Replit** | git hook + CI gate only — no native hook API yet | — | -Repo hooks refused commands live in Copilot CLI and VS Code agent mode, Claude Code, Cursor and Codex CLI (Windows; Codex after the project's hooks were trusted); other hooks follow agentseam's record, untested. +Repo hooks refused commands live in Copilot CLI and VS Code agent mode and, on Windows, in Claude Code, Cursor and Codex CLI (Codex after the project's hooks were trusted); others follow agentseam's record, untested. ## What it stops @@ -267,7 +267,7 @@ The 15 public repositories:

Three numbered contribution cards, each with a one-line how-to.

-A policy manifest for the guardrail your stack needs is the contribution we want most; send it to the [catalog](https://github.com/open-coder-ai/chock-catalog). An evidence report on what your agent does, or a `policy wanted` entry in the [threat ledger](https://github.com/open-coder-ai/chock-threat-intel/blob/main/reference/agentic-threat-ledger.md), also counts; see [docs/ecosystem.md](docs/ecosystem.md). Run `pytest -q && ruff check . && ruff format --check . && chock check` before a pull request; [`good first issue`](https://github.com/open-coder-ai/chock/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) is where to start. Questions: [Discussions](https://github.com/open-coder-ai/chock/discussions). +A policy manifest for the guardrail your stack needs is the contribution we want most; send it to the [catalog](https://github.com/open-coder-ai/chock-catalog). An evidence report on what your agent does, or a `policy wanted` entry in the [threat ledger](https://github.com/open-coder-ai/chock-threat-intel/blob/main/reference/agentic-threat-ledger.md), counts; see [docs/ecosystem.md](docs/ecosystem.md). Run `pytest -q && ruff check . && ruff format --check . && chock check` before a pull request; [`good first issue`](https://github.com/open-coder-ai/chock/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) is where to start. Questions: [Discussions](https://github.com/open-coder-ai/chock/discussions). Chock policies are committed content, so your rules travel with every clone and fork, and the CI gate (`chock sync --ci`) depends on nothing the contributor does. [The full case.](docs/why.md) diff --git a/docs/assets/readme/derive.py b/docs/assets/readme/derive.py index 4d53c392..ff4414a6 100644 --- a/docs/assets/readme/derive.py +++ b/docs/assets/readme/derive.py @@ -155,18 +155,22 @@ def plugin_clients() -> tuple[list[str], list[str]]: return [n for cid, n in names if cid not in untested], [n for cid, n in names if cid in untested] -def witnessed_clients(rows: list) -> tuple[str, str]: - """(client names joined plainly, the platform and trust conditions) from the ledger rows; versions stay in the ledger.""" - names, platforms, notes = [], [], [] +def witnessed_clients(rows: list) -> str: + """Client names from the ledger rows, grouped by the platform each records; versions stay in the ledger.""" + groups: dict[str, list[str]] = {} + notes = [] for row in rows: platform = CLIENT_PLATFORM.search(row.client) name = CLIENT_VERSION.sub("", CLIENT_PLATFORM.sub("", row.client)).strip() - names.append(name) - if platform and platform["platform"] not in platforms: - platforms.append(platform["platform"]) + groups.setdefault(platform["platform"] if platform else "", []).append(name) if TRUST_CONDITION in row.method: notes.append(f"{name.split()[0]} {TRUST_CONDITION}") - return _either_and(names), "; ".join([" and ".join(platforms)] * bool(platforms) + notes) + parts = [ + _either_and(names) if not platform else f"on {platform}, in {_either_and(names)}" + for platform, names in sorted(groups.items(), key=lambda g: g[0] != "") + ] + text = parts[0] if len(parts) == 1 else ", ".join(parts[:-1]) + " and, " + parts[-1] + return text + (f" ({'; '.join(notes)})" if notes else "") def repo_agents() -> tuple[int, int, list]: @@ -211,7 +215,7 @@ def readme_blocks(data: dict) -> dict[str, str]: + _table(("Class of flaw behind", "Policy", "Tier", "Eval cases"), (":---", ":---", ":---", "---:"), incidents) + "\n", "plugins": _fill(t["plugins"], TESTED=_either(tested), UNTESTED=_either(untested)), - "repo_agents": _fill(t["repo_agents"], WITNESSED=witnessed_clients(seen)[0], NOTES=witnessed_clients(seen)[1]), + "repo_agents": _fill(t["repo_agents"], WITNESSED=witnessed_clients(seen)), "faq_agents": _fill(t["faq_agents"], ALL=agents, NATIVE=native, TESTED_N=len(tested), UNTESTED_N=len(untested)), "repos": f"\n{_fill(t['repos'], COUNT=len(repos))}\n\n" + _table(("Repository", "What it is"), (":---", ":---"), repos) diff --git a/docs/assets/readme/readme-blocks.json b/docs/assets/readme/readme-blocks.json index b2096de1..3999142e 100644 --- a/docs/assets/readme/readme-blocks.json +++ b/docs/assets/readme/readme-blocks.json @@ -2,7 +2,7 @@ "_": "Templates for derive.py readme: README.md text between and is rewritten from data.json and chock's own data. __TOKEN__ placeholders only.", "stamp": "From `registry.yaml` in [chock-catalog](https://github.com/open-coder-ai/chock-catalog) at `__SHA__`:", "plugins": "It builds one plugin for __TESTED__, tested live, and, untested by chock, for __UNTESTED__.", - "repo_agents": "Repo hooks refused commands live in __WITNESSED__ (__NOTES__); other hooks follow agentseam's record, untested.", + "repo_agents": "Repo hooks refused commands live in __WITNESSED__; others follow agentseam's record, untested.", "faq_agents": "All __ALL__ above get a git hook, a CI gate and an `AGENTS.md` rule; __NATIVE__ also get a native hook. As one plugin: __TESTED_N__ tested, __UNTESTED_N__ untested.", "repos": "The __COUNT__ public repositories:", "numbers": [ diff --git a/tests/test_readme_derived.py b/tests/test_readme_derived.py index 7aaa8926..f3a78e51 100644 --- a/tests/test_readme_derived.py +++ b/tests/test_readme_derived.py @@ -78,19 +78,19 @@ def entry(pid: str, status: str) -> dict: assert sum(data["tiers"].values()) == live -def test_witnessed_clients_read_plainly_without_versions() -> None: +def test_witnessed_clients_group_by_the_platform_the_ledger_records() -> None: from chock.evidence import Witness + trusted = "x, after the project's hooks were trusted (y)" rows = [ + Witness("vscode_copilot", "agent-hooks", "Copilot CLI and VS Code agent mode", "2026-08-23", "live probe"), Witness("claude_code", "agent-hooks", "Claude Code v2.1.317, Windows", "2026-11-11", "live probe"), - Witness( - "codex_cli", - "agent-hooks", - "Codex CLI 0.169.0, Windows", - "2026-11-14", - "x, after the project's hooks were trusted (y)", - ), + Witness("codex_cli", "agent-hooks", "Codex CLI 0.169.0, Windows", "2026-11-14", trusted), ] - names, notes = _derive().witnessed_clients(rows) - assert names == "Claude Code and Codex CLI" - assert notes == "Windows; Codex after the project's hooks were trusted" + text = _derive().witnessed_clients(rows) + assert text == ( + "Copilot CLI and VS Code agent mode and, on Windows, in Claude Code and Codex CLI " + "(Codex after the project's hooks were trusted)" + ) + assert text.index("Windows") > text.index("Copilot CLI and VS Code agent mode") + assert _derive().witnessed_clients(rows[1:2]) == "on Windows, in Claude Code" From cef2f2f9a0d72af2ffe65f4cc0843e518f890c94 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 02:44:45 +0000 Subject: [PATCH 39/57] docs: restore the six FAQ entries and shrink the new README section (REL-D1c) Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 38 ++++++++++++++++++++++------- changelog.d/rel-d1-docs-evidence.md | 9 ++++--- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index fe066f25..60d84ff7 100644 --- a/README.md +++ b/README.md @@ -104,13 +104,11 @@ git commit -m "read key from env" # passes alt="Terminal recording of the Quick start above, typed in real time, ending with the refused commit and the clean one.">

-With no `--ref`, `chock add` pins the catalog tag `v`, prints the commit and sha256, and installs nothing if that tag is missing. `--ref <40-hex commit>` and `--verify-sha ` pin by hand; `--ref` refuses a commit that was never published. The allowlist pragma on the key line is for documented test fixtures only. It is honoured at commit, push and in CI, and deliberately not at tool-use, where the scanned text is a live tool argument. +With no `--ref`, `chock add` pins the catalog tag `v`, prints the commit and sha256, and fails if the tag is missing. `--ref` and `--verify-sha` pin by hand; `--ref` refuses a commit never published. The allowlist pragma on the key line is for documented test fixtures only. It is honoured at commit, push and in CI, and deliberately not at tool-use, where the scanned text is a live tool argument. ### Check it, or try it without blocking -`chock doctor` prints PASS, WARN, FAIL or INFO with a fix for each client's hooks, and exits 1 on a FAIL ([Doctor](docs/doctor.md)). `chock doctor --live` also runs chock's own hook commands, not the client: once on a command a policy refuses, once on `echo chock-doctor-live`. It skips plugin installs and never runs a hook command chock did not render. - -A top-level `rollout:` line in `.chock/config.yaml` records instead of refusing: `observe` warns and logs what `enforce` would have stopped, `ask` caps a block at ask, and `enforce` (the default, also for a missing or unknown value) blocks. An agent is held to the level committed at HEAD. `chock stop` ([Stop](docs/stop.md)) stays in force at every level. +`chock doctor` and `--live` check your hooks ([Doctor](docs/doctor.md)). `rollout: observe` in `.chock/config.yaml` logs refusals instead of blocking; `chock stop` still holds. ### Shift left, with no model and no tokens @@ -118,7 +116,7 @@ A top-level `rollout:` line in `.chock/config.yaml` records instead of refusing: - With Chock: the check runs as the agent writes, the refusal names the fix, and the agent corrects it in the same turn. - A check costs no tokens. A passing check adds nothing to the agent's context; a refusal adds one short reason. - Chock adds no new place your code goes: checks run where the agent writes, on the laptop, in the dev container or in the cloud agent's environment. The agent still sends context to its own model provider. Installing fetches policies once; enforcement makes no network calls. -- Chock does not replace code review, your SAST suite or a penetration test. It refuses known classes while the agent writes, so they are fixed before review. +- Chock does not replace code review, your SAST suite or a penetration test. | | Agent defaults | With Chock | | :--- | :--- | :--- | @@ -131,9 +129,7 @@ A top-level `rollout:` line in `.chock/config.yaml` records instead of refusing: ### Supported agents -Every agent in this table gets the same floor: a git hook, a CI gate and an ambient `AGENTS.md` -rule, which is why those three columns aren't repeated below. What varies is whether the agent -also exposes a native hook chock can wire directly into its own tool-call loop, and where the +Every agent in this table gets the same floor: a git hook, a CI gate and an ambient `AGENTS.md` rule, which is why those three columns aren't repeated below. What varies is whether the agent also exposes a native hook chock can wire directly into its own tool-call loop, and where the file that wiring lives. A row here is support, not installation — coverage is only credited on a given repo once `chock sync` has written the file, which is why the CLI's own `chock status` always beats this table for what's true *here*. Four of the nine surfaces are @@ -218,14 +214,38 @@ OWASP Top 10 for Agentic Applications, from each manifest's `compliance.owasp_as Tiers: `commit` is a git hook or CI gate that exits non-zero. `in-agent` is the agent's pre-tool hook: best-effort, and it fails open. `advisory` is rule text the agent reads. No agent reaches `enforced` today. -OWASP mappings are partial and the engine is frozen at the commit above. Chock does not stop every attack: it closes common, known entry points before they ship. How it compares with other tools: [docs/compare.md](docs/compare.md). +OWASP mappings are partial and the engine is frozen at the commit above. Chock does not stop every attack: it closes common, known entry points before they ship. ## FAQ for people and agents +### Does Chock use an LLM? + +No. Each check is a deterministic script (a parser plus rules) with no model call and no network access at enforcement time. + +### Does my code leave my machine? + +Chock adds no new place your code goes. Your agent still sends context to its own model provider. + ### Which agents does it work with? All 20 above get a git hook, a CI gate and an `AGENTS.md` rule; 15 also get a native hook. As one plugin: 8 tested, 3 untested. +### How do I install it? + +See [Install](#install): in your repo, or as one plugin from the builder. + +### What does it cost? + +Nothing: Apache-2.0, and a check costs no tokens. + +### How does it compare with other tools? + +See [docs/compare.md](docs/compare.md). + +### Does it replace SAST or code review? + +No. It refuses known classes while the agent writes, so they are fixed before review. + ### Which OWASP and CWE items does it cover? Partially. The OWASP Agentic mapping is above; Java rules name their CWE in each refusal. diff --git a/changelog.d/rel-d1-docs-evidence.md b/changelog.d/rel-d1-docs-evidence.md index 6c8237f3..2c7ebd81 100644 --- a/changelog.d/rel-d1-docs-evidence.md +++ b/changelog.d/rel-d1-docs-evidence.md @@ -10,9 +10,10 @@ - Witness ledger: a second row, Cursor 3.30.14 on Windows, 2026-11-14: the repo's `beforeShellExecution` hook refused `git commit --no-verify` and no commit was made (chock `cc127e4`). Surface `agent-hooks`, the name the ledger's readers (`sync_notes`, `derive.py`) key on for repo hooks. -- README: made room for the new section within the README budgets (1,600 words, 300 lines): the cost-estimate - sentence and the FAQ entries on LLM use, data leaving the machine, SAST, install, comparison and cost were removed; - each is answered elsewhere in the README or in `docs/`; the comparison link moved to "Guardrails, not guarantees". +- README: the new section is kept to a short paragraph; the FAQ keeps its six entries. The "estimate what a late + finding costs" paragraph and a sentence repeating the comparison link were removed to stay within the README + budgets (1,600 words, 300 lines). - Witness ledger: a third row, Codex CLI 0.169.0 on Windows, 2026-11-14: the repo's `PreToolUse` hook refused `git commit --no-verify` and no commit was made, after the project's hooks were trusted (chock `cc127e4`). - `derive.py` now joins the ledger's clients plainly, without versions, and carries the platform and the trust condition. + `derive.py` now groups the ledger's clients by the platform each records, without versions, and carries the trust + condition. From 2bd9893a1af6440b276d0f535dad4fd562a95528 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:17:48 +0000 Subject: [PATCH 40/57] gate: follow a git-reported rename for the baseline (E-1) A file git reports as renamed (-M) is judged against its old path's content, for script and content_regex gates, at commit, at the turn's end and on the CI range. Copies stay judged as new. The floor context keeps --no-renames and its own readers. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/e1-rename-baseline.md | 4 + src/chock/gate/runner/context.py | 38 ++++++++- tests/test_gate_rename_baseline.py | 129 +++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 changelog.d/e1-rename-baseline.md create mode 100644 tests/test_gate_rename_baseline.py diff --git a/changelog.d/e1-rename-baseline.md b/changelog.d/e1-rename-baseline.md new file mode 100644 index 00000000..37a67bb0 --- /dev/null +++ b/changelog.d/e1-rename-baseline.md @@ -0,0 +1,4 @@ +- A gate judges a file that git reports as renamed (`-M`) against its old path's content, for a script gate and a + `content_regex` gate, at commit, at the turn's end and on the CI range. A pure `git mv` of a file holding an old + finding is no longer refused as new work; a new finding added in the move is, and a copy (old path kept) is judged + as new, as before. diff --git a/src/chock/gate/runner/context.py b/src/chock/gate/runner/context.py index 46b00539..23fadbf5 100644 --- a/src/chock/gate/runner/context.py +++ b/src/chock/gate/runner/context.py @@ -67,6 +67,7 @@ def __init__( self.scope = tuple(scope or ()) #: Path prefixes this gate never judges: its own policy's source and compiled folders. self.own = tuple(own) + self._rename_map: dict[str, str] | None = None def is_own(self, path: str) -> bool: """Whether `path` is in this policy's own source or compiled folder.""" @@ -133,9 +134,34 @@ def staged_paths(self, diff_filter: str = "ACMRT", *, judge_own: bool = False) - paths = self._listing("diff", *self._range(), "--name-only", f"--diff-filter={diff_filter}") return [path for path in paths if self.in_scope(path, judge_own=judge_own)] + def _rename_range(self) -> list[str] | None: + """The git-diff scope whose renames the baseline follows; None when this context has none.""" + return self._range() + + def _renames(self) -> dict[str, str]: + """{new path: old path} for what git itself reports as renamed (`-M`, default similarity); never a guess. + + A listing that fails or does not parse says no rename, which leaves the baseline at the path (stricter). + """ + if self._rename_map is None: + scope = self._rename_range() + found: dict[str, str] = {} + if scope is not None: + try: + tokens = self._listing("diff", *scope, "-M", "--relative", "--name-status", "--diff-filter=R") + except ListingError: + tokens = [] + for index in range(0, len(tokens) - 2, 3): + if tokens[index].startswith("R"): + found[tokens[index + 2]] = tokens[index + 1] + self._rename_map = found + return self._rename_map + def _diff_text(self, path: str) -> str: """`path`'s `-U0` diff as git writes it, unconverted; raises DiffError when git fails.""" - args = ("diff", *self._range(), "-U0", "--no-color", "--no-ext-diff", "--no-textconv", "--", path) + old = self._renames().get(path) + paths = (path, old) if old else (path,) + args = ("diff", *self._range(), "-M", "-U0", "--no-color", "--no-ext-diff", "--no-textconv", "--", *paths) try: proc = subprocess.run( # noqa: S603 -- reading repo facts via git is this class's whole job [_GIT, "--literal-pathspecs", *args], @@ -164,8 +190,8 @@ def staged_blob(self, path: str) -> str: return self._git("show", f"HEAD:{path}" if self.base else f":{path}") def head_blob(self, path: str) -> str: - """Content before the change, or "" when the path is new in it.""" - return self._git("show", f"{self.base or 'HEAD'}:{path}") + """Content before the change, or "" when the path is new in it; a renamed path reads its old path.""" + return self._git("show", f"{self.base or 'HEAD'}:{self._renames().get(path, path)}") def committed_blob(self, path: str) -> str: """The file as HEAD has it, or "" when HEAD does not.""" @@ -234,6 +260,10 @@ def __init__( self._added = dict(added or {}) self._stop = stop + def _rename_range(self) -> list[str] | None: + """At the turn's end, HEAD against the index and tree; a write before the tool runs has no rename.""" + return ["HEAD"] if self._stop else None + def staged_paths(self, diff_filter: str = "ACMRT", *, judge_own: bool = False) -> list[str]: """Every write is a present file, so a filter asking only for deletions finds none.""" if not set(diff_filter) & set("ACMRT"): @@ -265,7 +295,7 @@ def committed_blob(self, path: str) -> str: def head_blob(self, path: str) -> str: """The baseline: HEAD at the turn's end, else what is on disk now, which this write would replace.""" if self._stop: - return self.committed_blob(path) + return self.committed_blob(self._renames().get(path, path)) try: return (self.repo_root / path).read_text(encoding="utf-8") except (OSError, UnicodeDecodeError): diff --git a/tests/test_gate_rename_baseline.py b/tests/test_gate_rename_baseline.py new file mode 100644 index 00000000..e879c8a9 --- /dev/null +++ b/tests/test_gate_rename_baseline.py @@ -0,0 +1,129 @@ +"""A file git reports as renamed is judged against its old path's content, on a real git repository. + +A pure `git mv` of a file holding an old finding adds nothing; a copy (old path kept) is new work. +Each case runs for a script gate and a `content_regex` gate, at commit, at the turn's end and on the CI range. +""" + +from __future__ import annotations + +import subprocess +from collections.abc import Callable +from pathlib import Path + +import pytest +from conftest import write_gate +from findings_support import make_gate, repo_with + +from chock.gate.runner import run + +PAD = "a = 1\nb = 2\nc = 3\nd = 4\n" +OLD = PAD + "x = BAD\n" +BASE = "trunk" +REGEX = {"kind": "content_regex", "on": ["commit", "tool_use"], "action": "block", "message": "m"} +REGEX["params"] = {"scan": "added_lines", "content_pattern": "BAD"} + +Gate = Callable[[Path, Path], Path] +Surface = Callable[[Path, Path, dict[str, str]], int] + + +def _git(repo: Path, *args: str) -> None: + subprocess.run(["git", *args], cwd=repo, check=True, capture_output=True) + + +def _script_gate(repo: Path, _tmp: Path) -> Path: + return make_gate(repo) + + +def _regex_gate(_repo: Path, tmp: Path) -> Path: + return write_gate(tmp, REGEX) + + +def _put(repo: Path, files: dict[str, str]) -> None: + for rel, text in files.items(): + (repo / rel).write_text(text, encoding="utf-8") + + +def _commit_surface(gate: Path, repo: Path, files: dict[str, str]) -> int: + _put(repo, files) + _git(repo, "add", "-A", "--", *files) + return run(gate, "commit", None, repo) + + +def _stop_surface(gate: Path, repo: Path, files: dict[str, str]) -> int: + _put(repo, files) + return run(gate, "stop", None, repo, writes=files) + + +def _ci_surface(gate: Path, repo: Path, files: dict[str, str]) -> int: + _git(repo, "checkout", "-q", "-b", "feature") + _put(repo, files) + _git(repo, "add", "-A", "--", *files) + _git(repo, "commit", "-qm", "move") + return run(gate, "ci", None, repo, base=BASE) + + +def _start(tmp_path: Path) -> Path: + repo = repo_with(tmp_path, **{"old.py": OLD, "keep.py": "k = 1\n"}) + _git(repo, "branch", "-M", BASE) + return repo + + +def _move(repo: Path, text: str | None, *, copy: bool = False) -> dict[str, str]: + """Move (or copy) old.py to new.py, returning the new file's text for the surface to judge.""" + if copy: + (repo / "new.py").write_text(OLD, encoding="utf-8") + _git(repo, "add", "new.py") + else: + _git(repo, "mv", "old.py", "new.py") + return {"new.py": OLD if text is None else text} + + +GATES = [pytest.param(_script_gate, id="script"), pytest.param(_regex_gate, id="content_regex")] +SURFACES = [ + pytest.param(_commit_surface, id="commit"), + pytest.param(_stop_surface, id="stop"), + pytest.param(_ci_surface, id="ci"), +] + + +@pytest.mark.parametrize("surface", SURFACES) +@pytest.mark.parametrize("make", GATES) +def test_a_pure_rename_of_an_old_finding_is_allowed(tmp_path: Path, make: Gate, surface: Surface) -> None: + repo = _start(tmp_path) + gate = make(repo, tmp_path) + assert surface(gate, repo, _move(repo, None)) == 0 + + +@pytest.mark.parametrize("surface", SURFACES) +@pytest.mark.parametrize("make", GATES) +def test_a_rename_that_adds_a_finding_refuses_the_new_one(tmp_path: Path, make: Gate, surface: Surface) -> None: + repo = _start(tmp_path) + gate = make(repo, tmp_path) + assert surface(gate, repo, _move(repo, OLD + "y = BAD two\n")) == 1 + + +@pytest.mark.parametrize("surface", SURFACES) +@pytest.mark.parametrize("make", GATES) +def test_a_rename_that_removes_a_finding_is_allowed(tmp_path: Path, make: Gate, surface: Surface) -> None: + repo = _start(tmp_path) + gate = make(repo, tmp_path) + assert surface(gate, repo, _move(repo, PAD + "x = 1\n")) == 0 + + +@pytest.mark.parametrize("surface", SURFACES) +@pytest.mark.parametrize("make", GATES) +def test_a_copy_with_the_old_path_kept_is_judged_as_new(tmp_path: Path, make: Gate, surface: Surface) -> None: + repo = _start(tmp_path) + gate = make(repo, tmp_path) + assert surface(gate, repo, _move(repo, None, copy=True)) == 1 + + +@pytest.mark.parametrize("surface", SURFACES) +def test_the_script_gate_names_only_the_added_finding_after_a_rename( + tmp_path: Path, surface: Surface, capsys: pytest.CaptureFixture +) -> None: + repo = _start(tmp_path) + assert surface(make_gate(repo), repo, _move(repo, OLD + "y = BAD two\n")) == 1 + err = capsys.readouterr().err + assert "new.py:6: bad y = BAD two" in err + assert "x = BAD" not in err From f9abf6f8ada3b7d977d8f87d26f5ba9e431c3a6e Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:38:31 +0000 Subject: [PATCH 41/57] gate: keep context.py inside the 300-line review budget (E-1) Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- src/chock/gate/runner/context.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/chock/gate/runner/context.py b/src/chock/gate/runner/context.py index 23fadbf5..9587b05b 100644 --- a/src/chock/gate/runner/context.py +++ b/src/chock/gate/runner/context.py @@ -2,6 +2,7 @@ from __future__ import annotations +import contextlib import difflib import fnmatch import shutil @@ -144,17 +145,11 @@ def _renames(self) -> dict[str, str]: A listing that fails or does not parse says no rename, which leaves the baseline at the path (stricter). """ if self._rename_map is None: - scope = self._rename_range() - found: dict[str, str] = {} + scope, tokens = self._rename_range(), [] if scope is not None: - try: + with contextlib.suppress(ListingError): tokens = self._listing("diff", *scope, "-M", "--relative", "--name-status", "--diff-filter=R") - except ListingError: - tokens = [] - for index in range(0, len(tokens) - 2, 3): - if tokens[index].startswith("R"): - found[tokens[index + 2]] = tokens[index + 1] - self._rename_map = found + self._rename_map = {tokens[i + 2]: tokens[i + 1] for i in range(0, len(tokens) - 2, 3)} return self._rename_map def _diff_text(self, path: str) -> str: From bd4c1cd3469499c3d0b380f16c0d978bde7c5259 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:50:10 +0000 Subject: [PATCH 42/57] gate: a script gate's verdict comes from the findings the change adds (E-2) A finding may carry a tier (block, ask, warn). When every finding the change adds carries a known tier, the verdict is the highest among them; otherwise the exit code decides, as before. An old block-tier finding in a touched file no longer turns a new ask-tier finding into a block. spec/gate-dsl.md documents the field; tests/test_script_gate_tiers.py covers commit and agent write. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- changelog.d/e2-verdict-from-new.md | 4 + spec/gate-dsl.md | 15 ++-- src/chock/gate/runner/script.py | 19 ++++- tests/test_script_gate_tiers.py | 127 +++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 changelog.d/e2-verdict-from-new.md create mode 100644 tests/test_script_gate_tiers.py diff --git a/changelog.d/e2-verdict-from-new.md b/changelog.d/e2-verdict-from-new.md new file mode 100644 index 00000000..ec007197 --- /dev/null +++ b/changelog.d/e2-verdict-from-new.md @@ -0,0 +1,4 @@ +- A script gate's finding may carry a `tier` (`block`, `ask` or `warn`). When every finding the change adds carries + one, the verdict is the highest tier among them, so an old block-tier finding in a touched file no longer turns a + new ask-tier finding into a block. A script that reports no tier, or tiers only some findings, keeps its exit code + as the verdict, as before. See `spec/gate-dsl.md`. diff --git a/spec/gate-dsl.md b/spec/gate-dsl.md index c4c26106..9fda853c 100644 --- a/spec/gate-dsl.md +++ b/spec/gate-dsl.md @@ -258,7 +258,9 @@ introduces: are for display. An optional `"new": true` makes the finding count as new whatever the baseline holds, for a script that already compared with HEAD itself. An optional `rule` (string, `[A-Za-z0-9][A-Za-z0-9 ._:/-]{0,63}`) names the rule for the gate log; a missing or malformed - `rule` is ignored and never invalidates the document. + `rule` is ignored and never invalidates the document. An optional `tier` (`"block"`, `"ask"` or + `"warn"`) is the verdict this one finding warrants (see Verdict); any other value counts as no tier + and never invalidates the document. - A document is valid only when `findings` is a list of objects each carrying string `key`, `path` and `message`, an integer `line`, and a boolean `new` if present. Anything else on stdout is not a document, and the script keeps the exit-code contract above exactly (one run). @@ -271,10 +273,13 @@ introduces: - **New findings.** Per `path`, each baseline finding cancels one change-run finding with the same `key`, so a finding is new when its key occurs more often in the change-run than in the baseline; a second copy of an old violation is new. -- **Verdict.** No new findings allows, whatever the change-run's exit code. Otherwise the - change-run's exit code decides (`1` blocks, `3` asks, `4` warns, capped by the declared - `action`; exit `0` allows; any other code is undecided as above), and the reason lists only the - new findings as `path:line: message`, not the script's stderr. +- **Verdict.** No new findings allows, whatever the change-run's exit code. Otherwise, when every + new finding carries a `tier`, the verdict is the highest among them (`block` over `ask` over + `warn`), so an old block-tier finding in a touched file cannot turn a new ask-tier one into a + block; when any new finding lacks one, the change-run's exit code decides (`1` blocks, `3` asks, + `4` warns; any other code is undecided as above). Either way the verdict is capped by the declared + `action`, exit `0` allows, and the reason lists only the new findings as `path:line: message`, + not the script's stderr. A script that sets no `tier` behaves exactly as before. - **Baseline text** is the file before the change: `commit`, HEAD; `push` and `ci`, the base of the range; `tool_use` before the write (PreToolUse), the file on disk, and at the turn's end (Stop), HEAD. A write outside the repository has the file on disk before the write, or no diff --git a/src/chock/gate/runner/script.py b/src/chock/gate/runner/script.py index e517444b..9f539c4b 100644 --- a/src/chock/gate/runner/script.py +++ b/src/chock/gate/runner/script.py @@ -109,6 +109,19 @@ def _new_findings(found: list[dict], baseline: list[dict]) -> list[dict]: return fresh +#: A finding's optional `tier`, ranked; a verdict is the highest among the findings the change adds. +_TIER_RANK = {"warn": 1, "ask": 2, "block": 3} + + +def _tier_verdict(fresh: list[dict]) -> str | None: + """The verdict word ("" blocks) when every kept finding carries a known tier, else None: the exit code decides.""" + tiers = [item.get("tier") for item in fresh] + if not all(isinstance(tier, str) and tier in _TIER_RANK for tier in tiers): + return None + top = max(tiers, key=_TIER_RANK.__getitem__) + return "" if top == "block" else top + + def _baseline_findings(ctx: GateContext, script: Path, material: dict, started: float) -> list[dict]: """What the script finds in the baseline text of the change's files; empty when it cannot say. @@ -147,7 +160,7 @@ def _exit_verdict(script: Path, proc: subprocess.CompletedProcess[str]) -> GateR def _judge_findings( ctx: GateContext, script: Path, material: dict, proc: subprocess.CompletedProcess[str], started: float ) -> GateResult: - """Judge only the findings the change introduces; the change-run's exit code is the verdict for them.""" + """Judge only the findings the change introduces: the highest tier among them, else the change-run's exit code.""" if proc.returncode not in (_SCRIPT_ALLOW, _SCRIPT_BLOCK, _SCRIPT_ASK, _SCRIPT_WARN): # A document followed by a crash or an unknown exit is undecided, never a clean pass. return _exit_verdict(script, proc) @@ -160,7 +173,9 @@ def _judge_findings( if not fresh or proc.returncode == _SCRIPT_ALLOW: return GateResult(allowed=True, detail=counts) lines = [f"{item['path']}:{item['line']}: {item['message']}" for item in fresh] - verdict = {_SCRIPT_ASK: "ask", _SCRIPT_WARN: "warn"}.get(proc.returncode, "") + verdict = _tier_verdict(fresh) + if verdict is None: + verdict = {_SCRIPT_ASK: "ask", _SCRIPT_WARN: "warn"}.get(proc.returncode, "") return GateResult( allowed=False, matches=lines, verdict=verdict, detail=counts, rules=_rule_ids(fresh), findings=fresh ) diff --git a/tests/test_script_gate_tiers.py b/tests/test_script_gate_tiers.py new file mode 100644 index 00000000..f3018244 --- /dev/null +++ b/tests/test_script_gate_tiers.py @@ -0,0 +1,127 @@ +"""A script gate whose findings carry a tier takes its verdict from the findings the change adds. + +The toy script computes its exit code over every finding in the files, old ones included, as real +scripts do; the tier on each finding is what lets the engine ignore the old ones. +""" + +from __future__ import annotations + +import json +import subprocess +import textwrap +from pathlib import Path + +import pytest +from findings_support import repo_with + +from chock.gate.runner.verdict import judge + +#: BLOCKME lines are block-tier findings, ASKME lines ask-tier, WARNME lines warn-tier, ODDME a tier the protocol lacks. +#: TOY_TIERS=off drops the tier field; TOY_TIERS=partial drops it from ASKME findings only. +TOY = textwrap.dedent( + """\ + import json, os, sys + payload = json.load(sys.stdin) + tiers = {"BLOCKME": "block", "ASKME": "ask", "WARNME": "warn", "ODDME": "severe"} + mode = os.environ.get("TOY_TIERS", "on") + findings = [] + for path, text in payload["writes"].items(): + for number, line in enumerate(text.splitlines(), 1): + for word, tier in tiers.items(): + if word in line: + item = {"key": word + ":" + line.strip(), "path": path, "line": number, "message": tier + " " + line} + if mode == "on" or (mode == "partial" and word != "ASKME"): + item["tier"] = tier + findings.append(item) + print(json.dumps({"findings": findings})) + kinds = {item["message"].split()[0] for item in findings} + if "block" in kinds or "severe" in kinds: + sys.exit(1) + sys.exit(3 if "ask" in kinds else 4 if kinds else 0) + """ +) + +OLD = "x = BLOCKME\n" +COMMIT, WRITE = "commit", "pre-tool-use" + + +def _gate(repo: Path, action: str = "block") -> Path: + (repo / "toy.py").write_text(TOY, encoding="utf-8") + gate = repo / "gate.json" + spec = {"kind": "script", "on": ["commit", "tool_use"], "action": action, "message": "m"} + gate.write_text(json.dumps({**spec, "params": {"script": "toy.py"}}), encoding="utf-8") + return gate + + +def _verdict(gate: Path, repo: Path, event: str, text: str) -> str: + """The verdict of changing app.py to `text`, as a commit stages it or an agent writes it.""" + if event == COMMIT: + (repo / "app.py").write_text(text, encoding="utf-8") + subprocess.run(["git", "add", "app.py"], cwd=repo, check=True) + return judge(gate, COMMIT, None, repo)[1] + return judge(gate, event, None, repo, writes={"app.py": text})[1] + + +@pytest.fixture(params=[COMMIT, WRITE]) +def event(request: pytest.FixtureRequest) -> str: + return request.param + + +def test_a_new_ask_finding_beside_an_old_block_finding_asks(tmp_path: Path, event: str) -> None: + repo = repo_with(tmp_path, **{"app.py": OLD}) + assert _verdict(_gate(repo), repo, event, OLD + "y = ASKME\n") == "ask" + + +def test_a_new_block_finding_beside_an_old_one_blocks(tmp_path: Path, event: str) -> None: + repo = repo_with(tmp_path, **{"app.py": OLD}) + assert _verdict(_gate(repo), repo, event, OLD + "y = BLOCKME two\n") == "block" + + +def test_touching_only_old_findings_allows(tmp_path: Path, event: str) -> None: + repo = repo_with(tmp_path, **{"app.py": OLD}) + assert _verdict(_gate(repo), repo, event, "# top\n" + OLD) == "allow" + + +def test_the_highest_tier_among_new_findings_decides(tmp_path: Path, event: str) -> None: + repo = repo_with(tmp_path, **{"app.py": "x = 1\n"}) + assert _verdict(_gate(repo), repo, event, "a = WARNME\nb = ASKME\n") == "ask" + assert _verdict(_gate(repo), repo, event, "a = WARNME\n") == "warn" + + +def test_the_refusal_text_lists_only_the_new_finding(tmp_path: Path, capsys: pytest.CaptureFixture) -> None: + repo = repo_with(tmp_path, **{"app.py": OLD}) + _verdict(_gate(repo), repo, WRITE, OLD + "y = ASKME\n") + err = capsys.readouterr().err + assert "app.py:2: ask y = ASKME" in err + assert "x = BLOCKME" not in err + + +def test_the_declared_action_still_caps_a_block_tier_finding(tmp_path: Path, event: str) -> None: + repo = repo_with(tmp_path, **{"app.py": "x = 1\n"}) + assert _verdict(_gate(repo, "ask"), repo, event, "y = BLOCKME\n") == "ask" + + +def test_a_script_with_no_tiers_keeps_its_exit_code( + tmp_path: Path, event: str, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("TOY_TIERS", "off") + repo = repo_with(tmp_path, **{"app.py": OLD}) + assert _verdict(_gate(repo), repo, event, OLD + "y = ASKME\n") == "block" + + +def test_one_finding_without_a_tier_hands_the_verdict_back_to_the_exit_code( + tmp_path: Path, event: str, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("TOY_TIERS", "partial") + repo = repo_with(tmp_path, **{"app.py": OLD}) + assert _verdict(_gate(repo), repo, event, OLD + "y = ASKME\n") == "block" + + +def test_a_tier_the_protocol_lacks_is_ignored(tmp_path: Path, event: str) -> None: + repo = repo_with(tmp_path, **{"app.py": OLD}) + assert _verdict(_gate(repo), repo, event, OLD + "y = ODDME\nz = ASKME\n") == "block" + + +def test_a_new_file_with_an_ask_finding_asks(tmp_path: Path, event: str) -> None: + repo = repo_with(tmp_path, **{"keep.py": OLD}) + assert _verdict(_gate(repo), repo, event, "y = ASKME\n") == "ask" From f98263c9d1a7632bb209fa68f20014d05278cff4 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:37:30 +0000 Subject: [PATCH 43/57] spec: keep gate-dsl.md inside the 300-line budget after the tier field (E-2) Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- spec/gate-dsl.md | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/spec/gate-dsl.md b/spec/gate-dsl.md index 9fda853c..19ea2f9a 100644 --- a/spec/gate-dsl.md +++ b/spec/gate-dsl.md @@ -258,9 +258,8 @@ introduces: are for display. An optional `"new": true` makes the finding count as new whatever the baseline holds, for a script that already compared with HEAD itself. An optional `rule` (string, `[A-Za-z0-9][A-Za-z0-9 ._:/-]{0,63}`) names the rule for the gate log; a missing or malformed - `rule` is ignored and never invalidates the document. An optional `tier` (`"block"`, `"ask"` or - `"warn"`) is the verdict this one finding warrants (see Verdict); any other value counts as no tier - and never invalidates the document. + `rule` is ignored and never invalidates the document. An optional `tier` (`block`, `ask` or `warn`) + is the verdict this finding warrants (see Verdict); any other value is ignored. - A document is valid only when `findings` is a list of objects each carrying string `key`, `path` and `message`, an integer `line`, and a boolean `new` if present. Anything else on stdout is not a document, and the script keeps the exit-code contract above exactly (one run). @@ -273,13 +272,11 @@ introduces: - **New findings.** Per `path`, each baseline finding cancels one change-run finding with the same `key`, so a finding is new when its key occurs more often in the change-run than in the baseline; a second copy of an old violation is new. -- **Verdict.** No new findings allows, whatever the change-run's exit code. Otherwise, when every - new finding carries a `tier`, the verdict is the highest among them (`block` over `ask` over - `warn`), so an old block-tier finding in a touched file cannot turn a new ask-tier one into a - block; when any new finding lacks one, the change-run's exit code decides (`1` blocks, `3` asks, - `4` warns; any other code is undecided as above). Either way the verdict is capped by the declared - `action`, exit `0` allows, and the reason lists only the new findings as `path:line: message`, - not the script's stderr. A script that sets no `tier` behaves exactly as before. +- **Verdict.** No new findings allows, whatever the change-run's exit code. Otherwise, when every new + finding carries a `tier`, the highest (`block` over `ask` over `warn`) decides, so an old block-tier + finding cannot turn a new ask-tier one into a block; if any lacks one, the change-run's exit code + decides (codes as above). The declared `action` caps either; the reason lists only the new + findings as `path:line: message`, not the script's stderr. No `tier` anywhere: exactly as before. - **Baseline text** is the file before the change: `commit`, HEAD; `push` and `ci`, the base of the range; `tool_use` before the write (PreToolUse), the file on disk, and at the turn's end (Stop), HEAD. A write outside the repository has the file on disk before the write, or no @@ -289,16 +286,14 @@ introduces: git, no HEAD) leaves the baseline findings empty, which errs toward blocking. - **Budget.** Both runs share the 30s timeout; the baseline run gets what the change-run left. - The gate log records `new_findings`, `baseline_findings` and, per declared `rule`, `rules` - (rule to count of new findings). `tool_call` scripts and the - `hook.script` event scripts are unchanged. + (rule to count of new findings). `tool_call` scripts and the `hook.script` event scripts are unchanged. `chock compile` rewrites `script` to the file's path from the repository root, which is all the runner has; the ambient line an agent reads keeps the bare name, so the packaged `SKILL.md` is the same wherever the policy sits. `chock check` refuses a name that is not a bare `.py` file name, and a script the policy does not ship. The script is deterministic code under -`implementations/`, so SEC-2 applies to it as to any guard, and `chock check --only evals` -copies that directory into the throwaway repository a staged-files case is replayed in, where -the compiled gate names it. +`implementations/`, so SEC-2 applies to it, and `chock check --only evals` copies that directory +into the throwaway repository a staged-files case is replayed in, where the compiled gate names it. ## Runtime note From 2fd4c1b0ad91f85b1dcd917cb0c2d02bc616292e Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:37:15 -0500 Subject: [PATCH 44/57] sync the compiled runtime, goldens, lockfile and hook configs Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- .chock/bin/gate.py | 51 ++++++++++++++++--- .../fixtures/plugin_targets/antigravity.json | 4 +- tests/fixtures/plugin_targets/droid.json | 6 +-- tests/fixtures/plugin_targets/goose.json | 6 +-- tests/fixtures/plugin_targets/kimi.json | 8 +-- tests/fixtures/plugin_targets/openhands.json | 6 +-- 6 files changed, 60 insertions(+), 21 deletions(-) diff --git a/.chock/bin/gate.py b/.chock/bin/gate.py index 0cf99ede..a76d19f8 100755 --- a/.chock/bin/gate.py +++ b/.chock/bin/gate.py @@ -319,6 +319,7 @@ def __init__( self.scope = tuple(scope or ()) #: Path prefixes this gate never judges: its own policy's source and compiled folders. self.own = tuple(own) + self._rename_map: dict[str, str] | None = None def is_own(self, path: str) -> bool: """Whether `path` is in this policy's own source or compiled folder.""" @@ -385,9 +386,28 @@ def staged_paths(self, diff_filter: str = "ACMRT", *, judge_own: bool = False) - paths = self._listing("diff", *self._range(), "--name-only", f"--diff-filter={diff_filter}") return [path for path in paths if self.in_scope(path, judge_own=judge_own)] + def _rename_range(self) -> list[str] | None: + """The git-diff scope whose renames the baseline follows; None when this context has none.""" + return self._range() + + def _renames(self) -> dict[str, str]: + """{new path: old path} for what git itself reports as renamed (`-M`, default similarity); never a guess. + + A listing that fails or does not parse says no rename, which leaves the baseline at the path (stricter). + """ + if self._rename_map is None: + scope, tokens = self._rename_range(), [] + if scope is not None: + with contextlib.suppress(ListingError): + tokens = self._listing("diff", *scope, "-M", "--relative", "--name-status", "--diff-filter=R") + self._rename_map = {tokens[i + 2]: tokens[i + 1] for i in range(0, len(tokens) - 2, 3)} + return self._rename_map + def _diff_text(self, path: str) -> str: """`path`'s `-U0` diff as git writes it, unconverted; raises DiffError when git fails.""" - args = ("diff", *self._range(), "-U0", "--no-color", "--no-ext-diff", "--no-textconv", "--", path) + old = self._renames().get(path) + paths = (path, old) if old else (path,) + args = ("diff", *self._range(), "-M", "-U0", "--no-color", "--no-ext-diff", "--no-textconv", "--", *paths) try: proc = subprocess.run( # noqa: S603 -- reading repo facts via git is this class's whole job [_GIT, "--literal-pathspecs", *args], @@ -416,8 +436,8 @@ def staged_blob(self, path: str) -> str: return self._git("show", f"HEAD:{path}" if self.base else f":{path}") def head_blob(self, path: str) -> str: - """Content before the change, or "" when the path is new in it.""" - return self._git("show", f"{self.base or 'HEAD'}:{path}") + """Content before the change, or "" when the path is new in it; a renamed path reads its old path.""" + return self._git("show", f"{self.base or 'HEAD'}:{self._renames().get(path, path)}") def committed_blob(self, path: str) -> str: """The file as HEAD has it, or "" when HEAD does not.""" @@ -486,6 +506,10 @@ def __init__( self._added = dict(added or {}) self._stop = stop + def _rename_range(self) -> list[str] | None: + """At the turn's end, HEAD against the index and tree; a write before the tool runs has no rename.""" + return ["HEAD"] if self._stop else None + def staged_paths(self, diff_filter: str = "ACMRT", *, judge_own: bool = False) -> list[str]: """Every write is a present file, so a filter asking only for deletions finds none.""" if not set(diff_filter) & set("ACMRT"): @@ -517,7 +541,7 @@ def committed_blob(self, path: str) -> str: def head_blob(self, path: str) -> str: """The baseline: HEAD at the turn's end, else what is on disk now, which this write would replace.""" if self._stop: - return self.committed_blob(path) + return self.committed_blob(self._renames().get(path, path)) try: return (self.repo_root / path).read_text(encoding="utf-8") except (OSError, UnicodeDecodeError): @@ -1058,6 +1082,19 @@ def _new_findings(found: list[dict], baseline: list[dict]) -> list[dict]: return fresh +#: A finding's optional `tier`, ranked; a verdict is the highest among the findings the change adds. +_TIER_RANK = {"warn": 1, "ask": 2, "block": 3} + + +def _tier_verdict(fresh: list[dict]) -> str | None: + """The verdict word ("" blocks) when every kept finding carries a known tier, else None: the exit code decides.""" + tiers = [item.get("tier") for item in fresh] + if not all(isinstance(tier, str) and tier in _TIER_RANK for tier in tiers): + return None + top = max(tiers, key=_TIER_RANK.__getitem__) + return "" if top == "block" else top + + def _baseline_findings(ctx: GateContext, script: Path, material: dict, started: float) -> list[dict]: """What the script finds in the baseline text of the change's files; empty when it cannot say. @@ -1096,7 +1133,7 @@ def _exit_verdict(script: Path, proc: subprocess.CompletedProcess[str]) -> GateR def _judge_findings( ctx: GateContext, script: Path, material: dict, proc: subprocess.CompletedProcess[str], started: float ) -> GateResult: - """Judge only the findings the change introduces; the change-run's exit code is the verdict for them.""" + """Judge only the findings the change introduces: the highest tier among them, else the change-run's exit code.""" if proc.returncode not in (_SCRIPT_ALLOW, _SCRIPT_BLOCK, _SCRIPT_ASK, _SCRIPT_WARN): # A document followed by a crash or an unknown exit is undecided, never a clean pass. return _exit_verdict(script, proc) @@ -1109,7 +1146,9 @@ def _judge_findings( if not fresh or proc.returncode == _SCRIPT_ALLOW: return GateResult(allowed=True, detail=counts) lines = [f"{item['path']}:{item['line']}: {item['message']}" for item in fresh] - verdict = {_SCRIPT_ASK: "ask", _SCRIPT_WARN: "warn"}.get(proc.returncode, "") + verdict = _tier_verdict(fresh) + if verdict is None: + verdict = {_SCRIPT_ASK: "ask", _SCRIPT_WARN: "warn"}.get(proc.returncode, "") return GateResult( allowed=False, matches=lines, verdict=verdict, detail=counts, rules=_rule_ids(fresh), findings=fresh ) diff --git a/tests/fixtures/plugin_targets/antigravity.json b/tests/fixtures/plugin_targets/antigravity.json index 95b251a3..3a86e1a3 100644 --- a/tests/fixtures/plugin_targets/antigravity.json +++ b/tests/fixtures/plugin_targets/antigravity.json @@ -41,7 +41,7 @@ "Stop": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-501fc3f8.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-501fc3f8.json\" --index-sha256 0723fe19cf149e5cfb81c4212a4a48e6dd9087a4503e8e06c4700b6050f48707 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 }, { @@ -55,7 +55,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b4ab2f9e.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"__CHOCK_PLUGIN_ROOT__\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/antigravity.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b4ab2f9e.json\" --index-sha256 953302045b1f8ba8218f7147da4f3fa6c806e361ab15b22a82cd4b0a096fc2f3 --root \"__CHOCK_PLUGIN_ROOT__\" --stop-check", "timeout": 45 } ], diff --git a/tests/fixtures/plugin_targets/droid.json b/tests/fixtures/plugin_targets/droid.json index ece3f350..b12c9460 100644 --- a/tests/fixtures/plugin_targets/droid.json +++ b/tests/fixtures/plugin_targets/droid.json @@ -44,7 +44,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-f8a1fdfc.json\" --index-sha256 701603c13fd36de20f91005e93e586186f4dd8f6aa0f0a37cffabb1f58189f71 --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-f8a1fdfc.json\" --index-sha256 1fe6eafa07fd4ac4378bd7836ed75ee30f83b2022d9f8d0717b26d8f5dde71ff --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ], @@ -54,7 +54,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-91dc9fe9.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/PreToolUse-91dc9fe9.json\" --index-sha256 953302045b1f8ba8218f7147da4f3fa6c806e361ab15b22a82cd4b0a096fc2f3 --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ], @@ -75,7 +75,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/Stop-185f9750.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"${DROID_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${DROID_PLUGIN_ROOT}/scripts/launch.sh\" \"${DROID_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${DROID_PLUGIN_ROOT}/scripts/factory_droid.py\" --multi \"${DROID_PLUGIN_ROOT}/scripts/chock_index/Stop-185f9750.json\" --index-sha256 0723fe19cf149e5cfb81c4212a4a48e6dd9087a4503e8e06c4700b6050f48707 --root \"${DROID_PLUGIN_ROOT}\"", "timeout": 45 } ] diff --git a/tests/fixtures/plugin_targets/goose.json b/tests/fixtures/plugin_targets/goose.json index f79fa93f..99e841ce 100644 --- a/tests/fixtures/plugin_targets/goose.json +++ b/tests/fixtures/plugin_targets/goose.json @@ -40,7 +40,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-b6718bef.json\" --index-sha256 701603c13fd36de20f91005e93e586186f4dd8f6aa0f0a37cffabb1f58189f71 --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-b6718bef.json\" --index-sha256 1fe6eafa07fd4ac4378bd7836ed75ee30f83b2022d9f8d0717b26d8f5dde71ff --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } @@ -51,7 +51,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-031b12f7.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/PreToolUse-031b12f7.json\" --index-sha256 953302045b1f8ba8218f7147da4f3fa6c806e361ab15b22a82cd4b0a096fc2f3 --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } @@ -74,7 +74,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/Stop-4cd5d275.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"${PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${PLUGIN_ROOT}/scripts/launch.sh\" \"${PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${PLUGIN_ROOT}/scripts/goose.py\" --multi \"${PLUGIN_ROOT}/scripts/chock_index/Stop-4cd5d275.json\" --index-sha256 0723fe19cf149e5cfb81c4212a4a48e6dd9087a4503e8e06c4700b6050f48707 --root \"${PLUGIN_ROOT}\"", "on_failure": "block", "timeout": 45 } diff --git a/tests/fixtures/plugin_targets/kimi.json b/tests/fixtures/plugin_targets/kimi.json index 5bbd7994..7521273c 100644 --- a/tests/fixtures/plugin_targets/kimi.json +++ b/tests/fixtures/plugin_targets/kimi.json @@ -39,12 +39,12 @@ "hooks": [ { "event": "Stop", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"${KIMI_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 0723fe19cf149e5cfb81c4212a4a48e6dd9087a4503e8e06c4700b6050f48707 --root \"${KIMI_PLUGIN_ROOT}\"", "timeout": 45 }, { "event": "PreToolUse", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 953302045b1f8ba8218f7147da4f3fa6c806e361ab15b22a82cd4b0a096fc2f3 --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", "timeout": 45 }, { @@ -62,12 +62,12 @@ "hooks": [ { "event": "Stop", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"${KIMI_PLUGIN_ROOT}\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-55aaba09.json\" --index-sha256 0723fe19cf149e5cfb81c4212a4a48e6dd9087a4503e8e06c4700b6050f48707 --root \"${KIMI_PLUGIN_ROOT}\"", "timeout": 45 }, { "event": "PreToolUse", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"${KIMI_PLUGIN_ROOT}/scripts/launch.sh\" \"${KIMI_PLUGIN_ROOT}/scripts/chock_bundle.py\" --bundle demo-bundle --members \"${KIMI_PLUGIN_ROOT}/scripts/kimi_code.py\" --multi \"${KIMI_PLUGIN_ROOT}/scripts/chock_index/hooks-c573a7ea.json\" --index-sha256 953302045b1f8ba8218f7147da4f3fa6c806e361ab15b22a82cd4b0a096fc2f3 --root \"${KIMI_PLUGIN_ROOT}\" --stop-check", "timeout": 45 }, { diff --git a/tests/fixtures/plugin_targets/openhands.json b/tests/fixtures/plugin_targets/openhands.json index 570533a0..e30482c8 100644 --- a/tests/fixtures/plugin_targets/openhands.json +++ b/tests/fixtures/plugin_targets/openhands.json @@ -45,7 +45,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b310004d.json\" --index-sha256 701603c13fd36de20f91005e93e586186f4dd8f6aa0f0a37cffabb1f58189f71 --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-b310004d.json\" --index-sha256 1fe6eafa07fd4ac4378bd7836ed75ee30f83b2022d9f8d0717b26d8f5dde71ff --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ], @@ -55,7 +55,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-81fe7457.json\" --index-sha256 d561a195280bd5b44790dd05466dccd39663df79bc95d20be874d0518524340a --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/PreToolUse-81fe7457.json\" --index-sha256 953302045b1f8ba8218f7147da4f3fa6c806e361ab15b22a82cd4b0a096fc2f3 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ], @@ -76,7 +76,7 @@ "hooks": [ { "type": "command", - "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-185f9750.json\" --index-sha256 219740d3104a63ad9db2ddcd50a41365693dd6a9643996689a6df7cf6fe7a158 --root \"__CHOCK_PLUGIN_ROOT__\"", + "command": "git -c 'alias.chock-sh=!set -f; IFS=; test -f $1 || { echo chock: the plugin launcher is missing, so this call cannot be checked. Refusing. >&2; exit 2; }; sh' chock-sh \"__CHOCK_PLUGIN_ROOT__/scripts/launch.sh\" \"__CHOCK_PLUGIN_ROOT__/scripts/chock_bundle.py\" --bundle demo-bundle --members \"__CHOCK_PLUGIN_ROOT__/scripts/openhands.py\" --multi \"__CHOCK_PLUGIN_ROOT__/scripts/chock_index/Stop-185f9750.json\" --index-sha256 0723fe19cf149e5cfb81c4212a4a48e6dd9087a4503e8e06c4700b6050f48707 --root \"__CHOCK_PLUGIN_ROOT__\"", "timeout": 45 } ] From 876dfef8419a2436a4796ef620bc4b7d29a01c66 Mon Sep 17 00:00:00 2001 From: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:58:43 +0000 Subject: [PATCH 45/57] README: a figure of where Chock sits, under "How it works" (RD-7) Adds v2/where-chock-sits.png, rendered by render.mjs from where-chock-sits.json: you, your agent, the three points where Chock checks an action, the two outcomes and five things it stops. No prose added, so the README stays within its word budget. docs/architecture.md shows the same figure above the existing one. Tests tie the figure to its data file, the witness ledger and both docs. Signed-off-by: Jothi Mani Rajendran <250249270+jothimani-rajendran@users.noreply.github.com> --- README.md | 2 + changelog.d/rd7-readme-picture.md | 5 +++ docs/architecture.md | 4 ++ docs/assets/readme/render.mjs | 20 ++++++++- docs/assets/readme/v2/where-chock-sits.png | Bin 0 -> 259149 bytes docs/assets/readme/where-chock-sits.json | 29 +++++++++++++ tests/test_readme_picture.py | 45 +++++++++++++++++++++ 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 changelog.d/rd7-readme-picture.md create mode 100644 docs/assets/readme/v2/where-chock-sits.png create mode 100644 docs/assets/readme/where-chock-sits.json create mode 100644 tests/test_readme_picture.py diff --git a/README.md b/README.md index 60d84ff7..709a8ccb 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ Tiers: see [Guardrails, not guarantees](#guardrails-not-guarantees). ## How it works +

A left-to-right diagram: you ask your coding agent for a change; Chock checks each action before the agent's tool runs where the agent has hooks, at git commit and in CI; an allowed change lands, and a refused one tells the agent in one line what to do instead.

+ A policy is one folder, and the manifest is the rule. Here is `scan-secrets` in this repo: ``` diff --git a/changelog.d/rd7-readme-picture.md b/changelog.d/rd7-readme-picture.md new file mode 100644 index 00000000..38cda4d3 --- /dev/null +++ b/changelog.d/rd7-readme-picture.md @@ -0,0 +1,5 @@ +- README: "How it works" opens with a figure of where Chock sits for a person using a coding agent: you, your + agent, the three points where Chock checks an action (before the agent's tool runs where the agent has hooks, + at `git commit`, in CI), the two outcomes, and five things it stops. No prose was added: the README stays at + its word budget. The figure is `docs/assets/readme/v2/where-chock-sits.png`, rendered by `render.mjs` from + `where-chock-sits.json`; `docs/architecture.md` shows it above the author-compile-enforce figure. diff --git a/docs/architecture.md b/docs/architecture.md index 6fa48d5e..1d59e316 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -4,6 +4,10 @@ Chock has one job: let you **author a governance policy once** and **enforce it coding agent** that touches your repo — your team's or a contributor's — with proof of exactly where each guarantee holds. +
+ A left-to-right diagram: you ask your coding agent for a change; Chock checks each action before the agent's tool runs where the agent has hooks, at git commit and in CI; an allowed change lands, and a refused one tells the agent in one line what to do instead. +
+
Author a policy once, compile it, and enforce it on every agent's native surface.
diff --git a/docs/assets/readme/render.mjs b/docs/assets/readme/render.mjs index 5a758364..1d71dadd 100644 --- a/docs/assets/readme/render.mjs +++ b/docs/assets/readme/render.mjs @@ -215,9 +215,27 @@ const HONEST = SECTION('Guardrails, not guarantees', 'And we tell you `
${tag(t)}${p}
`))} ${grid(3, 16, [[A.grades.enforced, 'matrix cells graded “enforced”: no agent reaches it'], [`${D.asi.refused_at_commit.length} of ${D.asi.with_policy}`, 'OWASP Agentic risks with a slice refused at commit'], [D.asi.fully_covered, 'OWASP Agentic risks fully covered: every mapping is partial']].map(([n, l]) => stat(n, l)))}`); +// The README's "How it works" figure: the user's view. Every word comes from where-chock-sits.json. +const W = JSON.parse(readFileSync(path.join(HERE, 'where-chock-sits.json'), 'utf8')); +const esc = (s) => s.replace(/&/g, '&').replace(/ `
${esc(name)}${esc(line)}
`; +const point = (p) => `
${p.at}${esc(p.name)}
${esc(p.scope)}
`; +const outcome = (o) => `
${esc(o.name)}${esc(o.line)}
`; +const stopTag = (t) => `${t === 'commit' ? 'at commit' : 'in the agent, best-effort'}`; +const WHERE = SECTION('', '', ` +
${esc(W.title)}
+
+${node(W.you.name, W.you.line)}${arrow} +${node(W.agent.name, `${W.agent.names.join(', ')} ${W.agent.more}. It ${W.agent.line}.`)}${arrow} +
${esc(W.chock.name)}${esc(W.chock.line)}${W.chock.points.map(point).join('')}
${arrow} +
${W.outcomes.map(outcome).join('')}
+
${esc(W.stops_title)} +
${W.stops.map((s) => `
${esc(s.line)}${stopTag(s.tier)}
`).join('')}
+
${esc(W.footer)}
`, { bare: true }); + const ALL_NEW = { layers: LAYERS, quickstart: QUICKSTART, matrix: MATRIX, catalog: CATALOG, family: FAMILY, honest: HONEST }; // README v2 panels go under v2/; the titled refusal, roles and contribute PNGs at the old paths stay frozen until RW-7b. -const V2 = { 'v2/refusal': REFUSAL, 'v2/roles': ROLES, 'v2/contribute': CONTRIBUTE }; +const V2 = { 'v2/refusal': REFUSAL, 'v2/roles': ROLES, 'v2/contribute': CONTRIBUTE, 'v2/where-chock-sits': WHERE }; // 1280x640 social preview banners: the same specs as the covers, laid out for the fixed canvas. const social = ({ repo, title, sub, chips, right }) => `