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.

9.8 KB
Raw Blame
from posthog.test.base import ClickhouseTestMixin, NonAtomicBaseTest
from unittest.mock import MagicMock, patch

from langchain_core.runnables import RunnableConfig
from parameterized import parameterized

from posthog.schema import (
    AgentMode,
    ArtifactMessage,
    ArtifactSource,
    AssistantEventType,
    AssistantMessage,
    AssistantToolCall,
    AssistantUpdateEvent,
    VisualizationArtifactContent,
)

from products.posthog_ai.backend.models.assistant import Conversation

from ee.hogai.context.context import AssistantContextManager
from ee.hogai.stream.redis_stream import get_subagent_stream_key
from ee.hogai.tools.subagent_executor import SubagentExecutor
from ee.hogai.tools.task import TaskTool
from ee.hogai.utils.types import AssistantState
from ee.hogai.utils.types.base import NodePath


class TestSubagentExecutor(ClickhouseTestMixin, NonAtomicBaseTest):
    CLASS_DATA_LEVEL_SETUP = False

    def setUp(self):
        super().setUp()
        self.conversation = Conversation.objects.create(team=self.team, user=self.user)

    def test_executor_sets_stream_key(self):
        tool_call_id = "test_tool_call"
        execution_id = "test_execution"
        executor = SubagentExecutor(
            conversation=self.conversation, tool_call_id=tool_call_id, execution_id=execution_id
        )

        expected_stream_key = get_subagent_stream_key(self.conversation.id, f"{tool_call_id}-{execution_id}")
        self.assertEqual(executor._redis_stream._stream_key, expected_stream_key)

    def test_executor_sets_workflow_id(self):
        tool_call_id = "test_tool_call"
        execution_id = "test_execution"
        executor = SubagentExecutor(
            conversation=self.conversation, tool_call_id=tool_call_id, execution_id=execution_id
        )

        expected_workflow_id = f"subagent-{self.conversation.id}-{tool_call_id}-{execution_id}"
        self.assertEqual(executor._workflow_id, expected_workflow_id)

    def test_executor_disables_reconnect(self):
        executor = SubagentExecutor(conversation=self.conversation, tool_call_id="test", execution_id="test_exec")
        self.assertFalse(executor._reconnectable)


