Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(chat): grow a paged window instead of skipping a line bigger than it
review500a80fb1586
diff --git a/CLAUDE.md b/CLAUDE.mdindex 995bd4574..f77778fc8 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -2386,9 +2386,14 @@ command-bearing opens; this does not add a human-confirm dialog or change mobileread first, so the renderer holds them until that page arrives). Parsing is the pure`parseChatWindow` over BYTES (a multi-byte char cut by the window edge only lands in the droppedpartial line), and every window is read with **one byte of lookbehind** — the only way to know a- line begins exactly on the edge; without it that line is dropped as "partial" and lost. A line- longer than the whole window is skipped (`olderCursor = window start`, never the window end,- which would re-request the identical window forever). The **SSH leg** is a ranged read+ line begins exactly on the edge; without it that line is dropped as "partial" and lost. A window+ with **no complete line** (one record bigger than the window — in practice a `type:user` line+ carrying a pasted screenshot or an image tool_result; 181 lines over 512 KB in 30 days on one+ host) is flagged `noCompleteLine` and **re-read at the same `before` with ×4 the bytes, up to the 5 MB cap**+ (`parseGrowingWindow` in `core/transcript-ipc.ts` — local AND SSH leg; a failed re-read is+ not-found, never the skip). Only a line **longer than 5 MB** is skipped (`olderCursor = window+ start`, never the window end, which would re-request the identical window forever) — before this,+ every line bigger than the page vanished, a regression against the legacy 5 MB read. The **SSH leg** is a ranged read(`transcriptPageCommand`, `core/remote-ssh/transcript-window.ts` — size + window in ONE roundtrip, dd status inside the base64 like the context-tail's window command) instead of pulling the5 MB tail on every open and every turn-end reload; its `{ok:false}` is terminal (never the localdiff --git a/src/core/remote-ssh/transcript-page.realsh.test.ts b/src/core/remote-ssh/transcript-page.realsh.test.tsindex 0d1c299b6..c7a0b14a3 100644--- a/src/core/remote-ssh/transcript-page.realsh.test.ts+++ b/src/core/remote-ssh/transcript-page.realsh.test.ts@@ -10,6 +10,11 @@ import os from 'os'import path from 'path'import { parseTranscriptPage, transcriptPageCommand } from './transcript-window'import { parseChatWindow } from '../transcript-reader'+import { fakePlatform } from '../platform-fake'+import { initPlatform, resetPlatformForTests } from '../platform'+import { registerTranscriptIpc } from '../transcript-ipc'+import { IPC } from '../../shared/ipc'+import type { ChatTranscriptResult } from '../../shared/types'let dir: stringbeforeAll(() => {@@ -96,3 +101,35 @@ describe('transcriptPageCommand under /bin/sh', () => {expect(fs.existsSync(marker)).toBe(false)})})++// The whole remote leg of a paged read — core's `readChatPage` driving the REAL generated command —+// on the case that used to lose data: a final line bigger than the page (a pasted screenshot). The+// tail window holds no complete line, and core must grow it over ssh rather than skip the record.+describe('paged chat read over a real /bin/sh — a final line bigger than the window', () => {+ afterAll(() => resetPlatformForTests())++ it('the 300 KB last record is in the first (tail) page', async () => {+ const small = JSON.stringify({ type: 'user', message: { content: 'look at this' } })+ const big = JSON.stringify({ type: 'user', message: { content: `IMG ${'Q'.repeat(300 * 1024)}` } })+ const p = write('screenshot.jsonl', `${small}\n${big}\n`)+ const f = fakePlatform()+ initPlatform(f)+ const asked: number[] = []+ registerTranscriptIpc({+ readRemotePage: async (_q, pg) => {+ asked.push(pg.maxBytes)+ const w = page(p, pg.before, pg.maxBytes)+ return { ok: true, data: w.data, start: w.start }+ }+ })+ const res = (await f.handlers[IPC.chatReadTranscript](+ '46b36ce2-dd77-4f5e-a89e-4a0e831e83df', '/srv', undefined, 'nt-1', undefined, { maxBytes: 65536 }+ )) as ChatTranscriptResult+ expect(res.found).toBe(true)+ const texts = res.messages.map((m) => (m.parts[0] as { text: string }).text)+ expect(texts.at(-1)?.startsWith('IMG QQQ')).toBe(true)+ expect(texts.at(-1)?.length).toBe(4 + 300 * 1024)+ expect(asked[0]).toBe(65536)+ expect(asked.length).toBeGreaterThan(1)+ })+})diff --git a/src/core/transcript-ipc.test.ts b/src/core/transcript-ipc.test.tsindex 12f8ee6d8..9936439b8 100644--- a/src/core/transcript-ipc.test.ts+++ b/src/core/transcript-ipc.test.ts@@ -327,6 +327,39 @@ describe('registerTranscriptIpc — paged chat reads', () => {expect(new Set(all.map((m) => m.key)).size).toBe(1000)})+ // A pasted screenshot is a `type:user` line carrying base64 image data, routinely far bigger than+ // one page (measured on a real host: 378 lines > 256 KB in 30 days, 133 of the > 512 KB ones+ // image-bearing user records). A window with no complete line in it must GROW, not skip it.+ it('pages back THROUGH a line bigger than the window — the big record is shown, not skipped', async () => {+ const big = userLine(`screenshot ${'i'.repeat(600 * 1024)}`)+ writeTranscript(lines(userLine('before'), big, assistantLine('after 1'), assistantLine('after 2')))+ registerTranscriptIpc()+ const seen: string[] = []+ let res = await paged({ maxBytes: 65536 })+ for (let guard = 0; guard < 50; guard++) {+ seen.unshift(...res.messages.map((m) => (m.parts[0] as { text: string }).text.slice(0, 10)))+ if (res.olderCursor === null || res.olderCursor === undefined) break+ res = await paged({ before: res.olderCursor, maxBytes: 65536 })+ }+ expect(seen).toEqual(['before', 'screenshot', 'after 1', 'after 2'])+ // The internal flag never leaks onto the wire.+ expect(res).not.toHaveProperty('noCompleteLine')+ })++ it('a line bigger than the 5 MB cap is the ONLY thing ever skipped, and paging still ends', async () => {+ const huge = userLine(`h${'z'.repeat(5 * 1024 * 1024 + 10)}`)+ writeTranscript(lines(userLine('oldest'), huge, assistantLine('newest')))+ registerTranscriptIpc()+ const seen: string[] = []+ let res = await paged({ maxBytes: 65536 })+ for (let guard = 0; guard < 50; guard++) {+ seen.unshift(...res.messages.map((m) => (m.parts[0] as { text: string }).text))+ if (res.olderCursor === null || res.olderCursor === undefined) break+ res = await paged({ before: res.olderCursor, maxBytes: 65536 })+ }+ expect(seen).toEqual(['oldest', 'newest'])+ })+it('an unresolvable transcript is still not-found, with no cursor to follow', async () => {registerTranscriptIpc()expect(await paged({})).toEqual({ messages: [], found: false, olderCursor: null, unmatchedResults: [] })@@ -389,6 +422,43 @@ describe('registerTranscriptIpc — paged chat reads', () => {})})+ it('grows a window that holds no complete line by re-asking the HOST for the same end', async () => {+ const body = Buffer.from(lines(userLine('small'), userLine(`img ${'b'.repeat(300 * 1024)}`)))+ const asked: Array<{ before: number | null; maxBytes: number }> = []+ registerTranscriptIpc({+ readRemotePage: async (_q, page) => {+ asked.push(page)+ const end = page.before === null ? body.length : page.before+ const ws = Math.max(0, end - page.maxBytes)+ const start = ws > 0 ? ws - 1 : 0+ return { ok: true, data: body.subarray(start, end), start }+ }+ })+ const res = await paged({ maxBytes: 65536 }, 'nt-1')+ expect(asked.map((a) => a.maxBytes)).toEqual([65536, 262144, 1048576])+ expect(asked.every((a) => a.before === null)).toBe(true)+ expect(res.messages.map((m) => (m.parts[0] as { text: string }).text.slice(0, 5))).toEqual(['small', 'img b'])+ expect(res.olderCursor).toBeNull()+ })++ it('a failed host read while growing is not-found — never the skipped-line answer', async () => {+ const body = Buffer.from(lines(userLine(`img ${'b'.repeat(300 * 1024)}`)))+ let n = 0+ registerTranscriptIpc({+ readRemotePage: async (_q, page) => {+ if (n++ > 0) return { ok: false }+ const start = body.length - page.maxBytes - 1+ return { ok: true, data: body.subarray(start), start }+ }+ })+ expect(await paged({ maxBytes: 65536 }, 'nt-1')).toEqual({+ messages: [],+ found: false,+ olderCursor: null,+ unmatchedResults: []+ })+ })+it('null means "not a remote session" — the local path pages', async () => {writeTranscript(lines(assistantLine('local')))registerTranscriptIpc({ readRemotePage: async () => null })diff --git a/src/core/transcript-ipc.ts b/src/core/transcript-ipc.tsindex 93403235d..0a5c5d3ee 100644--- a/src/core/transcript-ipc.ts+++ b/src/core/transcript-ipc.ts@@ -12,7 +12,7 @@import fsp from 'node:fs/promises'import { IPC } from '../shared/ipc'import type { ChatTranscriptResult, TranscriptLine, TranscriptPresence } from '../shared/types'-import { normalizeChatPage, type ChatTranscriptPage } from '../shared/chat-page'+import { CHAT_PAGE_MAX_BYTES, normalizeChatPage, type ChatTranscriptPage } from '../shared/chat-page'import { platform } from './platform'import { chatMessagesFromGrok } from './grok-chat'import { locateGrok } from './handoff/locate'@@ -69,6 +69,46 @@ export interface TranscriptIpcDeps {export type RemoteTranscriptPage = { ok: true; data: Buffer; start: number } | { ok: false }+/** How much a window with no complete line grows per re-read. ×4 reaches the 5 MB cap from the+ * default 256 KB tail in three extra reads, and from the 64 KB minimum in four. */+const CHAT_PAGE_GROWTH = 4++/** A window read: its bytes from absolute offset `start` (lookbehind byte included), or `null` when+ * the read failed — which ends the whole page as not-found. */+type WindowRead = (page: ChatTranscriptPage) => Promise<{ data: Buffer; start: number } | null>++/**+ * Parse the window `first` (already read for `page`), GROWING it while it holds no complete line.+ *+ * A record bigger than the window — in practice a `type:user` line carrying a pasted screenshot or+ * an image tool_result, measured at 181 lines above 512 KB in 30 days on one host — used to leave+ * the window empty, and the cursor then pointed INTO that record, so the next older read ended+ * mid-line, failed to parse and the record vanished: screenshot prompts gone, tools missing their+ * results. The legacy 5 MB read showed them. So the same `before` is re-read with a bigger window+ * (×`CHAT_PAGE_GROWTH`) up to `CHAT_PAGE_MAX_BYTES`, the legacy cap — no paged read ever costs more+ * than an unpaged one did, and only a line longer than 5 MB (which the legacy read could not show+ * either) is skipped. Shared by the local and the remote leg, so both page identically.+ *+ * A failed re-read is NOT answered with the skip: dropping a record because the host blinked is the+ * very bug this exists to fix. It is a failed read, and the caller's retry path handles it.+ */+async function parseGrowingWindow(+ page: ChatTranscriptPage,+ first: { data: Buffer; start: number },+ read: WindowRead+): Promise<ChatTranscriptResult> {+ let w = first+ let maxBytes = page.maxBytes+ for (;;) {+ const { noCompleteLine, ...parsed } = parseChatWindow(w.data, w.start)+ if (!noCompleteLine || w.start === 0 || maxBytes >= CHAT_PAGE_MAX_BYTES) return { found: true, ...parsed }+ maxBytes = Math.min(CHAT_PAGE_MAX_BYTES, maxBytes * CHAT_PAGE_GROWTH)+ const next = await read({ before: page.before, maxBytes })+ if (!next) return notFoundPage()+ w = next+ }+}+const notFoundPage = (): ChatTranscriptResult => ({messages: [],found: false,@@ -82,26 +122,27 @@ const notFoundPage = (): ChatTranscriptResult => ({** Remote first, as in the legacy path, and a remote failure is TERMINAL: a remote session's* transcript lives on the host, so falling through to the local resolver would answer from the- * wrong machine. A shell that injects only the legacy `readRemote` (none does today) still gets a- * correct — unpaged — answer from it rather than a local one.+ * wrong machine. The remote leg is `readRemotePage` ONLY: a shell that can read a remote transcript+ * must inject it (the desktop does), because paging with only the legacy `readRemote` would read a+ * remote session's LOCAL namesake. (A `readRemote`-only fallback lived here with no caller; it was+ * removed rather than kept untested.)*/async function readChatPage(q: TranscriptQuery,page: ChatTranscriptPage,deps: TranscriptIpcDeps): Promise<ChatTranscriptResult> {- if (deps.readRemotePage) {- const remote = await deps.readRemotePage(q, page)+ const readRemotePage = deps.readRemotePage+ if (readRemotePage) {+ const remote = await readRemotePage(q, page)if (remote !== null) {if (!remote.ok) return notFoundPage()- return { found: true, ...parseChatWindow(remote.data, remote.start) }- }- } else if (deps.readRemote) {- const text = await deps.readRemote(q)- if (text !== null) {- return text- ? { messages: parseChatMessages(text.split('\n')), found: true, olderCursor: null, unmatchedResults: [] }- : notFoundPage()+ // A growth re-read that suddenly says "not remote" (null) is a failed read too — never a+ // reason to go read THIS machine's disk halfway through a remote page.+ return parseGrowingWindow(page, remote, async (p) => {+ const r = await readRemotePage(q, p)+ return r && r.ok ? r : null+ })}}const p = await resolveTranscript(q, deps.pathFor)@@ -109,7 +150,7 @@ async function readChatPage(const w = await readChatWindow(p, page)// Resolved but unreadable (deleted between resolve and read): not-found, like a failed remote.if (!w) return notFoundPage()- return { found: true, ...parseChatWindow(w.data, w.start) }+ return parseGrowingWindow(page, w, async (pg) => (await readChatWindow(p, pg)) ?? null)}/**diff --git a/src/core/transcript-reader.test.ts b/src/core/transcript-reader.test.tsindex ef66677a9..3d8d2e588 100644--- a/src/core/transcript-reader.test.ts+++ b/src/core/transcript-reader.test.ts@@ -178,6 +178,19 @@ describe('parseChatWindow — pure window parsing', () => {const r = parseChatWindow(file.subarray(start), start)expect(r.messages).toEqual([])expect(r.olderCursor).toBe(start) // strictly older than the window end → paging progresses+ // …and SAYS so, explicitly: the reader grows the window on this flag instead of skipping the+ // line (a pasted screenshot's user record is routinely bigger than a whole page).+ expect(r.noCompleteLine).toBe(true)+ })++ it('noCompleteLine is false whenever the window holds a complete line, or starts at 0', () => {+ const l1 = said('user', 'x'.repeat(50))+ const l2 = said('assistant', 'kept')+ const file = Buffer.from(l1 + l2)+ expect(parseChatWindow(file.subarray(10), 10).noCompleteLine).toBe(false)+ expect(parseChatWindow(file, 0).noCompleteLine).toBe(false)+ // A window that is ONE line from the very start of the file is complete, not oversized.+ expect(parseChatWindow(Buffer.from(l1), 0).noCompleteLine).toBe(false)})it('pages stitched together equal the unpaged parse, with no line lost or duplicated', () => {diff --git a/src/core/transcript-reader.ts b/src/core/transcript-reader.tsindex 374a8713d..b18d64ce8 100644--- a/src/core/transcript-reader.ts+++ b/src/core/transcript-reader.ts@@ -218,6 +218,14 @@ export interface ChatWindowParse {messages: ChatMessage[]olderCursor: number | nullunmatchedResults: ChatCarriedToolResult[]+ /**+ * The window (not starting at 0) held no complete line: one record is bigger than the whole+ * window. Explicit rather than inferred from "no messages", because a window of complete lines+ * can legitimately yield no messages (metadata-only records). The reader (`readChatPage`) GROWS+ * the window on this flag — a pasted screenshot is routinely bigger than a page — and only past+ * the 5 MB cap takes the `olderCursor` below and skips the record. Never sent over the wire.+ */+ noCompleteLine: boolean}/**@@ -237,9 +245,10 @@ export interface ChatWindowParse {* multi-byte character cut by the window edge only ever lands in the dropped partial line (each* kept line is decoded on its own, from newline to newline).*- * A line longer than the whole window leaves no complete line in it. `olderCursor` is then- * `bufStart` — strictly older than the window end — so paging keeps moving and that one oversized- * record is skipped. Answering the window end instead would ask for the identical window forever.+ * A line longer than the whole window leaves no complete line in it: `noCompleteLine` is set, and+ * `olderCursor` is `bufStart` — strictly older than the window end, so a caller that gives up+ * (`readChatPage`, only once the window is already at the 5 MB cap) keeps paging and skips that one+ * record. Answering the window end instead would ask for the identical window forever.*/export function parseChatWindow(buf: Buffer, bufStart: number): ChatWindowParse {const end = bufStart + buf.length@@ -248,7 +257,7 @@ export function parseChatWindow(buf: Buffer, bufStart: number): ChatWindowParseif (bufStart > 0) {const nl = buf.indexOf(0x0a)if (nl < 0 || bufStart + nl + 1 >= end) {- return { messages: [], olderCursor: bufStart, unmatchedResults: [] }+ return { messages: [], olderCursor: bufStart, unmatchedResults: [], noCompleteLine: true }}from = nl + 1olderCursor = bufStart + from@@ -264,7 +273,8 @@ export function parseChatWindow(buf: Buffer, bufStart: number): ChatWindowParsereturn {messages,olderCursor,- unmatchedResults: [...unmatched].map(([id, result]) => ({ id, result }))+ unmatchedResults: [...unmatched].map(([id, result]) => ({ id, result })),+ noCompleteLine: false}}