From 16c8a188e007658a10be27aa127d37620e521577 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 25 Nov 2026 07:41:12 +0100 Subject: [PATCH] perf(timeline): read the chat outline's cursor from the head, not a tail page The prompt index fetched a one-row tail page only for its epoch and retirement count. A tool call updated after other rows makes that page cover every row in between, so the outline still decoded tool output. AgentTimeline.readPrompts returns the prompt rows with the head they belong to, in one timeline operation. --- .../server/src/server/agent/agent-manager.ts | 7 +++--- .../server/src/server/agent/timeline/index.ts | 1 + .../agent/timeline/internal/agent-timeline.ts | 14 ++++++++--- .../agent/timeline/sqlite-backend.test.ts | 23 +++++++++++++++++++ packages/server/src/server/session.ts | 8 ++----- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/packages/server/src/server/agent/agent-manager.ts b/packages/server/src/server/agent/agent-manager.ts index c0bec5fcc0..e024686148 100644 --- a/packages/server/src/server/agent/agent-manager.ts +++ b/packages/server/src/server/agent/agent-manager.ts @@ -97,6 +97,7 @@ import { type TimelineAppendResult, type TimelineCommit, type TimelineHead, + type TimelinePrompts, type TimelineEntry, type TimelineIngestResult, type TimelineRetirementCommit, @@ -1580,10 +1581,10 @@ export class AgentManager { return await this.timeline.readRows({ agentId: id }); } - /** The agent's active prompt rows, without reading its replies or tool output. */ - async getTimelinePromptRows(id: string): Promise { + /** The agent's active prompt rows and timeline head, without its replies or tool output. */ + async getTimelinePrompts(id: string): Promise { await this.requireHistory(id); - return await this.timeline.readPromptRows({ agentId: id }); + return await this.timeline.readPrompts({ agentId: id }); } /** The timeline head of a loaded agent, or null when this agent is not loaded. Never loads. */ diff --git a/packages/server/src/server/agent/timeline/index.ts b/packages/server/src/server/agent/timeline/index.ts index a0e9e8d888..a667edc0f7 100644 --- a/packages/server/src/server/agent/timeline/index.ts +++ b/packages/server/src/server/agent/timeline/index.ts @@ -14,6 +14,7 @@ export { type TimelineEntry, type TimelineHead, type TimelineImportResult, + type TimelinePrompts, type TimelineIngestResult, type TimelineReconcileResult, type TimelineRetirement, diff --git a/packages/server/src/server/agent/timeline/internal/agent-timeline.ts b/packages/server/src/server/agent/timeline/internal/agent-timeline.ts index dfdbc74570..cf38503799 100644 --- a/packages/server/src/server/agent/timeline/internal/agent-timeline.ts +++ b/packages/server/src/server/agent/timeline/internal/agent-timeline.ts @@ -36,6 +36,11 @@ export type { } from "./backend.js"; export type { TimelineRetirement } from "../../timeline-projection.js"; +export interface TimelinePrompts { + head: TimelineHead; + rows: ProjectedTimelineRow[]; +} + export interface TimelineCommit { row: AgentTimelineRow; epoch: string; @@ -394,9 +399,12 @@ export class AgentTimeline { return this.run(scope, () => this.backendFor(scope).readRows(scope)); } - /** The active user prompt rows, without reading replies or tool output. */ - readPromptRows(scope: TimelineScope): Promise { - return this.run(scope, () => this.backendFor(scope).promptRows(scope)); + /** The active user prompt rows and the head they belong to, without replies or tool output. */ + readPrompts(scope: TimelineScope): Promise { + return this.run(scope, async () => ({ + head: await this.requireHead(scope), + rows: await this.backendFor(scope).promptRows(scope), + })); } lastItem(scope: TimelineScope): Promise { diff --git a/packages/server/src/server/agent/timeline/sqlite-backend.test.ts b/packages/server/src/server/agent/timeline/sqlite-backend.test.ts index d0a0babece..e4986bdc24 100644 --- a/packages/server/src/server/agent/timeline/sqlite-backend.test.ts +++ b/packages/server/src/server/agent/timeline/sqlite-backend.test.ts @@ -472,4 +472,27 @@ describe("SQLite timeline reads", () => { { type: "assistant_message", text: chunks.join("") }, ]); }); + + it("reads the chat outline's prompts and cursor without a tool's interleaved rows", async () => { + const database = new CountingDatabase(openTestTimelineDatabase()); + const timeline = new AgentTimeline(new SqliteTimelineBackend(database)); + await timeline.create(scope, { epoch: "e" }); + // The tool row spans everything between its start and its update, so a one-row tail page + // covers all 200 tool rows in between. + const entries: TimelineEntry[] = [ + entry({ type: "user_message", text: "run it", messageId: "p1" }), + entry(tool("long", "running")), + ]; + for (let index = 0; index < 200; index++) entries.push(entry(tool(`t${index}`, "completed"))); + entries.push(entry(tool("long", "completed"))); + await timeline.append(scope, entries); + + database.rowsRead = 0; + const outline = await timeline.readPrompts(scope); + expect(outline.head).toMatchObject({ epoch: "e", retirementCount: 0 }); + expect(outline.rows.map((row) => row.item)).toEqual([entries[0]!.item]); + expect(database.rowsRead).toBeLessThan(5); + await timeline.close(); + database.close(); + }); }); diff --git a/packages/server/src/server/session.ts b/packages/server/src/server/session.ts index b6b18458e5..19867a87af 100644 --- a/packages/server/src/server/session.ts +++ b/packages/server/src/server/session.ts @@ -8915,13 +8915,9 @@ export class Session { ): Promise { try { await this.loadAgentHistoryPayload(msg.agentId); - const rows = await this.agentManager.getTimelinePromptRows(msg.agentId); - const timeline = await this.agentManager.fetchTimeline(msg.agentId, { - direction: "tail", - limit: 1, - }); + const { head, rows } = await this.agentManager.getTimelinePrompts(msg.agentId); const index = buildTimelinePromptIndex( - this.wireEpoch(timeline.epoch, timeline.retirementCount, source), + this.wireEpoch(head.epoch, head.retirementCount, source), rows, ); this.emitForSource(