Skip to content

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.

fix(core): cap subagent-tail reads at 1MB per tick, matching context-tail

enesc696ad9773b2
diff --git a/src/core/subagent-tail.test.ts b/src/core/subagent-tail.test.ts
index 30bc20788..8b1f01b90 100644
--- a/src/core/subagent-tail.test.ts
+++ b/src/core/subagent-tail.test.ts
@@ -2,7 +2,12 @@ import { describe, expect, it, vi } from 'vitest'
import fs from 'fs'
import os from 'os'
import path from 'path'
-import { createSubagentTail, formatSubagentChunk, splitCompleteLines } from './subagent-tail'
+import {
+ createSubagentTail,
+ formatSubagentChunk,
+ splitCompleteLines,
+ SUBAGENT_READ_CAP
+} from './subagent-tail'
const assistant = (text: string): string =>
JSON.stringify({ type: 'assistant', message: { content: [{ type: 'text', text }] } })
@@ -113,3 +118,34 @@ describe('createSubagentTail', () => {
expect(streamed(send)).toContain('unterminated final line')
})
})
+
+describe('subagent-tail read cap', () => {
+ it('reads at most SUBAGENT_READ_CAP bytes per tick and continues next tick', async () => {
+ expect(SUBAGENT_READ_CAP).toBe(1024 * 1024)
+ const { transcriptPath, subDir } = setup()
+ fs.writeFileSync(path.join(subDir, 'agent-1.meta.json'), JSON.stringify({ toolUseId: 'tu1' }))
+ // One assistant line ~2.5x the cap: it can only arrive whole if the tail keeps reading
+ // where the previous tick stopped.
+ const text = 'x'.repeat(Math.floor(SUBAGENT_READ_CAP * 2.5))
+ fs.writeFileSync(path.join(subDir, 'agent-1.jsonl'), assistant(text) + '\n')
+
+ const send = vi.fn()
+ const tail = createSubagentTail(send)
+ tail.track('tu1', transcriptPath)
+
+ // One tick in, the capped slice holds no newline, so there is no complete line to emit yet
+ // — an uncapped read would have swallowed the whole file here and emitted already.
+ await wait(700)
+ expect(send).not.toHaveBeenCalled()
+
+ // The remaining slices arrive on later ticks and the line lands intact.
+ await vi.waitFor(() => expect(send).toHaveBeenCalled(), { timeout: 5000, interval: 50 })
+ const out = streamed(send)
+ // Match by shape, not toBe: a multi-MB diff on failure is unreadable (and slow).
+ expect(out.length).toBe(text.length + 1)
+ expect(/^x+\n$/.test(out)).toBe(true)
+
+ tail.finish('tu1')
+ fs.rmSync(path.dirname(transcriptPath), { recursive: true, force: true })
+ })
+})
diff --git a/src/core/subagent-tail.ts b/src/core/subagent-tail.ts
index d3ae6ad3e..39020ac14 100644
--- a/src/core/subagent-tail.ts
+++ b/src/core/subagent-tail.ts
@@ -8,6 +8,12 @@
import fs from 'fs'
import path from 'path'
+// Per-tick read ceiling, the same discipline as context-tail's INITIAL_READ_CAP: without it the
+// first tick after track() (or any burst) allocates the entire delta in one Buffer. Unlike
+// context-tail, a capped read here loses nothing — the tail is offset-based, so the next 400ms
+// tick continues where this one stopped.
+export const SUBAGENT_READ_CAP = 1024 * 1024 // 1 MB
+
interface Tracked {
dir: string
file: string | null
@@ -175,14 +181,15 @@ export function createSubagentTail(
}
const size = (await fs.promises.stat(e.file)).size
if (size <= e.offset) return
- const buf = Buffer.alloc(size - e.offset)
+ const len = Math.min(size - e.offset, SUBAGENT_READ_CAP)
+ const buf = Buffer.alloc(len)
const fd = await fs.promises.open(e.file, 'r')
try {
- await fd.read(buf, 0, buf.length, e.offset)
+ await fd.read(buf, 0, len, e.offset)
} finally {
await fd.close()
}
- e.offset = size
+ e.offset += len
const data = e.carry?.length ? Buffer.concat([e.carry, buf]) : buf
const { text, carry } = splitCompleteLines(data)
e.carry = carry