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
2 changes: 2 additions & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ loading the delivery [commands](../src/loopzero/cli.py), which implement the con
for local sandbox receipts. Explicit settings, delegated runner environments
and stage reuse keys still bind the grant. Waiters check for policy changes
on every poll.
Local readiness recomputes input keys recorded in the passing receipt from
the checkout bytes. Missing or changed comparisons require a fresh check.
Local readiness and hosted eligibility carry a review forward only across
a first-parent chain of verified merges from the configured PR base. They
reconstruct each merge in an isolated Git repository and compare its tree
Expand Down
2 changes: 2 additions & 0 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ 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):
return f"declared check inputs changed or are unavailable. {recovery}"
return None


Expand Down
11 changes: 11 additions & 0 deletions src/loopzero/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ def _input_blobs(config: Config, worktree: Path, paths: tuple[str, ...]) -> str
raise


def receipt_inputs_match(config: Config, worktree: Path, report: CheckReport) -> bool:
keyed = [stage for stage in report.results if stage.input_key is not None]
if not keyed:
return True
try:
base = _check_base(config, worktree)
return all(stage.input_key == _input_key(config, worktree, stage.command, base) for stage in keyed)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

important: Recomputed input key binds the transient HEAVY_SLOT_GRANTED_CORES grant, so ready falsely rejects valid receipts

_hash_inputs hashes _execution_settings(config, base). For local checks that includes environment, and environment includes an inherited HEAVY_SLOT_GRANTED_CORES whenever it is on the allowlist. settings_fingerprint deliberately removes that variable (sandbox.py:256-258, and docs/ARCHITECTURE.md:113 says the receipt excludes it), so ready/merge can accept a receipt recorded under a different worker grant. The new receipt_inputs_match recomputes the full stage key in ready's own environment. Suppose check ran with a heavy slot granted (e.g. HEAVY_SLOT_GRANTED_CORES=2) and ready runs with a different grant or none. Every keyed stage then mismatches and the receipt is rejected with "declared check inputs changed", even though no input byte changed. This contradicts the existing contract asserted in tests/test_sandbox.py:783-787 (test_declared_input_reuse[grant] expects _current_check_report(...) is not None after the grant changes to 4 and after it is removed). That test should now fail, because README.md matches the *.md inputs and so the stages carry keys. Smallest fix: build the comparison from the same settings that the receipt gate already accepts. One way is to factor _hash_inputs so it takes the settings dict, then compare using the grant-stripped settings that settings_fingerprint uses. Since the settings fingerprint already binds the other settings, another option is to compare only the per-path content digests and blob listing against something saved in the receipt. Run the existing grant test as a regression.

except (SandboxUnavailable, OSError, _proc.ProcTimeout, _proc.ToolMissing):

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: Exception set differs from _input_key's, so a ValueError escapes ready/merge

_input_key catches (SandboxUnavailable, _proc.ProcTimeout, OSError, ValueError) and re-raises when base is non-empty, which is always the case for a keyed receipt. receipt_inputs_match catches ToolMissing but not ValueError. Any ValueError from _git or the blob listing would therefore propagate out of _check_report_problem and crash ready/merge instead of returning the rerun instruction. Add ValueError to the caught tuple (or share one tuple constant) so that 'cannot be recomputed' always maps to rejection, as the acceptance criteria require.

return False


def _input_key(config: Config, worktree: Path, command: str, base: str, *,
status: Callable[[str], None] | None = None,
workflow: str | Path = "workflow.toml") -> str | None:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2997,6 +2997,30 @@ def test_check_refuses_unverified_receipt_after_runner_retarget(wt, gh, capsys,
assert run(capsys, "ready")[0] == 0


def test_ready_and_merge_reject_receipt_after_clean_checkout_rewrites_input(wt, gh, capsys, tmp_path):
path, head = wt / "feature.py", 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())'"""
settings = tmp_path / "workflow.toml"
settings.write_text(WORKFLOW.replace('"echo ok"', json.dumps(command)).replace(
"[delivery]", f'[checks.inputs]\n{json.dumps(command)} = ["feature.py"]\n\n[delivery]',
))
args = ("--config", str(settings))
expect_cli(capsys, 0, *args, "check")
expect_cli(capsys, 0, *args, "ready")

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 "declared check inputs" in out + err
assert "Run loopzero check" in out + err
expect_cli(capsys, 1, *args, "check")


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