From 0cac29763ea42e91d4199554a09c44d3c0327984 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:24:43 +0100 Subject: [PATCH 1/4] Run checks fresh when declared inputs match no files Architecture-Review: unchanged; stage reuse still depends on a Git and checkout fingerprint, now unavailable for unmatched pathspecs. Co-Authored-By: Codex --- src/loopzero/cli.py | 3 ++- src/loopzero/sandbox.py | 31 +++++++++++++++++++++++-------- tests/test_sandbox.py | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 46a93e29..cafd980b 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -598,7 +598,8 @@ def cmd_check(args: argparse.Namespace) -> int: report = sandbox.run_checks(config, wt, progress=lambda command: print( f"running: {command}", flush=True), status=lambda message: print(message, flush=True), - checkpoint=lambda partial: _save_report(wt, partial)) + checkpoint=lambda partial: _save_report(wt, partial), + workflow=args.config or f"origin/{config.base_branch}:workflow.toml") except sandbox.SandboxUnavailable as exc: print(f"sandbox unavailable: {exc}", file=sys.stderr) return 2 diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index a358d56f..a5c8d330 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -130,7 +130,7 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO progress: Callable[[str], None] | None = None, status: Callable[[str], None] | None = None, checkpoint: Callable[[CheckReport], None] | None = None, - _resolved: bool = False) -> CheckReport: + workflow: str | Path = "workflow.toml", _resolved: bool = False) -> CheckReport: """Run checks, stopping at the first failure; status emits only stage metadata. progress receives each command before launch, as before. Optional status receives @@ -179,7 +179,8 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO for index, command in enumerate(commands): if progress: progress(command) - key = _input_key(config, worktree, command, base) if not dirty else None + key = _input_key(config, worktree, command, base, status=status, + workflow=workflow) if not dirty else None old = previous.get(command) if key and old and old.input_key == key: result = old @@ -303,19 +304,33 @@ def settings_fingerprint(config: Config, worktree: Path, *, base: str | None = N return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest() -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("*", ())): - return None - paths = (*config.inputs.get(command, ()), *config.inputs.get("*", ())) +def _input_blobs(config: Config, worktree: Path, paths: tuple[str, ...]) -> str | None: # Empty pathspecs must not accidentally mean the whole tree. + if not paths: + return "" empty = _git(config, worktree, "hash-object", "-t", "tree", "/dev/null").strip() try: - blobs = _git(config, worktree, "-c", "core.quotePath=true", "diff-tree", "--no-commit-id", - "--raw", "-r", "--no-abbrev", empty, "HEAD", "--", *paths) if paths else "" + return _git(config, worktree, "-c", "core.quotePath=true", "diff-tree", "--no-commit-id", + "--raw", "-r", "--no-abbrev", empty, "HEAD", "--", *paths) except OSError as exc: if exc.errno == errno.E2BIG: return None raise + + +def _input_key(config: Config, worktree: Path, command: str, base: str, *, + status: Callable[[str], None] | None = None, + workflow: str | Path = "workflow.toml") -> str | None: + if not base or (command not in config.inputs and "." not in config.inputs.get("*", ())): + return None + paths = (*config.inputs.get(command, ()), *config.inputs.get("*", ())) + blobs = _input_blobs(config, worktree, paths) + if not blobs: + if blobs == "" and status: + entry = command if command in config.inputs else "*" + status(f"{workflow}: checks.inputs[{entry!r}] matches no tracked files. " + f"Correct the pathspecs {paths!r}. Running the check fresh.") + return None contents = [] root = worktree.resolve() try: diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index 5e805278..a0ad0fe8 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -771,6 +771,23 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch assert report.head == git(git_repo, 'rev-parse', 'HEAD').strip() +@pytest.mark.parametrize("paths", [("ipnut.txt",), ()]) +def test_unmatched_inputs_run_fresh(git_repo, bwrap_log, fetched_base, paths): + (git_repo / ".git/info/exclude").write_text(".loopzero/\n") + command = 'test "$(cat README.md)" = hello' + config = make_config(checks=(command,), inputs={command: paths}) + first = sandbox.run_checks(config, git_repo) + assert first.ok + _save_report(git_repo, first) + (git_repo / "README.md").write_text("fail\n") + git(git_repo, "commit", "-qam", "change actual input") + + result = sandbox.run_checks(config, git_repo, status=(messages := []).append) + assert result.results[0].exit_code == 1 + assert any("workflow.toml: checks.inputs" in message and repr(paths) in message + for message in messages) + + def test_local_check_reuse_tracks_checkout_bytes(git_repo, bwrap_log, fetched_base): (git_repo / ".git/info/exclude").write_text(".loopzero/\n") path = git_repo / "README.md" From 084c7469f0c5bb37b9edb1422cdfe562ea9edc29 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:32:02 +0100 Subject: [PATCH 2/4] Report unmatched inputs even when reuse is unavailable Architecture-Review: unchanged; execution still runs fresh when stage reuse is unavailable. Co-Authored-By: Codex --- src/loopzero/sandbox.py | 23 +++++++++++++---------- tests/test_sandbox.py | 10 +++++++--- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index a5c8d330..afa65d61 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -179,8 +179,8 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO for index, command in enumerate(commands): if progress: progress(command) - key = _input_key(config, worktree, command, base, status=status, - workflow=workflow) if not dirty else None + key = _input_key(config, worktree, command, "" if dirty else base, + status=status, workflow=workflow) old = previous.get(command) if key and old and old.input_key == key: result = old @@ -321,16 +321,19 @@ def _input_blobs(config: Config, worktree: Path, paths: tuple[str, ...]) -> str def _input_key(config: Config, worktree: Path, command: str, base: str, *, status: Callable[[str], None] | None = None, workflow: str | Path = "workflow.toml") -> str | None: - if not base or (command not in config.inputs and "." not in config.inputs.get("*", ())): + if command not in config.inputs and "." not in config.inputs.get("*", ()): return None - paths = (*config.inputs.get(command, ()), *config.inputs.get("*", ())) - blobs = _input_blobs(config, worktree, paths) - if not blobs: - if blobs == "" and status: - entry = command if command in config.inputs else "*" - status(f"{workflow}: checks.inputs[{entry!r}] matches no tracked files. " - f"Correct the pathspecs {paths!r}. Running the check fresh.") + entries = {key: config.inputs[key] for key in (command, "*") if key in config.inputs} + blobs = _input_blobs(config, worktree, tuple(path for paths in entries.values() for path in paths)) + if blobs == "" and status: + status(f"{workflow}: checks.inputs for {command!r} matches no tracked files. " + f"Correct these entries: {entries!r}. Running the check fresh.") + if not blobs or not base: return None + return _hash_inputs(config, worktree, command, base, blobs) + + +def _hash_inputs(config: Config, worktree: Path, command: str, base: str, blobs: str) -> str | None: contents = [] root = worktree.resolve() try: diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index a0ad0fe8..b2d0362c 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -771,16 +771,20 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch assert report.head == git(git_repo, 'rev-parse', 'HEAD').strip() -@pytest.mark.parametrize("paths", [("ipnut.txt",), ()]) -def test_unmatched_inputs_run_fresh(git_repo, bwrap_log, fetched_base, paths): +@pytest.mark.parametrize("case", ["typo", "empty", "dirty", "no_base"]) +def test_unmatched_inputs_run_fresh(git_repo, bwrap_log, fetched_base, case): (git_repo / ".git/info/exclude").write_text(".loopzero/\n") command = 'test "$(cat README.md)" = hello' + paths = () if case == "empty" else ("ipnut.txt",) config = make_config(checks=(command,), inputs={command: paths}) first = sandbox.run_checks(config, git_repo) assert first.ok _save_report(git_repo, first) (git_repo / "README.md").write_text("fail\n") - git(git_repo, "commit", "-qam", "change actual input") + if case != "dirty": + git(git_repo, "commit", "-qam", "change actual input") + if case == "no_base": + git(git_repo, "update-ref", "-d", "refs/remotes/origin/main") result = sandbox.run_checks(config, git_repo, status=(messages := []).append) assert result.results[0].exit_code == 1 From ac15e09ec2e9debbc79fbbf5472904433fd79640 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:35:04 +0100 Subject: [PATCH 3/4] Keep diagnostic lookup failures from blocking fresh stages Co-Authored-By: Codex --- src/loopzero/sandbox.py | 11 +++++++++-- tests/test_sandbox.py | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index afa65d61..bfcb121d 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -323,10 +323,17 @@ def _input_key(config: Config, worktree: Path, command: str, base: str, *, workflow: str | Path = "workflow.toml") -> str | None: if command not in config.inputs and "." not in config.inputs.get("*", ()): return None + if not base and status is None: + return None entries = {key: config.inputs[key] for key in (command, "*") if key in config.inputs} - blobs = _input_blobs(config, worktree, tuple(path for paths in entries.values() for path in paths)) + try: + blobs = _input_blobs(config, worktree, tuple(path for paths in entries.values() for path in paths)) + except (SandboxUnavailable, OSError): + if not base: + return None + raise if blobs == "" and status: - status(f"{workflow}: checks.inputs for {command!r} matches no tracked files. " + status(f"{workflow}: checks.inputs for {command!r} matches no files in HEAD. " f"Correct these entries: {entries!r}. Running the check fresh.") if not blobs or not base: return None diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index b2d0362c..de42feab 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -792,6 +792,14 @@ def test_unmatched_inputs_run_fresh(git_repo, bwrap_log, fetched_base, case): for message in messages) +def test_input_diagnostic_failure_does_not_block_dirty_check(git_repo, bwrap_log, fetched_base): + (git_repo / "README.md").write_text("dirty\n") + command = 'test "$(cat README.md)" = dirty' + config = make_config(checks=(command,), inputs={command: (":(bogus)README.md",)}) + report = sandbox.run_checks(config, git_repo, status=lambda _: None) + assert report.ok and report.dirty and report.results[0].command == command + + def test_local_check_reuse_tracks_checkout_bytes(git_repo, bwrap_log, fetched_base): (git_repo / ".git/info/exclude").write_text(".loopzero/\n") path = git_repo / "README.md" From abd4c709c6a2d3dd65a72be04441488e3dff17ab Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:40:25 +0100 Subject: [PATCH 4/4] Keep timeout and invalid argument diagnostics from blocking checks Co-Authored-By: Codex --- src/loopzero/sandbox.py | 2 +- tests/test_sandbox.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index bfcb121d..4a0df20f 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -328,7 +328,7 @@ def _input_key(config: Config, worktree: Path, command: str, base: str, *, entries = {key: config.inputs[key] for key in (command, "*") if key in config.inputs} try: blobs = _input_blobs(config, worktree, tuple(path for paths in entries.values() for path in paths)) - except (SandboxUnavailable, OSError): + except (SandboxUnavailable, _proc.ProcTimeout, OSError, ValueError): if not base: return None raise diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index de42feab..e660ee74 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -792,10 +792,18 @@ def test_unmatched_inputs_run_fresh(git_repo, bwrap_log, fetched_base, case): for message in messages) -def test_input_diagnostic_failure_does_not_block_dirty_check(git_repo, bwrap_log, fetched_base): +@pytest.mark.parametrize("path", [":(bogus)README.md", "README\0.md", "README.md"]) +def test_input_diagnostic_failure_does_not_block_dirty_check( + git_repo, bwrap_log, fetched_base, monkeypatch, path, +): + if path == "README.md": + def timed_out(*args): + raise sandbox._proc.ProcTimeout("input lookup timed out") + + monkeypatch.setattr(sandbox, "_input_blobs", timed_out) (git_repo / "README.md").write_text("dirty\n") command = 'test "$(cat README.md)" = dirty' - config = make_config(checks=(command,), inputs={command: (":(bogus)README.md",)}) + config = make_config(checks=(command,), inputs={command: (path,)}) report = sandbox.run_checks(config, git_repo, status=lambda _: None) assert report.ok and report.dirty and report.results[0].command == command