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(ai): prevent chat nav tab crash on null conversation in history (#71246)

Steven Shults8dd4100c25fc
diff --git a/frontend/src/layout/panel-layout/ai-first/tabs/NavTabChat.test.ts b/frontend/src/layout/panel-layout/ai-first/tabs/NavTabChat.test.ts
new file mode 100644
index 000000000000..ca578481c59c
--- /dev/null
+++ b/frontend/src/layout/panel-layout/ai-first/tabs/NavTabChat.test.ts
@@ -0,0 +1,27 @@
+import { MOCK_DEFAULT_BASIC_USER } from 'lib/api.mock'
+
+import { Conversation, ConversationStatus, ConversationType } from '~/types'
+
+import { groupConversations } from './NavTabChat'
+
+const conversation: Conversation = {
+ id: 'conversation-id',
+ status: ConversationStatus.Idle,
+ title: 'Testing',
+ created_at: new Date().toISOString(),
+ updated_at: new Date().toISOString(),
+ user: MOCK_DEFAULT_BASIC_USER,
+ type: ConversationType.Assistant,
+}
+
+describe('groupConversations', () => {
+ // A null slipping into conversation history (e.g. from an empty API response body) must not
+ // crash the whole chat tab render — skip it and keep the rest of the history usable.
+ it('skips null entries instead of throwing', () => {
+ const groups = groupConversations([conversation, null as unknown as Conversation])
+
+ // Assert survival of the non-null entry, not the bucket label — the label is incidental
+ // to the regression and depends on the wall clock.
+ expect(groups.flatMap((group) => group.items)).toEqual([conversation])
+ })
+})
diff --git a/frontend/src/layout/panel-layout/ai-first/tabs/NavTabChat.tsx b/frontend/src/layout/panel-layout/ai-first/tabs/NavTabChat.tsx
index e11d429bf62c..6811583b4e12 100644
--- a/frontend/src/layout/panel-layout/ai-first/tabs/NavTabChat.tsx
+++ b/frontend/src/layout/panel-layout/ai-first/tabs/NavTabChat.tsx
@@ -46,11 +46,29 @@ function getDateGroupLabel(dateString: string | null): string {
return 'Older'
}
-interface ConversationGroup {
+export interface ConversationGroup {
value: string
items: Conversation[]
}
+export function groupConversations(conversationHistory: Conversation[]): ConversationGroup[] {
+ const grouped: Record<string, Conversation[]> = {}
+ for (const conversation of conversationHistory) {
+ if (!conversation) {
+ continue
+ }
+ const label = getDateGroupLabel(conversation.updated_at)
+ if (!grouped[label]) {
+ grouped[label] = []
+ }
+ grouped[label].push(conversation)
+ }
+
+ return DATE_GROUP_ORDER.filter((label) => grouped[label]?.length > 0).map(
+ (label): ConversationGroup => ({ value: label, items: grouped[label] })
+ )
+}
+
export function NavTabChat({
inPanel = false,
onItemClick,
@@ -64,20 +82,7 @@ export function NavTabChat({
const { conversationHistory, conversationHistoryLoading, currentConversationId } = useValues(maxGlobalLogic)
const [inputValue, setInputValue] = useState('')
- const conversationGroups = useMemo(() => {
- const grouped: Record<string, Conversation[]> = {}
- for (const conversation of conversationHistory) {
- const label = getDateGroupLabel(conversation.updated_at)
- if (!grouped[label]) {
- grouped[label] = []
- }
- grouped[label].push(conversation)
- }
-
- return DATE_GROUP_ORDER.filter((label) => grouped[label]?.length > 0).map(
- (label): ConversationGroup => ({ value: label, items: grouped[label] })
- )
- }, [conversationHistory])
+ const conversationGroups = useMemo(() => groupConversations(conversationHistory), [conversationHistory])
return (
<div className="flex flex-col flex-1 overflow-hidden">
diff --git a/frontend/src/scenes/max/maxGlobalLogic.test.ts b/frontend/src/scenes/max/maxGlobalLogic.test.ts
index 52a7ff27e068..103ac895104f 100644
--- a/frontend/src/scenes/max/maxGlobalLogic.test.ts
+++ b/frontend/src/scenes/max/maxGlobalLogic.test.ts
@@ -1,6 +1,7 @@
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
+import api from 'lib/api'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'
@@ -72,6 +73,27 @@ describe('maxGlobalLogic', () => {
)
})
+ // The conversation detail endpoint can return an empty body; letting that null into
+ // conversationHistory crashes every consumer that dereferences entries (e.g. the AI chat nav tab).
+ describe('loadConversation', () => {
+ it.each([
+ { case: 'a conversation already in history', conversationId: MOCK_CONVERSATION_ID },
+ { case: 'a conversation not in history', conversationId: 'unknown-conversation-id' },
+ ])('keeps history intact when the API returns a null body for $case', async ({ conversationId }) => {
+ // Let the mount-time loadConversationHistory settle so it can't overwrite the seeded history
+ await expectLogic(logic).toDispatchActions(['loadConversationHistorySuccess'])
+ logic.actions.prependOrReplaceConversation(MOCK_CONVERSATION)
+ jest.spyOn(api.conversations, 'get').mockResolvedValue(null as any)
+
+ await expectLogic(logic, () => {
+ logic.actions.loadConversation(conversationId)
+ }).toFinishAllListeners()
+
+ expect(logic.values.conversationHistory).toHaveLength(1)
+ expect(logic.values.conversationHistory[0]?.id).toBe(MOCK_CONVERSATION_ID)
+ })
+ })
+
describe('isMaxAvailable selector', () => {
it.each([
{ realm: 'a not-yet-loaded preflight', preflight: null, expected: true },
diff --git a/frontend/src/scenes/max/maxGlobalLogic.tsx b/frontend/src/scenes/max/maxGlobalLogic.tsx
index 32b291aef2b6..a16d164ff75f 100644
--- a/frontend/src/scenes/max/maxGlobalLogic.tsx
+++ b/frontend/src/scenes/max/maxGlobalLogic.tsx
@@ -149,6 +149,10 @@ export const maxGlobalLogic = kea<maxGlobalLogicType>([
loadConversation: async (conversationId: string) => {
const response = await api.conversations.get(conversationId)
+ if (!response) {
+ // The endpoint can return an empty body; a null in the history crashes consumers
+ return values.conversationHistory
+ }
const itemIndex = values.conversationHistory.findIndex((c) => c.id === conversationId)
if (itemIndex !== -1) {