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(posthog-ai): keep a streamed answer whole across a debug row (#102899)

Vojta Bartoš130f3a1c25db
diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.test.ts b/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
index 8ce6296e12ae..d7a989fa1c92 100644
--- a/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
+++ b/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
@@ -254,6 +254,44 @@ describe('runStreamLogic', () => {
})
expect(result.threadItems.find((item) => item.id === 'missing-start')?.startedAt).toBeUndefined()
})
+
+ it.each([
+ {
+ label: 'a debug row stays whole',
+ interloper: notification('_posthog/console', { level: 'debug', message: 'Refresh session requested' }),
+ expected: ['the snapshot runner stops waiting on a skeleton'],
+ },
+ {
+ label: 'a tool call splits at the boundary',
+ interloper: sessionUpdate({ sessionUpdate: 'tool_call', toolCallId: 'tc-1', status: 'in_progress' }),
+ expected: ['the snapshot runner stops wa', 'iting on a skeleton'],
+ },
+ ])('a streamed answer interrupted by $label', ({ interloper, expected }) => {
+ const frames = [
+ sessionUpdate({
+ sessionUpdate: 'agent_message_chunk',
+ messageId: 'm-1',
+ content: { type: 'text', text: 'the snapshot runner stops wa' },
+ }),
+ interloper,
+ sessionUpdate({
+ sessionUpdate: 'agent_message_chunk',
+ messageId: 'm-1',
+ content: { type: 'text', text: 'iting on a skeleton' },
+ }),
+ ]
+
+ const result = foldLogToThread(
+ frames.map((entry) => ({ source: 'replay' as const, entry })),
+ { isResumeRun: false }
+ )
+
+ // A debug row renders nothing for most viewers, so a split there would have no visible cause.
+ expect(
+ result.threadItems.filter((item) => item.type === 'assistant_message').map((item) => item.text)
+ ).toEqual(expected)
+ })
+
it.each([
['Claude', 'live'],
['Claude', 'history'],
diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.ts b/products/posthog_ai/frontend/logics/runStreamLogic.ts
index b9e312f65310..54e9ca6eb583 100644
--- a/products/posthog_ai/frontend/logics/runStreamLogic.ts
+++ b/products/posthog_ai/frontend/logics/runStreamLogic.ts
@@ -601,6 +601,20 @@ function findLastBufferIndex(state: ThreadItem[], id: string, type: ThreadItemTy
return -1
}
+/**
+ * A debug row carries a `_posthog/console` line from the agent server, and `threadItems` gates it on
+ * `showDebugLogs`, so for most viewers it renders nothing. Ending a streamed message on one splits
+ * the answer wherever the delta happened to end, with nothing between the halves to explain it.
+ */
+function onlyDebugRowsFollow(state: ThreadItem[], idx: number): boolean {
+ for (let i = idx + 1; i < state.length; i++) {
+ if (state[i].type !== 'debug') {
+ return false
+ }
+ }
+ return true
+}
+
/** The in-progress spinner for a long-running status — retired when it completes, fails, or its boundary lands. */
function isPendingStatus(item: ThreadItem, status: string): boolean {
return item.type === 'status' && item.status === status && item.isComplete !== true
@@ -1194,13 +1208,14 @@ export function foldLogToThread(
const appendChunk = (id: string, type: ThreadItemType, delta: string): void => {
const idx = findLastBufferIndex(items, id, type, false)
- // Continue the matched buffer only while it's still the tail and incomplete; otherwise (no
- // buffer, a finalized one, or one interrupted by a tool call/separator) start a fresh bubble
- // so text resuming after an interruption renders in chronological order. Every fresh bubble
- // gets a unique `${id}@<seq>` id — the wire often omits `messageId` (and the S3 replay always
- // does, since the backend drops chunks), so the bare fallback id would collide as a React key
- // across messages. The continuation lookup matches the `${id}@` prefix, so it still works.
- if (idx === -1 || items[idx].complete || idx !== items.length - 1) {
+ // Continue the matched buffer only while it's incomplete and only debug rows followed it;
+ // otherwise (no buffer, a finalized one, or one interrupted by a tool call/separator) start
+ // a fresh bubble so text resuming after an interruption renders in chronological order.
+ // Every fresh bubble gets a unique `${id}@<seq>` id — the wire often omits `messageId` (and
+ // the S3 replay always does, since the backend drops chunks), so the bare fallback id would
+ // collide as a React key across messages. The continuation lookup matches the `${id}@`
+ // prefix, so it still works.
+ if (idx === -1 || items[idx].complete || !onlyDebugRowsFollow(items, idx)) {
items.push({
id: `${id}@${bubbleSeq++}`,
type,