Agent pages: the older pages of an agent's log are read without the raw prompts, where the mind offers that read
Jonas Templestein, 2026-11-28 16:15 UTC · f35f6928d602a894597c01fd35aae29035bd34a3 · parent 6b9b113
3 files changed
- A src/agent-kind-view/chat-history.test.ts
- A src/agent-kind-view/chat-history.ts
- M src/agent-kind-view/dash-agents.tsx
A src/agent-kind-view/chat-history.test.ts
@@ -0,0 +1,50 @@ +import { expect, test } from "vitest"; +import { withChatHistory } from "./chat-history.ts"; + +const page = (from: string) => ({ events: [from], atHead: false, scannedThroughOffset: 0 }); + +test("the first page and what is over it are the context's own; the pages below come from the chat's read", 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"); + }); + 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", + ]); +}); + +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) => { + 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"]); +});
A src/agent-kind-view/chat-history.ts
@@ -0,0 +1,43 @@ +// chat-history.ts — THE OLDER PAGES OF AN AGENT'S LOG, READ FOR THE CHAT WITHOUT THE RAW PROMPTS. +// +// Jonas, 28 Nov 2026, in the Dash: "proactively load more events in the past in agent and context +// views on dash - atm too many loading breaks when scrolling up". A page of an agent's log is +// mostly a few facts that the chat draws nothing of (a request's instructions, the provider's +// body): megabytes for each screen of rows, and a page a second. A mind may offer a read that +// cuts those texts short, `itx.config.chatHistory({ path, after, limit })`, which answers what +// the context's `readEvents` answers (the config repo's chat-history.ts). +// +// `withChatHistory(stub, read)` is the context's stub with that read in the place of `readEvents` +// 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. + +type Page = { events: unknown[]; atHead: boolean; scannedThroughOffset: number }; +type Reads = { readEvents(after?: number, limit?: number): Promise<Page> }; + +export function withChatHistory<Stub extends object>( + stub: Stub, + read: (after: number, limit: number) => Promise<Page>, +): Stub { + // where the first page began: a read from below it is an older page + let firstPage: number | undefined; + let refused = false; + const plain = (after?: number, limit?: number) => + (stub as unknown as Reads).readEvents(after, limit); + 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) + return plain(after, limit); + if (firstPage === undefined || after >= firstPage) { + firstPage ??= after; + return plain(after, limit); + } + return read(after, limit).catch(() => { + refused = true; + 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
@@ -19,6 +19,7 @@ // and reads the kinds once for the page's life: which kind has which file (the mind's KV, // `agent-kinds`, which worker.ts writes at every publish), and each file's source. import { useAtWorkBelow } from "./below.ts"; +import { withChatHistory } from "./chat-history.ts"; import { useWriters, type WritersItx } from "./writers.ts"; import { createContext, @@ -1039,8 +1040,17 @@ // load, until then. The saved rows are in `inputs` from the first render, and the link sends // the latest inputs when the frame says it is ready (`AgentKindViewLink.push`): the frame // draws them with its first message. + // the pages below the first one without the raw prompts, where the mind offers that read + const logStub = useMemo( + () => + agent.stub && + withChatHistory(agent.stub, (after, limit) => + (itx as unknown as ChatHistoryItx).config.chatHistory({ path, after, limit }), + ), + [agent.stub, itx, path], + ); const log = useSavedContext({ - held: { stub: loaded ? agent.stub : undefined, error: agent.error }, + held: { stub: loaded ? logStub : undefined, error: agent.error }, options, path, session, @@ -1156,6 +1166,17 @@ hasChat: false, }; } + +/** A mind's read of a context's older pages for the chat (chat-history.ts). */ +type ChatHistoryItx = { + config: { + chatHistory(input: { + path: string; + after: number; + limit: number; + }): Promise<{ events: unknown[]; atHead: boolean; scannedThroughOffset: number }>; + }; +}; /** How many events one `readEvents` of a kind's frame answers at most. */ const EVENTS_BY_OFFSET_MOST = 8;