diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c474e6e7..769418f7 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -490,14 +490,15 @@ belongs to the runner: the outer `loopzero check` must not hold a heavy slot when a runner is configured. Without a runner, the existing local execution path is unchanged. -Declared `[checks.inputs]` pathspecs opt commands into local passing-result reuse. -Keys bind committed paths/blob IDs, the bytes of the checked-out regular files, -command, version, sandbox configuration and the resolved base SHA. A stage runs -fresh when a matched path cannot be identified as a readable regular file. -The receipt fingerprint also binds the base SHA when reuse -is configured, so advancing the base invalidates a pass at the same task head. -If the remote ref is absent, checks use the task's recorded base. Without -either base, local stages run fresh. +Declared `[checks.inputs]` pathspecs opt commands into local passing-result reuse +and bind their receipts for readiness and merge. Keys cover committed paths and +blob IDs, the bytes of the checked-out regular files, the command, version, +sandbox configuration and the resolved base SHA. Stages run fresh when keys +are unavailable. Readiness and merge require matching keys for declared stages +even after a successful check. Restore an available base or narrow oversized +pathspecs and declarations that match unreadable or non-regular paths, then rerun +`loopzero check`. The fingerprint also binds the base SHA when reuse is configured. +If the remote ref is absent, checks use the task's recorded base. The `"*"` entry adds shared inputs to listed commands. Unlisted commands reuse only when `"*"` includes `"."` (the whole tree); a partial or empty `"*"` leaves them unkeyed. Consumers own complete input declarations. diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 2077c223..33a92df9 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -326,6 +326,10 @@ 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 ("declared input keys changed or are unavailable; run loopzero check at this head. " + "If keys remain unavailable, ensure the check base is available and " + "declared paths select only readable regular files.") return None diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index f74eed7e..b69078ee 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -253,8 +253,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 is not None + and result.input_key == _input_key(config, worktree, result.command, base) + for result in report.results if _keyed_stage(config, result.command)) + 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("*", ())): + 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 6933ecd3..66d3251f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3128,6 +3128,41 @@ def test_check_refuses_unverified_receipt_after_runner_retarget(wt, gh, capsys, assert run(capsys, "ready")[0] == 0 +@pytest.mark.parametrize("keyable", [True, False]) +def test_ready_and_merge_reject_receipt_after_clean_checkout_rewrites_input( + wt, gh, capsys, tmp_path, keyable, +): + path = wt / "feature.py" + if not keyable: + (wt / "link").symlink_to(path.name) + git(wt, "add", "link") + git(wt, "commit", "-qm", "add tracked symlink") + head = 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())'""" + inputs = f'{json.dumps(command)} = ["feature.py"]' if keyable else '"*" = ["."]' + settings = tmp_path / "workflow.toml" + settings.write_text(WORKFLOW.replace('"echo ok"', json.dumps(command)).replace( + "[delivery]", f'[checks.inputs]\n{inputs}\n\n[delivery]', + )) + args = ("--config", str(settings)) + assert run(capsys, *args, "check")[0] == 0 + code, out, _ = run(capsys, *args, "ready") + assert code == (0 if keyable else 1) + if not keyable: + assert "declared paths select only readable regular files" in out + + 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 "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: