From 9018045bf14da545dedda4ba9ee5f88e6b73110c Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:26:48 +0100 Subject: [PATCH] Recheck saved stage input keys before accepting a receipt Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 2 ++ src/loopzero/cli.py | 2 ++ src/loopzero/sandbox.py | 11 +++++++++++ tests/test_cli.py | 24 ++++++++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 49620cba..cc66719a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -114,6 +114,8 @@ loading the delivery [commands](../src/loopzero/cli.py), which implement the con for local sandbox receipts. Explicit settings, delegated runner environments and stage reuse keys still bind the grant. Waiters check for policy changes on every poll. + Local readiness recomputes input keys recorded in the passing receipt from + the checkout bytes. Missing or changed comparisons require a fresh check. Local readiness and hosted eligibility carry a review forward only across a first-parent chain of verified merges from the configured PR base. They reconstruct each merge in an isolated Git repository and compare its tree diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index f1645fb8..0ff4722b 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -364,6 +364,8 @@ def _check_report_problem( return f"check receipt is missing its settings fingerprint. {recovery}" if report.settings_sha256 != sandbox.settings_fingerprint(config, wt): return f"check receipt has an incompatible settings fingerprint. {recovery}" + if not sandbox.receipt_inputs_match(config, wt, report): + return f"declared check inputs changed or are unavailable. {recovery}" return None diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index 6f725e28..98cd5275 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -274,6 +274,17 @@ def _input_blobs(config: Config, worktree: Path, paths: tuple[str, ...]) -> str raise +def receipt_inputs_match(config: Config, worktree: Path, report: CheckReport) -> bool: + keyed = [stage for stage in report.results if stage.input_key is not None] + if not keyed: + return True + try: + base = _check_base(config, worktree) + return all(stage.input_key == _input_key(config, worktree, stage.command, base) for stage in keyed) + except (SandboxUnavailable, OSError, _proc.ProcTimeout, _proc.ToolMissing): + return False + + def _input_key(config: Config, worktree: Path, command: str, base: str, *, status: Callable[[str], None] | None = None, workflow: str | Path = "workflow.toml") -> str | None: diff --git a/tests/test_cli.py b/tests/test_cli.py index 5c136d5d..4dac477f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2997,6 +2997,30 @@ def test_check_refuses_unverified_receipt_after_runner_retarget(wt, gh, capsys, assert run(capsys, "ready")[0] == 0 +def test_ready_and_merge_reject_receipt_after_clean_checkout_rewrites_input(wt, gh, capsys, tmp_path): + path, head = wt / "feature.py", head_of(wt) + arm_approved_ready_pr(gh, head, wt, readiness={"local_exit": None}, isDraft=False) + 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)) + expect_cli(capsys, 0, *args, "check") + expect_cli(capsys, 0, *args, "ready") + + 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 and "declared check inputs" in out + err + assert "Run loopzero check" in out + err + expect_cli(capsys, 1, *args, "check") + + @pytest.mark.parametrize("base_sensitive", [False, True]) def test_wait_snapshot_handles_trusted_base_advance(wt, repo, base_sensitive): if base_sensitive: