: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(posthog-ai): show when a turn stops early and why a queued message waits (#120582)
posthog[bot]7dd7164adca5
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 3ecdd6376ae4..f6e5c3f84012 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -7671,9 +7671,9 @@ snapshots:products-posthog-ai-queuedmessagelist--empty--light:hash: v1.k794b7964.8ff99b0d11642dd53f5f0f70fc69106ee0e0158728fd6a73cf8699c4d68f8186.BQuDnKXQn4DwravdDwbmq3GC_BR06D3nntvW0bDvyzEproducts-posthog-ai-queuedmessagelist--held--dark:- hash: v1.k794b7964.2d66446284f130978b3290d7b3562839ead19cf4533ec7f9fc49e154261a7e44.zUN-L242xFuvmgjcGFJF4wzS_hS1KQhFfVLFBkxhXHc+ hash: v1.k794b7964.1ca863789adfa0501f7afab71692fa7ad4ad0e1f0b2d07b8e3b2a1209b1c4a8a.PQslwLlPX35V7gE8P65Il12FcAH2MvJhacLJuKZJw5sproducts-posthog-ai-queuedmessagelist--held--light:- hash: v1.k794b7964.22ceb3b4dc894ff82f41e975b5fc7d4629659961900e3701eebc5c3502273233.iL8GeYKEdNPHfvd0d6k_QpLobW9yCS4iXEjLGOJBP2o+ hash: v1.k794b7964.4f06731b1429f125483891ce350d1aa0a16898deda4c1fb0608646e2a690bf42.Or92bG7oI4N06Y1vmq8Z_mWLp8iVp_QBkJIwW5A_x3wproducts-posthog-ai-queuedmessagelist--multiple--dark:hash: v1.k794b7964.944199db52c0297f3eb56cd1df476f976e11d8e224afbd556ec4864c7cffb324.hDBgtSt7QWpeqbEcIvPWWLh_a8mBMXy12Ia6tP6XWUMproducts-posthog-ai-queuedmessagelist--multiple--light:diff --git a/products/posthog_ai/frontend/components/QueuedMessageList.stories.tsx b/products/posthog_ai/frontend/components/QueuedMessageList.stories.tsxindex b6b242551509..8e5268f3c4c9 100644--- a/products/posthog_ai/frontend/components/QueuedMessageList.stories.tsx+++ b/products/posthog_ai/frontend/components/QueuedMessageList.stories.tsx@@ -10,7 +10,7 @@ const meta: Meta<typeof QueuedMessageList> = {title: 'Products/PostHog AI/QueuedMessageList',component: QueuedMessageList,tags: ['autodocs'],- render: ({ messages, steerPending, held }) => {+ render: ({ messages, steerPending, hold }) => {const [queue, setQueue] = useState<QueuedMessage[]>(messages)return (<div className="max-w-180 mx-auto p-4">@@ -20,7 +20,7 @@ const meta: Meta<typeof QueuedMessageList> = {onRemove={(id) => setQueue((q) => q.filter((m) => m.id !== id))}onSteer={() => setQueue([])}steerPending={steerPending}- held={held}+ hold={hold}/></div>)@@ -39,9 +39,9 @@ export const WaitingForApproval: Story = {parameters: { testOptions: { waitForLoadersToDisappear: false } },}-/** A Stop or a failed send leaves the message staged. Nothing sends it until the user steers it. */+/** A Stop leaves the message staged. Nothing sends it until the user steers it. */export const Held: Story = {- args: { messages: [{ id: '1', content: 'Also break it down by browser' }], held: true },+ args: { messages: [{ id: '1', content: 'Also break it down by browser' }], hold: 'cancelled' },}export const Multiple: Story = {diff --git a/products/posthog_ai/frontend/components/QueuedMessageList.tsx b/products/posthog_ai/frontend/components/QueuedMessageList.tsxindex fd5084ea6d9b..a746de8321de 100644--- a/products/posthog_ai/frontend/components/QueuedMessageList.tsx+++ b/products/posthog_ai/frontend/components/QueuedMessageList.tsx@@ -6,7 +6,7 @@ import { LemonButton, LemonTextArea } from '@posthog/lemon-ui'import { KeyboardShortcut } from 'lib/components/KeyboardShortcut/KeyboardShortcut'import { cn } from 'lib/utils/css-classes'-import type { QueuedMessage } from '../logics/runInteractionLogic'+import type { QueueHold, QueuedMessage } from '../logics/runInteractionLogic'import { splitLeadingCommentContexts } from '../utils/commentContext'import { CommentContextChip } from './CommentContextChip'@@ -17,12 +17,17 @@ export interface QueuedMessageListProps {onSteer?: () => voidsteerPending?: booleansteerDisabledReason?: string- /** The staged messages wait on the user, not on the agent — say so instead of looking like a queue. */- held?: boolean+ /** Why the staged messages wait on the user, not on the agent — say so instead of looking like a queue. */+ hold?: QueueHold/** Reports an open row editor, so the consumer can hold a send that would ship the pre-edit text. */onEditingChange?: (editing: boolean) => void}+const HOLD_REASONS: Record<NonNullable<QueueHold>, string> = {+ cancelled: "The agent stopped, so these weren't sent. Send them when you're ready.",+ unconfirmed: "We couldn't confirm these reached the agent. Send them again, or remove them.",+}+interface QueuedMessageItemProps {message: QueuedMessageisEditing: boolean@@ -132,9 +137,10 @@ export function QueuedMessageList({onSteer,steerPending = false,steerDisabledReason,- held = false,+ hold = null,onEditingChange,}: QueuedMessageListProps): JSX.Element | null {+ const held = hold !== nullconst [editingId, setEditingId] = useState<string | null>(null)const [collapsed, setCollapsed] = useState(false)// A row can leave while its editor is open — a flush clears the queue, and the consumer may drop a row@@ -195,6 +201,11 @@ export function QueuedMessageList({</LemonButton>)}</div>+ {held && (+ <p className="mb-0 px-2 text-xs text-warning" data-attr="run-queue-hold-reason">+ {HOLD_REASONS[hold]}+ </p>+ )}{/* Capped so a fast typist can't grow the banner until it pushes the composer off screen. */}{!collapsed && (<div className="flex flex-col gap-1 max-h-48 overflow-y-auto">diff --git a/products/posthog_ai/frontend/components/ThreadItems.tsx b/products/posthog_ai/frontend/components/ThreadItems.tsxindex ea2c3abf5824..d99854a2848d 100644--- a/products/posthog_ai/frontend/components/ThreadItems.tsx+++ b/products/posthog_ai/frontend/components/ThreadItems.tsx@@ -42,6 +42,13 @@ export function StatusItem({ item }: { item: ThreadItem }): JSX.Element {: "Couldn't compact the conversation"return <StatusLine icon={<IconWarning className="size-3" />}>{reason}</StatusLine>}+ if (item.status === 'turn_cancelled') {+ return (+ <StatusLine icon={<IconWarning className="size-3" />}>+ The agent stopped before it finished. Background agents may have stopped too.+ </StatusLine>+ )+ }if (item.status === 'extension_notice' && item.message) {return <StatusLine icon={<IconWarning className="size-3" />}>{item.message}</StatusLine>}diff --git a/products/posthog_ai/frontend/components/quill/QuillSeparatorRow.tsx b/products/posthog_ai/frontend/components/quill/QuillSeparatorRow.tsxindex 6bd01459499d..e159a5d47c24 100644--- a/products/posthog_ai/frontend/components/quill/QuillSeparatorRow.tsx+++ b/products/posthog_ai/frontend/components/quill/QuillSeparatorRow.tsx@@ -16,6 +16,7 @@ const SETTLED_LABELS: Record<string, string> = {compacting_failed: "Couldn't compact the conversation",refusal: 'The model declined this request. Rephrase it, or switch models and try again.',refusal_fallback: 'Request declined, retried with the fallback model',+ turn_cancelled: 'The agent stopped before it finished. Background agents may have stopped too.',}function statusLabel(item: ThreadItem, live: boolean): { label: string; running: boolean } {diff --git a/products/posthog_ai/frontend/logics/runCancellationLogic.test.ts b/products/posthog_ai/frontend/logics/runCancellationLogic.test.tsindex 2dfb5d2587e7..a7a04736c675 100644--- a/products/posthog_ai/frontend/logics/runCancellationLogic.test.ts+++ b/products/posthog_ai/frontend/logics/runCancellationLogic.test.ts@@ -174,7 +174,9 @@ describe('runCancellationLogic', () => {error: { code: -32000, message: 'Agent unavailable' },})}- await expectLogic(logic, () => logic.actions.requestCancellation()).toFinishAllListeners()+ await expectLogic(logic, () => logic.actions.requestCancellation())+ .toFinishAllListeners()+ .toDispatchActions(['cancellationFailed'])expect(logic.values.cancellationState).toBeNull()expect(lemonToast.error).toHaveBeenCalled()await expectLogic(logic, () => logic.actions.requestCancellation()).toFinishAllListeners()diff --git a/products/posthog_ai/frontend/logics/runCancellationLogic.ts b/products/posthog_ai/frontend/logics/runCancellationLogic.tsindex 974eedc970fd..4b8db64f5f4a 100644--- a/products/posthog_ai/frontend/logics/runCancellationLogic.ts+++ b/products/posthog_ai/frontend/logics/runCancellationLogic.ts@@ -69,6 +69,9 @@ export interface runCancellationLogicActions {cancelWhenReady: () => {value: true}+ cancellationFailed: () => {+ value: true+ }clearCancellation: () => {value: true}@@ -109,6 +112,8 @@ export const runCancellationLogic: LogicWrapper<runCancellationLogicType> = kea<requestCancellation: true,cancelWhenReady: true,setCancellationState: (state: CancellationState) => ({ state }),+ // Its own action, so a listener can tell a failed Stop from the clear that a turn end brings.+ cancellationFailed: true,clearCancellation: true,}),reducers({@@ -116,6 +121,7 @@ export const runCancellationLogic: LogicWrapper<runCancellationLogicType> = kea<null as CancellationState,{setCancellationState: (_, { state }) => state,+ cancellationFailed: () => null,clearCancellation: () => null,},],@@ -156,12 +162,15 @@ export const runCancellationLogic: LogicWrapper<runCancellationLogicType> = kea<}} catch (error) {if (!disposables.isDisposed && cache.cancelRequest === request && values.bootstrappedRunId === runId) {- actions.clearCancellation()+ actions.cancellationFailed()lemonToast.error("Couldn't stop this run. Please try again.")posthog.captureException(error)}}},+ cancellationFailed: () => {+ cache.cancelRequest = undefined+ },clearCancellation: () => {cache.cancelRequest = undefined},diff --git a/products/posthog_ai/frontend/logics/runInteractionLogic.test.ts b/products/posthog_ai/frontend/logics/runInteractionLogic.test.tsindex 0a33c441ff1b..e18248e5d16d 100644--- a/products/posthog_ai/frontend/logics/runInteractionLogic.test.ts+++ b/products/posthog_ai/frontend/logics/runInteractionLogic.test.ts@@ -1,4 +1,5 @@import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'import { ApiError } from 'lib/api-error'import { lemonToast } from 'lib/lemon-ui/LemonToast'@@ -17,6 +18,7 @@ import {import { TaskRuntimeEnumApi } from 'products/tasks/frontend/generated/api.schemas'import type { PermissionRequestRecord } from '../types/streamTypes'+import type { StoredLogEntry } from '../types/wireTypes'import { uploadRunAttachments, uploadStagedTaskAttachments } from '../utils/artifactUpload'import { contextItemLine } from '../utils/posthogContextBlock'import { attachedContextLogic } from './attachedContextLogic'@@ -64,7 +66,7 @@ jest.mock('./runStreamLogic', () => {clearPermissionRequest: true,reset: true,appendEntries: true,- ingestAcpFrame: true,+ ingestAcpFrame: (entry: unknown, source: string = 'live') => ({ entry, source }),closeSse: true,bootstrapRun: true,}),@@ -1076,6 +1078,42 @@ describe('runInteractionLogic', () => {expect(logic.values.queueHeld).toBe(false)})+ it.each([+ ['steer', 'steer'],+ ['stop', 'user_stop'],+ ['failed stop then steer', 'steer'],+ ['failed steer', 'unknown'],+ ['nothing', 'unknown'],+ ])('reports what came before a cancelled turn: %s', async (trigger, cause) => {+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ setThinking(true)+ if (trigger.includes('stop')) {+ logic.actions.requestCancellation()+ }+ if (trigger.startsWith('failed stop')) {+ runCancellationLogic({ streamKey: RUN_ID }).actions.cancellationFailed()+ }+ if (trigger === 'failed steer') {+ ;(tasksRunsCommandCreate as jest.Mock).mockRejectedValueOnce(new Error('Connection lost'))+ }+ if (trigger.includes('steer')) {+ logic.actions.enqueueMessage('change course')+ await expectLogic(logic, () => logic.actions.steerQueue()).toFinishAllListeners()+ }+ const turnComplete = (stopReason: string): StoredLogEntry => ({+ type: 'notification',+ notification: { method: '_posthog/turn_complete', params: { stopReason } },+ })++ stream.actions.ingestAcpFrame(turnComplete('cancelled'))+ stream.actions.ingestAcpFrame(turnComplete('cancelled'))++ const causes = capture.mock.calls+ .filter(([event]) => event === 'task_run_turn_cancelled')+ .map(([, properties]) => properties?.cause)+ expect(causes).toEqual([cause, 'unknown'])+ })+it('holds an unconfirmed send as its own rows until the user asks for it again', async () => {;(tasksRunsCommandCreate as jest.Mock).mockRejectedValueOnce(new Error('Connection lost'))setThinking(true)diff --git a/products/posthog_ai/frontend/logics/runInteractionLogic.ts b/products/posthog_ai/frontend/logics/runInteractionLogic.tsindex 6d935fd36e3b..2d90472df230 100644--- a/products/posthog_ai/frontend/logics/runInteractionLogic.ts+++ b/products/posthog_ai/frontend/logics/runInteractionLogic.ts@@ -14,6 +14,7 @@ import {} from 'kea'import { forms } from 'kea-forms'import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'+import posthog from 'posthog-js'import { ApiError, readableErrorMessage } from 'lib/api-error'import { lemonToast } from 'lib/lemon-ui/LemonToast'@@ -121,6 +122,8 @@ export interface RunContinuationHandoff {*/export type QueueHold = 'cancelled' | 'unconfirmed' | null+type TurnCancelCause = 'user_stop' | 'steer' | 'unknown'+/** One follow-up staged in the queue while the agent is mid-turn. */export interface QueuedMessage {id: string@@ -1266,6 +1269,41 @@ export const runInteractionLogic = kea<runInteractionLogicType>([}return {+ // Only a Stop the cancellation logic accepts counts: one it ignores or fails to send must not claim+ // the next cancel, which a later steer may cause.+ [runCancellationLogic({ streamKey: props.streamKey ?? props.runId }).actionTypes.setCancellationState]: ({+ state,+ }) => {+ if (state === 'waiting') {+ cache.turnCancelCause = 'user_stop'+ }+ },+ [runCancellationLogic({ streamKey: props.streamKey ?? props.runId }).actionTypes.cancellationFailed]:+ () => {+ cache.turnCancelCause = undefined+ },+ // Separates a cancel the user asked for from one a steer caused, which the thread can't tell apart.+ [runStreamLogic({ streamKey: props.streamKey ?? props.runId }).actionTypes.ingestAcpFrame]: ({+ entry,+ source,+ }) => {+ if (+ source !== 'live' ||+ entry.notification.method !== '_posthog/turn_complete' ||+ (entry.source_run_id && entry.source_run_id !== values.bootstrappedRunId)+ ) {+ return+ }+ const cause: TurnCancelCause = cache.turnCancelCause ?? 'unknown'+ cache.turnCancelCause = undefined+ if (entry.notification.params?.stopReason === 'cancelled') {+ posthog.capture('task_run_turn_cancelled', {+ cause,+ task_id: props.taskId,+ run_id: values.bootstrappedRunId,+ })+ }+ },handleEscape: () => {if (values.cancellationState || values.steerPending) {return@@ -1471,6 +1509,9 @@ export const runInteractionLogic = kea<runInteractionLogicType>([if (!isCurrent() || values.hasUnresolvedApproval) {throw new Error('The run cannot accept this message yet')}+ if (steer && !cache.turnCancelCause) {+ cache.turnCancelCause = 'steer'+ }const response = await tasksRunsCommandCreate(String(projectId), taskId, runId, {jsonrpc: '2.0',method: 'user_message',@@ -1521,6 +1562,9 @@ export const runInteractionLogic = kea<runInteractionLogicType>([} else {actions.queueDeliveryFailed()restoreQueuedRows(rows, content)+ if (steer && cache.turnCancelCause === 'steer') {+ cache.turnCancelCause = undefined+ }}actions.finishTaskDraftDelivery()lemonToast.error('Failed to send message. Please try again.')diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.test.ts b/products/posthog_ai/frontend/logics/runStreamLogic.test.tsindex 42c03236cac4..63eb5915f8c1 100644--- a/products/posthog_ai/frontend/logics/runStreamLogic.test.ts+++ b/products/posthog_ai/frontend/logics/runStreamLogic.test.ts@@ -1503,6 +1503,24 @@ describe('runStreamLogic', () => {expect(logic.values.latestTurnTraceId).toBe(TRACE)})+ it.each([+ ['cancelled', ['human_message', 'assistant_message', 'status', 'turn_separator']],+ ['end_turn', ['human_message', 'assistant_message', 'turn_separator']],+ ])('marks a turn that stops early: %s', async (stopReason, types) => {+ await expectLogic(logic, () => {+ logic.actions.ingestAcpFrame(notification('_posthog/user_message', { content: 'q' }))+ logic.actions.ingestAcpFrame(+ sessionUpdate({ sessionUpdate: 'agent_message', messageId: 'm1', content: { text: 'a' } })+ )+ logic.actions.ingestAcpFrame(notification('_posthog/turn_complete', { stopReason }))+ }).toFinishAllListeners()++ expect(logic.values.threadItems.map((item) => item.type)).toEqual(types)+ expect(logic.values.threadItems.find((item) => item.type === 'status')?.status).toEqual(+ stopReason === 'cancelled' ? 'turn_cancelled' : undefined+ )+ })+it('keeps the last real turn id when a trailing traceless separator follows', async () => {await expectLogic(logic, () => {logic.actions.ingestAcpFrame(notification('_posthog/user_message', { content: 'q' }))diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.ts b/products/posthog_ai/frontend/logics/runStreamLogic.tsindex 11c594db5ffa..1422a7b0a46d 100644--- a/products/posthog_ai/frontend/logics/runStreamLogic.ts+++ b/products/posthog_ai/frontend/logics/runStreamLogic.ts@@ -2101,6 +2101,16 @@ export function foldLogFromCheckpoint(}if (method === '_posthog/turn_complete') {const traceId = typeof params.traceId === 'string' ? params.traceId : undefined+ // A Stop or a steer ends the turn early. Without a notice the thread reads as a turn that went quiet.+ if (params.stopReason === 'cancelled') {+ items.push({+ id: `turn-cancelled-${separatorSeq}`,+ type: 'status',+ status: 'turn_cancelled',+ isComplete: true,+ ...(timestamp !== undefined && { startedAt: timestamp, endedAt: timestamp }),+ })+ }items.push({id: `turn-${separatorSeq++}`,type: 'turn_separator',diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunComposer.tsx b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunComposer.tsxindex 3531d78d7294..d26a930165c6 100644--- a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunComposer.tsx+++ b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunComposer.tsx@@ -64,7 +64,7 @@ export function TaskRunComposer({isSubmitting,isBusy,queuedMessages,- queueHeld,+ queueHold,isTerminal,selectedModel,defaultModel,@@ -260,7 +260,7 @@ export function TaskRunComposer({: undefined}steerPending={steerPending || isSubmitting || !!cancellationState}- held={queueHeld}+ hold={queueHold}onEditingChange={setQueueEditing}/></Composer.Banner>