Skip to content

: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.

fix(max): guard scene-context reads against unmounted scene logic (#70335)

Vasco de Krijger671ea6a779ce
diff --git a/frontend/src/scenes/max/maxContextLogic.ts b/frontend/src/scenes/max/maxContextLogic.ts
index ad9de0832c59..e16be5505b26 100644
--- a/frontend/src/scenes/max/maxContextLogic.ts
+++ b/frontend/src/scenes/max/maxContextLogic.ts
@@ -37,6 +37,7 @@ import {
} from './maxTypes'
import {
actionToMaxContextPayload,
+ activeSceneLogicHasMaxContext,
dashboardToMaxContext,
errorTrackingIssueToMaxContextPayload,
evaluationToMaxContextPayload,
@@ -525,16 +526,7 @@ export const maxContextLogic = kea<maxContextLogicType>([
(state): MaxContextInput[] => {
const activeSceneLogic = sceneLogic.selectors.activeSceneLogic(state, {})
- // During a scene transition, sceneLogic can still resolve the outgoing scene's
- // (keyed) logic reference after kea has already unmounted it. Reading its
- // maxContext then walks into a store path that no longer exists and throws
- // "Can not find path". Skip the read while unmounted — the next recompute after
- // the new scene mounts returns fresh context.
- if (
- activeSceneLogic &&
- activeSceneLogic.isMounted() &&
- 'maxContext' in activeSceneLogic.selectors
- ) {
+ if (activeSceneLogicHasMaxContext(activeSceneLogic)) {
try {
const activeLoadedScene = sceneLogic.selectors.activeLoadedScene(state, {})
return activeSceneLogic.selectors.maxContext(
diff --git a/frontend/src/scenes/max/maxThreadLogic.test.ts b/frontend/src/scenes/max/maxThreadLogic.test.ts
index 4e1ce2e6b0ce..678f23ff66b0 100644
--- a/frontend/src/scenes/max/maxThreadLogic.test.ts
+++ b/frontend/src/scenes/max/maxThreadLogic.test.ts
@@ -591,6 +591,31 @@ describe('maxThreadLogic', () => {
}
})
+ it('sends promptly instead of waiting for the cap when the dashboard scene logic cannot be built', async () => {
+ jest.useFakeTimers()
+ const captureSpy = jest.spyOn(posthog, 'capture')
+ try {
+ const streamSpy = mockStream()
+ // On the dashboard scene, but its logic key hasn't resolved / can't be built, so there
+ // is no logic to wait on. The gate must not hold for the full cap in this state.
+ jest.spyOn(sceneLogic.selectors, 'activeSceneId').mockReturnValue(Scene.Dashboard)
+ jest.spyOn(sceneLogic.selectors, 'activeSceneLogic').mockReturnValue(null as any)
+
+ 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?')
+
+ await jest.advanceTimersByTimeAsync(300)
+
+ expect(streamSpy).toHaveBeenCalledTimes(1)
+ expect(captureSpy).not.toHaveBeenCalledWith('max dashboard context wait timed out', expect.anything())
+ } 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.tsx
index 4b08a3a700db..de1b7c55a136 100644
--- a/frontend/src/scenes/max/maxThreadLogic.tsx
+++ b/frontend/src/scenes/max/maxThreadLogic.tsx
@@ -91,6 +91,7 @@ import { posthogAiContextLogic } from './posthogAiContextLogic'
import { MAX_SLASH_COMMANDS, SlashCommand } from './slash-commands'
import { getToolCallDescriptionAndWidgetDef } from './toolCallDisplay'
import {
+ activeSceneLogicHasMaxContext,
findPendingClientToolCall,
getAgentModeForScene,
isAssistantMessage,
@@ -1292,9 +1293,18 @@ export const maxThreadLogic = kea<maxThreadLogicType>([
return false
}
const activeSceneLogic = sceneLogic.values.activeSceneLogic
- if (!activeSceneLogic || !('maxContext' in activeSceneLogic.selectors)) {
+ if (!activeSceneLogic) {
+ // No dashboard scene logic to wait on — its key hasn't resolved or it can't be
+ // built. Nothing will land, so don't block: send now rather than stalling for the
+ // full cap and shipping without context anyway.
return false
}
+ if (!activeSceneLogicHasMaxContext(activeSceneLogic)) {
+ // The logic exists but isn't mounted yet — building, or briefly unmounted mid
+ // dashboard→dashboard navigation. Keep waiting (bounded by the cap) so context
+ // collection picks up the dashboard once it mounts.
+ return true
+ }
return !(activeSceneLogic.values as { dashboard?: unknown }).dashboard
}
// Measure real elapsed time, not tick count: breakpoint() only guarantees a *minimum*
diff --git a/frontend/src/scenes/max/posthogAiContextLogic.tsx b/frontend/src/scenes/max/posthogAiContextLogic.tsx
index c74fae08deb2..03aeb5252b56 100644
--- a/frontend/src/scenes/max/posthogAiContextLogic.tsx
+++ b/frontend/src/scenes/max/posthogAiContextLogic.tsx
@@ -9,6 +9,7 @@ import { sceneLogic } from 'scenes/sceneLogic'
import { AttachedContext, MaxContextInput, MaxContextType } from './maxTypes'
import type { posthogAiContextLogicType } from './posthogAiContextLogicType'
+import { activeSceneLogicHasMaxContext } from './utils'
export interface PosthogAiContextLogicProps {
conversationId: string
@@ -131,7 +132,7 @@ export const posthogAiContextLogic = kea<posthogAiContextLogicType>([
listeners(({ values, actions }) => ({
syncSceneAttachments: () => {
const activeSceneLogic = values.activeSceneLogic
- if (!activeSceneLogic || !('maxContext' in activeSceneLogic.selectors)) {
+ if (!activeSceneLogicHasMaxContext(activeSceneLogic)) {
return
}
let sceneItems: MaxContextInput[] = []
diff --git a/frontend/src/scenes/max/utils.test.ts b/frontend/src/scenes/max/utils.test.ts
index 7a2bd975ec34..518a55a96a86 100644
--- a/frontend/src/scenes/max/utils.test.ts
+++ b/frontend/src/scenes/max/utils.test.ts
@@ -1,3 +1,5 @@
+import type { BuiltLogic } from 'kea'
+
import {
AssistantMessage,
AssistantMessageType,
@@ -6,7 +8,12 @@ import {
} from '~/queries/schema/schema-assistant-messages'
import { EnhancedToolCall } from './max-constants'
-import { findPendingClientToolCall, isMultiQuestionFormMessage, threadEndsWithMultiQuestionForm } from './utils'
+import {
+ activeSceneLogicHasMaxContext,
+ findPendingClientToolCall,
+ isMultiQuestionFormMessage,
+ threadEndsWithMultiQuestionForm,
+} from './utils'
describe('max/utils', () => {
describe('isMultiQuestionFormMessage()', () => {
@@ -281,4 +288,26 @@ describe('max/utils', () => {
expect(findPendingClientToolCall([humanMessage('Hello')], clientToolNames)).toBeNull()
})
})
+
+ describe('activeSceneLogicHasMaxContext()', () => {
+ it('returns false for null or undefined scene logic', () => {
+ expect(activeSceneLogicHasMaxContext(null)).toBe(false)
+ expect(activeSceneLogicHasMaxContext(undefined)).toBe(false)
+ })
+
+ it.each([
+ { mounted: true, hasMaxContext: true, expected: true },
+ { mounted: true, hasMaxContext: false, expected: false },
+ // The crux of the fix: a built-but-unmounted scene logic must be rejected, since reading
+ // its selectors/values throws `[KEA] Can not find path` once its reducer path is gone.
+ { mounted: false, hasMaxContext: true, expected: false },
+ { mounted: false, hasMaxContext: false, expected: false },
+ ])('mounted=$mounted hasMaxContext=$hasMaxContext -> $expected', ({ mounted, hasMaxContext, expected }) => {
+ const logic = {
+ isMounted: () => mounted,
+ selectors: hasMaxContext ? { maxContext: () => [] } : {},
+ } as unknown as BuiltLogic
+ expect(activeSceneLogicHasMaxContext(logic)).toBe(expected)
+ })
+ })
})
diff --git a/frontend/src/scenes/max/utils.ts b/frontend/src/scenes/max/utils.ts
index f7ee80cdf738..3d606f59fc64 100644
--- a/frontend/src/scenes/max/utils.ts
+++ b/frontend/src/scenes/max/utils.ts
@@ -1,3 +1,4 @@
+import type { BuiltLogic } from 'kea'
import posthog from 'posthog-js'
import { FEATURE_FLAGS } from 'lib/constants'
@@ -376,3 +377,16 @@ export const visualizationTypeToQuery = (
}
return source
}
+
+/**
+ * Whether it's safe to read auto-context from the active scene logic. `sceneLogic`'s
+ * `activeSceneLogic` is built but may already be unmounted mid scene-transition (e.g. navigating
+ * away from a dashboard); reading its selectors/values then throws `[KEA] Can not find path`
+ * because the reducer path is gone from the store. Check this at read time — mounted state changes
+ * without a selector-input change, so it can't be memoized upstream.
+ */
+export function activeSceneLogicHasMaxContext(
+ activeSceneLogic: BuiltLogic | null | undefined
+): activeSceneLogic is BuiltLogic {
+ return !!activeSceneLogic?.isMounted() && 'maxContext' in activeSceneLogic.selectors
+}