: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(phai): stop presenting subagent saved insights as unlinkable artifacts (#75577)
Christiaan Hendriksen4cfb8bf99ef2
diff --git a/ee/hogai/tools/task.py b/ee/hogai/tools/task.pyindex e73142cbe789..e463f252c2cc 100644--- a/ee/hogai/tools/task.py+++ b/ee/hogai/tools/task.py@@ -8,6 +8,7 @@ from pydantic import BaseModel, Field, create_modelfrom posthog.schema import (AgentMode,ArtifactMessage,+ ArtifactSource,AssistantEventType,AssistantGenerationStatusEvent,AssistantMessage,@@ -22,6 +23,7 @@ from ee.hogai.artifacts.utils import unwrap_notebook_artifact_content, unwrap_vifrom ee.hogai.context.context import AssistantContextManagerfrom ee.hogai.stream.redis_stream import get_subagent_stream_keyfrom ee.hogai.tool import MaxTool, ToolMessagesArtifact+from ee.hogai.utils.helpers import build_insight_urlfrom ee.hogai.utils.prompt import format_prompt_stringfrom ee.hogai.utils.types.base import AssistantState, NodePath@@ -95,8 +97,10 @@ TASK_ARTIFACTS_PROMPT = """The following artifacts have been generated:{{{artifacts_list}}}+{{#has_transient_artifacts}}-Any `Artifact ID` above is scoped to this conversation, not an insight short ID, so an `/insights/...` link built from it would 404. If the user wants a saved insight, tell them to open the chart as a new insight from the icon below it and save it from there.+Any `Artifact ID` above is scoped to this conversation, not an insight short ID, so an `/insights/...` link built from it would 404. If the user wants a saved insight, tell them to open the chart as a new insight from the icon below it and save it from there. Any `Insight ID` above is a saved insight, and its insight URL is a real link you can give the user.+{{/has_transient_artifacts}}"""@@ -184,12 +188,19 @@ class TaskTool(MaxTool):artifacts_prompt = ""if len(artifact_messages) > 0:artifacts_list_prompt = []+ has_transient_artifacts = Falsefor message in artifact_messages:viz_content = unwrap_visualization_artifact_content(message)if viz_content:- artifacts_list_prompt.append(- f"- Artifact ID: {message.artifact_id}\nName: {viz_content.name}\nDescription: {viz_content.description}\nQuery: {viz_content.query}"- )+ if message.source == ArtifactSource.INSIGHT:+ artifacts_list_prompt.append(+ f"- Insight ID: {message.artifact_id}\nName: {viz_content.name}\nDescription: {viz_content.description}\nQuery: {viz_content.query}\nInsight URL: {build_insight_url(message.artifact_id)}"+ )+ else:+ has_transient_artifacts = True+ artifacts_list_prompt.append(+ f"- Artifact ID: {message.artifact_id}\nName: {viz_content.name}\nDescription: {viz_content.description}\nQuery: {viz_content.query}"+ )continuenotebook_content = unwrap_notebook_artifact_content(message)if notebook_content:@@ -197,7 +208,9 @@ class TaskTool(MaxTool):f"- Notebook ID: {message.artifact_id}\nTitle: {notebook_content.title}")artifacts_prompt = format_prompt_string(- TASK_ARTIFACTS_PROMPT, artifacts_list="\n\n".join(artifacts_list_prompt)+ TASK_ARTIFACTS_PROMPT,+ artifacts_list="\n\n".join(artifacts_list_prompt),+ has_transient_artifacts=has_transient_artifacts,)result_prompt = format_prompt_string(diff --git a/ee/hogai/tools/test/test_task.py b/ee/hogai/tools/test/test_task.pyindex 8eae861cf109..f40498a2dbed 100644--- a/ee/hogai/tools/test/test_task.py+++ b/ee/hogai/tools/test/test_task.py@@ -2,6 +2,7 @@ from posthog.test.base import ClickhouseTestMixin, NonAtomicBaseTestfrom unittest.mock import MagicMock, patchfrom langchain_core.runnables import RunnableConfig+from parameterized import parameterizedfrom posthog.schema import (AgentMode,@@ -102,7 +103,13 @@ class TestTaskTool(ClickhouseTestMixin, NonAtomicBaseTest):self.assertIn("Final response", result_text)self.assertIsNone(artifact)- async def test_arun_impl_handles_artifact_messages(self):+ @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@@ -110,7 +117,7 @@ class TestTaskTool(ClickhouseTestMixin, NonAtomicBaseTest):artifact_message = ArtifactMessage(id="artifact_1",artifact_id="short_id_123",- source=ArtifactSource.ARTIFACT,+ source=source,content=VisualizationArtifactContent(query=AssistantTrendsQuery(series=[]),name="Test",@@ -137,11 +144,17 @@ class TestTaskTool(ClickhouseTestMixin, NonAtomicBaseTest):self.assertIn("Done", result_text)self.assertIsNone(artifact)- self.assertIn("Artifact ID: short_id_123", result_text)self.assertIn("Test", 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)+ 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()