Skip to content

: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.

fix(stamphog): use one turn budget for every review (#122362)

Raúl Negrón-Otero6d0af5c67d75
diff --git a/products/stamphog/packages/pr-approval-agent/openai_reviewer.py b/products/stamphog/packages/pr-approval-agent/openai_reviewer.py
index f7aeb74bf678..194c2ad41153 100644
--- a/products/stamphog/packages/pr-approval-agent/openai_reviewer.py
+++ b/products/stamphog/packages/pr-approval-agent/openai_reviewer.py
@@ -26,7 +26,7 @@ from typing import Any
from gateway import REVIEWER_EFFORT, REVIEWER_MODEL, openai_gateway_headers, resolve_gateway_config
from github import PRData
from openai import OpenAI
-from reviewer import REVIEWER_SYSTEM, Reviewer, _verdict_from_facts, has_current_head_review, max_turns
+from reviewer import MAX_TURNS, REVIEWER_SYSTEM, Reviewer, _verdict_from_facts, has_current_head_review
from verdict_rule import FACTS_SCHEMA
TOOL_OUTPUT_MAX_CHARS = 50_000
@@ -383,8 +383,7 @@ class OpenAIReviewer(Reviewer):
readable_diff = self._copy_diff_into_explore_root(original_diff)
prompt = self._build_review_prompt(pr, classification, gate_context, readable_diff)
client = self.client or self._new_client(pr, classification, gate_context)
- turns = max_turns(classification, gate_context)
- return self._run_loop(client, prompt, turns, classification, head_reviewed=has_current_head_review(pr))
+ return self._run_loop(client, prompt, classification, head_reviewed=has_current_head_review(pr))
finally:
if readable_diff != original_diff:
readable_diff.unlink(missing_ok=True)
@@ -413,11 +412,11 @@ class OpenAIReviewer(Reviewer):
raise RuntimeError(f"Review time budget exhausted ({REVIEW_TIME_BUDGET_SECONDS}s)")
return min(REQUEST_TIMEOUT_SECONDS, remaining)
- def _run_loop(self, client: Any, prompt: str, turns: int, classification: dict, *, head_reviewed: bool) -> dict:
+ def _run_loop(self, client: Any, prompt: str, classification: dict, *, head_reviewed: bool) -> dict:
tools = RepoTools(self.explore_root)
conversation: list[Any] = [{"role": "user", "content": prompt}]
usage = _TokenUsage()
- for turn in range(1, turns + 1):
+ for turn in range(1, MAX_TURNS + 1):
# store=False keeps the PR content out of OpenAI's response storage. The conversation
# is resent every turn, and the encrypted reasoning items carry the model's reasoning
# from one turn to the next.
@@ -447,7 +446,7 @@ class OpenAIReviewer(Reviewer):
output = tools.call(call.name, call.arguments)
conversation.append({"type": "function_call_output", "call_id": call.call_id, "output": output})
# Same wording as the Agent SDK's turn-limit error, so the pipeline treats it as non-retryable.
- raise RuntimeError(f"Reached maximum number of turns ({turns})")
+ raise RuntimeError(f"Reached maximum number of turns ({MAX_TURNS})")
def _final_result(self, response: Any, classification: dict, head_reviewed: bool) -> dict:
if response.status != "completed":
diff --git a/products/stamphog/packages/pr-approval-agent/reviewer.py b/products/stamphog/packages/pr-approval-agent/reviewer.py
index d975bc4aaa5a..00741f1a822c 100644
--- a/products/stamphog/packages/pr-approval-agent/reviewer.py
+++ b/products/stamphog/packages/pr-approval-agent/reviewer.py
@@ -58,6 +58,9 @@ INLINE_COMMENT_CAP = 60
DISCUSSION_HEAD_KEEP = 15
DISCUSSION_TAIL_KEEP = 35
AUTHOR_DISCUSSION_KEEP = 10
+# Every review gets one budget. The reviewer spends its first turn reading the diff, so a small
+# budget ends a PR of any size in ERROR instead of a verdict.
+MAX_TURNS = 20
# _sanitize_untrusted lives in policy.py (shared with the folder-prose
@@ -429,14 +432,6 @@ _INVOCATION_LINES = {
}
-def max_turns(classification: dict, gate_context: dict) -> int:
- """The reviewer's turn budget for this PR."""
- # Gate denials and trivial PRs don't need deep exploration —
- # just read the diff and report.
- quick = gate_context["gate_verdict"] == "DENIED" or classification.get("t1_subclass") == "T1a-trivial"
- return 5 if quick else 20
-
-
class Reviewer:
"""LLM reviewer using Agent SDK."""
@@ -507,7 +502,7 @@ class Reviewer:
# ships in the head tree, regardless of CLI defaults.
mcp_servers={},
strict_mcp_config=True,
- max_turns=max_turns(classification, gate_context),
+ max_turns=MAX_TURNS,
model=CLAUDE_REVIEWER_MODEL,
permission_mode="dontAsk",
output_format=FACTS_SCHEMA,
diff --git a/products/stamphog/packages/pr-approval-agent/test_openai_reviewer.py b/products/stamphog/packages/pr-approval-agent/test_openai_reviewer.py
index 14099eec6d3a..3f58f46c1e04 100644
--- a/products/stamphog/packages/pr-approval-agent/test_openai_reviewer.py
+++ b/products/stamphog/packages/pr-approval-agent/test_openai_reviewer.py
@@ -252,7 +252,7 @@ def test_tool_loop_reads_the_diff_and_derives_the_verdict_from_the_final_facts(c
def test_tool_loop_stops_with_an_error_the_pipeline_does_not_retry(
checkout: Path, budget_spent: bool, expected_error: str
) -> None:
- calls = [_tool_call("glob", {"pattern": "src/*"}) for _ in range(5)]
+ calls = [_tool_call("glob", {"pattern": "src/*"}) for _ in range(openai_reviewer.MAX_TURNS)]
reviewer = OpenAIReviewer(checkout, client=SimpleNamespace(responses=_FakeResponses(calls)))
if budget_spent:
reviewer.deadline = 0.0
diff --git a/products/stamphog/packages/pr-approval-agent/version.py b/products/stamphog/packages/pr-approval-agent/version.py
index 7b7fa66d84a2..0a460dc53dbd 100644
--- a/products/stamphog/packages/pr-approval-agent/version.py
+++ b/products/stamphog/packages/pr-approval-agent/version.py
@@ -9,4 +9,4 @@ changes don't need a bump - they are tracked by the policy sha shown next to
the version in the verdict table.
"""
-STAMPHOG_VERSION = "2.6.0"
+STAMPHOG_VERSION = "2.6.1"