Repository navigation
Deduplicate candidate and workflow policy handling #1845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,12 +90,11 @@ def _has_replacement_refs(common_dir: Path) -> bool: | |
| return True | ||
| return True | ||
| try: | ||
| packed = (common_dir / "packed-refs").read_bytes() | ||
| return b" refs/replace/" in (common_dir / "packed-refs").read_bytes() | ||
| except FileNotFoundError: | ||
| return False | ||
| except OSError: | ||
| return True | ||
| return b" refs/replace/" in packed | ||
|
|
||
|
|
||
| def _remote_workflow(worktree: Path, branch: str, previous: Config | None, *, | ||
|
|
@@ -104,11 +103,8 @@ def _remote_workflow(worktree: Path, branch: str, previous: Config | None, *, | |
| hint = "Fetch the trusted base workflow, or supply --config for bootstrap or an override." | ||
| ref = f"refs/remotes/origin/{branch}" | ||
| if refresh: | ||
| fetched = _proc.run( | ||
| ["git", "fetch", "--quiet", "--no-tags", "--no-recurse-submodules", | ||
| "--no-write-fetch-head", "origin", f"+refs/heads/{branch}:{ref}"], | ||
| cwd=worktree, env_allowlist=worktree_mod.GIT_ENV, timeout=read_timeout(60), | ||
| ) | ||
| fetched = run_git(worktree, "fetch", "--quiet", "--no-tags", "--no-recurse-submodules", | ||
| "--no-write-fetch-head", "origin", f"+refs/heads/{branch}:{ref}", timeout=read_timeout(60)) | ||
| if fetched.exit_code != 0: | ||
| raise ConfigError(f"could not refresh origin/{branch}. {hint}") | ||
| directory_args = ["--path-format=absolute", "--git-common-dir"] if previous is not None else [] | ||
|
|
@@ -129,13 +125,12 @@ def _remote_workflow(worktree: Path, branch: str, previous: Config | None, *, | |
| if shown.exit_code != 0: | ||
| raise ConfigError(f"cannot read {source}. {hint}") | ||
| try: | ||
| data = tomllib.loads(shown.stdout.encode("utf-8", errors="surrogateescape").decode("utf-8")) | ||
| text = shown.stdout.encode("utf-8", errors="surrogateescape").decode("utf-8") | ||
| return _build(tomllib.loads(text)), revision | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The result is the same today. |
||
| except UnicodeDecodeError as exc: | ||
| raise ConfigError(f"{source}: {exc}") from exc | ||
| except tomllib.TOMLDecodeError as exc: | ||
| raise ConfigError(f"{source}: invalid TOML: {exc}. {hint}") from None | ||
| try: | ||
| return _build(data), revision | ||
| except ConfigError as exc: | ||
| raise ConfigError(f"{source}: {exc}. {hint}") from None | ||
|
|
||
|
|
@@ -153,12 +148,12 @@ def load_trusted(worktree: Path, *, previous: Config | None = None, refresh: boo | |
| ) | ||
| branch = ref.removeprefix(prefix) | ||
| policy, revision = _remote_workflow(worktree, branch, previous, refresh=refresh, read_timeout=read_timeout) | ||
| if policy.base_branch == branch: | ||
| return replace(policy, trusted_base_revision=revision) | ||
| base, revision = _remote_workflow(worktree, policy.base_branch, previous, refresh=refresh, read_timeout=read_timeout) | ||
| if base.base_branch != policy.base_branch: | ||
| raise ConfigError("trusted origin workflows disagree on repo.base; align them or supply --config") | ||
| return replace(base, trusted_base_revision=revision) | ||
| if policy.base_branch != branch: | ||
| branch = policy.base_branch | ||
| policy, revision = _remote_workflow(worktree, branch, previous, refresh=refresh, read_timeout=read_timeout) | ||
| if policy.base_branch != branch: | ||
| raise ConfigError("trusted origin workflows disagree on repo.base; align them or supply --config") | ||
| return replace(policy, trusted_base_revision=revision) | ||
|
|
||
|
|
||
| def _build(data: dict[str, Any]) -> Config: | ||
|
|
@@ -225,21 +220,18 @@ def _build(data: dict[str, Any]) -> Config: | |
| if "/" in runner[0] and not Path(runner[0]).is_absolute(): | ||
| raise ConfigError("checks.runner executable paths must be absolute") | ||
| kwargs["check_runner"] = runner | ||
| if "inputs" in checks: | ||
| inputs = {} | ||
| for key, value in checks["inputs"].items(): | ||
| label = f'checks.inputs."{key}"' | ||
| if key != "*" and key not in commands: | ||
| raise ConfigError(f"{label} must exactly match a checks.commands entry") | ||
| inputs[key] = _strings(label, value) | ||
| kwargs["inputs"] = inputs | ||
| inputs = {} | ||
| for key, value in checks.get("inputs", {}).items(): | ||
| label = f'checks.inputs."{key}"' | ||
| if key != "*" and key not in commands: | ||
| raise ConfigError(f"{label} must exactly match a checks.commands entry") | ||
| inputs[key] = _strings(label, value) | ||
| for key, field, read in (("env_allowlist", "env_allowlist", _strings), | ||
| ("ro_paths", "sandbox_ro", _host_paths), | ||
| ("writable", "writable", _host_paths)): | ||
| if key in checks: | ||
| kwargs[field] = read(f"checks.{key}", checks[key]) | ||
| if "env" in checks: | ||
| kwargs["env"] = _env_table(checks["env"]) | ||
| env = _env_table(checks.get("env", {})) | ||
| if "scratch" in checks: | ||
| kwargs["scratch"] = _strings("checks.scratch", checks["scratch"]) | ||
| for entry in kwargs["scratch"]: | ||
|
|
@@ -268,7 +260,7 @@ def _build(data: dict[str, Any]) -> Config: | |
| reviewer_ro_paths=reviewer_ro_paths, | ||
| review_chunk_bytes=review_chunk_bytes, | ||
| network=checks.get("network", False), | ||
| **kwargs, | ||
| inputs=inputs, env=env, **kwargs, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -326,8 +318,7 @@ def _env_table(table: dict[str, Any]) -> tuple[tuple[str, str], ...]: | |
|
|
||
| def _limits(table: dict[str, Any]) -> ResourceLimits: | ||
| defaults = ResourceLimits() | ||
| allowed = tuple(defaults.__dict__) | ||
| _reject_unknown("checks.limits", table, allowed) | ||
| _reject_unknown("checks.limits", table, defaults.__dict__) | ||
| values = defaults.__dict__ | table | ||
| for name, value in values.items(): | ||
| if isinstance(value, bool) or not isinstance(value, int) or value <= 0: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Head-ref prefix check now depends on an earlier check in the caller
_candidate_snapshotno longer checks thatevent_headis a dict or thathead.refstarts withQUEUE_BRANCH_PREFIX. Today nothing changes, because the only caller,_attest_candidate_once, does both checks first:_object(...)at line 444 and the prefix check at line 446. The new checkhead.get("ref") == event_head.get("ref")still rejects any ref that isn't a queue branch, and the error message is unchanged. The risk is in the future: the function's safety now rests on a check that lives somewhere else. If anyone calls_candidate_snapshotfrom a different place, the prefix check is silently lost. A small fix is to add a one-line comment or docstring sayingevent_headmust already be validated and prefix-checked.