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(signals): echo the prompt Ask AI sent, not the raw question (#108272)

Vojta Bartošebc85129c483
diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.test.ts b/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
index 43225e7e50a8..e81bae4282d6 100644
--- a/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
+++ b/products/posthog_ai/frontend/logics/runStreamLogic.test.ts
@@ -1206,6 +1206,47 @@ describe('runStreamLogic', () => {
).toEqual(['still here', '10', 'answer to 10', '11'])
})
+ it('stops sinking a send the agent never took up once two turns closed over it', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.pushHumanMessage('never echoed')
+ logic.actions.ingestAcpFrame(notification('_posthog/turn_complete', {}))
+ logic.actions.ingestAcpFrame(
+ sessionUpdate({ sessionUpdate: 'agent_message', messageId: 'm1', content: { text: 'first' } })
+ )
+ logic.actions.ingestAcpFrame(notification('_posthog/turn_complete', {}))
+ logic.actions.ingestAcpFrame(
+ sessionUpdate({ sessionUpdate: 'agent_message', messageId: 'm2', content: { text: 'second' } })
+ )
+ }).toFinishAllListeners()
+
+ expect(
+ logic.values.threadItems
+ .filter((item) => item.type === 'human_message' || item.type === 'assistant_message')
+ .map((item) => item.text)
+ ).toEqual(['never echoed', 'first', 'second'])
+ })
+
+ it('still takes up a send that settled when its echo finally arrives', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.pushHumanMessage('late echo')
+ logic.actions.ingestAcpFrame(notification('_posthog/turn_complete', {}))
+ logic.actions.ingestAcpFrame(
+ sessionUpdate({ sessionUpdate: 'agent_message', messageId: 'm1', content: { text: 'first' } })
+ )
+ logic.actions.ingestAcpFrame(notification('_posthog/turn_complete', {}))
+ logic.actions.ingestAcpFrame(notification('_posthog/user_message', { content: 'late echo' }))
+ logic.actions.ingestAcpFrame(
+ sessionUpdate({ sessionUpdate: 'agent_message', messageId: 'm2', content: { text: 'at last' } })
+ )
+ }).toFinishAllListeners()
+
+ expect(
+ logic.values.threadItems
+ .filter((item) => item.type === 'human_message' || item.type === 'assistant_message')
+ .map((item) => item.text)
+ ).toEqual(['first', 'late echo', 'at last'])
+ })
+
it('leaves a send typed mid-answer below the text already streaming', async () => {
await expectLogic(logic, () => {
logic.actions.ingestAcpFrame(notification('_posthog/turn_complete', {}))
diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.ts b/products/posthog_ai/frontend/logics/runStreamLogic.ts
index 0f2e393dcdb6..ede76c0a28da 100644
--- a/products/posthog_ai/frontend/logics/runStreamLogic.ts
+++ b/products/posthog_ai/frontend/logics/runStreamLogic.ts
@@ -682,6 +682,10 @@ function onlyDebugRowsFollow(state: ThreadItem[], idx: number, waitingIds: Reado
return true
}
+// The agent takes a queued or steering send up at the next turn boundary, so a placeholder that
+// outlives a second boundary has missed the window its echo could arrive in.
+const SETTLED_AFTER_TURNS = 2
+
/** 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
@@ -1502,7 +1506,7 @@ export function foldLogToThread(
// steering send is echoed only when the agent takes it up, which is a turn later than the bubble,
// so the pairing outlives that turn. Text is what an echo matches on, because a client echo
// carries no id, and send order keeps repeated sends of one text apart.
- const waitingSends: { id: string; text: string }[] = []
+ const waitingSends: { id: string; text: string; turns: number }[] = []
// Sends this turn already paired, counted per text so the same send's second wire form takes no
// further placeholder while a second send of that text still takes its own.
const pairedSends = new Map<string, number>()
@@ -1584,7 +1588,8 @@ export function foldLogToThread(
}
}
- const waitingSendIds = (): ReadonlySet<string> => new Set(waitingSends.map((send) => send.id))
+ const waitingSendIds = (): ReadonlySet<string> =>
+ new Set(waitingSends.filter((send) => send.turns < SETTLED_AFTER_TURNS).map((send) => send.id))
const appendChunk = (id: string, type: ThreadItemType, delta: string): void => {
const idx = findLastBufferIndex(items, id, type, false)
@@ -1806,7 +1811,7 @@ export function foldLogToThread(
if (method === '_client/human_message') {
const optimisticText = String(params.content ?? '')
const id = pushHuman(optimisticText, optimisticAttachments(params.attachments), { atFoot: true })
- waitingSends.push({ id, text: optimisticText })
+ waitingSends.push({ id, text: optimisticText, turns: 0 })
continue
}
if (method === '_client/error') {
@@ -1835,6 +1840,7 @@ export function foldLogToThread(
...(timestamp !== undefined && { startedAt: timestamp }),
})
pairedSends.clear()
+ waitingSends.forEach((send) => (send.turns += 1))
continue
}
if (method === '_posthog/progress') {
@@ -2058,7 +2064,8 @@ export function foldLogToThread(
}
// A send the agent has not taken up sits below everything that has landed, in send order, however
- // much arrived after the composer drew it.
+ // much arrived after the composer drew it. A settled one leaves the sink, so a send whose echo
+ // can no longer pair cannot walk the thread for the rest of the run.
const waiting = waitingSendIds()
if (waiting.size > 0) {
items = [...items.filter((item) => !waiting.has(item.id)), ...items.filter((item) => waiting.has(item.id))]
diff --git a/products/signals/frontend/inbox/inboxTaskKickoffLogic.test.ts b/products/signals/frontend/inbox/inboxTaskKickoffLogic.test.ts
index 07dd57e1c1d4..874185cb6dda 100644
--- a/products/signals/frontend/inbox/inboxTaskKickoffLogic.test.ts
+++ b/products/signals/frontend/inbox/inboxTaskKickoffLogic.test.ts
@@ -206,6 +206,20 @@ describe('inboxTaskKickoffLogic', () => {
})
})
+ it('shows the prompt it sent, so the message pairs with the agent echo', async () => {
+ logic.actions.openReportDiscussion(report, 'https://example.com/report')
+
+ await expectLogic(logic, () =>
+ logic.actions.discussReport(report, 'https://example.com/report', 'Explain the recommendation')
+ ).toFinishAllListeners()
+
+ const { streamKey } = runnerPanelLogic({ panelId: REPORT_AI_PANEL_ID }).values.activeCreation ?? {}
+ const { threadItems } = runStreamLogic({ streamKey: String(streamKey) }).values
+ expect(threadItems.filter((item) => item.type === 'human_message').map((item) => item.text)).toEqual([
+ createdTasks[0].description,
+ ])
+ })
+
it('warms a repo-less sandbox for the report when Ask AI opens, and only once per report', async () => {
warmResponse = { task_id: 'warm-task', run_id: 'warm-run' }
diff --git a/products/signals/frontend/inbox/inboxTaskKickoffLogic.ts b/products/signals/frontend/inbox/inboxTaskKickoffLogic.ts
index a05413b48464..58a47b848404 100644
--- a/products/signals/frontend/inbox/inboxTaskKickoffLogic.ts
+++ b/products/signals/frontend/inbox/inboxTaskKickoffLogic.ts
@@ -732,10 +732,13 @@ export const inboxTaskKickoffLogic = kea<inboxTaskKickoffLogicType>([
return
}
try {
- const prompt = wrapWithPosthogContext(
- buildDiscussReportPrompt(currentReport, reportUrl, agentQuestion ?? question, intent),
- contextItems
+ const discussPrompt = buildDiscussReportPrompt(
+ currentReport,
+ reportUrl,
+ agentQuestion ?? question,
+ intent
)
+ const prompt = wrapWithPosthogContext(discussPrompt, contextItems)
const warmLease = values.reportWarmLease?.reportId === report.id ? values.reportWarmLease : null
if (warmLease) {
actions.setReportWarmLease(null)
@@ -763,7 +766,9 @@ export const inboxTaskKickoffLogic = kea<inboxTaskKickoffLogicType>([
cache.disposables.add(() => stream.mount(), OPTIMISTIC_REPORT_STREAM, {
pauseOnPageHidden: false,
})
- stream.actions.startOptimisticRun(question)
+ // The thread pairs this with its wire echo by message text, so it has to be the
+ // prompt that was sent rather than the question inside it.
+ stream.actions.startOptimisticRun(discussPrompt)
actions.openReportTask(report, taskId, runId, streamKey)
}
captureInboxReportActionCompleted({ report, actionType: 'discuss', outcome: 'success' })