Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: tail-read raw.txt, re-schedule flushes, clear postPasteBuffer in finish

Adam Eivy7d925bb9839c
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index 7e58f1187f..818f1f9232 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -8,7 +8,7 @@
import { join } from 'path';
import { existsSync } from 'fs';
-import { appendFile, readFile, rm } from 'fs/promises';
+import { appendFile, readFile, rm, open, stat as fsStat } from 'fs/promises';
import * as shellService from './shell.js';
import { emitLog } from './cosEvents.js';
import { appendAgentOutputLines, updateAgent } from './cosAgents.js';
@@ -38,6 +38,35 @@ import {
// Agent-specific timing/lifecycle constants (not shared with the one-shot
// runner — agents stay alive much longer and write a sentinel file when done).
const DEFAULT_TUI_MIN_RUNTIME_MS = 15000;
+// Tail-read window for raw.txt at failure analysis. analyzeAgentFailure only
+// inspects the last ~200 lines, so reading the whole file (which has no upper
+// bound for long-running agents) would reintroduce the OOM risk the disk
+// spool was meant to avoid. 1MB easily contains the last 200 lines of any
+// realistic PTY stream while keeping peak finalize memory bounded.
+const RAW_TAIL_ANALYSIS_BYTES = 1024 * 1024;
+
+/**
+ * Read at most `maxBytes` from the end of a file. Returns null when the file
+ * doesn't exist or can't be opened; an empty string for a zero-byte file.
+ * Used to bound the memory footprint of failure-analysis reads against the
+ * uncapped raw PTY spool.
+ */
+async function readFileTail(path, maxBytes) {
+ const st = await fsStat(path).catch(() => null);
+ if (!st) return null;
+ if (st.size === 0) return '';
+ const start = Math.max(0, st.size - maxBytes);
+ const length = st.size - start;
+ const fh = await open(path, 'r').catch(() => null);
+ if (!fh) return null;
+ try {
+ const buf = Buffer.alloc(length);
+ await fh.read(buf, 0, length, start);
+ return buf.toString('utf8');
+ } finally {
+ await fh.close().catch(() => {});
+ }
+}
// Debounce window for batching parsed output to disk + state. A chatty TUI can
// emit hundreds of lines/sec; without batching, each line triggers a full
// state load+save (see appendAgentOutput) and a small appendFile, which slows
@@ -182,7 +211,13 @@ export async function spawnTuiAgent({
if (flushTimer || flushing) return;
flushTimer = setTimeout(() => {
flushTimer = null;
- flushing = flushPendingLines().finally(() => { flushing = null; });
+ flushing = flushPendingLines().finally(() => {
+ flushing = null;
+ // Catch chunks that arrived during the in-flight flush — without
+ // this, a producer that goes quiet right after the flush starts
+ // strands its last batch in pendingLines until finalize.
+ if (pendingLines.length > 0) scheduleFlush();
+ });
}, OUTPUT_FLUSH_INTERVAL_MS);
};
@@ -201,7 +236,13 @@ export async function spawnTuiAgent({
if (rawFlushTimer || rawFlushing) return;
rawFlushTimer = setTimeout(() => {
rawFlushTimer = null;
- rawFlushing = flushPendingRawChunks().finally(() => { rawFlushing = null; });
+ rawFlushing = flushPendingRawChunks().finally(() => {
+ rawFlushing = null;
+ // Same re-schedule guard as scheduleFlush: chunks that arrived
+ // during the in-flight appendFile would otherwise sit until
+ // finalize if the producer goes quiet immediately after.
+ if (pendingRawChunks.length > 0) scheduleRawFlush();
+ });
}, OUTPUT_FLUSH_INTERVAL_MS);
};
@@ -236,6 +277,11 @@ export async function spawnTuiAgent({
if (agentData?.promptTimer) clearInterval(agentData.promptTimer);
if (agentData?.doneSentinelTimer) clearInterval(agentData.doneSentinelTimer);
if (pasteEnterTimer) { clearInterval(pasteEnterTimer); pasteEnterTimer = null; }
+ // Release the post-paste accumulator even when finalize fires mid-paste-
+ // window. The pasteEnterTimer's own cleanup path nulls this too, but if
+ // finalize comes from elsewhere (shell-exit, command-not-found, user
+ // termination) the timer never gets a chance to run.
+ postPasteBuffer = null;
// Drain pending parsed lines AND raw chunks before the final state
// writes so completion events don't beat the last output batch to disk.
@@ -270,13 +316,15 @@ export async function spawnTuiAgent({
// capped at OUTPUT_BUFFER_CAP and would silently truncate the on-disk
// record for long runs. The append-only stream is the authoritative copy.
//
- // For failure analysis: read the full raw PTY stream from raw.txt rather
- // than holding it in memory. Successful runs skip the read entirely.
- // raw.txt stays in agentDir alongside output.txt as the persistent record
- // of the agent's full PTY transcript.
+ // For failure analysis: read only the tail of the raw PTY spool — the
+ // analyzer's window is the last ~200 lines, so reading the full file
+ // (potentially many MB on long agents) would defeat the disk-spool's
+ // memory-bound guarantee. Successful runs skip the read entirely.
+ // raw.txt stays in agentDir alongside output.txt as the persistent
+ // record of the agent's full PTY transcript.
const rawAnalysisText = finalSuccess
? null
- : await readFile(rawFile, 'utf8').catch(() => null);
+ : await readFileTail(rawFile, RAW_TAIL_ANALYSIS_BYTES);
const errorAnalysis = finalSuccess
? null
: analyzeAgentFailure(rawAnalysisText || outputBuffer, task, model);
diff --git a/server/services/agentTuiSpawning.test.js b/server/services/agentTuiSpawning.test.js
index f76637c3d3..6d4971331e 100644
--- a/server/services/agentTuiSpawning.test.js
+++ b/server/services/agentTuiSpawning.test.js
@@ -72,7 +72,16 @@ vi.mock('fs/promises', () => ({
writeFile: vi.fn().mockResolvedValue(undefined),
appendFile: vi.fn().mockResolvedValue(undefined),
readFile: vi.fn().mockResolvedValue(''),
- rm: vi.fn().mockResolvedValue(undefined)
+ rm: vi.fn().mockResolvedValue(undefined),
+ // raw.txt tail-read for failure analysis. Tests don't exercise the failure
+ // path's analyzer (analyzeAgentFailure is mocked to return null), so a
+ // simple stub that reports an empty file is sufficient. stat returns
+ // size:0 so readFileTail short-circuits to '' without touching open/read.
+ stat: vi.fn().mockResolvedValue({ size: 0 }),
+ open: vi.fn().mockResolvedValue({
+ read: vi.fn().mockResolvedValue({ bytesRead: 0 }),
+ close: vi.fn().mockResolvedValue(undefined),
+ }),
}));
vi.mock('../lib/fileUtils.js', () => ({
@@ -433,7 +442,7 @@ describe('spawnTuiAgent runtime', () => {
// memory stays bounded regardless of run length. analyzeAgentFailure
// reads the file on failure. No warn, no metadata flag, even when the
// chunk total dwarfs the previous 640KB in-memory cap.
- it('rawBuffer grows without truncation warn or metadata flag, spools to raw.txt', async () => {
+ it('raw PTY bytes spool to raw.txt without truncation warn or metadata flag', async () => {
const { appendFile } = await import('fs/promises');
runSpawn();
await flushMicrotasks();