: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): include a leading empty chunk in the merged message range (#125392)
Shy Alterc4bda12041ad
diff --git a/packages/agent/packages/agent/src/session-log-writer.test.ts b/packages/agent/packages/agent/src/session-log-writer.test.tsindex 570be0515de6..a40ec6e23790 100644--- a/packages/agent/packages/agent/src/session-log-writer.test.ts+++ b/packages/agent/packages/agent/src/session-log-writer.test.ts@@ -520,38 +520,72 @@ describe("SessionLogWriter", () => {]);});- it("stamps the coalesced entry with the covered chunk id range", async () => {- const sessionId = "s1";- logWriter.register(sessionId, { taskId: "t1", runId: sessionId });-- logWriter.appendRawLine(- sessionId,- makeSessionUpdate("agent_message_chunk", {- content: { type: "text", text: "Hello " },- }),+ it.each([+ ["without a leading empty chunk", false, [], "boot-3"],+ ["with a leading empty chunk", true, [], "boot-2"],+ [+ "with a control response after the empty chunk",+ true,+ [+ { jsonrpc: "2.0", id: 9, method: "_posthog/ping" },+ { jsonrpc: "2.0", id: 9, result: {} },+ ],+ "boot-2",+ ],+ [+ "when a turn ends after the empty chunk",+ true,+ [{ jsonrpc: "2.0", method: "_posthog/turn_complete", params: {} }],"boot-3",- );- logWriter.appendRawLine(- sessionId,- makeSessionUpdate("agent_message_chunk", {- content: { type: "text", text: "world" },- }),- "boot-4",- );- logWriter.appendRawLine(- sessionId,- makeSessionUpdate("tool_call", { toolCallId: "tc1" }),- "boot-5",- );+ ],+ ])(+ "stamps the coalesced entry with the covered chunk id range %s",+ async (_case, leadingEmpty, between, firstEventId) => {+ const sessionId = "s1";+ logWriter.register(sessionId, { taskId: "t1", runId: sessionId });- await logWriter.flush(sessionId);+ if (leadingEmpty) {+ logWriter.appendRawLine(+ sessionId,+ makeSessionUpdate("agent_message_chunk", {+ content: { type: "text", text: "" },+ }),+ "boot-2",+ );+ }+ for (const line of between) {+ logWriter.appendRawLine(sessionId, JSON.stringify(line));+ }+ logWriter.appendRawLine(+ sessionId,+ makeSessionUpdate("agent_message_chunk", {+ content: { type: "text", text: "Hello " },+ }),+ "boot-3",+ );+ logWriter.appendRawLine(+ sessionId,+ makeSessionUpdate("agent_message_chunk", {+ content: { type: "text", text: "world" },+ }),+ "boot-4",+ );+ logWriter.appendRawLine(+ sessionId,+ makeSessionUpdate("tool_call", { toolCallId: "tc1" }),+ "boot-5",+ );- const entries: StoredNotification[] = mockAppendLog.mock.calls[0][2];- expect(entries[0].first_event_id).toBe("boot-3");- expect(entries[0].event_id).toBe("boot-4");- expect(entries[1].event_id).toBe("boot-5");- expect(entries[1].first_event_id).toBeUndefined();- });+ await logWriter.flush(sessionId);++ const entries: StoredNotification[] = mockAppendLog.mock.calls[0][2];+ const [message, toolCall] = entries.filter((entry) => entry.event_id);+ expect(message.first_event_id).toBe(firstEventId);+ expect(message.event_id).toBe("boot-4");+ expect(toolCall.event_id).toBe("boot-5");+ expect(toolCall.first_event_id).toBeUndefined();+ },+ );it("tracks direct agent_message updates", async () => {const sessionId = "s1";diff --git a/packages/agent/packages/agent/src/session-log-writer.ts b/packages/agent/packages/agent/src/session-log-writer.tsindex 0d2f349db642..91669f4c0563 100644--- a/packages/agent/packages/agent/src/session-log-writer.ts+++ b/packages/agent/packages/agent/src/session-log-writer.ts@@ -71,6 +71,7 @@ interface SessionState {context: SessionContext;transcriptBoundaries: TranscriptBoundaries;chunkBuffer?: ChunkBuffer;+ leadingEmptyChunkEventId?: string;lastAgentMessage?: string;currentTurnMessages: string[];toolUpdateCache: Map<string, BufferedToolUpdate>;@@ -256,9 +257,10 @@ export class SessionLogWriter {session.chunkBuffer = {text,firstTimestamp: timestamp,- firstEventId: eventId,+ firstEventId: session.leadingEmptyChunkEventId ?? eventId,lastEventId: eventId,};+ session.leadingEmptyChunkEventId = undefined;} else {session.chunkBuffer.text += text;if (eventId) {@@ -267,6 +269,8 @@ export class SessionLogWriter {}} else if (session.chunkBuffer && eventId) {session.chunkBuffer.lastEventId = eventId;+ } else if (eventId && !session.leadingEmptyChunkEventId) {+ session.leadingEmptyChunkEventId = eventId;}// Don't emit chunk eventsreturn;@@ -275,13 +279,17 @@ export class SessionLogWriter {// Non-chunk event: flush any buffered chunks first.// If this is a direct agent_message AND there are buffered chunks,// the direct message supersedes the partial chunks+ const isNeutral = session.transcriptBoundaries.isNeutral(message);let supersededChunks: ChunkBuffer | undefined;if (this.isDirectAgentMessage(message) && session.chunkBuffer) {supersededChunks = session.chunkBuffer;session.chunkBuffer = undefined;- } else if (!session.transcriptBoundaries.isNeutral(message)) {+ } else if (!isNeutral) {this.emitCoalescedMessage(sessionId, session);}+ if (!isNeutral) {+ session.leadingEmptyChunkEventId = undefined;+ }message = redactSecrets(message) as Record<string, unknown>;const nonChunkAgentText = this.extractAgentMessageText(message);