class TestTaskTool(ClickhouseTestMixin, NonAtomicBaseTest):
    CLASS_DATA_LEVEL_SETUP = False

    def setUp(self):
        super().setUp()
        self.conversation = Conversation.objects.create(team=self.team, user=self.user)
        self.tool_call_id = "test_tool_call_id"

    def _create_tool(self, state: AssistantState | None = None):
        if state is None:
            state = AssistantState(messages=[])

        config = RunnableConfig(configurable={"thread_id": str(self.conversation.id)})
        context_manager = AssistantContextManager(team=self.team, user=self.user, config=config)

        return TaskTool(
            team=self.team,
            user=self.user,
            state=state,
            context_manager=context_manager,
            config=config,
            node_path=(NodePath(name="test_node", tool_call_id=self.tool_call_id, message_id="test"),),
        )

    async def test_arun_impl_success_returns_final_response(self):
        tool = self._create_tool()

        async def mock_astream(*args, **kwargs):
            yield (AssistantEventType.MESSAGE, AssistantMessage(content="Final response", id="msg_1"))

        mock_dispatcher = MagicMock()

        with (
            patch.object(SubagentExecutor, "astream", mock_astream),
            patch(
                "ee.hogai.core.mixins.create_dispatcher_from_config",
                return_value=mock_dispatcher,
            ),
        ):
            result_text, artifact = await tool._arun_impl(
                title="Test", task="Do something", agent_mode=AgentMode.PRODUCT_ANALYTICS
            )

        self.assertIn("Final response", result_text)
        self.assertIsNone(artifact)

    @parameterized.expand(
        [
            ("transient_artifact", ArtifactSource.ARTIFACT),
            ("saved_insight", ArtifactSource.INSIGHT),
        ]
    )
    async def test_arun_impl_handles_artifact_messages(self, _name, source):
        tool = self._create_tool()

        from posthog.schema import AssistantTrendsQuery

        artifact_message = ArtifactMessage(
            id="artifact_1",
            artifact_id="short_id_123",
            source=source,
            content=VisualizationArtifactContent(
                query=AssistantTrendsQuery(series=[]),
                name="Test",
                description="Test",
            ),
        )

        async def mock_astream(*args, **kwargs):
            yield (AssistantEventType.MESSAGE, artifact_message)
            yield (AssistantEventType.MESSAGE, AssistantMessage(content="Done", id="msg_1"))

        mock_dispatcher = MagicMock()

        with (
            patch.object(SubagentExecutor, "astream", mock_astream),
            patch(
                "ee.hogai.core.mixins.create_dispatcher_from_config",
                return_value=mock_dispatcher,
            ),
        ):
            result_text, artifact = await tool._arun_impl(
                title="Test", task="Do something", agent_mode=AgentMode.PRODUCT_ANALYTICS
            )

        self.assertIn("Done", result_text)
        self.assertIsNone(artifact)
        self.assertIn("Test", result_text)
        if source == ArtifactSource.INSIGHT:
            self.assertIn("Insight ID: short_id_123", result_text)
            self.assertIn("Insight URL: /insights/short_id_123", result_text)
            self.assertNotIn("Artifact ID:", result_text)
            self.assertNotIn("404", result_text)
        else:
            self.assertIn("Artifact ID: short_id_123", result_text)
            # Labelled as an insight, this handoff had the parent compose `/insights/short_id_123`, which 404s
            self.assertNotIn("Insight ID:", result_text)
            self.assertIn("404", result_text)

    async def test_arun_impl_dispatches_tool_call_updates(self):
        tool = self._create_tool()

        tool_call = AssistantToolCall(id="tc_1", name="some_tool", args={})
        message_with_tool_calls = AssistantMessage(content="Calling tool", id="msg_1", tool_calls=[tool_call])

        async def mock_astream(*args, **kwargs):
            yield (AssistantEventType.MESSAGE, message_with_tool_calls)
            yield (AssistantEventType.MESSAGE, AssistantMessage(content="Final", id="msg_2"))

        mock_dispatcher = MagicMock()

        with (
            patch.object(SubagentExecutor, "astream", mock_astream),
            patch(
                "ee.hogai.core.mixins.create_dispatcher_from_config",
                return_value=mock_dispatcher,
            ),
        ):
            await tool._arun_impl(title="Test", task="Do something", agent_mode=AgentMode.PRODUCT_ANALYTICS)

        # Verify tool call was dispatched as update
        mock_dispatcher.update.assert_called_with(content=tool_call)

    async def test_arun_impl_dispatches_update_events(self):
        tool = self._create_tool()

        update_event = AssistantUpdateEvent(content="Processing...", id="update_1", tool_call_id="tc_1")

        async def mock_astream(*args, **kwargs):
            yield (AssistantEventType.UPDATE, update_event)
            yield (AssistantEventType.MESSAGE, AssistantMessage(content="Done", id="msg_1"))

        mock_dispatcher = MagicMock()

        with (
            patch.object(SubagentExecutor, "astream", mock_astream),
            patch(
                "ee.hogai.core.mixins.create_dispatcher_from_config",
                return_value=mock_dispatcher,
            ),
        ):
            await tool._arun_impl(title="Test", task="Do something", agent_mode=AgentMode.PRODUCT_ANALYTICS)

        # Verify update was dispatched
        mock_dispatcher.update.assert_called_with("Processing...")

    async def test_arun_impl_error_handling(self):
        tool = self._create_tool()

        async def mock_astream_error(*args, **kwargs):
            yield (AssistantEventType.MESSAGE, AssistantMessage(content="Foobar", id="msg_1"))
            raise Exception("Stream error")

        mock_dispatcher = MagicMock()

        with (
            patch.object(SubagentExecutor, "astream", mock_astream_error),
            patch(
                "ee.hogai.core.mixins.create_dispatcher_from_config",
                return_value=mock_dispatcher,
            ),
        ):
            result_text, artifact = await tool._arun_impl(
                title="Test", task="Do something", agent_mode=AgentMode.PRODUCT_ANALYTICS
            )

        self.assertIn("Error running subagent", result_text)
        self.assertIn("Stream error", result_text)
        self.assertIsNone(artifact)


class TestTaskToolCreateToolClass(ClickhouseTestMixin, NonAtomicBaseTest):
    CLASS_DATA_LEVEL_SETUP = False

    async def test_create_tool_class_generates_description_with_modes(self):
        tool = await TaskTool.create_tool_class(team=self.team, user=self.user)

        # Check that description contains agent modes
        self.assertIn("product_analytics", tool.description)
        self.assertIn("sql", tool.description)
        self.assertIn("session_replay", tool.description)

    async def test_create_tool_class_includes_mode_descriptions(self):
        tool = await TaskTool.create_tool_class(team=self.team, user=self.user)

        # Description should contain mode information
        self.assertIn("Available modes", tool.description)

    async def test_create_tool_class_passes_through_params(self):
        state = AssistantState(messages=[])
        config = RunnableConfig(configurable={"test": "value"})
        node_path = (NodePath(name="test", tool_call_id="tc_1", message_id="msg_1"),)

        tool = await TaskTool.create_tool_class(
            team=self.team, user=self.user, state=state, config=config, node_path=node_path
        )

        self.assertEqual(tool._state, state)
        self.assertEqual(tool._config, config)
        self.assertEqual(tool._node_path, node_path)