: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
fix(devex): stop stamphog treating its own stale reviews as tampering (#67656)
Julian Bezdc1b994b12ea
diff --git a/tools/pr-approval-agent/README.md b/tools/pr-approval-agent/README.mdindex c3788aa20438..ff566a39cec3 100644--- a/tools/pr-approval-agent/README.md+++ b/tools/pr-approval-agent/README.md@@ -74,6 +74,9 @@ LLM Reviewapps like inkeep react for non-review reasons), never the PR author- An 👀 reaction signals an in-flight review — the LLM refuses rather thanapproving over someone who is mid-review+ - Stamphog's own prior reviews (stamphog[bot] refusals, github-actions[bot]+ approvals) are excluded from the prompt — they describe an earlier snapshot+ of the PR and are never independent review signal- For non-trivial changes, expects at least one independent reviewer (anagent reviewer like Codex/Greptile/Claude, or a teammate) to have passedover the current head; escalates otherwise. Trivial changes (docs, tests,diff --git a/tools/pr-approval-agent/github.py b/tools/pr-approval-agent/github.pyindex 883ac48cfeb7..145652d79c3e 100644--- a/tools/pr-approval-agent/github.py+++ b/tools/pr-approval-agent/github.py@@ -86,15 +86,28 @@ def is_bot_author(user: dict) -> bool:return "[bot]" in login or login in _BOT_MACHINE_USERS+# Stamphog's own review identities: REFUSE/ESCALATE comment reviews post via+# the GitHub App (stamphog[bot]); APPROVE reviews post via the workflow's+# GITHUB_TOKEN (github-actions[bot]) so they count toward branch protection.+_SELF_REVIEW_LOGINS = {"stamphog[bot]", "github-actions[bot]"}++def _normalize_reviews_for_prompt(reviews_raw: list[dict], head_sha: str) -> list[dict]:"""Normalize top-level reviews for the reviewer prompt.Preserve trusted/bot reviews, and annotate whether each review was left onthe current PR head. This lets the LLM distinguish active feedback fromolder context that may already have been addressed in follow-up commits.++ Stamphog's own prior reviews are excluded: they describe an earlier+ snapshot of the PR, are never independent review signal, and the reviewer+ has no way to recognize them as its own — re-reading a stale verdict after+ the PR state changed makes it suspect tampering and refuse forever."""normalized_reviews = []for review in reviews_raw:+ if review.get("user", {}).get("login") in _SELF_REVIEW_LOGINS:+ continueif not (review.get("author_association") in _TRUSTED_ASSOCIATIONSor review.get("author_association") == "BOT"diff --git a/tools/pr-approval-agent/reviewer.py b/tools/pr-approval-agent/reviewer.pyindex f50f12a4bb8c..a2dc17f2ff1b 100644--- a/tools/pr-approval-agent/reviewer.py+++ b/tools/pr-approval-agent/reviewer.py@@ -36,12 +36,27 @@ except ImportError:MODEL = "claude-sonnet-5"-_CONTROL_CHARS_RE = re.compile(r"[^\x20-\x7E\n\t]")+# Strip only invisible characters — the prompt-smuggling vectors: C0/C1+# controls, bidi overrides, zero-width chars, and the Unicode tags block+# (invisible ASCII). Visible unicode must survive: reviewer bots express+# verdicts as 👍/👀 in review bodies, and stripping emoji garbles those+# into text that reads like tampering on the next run. ZWJ is stripped+# with the other zero-width chars (it interleaves invisibly into words);+# composite emoji degrade to their visible components, which stays readable.+_INVISIBLE_CHARS_RE = re.compile(+ "[\x00-\x08\x0b-\x1f\x7f-\x9f" # C0/C1 controls and DEL (keep \t \n)+ "\u061c" # Arabic letter mark (bidi)+ "\u200b-\u200f" # zero-width space/joiners, LRM/RLM+ "\u2028\u2029" # line/paragraph separators+ "\u202a-\u202e\u2066-\u2069" # bidi embedding/override/isolate controls+ "\u2060\ufeff" # word joiner, BOM+ "\U000e0000-\U000e007f]" # tags block — invisible ASCII smuggling+)def _sanitize_untrusted(text: str, max_len: int = 200) -> str:- """Strip non-printable chars and cap length."""- return _CONTROL_CHARS_RE.sub("", text)[:max_len]+ """Strip invisible/control chars and cap length; visible unicode passes through."""+ return _INVISIBLE_CHARS_RE.sub("", text)[:max_len]def _reaction_token(reaction: dict) -> str:@@ -168,6 +183,10 @@ REVIEWER_SYSTEM = textwrap.dedent(REFUSE and tell the author to wait for that reviewer to finish andre-request. This overrides any 👍 present.- Bot/agent comments with valid concerns that were ignored → ESCALATE.+ - Your own prior reviews (posted as stamphog[bot] or github-actions[bot])+ are excluded from this context — each run judges the PR's current state+ fresh. If another reviewer quotes an earlier stamphog verdict, treat it+ as history, not as an independent signal or as tampering.Independent review (you are not a substitute for one):- Stamphog is the only automated approver in this path, so for anydiff --git a/tools/pr-approval-agent/test_github.py b/tools/pr-approval-agent/test_github.pyindex bbd680c282aa..e9299fcf6c8d 100644--- a/tools/pr-approval-agent/test_github.py+++ b/tools/pr-approval-agent/test_github.py@@ -11,7 +11,7 @@ from github import _normalize_reviews_for_prompt, _reaction_emoji, _trusted_reacdef test_normalize_reviews_marks_current_head_and_preserves_stale_reviews() -> None:head_sha = "072cdd75592bfd0bf0c016209385f20f85a45201"current_review = {- "user": {"login": "stamphog", "type": "Bot"},+ "user": {"login": "copilot-pull-request-reviewer", "type": "Bot"},"state": "COMMENTED","body": "Current head concern","commit_id": head_sha,@@ -31,7 +31,7 @@ def test_normalize_reviews_marks_current_head_and_preserves_stale_reviews() -> Nassert normalized == [{- "user": "stamphog",+ "user": "copilot-pull-request-reviewer","state": "COMMENTED","body": "Current head concern","commit_id": head_sha,@@ -80,6 +80,35 @@ def test_normalize_reviews_filters_by_trust_source(assert len(normalized) == expected_count+ "login,expected_count",+ [+ pytest.param("stamphog[bot]", 0, id="own-refuse-comment-review"),+ pytest.param("github-actions[bot]", 0, id="own-approve-review"),+ pytest.param("greptile-apps[bot]", 1, id="other-bot-kept"),+ ],+)+def test_normalize_reviews_excludes_stamphogs_own_prior_reviews(login: str, expected_count: int) -> None:+ # Feeding stamphog's own stale reviews back into the prompt makes the next+ # run read them as third-party claims about state that no longer matches —+ # it then suspects tampering and refuses forever.+ normalized = _normalize_reviews_for_prompt(+ [+ {+ "user": {"login": login, "type": "Bot"},+ "state": "COMMENTED",+ "body": "Refusing: reviews in flight",+ "commit_id": "abc123",+ "submitted_at": "2026-04-07T20:14:03Z",+ "author_association": "NONE",+ }+ ],+ "abc123",+ )++ assert len(normalized) == expected_count++@pytest.mark.parametrize("content,expected",[diff --git a/tools/pr-approval-agent/test_reviewer.py b/tools/pr-approval-agent/test_reviewer.pynew file mode 100644index 000000000000..058c2cda14ab--- /dev/null+++ b/tools/pr-approval-agent/test_reviewer.py@@ -0,0 +1,38 @@+"""Tests for prompt sanitization in reviewer.py."""++import sys++import pytest+from unittest.mock import MagicMock++# reviewer.py is imported by a uv-script; its `claude_agent_sdk` dep is+# installed by `uv run`, not the test venv. Stub the modules it imports.+sys.modules.setdefault("claude_agent_sdk", MagicMock())+sys.modules.setdefault("claude_agent_sdk.types", MagicMock())++from reviewer import _sanitize_untrusted # noqa: E402+++ "text,expected",+ [+ # Reviewer bots express verdicts as emoji in review bodies; stripping+ # them garbles quoted comments into text a later run can misread as+ # an injection attempt.+ pytest.param(+ "both have 👀 reactions — that overrides the 👍s present.",+ "both have 👀 reactions — that overrides the 👍s present.",+ id="emoji-and-dash-survive",+ ),+ pytest.param("ein Häkchen ✓ 中文", "ein Häkchen ✓ 中文", id="non-ascii-text-survives"),+ pytest.param("zero\u200bwidth\u200e", "zerowidth", id="zero-width-stripped"),+ # ZWJ interleaving (i\u200dg\u200dn\u200do\u200dr\u200de) is a smuggling vector, so it+ # is stripped too; composite emoji degrade to visible components.+ pytest.param("i\u200dg\u200dnore 🧑\u200d💻", "ignore 🧑💻", id="zwj-stripped-emoji-degrades-visibly"),+ pytest.param("a\u202egnihton\u202c od\u2066b\u2069\u061c", "agnihton odb", id="bidi-controls-stripped"),+ pytest.param("hidden\U000e0041\U000e0042tag", "hiddentag", id="tags-block-smuggling-stripped"),+ pytest.param("bell\x07cr\rok\ntab\t.", "bellcrok\ntab\t.", id="control-chars-stripped-keeps-nl-tab"),+ ],+)+def test_sanitize_untrusted_strips_invisible_keeps_visible(text: str, expected: str) -> None:+ assert _sanitize_untrusted(text) == expected