: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(llma): prevent playground pageview loop on evaluation source sync (#58586)
Andrew Maguire13a207d80cdf
diff --git a/products/llm_analytics/frontend/playground/llmPlaygroundLogic.test.ts b/products/llm_analytics/frontend/playground/llmPlaygroundLogic.test.tsindex 6a10f64b84cb..d2827bca8f8e 100644--- a/products/llm_analytics/frontend/playground/llmPlaygroundLogic.test.ts+++ b/products/llm_analytics/frontend/playground/llmPlaygroundLogic.test.ts@@ -1,6 +1,9 @@import { router } from 'kea-router'import { expectLogic } from 'kea-test-utils'+import { sceneLogic } from 'scenes/sceneLogic'+import { urls } from 'scenes/urls'+import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'@@ -1696,6 +1699,81 @@ describe('llmPlaygroundLogic', () => {expect(llmPlaygroundPromptsLogic.values.sourceSetupLoading).toBe(false)})++ it('syncs source param via replace, not push (regression for pageview loop)', async () => {+ // Previously `finishSourceSetup` used `router.actions.push`, which adds a history+ // entry and fires a `$pageview` on every setup. If `urlToAction` re-entered the+ // setup path (e.g. because multiple keyed logic instances were mounted), each+ // iteration pushed another history entry, producing thousands of pageviews per+ // minute on the LLM Analytics playground. Switching to `replace` keeps history+ // bounded so a single missed dedup cannot snowball.+ useMocks({+ get: {+ '/api/environments/:team_id/evaluations/:id/': {+ id: 'eval-1',+ name: 'judge-eval',+ evaluation_type: 'llm_judge',+ evaluation_config: { prompt: 'Rate.' },+ },+ },+ })++ const initialHistoryLength = window.history.length++ llmPlaygroundPromptsLogic.actions.setupPlaygroundFromEvent({+ sourceType: 'evaluation',+ sourceEvaluationId: 'eval-1',+ })+ await expectLogic(llmPlaygroundPromptsLogic).toFinishAllListeners()++ expect(router.values.searchParams).toHaveProperty('source_evaluation_id', 'eval-1')+ expect(window.history.length).toBe(initialHistoryLength)+ })++ it('urlToAction skips setup for non-active-tab instances (regression for default-instance fall-through)', async () => {+ // Before the fix, the guard `if (props.tabId && ...)` short-circuited to false+ // when `props.tabId` was undefined, so the unkeyed `'default'` instance also+ // ran URL-driven setup alongside the active tab. Multiple instances each+ // dispatching `setupPlaygroundFromEvent` + `finishSourceSetup` writing back+ // to the URL is what turned a missed dedup into a runaway pageview loop.+ useMocks({+ get: {+ '/api/environments/:team_id/evaluations/:id/': {+ id: 'eval-2',+ name: 'judge-eval',+ evaluation_type: 'llm_judge',+ evaluation_config: { prompt: 'Rate.' },+ },+ },+ })++ const findMountedSpy = jest.spyOn(sceneLogic, 'findMounted').mockReturnValue({+ values: { activeTabId: 'tab-A', tabs: [] },+ } as any)++ const activeInstance = llmPlaygroundPromptsLogic({ tabId: 'tab-A' })+ activeInstance.mount()+ const inactiveInstance = llmPlaygroundPromptsLogic({ tabId: 'tab-B' })+ inactiveInstance.mount()++ try {+ router.actions.push(`${urls.llmAnalyticsPlayground()}?source_evaluation_id=eval-2&_=${Date.now()}`)+ await expectLogic(activeInstance).toFinishAllListeners()+ await expectLogic(inactiveInstance).toFinishAllListeners()+ await expectLogic(llmPlaygroundPromptsLogic).toFinishAllListeners()++ expect(activeInstance.values.linkedSource).toMatchObject({+ type: 'evaluation',+ evaluationId: 'eval-2',+ })+ expect(inactiveInstance.values.linkedSource.type).toBeNull()+ expect(llmPlaygroundPromptsLogic.values.linkedSource.type).toBeNull()+ } finally {+ activeInstance.unmount()+ inactiveInstance.unmount()+ findMountedSpy.mockRestore()+ }+ })})describe('save actions', () => {diff --git a/products/llm_analytics/frontend/playground/llmPlaygroundPromptsLogic.ts b/products/llm_analytics/frontend/playground/llmPlaygroundPromptsLogic.tsindex 0d5dd8f17c24..cc9782d5dd3f 100644--- a/products/llm_analytics/frontend/playground/llmPlaygroundPromptsLogic.ts+++ b/products/llm_analytics/frontend/playground/llmPlaygroundPromptsLogic.ts@@ -909,7 +909,12 @@ export const llmPlaygroundPromptsLogic = kea<llmPlaygroundPromptsLogicType>([actions.setMessages([], promptId)actions.setActivePromptId(promptId)const cleanParams = cleanSourceSearchParams(router.values.searchParams)- router.actions.push(combineUrl(urls.llmAnalyticsPlayground(), { ...cleanParams, ...sourceParam }).url)+ // Use `replace` (not `push`) so we don't add a history entry — and a $pageview — on every+ // setup. If the URL-driven path ever re-enters this listener, `push` turns a single+ // missed dedup into a runaway pageview loop. See `urlToAction` for the dedup guard.+ router.actions.replace(+ combineUrl(urls.llmAnalyticsPlayground(), { ...cleanParams, ...sourceParam }).url+ )}try {@@ -1265,9 +1270,13 @@ export const llmPlaygroundPromptsLogic = kea<llmPlaygroundPromptsLogicType>([}}- // urlToAction fires on ALL mounted instances for the matching URL.- // Only process URL params for the active tab to avoid cross-tab interference.- if (props.tabId && sceneLogic.findMounted()?.values.activeTabId !== props.tabId) {+ // urlToAction fires on ALL mounted instances for the matching URL — including the+ // unkeyed `'default'` instance kept alive for backwards compatibility. Only the active+ // tab instance should run the URL-driven setup; otherwise multiple instances each+ // dispatch `setupPlaygroundFromEvent`, and any subsequent URL update (e.g. from+ // `finishSourceSetup`) re-enters this handler on every instance, producing a pageview+ // and API-fetch loop.+ if (!props.tabId || sceneLogic.findMounted()?.values.activeTabId !== props.tabId) {return}