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): pin the reviewer interpreter in the sandbox (#121220)

Georges-Antoine Assi0970514af47e
diff --git a/products/stamphog/backend/temporal/activities.py b/products/stamphog/backend/temporal/activities.py
index 375d6e30584d..be046b40de97 100644
--- a/products/stamphog/backend/temporal/activities.py
+++ b/products/stamphog/backend/temporal/activities.py
@@ -1205,6 +1205,7 @@ def _run_reviewer(
token_path = shlex.quote(STAMPHOG_SANDBOX_GATEWAY_TOKEN_PATH)
command = (
f'export AI_GATEWAY_API_KEY="$(cat {token_path})" && rm -f {token_path} && '
+ f'{_SANDBOX_PYTHON_VAR}="$(command -v python3)" && '
f"cd {shlex.quote(STAMPHOG_SANDBOX_REPO_DIR)} && "
f"STAMPHOG_REPO_ROOT={shlex.quote(STAMPHOG_SANDBOX_REPO_DIR)} "
f"STAMPHOG_LAUNCHED_AT_MS=$(date +%s%3N) {_harden_reviewer_command(invocation.command)}"
@@ -1997,6 +1998,9 @@ def mark_review_failed(input: MarkReviewFailedInput) -> None:
_post_failure_notice(client, run, input.team_id)
+_SANDBOX_PYTHON_VAR = "STAMPHOG_PYTHON"
+
+
def _harden_reviewer_command(command: Sequence[str] | str) -> str:
"""Turn the reviewer invocation into a shell string that runs uv in isolation.
@@ -2006,10 +2010,17 @@ def _harden_reviewer_command(command: Sequence[str] | str) -> str:
to a hostile package index — arbitrary code execution inside a sandbox that holds a live
installation token. ``--no-config`` ignores discovered uv config, ``--no-project`` ignores
the surrounding project/lockfile; the engine's own PEP 723 script pins its deps.
+
+ Interpreter discovery reads the checkout too: uv looks for a virtual environment in the working
+ directory and its parents, and it executes each candidate to query it before any version or
+ ``--managed-python`` filter applies. Only an absolute ``--python`` path skips that discovery, so
+ the caller resolves the image's ``python3`` into ``_SANDBOX_PYTHON_VAR`` before it changes into
+ the checkout.
"""
parts = shlex.split(command) if isinstance(command, str) else list(command)
if len(parts) >= 2 and parts[0] == "uv" and parts[1] == "run":
- parts = [*parts[:2], "--no-config", "--no-project", *parts[2:]]
+ uv_run = shlex.join([*parts[:2], "--no-config", "--no-project"])
+ return f'{uv_run} --python "${_SANDBOX_PYTHON_VAR}" {shlex.join(parts[2:])}'
return shlex.join(parts)
diff --git a/products/stamphog/backend/tests/test_integration.py b/products/stamphog/backend/tests/test_integration.py
index 37bc5c16f1e2..c4e06ec890ea 100644
--- a/products/stamphog/backend/tests/test_integration.py
+++ b/products/stamphog/backend/tests/test_integration.py
@@ -416,6 +416,12 @@ def test_a_final_gate_verdict_is_posted_without_a_sandbox_review(
assert reviewer_commands
# A PR can add a .stamphog/ between the checkout root and the engine, so the root is named, not found.
assert all("STAMPHOG_REPO_ROOT=" in command for command in reviewer_commands)
+ # uv must not discover its interpreter from the checkout, so it gets one resolved before the cd.
+ assert all(
+ command.index('STAMPHOG_PYTHON="$(command -v python3)" && ') < command.index("cd ")
+ and 'uv run --no-config --no-project --python "$STAMPHOG_PYTHON" ' in command
+ for command in reviewer_commands
+ )
assert "fast_path" not in run.output
return