: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(max): include open dashboard in context on the first message (#63208)
Vasco de Krijgerf1742e7189a4
diff --git a/frontend/src/scenes/max/maxThreadLogic.test.ts b/frontend/src/scenes/max/maxThreadLogic.test.tsindex ecfa2ff93b9b..1a7e3b50f758 100644--- a/frontend/src/scenes/max/maxThreadLogic.test.ts+++ b/frontend/src/scenes/max/maxThreadLogic.test.ts@@ -3,6 +3,7 @@ import { MOCK_DEFAULT_BASIC_USER } from 'lib/api.mock'import { router } from 'kea-router'import { partial } from 'kea-test-utils'import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'import React from 'react'import api, { ApiError } from 'lib/api'@@ -11,6 +12,8 @@ import { lemonToast } from 'lib/lemon-ui/LemonToast'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { notebookLogic } from 'scenes/notebooks/Notebook/notebookLogic'import { NotebookTarget } from 'scenes/notebooks/types'+import { sceneLogic } from 'scenes/sceneLogic'+import { Scene } from 'scenes/sceneTypes'import { urls } from 'scenes/urls'import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'@@ -31,7 +34,8 @@ import { EnhancedToolCall } from './max-constants'import { maxContextLogic } from './maxContextLogic'import { maxGlobalLogic } from './maxGlobalLogic'import { maxLogic } from './maxLogic'-import { maxThreadLogic } from './maxThreadLogic'+import { MAX_DASHBOARD_CONTEXT_WAIT_MS, maxThreadLogic } from './maxThreadLogic'+import { MaxContextType } from './maxTypes'import {MOCK_CONVERSATION,MOCK_CONVERSATION_ID,@@ -452,6 +456,125 @@ describe('maxThreadLogic', () => {)})+ // Simulate being on a dashboard scene whose data has not loaded yet: dashboardLogic.dashboard+ // is null, so its maxContext returns []. The first message must wait for the load, otherwise+ // it ships with no dashboard context and Max can't see the open dashboard.+ const mockLoadingDashboardScene = (): { values: { dashboard: any } } => {+ const fakeDashboardLogic: any = {+ selectors: {+ maxContext: () =>+ fakeDashboardLogic.values.dashboard+ ? [{ type: MaxContextType.DASHBOARD, data: fakeDashboardLogic.values.dashboard }]+ : [],+ },+ values: { dashboard: null as any },+ }+ jest.spyOn(sceneLogic.selectors, 'activeSceneId').mockReturnValue(Scene.Dashboard)+ jest.spyOn(sceneLogic.selectors, 'activeSceneLogic').mockReturnValue(fakeDashboardLogic)+ jest.spyOn(sceneLogic.selectors, 'activeLoadedScene').mockReturnValue({+ paramsToProps: () => ({ id: 1 }),+ sceneParams: {},+ } as any)+ return fakeDashboardLogic+ }++ it('waits for the open dashboard to load before sending the first message (regression for #61414)', async () => {+ jest.useFakeTimers()+ const captureSpy = jest.spyOn(posthog, 'capture')+ try {+ const streamSpy = mockStream()+ const fakeDashboardLogic = mockLoadingDashboardScene()++ maxLogicInstance.actions.setConversationId(MOCK_TEMP_CONVERSATION_ID)+ logic = maxThreadLogic({ conversationId: MOCK_TEMP_CONVERSATION_ID, panelId: 'test' })+ logic.mount()++ // Fire the first message while the dashboard is still loading.+ logic.actions.askMax('what am I seeing on this dashboard?')++ // The gate holds the send while the dashboard is loading.+ await jest.advanceTimersByTimeAsync(300)+ expect(streamSpy).toHaveBeenCalledTimes(0)++ // Dashboard finishes loading → gate releases → the message sends WITH the dashboard context.+ fakeDashboardLogic.values.dashboard = { id: 1, name: 'Test Dashboard', tiles: [] }+ await jest.advanceTimersByTimeAsync(500)++ expect(streamSpy).toHaveBeenCalledTimes(1)+ expect(streamSpy).toHaveBeenCalledWith(+ expect.objectContaining({+ ui_context: expect.objectContaining({+ dashboards: expect.arrayContaining([expect.objectContaining({ id: 1 })]),+ }),+ }),+ expect.any(Object)+ )+ // A normal load must NOT report a timeout.+ expect(captureSpy).not.toHaveBeenCalledWith('max dashboard context wait timed out', expect.anything())+ } finally {+ jest.useRealTimers()+ }+ })++ it('reports a telemetry event and still sends if the dashboard never loads within the cap', async () => {+ jest.useFakeTimers()+ const captureSpy = jest.spyOn(posthog, 'capture')+ try {+ const streamSpy = mockStream()+ mockLoadingDashboardScene() // dashboard stays null past the wait cap++ maxLogicInstance.actions.setConversationId(MOCK_TEMP_CONVERSATION_ID)+ logic = maxThreadLogic({ conversationId: MOCK_TEMP_CONVERSATION_ID, panelId: 'test' })+ logic.mount()++ logic.actions.askMax('what am I seeing on this dashboard?')++ // Advance past the 8s wait cap without the dashboard ever loading.+ await jest.advanceTimersByTimeAsync(8100)++ // The cap must never block the user — the message still sends...+ expect(streamSpy).toHaveBeenCalledTimes(1)+ // ...but we record that the wait timed out, so the cap's impact is observable in prod.+ expect(captureSpy).toHaveBeenCalledWith(+ 'max dashboard context wait timed out',+ expect.objectContaining({ dashboard_id: 1, waited_ms: expect.any(Number) })+ )+ // waited_ms is real elapsed time, so it must be at least the cap (never under-reported).+ const timeoutCall = captureSpy.mock.calls.find((c) => c[0] === 'max dashboard context wait timed out')+ expect(timeoutCall?.[1]?.waited_ms).toBeGreaterThanOrEqual(MAX_DASHBOARD_CONTEXT_WAIT_MS)+ } finally {+ jest.useRealTimers()+ }+ })++ it('releases the gate and sends if the user navigates away while the dashboard is still loading', async () => {+ jest.useFakeTimers()+ try {+ const streamSpy = mockStream()+ mockLoadingDashboardScene()++ maxLogicInstance.actions.setConversationId(MOCK_TEMP_CONVERSATION_ID)+ logic = maxThreadLogic({ conversationId: MOCK_TEMP_CONVERSATION_ID, panelId: 'test' })+ logic.mount()++ logic.actions.askMax('what am I seeing on this dashboard?')++ // Still on the (never-loading) dashboard → the gate holds.+ await jest.advanceTimersByTimeAsync(300)+ expect(streamSpy).toHaveBeenCalledTimes(0)++ // User leaves the dashboard before it ever loads. The gate re-reads the scene each tick,+ // so it must stop waiting and send rather than block until the timeout.+ jest.spyOn(sceneLogic.selectors, 'activeSceneId').mockReturnValue(Scene.SavedInsights)+ jest.spyOn(sceneLogic.selectors, 'activeSceneLogic').mockReturnValue(null as any)+ await jest.advanceTimersByTimeAsync(300)++ expect(streamSpy).toHaveBeenCalledTimes(1)+ } finally {+ jest.useRealTimers()+ }+ })+it('sends form_answers in ui_context when provided', async () => {const streamSpy = mockStream()diff --git a/frontend/src/scenes/max/maxThreadLogic.tsx b/frontend/src/scenes/max/maxThreadLogic.tsxindex cd06231c107d..b8f6eaa8a783 100644--- a/frontend/src/scenes/max/maxThreadLogic.tsx+++ b/frontend/src/scenes/max/maxThreadLogic.tsx@@ -89,6 +89,12 @@ import { getRandomThinkingMessage } from './utils/thinkingMessages'/** Key for persisting pending AI prompts across page reloads (e.g., OAuth redirects) */export const PENDING_AI_PROMPT_KEY = 'posthog_ai_pending_prompt'+// On a dashboard, the first message can fire before the dashboard has loaded, when+// dashboardLogic.maxContext still returns []. askMax waits (bounded) for the load so the+// dashboard context is included. Bounded so a stuck/failed load never blocks sending.+export const MAX_DASHBOARD_CONTEXT_WAIT_MS = 8000+const DASHBOARD_CONTEXT_POLL_INTERVAL_MS = 100+export type MessageStatus = 'loading' | 'completed' | 'error'export type ThreadMessage = RootAssistantMessage & {@@ -997,11 +1003,47 @@ export const maxThreadLogic = kea<maxThreadLogicType>([actions.clearQueuedMessages()}},- askMax: async ({ prompt, addToThread = true, uiContext }) => {+ askMax: async ({ prompt, addToThread = true, uiContext }, breakpoint) => {// Only process if this thread is the currently active oneif (values.conversationId !== values.activeThreadKey) {return}+ // Wait for the open dashboard to finish loading before collecting context (see the+ // constants above for why). The scene is re-read every tick, so the gate releases the+ // moment the dashboard's metadata lands — and immediately if the user navigates away+ // mid-wait (no longer on a dashboard, or onto a different one that's already loaded).+ const isDashboardSceneLoading = (): boolean => {+ if (sceneLogic.values.activeSceneId !== Scene.Dashboard) {+ return false+ }+ const activeSceneLogic = sceneLogic.values.activeSceneLogic+ if (!activeSceneLogic || !('maxContext' in activeSceneLogic.selectors)) {+ return false+ }+ return !(activeSceneLogic.values as { dashboard?: unknown }).dashboard+ }+ // Measure real elapsed time, not tick count: breakpoint() only guarantees a *minimum*+ // delay, so a busy event loop would make a tick counter under-report the wait — letting+ // it run past the cap and skewing the telemetry below. performance.now() is monotonic.+ const dashboardWaitStart = performance.now()+ while (+ isDashboardSceneLoading() &&+ performance.now() - dashboardWaitStart < MAX_DASHBOARD_CONTEXT_WAIT_MS+ ) {+ await breakpoint(DASHBOARD_CONTEXT_POLL_INTERVAL_MS)+ }+ if (isDashboardSceneLoading()) {+ // We hit the wait cap while the dashboard was still loading, so the message ships+ // without dashboard context (the original "Max can't see this dashboard" symptom).+ // Capture it so we can tell whether the cap is ever the binding constraint in prod.+ const activeLoadedScene = sceneLogic.values.activeLoadedScene+ const sceneProps = activeLoadedScene?.paramsToProps?.(activeLoadedScene?.sceneParams) || {}+ posthog.capture('max dashboard context wait timed out', {+ waited_ms: Math.round(performance.now() - dashboardWaitStart),+ dashboard_id: (sceneProps as { id?: number | string }).id,+ conversation_id: values.conversation?.id || values.conversationId,+ })+ }const contextualTools = Object.fromEntries(values.tools.map((tool) => [tool.identifier, tool.context]))// Always send voice_mode as an explicit boolean when handsFreeLogic is mounted,// not just when active. Otherwise a typed turn following a spoken one inherits