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.

feat(replay-vision): render group-summary citations as slack links (#69396)

Kim Svatos Dugan7fe312d2033c
diff --git a/products/replay_vision/backend/temporal/vision_actions/synthesis.py b/products/replay_vision/backend/temporal/vision_actions/synthesis.py
index 7166d0dbb482..03d4dc3e4cfd 100644
--- a/products/replay_vision/backend/temporal/vision_actions/synthesis.py
+++ b/products/replay_vision/backend/temporal/vision_actions/synthesis.py
@@ -82,9 +82,9 @@ _SYSTEM_PROMPT = (
_MARKDOWN_HEADING_RE = re.compile(r"^#{1,6}\s*(.+?)\s*#*$", re.MULTILINE)
_MARKDOWN_BOLD_RE = re.compile(r"\*\*([^*]+)\*\*")
-# `[obs N]` citation markers the model emits (see `_fetch_observations`); the in-app view resolves them to
-# observation links, Slack drops them.
-_OBS_CITATION_RE = re.compile(r"\s*\[obs \d+\]")
+# `[obs N]` citation markers the model emits (see `_fetch_observations`); the in-app view and the Slack pass
+# both resolve them to observation links. The captured group is the 1-based observation number.
+_OBS_CITATION_RE = re.compile(r"\[obs (\d+)\]")
# Cap adjacent citations on the stored report so an over-cited theme renders a representative handful, not a
# wall of links. Cross-section variety stays the prompt's job.
_MAX_CITATIONS_PER_RUN = 6
@@ -158,7 +158,7 @@ def _synthesize(inputs: SynthesizeGroupSummaryInputs) -> SynthesizeGroupSummaryR
markdown = strip_external_links_markdown(
_summary_header(action, batch.window_start, len(batch.lines), batch.window_total) + markdown
)
- slack_text = _markdown_to_slack(markdown)
+ slack_text = _markdown_to_slack(markdown, team_id=team.id, observation_ids=batch.observation_ids)
run.synthesized_markdown = markdown
run.output = {"slack": slack_text}
@@ -445,10 +445,28 @@ def _run_synthesis(team: Team, action: VisionAction, lines: list[str]) -> str:
return (response.choices[0].message.content or "").strip()
-def _markdown_to_slack(markdown: str) -> str:
- """Light Markdown→Slack-mrkdwn pass: headings and **bold** become *bold*. Truncates long reports."""
- # Drop `[obs N]` markers — Slack can't resolve them to links yet; `synthesized_markdown` keeps them for in-app.
- text = _OBS_CITATION_RE.sub("", markdown)
+def _observation_url(team_id: int, observation_id: str) -> str:
+ return f"{settings.SITE_URL}/project/{team_id}/replay-vision/observations/{observation_id}"
+
+
+def _citations_to_slack_links(markdown: str, team_id: int, observation_ids: list[str]) -> str:
+ """Resolve each `[obs N]` citation into a Slack `<url|[N]>` link to that observation; drop any that don't
+ resolve (an out-of-range or hallucinated reference) so no bare label lingers. These links are added after
+ `strip_external_links_markdown` has already run, so the observation URLs aren't defanged."""
+
+ def _link(match: "re.Match[str]") -> str:
+ n = int(match.group(1))
+ if 1 <= n <= len(observation_ids):
+ return f"<{_observation_url(team_id, observation_ids[n - 1])}|[{n}]>"
+ return ""
+
+ return _OBS_CITATION_RE.sub(_link, markdown)
+
+
+def _markdown_to_slack(markdown: str, *, team_id: int, observation_ids: list[str]) -> str:
+ """Light Markdown→Slack-mrkdwn pass: headings and **bold** become *bold*, and `[obs N]` citations become
+ `[N]` links to each observation. Truncates long reports."""
+ text = _citations_to_slack_links(markdown, team_id, observation_ids)
text = _MARKDOWN_HEADING_RE.sub(lambda m: f"*{m.group(1)}*", text)
text = _MARKDOWN_BOLD_RE.sub(lambda m: f"*{m.group(1)}*", text)
if len(text) > SLACK_TEXT_MAX:
diff --git a/products/replay_vision/backend/tests/test_vision_action_synthesis.py b/products/replay_vision/backend/tests/test_vision_action_synthesis.py
index be457037a0c6..48af5a50c40f 100644
--- a/products/replay_vision/backend/tests/test_vision_action_synthesis.py
+++ b/products/replay_vision/backend/tests/test_vision_action_synthesis.py
@@ -4,6 +4,7 @@ from types import SimpleNamespace
from posthog.test.base import BaseTest
from unittest.mock import patch
+from django.conf import settings
from django.utils import timezone
from parameterized import parameterized
@@ -130,11 +131,10 @@ class TestVisionActionSynthesis(BaseTest):
self.assertIn("[obs 2] (", prompts[0])
self.assertNotIn("[obs 3]", prompts[0])
- def test_slack_drops_citations_that_markdown_keeps(self) -> None:
- # The canonical report keeps the `[obs N]` citation markers for the in-app renderer to resolve into
- # links; the Slack payload drops them (Slack has no observation deep-link to resolve them to yet), so
- # the prose stays clean rather than carrying bare labels.
- self._observation("Users churned at checkout", title="Checkout")
+ def test_slack_renders_citations_as_observation_links(self) -> None:
+ # The canonical report keeps the raw `[obs N]` markers for the in-app renderer; the Slack payload
+ # resolves them to `<url|[N]>` links so a Slack reader can open the recording behind a cited theme.
+ obs = self._observation("Users churned at checkout", title="Checkout")
action = self._action()
run = self._run_for(action)
@@ -142,8 +142,22 @@ class TestVisionActionSynthesis(BaseTest):
run.refresh_from_db()
self.assertIn("[obs 1]", run.synthesized_markdown)
+ expected_link = f"<{settings.SITE_URL}/project/{self.team.id}/replay-vision/observations/{obs.id}|[1]>"
+ self.assertIn(expected_link, run.output["slack"])
self.assertNotIn("[obs 1]", run.output["slack"])
- self.assertIn("Users hit friction at checkout.", run.output["slack"])
+
+ def test_slack_drops_unresolvable_citation(self) -> None:
+ # A citation the model invents past the observation count can't resolve to a link, so it's dropped
+ # rather than emitted as a dead label or a link to the wrong recording.
+ self._observation("Users churned at checkout", title="Checkout")
+ action = self._action()
+ run = self._run_for(action)
+
+ self._synthesize(action, run, llm_content="Friction everywhere [obs 9].")
+
+ run.refresh_from_db()
+ self.assertNotIn("[9]", run.output["slack"])
+ self.assertNotIn("[obs 9]", run.output["slack"])
def test_caps_runaway_citation_lists(self) -> None:
# A theme the model backs with many recordings must not render a wall of citations: an adjacent run
@@ -596,13 +610,13 @@ class TestMarkdownToSlack(BaseTest):
]
)
def test_markdown_converted_to_slack_mrkdwn(self, _label: str, markdown: str, expected: str) -> None:
- out = _markdown_to_slack(markdown)
+ out = _markdown_to_slack(markdown, team_id=self.team.id, observation_ids=[])
self.assertIn(expected, out)
self.assertNotIn("#", out)
self.assertNotIn("**", out)
def test_truncates_long_text(self) -> None:
- out = _markdown_to_slack("x" * 50_000)
+ out = _markdown_to_slack("x" * 50_000, team_id=self.team.id, observation_ids=[])
self.assertLessEqual(len(out), 39_000)
self.assertIn("truncated", out)
@@ -613,7 +627,7 @@ class TestMarkdownToSlack(BaseTest):
padding = "a" * (SLACK_TEXT_MAX - 5)
evil = "https://evil.example.com/exfil"
- out = _markdown_to_slack(padding + evil)
+ out = _markdown_to_slack(padding + evil, team_id=self.team.id, observation_ids=[])
# The host must not appear as a live (unquoted) URL in the output.
sanitized = out.replace("`https://evil.example.com/exfil`", "")
self.assertNotIn("https://evil.example.com/exfil", sanitized)