From 783bb8f8174d78c150177813a816a8008d8aaedc Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:29:25 +0100 Subject: [PATCH 1/2] Verify saved stage input keys before accepting check receipts Architecture-Review: unchanged; local readiness still requires a current successful check receipt, now verified against the existing stage input keys. Co-Authored-By: Codex --- src/loopzero/cli.py | 3 ++- src/loopzero/sandbox.py | 16 +++++++++++++++- tests/test_cli.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 20b73319..c91e940f 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -318,7 +318,8 @@ def _check_report_matches(report: CheckReport, wt: Path, config: Config, head: s return (report.head == head and not report.dirty and tuple(result.command for result in report.results) == config.checks and report.settings_sha256 is not None - and report.settings_sha256 == sandbox.settings_fingerprint(config, wt)) + and report.settings_sha256 == sandbox.settings_fingerprint(config, wt) + and sandbox.receipt_inputs_match(config, wt, report)) def _current_check_report(wt: Path, config: Config, head: str) -> CheckReport | None: diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index a358d56f..8d062f7c 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -303,8 +303,22 @@ def settings_fingerprint(config: Config, worktree: Path, *, base: str | None = N return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest() +def _keyed_stage(config: Config, command: str) -> bool: + return command in config.inputs or "." in config.inputs.get("*", ()) + + +def receipt_inputs_match(config: Config, worktree: Path, report: CheckReport) -> bool: + try: + base = _check_base(config, worktree) + return all(result.input_key == _input_key(config, worktree, result.command, base) + and result.input_key is not None + for result in report.results if _keyed_stage(config, result.command)) + except (SandboxUnavailable, OSError): + return False + + def _input_key(config: Config, worktree: Path, command: str, base: str) -> str | None: - if not base or (command not in config.inputs and "." not in config.inputs.get("*", ())): + if not base or not _keyed_stage(config, command): return None paths = (*config.inputs.get(command, ()), *config.inputs.get("*", ())) # Empty pathspecs must not accidentally mean the whole tree. diff --git a/tests/test_cli.py b/tests/test_cli.py index dfc31abb..1f2ed9f4 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2991,6 +2991,35 @@ def change_policy(_seconds): assert run(capsys, *args, "check")[0] == 1 +def test_ready_and_merge_reject_receipt_after_clean_checkout_rewrites_input( + wt, gh, capsys, head, tmp_path, +): + arm_approved_ready_pr(gh, head, wt, readiness={"local_exit": None}, isDraft=False) + path = wt / "feature.py" + command = """python3 -c 'from pathlib import Path; exit(13 in Path("feature.py").read_bytes())'""" + settings = tmp_path / "workflow.toml" + settings.write_text(WORKFLOW.replace('"echo ok"', json.dumps(command)).replace( + "[delivery]", f'[checks.inputs]\n{json.dumps(command)} = ["feature.py"]\n\n[delivery]', + )) + args = ("--config", str(settings)) + + assert b"\r" not in path.read_bytes() + assert run(capsys, *args, "check")[0] == 0 + assert run(capsys, *args, "ready")[0] == 0 + + git(wt, "config", "core.autocrlf", "true") + path.unlink() + git(wt, "checkout-index", "-u", "--", path.name) + assert b"\r\n" in path.read_bytes() + assert head_of(wt) == head and not git(wt, "status", "--porcelain") + + for action in ("ready", "merge"): + code, out, err = run(capsys, *args, action) + assert code == 1 + assert "run loopzero check at this head" in out + err + assert run(capsys, *args, "check")[0] == 1 + + @pytest.mark.parametrize("base_sensitive", [False, True]) def test_wait_snapshot_handles_trusted_base_advance(wt, repo, base_sensitive): if base_sensitive: From c859414664413a56455f62a2b1e89f247ffc2637 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:36:18 +0100 Subject: [PATCH 2/2] Preserve receipt validation for stages without reuse keys Architecture-Review: unchanged; stages without an input key keep their existing successful receipt semantics. Co-Authored-By: Codex --- src/loopzero/sandbox.py | 11 +++-------- tests/test_cli.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index 8d062f7c..d7dc23dc 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -303,22 +303,17 @@ def settings_fingerprint(config: Config, worktree: Path, *, base: str | None = N return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest() -def _keyed_stage(config: Config, command: str) -> bool: - return command in config.inputs or "." in config.inputs.get("*", ()) - - def receipt_inputs_match(config: Config, worktree: Path, report: CheckReport) -> bool: try: base = _check_base(config, worktree) return all(result.input_key == _input_key(config, worktree, result.command, base) - and result.input_key is not None - for result in report.results if _keyed_stage(config, result.command)) - except (SandboxUnavailable, OSError): + for result in report.results) + except (SandboxUnavailable, OSError, _proc.ProcTimeout, _proc.ToolMissing): return False def _input_key(config: Config, worktree: Path, command: str, base: str) -> str | None: - if not base or not _keyed_stage(config, command): + if not base or (command not in config.inputs and "." not in config.inputs.get("*", ())): return None paths = (*config.inputs.get(command, ()), *config.inputs.get("*", ())) # Empty pathspecs must not accidentally mean the whole tree. diff --git a/tests/test_cli.py b/tests/test_cli.py index 1f2ed9f4..3e1b95cc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3020,6 +3020,22 @@ def test_ready_and_merge_reject_receipt_after_clean_checkout_rewrites_input( assert run(capsys, *args, "check")[0] == 1 +def test_ready_accepts_fresh_check_for_input_without_reuse_key(wt, gh, capsys, tmp_path): + (wt / "link").symlink_to("feature.py") + git(wt, "add", "link") + git(wt, "commit", "-qm", "add tracked symlink") + arm_approved_ready_pr(gh, head_of(wt), wt, readiness={"local_exit": None}, isDraft=False) + settings = tmp_path / "workflow.toml" + settings.write_text(WORKFLOW.replace( + "[delivery]", '[checks.inputs]\n"*" = ["."]\n\n[delivery]', + )) + args = ("--config", str(settings)) + assert run(capsys, *args, "check")[0] == 0 + report = json.loads((wt / ".loopzero/checks.json").read_text()) + assert all("input_key" not in stage for stage in report["results"]) + assert run(capsys, *args, "ready")[0] == 0 + + @pytest.mark.parametrize("base_sensitive", [False, True]) def test_wait_snapshot_handles_trusted_base_advance(wt, repo, base_sensitive): if base_sensitive: