From be5d4a5f8ceea7f74ce5e2cb513c85e49e826c4a Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 15:01:13 +0100 Subject: [PATCH 1/3] Deduplicate candidate and sandbox validation Architecture-Review: unchanged; the predicates retain the same errors, isolation boundaries, and GitHub checks. Co-Authored-By: Codex --- src/loopzero/candidate.py | 68 ++++++++++++++------------------------- src/loopzero/sandbox.py | 9 ++---- src/loopzero/worktree.py | 11 +++---- 3 files changed, 32 insertions(+), 56 deletions(-) diff --git a/src/loopzero/candidate.py b/src/loopzero/candidate.py index 27eff315..676b9926 100644 --- a/src/loopzero/candidate.py +++ b/src/loopzero/candidate.py @@ -80,6 +80,14 @@ def _require(condition: bool, message: str, error: type[CandidateError] = Candid raise error(message) +def _field(value: object, name: str) -> object: + return value.get(name) if isinstance(value, dict) else None + + +def _matches(pattern: re.Pattern[str], value: object) -> bool: + return isinstance(value, str) and pattern.fullmatch(value) is not None + + def _identity(actor: object) -> tuple[object, object, object, object]: if not isinstance(actor, dict): return (None, None, None, None) @@ -151,8 +159,8 @@ def _candidate_lineage(status: JsonObject, candidate_number: int) -> tuple[str, return candidate_id, sorted(source_numbers), sorted(missing) -def _repo_identity(repo: JsonObject) -> tuple[object, object]: - return repo.get("id"), repo.get("full_name") +def _repo_identity(repo: object) -> tuple[object, object]: + return _field(repo, "id"), _field(repo, "full_name") def _candidate_snapshot( @@ -179,25 +187,14 @@ def _candidate_snapshot( def _source_fingerprint(source: JsonObject) -> tuple[object, ...]: - head = source.get("head") base = source.get("base") - base = base if isinstance(base, dict) else {} - base_repo = base.get("repo") + base_repo = _field(base, "repo") labels = source.get("labels", []) if not isinstance(labels, list) or any(not isinstance(label, dict) or not isinstance(label.get("name"), str) for label in labels): raise CandidateError("invalid source PR labels") - return ( - source.get("number"), - source.get("merged"), - source.get("mergeable") is False, - tuple(label["name"] for label in labels), - source.get("state"), - source.get("draft"), - source.get("body"), - head.get("sha") if isinstance(head, dict) else None, - base.get("ref"), - *_repo_identity(base_repo if isinstance(base_repo, dict) else {}), - ) + return (source.get("number"), source.get("merged"), source.get("mergeable") is False, + tuple(label["name"] for label in labels), source.get("state"), source.get("draft"), source.get("body"), + _field(source.get("head"), "sha"), _field(base, "ref"), *_repo_identity(base_repo)) def _lookup_source(github_get: GitHubGet, repo: str, number: int) -> JsonObject: @@ -220,12 +217,8 @@ def _attest_source( f"source PR #{number} is not open", CandidateRaceError if source.get("merged") is True else CandidateError, ) - head = source.get("head") - base = source.get("base") - _require( - isinstance(head, dict) and isinstance(base, dict), - f"source PR #{number} refs are missing", - ) + head, base = source.get("head"), source.get("base") + _require(isinstance(head, dict) and isinstance(base, dict), f"source PR #{number} refs are missing") base_repo = base.get("repo") _require( base.get("ref") == base_branch @@ -234,17 +227,13 @@ def _attest_source( f"source PR #{number} does not target this repository's {base_branch} branch", ) source_sha = head.get("sha") - _require( - isinstance(source_sha, str) and _SHA.fullmatch(source_sha) is not None, - f"source PR #{number} has an invalid head", - ) + _require(_matches(_SHA, source_sha), f"source PR #{number} has an invalid head") comparison = _object( github_get(f"/repos/{repository['full_name']}/compare/{source_sha}...{candidate_sha}"), "GitHub compare returned an invalid payload", ) - merge_base = comparison.get("merge_base_commit") _require(comparison.get("status") in {"ahead", "identical"} - and isinstance(merge_base, dict) and merge_base.get("sha") == source_sha, + and _field(comparison.get("merge_base_commit"), "sha") == source_sha, f"source PR #{number} head {source_sha} is not an ancestor of the candidate") return source_sha @@ -426,8 +415,7 @@ def _attest_candidate_once( _require(event.get("action") in {"opened", "synchronize"}, "Candidate attestation requires its creation or head-update event") full_name = repository.get("full_name") - _require(isinstance(full_name, str) and _REPOSITORY.fullmatch(full_name) is not None, - "GitHub event repository name is invalid") + _require(_matches(_REPOSITORY, full_name), "GitHub event repository name is invalid") event_head = _object(event_pr.get("head"), "GitHub event candidate head is missing") event_ref = event_head.get("ref") _require(isinstance(event_ref, str) and event_ref.startswith(QUEUE_BRANCH_PREFIX), @@ -435,9 +423,8 @@ def _attest_candidate_once( owner, repo = full_name.split("/", 1) app = _object(github_get("/apps/mergify"), "GitHub App lookup is invalid") - app_owner = app.get("owner") _require(app.get("id") == MERGIFY_APP_ID and app.get("slug") == "mergify" - and isinstance(app_owner, dict) and app_owner.get("login") == MERGIFY_APP_OWNER, + and _field(app.get("owner"), "login") == MERGIFY_APP_OWNER, "Could not resolve the official Mergify GitHub App") expected_identity = _identity(_object(github_get("/users/mergify%5Bbot%5D"), "GitHub bot lookup is invalid")) _require(expected_identity == (MERGIFY_BOT_ID, MERGIFY_BOT_LOGIN, "Bot", MERGIFY_APP_URL), @@ -450,8 +437,7 @@ def _attest_candidate_once( candidate_number = event_pr.get("number") candidate_sha = event_head.get("sha") _require(isinstance(candidate_number, int) and candidate_number > 0, "Candidate PR number is invalid") - _require(isinstance(candidate_sha, str) and _SHA.fullmatch(candidate_sha) is not None, - "Candidate head SHA is invalid") + _require(_matches(_SHA, candidate_sha), "Candidate head SHA is invalid") candidate_path = f"/repos/{full_name}/pulls/{candidate_number}" def recheck_candidate(message: str = "GitHub candidate lookup returned an invalid payload") -> None: candidate = _object(github_get(candidate_path), message) @@ -498,8 +484,7 @@ def live_lineage() -> tuple[str, list[int], list[str]]: with contextlib.suppress(Exception): current = _lookup_source(github_get, full_name, number) if (isinstance(current, dict) and current.get("merged") is True - and isinstance(current.get("head"), dict) - and current["head"].get("sha") == source_sha): + and _field(current.get("head"), "sha") == source_sha): raise CandidateRaceError(f"source PR #{number} landed during attestation") from exc raise @@ -507,7 +492,7 @@ def live_lineage() -> tuple[str, list[int], list[str]]: try: base_ref = _object(base_before, f"{base_branch} ref unavailable") base_sha = _object(base_ref.get("object"), f"{base_branch} object unavailable").get("sha") - _require(isinstance(base_sha, str) and _SHA.fullmatch(base_sha) is not None, f"invalid {base_branch} head") + _require(_matches(_SHA, base_sha), f"invalid {base_branch} head") _verify_tree(full_name, base_sha, candidate_sha, [snapshot[0] for snapshot in source_snapshots.values()]) _require( @@ -531,11 +516,8 @@ def live_lineage() -> tuple[str, list[int], list[str]]: recheck_candidate() for number, (expected_sha, expected_fingerprint) in source_snapshots.items(): current = _lookup_source(github_get, full_name, number) - current_head = current.get("head") - _require( - isinstance(current_head, dict) and current_head.get("sha") == expected_sha, - f"source PR #{number} head changed during attestation", - ) + _require(_field(current.get("head"), "sha") == expected_sha, + f"source PR #{number} head changed during attestation") _require( _source_fingerprint(current) == expected_fingerprint, f"source PR #{number} metadata changed during attestation", diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index dd8a9df6..46e54b72 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -222,9 +222,8 @@ def _execution_settings(config: Config, base: str) -> dict: settings["base"] = base if config.check_runner: settings["runner_executable"] = _runner_identity(config) - settings["environment"] = _proc.build_env(config.env_allowlist) - else: - settings["environment"] = check_environment(config) + settings["environment"] = (_proc.build_env(config.env_allowlist) if config.check_runner + else check_environment(config)) return settings @@ -268,9 +267,7 @@ def _has_inputs(config: Config, command: str) -> bool: 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 _has_inputs(config, command): - return None - if not base and status is None: + if not _has_inputs(config, command) or (not base and status is None): return None entries = {key: config.inputs[key] for key in (command, "*") if key in config.inputs} try: diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index 3164a27a..d0e8667f 100644 --- a/src/loopzero/worktree.py +++ b/src/loopzero/worktree.py @@ -374,13 +374,10 @@ def _require_no_ignored_user_files(wt: Path) -> None: wt, "ls-files", "--others", "--ignored", "--exclude-standard", "-z", preserve_newlines=True ) for path in ignored.split("\0")[:-1]: - if ( - (path in {".loopzero/task.md", ".loopzero/checks.json"} - or re.fullmatch(r"\.loopzero/review(?:er-output)?-[0-9a-f]{12}-(primary|delta)\.json", path)) - and not (wt / path).is_symlink() - ): - continue - raise WorktreeError(f"worktree contains ignored file {path!r}, refusing to remove: {wt}") + if not ((path in {".loopzero/task.md", ".loopzero/checks.json"} + or re.fullmatch(r"\.loopzero/review(?:er-output)?-[0-9a-f]{12}-(primary|delta)\.json", path)) + and not (wt / path).is_symlink()): + raise WorktreeError(f"worktree contains ignored file {path!r}, refusing to remove: {wt}") def cleanup(wt: Path, expected_head: str) -> None: From 66378ab6107b516e0f5215213193984f947c9fd9 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 15:15:21 +0100 Subject: [PATCH 2/3] Keep one runner settings decision Architecture-Review: unchanged; this restores the original check settings branch. Co-Authored-By: Codex --- src/loopzero/sandbox.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index a20dffdd..a015f279 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -230,8 +230,9 @@ def _execution_settings(config: Config, base: str) -> dict: settings["base"] = base if config.check_runner: settings["runner_executable"] = _runner_identity(config) - settings["environment"] = (_proc.build_env(config.env_allowlist) if config.check_runner - else check_environment(config)) + settings["environment"] = _proc.build_env(config.env_allowlist) + else: + settings["environment"] = check_environment(config) return settings From b151c4da0907c2579aebbf104635aa14e8063848 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 15:33:21 +0100 Subject: [PATCH 3/3] Check the resolved head after a required base refresh Architecture-Review: unchanged; skill text clarifies check sequencing under the existing review and receipt rules. Co-Authored-By: Codex --- core/skills/implement/SKILL.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/skills/implement/SKILL.md b/core/skills/implement/SKILL.md index fd485570..419f753a 100644 --- a/core/skills/implement/SKILL.md +++ b/core/skills/implement/SKILL.md @@ -62,6 +62,8 @@ Before the check and draft push for primary review, rebase your own commits onto Leave base merges out of a branch awaiting its delta. For base or check-script changes after review, follow the [Review budget](../../CONTRACT.md#review-budget). +If a reviewed branch needs a conflict refresh, confirm the review path and +merge the base before starting the check for the resolved head. Preserve the primary for the delta and the delta for post-review fixes. Never bypass the sandbox, change the check list or enable `[checks].network` to pass checks.