: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(replay-vision): collapse adjacent citations into one bracket group (#75802)
Arno Hillenb8e47ac00dac
diff --git a/products/replay_vision/frontend/replay_scanners/visionActionRunSceneLogic.test.ts b/products/replay_vision/frontend/replay_scanners/visionActionRunSceneLogic.test.tsindex ac8524b93e7d..c614e8990ee7 100644--- a/products/replay_vision/frontend/replay_scanners/visionActionRunSceneLogic.test.ts+++ b/products/replay_vision/frontend/replay_scanners/visionActionRunSceneLogic.test.ts@@ -12,24 +12,55 @@ const obs = (index: number, id: string): RunObservationApi => ({created_at: '2026-01-01T00:00:00Z',})+const link = (id: string): string => urls.replayVisionObservation(id)++// Group members join on a no-break space so a `[2, 5, 33]` group can't wrap mid-bracket.+const NBSP = '\u00a0'+describe('resolveObservationCitations', () => {- it('links resolvable [obs N] markers as [N] and drops the ones that no longer resolve', () => {+ it('links a lone [obs N] marker as an [N] link and drops the ones that no longer resolve', () => {// obs 3 has no matching observation (deleted, or the model invented it) — it must be dropped, not// rendered as a dead `[3]` or misdirected to another row.- const out = resolveObservationCitations('Friction here [obs 1] [obs 2]. Gone [obs 3].', [- obs(1, 'aaa'),- obs(2, 'bbb'),+ const out = resolveObservationCitations('Friction here [obs 1]. Gone [obs 3].', [obs(1, 'aaa'), obs(2, 'bbb')])+ expect(out).toBe(`Friction here [\\[1\\]](${link('aaa')}). Gone .`)+ })++ it('collapses adjacent markers into one bracket group where every number is its own link', () => {+ const out = resolveObservationCitations('Direct to the button [obs 2] [obs 5] [obs 33].', [+ obs(2, 'aaa'),+ obs(5, 'bbb'),+ obs(33, 'ccc'),])+ // Renders as `[2, 5, 33]`: brackets and commas live inside the link texts so the whole group+ // gets the superscript citation styling.expect(out).toBe(- `Friction here [[1]](${urls.replayVisionObservation('aaa')}) [[2]](${urls.replayVisionObservation(- 'bbb'- )}). Gone .`+ `Direct to the button [\\[2,](${link('aaa')})${NBSP}[5,](${link('bbb')})${NBSP}[33\\]](${link('ccc')}).`)})+ it('drops unresolvable markers from a group and keeps the brackets balanced', () => {+ const out = resolveObservationCitations('Here [obs 1] [obs 3] [obs 2].', [obs(1, 'aaa'), obs(2, 'bbb')])+ expect(out).toBe(`Here [\\[1,](${link('aaa')})${NBSP}[2\\]](${link('bbb')}).`)+ })++ it('cites a duplicated marker once so it does not read as extra evidence', () => {+ const out = resolveObservationCitations('Twice [obs 1] [obs 1] [obs 2].', [obs(1, 'aaa'), obs(2, 'bbb')])+ expect(out).toBe(`Twice [\\[1,](${link('aaa')})${NBSP}[2\\]](${link('bbb')}).`)+ })++ it('drops a group entirely when none of its markers resolve', () => {+ const out = resolveObservationCitations('Nothing left [obs 3] [obs 4].', [obs(1, 'aaa')])+ expect(out).toBe('Nothing left .')+ })++ it('does not collapse markers separated by other text or newlines', () => {+ const out = resolveObservationCitations('One [obs 1], two [obs 2]\n[obs 1]', [obs(1, 'aaa'), obs(2, 'bbb')])+ expect(out).toBe(`One [\\[1\\]](${link('aaa')}), two [\\[2\\]](${link('bbb')})\n[\\[1\\]](${link('aaa')})`)+ })+it('maps a citation to its index, not its array position, after an earlier observation is deleted', () => {// obs 1 was deleted, so the array starts at index 2. `[obs 2]` must still resolve to id 'bbb'.const out = resolveObservationCitations('See [obs 2].', [obs(2, 'bbb'), obs(3, 'ccc')])- expect(out).toBe(`See [[2]](${urls.replayVisionObservation('bbb')}).`)+ expect(out).toBe(`See [\\[2\\]](${link('bbb')}).`)})})diff --git a/products/replay_vision/frontend/replay_scanners/visionActionRunSceneLogic.ts b/products/replay_vision/frontend/replay_scanners/visionActionRunSceneLogic.tsindex 663e934c9b80..111ad9c930c1 100644--- a/products/replay_vision/frontend/replay_scanners/visionActionRunSceneLogic.ts+++ b/products/replay_vision/frontend/replay_scanners/visionActionRunSceneLogic.ts@@ -13,14 +13,31 @@ import type { RunObservationApi, VisionActionApi, VisionActionRunApi } from '..//*** Resolve the `[obs N]` citation markers the synthesizer leaves in a group summary into `[N]` links to- * each observation. `N` is the observation's stable `index` (its position in the summary), so a deleted+ * each observation. Adjacent markers collapse into one bracket group (`[2, 5, 33]`) where every number+ * stays its own link. `N` is the observation's stable `index` (its position in the summary), so a deleted* observation drops its citation rather than misdirecting to a renumbered neighbor.*/export function resolveObservationCitations(markdown: string, observations: readonly RunObservationApi[]): string {const byIndex = new Map(observations.map((obs) => [obs.index, obs]))- return markdown.replace(/\[obs (\d+)\]/g, (_match, n: string) => {- const obs = byIndex.get(Number(n))- return obs ? `[[${n}]](${urls.replayVisionObservation(obs.id)})` : ''+ return markdown.replace(/\[obs \d+\](?:[ \t]*\[obs \d+\])*/g, (group) => {+ const cited: { n: string; url: string }[] = []+ for (const [, n] of group.matchAll(/\[obs (\d+)\]/g)) {+ const obs = byIndex.get(Number(n))+ // Synthesizers often repeat a citation; a group citing the same observation twice would+ // read as two distinct pieces of evidence.+ if (obs && !cited.some((c) => c.n === n)) {+ cited.push({ n, url: urls.replayVisionObservation(obs.id) })+ }+ }+ // Brackets and commas sit inside the link texts (escaped) so the whole group inherits the+ // superscript citation styling, which only targets the <a> elements (ScannerSummary.scss).+ // Joined with no-break spaces so a group can't wrap mid-bracket (orphaning e.g. `[2,` at a line end).+ return cited+ .map(({ n, url }, i) => {+ const text = `${i === 0 ? '\\[' : ''}${n}${i === cited.length - 1 ? '\\]' : ','}`+ return `[${text}](${url})`+ })+ .join('\u00a0')})}