: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(frontend): reuse fresh ai conversation history (#117794)
posthog[bot]064788398bed
diff --git a/frontend/src/scenes/max/maxGlobalLogic.tsx b/frontend/src/scenes/max/maxGlobalLogic.tsxindex f6147e3544f2..4789defa4f28 100644--- a/frontend/src/scenes/max/maxGlobalLogic.tsx+++ b/frontend/src/scenes/max/maxGlobalLogic.tsx@@ -101,6 +101,7 @@ export interface maxGlobalLogicValues {currentTeamIdStrict: number | string // teamLogicavailableStaticTools: ToolRegistration[]conversationHistory: ConversationDetail[]+ conversationHistoryLoadedAt: number | nullconversationHistoryLoading: booleancurrentConversationId: string | nulleditInsightToolRegistered: true@@ -345,6 +346,12 @@ export const maxGlobalLogic = kea<maxGlobalLogicType>([return mergeConversationHistory(state, conversation)},},+ conversationHistoryLoadedAt: [+ null as number | null,+ {+ loadConversationHistorySuccess: () => Date.now(),+ },+ ],liabilityNoticeDismissed: [false,{ persist: true, storageKey: AI_LIABILITY_NOTICE_STORAGE_KEY },diff --git a/frontend/src/scenes/max/maxLogic.test.ts b/frontend/src/scenes/max/maxLogic.test.tsindex 376d934592ff..b6e5eb179ee1 100644--- a/frontend/src/scenes/max/maxLogic.test.ts+++ b/frontend/src/scenes/max/maxLogic.test.ts@@ -129,7 +129,11 @@ describe('maxLogic', () => {})})- it('shares the initial history request and does not load actions when Max mounts', async () => {+ it.each([+ { age: 0, expectedRequests: 1 },+ { age: 29_999, expectedRequests: 1 },+ { age: 30_000, expectedRequests: 2 },+ ])('shares history requests until the result is stale ($age ms)', async ({ age, expectedRequests }) => {logic = maxLogic({ panelId: 'test' })logic.mount()@@ -137,6 +141,17 @@ describe('maxLogic', () => {expect(actionsRequestCount).toBe(0)expect(conversationRequestCount).toBe(1)++ const loadedAt = maxGlobalLogic.values.conversationHistoryLoadedAt!+ const clock = jest.spyOn(Date, 'now').mockReturnValue(loadedAt + age)+ const unmountPanel = maxLogic({ panelId: 'another-panel' }).mount()+ try {+ await expectLogic(maxGlobalLogic).toFinishAllListeners()+ expect(conversationRequestCount).toBe(expectedRequests)+ } finally {+ unmountPanel()+ clock.mockRestore()+ }})it.each([diff --git a/frontend/src/scenes/max/maxLogic.tsx b/frontend/src/scenes/max/maxLogic.tsxindex cf867b5fa207..fb389e81dcc4 100644--- a/frontend/src/scenes/max/maxLogic.tsx+++ b/frontend/src/scenes/max/maxLogic.tsx@@ -48,6 +48,9 @@ export const PENDING_MAX_CONTEXT_KEY = 'posthog.pending_max_context'/** Maximum age for restored context (5 minutes) */const PENDING_CONTEXT_MAX_AGE_MS = 5 * 60 * 1000+/** A panel that mounts within this time of the last history load reuses that result (30 seconds) */+const FRESH_CONVERSATION_HISTORY_MS = 30 * 1000+/** Stored context structure for sessionStorage */interface StoredMaxContext {context: Partial<MaxUIContext>@@ -165,6 +168,7 @@ function sceneTabId(panelId?: string, syncUrl?: boolean): string | null {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface maxLogicValues {conversationHistory: ConversationDetail[] // maxGlobalLogic+ conversationHistoryLoadedAt: number | null // maxGlobalLogicconversationHistoryLoading: boolean // maxGlobalLogicdataProcessingAccepted: boolean // maxGlobalLogicdataProcessingApprovalDisabledReason: string | null // maxGlobalLogic@@ -405,6 +409,7 @@ export const maxLogic = kea<maxLogicType>(['toolSuggestions','conversationHistory','conversationHistoryLoading',+ 'conversationHistoryLoadedAt','effectivePhaiView',],tabUiStateLogic,@@ -994,8 +999,11 @@ export const maxLogic = kea<maxLogicType>([handleCommandString(sidePanelStateLogic.values.selectedTabOptions, actions, values.effectivePhaiView)}- // The global logic may already be loading history for the navigation.- if (!values.conversationHistoryLoading) {+ // The global logic loads history for the navigation at boot. Reuse that request, or its fresh result.+ const historyIsFresh =+ values.conversationHistoryLoadedAt !== null &&+ Date.now() - values.conversationHistoryLoadedAt < FRESH_CONVERSATION_HISTORY_MS+ if (!values.conversationHistoryLoading && !historyIsFresh) {actions.loadConversationHistory()}}),