diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 1c4f145e..fc3fa53c 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -538,8 +538,9 @@ stage is retried after success or uncertain launch. Only the coordinator writes the authoritative `checks.json` receipt. Writers lock the task state directory while rechecking HEAD and atomically replacing the receipt from a synced sibling temporary file. Successful keyed stages are checkpointed in the same receipt -for reuse. The checkpoint cannot satisfy readiness before final worktree -validation. Runners cannot select stages or mint receipts. Stage admission +for reuse. A checkpoint cannot replace a clean pass for the same head and +settings or satisfy readiness before final worktree validation. Completed +failures still replace passes. Runners cannot select stages or mint receipts. Stage admission 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. diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 3ac7a1c5..cd54400d 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -418,7 +418,7 @@ def _print_receipt(wt: Path, report: CheckReport, action: str) -> None: return -def _save_report(wt: Path, report: CheckReport) -> str | None: +def _save_report(wt: Path, report: CheckReport, *, checkpoint_checks: tuple[str, ...] | None = None) -> str | None: """Publish the receipt or return the current HEAD if the result is stale.""" results = [ {k: v for k, v in dataclasses.asdict(result).items() @@ -428,8 +428,15 @@ def _save_report(wt: Path, report: CheckReport) -> str | None: payload = {"head": report.head, "dirty": report.dirty, "results": results} if report.settings_sha256 is not None: payload["settings_sha256"] = report.settings_sha256 + def preserve_pass() -> bool: + saved = _load_report(wt) + return (saved is not None and saved.ok and not saved.dirty and saved.head == report.head + and saved.settings_sha256 == report.settings_sha256 + and tuple(result.command for result in saved.results) == checkpoint_checks) + return worktree.write_local_state( wt, CHECKS_FILE.name, json.dumps(payload, indent=2) + "\n", expected_head=report.head, + preserve=preserve_pass if checkpoint_checks is not None else None, ) @@ -630,7 +637,8 @@ def cmd_check(args: argparse.Namespace) -> int: raise config_mod.ConfigError("checks.commands must list at least one command to run checks") report = sandbox.run_checks( config, wt, progress=lambda command: print(f"running: {github.redact_credentials(command)}", flush=True), - status=lambda message: print(message, flush=True), checkpoint=lambda partial: _save_report(wt, partial), + status=lambda message: print(message, flush=True), + checkpoint=lambda partial: _save_report(wt, partial, checkpoint_checks=config.checks), workflow=args.config or f"origin/{config.base_branch}:workflow.toml", ) shortened = False diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index 8916bc2a..4d38c4de 100644 --- a/src/loopzero/worktree.py +++ b/src/loopzero/worktree.py @@ -9,6 +9,7 @@ import stat import subprocess from collections.abc import Iterator +from collections.abc import Callable from contextlib import contextmanager, suppress from pathlib import Path from select import select @@ -147,12 +148,15 @@ def read_local_state(wt: Path, name: str) -> str: def write_local_state( wt: Path, name: str, text: str, *, private: bool = False, exclusive: bool = False, - expected_head: str | None = None, + expected_head: str | None = None, preserve: Callable[[], bool] | None = None, ) -> str | None: - with _local_state_directory(wt, create=True, lock=private or expected_head is not None) as fd: + lock = private or expected_head is not None or preserve is not None + with _local_state_directory(wt, create=True, lock=lock) as fd: if expected_head is not None and (current_head := head(wt)) != expected_head: return current_head _regular_state(wt, fd, name) + if preserve is not None and preserve(): + return None if private: os.fchmod(fd, 0o700) temp = f".{name}-{uuid4().hex}" diff --git a/tests/test_cli.py b/tests/test_cli.py index e790e384..c3edef52 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4814,3 +4814,26 @@ def test_pr_marks_legacy_same_head_check_unverified(wt, gh, capsys, refresh): draft = json.loads(gh.calls[-1]["--input"])["body"] assert "unverified" not in draft assert_contains(draft, f"Recorded for `{head[:12]}`\n- `echo ok`: exit 0", "Manual validation evidence.") + + +def test_late_checkpoint_preserves_completed_pass(wt, gh, capsys, head, monkeypatch): + def run_checks(cfg, path, *, progress, status, checkpoint, workflow): + results = tuple(CheckResult(c, 0, 0, "", input_key="key") for c in cfg.checks) + passed = CheckReport(head, False, results, cli.sandbox.settings_fingerprint(cfg)) + incomplete = CheckResult(cli.sandbox.INCOMPLETE_CHECK_COMMAND, 1, 0.0, "") + partial = replace(passed, results=(results[0], incomplete)) + checkpoint(partial) + assert cli._load_report(wt) == partial + cli._save_report(wt, passed) + checkpoint(partial) + assert cli._current_check_report(wt, cfg, head) == passed + cli._save_report(wt, replace(passed, results=results[:1])) + checkpoint(partial) + assert cli._load_report(wt) == partial + cli._save_report(wt, passed) + return replace(passed, results=(replace(results[0], exit_code=1),)) + + monkeypatch.setattr(cli.sandbox, "run_checks", run_checks) + code, _, err = run(capsys, "check") + assert code == 1 and "(exit 1)" in err, err + assert cli._load_report(wt).results[0].exit_code == 1