: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(aio): keep prompt scene mounted when switching versions (#76672)
Juraj Majerikb4c707e6feb0
diff --git a/products/ai_observability/frontend/prompts/LLMPromptScene.tsx b/products/ai_observability/frontend/prompts/LLMPromptScene.tsxindex c2607bc3d479..62d5531d68e2 100644--- a/products/ai_observability/frontend/prompts/LLMPromptScene.tsx+++ b/products/ai_observability/frontend/prompts/LLMPromptScene.tsx@@ -43,7 +43,6 @@ export const scene: SceneExport<PromptLogicProps> = {promptName: name && name !== 'new' ? name : 'new',// kea-router JSON-decodes query values, so ?edit=true arrives as boolean truemode: String(searchParams?.edit) === 'true' ? PromptMode.Edit : PromptMode.View,- selectedVersion: searchParams?.version ? Number(searchParams.version) || null : null,}),}diff --git a/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts b/products/ai_observability/frontend/prompts/llmPromptLogic.test.tsindex 451fc4852f98..9d3b325386ee 100644--- a/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts+++ b/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts@@ -301,6 +301,33 @@ describe('llmPromptLogic', () => {logic.unmount()})+ it('reloads the URL-selected version in place, keeping the loaded prompt visible', async () => {+ const { versions, has_more, ...promptFields } = mockPrompt+ mockResolve.mockResolvedValue({+ prompt: promptFields,+ versions,+ has_more,+ } as unknown as LLMPromptResolveResponseApi)++ const logic = llmPromptLogic({ promptName: 'my-test-prompt' })+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadPromptSuccess'])++ router.actions.push('/prompt-management/prompts/my-test-prompt', { version: 1 })++ // Old prompt data stays while the selected version loads: no skeleton flash+ expect(logic.values.promptLoading).toBe(true)+ expect(logic.values.prompt).not.toBeNull()++ await expectLogic(logic).toDispatchActions(['loadPromptSuccess'])+ expect(mockResolve).toHaveBeenLastCalledWith(expect.any(String), 'my-test-prompt', {+ version: 1,+ limit: 50,+ })++ logic.unmount()+ })+it('drops prompt-scene params from the breadcrumb link back to the list', async () => {router.actions.push('/prompt-management/prompts/existing-prompt', {edit: true,@@ -309,7 +336,7 @@ describe('llmPromptLogic', () => {search: 'checkout',})- const logic = llmPromptLogic({ promptName: 'existing-prompt', selectedVersion: 2 })+ const logic = llmPromptLogic({ promptName: 'existing-prompt' })logic.mount()expect(logic.values.breadcrumbs[0].path).toBe('/prompt-management/prompts?search=checkout')diff --git a/products/ai_observability/frontend/prompts/llmPromptLogic.ts b/products/ai_observability/frontend/prompts/llmPromptLogic.tsindex ff794d9aa906..08db0f43a06a 100644--- a/products/ai_observability/frontend/prompts/llmPromptLogic.ts+++ b/products/ai_observability/frontend/prompts/llmPromptLogic.ts@@ -71,7 +71,6 @@ export enum PromptAnalyticsScope {export interface PromptLogicProps {promptName: string | 'new'mode?: PromptMode- selectedVersion?: number | null}export interface PromptFormValues {@@ -506,7 +505,9 @@ export type llmPromptLogicType = MakeLogicType<export const llmPromptLogic = kea<llmPromptLogicType>([path(['scenes', 'ai-observability', 'llmPromptLogic']),props({ promptName: 'new' } as PromptLogicProps),- key(({ promptName, selectedVersion }) => `prompt-${promptName}:${selectedVersion ?? 'latest'}`),+ // Keyed by name only: switching versions loads into the same logic instance, so the+ // scene chrome (header, tabs, sidebar) survives and only prompt-derived UI updates.+ key(({ promptName }) => `prompt-${promptName}`),connect(() => ({actions: [teamLogic, ['addProductIntent']],})),@@ -661,9 +662,11 @@ export const llmPromptLogic = kea<llmPromptLogicType>([loaders(({ props }) => ({prompt: {__default: null as ResolvedLLMPrompt | PromptFormValues | null,+ // Version comes from the router, not props: scene props only update after+ // React re-renders, so they are stale inside urlToAction-triggered loads.loadPrompt: async () =>fetchResolvedPrompt(props.promptName, {- version: props.selectedVersion ?? undefined,+ version: getSelectedVersionFromUrl(),}),},comparePrompt: {@@ -1486,13 +1489,20 @@ export const llmPromptLogic = kea<llmPromptLogicType>([return}- if (method === 'PUSH' && !values.isNewPrompt) {+ // POP included: back/forward between versions lands on the same logic+ // instance, so the selected version must be re-resolved from the URL.+ if ((method === 'PUSH' || method === 'POP') && !values.isNewPrompt) {actions.loadPrompt()}},})),])+function getSelectedVersionFromUrl(): number | undefined {+ const raw = router.values.searchParams?.version+ return raw ? Number(raw) || undefined : undefined+}+function getPromptFormDefaults(prompt: LLMPrompt): PromptFormValues {return {name: prompt.name,diff --git a/products/ai_observability/frontend/prompts/promptSceneComponents.tsx b/products/ai_observability/frontend/prompts/promptSceneComponents.tsxindex 712ed1f404bd..92123cadf2f1 100644--- a/products/ai_observability/frontend/prompts/promptSceneComponents.tsx+++ b/products/ai_observability/frontend/prompts/promptSceneComponents.tsx@@ -1055,7 +1055,13 @@ export function PromptVersionSidebar({const selected = prompt?.id === versionPrompt.idconst isCompareTarget = compareVersion === versionPrompt.versionconst canCompare = !readOnly && prompt?.version !== versionPrompt.version- const versionUrl = buildPromptUrl(promptName, searchParams, versionPrompt.version)+ // The latest version's canonical URL has no ?version param, so landing on+ // it matches the state the scene starts in.+ const versionUrl = buildPromptUrl(+ promptName,+ searchParams,+ versionPrompt.is_latest ? null : versionPrompt.version+ )const cardContent = (<>@@ -1165,7 +1171,9 @@ export function PromptVersionSidebar({: 'border-primary/10 hover:bg-fill-secondary'}`- if (readOnly) {+ // The selected version is not a link: navigating to the version already+ // shown would only trigger a pointless reload.+ if (readOnly || selected) {return (<div key={versionPrompt.id} className={cardClassName}>{cardContent}