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
5 changes: 3 additions & 2 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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: Reflow the edited paragraph

The new sentence leaves failures still replace passes. Runners cannot select stages or mint receipts. Stage admission on one line that is much longer than the surrounding ~80-column wrap. Reflow the paragraph so it matches the rest of the file.

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
12 changes: 10 additions & 2 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
)


Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/loopzero/worktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import stat
import subprocess
from collections.abc import Iterator
from collections.abc import Callable

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: Merge the new Callable import into the existing collections.abc import

The diff adds a second line, from collections.abc import Callable, right below from collections.abc import Iterator. It works, but it is redundant style. cli.py already uses the combined form (from collections.abc import Callable, Iterator). Smallest fix: use one line, from collections.abc import Callable, Iterator.

from contextlib import contextmanager, suppress
from pathlib import Path
from select import select
Expand Down Expand Up @@ -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}"
Expand Down
23 changes: 23 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

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: Test silently depends on workflow.toml declaring at least two checks

The 'commands differ' step saves replace(passed, results=results[:1]) and then expects the checkpoint to overwrite it. If the test fixture's workflow ever lists only one check, results[:1] equals passed. The checkpoint would then preserve it, and the test would fail with no clear reason. Add assert len(cfg.checks) > 1 at the top of the fake run_checks so the precondition is explicit.



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
Loading