Skip to content
Closed
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: 9 additions & 8 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,15 @@ 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.

Declared `[checks.inputs]` pathspecs opt commands into local passing-result reuse.
Keys bind committed paths/blob IDs, the bytes of the checked-out regular files,
command, version, sandbox configuration and the resolved base SHA. A stage runs
fresh when a matched path cannot be identified as a readable regular file.
The receipt fingerprint also binds the base SHA when reuse
is configured, so advancing the base invalidates a pass at the same task head.
If the remote ref is absent, checks use the task's recorded base. Without
either base, local stages run fresh.
Declared `[checks.inputs]` pathspecs opt commands into local passing-result reuse
and bind their receipts for readiness and merge. Keys cover committed paths and
blob IDs, the bytes of the checked-out regular files, the command, version,
sandbox configuration and the resolved base SHA. Stages run fresh when keys
are unavailable. Readiness and merge require matching keys for declared stages
even after a successful check. Restore an available base or narrow oversized
pathspecs and declarations that match unreadable or non-regular paths, then rerun
`loopzero check`. The fingerprint also binds the base SHA when reuse is configured.
If the remote ref is absent, checks use the task's recorded base.
The `"*"` entry adds shared inputs to listed commands. Unlisted commands reuse only
when `"*"` includes `"."` (the whole tree); a partial or empty `"*"` leaves them unkeyed.
Consumers own complete input declarations.
Expand Down
4 changes: 4 additions & 0 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ def _check_report_problem(
return f"check receipt is missing its settings fingerprint. {recovery}"
if report.settings_sha256 != sandbox.settings_fingerprint(config, wt):
return f"check receipt has an incompatible settings fingerprint. {recovery}"
if not sandbox.receipt_inputs_match(config, wt, report):
Comment thread
eowca marked this conversation as resolved.
Resolved
return ("declared input keys changed or are unavailable; run loopzero check at this head. "

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: Recovery guidance omits the oversized-pathspec cause

_input_key also returns None when git raises E2BIG for too many pathspecs. In that case the recovery message ('ensure the check base is available and declared paths select only readable regular files') does not point at the real cause. Consider mentioning that the declarations should be reduced, or reusing the recovery wording above it so the guidance stays in one place.

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: The ready/merge error message leaves out the oversized-pathspec recovery that the docs now describe

The new text in docs/ARCHITECTURE.md lists three ways to recover when keys are unavailable: restore the base, narrow oversized pathspecs, or narrow declarations that match unreadable or non-regular paths. The code supports all three: _input_key (sandbox.py:279-281) returns None on E2BIG, which is what happens when the pathspec list is too long.

The error message users actually see, at cli.py:330-332, only covers the base and the readable-regular-files cases. A user who hits E2BIG would rerun loopzero check and keep getting the same block with nothing pointing at the cause.

Smallest fix: add "and pathspecs are not oversized" (or similar) to that message so it matches the docs. Separately, the doc sentence "narrow oversized pathspecs and declarations that match unreadable or non-regular paths" is hard to parse; splitting it into two clauses would make it clearer.

"If keys remain unavailable, ensure the check base is available and "
"declared paths select only readable regular files.")
return None


Expand Down
16 changes: 15 additions & 1 deletion src/loopzero/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,22 @@ def settings_fingerprint(config: Config, worktree: Path, *, base: str | None = N
return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest()


def _keyed_stage(config: Config, command: str) -> bool:
return command in config.inputs or "." in config.inputs.get("*", ())


def receipt_inputs_match(config: Config, worktree: Path, report: CheckReport) -> bool:

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: Readiness now rehashes every declared input once per stage

receipt_inputs_match calls _input_key for each keyed result. Each call runs hash-object and diff-tree and SHA-256s every matched file. With "*" = ["."] that means the whole tree is read N times for N stages, on every ready, merge, PR body refresh (_validation_section) and wait snapshot. This is correct, but it can be slow on large repos. One option is to compute the diff-tree output and file digests once per distinct pathspec set and reuse them across stages. This is non-blocking.

try:
base = _check_base(config, worktree)
return all(result.input_key is not None
and result.input_key == _input_key(config, worktree, result.command, base)
for result in report.results if _keyed_stage(config, result.command))
except (SandboxUnavailable, OSError, _proc.ProcTimeout, _proc.ToolMissing):
return False


def _input_key(config: Config, worktree: Path, command: str, base: str) -> str | None:
if not base or (command not in config.inputs and "." not in config.inputs.get("*", ())):
if not base or not _keyed_stage(config, command):
return None
paths = (*config.inputs.get(command, ()), *config.inputs.get("*", ()))
# Empty pathspecs must not accidentally mean the whole tree.
Expand Down
35 changes: 35 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3128,6 +3128,41 @@ def test_check_refuses_unverified_receipt_after_runner_retarget(wt, gh, capsys,
assert run(capsys, "ready")[0] == 0


@pytest.mark.parametrize("keyable", [True, False])
def test_ready_and_merge_reject_receipt_after_clean_checkout_rewrites_input(
wt, gh, capsys, tmp_path, keyable,
):
path = wt / "feature.py"
if not keyable:
(wt / "link").symlink_to(path.name)
git(wt, "add", "link")
git(wt, "commit", "-qm", "add tracked symlink")
head = head_of(wt)
arm_approved_ready_pr(gh, head, wt, readiness={"local_exit": None}, isDraft=False)
command = """python3 -c 'from pathlib import Path; exit(13 in Path("feature.py").read_bytes())'"""
inputs = f'{json.dumps(command)} = ["feature.py"]' if keyable else '"*" = ["."]'
settings = tmp_path / "workflow.toml"
settings.write_text(WORKFLOW.replace('"echo ok"', json.dumps(command)).replace(
"[delivery]", f'[checks.inputs]\n{inputs}\n\n[delivery]',
))
args = ("--config", str(settings))
assert run(capsys, *args, "check")[0] == 0
code, out, _ = run(capsys, *args, "ready")
assert code == (0 if keyable else 1)
if not keyable:
assert "declared paths select only readable regular files" in out

git(wt, "config", "core.autocrlf", "true")
path.unlink()
git(wt, "checkout-index", "-u", "--", path.name)
assert b"\r\n" in path.read_bytes()
assert head_of(wt) == head and not git(wt, "status", "--porcelain")
for action in ("ready", "merge"):
code, out, err = run(capsys, *args, action)
assert code == 1 and "run loopzero check at this head" in out + err
assert run(capsys, *args, "check")[0] == 1


@pytest.mark.parametrize("base_sensitive", [False, True])
def test_wait_snapshot_handles_trusted_base_advance(wt, repo, base_sensitive):
if base_sensitive:
Expand Down