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
4 changes: 4 additions & 0 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,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):
return ("declared input keys changed or are unavailable; run loopzero check at this head. "
"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 @@ -303,8 +303,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:
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 @@ -3058,6 +3058,41 @@ def change_policy(_seconds):
assert run(capsys, *args, "check")[0] == 1


@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
Loading