diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 61abd317..a8cb0d48 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -545,10 +545,11 @@ the authoritative `checks.json` receipt. Checks lock the task state directory through execution and receipt publication. A waiter can reuse a newly published passing receipt when HEAD, settings and declared inputs match. Writers recheck HEAD and atomically replace the receipt from a synced sibling temporary file. -Successful keyed stages are checkpointed in the same receipt -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 +Successful keyed stages are checkpointed in the same receipt for reuse. +A successful stage checkpoint preserves a clean pass for the same head, +settings and commands. A failed stage replaces an existing receipt immediately. +Final worktree validation is required before publishing a pass. 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 d06172dc..ba7f257d 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -429,7 +429,9 @@ def _save_report( 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: + def preserve_checkpoint() -> bool: + if report.results[-1] is not sandbox.INCOMPLETE_CHECK: + return not worktree.local_state_files(wt, CHECKS_FILE.name)[0] 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 @@ -437,7 +439,7 @@ def preserve_pass() -> bool: return worktree.write_local_state( wt, CHECKS_FILE.name, json.dumps(payload, indent=2) + "\n", expected_head=report.head, locked_fd=locked_fd, - preserve=preserve_pass if checkpoint_checks is not None else None, + preserve=preserve_checkpoint if checkpoint_checks is not None else None, ) diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index dd8a9df6..b4578b03 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -140,11 +140,10 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO result = replace(result, input_key=key) if ordinal > len(preparations) or result.exit_code: results.append(result) - if checkpoint is not None and result.input_key and result.exit_code == 0: - # The receipt stays nonpassing until final validation. - incomplete = CheckResult(INCOMPLETE_CHECK_COMMAND, 1, 0.0, "") - unvisited = (previous[stage] for stage in commands[ordinal:] if stage in previous) - checkpoint(CheckReport(head, dirty, (*results, *unvisited, incomplete), settings_sha256)) + _checkpoint_stage( + checkpoint, CheckReport(head, dirty, tuple(results), settings_sha256), + previous, commands[ordinal:], + ) if status: status(f"stage {ordinal}/{len(commands)}: exit {result.exit_code} " f"({result.duration_s:.1f}s)") @@ -167,6 +166,21 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO INCOMPLETE_CHECK_COMMAND = "" +INCOMPLETE_CHECK = CheckResult(INCOMPLETE_CHECK_COMMAND, 1, 0.0, "") + + +def _checkpoint_stage(checkpoint: Callable[[CheckReport], None] | None, + report: CheckReport, previous: dict[str, CheckResult], + remaining: tuple[str, ...]) -> None: + if checkpoint is None: + return + stage = report.results[-1] + if stage.exit_code: + checkpoint(report) + elif stage.input_key: + # Even the last stage leaves a nonpassing receipt until final validation. + unvisited = tuple(previous[command] for command in remaining if command in previous) + checkpoint(replace(report, results=(*report.results, *unvisited, INCOMPLETE_CHECK))) def _previous(worktree: Path, settings_sha256: str) -> dict[str, CheckResult]: diff --git a/tests/test_cli.py b/tests/test_cli.py index f06bb890..2ef69ecb 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4775,8 +4775,7 @@ def run_checks(cfg, path, *, progress, status, checkpoint, workflow): locked_fd = checkpoint.keywords["locked_fd"] 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)) + partial = replace(passed, results=(results[0], cli.sandbox.INCOMPLETE_CHECK)) checkpoint(partial) assert cli._load_report(wt) == partial cli._save_report(wt, passed, locked_fd=locked_fd) @@ -4792,3 +4791,35 @@ def run_checks(cfg, path, *, progress, status, checkpoint, workflow): code, _, err = run(capsys, "check") assert code == 1 and "(exit 1)" in err, err assert cli._load_report(wt).results[0].exit_code == 1 + +def test_ready_rejects_check_interrupted_after_a_failed_stage(wt, head, gh, capsys, monkeypatch): + settings = wt / ".loopzero" / "workflow.toml" + settings.write_text(WORKFLOW.replace("test -f README.md", "test ! -e .loopzero/fail")) + args = ["--config", str(settings)] + assert cli.main([*args, "check"]) == 0 + (wt / ".loopzero" / "fail").touch() + run_one, git_check = cli.sandbox._run_one, cli.sandbox._git + failed = False + + def observe_stage(*stage_args): + nonlocal failed + result = run_one(*stage_args) + failed = result.exit_code != 0 + return result + + def interrupt_final_validation(*git_args, **kwargs): + if failed: + raise KeyboardInterrupt + return git_check(*git_args, **kwargs) + + with monkeypatch.context() as patch: + patch.setattr(cli.sandbox, "_run_one", observe_stage) + patch.setattr(cli.sandbox, "_git", interrupt_final_validation) + assert cli.main([*args, "check"]) == 130 + + assert failed + arm_approved_pr_with_check_result( + gh, head, wt, "success", isDraft=False, readiness={"local_exit": None}, + ) + assert cli.main([*args, "ready"]) == 1 + assert "check receipt records a failed stage" in capsys.readouterr().out