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..d7dc23dc 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -303,6 +303,15 @@ def settings_fingerprint(config: Config, worktree: Path, *, base: str | None = N return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest() +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) + 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 (command not in config.inputs and "." not in config.inputs.get("*", ())): return None diff --git a/tests/test_cli.py b/tests/test_cli.py index dfc31abb..3e1b95cc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2991,6 +2991,51 @@ 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 + + +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: