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
17 changes: 15 additions & 2 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,22 @@ def _readiness(
def _check_report_problem(
report: CheckReport | None, wt: Path, config: Config, head: str, *, require_pass: bool = True,
) -> str | None:
if (report is None or report.head != head or report.dirty or (require_pass and not report.ok)
or tuple(result.command for result in report.results) != config.checks):
if report is None:
return "run loopzero check at this head"
if report.head != head:
return f"check receipt is for {report.head[:12]}. Run `loopzero check` at {head[:12]}."
if report.dirty:
return ("check receipt was recorded in a dirty worktree. Run `git status --short`, "
"settle the changes, then run `loopzero check`.")
commands = tuple(result.command for result in report.results)
if (require_pass and not report.ok) or commands != config.checks:
if not commands or commands[-1] == sandbox.INCOMPLETE_CHECK_COMMAND:
return "check receipt is incomplete. Run `loopzero check` to resume with the current workflow."
if require_pass and not report.ok:
return ("check receipt records a failed stage. Fix the failure, then run `loopzero check`. "
"Retry unchanged only if the failure was transient.")
return ("check receipt's plan differs from the trusted workflow. "
"Run `loopzero check` with the current workflow.")
recovery = ("Run loopzero check in this worktree at this head with the same current CLI and effective "
"settings as ready, using a local coordinator for delegated stages.")
if report.settings_sha256 is None:
Expand Down
5 changes: 4 additions & 1 deletion src/loopzero/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,16 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO
return CheckReport(head=head, dirty=dirty, results=tuple(results), settings_sha256=settings_sha256)


INCOMPLETE_CHECK_COMMAND = "<checks incomplete>"


def _checkpoint_success(checkpoint: Callable[[CheckReport], None] | None,
report: CheckReport, previous: dict[str, CheckResult],
remaining: tuple[str, ...]) -> None:
stage = report.results[-1]
if checkpoint is not None and stage.input_key and stage.exit_code == 0:
# Even the last stage leaves a nonpassing receipt until final validation.
incomplete = CheckResult("<checks incomplete>", 1, 0.0, "")
incomplete = CheckResult(INCOMPLETE_CHECK_COMMAND, 1, 0.0, "")
unvisited = tuple(previous[command] for command in remaining if command in previous)
checkpoint(replace(report, results=(*report.results, *unvisited, incomplete)))

Expand Down
35 changes: 20 additions & 15 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import subprocess
import sys
from contextlib import suppress
from dataclasses import replace
from dataclasses import asdict, replace
from pathlib import Path
from select import select
from types import SimpleNamespace
Expand Down Expand Up @@ -3022,7 +3022,7 @@ def test_ready_requires_clean_successful_local_report_at_head(
if condition == "wrong-head":
report["head"] = "0" * 40
elif condition == "wrong-commands":
report["results"][0]["command"] = "true"
report["results"].pop()
elif condition == "legacy":
del report["settings_sha256"]
else:
Expand All @@ -3036,8 +3036,10 @@ def test_ready_requires_clean_successful_local_report_at_head(
assert f"not ready: {reason}." in out
assert ("Run loopzero check in this worktree at this head with the same current CLI and effective "
"settings as ready, using a local coordinator for delegated stages." in out)
elif condition == "wrong-commands":
assert "not ready: check receipt's plan differs from the trusted workflow." in out
elif condition != "matching":
assert out.endswith("not ready: run loopzero check at this head\n")
assert ("not ready: run loopzero check at this head" if condition == "missing" else "not ready: check receipt") in out


@pytest.mark.parametrize("wait_for", [None, "ready", "merge"])
Expand Down Expand Up @@ -3765,7 +3767,7 @@ def test_status_silently_ignores_legacy_report_without_dirty(

assert code == 0 and err == ""
assert "ready: no" in out
assert " - run loopzero check at this head" in out
assert " - check receipt was recorded in a dirty worktree." in out


def test_gh_errors_are_one_line_on_stderr(wt: Path, gh: FakeGh, capsys) -> None:
Expand Down Expand Up @@ -4595,22 +4597,25 @@ def broken(*args, **kwargs):
assert run(capsys, command)[0] == 0


@pytest.mark.parametrize("invalid", ["stale", "red", "dirty", "commands"])
def test_invalid_receipt_is_not_announced(wt, gh, capsys, head, invalid):
@pytest.mark.parametrize("field,value,reason", [
("head", "a" * 40, "check receipt is for aaaaaaaaaaaa."),
("exit_code", 1, "check receipt records a failed stage."),
("dirty", True, "check receipt was recorded in a dirty worktree."),
("command", "changed", "check receipt's plan differs from the trusted workflow."),
("results", [], "check receipt is incomplete."),
("results", "checkpoint", "check receipt is incomplete."),
])
def test_invalid_receipt_is_not_announced(wt, gh, capsys, head, field, value, reason):
arm_approved_ready_pr(gh, head, wt)
path = wt / ".loopzero/checks.json"
data = json.loads(path.read_text())
if invalid == "stale":
data["head"] = "a" * 40
elif invalid == "dirty":
data["dirty"] = True
elif invalid == "red":
data["results"][0]["exit_code"] = 1
else:
data["results"][0]["command"] = "changed"
target = data["results"][0] if field in {"exit_code", "command"} else data
if value == "checkpoint":
value = [data["results"][0], asdict(CheckResult("<checks incomplete>", 1, 0.0, ""))]
target[field] = value
path.write_text(json.dumps(data))
code, out, _ = run(capsys, "ready")
assert code == 1 and "run loopzero check at this head" in out
assert code == 1 and f"not ready: {reason}" in out
assert "receipt accepted:" not in out

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: Assert the remedy for each rejected receipt

The parameterized test checks each rejection reason, but it would still pass if the recovery instructions disappeared. Add a short expected action to each case, such as git status --short for a dirty receipt and resume with the current workflow for a checkpoint, so the test covers the actionable part of the message.


Expand Down
Loading