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
3 changes: 2 additions & 1 deletion src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ def _check_report_matches(report: CheckReport, wt: Path, config: Config, head: s
return (report.head == head and not report.dirty
and tuple(result.command for result in report.results) == config.checks
and report.settings_sha256 is not None
and report.settings_sha256 == sandbox.settings_fingerprint(config, wt))
and report.settings_sha256 == sandbox.settings_fingerprint(config, wt)
and sandbox.receipt_inputs_match(config, wt, report))


def _current_check_report(wt: Path, config: Config, head: str) -> CheckReport | None:
Expand Down
9 changes: 9 additions & 0 deletions src/loopzero/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ def settings_fingerprint(config: Config, worktree: Path, *, base: str | None = N
return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest()


def receipt_inputs_match(config: Config, worktree: Path, report: CheckReport) -> bool:
Comment thread
eowca marked this conversation as resolved.
Resolved
try:
base = _check_base(config, worktree)
return all(result.input_key == _input_key(config, worktree, result.command, base)

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: Treating None == None as a match brings back the stale-receipt bug for declared stages whose key cannot be computed

This delta removes the result.input_key is not None requirement for declared stages. receipt_inputs_match now passes whenever the saved key and the recomputed key are both None. _input_key returns None for a declared stage in several cases:

  • any declared path is a symlink or a non-regular entry (mode not 100644/100755, resolve() != path, or not S_ISREG);
  • diff-tree hits E2BIG;
  • any read raises OSError, ValueError, SyntaxError or RuntimeError;
  • _check_base returns "" because base resolution failed and there is no runner.

In any of these cases the receipt stops tracking the stage's regular files. Example: with "*" = ["."] and a tracked symlink (the setup in the new test), Git rewrites feature.py from LF to CRLF. The key is still None, so ready and merge accept the old receipt. That is the exact failure #1296 is meant to close, and it contradicts the task note to "fail closed if a declared stage key is missing or unavailable". A transient read error (EACCES, ELOOP) is also read as "unchanged" instead of "cannot be verified".

The new test test_ready_accepts_fresh_check_for_input_without_reuse_key locks in this fail-open behaviour. It never checks that a byte rewrite is still rejected in this setup.

Smallest fix: keep reuse eligibility separate from receipt verification. Reuse can stay disabled when a key is None. Verification should compute a digest that stays defined in these cases, for example by hashing symlink targets with os.readlink and recording entry modes instead of returning None, and should treat any remaining unavailability as a mismatch. If that is out of scope, restore the previous fail-closed check (result.input_key is not None for declared stages) and accept that ready is blocked for such inputs. Either way, add a regression test that rewrites feature.py with the symlink present and asserts that ready fails.

for result in report.results)
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("*", ())):

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: Inlined predicate drops a named helper

Removing _keyed_stage copies its condition straight into _input_key. The reason for that condition (which stages are declared and therefore keyed) is now implicit, and if receipt verification needs to tell declared stages apart again, as the finding above requires, the logic will get duplicated. Consider keeping the helper.

return None
Expand Down
45 changes: 45 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2991,6 +2991,51 @@ def change_policy(_seconds):
assert run(capsys, *args, "check")[0] == 1


def test_ready_and_merge_reject_receipt_after_clean_checkout_rewrites_input(
wt, gh, capsys, head, tmp_path,
):
arm_approved_ready_pr(gh, head, wt, readiness={"local_exit": None}, isDraft=False)
path = wt / "feature.py"
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))

assert b"\r" not in path.read_bytes()
assert run(capsys, *args, "check")[0] == 0
assert run(capsys, *args, "ready")[0] == 0

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
assert "run loopzero check at this head" in out + err
assert run(capsys, *args, "check")[0] == 1


def test_ready_accepts_fresh_check_for_input_without_reuse_key(wt, gh, capsys, tmp_path):
(wt / "link").symlink_to("feature.py")
git(wt, "add", "link")
git(wt, "commit", "-qm", "add tracked symlink")
arm_approved_ready_pr(gh, head_of(wt), wt, readiness={"local_exit": None}, isDraft=False)
settings = tmp_path / "workflow.toml"
settings.write_text(WORKFLOW.replace(
"[delivery]", '[checks.inputs]\n"*" = ["."]\n\n[delivery]',
))
args = ("--config", str(settings))
assert run(capsys, *args, "check")[0] == 0
report = json.loads((wt / ".loopzero/checks.json").read_text())
assert all("input_key" not in stage for stage in report["results"])
assert run(capsys, *args, "ready")[0] == 0


@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