: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(agent): keep a streamed answer whole across in-place updates (#124627)
Shy Alterb76384f3c059
diff --git a/packages/agent/packages/agent-contracts/src/transcript-neutral-messages.ts b/packages/agent/packages/agent-contracts/src/transcript-neutral-messages.tsindex 62b1d8b60ff6..a6bd772809bd 100644--- a/packages/agent/packages/agent-contracts/src/transcript-neutral-messages.ts+++ b/packages/agent/packages/agent-contracts/src/transcript-neutral-messages.ts@@ -7,7 +7,10 @@* streaming splits one answer into two parts, and the Slack relay posts only the* last part, so the reply starts mid-sentence at whatever point the stream delta* ended. ACP's own `session/…` requests keep their boundary meaning, because a- * prompt and a permission request do belong in the transcript.+ * prompt and a permission request do belong in the transcript. A `session/update`+ * that changes session state or a tool call already on screen adds no row, so it+ * is neutral. A tool call's updates always follow its `tool_call`, which stays a+ * boundary, so the rule needs no state and every tracker agrees on it.** A response carries no method, so its own line cannot say what it answers.* Tracking the outstanding `_posthog/` control calls is what separates the@@ -38,6 +41,15 @@ const TRANSCRIPT_EVENT_NOTIFICATION_METHODS = new Set(["_posthog/user_message",]);+const NEUTRAL_SESSION_UPDATES = new Set<unknown>([+ "available_commands_update",+ "config_option_update",+ "current_mode_update",+ "session_info_update",+ "tool_call_update",+ "usage_update",+]);+/** `extNotification` puts the double-prefixed `__posthog/…` form on the wire. */function canonicalMethod(method: string): string {return method.startsWith("__posthog/") ? method.slice(1) : method;@@ -52,7 +64,11 @@ export class TranscriptBoundaries {*/private neutralCallIds = new Set<unknown>();- isNeutral(message: { id?: unknown; method?: unknown }): boolean {+ isNeutral(message: {+ id?: unknown;+ method?: unknown;+ params?: unknown;+ }): boolean {const isCall = message.id !== undefined;if (typeof message.method !== "string") {return isCall && this.neutralCallIds.delete(message.id);@@ -67,6 +83,13 @@ export class TranscriptBoundaries {}return isPosthogMethod;}+ if (method === "session/update") {+ const params = message.params as+ | { update?: { sessionUpdate?: unknown } }+ | null+ | undefined;+ return NEUTRAL_SESSION_UPDATES.has(params?.update?.sessionUpdate);+ }return (isPosthogMethod && !TRANSCRIPT_EVENT_NOTIFICATION_METHODS.has(method));diff --git a/packages/agent/packages/agent/src/session-log-writer.test.ts b/packages/agent/packages/agent/src/session-log-writer.test.tsindex 8792b1fe1ca1..570be0515de6 100644--- a/packages/agent/packages/agent/src/session-log-writer.test.ts+++ b/packages/agent/packages/agent/src/session-log-writer.test.ts@@ -438,6 +438,30 @@ describe("SessionLogWriter", () => {label: "an unrecognized server diagnostic",wireMessages: [{ method: "_posthog/some_new_diagnostic", params: {} }],},+ {+ label: "a session usage update",+ wireMessages: [+ {+ method: "session/update",+ params: { update: { sessionUpdate: "usage_update", used: 1 } },+ },+ ],+ },+ {+ label: "a progress update for an earlier tool call",+ wireMessages: [+ {+ method: "session/update",+ params: {+ update: {+ sessionUpdate: "tool_call_update",+ toolCallId: "tc0",+ status: "in_progress",+ },+ },+ },+ ],+ },])("keeps a streamed message whole across an interleaved $label",async ({ wireMessages }) => {@@ -456,6 +480,20 @@ describe("SessionLogWriter", () => {{ method: "_posthog/progress", params: { label: "Cloning" } },],},+ {+ label: "a thought chunk",+ wireMessages: [+ {+ method: "session/update",+ params: {+ update: {+ sessionUpdate: "agent_thought_chunk",+ content: { type: "text", text: "Checking" },+ },+ },+ },+ ],+ },{// A local session gets no `_posthog/turn_complete`, so this response is// the only signal that closes the last message of the turn.@@ -638,14 +676,14 @@ describe("SessionLogWriter", () => {// Now a non-chunk event arrives, triggering natural coalescinglogWriter.appendRawLine(sessionId,- makeSessionUpdate("usage_update", { used: 100 }),+ makeSessionUpdate("tool_call", { toolCallId: "tc1" }),);await logWriter.flush(sessionId);expect(mockAppendLog).toHaveBeenCalledTimes(1);const entries: StoredNotification[] = mockAppendLog.mock.calls[0][2];- expect(entries).toHaveLength(2); // coalesced agent_message + usage_update+ expect(entries).toHaveLength(2); // coalesced agent_message + tool_callconst coalesced = entries[0].notification;expect(coalesced.params?.update).toEqual({sessionUpdate: "agent_message",