From 717403809eddedb679d9fc0063f2d514ab7cc5d3 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:39:34 +0100 Subject: [PATCH 1/3] Preserve passing receipts across late check checkpoints Architecture-Review: document which completed receipts a checkpoint may replace. Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 5 +++-- src/loopzero/cli.py | 9 +++++++-- tests/test_cli.py | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index bfe286f2..9e50b205 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -481,8 +481,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 46a93e29..1cf1b68b 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -359,7 +359,7 @@ def _lock_task_state(wt: Path) -> Iterator[None]: os.close(fd) -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.""" path = wt / CHECKS_FILE payload = { @@ -375,6 +375,11 @@ def _save_report(wt: Path, report: CheckReport) -> str | None: current_head = worktree.head(wt) if report.head != current_head: return current_head + saved = _load_report(wt) if checkpoint_checks is not None else None + if (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 None _write_json_atomically(path, payload) return None @@ -598,7 +603,7 @@ def cmd_check(args: argparse.Namespace) -> int: report = sandbox.run_checks(config, wt, progress=lambda command: print( f"running: {command}", flush=True), status=lambda message: print(message, flush=True), - checkpoint=lambda partial: _save_report(wt, partial)) + checkpoint=lambda partial: _save_report(wt, partial, checkpoint_checks=config.checks)) except sandbox.SandboxUnavailable as exc: print(f"sandbox unavailable: {exc}", file=sys.stderr) return 2 diff --git a/tests/test_cli.py b/tests/test_cli.py index 3421662b..037c073b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -10,6 +10,7 @@ import subprocess import sys from dataclasses import replace +from functools import partial from pathlib import Path from types import SimpleNamespace from unittest.mock import Mock @@ -4763,3 +4764,22 @@ def test_pr_marks_legacy_same_head_check_unverified(wt, gh, capsys): assert "unverified" not in draft assert f"Recorded for `{head[:12]}`\n- `echo ok`: exit 0" in draft assert "Manual validation evidence." in draft + + +def test_late_checkpoint_preserves_completed_pass(wt, head): + cfg = config.load(wt / "workflow.toml") + passed = CheckReport(head, False, tuple(CheckResult(c, 0, 0, "", input_key="key") for c in cfg.checks), + cli.sandbox.settings_fingerprint(cfg, wt)) + save = partial(cli._save_report, wt) + checkpoint = cli.sandbox._checkpoint_success + checkpoint(partial(save, checkpoint_checks=cfg.checks), passed, {}, ()) + assert cli._load_report(wt).results[0].input_key == "key" + save(passed) + checkpoint(partial(save, checkpoint_checks=cfg.checks), passed, {}, ()) + assert cli._current_check_report(wt, cfg, head) == passed + failed = replace(passed, results=(replace(passed.results[0], exit_code=1),)) + save(failed) + assert cli._load_report(wt) == failed + save(passed) + checkpoint(partial(save, checkpoint_checks=cfg.checks[:1]), replace(passed, results=passed.results[:1]), {}, ()) + assert not cli._load_report(wt).ok From e03566dc3eab05604d30b90b45bae976f6d9303a Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:49:24 +0100 Subject: [PATCH 2/3] Persist observed check failures before final validation Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 7 ++++--- src/loopzero/cli.py | 3 ++- src/loopzero/sandbox.py | 20 +++++++++++++------- tests/test_cli.py | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 9e50b205..128e3f52 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -481,9 +481,10 @@ 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. 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 +for reuse. A successful stage checkpoint preserves a clean pass for the same +head, settings, and commands. A failed stage replaces a prior pass 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 1cf1b68b..5c0624eb 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -375,7 +375,8 @@ def _save_report(wt: Path, report: CheckReport, *, checkpoint_checks: tuple[str, current_head = worktree.head(wt) if report.head != current_head: return current_head - saved = _load_report(wt) if checkpoint_checks is not None else None + unfinished = checkpoint_checks is not None and report.results[-1] is sandbox.INCOMPLETE_CHECK + saved = _load_report(wt) if unfinished else None if (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): diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index a358d56f..7db4d228 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -193,7 +193,7 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO result = replace(result, input_key=key) if index >= len(preparations) or result.exit_code: results.append(result) - _checkpoint_success( + _checkpoint_stage( checkpoint, CheckReport(head, dirty, tuple(results), settings_sha256), previous, commands[index + 1:], ) @@ -218,15 +218,21 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO return CheckReport(head=head, dirty=dirty, results=tuple(results), settings_sha256=settings_sha256) -def _checkpoint_success(checkpoint: Callable[[CheckReport], None] | None, - report: CheckReport, previous: dict[str, CheckResult], - remaining: tuple[str, ...]) -> None: +INCOMPLETE_CHECK = CheckResult("", 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 checkpoint is not None and stage.input_key and stage.exit_code == 0: + if stage.exit_code: + checkpoint(report) + elif stage.input_key: # Even the last stage leaves a nonpassing receipt until final validation. - incomplete = CheckResult("", 1, 0.0, "") unvisited = tuple(previous[command] for command in remaining if command in previous) - checkpoint(replace(report, results=(*report.results, *unvisited, incomplete))) + checkpoint(replace(report, results=(*report.results, *unvisited, INCOMPLETE_CHECK))) def _previous(config: Config, worktree: Path, settings_sha256: str) -> dict[str, CheckResult]: diff --git a/tests/test_cli.py b/tests/test_cli.py index 037c073b..de496d04 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4766,12 +4766,45 @@ def test_pr_marks_legacy_same_head_check_unverified(wt, gh, capsys): assert "Manual validation evidence." in draft +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): + if failed: + raise KeyboardInterrupt + return git_check(*git_args) + + 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 "not ready: run loopzero check at this head" in capsys.readouterr().out + + def test_late_checkpoint_preserves_completed_pass(wt, head): cfg = config.load(wt / "workflow.toml") passed = CheckReport(head, False, tuple(CheckResult(c, 0, 0, "", input_key="key") for c in cfg.checks), cli.sandbox.settings_fingerprint(cfg, wt)) save = partial(cli._save_report, wt) - checkpoint = cli.sandbox._checkpoint_success + checkpoint = cli.sandbox._checkpoint_stage checkpoint(partial(save, checkpoint_checks=cfg.checks), passed, {}, ()) assert cli._load_report(wt).results[0].input_key == "key" save(passed) From 6712072debf467a1927db16ee5bba8921223c9ab Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 04:01:00 +0100 Subject: [PATCH 3/3] Keep an initial failed check from changing its own worktree status Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 2 +- src/loopzero/cli.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 128e3f52..a39cf1af 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -482,7 +482,7 @@ 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. A successful stage checkpoint preserves a clean pass for the same -head, settings, and commands. A failed stage replaces a prior pass immediately. +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` diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 5c0624eb..5d3f625b 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -376,6 +376,8 @@ def _save_report(wt: Path, report: CheckReport, *, checkpoint_checks: tuple[str, if report.head != current_head: return current_head unfinished = checkpoint_checks is not None and report.results[-1] is sandbox.INCOMPLETE_CHECK + if checkpoint_checks is not None and not unfinished and not path.exists(): + return None saved = _load_report(wt) if unfinished else None if (saved is not None and saved.ok and not saved.dirty and saved.head == report.head and saved.settings_sha256 == report.settings_sha256