Multiplayer agent harness for work.
Page transcripts by turns and keep model-only context out of the view (#1687)
Regan Bell4d4e260b7d04
diff --git a/plugins/web-ui/src/transcript-viewport.ts b/plugins/web-ui/src/transcript-viewport.tsindex a68f49b17..cb24c46cb 100644--- a/plugins/web-ui/src/transcript-viewport.ts+++ b/plugins/web-ui/src/transcript-viewport.ts@@ -27,6 +27,7 @@ export function preserveTranscriptScroll(root: HTMLElement): () => void {}const CONDENSED_LINES = 2;+const NEAR_TOP = 400;export function createTranscriptViewport() {let scroller: HTMLElement | null = null;@@ -49,6 +50,7 @@ export function createTranscriptViewport() {let contentMax = "";let collapseDistance = 0;let geometryDistance: number | null = null;+ let filledAtSize: number | null = null;const contentUpdates = new Set<Promise<void>>();function setFollowing(value: boolean): void {@@ -212,14 +214,28 @@ export function createTranscriptViewport() {if (movingUp) loadEarlier();}+ function earlierButton(): HTMLButtonElement | null {+ return scroller?.querySelector<HTMLButtonElement>(".earlier-messages-btn:not(:disabled)") ?? null;+ }+function loadEarlier(): void {- if (!scroller || scroller.scrollTop > 400) return;- const button = scroller.querySelector<HTMLButtonElement>(".earlier-messages-btn:not(:disabled)");+ if (!scroller || scroller.scrollTop > NEAR_TOP) return;+ const button = earlierButton();if (!button) return;cancelFollow();button.click();}+ function fillViewport(): void {+ const button = earlierButton();+ if (!button || !scroller || scroller.clientHeight === 0) return;+ if (scroller.scrollHeight - scroller.clientHeight > NEAR_TOP) return;+ const size = (stack ?? scroller).getElementsByTagName("*").length;+ if (size === filledAtSize) return;+ filledAtSize = size;+ button.click();+ }+function beforeRender(): void {if (!scroller || contentUpdates.size > 0) return;if (scroller.scrollTop !== lastTop) onScroll();@@ -271,6 +287,7 @@ export function createTranscriptViewport() {scroller = pins = prompt = stack = content = null;lastTop = 0;following = false;+ filledAtSize = null;contentUpdates.clear();}@@ -293,6 +310,7 @@ export function createTranscriptViewport() {beforeRender();syncSticky();follow();+ fillViewport();});if (scroller) observer.observe(scroller);}@@ -300,6 +318,7 @@ export function createTranscriptViewport() {const nextStack = scroller?.querySelector<HTMLElement>(".message-stack") ?? null;if (stack !== nextStack) {changed = true;+ filledAtSize = null;if (stack) observer?.unobserve(stack);stack = nextStack;if (stack) observer?.observe(stack);@@ -331,6 +350,7 @@ export function createTranscriptViewport() {if (content) observer?.observe(content);}if (changed) syncSticky();+ fillViewport();}function follow(force = false): void {diff --git a/plugins/web-ui/test/transcript-viewport.test.ts b/plugins/web-ui/test/transcript-viewport.test.tsindex 096988155..c09040a89 100644--- a/plugins/web-ui/test/transcript-viewport.test.ts+++ b/plugins/web-ui/test/transcript-viewport.test.ts@@ -227,6 +227,83 @@ test("upward wheel loads history even when the transcript cannot scroll", () =>}});+test("a transcript too short to fill the viewport keeps loading earlier pages until it does", () => {+ const f = fixture();+ try {+ f.fit();+ const button = f.s.ownerDocument.createElement("button");+ button.className = "earlier-messages-btn";+ const stack = f.s.querySelector<HTMLElement>(".message-stack")!;+ stack.prepend(button);+ const page = () => stack.append(f.s.ownerDocument.createElement("article"));+ let loads = 0;+ button.onclick = () => {+ loads++;+ button.disabled = true;+ };+ f.viewport.sync(f.s);+ assert.equal(loads, 1, "no scroll or wheel is needed when there is nothing to scroll");+ f.viewport.sync(f.s);+ assert.equal(loads, 1, "a page already loading is not requested twice");+ page();+ button.disabled = false;+ f.resize(30, 50);+ assert.equal(loads, 2, "still short after a page arrives, so the next page loads");+ for (let i = 0; i < 6; i++) f.grow();+ page();+ button.disabled = false;+ f.resize(30, 50);+ f.viewport.sync(f.s);+ assert.equal(loads, 2, "once the content overflows well past the top, loading waits for the reader");+ button.remove();+ } finally {+ f.close();+ }+});++test("a page that fails to load is not retried by auto-fill until the reader asks", () => {+ const f = fixture();+ try {+ f.fit();+ const button = f.s.ownerDocument.createElement("button");+ button.className = "earlier-messages-btn";+ f.s.querySelector(".message-stack")!.prepend(button);+ let loads = 0;+ button.onclick = () => loads++;+ f.viewport.sync(f.s);+ f.resize(30, 50);+ f.viewport.sync(f.s);+ assert.equal(loads, 1, "no growth since the last automatic load means no automatic retry");+ f.wheelUp();+ assert.equal(loads, 2, "the reader can still ask for it");+ } finally {+ f.close();+ }+});++test("a hidden pane never pulls history just because it has no height", () => {+ const dom = new JSDOM('<section class="chat-scroll"><div class="message-stack"></div></section>');+ const s = dom.window.document.querySelector<HTMLElement>("section")!;+ Object.defineProperties(s, { clientHeight: { value: 0 }, scrollHeight: { value: 0 } });+ const button = dom.window.document.createElement("button");+ button.className = "earlier-messages-btn";+ s.querySelector(".message-stack")!.prepend(button);+ let loads = 0;+ button.onclick = () => loads++;+ const viewport = createTranscriptViewport();+ const saved = Object.getOwnPropertyDescriptor(globalThis, "getComputedStyle");+ Object.assign(globalThis, { getComputedStyle: dom.window.getComputedStyle.bind(dom.window) });+ try {+ viewport.sync(s);+ assert.equal(loads, 0);+ } finally {+ viewport.dispose();+ if (saved) Object.defineProperty(globalThis, "getComputedStyle", saved);+ else Reflect.deleteProperty(globalThis, "getComputedStyle");+ dom.window.close();+ }+});+test("a bottom-pinned stream follows growth instantly and coalesces frames", () => {const f = fixture();try {diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.tsindex c65f9de0b..0ddcd0e34 100644--- a/src/sessions/session-store.ts+++ b/src/sessions/session-store.ts@@ -582,12 +582,15 @@ export function transcriptEntries(entries: readonly SessionEntry[]): SessionEntrreturn entries.filter((e) => e.type !== "soul");}-export const TRANSCRIPT_BYTE_BUDGET = 400_000;export const ENTRY_STRING_BUDGET = 2_000;export type TranscriptEntry = SessionEntry & { truncated?: true };const PROJECTED_TYPES: ReadonlySet<EntryType> = new Set<EntryType>(["tool_call", "tool_result"]);+const MODEL_ONLY_FIELDS: Partial<Record<EntryType, ReadonlySet<string>>> = {+ user: new Set(["environment", "memoryRecall"]),+ thinking: new Set(["thinkingSignature"]),+};const WALK_DEPTH = 8;function shortenStrings(value: unknown, depth: number): { value: unknown; truncated: boolean } {@@ -625,7 +628,16 @@ function postsToTheConversation(entry: SessionEntry): boolean {return entry.type === "tool_call" && p?.action === "post";}+function withoutModelContext(entry: SessionEntry, fields: ReadonlySet<string>): TranscriptEntry {+ const payload = entry.payload;+ if (!payload || typeof payload !== "object" || !Object.keys(payload).some((k) => fields.has(k))) return entry;+ const visible = Object.fromEntries(Object.entries(payload).filter(([k]) => !fields.has(k)));+ return { ...entry, payload: visible, truncated: true };+}+function projectEntry(entry: SessionEntry): TranscriptEntry {+ const modelOnly = MODEL_ONLY_FIELDS[entry.type];+ if (modelOnly) return withoutModelContext(entry, modelOnly);if (!PROJECTED_TYPES.has(entry.type) || postsToTheConversation(entry)) return entry;const walked = shortenStrings(entry.payload, 0);return walked.truncated ? { ...entry, payload: walked.value, truncated: true } : entry;@@ -672,21 +684,7 @@ export function windowedTranscript(}}}- const windowed = (cut > 0 ? entries.slice(cut) : entries).map(projectEntry);- if (window === undefined || window.sinceSeq !== undefined) return { entries: windowed, earlier: cut };- let spend = 0;- let from = windowed.length;- while (from > 0) {- const bytes = payloadBytes(windowed[from - 1]!.payload, 0);- if (spend + bytes > TRANSCRIPT_BYTE_BUDGET && from < windowed.length) break;- spend += bytes;- from--;- }- if (from > 0) {- const boundary = windowed.findIndex((e, i) => i >= from && e.type === "user");- if (boundary > 0) from = boundary;- }- return { entries: from > 0 ? windowed.slice(from) : windowed, earlier: cut + from };+ return { entries: (cut > 0 ? entries.slice(cut) : entries).map(projectEntry), earlier: cut };}export function isOverheardEntry(e: Pick<SessionEntry, "type" | "payload">): boolean {diff --git a/test/session-fork.test.ts b/test/session-fork.test.tsindex 4e5e2cd52..e9afd0e11 100644--- a/test/session-fork.test.ts+++ b/test/session-fork.test.ts@@ -32,9 +32,13 @@ test("forking copies the transcript into a fresh independent web session", asyncassert.ok(fork.session.threadRef.startsWith("web:U1:"), "fork lives on a fresh web thread");assert.notEqual(fork.session.threadRef, source.session.threadRef);assert.equal(fork.session.scopeId, source.session.scopeId);+ const stored = await Promise.all(+ source.entries.map(async (e) => (await app.getSessionEntryForViewer(sid, "U1", e.seq))!.entry),+ );assert.deepEqual(fork.entries.map((e) => [e.type, e.payload]),- source.entries.map((e) => [e.type, e.payload]),+ stored.map((e) => [e.type, e.payload]),+ "the fork copies whole stored entries, model context included, not the trimmed view",);assert.deepEqual(fork.session.forkedFrom, { sessionId: sid, title: source.session.title ?? null });assert.equal(fork.session.forkBoundarySeq, fork.entries.at(-1)?.seq);diff --git a/test/transcript-window.test.ts b/test/transcript-window.test.tsindex 0274d2d28..603e91ba5 100644--- a/test/transcript-window.test.ts+++ b/test/transcript-window.test.ts@@ -8,7 +8,7 @@ import { join } from "node:path";import type { AddressInfo } from "node:net";import { createInsecureTestServer } from "../src/api/server.ts";import { buildApp, type BuiltApp } from "../src/wiring.ts";-import { ENTRY_STRING_BUDGET, TRANSCRIPT_BYTE_BUDGET, windowedTranscript } from "../src/sessions/session-store.ts";+import { ENTRY_STRING_BUDGET, windowedTranscript } from "../src/sessions/session-store.ts";import type { SessionEntry } from "../src/types.ts";import { scopeId } from "../src/types.ts";import { testConfig } from "./support/test-config.ts";@@ -151,23 +151,6 @@ test("windowedTranscript: conversation text is never truncated — only tool pay}});-test("windowedTranscript: the byte budget drops the oldest entries and counts them as earlier", () => {- const log = [entry(0, "user")];- for (let seq = 1; seq <= 400; seq++) log.push(fat(seq, "tool_result", ENTRY_STRING_BUDGET));- const w = windowedTranscript(log, { tailTurns: 99 });- const shipped = w.entries.reduce((a, e) => a + JSON.stringify(e.payload).length, 0);- assert.ok(shipped <= TRANSCRIPT_BYTE_BUDGET * 1.1, `shipped ${shipped} must respect the budget`);- assert.ok(w.entries.length < log.length, "the budget must bite");- assert.equal(w.entries.length + w.earlier, log.length, "everything dropped is counted as earlier");- assert.equal(w.entries[w.entries.length - 1]!.seq, 400, "the newest end is what survives");-});--test("windowedTranscript: one entry over budget still ships — a window is never empty", () => {- const w = windowedTranscript([fat(0, "user", TRANSCRIPT_BYTE_BUDGET * 2)], { tailTurns: 1 });- assert.equal(w.entries.length, 1);- assert.equal(w.earlier, 0);-});-function start(): { base: string; built: BuiltApp; close: () => Promise<void> } {const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "transcript-window-")) }));const server = createInsecureTestServer(built.app, {@@ -238,7 +221,16 @@ test("GET /v1/sessions/:id honors tailTurns/sinceSeq and reports earlierEntries"const one = await fetch(`${srv.base}/v1/sessions/${encodeURIComponent(sessionId)}/entries/${seq}?viewer=U1`);assert.equal(one.status, 200);const oneBody = (await one.json()) as { entry: SessionEntry };- assert.deepEqual(oneBody.entry, fullBody.entries[0], "the whole entry, exactly as stored");+ const { truncated, ...shown } = fullBody.entries[0] as SessionEntry & { truncated?: true };+ assert.equal(truncated, true, "the view marks a user entry whose model-only context it left out");+ assert.equal((shown.payload as { environment?: unknown }).environment, undefined);+ assert.equal((shown.payload as { memoryRecall?: unknown }).memoryRecall, undefined);+ assert.equal(typeof (oneBody.entry.payload as { environment?: unknown }).environment, "string");+ assert.deepEqual(+ { ...oneBody.entry, payload: { ...(oneBody.entry.payload as object), environment: undefined } },+ { ...shown, payload: { ...(shown.payload as object), environment: undefined } },+ "the whole entry, exactly as stored, is one fetch away",+ );const missing = await fetch(`${srv.base}/v1/sessions/${encodeURIComponent(sessionId)}/entries/99999?viewer=U1`);assert.equal(missing.status, 404, "a seq that isn't in this session");@@ -279,7 +271,7 @@ test("windowedTranscript: the text a post tool call puts in the conversation isassert.equal(posted.truncated, undefined);});-test("windowedTranscript: a deeply nested payload cannot smuggle bytes past the budget", () => {+test("windowedTranscript: a deeply nested payload cannot smuggle bytes past the preview", () => {const deep = (depth: number, leaf: unknown): unknown => (depth === 0 ? leaf : { nest: deep(depth - 1, leaf) });const log = [entry(0, "user")];for (let seq = 1; seq <= 6; seq++) {@@ -287,22 +279,10 @@ test("windowedTranscript: a deeply nested payload cannot smuggle bytes past the}const w = windowedTranscript(log, { tailTurns: 1 });const shipped = JSON.stringify(w.entries).length;- assert.ok(shipped < TRANSCRIPT_BYTE_BUDGET * 2, `a nested subtree must be charged its real size, shipped ${shipped}`);-});--test("windowedTranscript: the byte cut lands on a turn boundary, so a call keeps its result", () => {- const log: SessionEntry[] = [];- let seq = 0;- const said = (n: number): SessionEntry => ({ ...entry(n, "assistant"), payload: { text: "s".repeat(20_000) } });- for (let turn = 0; turn < 40; turn++) {- log.push(entry(seq++, "user"));- log.push(fat(seq++, "tool_call", ENTRY_STRING_BUDGET * 2));- log.push(fat(seq++, "tool_result", ENTRY_STRING_BUDGET * 2));- log.push(said(seq++));- }- const w = windowedTranscript(log, { tailTurns: 99 });- assert.ok(w.earlier > 0, "the budget must bite for this to mean anything");- assert.equal(w.entries[0]!.type, "user", "a page opens on a turn, never mid-turn");+ assert.ok(+ shipped < 7 * 2 * ENTRY_STRING_BUDGET,+ `a nested subtree must be charged its real size, shipped ${shipped}`,+ );});test("windowedTranscript: a sinceSeq re-read is never trimmed — it refreshes what the client already holds", () => {@@ -313,17 +293,47 @@ test("windowedTranscript: a sinceSeq re-read is never trimmed — it refreshes wassert.equal(w.earlier, 1);});-test("a turn larger than the byte budget leaves a window with nothing the person said", () => {- const big = "x".repeat(ENTRY_STRING_BUDGET);- const log: SessionEntry[] = [entry(0, "user")];- for (let seq = 1; seq <= 2 * Math.ceil(TRANSCRIPT_BYTE_BUDGET / ENTRY_STRING_BUDGET); seq++) {- log.push({ ...entry(seq, seq % 2 ? "tool_call" : "tool_result"), payload: { text: big } });+test("a user entry carrying huge model-only context no longer hides the turns before it", () => {+ const context = "c".repeat(300_000);+ const log: SessionEntry[] = [];+ for (let turn = 0; turn < 4; turn++) {+ log.push({+ ...entry(turn * 2, "user"),+ payload: { text: `ask ${turn}`, environment: context, memoryRecall: { body: context } },+ });+ log.push(entry(turn * 2 + 1, "assistant"));}const w = windowedTranscript(log, { tailTurns: 25 });- assert.equal(- w.entries.some((e) => e.type === "user"),- false,- "the byte budget trims past the only user entry and the forward snap finds no other",+ assert.equal(w.earlier, 0, "every turn is shipped; nothing is pushed behind the earlier button");+ assert.deepEqual(+ w.entries.map((e) => e.seq),+ log.map((e) => e.seq),);- assert.ok(w.earlier > 0, "the dropped entries are still reported as earlier");+ for (const user of w.entries.filter((e) => e.type === "user")) {+ assert.deepEqual(Object.keys(user.payload as object), ["text"], "model-only context stays out of the view");+ assert.equal(user.truncated, true, "the full entry is one lazy fetch away");+ }+ assert.ok(JSON.stringify(w.entries).length < 10_000);+});++test("thinking entries keep their text but not the provider's signature", () => {+ const log = [+ entry(0, "user"),+ { ...entry(1, "thinking"), payload: { thinking: "weighing it", thinkingSignature: "s".repeat(50_000) } },+ ];+ const thought = windowedTranscript(log, { tailTurns: 1 }).entries[1]!;+ assert.deepEqual(thought.payload, { thinking: "weighing it" });+ assert.equal(thought.truncated, true);+});++test("paging is by turns alone, however large the turns are", () => {+ const log: SessionEntry[] = [];+ const said = (n: number): SessionEntry => ({ ...entry(n, "assistant"), payload: { text: "s".repeat(50_000) } });+ for (let turn = 0; turn < 40; turn++) {+ log.push(entry(turn * 2, "user"));+ log.push(said(turn * 2 + 1));+ }+ const w = windowedTranscript(log, { tailTurns: 25 });+ assert.equal(w.entries.filter((e) => e.type === "user").length, 25);+ assert.equal(w.earlier, 30);});