diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 5140e395..8da6881c 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -435,8 +435,9 @@ index, README and AGENTS guidance without network access. The repository's CI enforce it against the event's base SHA. Push-to-main CI runs `--links-only`, because squash merging can drop authored trailers and landed history cannot re-establish the pre-merge acknowledgement; that mode still rejects broken local links and anchors. -`loopzero check` pins checks to trusted base configuration, so run a new or -changed gate directly until its configuration lands on the base. +`loopzero check` pins checks to trusted base configuration and reuses a passing +receipt when readiness validation, the clean checkout and declared inputs still +match. Run a new or changed gate directly until its configuration lands on the base. The checker watches source-owned contracts, skill routing, the example and actual workflow, hosted workflow files, itself, and selected CLI/configuration interfaces; diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index f3c59c09..fe02e622 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -624,10 +624,28 @@ def cmd_start(args: argparse.Namespace) -> int: return 0 +def _reusable_check_report(wt: Path, config: Config) -> CheckReport | None: + if not (wt / CHECKS_FILE).is_file() or worktree.is_dirty(wt): + return None + head = worktree.head(wt) + with suppress(OSError): + if not sandbox.has_untracked_task_state(config, wt): + return None + report = _current_check_report(wt, config, head) + if report is not None and sandbox.receipt_checkout_matches(config, wt, report): + return report + return None + + def cmd_check(args: argparse.Namespace) -> int: wt, config = _context(args, pin_checks=True) if not config.checks: raise config_mod.ConfigError("checks.commands must list at least one command to run checks") + if (receipt := _reusable_check_report(wt, config)) is not None: + _print_receipt(wt, receipt, f"reused from {CHECKS_FILE}") + _refresh_pr_checks(wt, config) + print("PASS") + return 0 try: report = sandbox.run_checks(config, wt, progress=lambda command: print( f"running: {command}", flush=True), diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index 5edb2449..fd706ea9 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -183,8 +183,22 @@ def _checkpoint_success(checkpoint: Callable[[CheckReport], None] | None, checkpoint(replace(report, results=(*report.results, *unvisited, incomplete))) +def has_untracked_task_state(config: Config, worktree: Path) -> bool: + return not _git(config, worktree, "ls-files", "--", ".loopzero") + + +def receipt_checkout_matches(config: Config, worktree: Path, report: CheckReport) -> bool: + if not _stage_unchanged(config, worktree, report.head): + return False + 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 _has_inputs(config, result.command) + ) and _stage_unchanged(config, worktree, report.head) + + def _previous(config: Config, worktree: Path, settings_sha256: str) -> dict[str, CheckResult]: - if _git(config, worktree, "ls-files", "--", ".loopzero"): + if not has_untracked_task_state(config, worktree): return {} try: data = json.loads((worktree / ".loopzero/checks.json").read_text()) @@ -271,10 +285,14 @@ def _input_blobs(config: Config, worktree: Path, paths: tuple[str, ...]) -> str raise +def _has_inputs(config: Config, command: str) -> bool: + return command in config.inputs or "." in config.inputs.get("*", ()) + + def _input_key(config: Config, worktree: Path, command: str, base: str, *, status: Callable[[str], None] | None = None, workflow: str | Path = "workflow.toml") -> str | None: - if command not in config.inputs and "." not in config.inputs.get("*", ()): + if not _has_inputs(config, command): return None if not base and status is None: return None diff --git a/tests/test_cli.py b/tests/test_cli.py index f679c279..51183459 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -344,25 +344,25 @@ def test_start_without_config(git_repo: Path, monkeypatch, capsys) -> None: assert code == 1 and "workflow.toml" in err and err.count("\n") == 1 -def test_check_pass_writes_report(wt: Path, gh: FakeGh, capsys) -> None: +def test_check_pass_writes_and_reuses_report(wt, gh, capsys, monkeypatch): code, out, _ = run(capsys, "check") - assert gh.calls == [] - lines = out.splitlines() - assert code == 0 and lines[-1] == "PASS" - assert lines[0].startswith("checks pinned to origin/main@") - assert lines[1] == "running: echo ok" - assert lines[2].startswith("stage 1/2: exit 0 (") - assert lines[3] == "running: test -f README.md" - assert lines[4].startswith("stage 2/2: exit 0 (") - assert sum("exit 0" in line for line in lines) == 2 - report = json.loads((wt / ".loopzero" / "checks.json").read_text()) - assert report["head"] == head_of(wt) - assert report["dirty"] is False - assert f"receipt produced: head={report['head']} content=" in out - assert "config=commands-sha256:" in out and "environment=unknown" in out - assert [(r["command"], r["exit_code"]) for r in report["results"]] == [ - ("echo ok", 0), ("test -f README.md", 0)] - assert report["results"][0]["tail"] == "ok" + pin, first, first_exit, second, second_exit, receipt, verdict = out.splitlines() + assert code == 0 and verdict == "PASS" and not gh.calls + assert pin.startswith("checks pinned to origin/main@") + assert (first, second) == ("running: echo ok", "running: test -f README.md") + assert first_exit.startswith("stage 1/2: exit 0 (") and second_exit.startswith("stage 2/2: exit 0 (") + path = wt / ".loopzero/checks.json" + report = json.loads(original := path.read_bytes()) + assert report["head"] == head_of(wt) and report["dirty"] is False + assert receipt.startswith(f"receipt produced: head={report['head']} content=") + assert "config=commands-sha256:" in receipt and "environment=unknown" in receipt + first_result, second_result = report["results"] + assert (first_result["command"], first_result["exit_code"], first_result["tail"]) == ("echo ok", 0, "ok") + assert (second_result["command"], second_result["exit_code"]) == ("test -f README.md", 0) + monkeypatch.setattr(cli.sandbox, "run_checks", Mock(side_effect=AssertionError("stages ran"))) + code, out, err = run(capsys, "check") + reused = receipt.replace("receipt produced", "receipt reused from .loopzero/checks.json", 1) + assert (code, out.splitlines(), err, path.read_bytes()) == (0, [pin, reused, "PASS"], "", original) @pytest.mark.parametrize("state", [None, "CLOSED", "MERGED"]) @@ -2295,8 +2295,8 @@ def test_passing_check_discarded_after_sandbox_when_head_moves(wt, gh, capsys, m refreshed = [] monkeypatch.setattr(cli, "_refresh_pr_checks", lambda *_: refreshed.append(head_of(wt))) assert run(capsys, "check")[0] == 0 - original_receipt = receipt.read_bytes() - + original_receipt = receipt.read_bytes().replace(b'"settings_sha256"', b'"obsolete_settings_sha256"') + receipt.write_bytes(original_receipt) save_report = cli._save_report def advance_before_save(path, report): diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index ecff7435..8e444ba1 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -832,22 +832,22 @@ def timed_out(*args): assert report.ok and report.dirty and report.results[0].command == command -def test_local_check_reuse_tracks_checkout_bytes(git_repo, bwrap_log, fetched_base, cache_check): +def test_local_check_reuse_tracks_checkout_bytes(git_repo, bwrap_log, fetched_base, cache_check, monkeypatch): path = git_repo / "README.md" command = "python3 -c 'from pathlib import Path; exit(13 in Path(\"README.md\").read_bytes())'" config = make_config(checks=(command,), inputs={command: ("README.md",)}) assert path.read_bytes() == b"hello\n" assert cache_check(config, clean=True, verify_reuse=True).head == fetched_base - + monkeypatch.setattr(cli, "_context", lambda *args, **kwargs: (git_repo, config)) git(git_repo, "config", "core.autocrlf", "true") path.unlink() git(git_repo, "checkout-index", "-u", "--", path.name) assert path.read_bytes() == b"hello\r\n" assert not git(git_repo, "status", "--porcelain") - report = sandbox.run_checks(config, git_repo) + assert cli.main(["check"]) == 1 + report = cli._load_report(git_repo) assert not report.dirty and report.head == fetched_base - assert report.results[0].exit_code == 1 - assert report.results[0].reused_from is None + assert (report.results[0].exit_code, report.results[0].reused_from) == (1, None) @pytest.mark.parametrize("recorded_base", [False, True]) @@ -940,7 +940,7 @@ def test_local_checks_run_fresh_without_a_base(git_repo, bwrap_log, cache_check) @pytest.mark.parametrize('symlink', [False, True]) -def test_tracked_forged_receipt_runs_failing_stage(git_repo, bwrap_log, fetched_base, symlink): +def test_tracked_forged_receipt_runs_failing_stage(git_repo, bwrap_log, fetched_base, symlink, monkeypatch): from loopzero.types import CheckReport, CheckResult command = 'echo stage-ran; exit 7' @@ -958,12 +958,12 @@ def test_tracked_forged_receipt_runs_failing_stage(git_repo, bwrap_log, fetched_ git(git_repo, 'commit', '-m', 'forge passing receipt') assert not git(git_repo, 'status', '--porcelain').strip() assert key == sandbox._input_key(cfg, git_repo, command, fetched_base) - + receipt_reader = Mock(side_effect=AssertionError("tracked receipt opened")) + monkeypatch.setattr(cli, "_current_check_report", receipt_reader) + assert cli._reusable_check_report(git_repo, cfg) is None report = sandbox.run_checks(cfg, git_repo) - assert not report.dirty - assert report.results[0].exit_code == 7 + assert not report.dirty and (report.results[0].exit_code, report.results[0].reused_from) == (7, None) assert 'stage-ran' in report.results[0].tail - assert not report.results[0].reused_from def test_no_inputs_preserves_receipt(git_repo, bwrap_log):