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 finished turn closed on an empty agent chunk (#125358)

Shy Alter12829f815740
diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.test.ts b/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
index 2358841a1233..71a4d1566a3e 100644
--- a/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
+++ b/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
@@ -2943,11 +2943,21 @@ describe('runStreamLogic', () => {
expect(logic.values.isThinking).toEqual(true)
})
- it('stays off after turn_complete on an update that does not mean the agent is generating', () => {
+ it.each([
+ ['a mode change', { sessionUpdate: 'current_mode_update', currentModeId: 'auto' }],
+ [
+ 'an empty message chunk',
+ { sessionUpdate: 'agent_message_chunk', messageId: 'm1', content: { type: 'text', text: '' } },
+ ],
+ [
+ 'an empty thought chunk',
+ { sessionUpdate: 'agent_thought_chunk', messageId: 't1', content: { type: 'text', text: '' } },
+ ],
+ ])('stays off after turn_complete on %s, which does not mean the agent is generating', (_case, update) => {
logic.actions.ingestAcpFrame(notification('_posthog/run_started', {}))
logic.actions.ingestAcpFrame(notification('_posthog/turn_complete', {}))
- logic.actions.ingestAcpFrame(sessionUpdate({ sessionUpdate: 'current_mode_update', currentModeId: 'auto' }))
+ logic.actions.ingestAcpFrame(sessionUpdate(update))
expect(logic.values.isThinking).toEqual(false)
})
diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.ts b/products/posthog_ai/frontend/logics/runStreamLogic.ts
index 6ff28b816087..d027d5ea882e 100644
--- a/products/posthog_ai/frontend/logics/runStreamLogic.ts
+++ b/products/posthog_ai/frontend/logics/runStreamLogic.ts
@@ -76,6 +76,7 @@ import {
type PosthogProgressParams,
type AgentCommand,
type PosthogUsageUpdateParams,
+ type SessionUpdateBody,
type SessionUpdateUsage,
type SseErrorFrameData,
type StoredLogEntry,
@@ -221,6 +222,16 @@ const AGENT_GENERATING_SESSION_UPDATES: ReadonlySet<string> = new Set([
'plan',
])
+function opensAgentTurn(update: SessionUpdateBody): boolean {
+ if (!AGENT_GENERATING_SESSION_UPDATES.has(update.sessionUpdate)) {
+ return false
+ }
+ if (update.sessionUpdate !== 'agent_message_chunk' && update.sessionUpdate !== 'agent_thought_chunk') {
+ return true
+ }
+ return String(update.content?.text ?? update.text ?? '') !== ''
+}
+
/**
* In-band durable end-of-run sentinel emitted by both the Django stream view and the agent-proxy
* (`event: stream-end`, `data: {"status":"complete"}`). Distinct from the rotation `end` event (a
@@ -4742,7 +4753,7 @@ export const runStreamLogic = kea<runStreamLogicType>([
// stream never carries one, and the persisted one is written when the message is received, so a
// message sent mid-turn is replayed *before* the previous turn's `turn_complete`. Guarded on
// `turnComplete` so this costs one dispatch per turn rather than one per streamed chunk.
- if (values.turnComplete && AGENT_GENERATING_SESSION_UPDATES.has(update.sessionUpdate)) {
+ if (values.turnComplete && opensAgentTurn(update)) {
actions.markTurnStarted()
}
if (update.sessionUpdate === 'current_mode_update') {