Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Docs no longer say a checkpoint cannot satisfy readiness

The resolved paragraph drops main's sentence that a checkpoint cannot "satisfy readiness before final worktree validation". It keeps "Final worktree validation is required before publishing a pass." The behaviour has not changed: _checkpoint_stage (sandbox.py:183) still appends INCOMPLETE_CHECK, and _check_report_problem (cli.py:384) rejects that marker. But a reader now has to infer that an intermediate checkpoint never satisfies readiness. Optional fix: add a short clause such as "An intermediate checkpoint is never a pass."

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.
Expand Down
6 changes: 4 additions & 2 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,17 @@ 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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Checkpoint kind is detected by object identity with the INCOMPLETE_CHECK sentinel

preserve_checkpoint tells a success checkpoint from a failure checkpoint with report.results[-1] is not sandbox.INCOMPLETE_CHECK. This works today because _checkpoint_stage and the updated test both pass the module-level singleton. It breaks if any caller builds an equal CheckResult(INCOMPLETE_CHECK_COMMAND, 1, 0.0, ""), which is exactly how the pre-change test built it. That checkpoint would then be treated as negative evidence and overwrite a matching completed pass, which reintroduces the #1368 regression without any visible error. Smallest fix: compare report.results[-1].command == sandbox.INCOMPLETE_CHECK_COMMAND, matching what _check_report_problem already does at cli.py:385.

return not worktree.local_state_files(wt, CHECKS_FILE.name)[0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: A failure checkpoint is skipped when no receipt exists; this rule is implicit

For a failed stage, preserve_checkpoint returns not local_state_files(...)[0], so the early write only happens when a receipt already exists. Without a receipt, ready refuses anyway, so this is safe. Still, the condition is non-obvious, and the ARCHITECTURE.md wording ("replaces an existing receipt immediately") is the only explanation. A short comment at this branch, or a mention in the docstring, would stop a future reader from treating it as a bug and 'fixing' it to always write or never write.

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, 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,
)


Expand Down
24 changes: 19 additions & 5 deletions src/loopzero/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand All @@ -167,6 +166,21 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO


INCOMPLETE_CHECK_COMMAND = "<checks incomplete>"
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]:
Expand Down
35 changes: 33 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Loading