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.
This commit is contained in:
Mohamed Boudra
2026-11-26 02:45:19 +01:00
parent 325a65acac
commit 16c8a188e0
5 changed files with 41 additions and 12 deletions
@@ -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<AgentTimelineRow[]> {
/** The agent's active prompt rows and timeline head, without its replies or tool output. */
async getTimelinePrompts(id: string): Promise<TimelinePrompts> {
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. */
@@ -14,6 +14,7 @@ export {
type TimelineEntry,
type TimelineHead,
type TimelineImportResult,
type TimelinePrompts,
type TimelineIngestResult,
type TimelineReconcileResult,
type TimelineRetirement,
@@ -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<ProjectedTimelineRow[]> {
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<TimelinePrompts> {
return this.run(scope, async () => ({
head: await this.requireHead(scope),
rows: await this.backendFor(scope).promptRows(scope),
}));
}
lastItem(scope: TimelineScope): Promise<AgentTimelineItem | null> {
@@ -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();
});
});
+2 -6
View File
@@ -8915,13 +8915,9 @@ export class Session {
): Promise<void> {
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(