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(phai): stop linking artifact IDs as saved insight URLs (#85317)

Carlos Marchal6986286ac622
diff --git a/ee/hogai/chat_agent/insights/nodes.py b/ee/hogai/chat_agent/insights/nodes.py
index 679b21ceaeba..588ae95af185 100644
--- a/ee/hogai/chat_agent/insights/nodes.py
+++ b/ee/hogai/chat_agent/insights/nodes.py
@@ -192,7 +192,7 @@ class InsightSearchNode(AssistantNode):
self._evaluation_selections[insight_id] = {"insight": insight, "explanation": explanation}
name = insight["name"] or insight["derived_name"] or "Unnamed"
- insight_url = build_insight_url(self._team, insight["short_id"])
+ insight_url = build_insight_url(insight["short_id"])
return f"Selected insight {insight_id}: {name} (url: {insight_url})"
@tool
@@ -618,7 +618,7 @@ class InsightSearchNode(AssistantNode):
except Exception as e:
capture_exception(e)
- insight_url = build_insight_url(self._team, insight["short_id"])
+ insight_url = build_insight_url(insight["short_id"])
hyperlink_format = f"[{name}]({insight_url})"
summary_parts = [
@@ -829,7 +829,7 @@ class InsightSearchNode(AssistantNode):
visualization_messages.append(visualization_message)
insight_name = insight["name"] or insight["derived_name"] or "Unnamed"
- insight_url = build_insight_url(self._team, insight["short_id"])
+ insight_url = build_insight_url(insight["short_id"])
insight_hyperlink = f"[{insight_name}]({insight_url})"
explanations.append(
f"- {insight_hyperlink} (Artifact ID: {insight['short_id']}): {selection['explanation']}"
diff --git a/ee/hogai/chat_agent/query_executor/test/test_nodes.py b/ee/hogai/chat_agent/query_executor/test/test_nodes.py
index 91d84783d42d..5a3ea736eecd 100644
--- a/ee/hogai/chat_agent/query_executor/test/test_nodes.py
+++ b/ee/hogai/chat_agent/query_executor/test/test_nodes.py
@@ -145,7 +145,8 @@ class TestQueryExecutorNode(ClickhouseTestMixin, NonAtomicBaseTest):
mock_process_query_dict.assert_called_once() # Query processing started
msg = cast(AssistantToolCallMessage, new_state.messages[0])
self.assertIn("Here is the results table of the TrendsQuery insight:", msg.content)
- self.assertIn(f"Insight ID: {insight.short_id}", msg.content)
+ # The node renders an ephemeral artifact, so the ID is labelled as one and carries no URL
+ self.assertIn(f"Artifact ID: {insight.short_id}", msg.content)
self.assertIn("Name: test insight", msg.content)
self.assertIn("Description: test description", msg.content)
self.assertEqual(msg.type, "tool")
diff --git a/ee/hogai/context/context.py b/ee/hogai/context/context.py
index eb92be309eca..da7b199d885e 100644
--- a/ee/hogai/context/context.py
+++ b/ee/hogai/context/context.py
@@ -532,6 +532,8 @@ class AssistantContextManager(AssistantContextMixin):
name=insight.name,
description=insight.description,
insight_id=insight.id,
+ # `MaxInsightContext.id` is a saved insight's short_id
+ insight_short_id=insight.id,
dashboard_filters=dashboard_filters,
filters_override=filters_override,
variables_override=variables_override,
diff --git a/ee/hogai/context/dashboard/context.py b/ee/hogai/context/dashboard/context.py
index 45e8194f6215..423abe3aed04 100644
--- a/ee/hogai/context/dashboard/context.py
+++ b/ee/hogai/context/dashboard/context.py
@@ -66,7 +66,7 @@ class DashboardContext:
self.name = name
self.description = description
self.dashboard_id = dashboard_id
- self.dashboard_url = build_dashboard_url(team, int(dashboard_id)) if dashboard_id else None
+ self.dashboard_url = build_dashboard_url(int(dashboard_id)) if dashboard_id else None
self.dashboard_filters = dashboard_filters
self._semaphore = asyncio.Semaphore(max_concurrent_queries)
diff --git a/ee/hogai/context/dashboard/test/test_context.py b/ee/hogai/context/dashboard/test/test_context.py
index bdfc8038ca6f..8b100adebfa6 100644
--- a/ee/hogai/context/dashboard/test/test_context.py
+++ b/ee/hogai/context/dashboard/test/test_context.py
@@ -236,7 +236,7 @@ class TestDashboardContext(BaseTest):
self.assertIn("Dashboard name: Dashboard", result)
self.assertIn("Dashboard ID: 606", result)
- self.assertIn(f"Dashboard URL: /project/{self.team.id}/dashboard/606", result)
+ self.assertIn("Dashboard URL: /dashboard/606", result)
async def test_dashboard_url_included_in_format_schema(self):
dashboard_ctx = DashboardContext(
@@ -249,7 +249,7 @@ class TestDashboardContext(BaseTest):
result = await dashboard_ctx.format_schema()
- self.assertIn(f"Dashboard URL: /project/{self.team.id}/dashboard/12345", result)
+ self.assertIn("Dashboard URL: /dashboard/12345", result)
@patch("ee.hogai.context.insight.context.execute_and_format_query")
async def test_dashboard_url_included_in_execute_and_format(self, mock_execute):
@@ -273,7 +273,7 @@ class TestDashboardContext(BaseTest):
result = await dashboard_ctx.execute_and_format()
- self.assertIn(f"Dashboard URL: /project/{self.team.id}/dashboard/67890", result)
+ self.assertIn("Dashboard URL: /dashboard/67890", result)
@patch("ee.hogai.context.insight.context.execute_and_format_query")
async def test_custom_max_concurrent_queries(self, mock_execute):
diff --git a/ee/hogai/context/insight/context.py b/ee/hogai/context/insight/context.py
index aff556a23b34..11ef99d4133f 100644
--- a/ee/hogai/context/insight/context.py
+++ b/ee/hogai/context/insight/context.py
@@ -24,6 +24,9 @@ class InsightContext:
Accepts insight data directly and provides methods to format schema or execute and format results.
Supports optional dashboard filter/variable overrides before execution.
+
+ `insight_id` is only the identifier shown to the model; `insight_short_id` is the one that resolves to a
+ URL, so an ephemeral artifact passes the former and leaves the latter unset.
"""
def __init__(
@@ -60,7 +63,7 @@ class InsightContext:
def insight_url(self) -> str | None:
"""Generate insight URL from insight_short_id if available."""
if self.insight_short_id:
- return build_insight_url(self.team, self.insight_short_id)
+ return build_insight_url(self.insight_short_id)
return None
@classmethod
@@ -112,7 +115,6 @@ class InsightContext:
insight_description=self.description,
query_schema=query_schema,
results=results,
- include_url_reminder=self.insight_id is None,
insight_url=self.insight_url,
)
@@ -126,7 +128,6 @@ class InsightContext:
insight_id=self.insight_id,
insight_description=self.description,
query_schema=query_schema,
- include_url_reminder=self.insight_id is None,
insight_url=self.insight_url,
)
diff --git a/ee/hogai/context/insight/prompts.py b/ee/hogai/context/insight/prompts.py
index d91c1d465202..08c07180e67d 100644
--- a/ee/hogai/context/insight/prompts.py
+++ b/ee/hogai/context/insight/prompts.py
@@ -1,7 +1,7 @@
INSIGHT_RESULT_TEMPLATE = """
Name: {{{insight_name}}}
{{#insight_id}}
-Insight ID: {{{insight_id}}}
+{{#insight_url}}Insight ID: {{{insight_id}}}{{/insight_url}}{{^insight_url}}Artifact ID: {{{insight_id}}}{{/insight_url}}
{{/insight_id}}
{{#insight_description}}
Description: {{{insight_description}}}
@@ -10,7 +10,7 @@ Description: {{{insight_description}}}
Insight URL: {{{insight_url}}}
{{/insight_url}}
{{^insight_url}}
-This insight cannot be accessed via a URL.
+This insight is not saved in the project, so it cannot be accessed via a URL. 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.
{{/insight_url}}
{{#query_schema}}
@@ -46,8 +46,7 @@ The current date and time is {{{utc_datetime_display}}} UTC, which is {{{project
Always add `LIMIT 100` to your queries. The maximum allowed limit is 500 rows. If you need more data, paginate using LIMIT and OFFSET in subsequent queries.
{{/sql_query}}
It's expected that the data point for the current period may show a drop in value, as data collection for it is still ongoing. Do not point this out.
-Do not copy the results table as the user sees it in the UI.{{#include_url_reminder}}
-{{/include_url_reminder}}
+Do not copy the results table as the user sees it in the UI.
{{#has_truncated_values}}
Some JSON/array values were truncated. You can write a more specific SQL query to explore individual properties or array elements if needed.
{{/has_truncated_values}}
diff --git a/ee/hogai/context/insight/test/test_context.py b/ee/hogai/context/insight/test/test_context.py
index abdbf66e5462..a37dc88ec894 100644
--- a/ee/hogai/context/insight/test/test_context.py
+++ b/ee/hogai/context/insight/test/test_context.py
@@ -267,7 +267,7 @@ class TestInsightContext(BaseTest):
query = AssistantTrendsQuery(series=[AssistantTrendsEventsNode(name="$pageview")])
context = InsightContext(team=self.team, query=query, user=self.user, insight_short_id="abc123")
- self.assertEqual(context.insight_url, f"/project/{self.team.id}/insights/abc123")
+ self.assertEqual(context.insight_url, "/insights/abc123")
@patch("ee.hogai.context.insight.context.execute_and_format_query")
async def test_execute_and_format_includes_insight_url(self, mock_execute):
@@ -285,7 +285,8 @@ class TestInsightContext(BaseTest):
result = await context.execute_and_format()
- self.assertIn(f"/project/{self.team.id}/insights/xyz789", result)
+ self.assertIn("/insights/xyz789", result)
+ self.assertIn("Insight ID: display-id", result)
@patch("ee.hogai.context.insight.context.execute_and_format_query")
async def test_execute_and_format_shows_fallback_when_no_url(self, mock_execute):
@@ -297,8 +298,14 @@ class TestInsightContext(BaseTest):
query=query,
user=self.user,
name="Test Insight",
+ insight_id="cJcS",
)
result = await context.execute_and_format()
- self.assertIn("This insight cannot be accessed via a URL.", result)
+ self.assertIn("cannot be accessed via a URL", result)
+ # An artifact ID under an `Insight ID:` label is what led the model to compose `/insights/cJcS`, which 404s
+ self.assertIn("Artifact ID: cJcS", result)
+ self.assertNotIn("Insight ID:", result)
+ self.assertIn("/insights/", result)
+ self.assertIn("404", result)
diff --git a/ee/hogai/context/test/test_context.py b/ee/hogai/context/test/test_context.py
index da079f72b5b2..cac2ce093ad3 100644
--- a/ee/hogai/context/test/test_context.py
+++ b/ee/hogai/context/test/test_context.py
@@ -73,6 +73,24 @@ class TestAssistantContextManager(BaseTest):
self.assertIn("Trend results: 100 users", result)
mock_execute.assert_called_once()
+ @patch("ee.hogai.context.insight.context.execute_and_format_query")
+ async def test_build_and_execute_insight_links_to_saved_insight(self, mock_execute):
+ mock_execute.return_value = "Trend results: 100 users"
+
+ insight = MaxInsightContext(
+ id="123",
+ name="User Trends",
+ description=None,
+ query=TrendsQuery(series=[EventsNode(event="pageview")]),
+ )
+
+ insight_ctx = self.context_manager._build_insight_context(insight, dashboard_filters=None)
+ result = await self.context_manager._execute_and_format_insight(insight_ctx)
+ assert result is not None
+ # A UI-attached insight is always saved, so Max gets a real link rather than the unsaved-artifact warning
+ self.assertIn("Insight URL: /insights/123", result)
+ self.assertNotIn("cannot be accessed via a URL", result)
+
@patch("ee.hogai.context.insight.context.execute_and_format_query")
async def test_build_and_execute_insight_funnel_query(self, mock_execute):
mock_execute.return_value = "Funnel results: 50% conversion"
diff --git a/ee/hogai/tools/task.py b/ee/hogai/tools/task.py
index 56c85d61eb72..e73142cbe789 100644
--- a/ee/hogai/tools/task.py
+++ b/ee/hogai/tools/task.py
@@ -95,6 +95,8 @@ TASK_ARTIFACTS_PROMPT = """
The following artifacts have been generated:
{{{artifacts_list}}}
+
+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.
"""
@@ -186,7 +188,7 @@ class TaskTool(MaxTool):
viz_content = unwrap_visualization_artifact_content(message)
if viz_content:
artifacts_list_prompt.append(
- f"- Insight ID: {message.artifact_id}\nName: {viz_content.name}\nDescription: {viz_content.description}\nQuery: {viz_content.query}"
+ f"- Artifact ID: {message.artifact_id}\nName: {viz_content.name}\nDescription: {viz_content.description}\nQuery: {viz_content.query}"
)
continue
notebook_content = unwrap_notebook_artifact_content(message)
diff --git a/ee/hogai/tools/test/test_task.py b/ee/hogai/tools/test/test_task.py
index 53161e7ee240..8eae861cf109 100644
--- a/ee/hogai/tools/test/test_task.py
+++ b/ee/hogai/tools/test/test_task.py
@@ -137,8 +137,11 @@ class TestTaskTool(ClickhouseTestMixin, NonAtomicBaseTest):
self.assertIn("Done", result_text)
self.assertIsNone(artifact)
- self.assertIn("short_id_123", result_text)
+ 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)
async def test_arun_impl_dispatches_tool_call_updates(self):
tool = self._create_tool()
diff --git a/ee/hogai/tools/upsert_dashboard/test/test_tool.py b/ee/hogai/tools/upsert_dashboard/test/test_tool.py
index 0bb011cadd88..3129a30f7fbb 100644
--- a/ee/hogai/tools/upsert_dashboard/test/test_tool.py
+++ b/ee/hogai/tools/upsert_dashboard/test/test_tool.py
@@ -136,7 +136,7 @@ class TestUpsertDashboardTool(BaseTest):
result, _ = await tool._arun_impl(action)
dashboard = await Dashboard.objects.aget(name="URL Dashboard")
- expected_url = f"/project/{self.team.id}/dashboard/{dashboard.id}"
+ expected_url = f"/dashboard/{dashboard.id}"
self.assertIn(f"Dashboard URL: {expected_url}", result)
self.assertNotIn("/dashboards/", result)
@@ -159,7 +159,7 @@ class TestUpsertDashboardTool(BaseTest):
result, _ = await tool._arun_impl(action)
- expected_url = f"/project/{self.team.id}/dashboard/{dashboard.id}"
+ expected_url = f"/dashboard/{dashboard.id}"
self.assertIn(f"Dashboard URL: {expected_url}", result)
self.assertNotIn("/dashboards/", result)
diff --git a/ee/hogai/utils/helpers.py b/ee/hogai/utils/helpers.py
index 7a7252c076c3..087ce2ba8ec9 100644
--- a/ee/hogai/utils/helpers.py
+++ b/ee/hogai/utils/helpers.py
@@ -509,14 +509,18 @@ def cast_assistant_query(
raise ValueError(f"Unsupported query type: {query.kind}")
-def build_insight_url(team: Team, id: str) -> str:
- """Build the URL for an insight."""
- return f"/project/{team.id}/insights/{id}"
+def build_insight_url(id: str) -> str:
+ """Build the URL for an insight.
+
+ Unprefixed by `/project/<id>`: these URLs are handed to the model, which is instructed to omit
+ that prefix, and the app resolves them against the project the user is already in.
+ """
+ return f"/insights/{id}"
-def build_dashboard_url(team: Team, id: int) -> str:
- """Build the URL for a dashboard."""
- return f"/project/{team.id}/dashboard/{id}"
+def build_dashboard_url(id: int) -> str:
+ """Build the URL for a dashboard. Unprefixed, for the same reason as `build_insight_url`."""
+ return f"/dashboard/{id}"
def extract_stream_update(update: Any) -> Any: