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.

chore(tests): deflake rerun-prone backend tests batch 2 (#69775)

Raúl Negrón-Otero729ace2f020f
diff --git a/ee/hogai/chat_agent/test/test_mode_manager.py b/ee/hogai/chat_agent/test/test_mode_manager.py
index 9960ca1e5368..db41072a4ebd 100644
--- a/ee/hogai/chat_agent/test/test_mode_manager.py
+++ b/ee/hogai/chat_agent/test/test_mode_manager.py
@@ -1,5 +1,4 @@
import datetime
-from contextlib import contextmanager
from posthog.test.base import BaseTest, ClickhouseTestMixin
from unittest.mock import AsyncMock, MagicMock, patch
@@ -46,21 +45,11 @@ from ee.hogai.core.agent_modes.presets.product_analytics import ReadOnlyProductA
from ee.hogai.core.agent_modes.presets.survey import ReadOnlySurveyAgentToolkit
from ee.hogai.tools import CreateInsightTool, UpsertDashboardTool
from ee.hogai.tools.replay.filter_session_recordings import FilterSessionRecordingsTool
-from ee.hogai.utils.tests import FakeChatAnthropic, FakeChatOpenAI
+from ee.hogai.utils.tests import FakeChatAnthropic, FakeChatOpenAI, mock_contextual_tool
from ee.hogai.utils.types import AssistantState, PartialAssistantState
from ee.hogai.utils.types.base import AssistantNodeName, NodePath
-@contextmanager
-def mock_contextual_tool(mock_tool):
- """Helper to mock a contextual tool class with create_tool_class"""
- mock_tool_class = MagicMock()
- mock_tool_class.create_tool_class = AsyncMock(return_value=mock_tool)
-
- with patch("ee.hogai.registry.get_contextual_tool_class", return_value=mock_tool_class):
- yield
-
-
def _create_agent_node(
team: Team,
user: User,
@@ -113,10 +102,16 @@ def _create_agent_tools_node(
class TestAgentToolkit(BaseTest):
@patch("ee.hogai.core.agent_modes.executables.AgentExecutable._get_model")
@patch("ee.hogai.registry.get_contextual_tool_class")
- async def test_get_tools_ignores_unknown_contextual_tools(self, mock_get_tool_class, mock_model):
+ # toolkit.py imports get_contextual_tool_class by value at module level, so the
+ # registry patch alone never reaches it and the real import-scanning path runs
+ @patch("ee.hogai.chat_agent.toolkit.get_contextual_tool_class")
+ async def test_get_tools_ignores_unknown_contextual_tools(
+ self, mock_toolkit_get_tool_class, mock_get_tool_class, mock_model
+ ):
"""Test that unknown contextual tools (None from get_contextual_tool_class) are ignored"""
mock_model.return_value = FakeChatOpenAI(responses=[LangchainAIMessage(content="Response")])
mock_get_tool_class.return_value = None # Simulates unknown tool
+ mock_toolkit_get_tool_class.return_value = None
state = AssistantState(messages=[HumanMessage(content="Test")])
config = RunnableConfig(configurable={"contextual_tools": {"unknown_tool": {"some": "config"}}})
@@ -694,7 +689,7 @@ class TestRootNodeTools(BaseTest):
root_tool_call_id="tool-123",
)
- with patch("ee.hogai.registry.get_contextual_tool_class", return_value=None):
+ with patch("ee.hogai.chat_agent.toolkit.get_contextual_tool_class", return_value=None):
result = await node.arun(state, {})
self.assertIsInstance(result, PartialAssistantState)
diff --git a/ee/hogai/core/agent_modes/test/test_executables.py b/ee/hogai/core/agent_modes/test/test_executables.py
index 602432a80c00..289bbceaef7a 100644
--- a/ee/hogai/core/agent_modes/test/test_executables.py
+++ b/ee/hogai/core/agent_modes/test/test_executables.py
@@ -1,5 +1,3 @@
-from contextlib import contextmanager
-
from posthog.test.base import BaseTest, ClickhouseTestMixin
from unittest.mock import AsyncMock, MagicMock, patch
@@ -30,16 +28,6 @@ from ee.hogai.utils.types import AssistantState, PartialAssistantState
from ee.hogai.utils.types.base import AssistantMessageUnion, AssistantNodeName, NodePath
-@contextmanager
-def mock_contextual_tool(mock_tool):
- """Helper to mock a contextual tool class with create_tool_class"""
- mock_tool_class = MagicMock()
- mock_tool_class.create_tool_class = AsyncMock(return_value=mock_tool)
-
- with patch("ee.hogai.registry.get_contextual_tool_class", return_value=mock_tool_class):
- yield
-
-
def _create_agent_node(
team: Team,
user: User,
diff --git a/ee/hogai/utils/tests.py b/ee/hogai/utils/tests.py
index ed1a3e5f318f..537b97708c4f 100644
--- a/ee/hogai/utils/tests.py
+++ b/ee/hogai/utils/tests.py
@@ -1,4 +1,7 @@
from collections.abc import Sequence
+from contextlib import contextmanager
+
+from unittest.mock import AsyncMock, MagicMock, patch
from langchain_core.language_models import FakeMessagesListChatModel
from langchain_core.messages import BaseMessage, convert_to_messages, convert_to_openai_messages
@@ -7,6 +10,21 @@ from langchain_openai import ChatOpenAI
from pydantic import Field
+@contextmanager
+def mock_contextual_tool(mock_tool):
+ """Helper to mock a contextual tool class with create_tool_class"""
+ mock_tool_class = MagicMock()
+ mock_tool_class.create_tool_class = AsyncMock(return_value=mock_tool)
+
+ # toolkit.py imports get_contextual_tool_class by value at module level,
+ # so it must be patched there too, not just on the registry module
+ with (
+ patch("ee.hogai.registry.get_contextual_tool_class", return_value=mock_tool_class),
+ patch("ee.hogai.chat_agent.toolkit.get_contextual_tool_class", return_value=mock_tool_class),
+ ):
+ yield
+
+
class TokenCounterMixin:
model: str
diff --git a/posthog/management/commands/test/test_sync_persons_to_clickhouse.py b/posthog/management/commands/test/test_sync_persons_to_clickhouse.py
index 9f1deebe51ee..2e7f8687a5d9 100644
--- a/posthog/management/commands/test/test_sync_persons_to_clickhouse.py
+++ b/posthog/management/commands/test/test_sync_persons_to_clickhouse.py
@@ -215,7 +215,10 @@ class TestSyncPersonsToClickHouse(NonAtomicBaseTest, ClickhouseTestMixin):
created_at=ts,
version=0,
)
- raw_create_group_ch(self.team.pk, 2, "group-key", {"a": 5}, ts)
+ # Seed with _timestamp=ts so the sync's re-insert is strictly newer: groups is
+ # ReplacingMergeTree(ver=_timestamp) with second resolution, and a same-second tie
+ # makes ORDER BY _timestamp DESC pick between the two rows arbitrarily
+ raw_create_group_ch(self.team.pk, 2, "group-key", {"a": 5}, ts, timestamp=ts)
with persons_db_connection(writer=True, autocommit=True) as conn, conn.cursor() as cursor:
cursor.execute(
"UPDATE posthog_group SET group_properties = %s WHERE id = %s",