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(ai): skip agentic repo selection on sandbox open (#65463)

Georgiy Tarasovb021659b0ed1
diff --git a/ee/api/conversation.py b/ee/api/conversation.py
index 0f4e5b23ff08..4fd39d830e6f 100644
--- a/ee/api/conversation.py
+++ b/ee/api/conversation.py
@@ -795,7 +795,7 @@ class ConversationViewSet(
Runs only on a first message — no backing Task yet (`task_id is None`) and real content.
Followups and resumes reuse the existing Task's repository, and warming has no message to
- route on, so neither triggers the (potentially heavy) selection. Selection never raises;
+ route on, so neither triggers the explicit repository mention match. Selection never raises;
any failure degrades to None — a repo-less sandbox — so it can't block the conversation.
"""
if conversation.task_id is not None:
diff --git a/products/tasks/backend/logic/repo_selection/cascade.py b/products/tasks/backend/logic/repo_selection/cascade.py
index 1fb2ee00d794..b9cc66ddf270 100644
--- a/products/tasks/backend/logic/repo_selection/cascade.py
+++ b/products/tasks/backend/logic/repo_selection/cascade.py
@@ -3,13 +3,7 @@ import logging
from posthog.git import extract_explicit_repo
from posthog.sync import database_sync_to_async
-from products.tasks.backend.logic.repo_selection.agent import (
- RepoSelectionRejectedError,
- RepoSelectionUnavailableError,
- _list_candidate_repos,
- resolve_team_github_integration,
- select_repository,
-)
+from products.tasks.backend.logic.repo_selection.agent import _list_candidate_repos, resolve_team_github_integration
from products.tasks.backend.models import Task
logger = logging.getLogger(__name__)
@@ -22,14 +16,11 @@ async def select_repository_for_message(
*,
origin_product: Task.OriginProduct,
) -> str | None:
- """Pick the repository a free-form chat message is most likely about, cheapest strategy first.
+ """Pick a connected repository only when the message names it explicitly.
- 1. An explicit `owner/repo` token in the message that matches a connected repo.
- 2. Delegated to `select_repository`, which returns the team's single eligible repo instantly,
- or runs the sandbox LLM agent when there are several candidates.
-
- Returns `None` when nothing connects (no integration, no candidates, the agent finds no
- plausible subject, or selection fails for any reason). Selection must never block the
+ The sandbox conversation open path must stay fast: it runs before the Run is created, so we
+ avoid the repo-selection LLM agent here. Messages without an explicit connected `owner/repo`
+ mention return `None`, which starts a repo-less sandbox. Selection must never block the
conversation from starting, so this never raises — every failure degrades to "no repo".
"""
try:
@@ -40,24 +31,7 @@ async def select_repository_for_message(
if not candidates:
return None
- explicit = extract_explicit_repo(message, candidates)
- if explicit:
- return explicit
-
- # Reuse the integration and candidate list already resolved above so the heavy
- # multi-repo path doesn't re-run both queries inside `select_repository`.
- result = await select_repository(
- team_id,
- user_id,
- context=message,
- origin_product=origin_product,
- github=github,
- candidate_repos=candidates,
- )
- return result.repository
- except (RepoSelectionRejectedError, RepoSelectionUnavailableError) as e:
- logger.info("repo_selection_for_message.no_selection team_id=%s reason=%s", team_id, e)
- return None
+ return extract_explicit_repo(message, candidates)
except Exception:
logger.warning("repo_selection_for_message.failed team_id=%s", team_id, exc_info=True)
return None
diff --git a/products/tasks/backend/tests/test_repo_selection_cascade.py b/products/tasks/backend/tests/test_repo_selection_cascade.py
index 21249879418e..04590f00685f 100644
--- a/products/tasks/backend/tests/test_repo_selection_cascade.py
+++ b/products/tasks/backend/tests/test_repo_selection_cascade.py
@@ -1,12 +1,5 @@
-from unittest.mock import AsyncMock, MagicMock, patch
+from unittest.mock import MagicMock, patch
-from parameterized import parameterized
-
-from products.tasks.backend.logic.repo_selection.agent import (
- RepoSelectionRejectedError,
- RepoSelectionResult,
- RepoSelectionUnavailableError,
-)
from products.tasks.backend.logic.repo_selection.cascade import select_repository_for_message
from products.tasks.backend.models import Task
@@ -27,55 +20,20 @@ async def _run(message: str) -> str | None:
class TestSelectRepositoryForMessage:
async def test_no_github_integration_returns_none(self):
resolve, list_repos = _patch_candidates(None, [])
- with resolve, list_repos, patch(f"{_CASCADE}.select_repository", new=AsyncMock()) as select:
+ with resolve, list_repos:
assert await _run("anything") is None
- select.assert_not_called()
async def test_no_candidates_returns_none(self):
resolve, list_repos = _patch_candidates(MagicMock(), [])
- with resolve, list_repos, patch(f"{_CASCADE}.select_repository", new=AsyncMock()) as select:
+ with resolve, list_repos:
assert await _run("anything") is None
- select.assert_not_called()
async def test_explicit_mention_short_circuits(self):
resolve, list_repos = _patch_candidates(MagicMock(), ["posthog/posthog", "posthog/posthog-js"])
- with resolve, list_repos, patch(f"{_CASCADE}.select_repository", new=AsyncMock()) as select:
+ with resolve, list_repos:
assert await _run("please fix posthog/posthog-js") == "posthog/posthog-js"
- select.assert_not_called()
-
- async def test_delegates_to_select_repository_without_explicit_mention(self):
- resolve, list_repos = _patch_candidates(MagicMock(), ["posthog/posthog", "posthog/posthog-js"])
- result = RepoSelectionResult(repository="posthog/posthog", reason="agent picked it")
- with resolve, list_repos, patch(f"{_CASCADE}.select_repository", new=AsyncMock(return_value=result)) as select:
- assert await _run("the dashboards are slow") == "posthog/posthog"
- select.assert_awaited_once()
-
- async def test_forwards_resolved_integration_and_candidates(self):
- github = MagicMock()
- candidates = ["posthog/posthog", "posthog/posthog-js"]
- resolve, list_repos = _patch_candidates(github, candidates)
- result = RepoSelectionResult(repository="posthog/posthog", reason="agent picked it")
- with resolve, list_repos, patch(f"{_CASCADE}.select_repository", new=AsyncMock(return_value=result)) as select:
- await _run("the dashboards are slow")
- await_args = select.await_args
- assert await_args is not None
- assert await_args.kwargs["github"] is github
- assert await_args.kwargs["candidate_repos"] == candidates
- async def test_select_repository_null_returns_none(self):
- resolve, list_repos = _patch_candidates(MagicMock(), ["posthog/posthog", "posthog/posthog-js"])
- result = RepoSelectionResult(repository=None, reason="no plausible candidate")
- with resolve, list_repos, patch(f"{_CASCADE}.select_repository", new=AsyncMock(return_value=result)):
- assert await _run("a question about billing") is None
-
- @parameterized.expand(
- [
- ("rejected", RepoSelectionRejectedError("posthog/hallucinated", "made it up")),
- ("unavailable", RepoSelectionUnavailableError("all archived")),
- ("unexpected", RuntimeError("sandbox boot failed")),
- ]
- )
- async def test_selection_failure_degrades_to_none(self, _name: str, error: Exception):
+ async def test_multi_candidate_without_explicit_mention_returns_none(self):
resolve, list_repos = _patch_candidates(MagicMock(), ["posthog/posthog", "posthog/posthog-js"])
- with resolve, list_repos, patch(f"{_CASCADE}.select_repository", new=AsyncMock(side_effect=error)):
+ with resolve, list_repos:
assert await _run("the dashboards are slow") is None