Agent pages: an older page of an agent's log is answered from a block that is read ahead of the reader
Jonas Templestein, 2026-11-28 16:57 UTC · da956c1777943cf54b2d1c580f4c4bb57fba64ca · parent bc5f047
3 files changed
- M src/agent-kind-view/chat-history.test.ts
- M src/agent-kind-view/chat-history.ts
- M src/agent-kind-view/dash-agents.tsx
M src/agent-kind-view/chat-history.test.ts
@@ -1,50 +1,87 @@ import { expect, test } from "vitest"; import { withChatHistory } from "./chat-history.ts"; -const page = (from: string) => ({ events: [from], atHead: false, scannedThroughOffset: 0 }); +/** A log with an event at every tenth offset. */ +const eventsIn = (after: number, through: number) => { + const events: { offset: number }[] = []; + for (let offset = Math.ceil((after + 1) / 10) * 10; offset <= through; offset += 10) + events.push({ offset }); + return events; +}; +const logOf = (asked: string[]) => ({ + other: () => "kept", + readEvents: async (after = 0, limit = 1) => { + asked.push(`plain ${after} ${limit}`); + const events = eventsIn(after, after + limit * 10); + return { events, atHead: false, scannedThroughOffset: events.at(-1)?.offset ?? after }; + }, +}); +const mindOf = (asked: string[]) => async (after: number, limit: number, through?: number) => { + asked.push(`chat ${after} ${through ?? `x${limit}`}`); + const events = + through === undefined ? eventsIn(after, after + limit * 10) : eventsIn(after, through); + return { + events, + atHead: false, + scannedThroughOffset: through ?? events.at(-1)?.offset ?? after, + version: 3, + }; +}; -test("the first page and what is over it are the context's own; the pages below come from the chat's read", async () => { +test("the first page is the context's own; older pages are answered from one block, and the next block is read ahead", async () => { const asked: string[] = []; - const stub = { - other: () => "kept", - readEvents: async (after?: number, limit?: number) => { - asked.push(`plain ${after} ${limit}`); - return page("plain"); - }, - }; - const held = withChatHistory(stub, async (after, limit) => { - asked.push(`chat ${after} ${limit}`); - return page("chat"); - }); + const held = withChatHistory(logOf(asked), mindOf(asked)); expect(held.other()).toBe("kept"); await held.readEvents(Number.MAX_SAFE_INTEGER, 1); // the head's probe - await held.readEvents(9000, 1000); // the first page - await held.readEvents(9400, 600); // on, over it - await held.readEvents(8000, 1000); // an older page - await held.readEvents(8000, 1); // a probe below - expect(asked).toEqual([ - "plain 9007199254740991 1", - "plain 9000 1000", - "plain 9400 600", - "chat 8000 1000", - "plain 8000 1", - ]); + await held.readEvents(50_000, 1000); // the first page + await held.readEvents(50_400, 600); // on, over it + expect(asked).toEqual(["plain 9007199254740991 1", "plain 50000 1000", "plain 50400 600"]); + asked.length = 0; + + // the first older window: a block of 8,000 offsets that ends where the window ends + const first = await held.readEvents(49_000, 1000); + expect(asked).toEqual(["chat 42000 50000"]); + expect(first.events).toHaveLength(100); + expect(first).toMatchObject({ scannedThroughOffset: 50_000, atHead: false, version: 3 }); + + // the next windows come from the block: no call + const second = await held.readEvents(47_000, 1000); + expect(second.events).toHaveLength(300); + expect(asked).toEqual(["chat 42000 50000"]); + + // half through the block: the block below is read ahead, once + const third = await held.readEvents(45_000, 250); + expect(third.events).toHaveLength(250); + expect(third.scannedThroughOffset).toBe(47_500); + await held.readEvents(43_000, 1000); + expect(asked).toEqual(["chat 42000 50000", "chat 34000 42000"]); + // below the first block, from what was read ahead + const fourth = await held.readEvents(40_000, 1000); + expect(fourth.events[0]).toEqual({ offset: 40_010 }); + expect(asked).toHaveLength(2); }); test("a mind with no such read: one refusal, then the context's own read", async () => { const asked: string[] = []; - const stub = { - readEvents: async (after?: number, _limit?: number) => { - asked.push(`plain ${after}`); - return page("plain"); - }, - }; - const held = withChatHistory(stub, async (after) => { + const held = withChatHistory(logOf(asked), async (after) => { asked.push(`chat ${after}`); throw new Error("no such method"); }); await held.readEvents(9000, 1000); - expect((await held.readEvents(8000, 1000)).events).toEqual(["plain"]); - await held.readEvents(7000, 1000); - expect(asked).toEqual(["plain 9000", "chat 8000", "plain 8000", "plain 7000"]); + await held.readEvents(8000, 100); + await held.readEvents(7000, 100); + expect(asked).toEqual(["plain 9000 1000", "chat 100", "plain 8000 100", "plain 7000 100"]); +}); + +test("an answer that is not the whole block: the window is read as a page", async () => { + const asked: string[] = []; + const held = withChatHistory(logOf(asked), async (after, limit, through) => { + asked.push(`chat ${after} ${through ?? `x${limit}`}`); + const events = eventsIn(after, after + 50); + return { events, atHead: false, scannedThroughOffset: after + 50 }; + }); + await held.readEvents(9000, 1000); + const page = await held.readEvents(8000, 100); + expect(asked).toEqual(["plain 9000 1000", "chat 100 8100", "chat 8000 x100"]); + expect(page.events).toHaveLength(5); });
M src/agent-kind-view/chat-history.ts
@@ -11,19 +11,94 @@ // for the pages BELOW the first one. The first page (the newest events) and every read at or over // it come from the context itself, whole, and so do the live events: what the agent does now is // never cut. A mind with no such read answers an error once, and the stub reads plainly from then. +// +// A BLOCK AHEAD OF THE READER. The mind answers one large read far faster than many small ones +// (measured 28 Nov 2026 on a large chat: about 100 ms a call, then 45 ms for each 1,000 offsets, +// and calls at once are answered one after the other). The log's own reader asks for small +// windows, 1,000 offsets and then 2,000, so on a chat whose rows are few for its offsets a reader +// on a steady way up stood at the list's top for 15 s of 25. So an older page is answered from a +// block that this stub holds: `BLOCK` offsets read in one call (`through`), and the next block +// below is read as soon as the reader is half through the one held. One block ahead, never more: +// nothing is read for a reader who asked for no older page. -type Page = { events: unknown[]; atHead: boolean; scannedThroughOffset: number }; +type Page = { + events: unknown[]; + atHead: boolean; + scannedThroughOffset: number; + version?: number; + versionStartedAtOffset?: number; +}; type Reads = { readEvents(after?: number, limit?: number): Promise<Page> }; +/** The mind's read: a page after `after`, or with `through` every event in (after, through]. */ +export type ChatHistoryRead = (after: number, limit: number, through?: number) => Promise<Page>; -export function withChatHistory<Stub extends object>( - stub: Stub, - read: (after: number, limit: number) => Promise<Page>, -): Stub { +/** How many offsets one block holds. */ +const BLOCK = 8_000; +/** The events above the reader that the stub keeps: a read never asks for them again. */ +const KEPT_ABOVE = 24_000; + +const offsetOf = (event: unknown) => (event as { offset: number }).offset; + +export function withChatHistory<Stub extends object>(stub: Stub, read: ChatHistoryRead): Stub { // where the first page began: a read from below it is an older page let firstPage: number | undefined; let refused = false; + // the mind answered once: a later error is the connection's, and only that read is plain + let answered = false; + // the block held: every event that the chat reads in (lo, hi], ascending + let block: { lo: number; hi: number; events: unknown[]; of: Page } | undefined; + let ahead: Promise<void> | undefined; const plain = (after?: number, limit?: number) => (stub as unknown as Reads).readEvents(after, limit); + + /** Every event in (lo, hi], or nothing when the mind's answer is not the whole span. */ + const span = async (lo: number, hi: number) => { + const page = await read(lo, 1, hi); + if (page.scannedThroughOffset < hi) return undefined; + return { lo, hi, events: page.events.filter((event) => offsetOf(event) <= hi), of: page }; + }; + /** The block below the one held, joined to it. */ + const below = async () => { + const held = block; + if (!held || held.lo <= 0) return; + const more = await span(Math.max(0, held.lo - BLOCK), held.lo); + if (!more || block !== held) return; + block = { lo: more.lo, hi: held.hi, events: [...more.events, ...held.events], of: held.of }; + }; + const older = async (after: number, limit: number): Promise<Page> => { + await ahead?.catch(() => undefined); + // over the block: a window's rest, which the log's reader asks for after a full answer + if (block && after >= block.hi) return read(after, limit); + if (!block || after + limit < block.lo) { + // the first older page, or a jump far below: a block that ends where this window ends + const fresh = await span(Math.max(0, after + limit - BLOCK), after + limit); + if (!fresh || after < fresh.lo) return read(after, limit); + block = fresh; + } else if (after < block.lo) { + await below(); + if (after < block.lo) return read(after, limit); + } + const held = block; + const from = held.events.findIndex((event) => offsetOf(event) > after); + const events = from < 0 ? [] : held.events.slice(from, from + limit); + const all = from < 0 || from + limit >= held.events.length; + // the reader is half through the block: read the next one now, for the ask that comes + if (after - held.lo < BLOCK / 2 && held.lo > 0 && !ahead) + ahead = below().finally(() => { + ahead = undefined; + }); + // what stands far over the reader is never asked for again + if (held.hi - after > KEPT_ABOVE) { + const hi = after + KEPT_ABOVE; + block = { ...held, hi, events: held.events.filter((event) => offsetOf(event) <= hi) }; + } + return { + ...held.of, + events, + atHead: false, + scannedThroughOffset: all ? held.hi : offsetOf(events.at(-1)), + }; + }; const readEvents = (after?: number, limit?: number): Promise<Page> => { // a probe of one row (the head, a gap) is no page if (refused || after === undefined || limit === undefined || limit <= 1) @@ -32,10 +107,17 @@ firstPage ??= after; return plain(after, limit); } - return read(after, limit).catch(() => { - refused = true; - return plain(after, limit); - }); + return older(after, limit).then( + (page) => { + answered = true; + return page; + }, + () => { + if (!answered) refused = true; + block = undefined; + return plain(after, limit); + }, + ); }; return new Proxy(stub, { get: (target, key) => (key === "readEvents" ? readEvents : Reflect.get(target, key)),
M src/agent-kind-view/dash-agents.tsx
@@ -1044,8 +1044,8 @@ const logStub = useMemo( () => agent.stub && - withChatHistory(agent.stub, (after, limit) => - (itx as unknown as ChatHistoryItx).config.chatHistory({ path, after, limit }), + withChatHistory(agent.stub, (after, limit, through) => + (itx as unknown as ChatHistoryItx).config.chatHistory({ path, after, limit, through }), ), [agent.stub, itx, path], ); @@ -1174,6 +1174,7 @@ path: string; after: number; limit: number; + through?: number; }): Promise<{ events: unknown[]; atHead: boolean; scannedThroughOffset: number }>; }; };