From 8c88b83cd8d835a51d64870bdded92a193532cdf Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:02:46 +0000 Subject: [PATCH] fix(server): preview the newest turn's reply in finished notifications (#9793) A turn the provider starts on its own, such as Claude waking after a background subagent, has no user message before its reply. The last assistant message lookup joined every adjacent assistant row, so it returned the previous turn's reply with the new one appended, and the 220-character notification preview showed only the old reply. Stop the backward scan at the turn boundary so the lookup returns only the newest turn's reply, while chunks of one turn are still joined. Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../src/server/agent/agent-manager.test.ts | 45 +++++++++++++++++++ .../server/src/server/agent/agent-manager.ts | 23 +++++++--- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/packages/server/src/server/agent/agent-manager.test.ts b/packages/server/src/server/agent/agent-manager.test.ts index 966b66f2e..4c592d538 100644 --- a/packages/server/src/server/agent/agent-manager.test.ts +++ b/packages/server/src/server/agent/agent-manager.test.ts @@ -13487,6 +13487,51 @@ test.each(["live", "history", "accepted"] as const)( }, ); +test("the last assistant message is the newest turn's reply, not joined to the previous turn's", async () => { + const session = new TestAgentSession({ provider: "codex", cwd: process.cwd() }); + const workdir = mkdtempSync(join(tmpdir(), "agent-manager-last-reply-")); + const client = new (class extends TestAgentClient { + override async createSession(): Promise { + return session; + } + })(); + const manager = new AgentManager({ clients: { codex: client }, logger }); + const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, { + workspaceId: undefined, + }); + const reply = (turnId: string, messageId: string, text: string) => + session.pushEvent({ + type: "timeline", + provider: "codex", + turnId, + item: { type: "assistant_message", messageId, text }, + }); + try { + session.pushEvent({ type: "turn_started", provider: "codex", turnId: "turn-1" }); + reply("turn-1", "message-1", "Earlier status, "); + reply("turn-1", "message-1", "streamed in two chunks."); + session.pushEvent({ type: "turn_completed", provider: "codex", turnId: "turn-1" }); + await vi.waitFor(async () => + expect(await manager.getLastAssistantMessage(agent.id)).toBe( + "Earlier status, streamed in two chunks.", + ), + ); + + // A turn the provider starts on its own, such as a background task waking the agent, + // has no user message between its reply and the previous turn's. + session.pushEvent({ type: "turn_started", provider: "codex", turnId: "turn-2" }); + reply("turn-2", "message-2", "New status."); + session.pushEvent({ type: "turn_completed", provider: "codex", turnId: "turn-2" }); + await vi.waitFor(() => + expect(manager.getTimeline(agent.id).at(-1)).toMatchObject({ messageId: "message-2" }), + ); + expect(await manager.getLastAssistantMessage(agent.id)).toBe("New status."); + } finally { + await manager.closeAgent(agent.id); + rmSync(workdir, { recursive: true, force: true }); + } +}); + test("reads the last durable assistant response without loading a stored-only child", async () => { const store = new RecordingTimelineStore(); await store.appendCommitted("stored-child", { diff --git a/packages/server/src/server/agent/agent-manager.ts b/packages/server/src/server/agent/agent-manager.ts index 34af30302..81d25be67 100644 --- a/packages/server/src/server/agent/agent-manager.ts +++ b/packages/server/src/server/agent/agent-manager.ts @@ -4049,23 +4049,33 @@ export class AgentManager { private getLastAssistantMessageFromTimeline( timeline: readonly AgentTimelineItem[], ): string | null { - return this.getLastAssistantMessageSegmentFromTimeline(timeline)?.text ?? null; + return ( + this.getLastAssistantMessageSegmentFromTimeline(timeline.map((item) => ({ item })))?.text ?? + null + ); } private getLastAssistantMessageSegmentFromTimeline( - timeline: readonly AgentTimelineItem[], + timeline: readonly Pick[], ): { text: string; startsAtBeginning: boolean } | null { - // Collect the last contiguous assistant messages (Claude streams chunks) + // Collect the last contiguous assistant messages of one turn (Claude streams chunks). + // A turn the provider starts on its own has no user message before its reply. const chunks: string[] = []; + let replyTurnId: string | undefined; let startsAtBeginning = false; for (let i = timeline.length - 1; i >= 0; i--) { - const item = timeline[i]; + const { item, turnId } = timeline[i]; if (item.type !== "assistant_message") { if (chunks.length) { break; } continue; } + if (chunks.length === 0) { + replyTurnId = turnId; + } else if (turnId !== replyTurnId) { + break; + } chunks.push(item.text); startsAtBeginning = i === 0; } @@ -4081,8 +4091,9 @@ export class AgentManager { } private async getLastAssistantMessageFromStores(agentId: string): Promise { - const liveTimeline = this.timelineStore.getItems(agentId); - const liveSegment = this.getLastAssistantMessageSegmentFromTimeline(liveTimeline); + const liveSegment = this.getLastAssistantMessageSegmentFromTimeline( + this.timelineStore.getRows(agentId), + ); if (!this.durableTimelineStore) { return liveSegment?.text ?? null; }