: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
perf(max): stop scene titles reloading the chat history at boot (#117799)
Paul D'Ambra2a5856607202
diff --git a/frontend/src/layout/navigation-3000/sidepanel/panels/max/SidePanelMax.test.tsx b/frontend/src/layout/navigation-3000/sidepanel/panels/max/SidePanelMax.test.tsxnew file mode 100644index 000000000000..8933a5e2f5a2--- /dev/null+++ b/frontend/src/layout/navigation-3000/sidepanel/panels/max/SidePanelMax.test.tsx@@ -0,0 +1,39 @@+import { cleanup, render, screen } from '@testing-library/react'+import { Suspense } from 'react'++import { SIDE_PANEL_PANEL_ID, maxLogic } from 'scenes/max/maxLogic'++import { initKeaTests } from '~/test/init'++import { SidePanelMax } from './SidePanelMax'++jest.mock('scenes/max/Max', () => ({+ MaxInstance: () => <div>side panel chat</div>,+}))++describe('SidePanelMax', () => {+ beforeEach(() => {+ initKeaTests()+ })++ afterEach(() => {+ cleanup()+ })++ it('keeps the side panel chat mounted after the panel closes', async () => {+ // Stands in for the mount the chat UI holds while the panel is open.+ const unmountPanelChat = maxLogic({ panelId: SIDE_PANEL_PANEL_ID }).mount()++ const { unmount } = render(+ <Suspense fallback={null}>+ <SidePanelMax />+ </Suspense>+ )+ await screen.findByText('side panel chat')++ unmount()+ unmountPanelChat()++ expect(maxLogic.findMounted({ panelId: SIDE_PANEL_PANEL_ID })).not.toBeNull()+ })+})diff --git a/frontend/src/layout/navigation-3000/sidepanel/panels/max/SidePanelMax.tsx b/frontend/src/layout/navigation-3000/sidepanel/panels/max/SidePanelMax.tsxindex a5b0e0155550..a2d2ec009873 100644--- a/frontend/src/layout/navigation-3000/sidepanel/panels/max/SidePanelMax.tsx+++ b/frontend/src/layout/navigation-3000/sidepanel/panels/max/SidePanelMax.tsx@@ -1,9 +1,16 @@+import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'import { lazyWithRetry } from 'lib/utils/retryImport'+import { sidePanelMaxLogic } from 'scenes/max/maxLogic'// Lazy so the side panel registry doesn't pull Max's thread graph (Query, message widgets)// into the shell chunk. SidePanel already wraps panel content in a Suspense boundary.const MaxInstance = lazyWithRetry(() => import('scenes/max/Max').then((m) => ({ default: m.MaxInstance })))export function SidePanelMax(): JSX.Element | null {+ // Closing the panel unmounts this tree, so hold the chat logic to keep the open chat and draft.+ useOnMountEffect(() => {+ sidePanelMaxLogic()+ })+return <MaxInstance sidePanel />}diff --git a/frontend/src/scenes/max/maxGlobalLogic.tsx b/frontend/src/scenes/max/maxGlobalLogic.tsxindex 79291c9f554b..dffc48c581c6 100644--- a/frontend/src/scenes/max/maxGlobalLogic.tsx+++ b/frontend/src/scenes/max/maxGlobalLogic.tsx@@ -27,7 +27,7 @@ import type { OrganizationType, PreflightStatus } from '../../types'import type { SceneConfig } from '../sceneTypes'import { TOOL_DEFINITIONS, ToolRegistration } from './max-constants'import { PHAI_VIEW_MODE_KEY } from './max-storage-keys'-import { SIDE_PANEL_PANEL_ID, maxLogic, mergeConversationHistory, mergeConversations } from './maxLogic'+import { maxLogic, mergeConversationHistory, mergeConversations, sidePanelMaxLogic } from './maxLogic'/*** Which PostHog AI implementation the Max scene renders. `'new'` is the posthog_ai/frontend surface@@ -413,12 +413,7 @@ export const maxGlobalLogic = kea<maxGlobalLogicType>([actions.openSidePanel(SidePanelTab.Max)}if (conversationId) {- let logic = maxLogic.findMounted({ panelId: SIDE_PANEL_PANEL_ID })- if (!logic) {- logic = maxLogic({ panelId: SIDE_PANEL_PANEL_ID })- logic.mount() // we're never unmounting this- }- logic.actions.openConversation(conversationId)+ sidePanelMaxLogic().actions.openConversation(conversationId)}},// Open the side panel on a fresh chat bound to a sandbox Task (inbox "Open task" — in-place,@@ -429,11 +424,7 @@ export const maxGlobalLogic = kea<maxGlobalLogicType>([if (!values.sidePanelOpen || values.selectedTab !== SidePanelTab.Max) {actions.openSidePanel(SidePanelTab.Max)}- let logic = maxLogic.findMounted({ panelId: SIDE_PANEL_PANEL_ID })- if (!logic) {- logic = maxLogic({ panelId: SIDE_PANEL_PANEL_ID })- logic.mount() // we're never unmounting this- }+ const logic = sidePanelMaxLogic()logic.actions.startNewConversation()logic.actions.setPendingBindTaskId(taskId)},diff --git a/frontend/src/scenes/max/maxLogic.tsx b/frontend/src/scenes/max/maxLogic.tsxindex 563353355f49..ad479c436f2f 100644--- a/frontend/src/scenes/max/maxLogic.tsx+++ b/frontend/src/scenes/max/maxLogic.tsx@@ -1106,6 +1106,20 @@ export const maxLogic = kea<maxLogicType>([}),])+/**+ * The side panel chat logic. The first call holds a mount for the rest of the session, so the open chat+ * survives when the panel closes and unmounts its React tree. The hold is separate from any mount React+ * already has, because that mount ends when the panel closes.+ */+export function sidePanelMaxLogic(): ReturnType<typeof maxLogic.build> {+ const logic = maxLogic({ panelId: SIDE_PANEL_PANEL_ID })+ if (!logic.cache.sessionMountHeld) {+ logic.mount()+ logic.cache.sessionMountHeld = true+ }+ return logic+}+export function getScrollableContainer(element?: Element | null): HTMLElement | null {if (!element) {return nulldiff --git a/frontend/src/scenes/max/useMaxTool.test.tsx b/frontend/src/scenes/max/useMaxTool.test.tsxindex d1a3f9f6fc59..7be000690c56 100644--- a/frontend/src/scenes/max/useMaxTool.test.tsx+++ b/frontend/src/scenes/max/useMaxTool.test.tsx@@ -1,16 +1,32 @@-import { cleanup, render } from '@testing-library/react'+import { act, cleanup, render } from '@testing-library/react'+import { expectLogic } from 'kea-test-utils'+import api from 'lib/api'++import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'import { initKeaTests } from '~/test/init'+import { SidePanelTab } from '~/types'import { maxGlobalLogic } from './maxGlobalLogic'-import { useMaxTool } from './useMaxTool'+import { SIDE_PANEL_PANEL_ID, maxLogic } from './maxLogic'+import { UseMaxToolReturn, useMaxTool } from './useMaxTool'-function TestMaxTool({ context, active = true }: { context: Record<string, unknown>; active?: boolean }): null {- useMaxTool({+function TestMaxTool({+ context,+ active = true,+ onRender,+}: {+ context: Record<string, unknown>+ active?: boolean+ onRender?: (result: UseMaxToolReturn) => void+}): null {+ const result = useMaxTool({identifier: 'read_data',context,active,+ suggestions: ['Show me signups'],})+ onRender?.(result)return null}@@ -45,4 +61,26 @@ describe('useMaxTool', () => {expect(deregisterTool).toHaveBeenCalledWith('read_data')})++ it('does not reload the chat history when a scene registers a tool', async () => {+ await expectLogic(logic).toDispatchActions(['loadConversationHistorySuccess'])+ const listConversations = jest.spyOn(api.conversations, 'list')++ render(<TestMaxTool context={{ query: 'select 1' }} />)+ await expectLogic(logic).toFinishAllListeners()++ expect(listConversations).not.toHaveBeenCalled()+ })++ it('opens the side panel on a new chat with the tool suggestions', () => {+ let openMax: UseMaxToolReturn['openMax'] = null+ render(<TestMaxTool context={{ query: 'select 1' }} onRender={(result) => (openMax = result.openMax)} />)++ act(() => openMax?.())++ expect(sidePanelStateLogic.values.selectedTab).toEqual(SidePanelTab.Max)+ expect(maxLogic({ panelId: SIDE_PANEL_PANEL_ID }).values.activeSuggestionGroup?.suggestions).toEqual([+ { content: 'Show me signups' },+ ])+ })})diff --git a/frontend/src/scenes/max/useMaxTool.ts b/frontend/src/scenes/max/useMaxTool.tsindex 68c605a62255..9ee231fb12a3 100644--- a/frontend/src/scenes/max/useMaxTool.ts+++ b/frontend/src/scenes/max/useMaxTool.ts@@ -8,7 +8,7 @@ import { SidePanelTab } from '~/types'import { ToolDefinition, ToolRegistration, getToolDefinition } from './max-constants'import { maxGlobalLogic } from './maxGlobalLogic'-import { SIDE_PANEL_PANEL_ID, maxLogic } from './maxLogic'+import { sidePanelMaxLogic } from './maxLogic'import { createSuggestionGroup } from './utils'export interface UseMaxToolOptions extends Omit<ToolRegistration, 'name' | 'description'> {@@ -47,7 +47,6 @@ export function useMaxTool({const { registerTool, deregisterTool } = useActions(maxGlobalLogic)const { openSidePanel } = useActions(sidePanelLogic)const { sidePanelOpen, selectedTab } = useValues(sidePanelLogic)- const { setActiveGroup, startNewConversation } = useActions(maxLogic({ panelId: SIDE_PANEL_PANEL_ID }))const definition = getToolDefinition(identifier)const isMaxOpen = sidePanelOpen && selectedTab === SidePanelTab.Max@@ -109,6 +108,7 @@ export function useMaxTool({openMax: !active? null: (): void => {+ const { startNewConversation, setActiveGroup } = sidePanelMaxLogic().actions// Start a new conversation so the prompt doesn't get added to an existing sessionstartNewConversation()// Show the suggestions from this specific tool