A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix(coding-agent): bound large session load memory (#549)
Seth Karten213fc0d441ec
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.mdindex 51d3f183e1..b52274531c 100644--- a/packages/coding-agent/CHANGELOG.md+++ b/packages/coding-agent/CHANGELOG.md@@ -2,6 +2,7 @@## [Unreleased]+- Changed large daemon session loads to stream JSONL history and avoid retaining a second full-file copy in memory.- Changed subagent guidance to retain reusable children and delete completed direct children once they are no longer needed.- Changed top-level CLI help and documentation to expose autonomous mode, quality gates, and their limits.- Fixed compaction retaining runtime resources after an explicitly deleted subagent had a transient cleanup failure.diff --git a/packages/coding-agent/src/core/session-manager.ts b/packages/coding-agent/src/core/session-manager.tsindex d1436f3846..54215f3ea7 100644--- a/packages/coding-agent/src/core/session-manager.ts+++ b/packages/coding-agent/src/core/session-manager.ts@@ -5,7 +5,6 @@ import {appendFileSync,chmodSync,chownSync,- createReadStream,existsSync,mkdirSync,readdirSync,@@ -18,10 +17,9 @@ import {} from "fs";import { readdir, readFile, stat } from "fs/promises";import { basename, dirname, join, resolve } from "path";-import { createInterface } from "readline";import { v7 as uuidv7 } from "uuid";import { getAgentDir as getDefaultAgentDir, getSessionsDir } from "../config.js";-import { readFirstLineSync } from "../utils/file-lines.js";+import { readFirstLineSync, readLinesAsBuffers } from "../utils/file-lines.js";import { captureGitContext, type GitContext, gitContextsEqual } from "../utils/git.js";import {type BashExecutionMessage,@@ -36,6 +34,8 @@ export const CURRENT_SESSION_VERSION = 3;const SESSION_LIST_SEARCH_TEXT_MAX_CHARS = 64 * 1024;const SESSION_LIST_PARSE_MAX_LINE_CHARS = 1024 * 1024;const SESSION_LIST_LARGE_MESSAGE_PREVIEW_MAX_CHARS = 256;+const SESSION_STREAMING_LOAD_THRESHOLD_BYTES = 128 * 1024 * 1024;+const SESSION_ASYNC_PARSE_YIELD_BYTES = 4 * 1024 * 1024;// Entry types that can represent user intent (vs. daemon bookkeeping like// session_state/agent_status/git_state/child_usage_attributed). Used by@@ -603,21 +603,41 @@ export function getDefaultSessionDir(_cwd: string, agentDir: string = getDefault// Decode per line off a Buffer: toString("utf8") on a whole large file is far slower// (one giant UTF-16 string). Splitting on 0x0a is UTF-8-safe.+function appendEntryFromBuffer(entries: FileEntry[], buffer: Buffer, start = 0, end = buffer.length): void {+ if (end <= start) return;+ try {+ entries.push(JSON.parse(buffer.toString("utf8", start, end)) as FileEntry);+ } catch {+ // Skip malformed or blank lines.+ }+}+function parseEntriesFromBuffer(buffer: Buffer): FileEntry[] {const entries: FileEntry[] = [];- const len = buffer.length;let start = 0;- while (start < len) {+ while (start < buffer.length) {let end = buffer.indexOf(0x0a, start);- if (end === -1) end = len;- if (end > start) {- try {- entries.push(JSON.parse(buffer.toString("utf8", start, end)) as FileEntry);- } catch {- // skip malformed/blank lines- }- }+ if (end === -1) end = buffer.length;+ appendEntryFromBuffer(entries, buffer, start, end);+ start = end + 1;+ }+ return entries;+}++async function parseEntriesFromBufferAsync(buffer: Buffer): Promise<FileEntry[]> {+ const entries: FileEntry[] = [];+ let start = 0;+ let bytesSinceYield = 0;+ while (start < buffer.length) {+ let end = buffer.indexOf(0x0a, start);+ if (end === -1) end = buffer.length;+ appendEntryFromBuffer(entries, buffer, start, end);+ bytesSinceYield += end - start + 1;start = end + 1;+ if (bytesSinceYield >= SESSION_ASYNC_PARSE_YIELD_BYTES) {+ bytesSinceYield = 0;+ await new Promise<void>((resolve) => setImmediate(resolve));+ }}return entries;}@@ -639,29 +659,25 @@ export function loadEntriesFromFile(filePath: string): FileEntry[] {}// Async loader for the daemon: reads off the event loop and yields while parsing so a-// large load doesn't freeze other sessions. Identical output to loadEntriesFromFile.-export async function loadEntriesFromFileAsync(filePath: string): Promise<FileEntry[]> {+// large load doesn't freeze other sessions. Large files stream to avoid retaining both+// the full input Buffer and the parsed entry graph at the same time.+export async function loadEntriesFromFileAsync(+ filePath: string,+ options: { streamThresholdBytes?: number } = {},+): Promise<FileEntry[]> {if (!existsSync(filePath)) return [];- const buffer = await readFile(filePath);+ const streamThresholdBytes = options.streamThresholdBytes ?? SESSION_STREAMING_LOAD_THRESHOLD_BYTES;+ if ((await stat(filePath)).size < streamThresholdBytes) {+ return finalizeLoadedEntries(await parseEntriesFromBufferAsync(await readFile(filePath)));+ }+const entries: FileEntry[] = [];- const len = buffer.length;- // yield by bytes, not entry count: parse cost scales with bytes (handles a few huge entries too)- const YIELD_BYTES = 4 * 1024 * 1024;- let start = 0;- let lastYield = 0;- while (start < len) {- let end = buffer.indexOf(0x0a, start);- if (end === -1) end = len;- if (end > start) {- try {- entries.push(JSON.parse(buffer.toString("utf8", start, end)) as FileEntry);- } catch {- // Skip malformed/blank lines (JSON.parse rejects whitespace-only slices)- }- }- start = end + 1;- if (start - lastYield >= YIELD_BYTES) {- lastYield = start;+ let bytesSinceYield = 0;+ for await (const line of readLinesAsBuffers(filePath)) {+ appendEntryFromBuffer(entries, line);+ bytesSinceYield += line.length + 1;+ if (bytesSinceYield >= SESSION_ASYNC_PARSE_YIELD_BYTES) {+ bytesSinceYield = 0;await new Promise<void>((resolve) => setImmediate(resolve));}}@@ -920,8 +936,6 @@ export async function readSessionInfo(filePath: string): Promise<SessionInfo | nasync function scanSessionInfo(filePath: string, stats: Awaited<ReturnType<typeof stat>>): Promise<SessionInfo | null> {try {- const stream = createReadStream(filePath, { encoding: "utf8" });- const lines = createInterface({ input: stream, crlfDelay: Infinity });let header: SessionHeader | undefined;let messageCount = 0;let firstMessage = "";@@ -931,7 +945,8 @@ async function scanSessionInfo(filePath: string, stats: Awaited<ReturnType<typeolet agentStatus: AgentStatus | undefined;let lastActivityTime: number | undefined;- for await (const line of lines) {+ for await (const lineBuffer of readLinesAsBuffers(filePath)) {+ const line = lineBuffer.toString("utf8");if (!line.trim()) continue;// Large tool-result entries can be many MB. They do not carry thediff --git a/packages/coding-agent/src/utils/file-lines.ts b/packages/coding-agent/src/utils/file-lines.tsindex 3bd0c4fcb7..84f226512c 100644--- a/packages/coding-agent/src/utils/file-lines.ts+++ b/packages/coding-agent/src/utils/file-lines.ts@@ -1,4 +1,4 @@-import { closeSync, openSync, readSync } from "node:fs";+import { closeSync, createReadStream, openSync, readSync } from "node:fs";export function readFirstLineSync(filePath: string, maxBytes = 64 * 1024): string | undefined {const fd = openSync(filePath, "r");@@ -33,3 +33,38 @@ export function readFirstLineSync(filePath: string, maxBytes = 64 * 1024): strin}return Buffer.concat(chunks).toString("utf8").replace(/\r$/, "");}++export async function* readLinesAsBuffers(filePath: string): AsyncGenerator<Buffer> {+ const pendingParts: Buffer[] = [];+ let pendingBytes = 0;+ for await (const chunk of createReadStream(filePath)) {+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);+ let start = 0;+ while (start < buffer.length) {+ const end = buffer.indexOf(0x0a, start);+ if (end === -1) {+ const part = buffer.subarray(start);+ pendingParts.push(part);+ pendingBytes += part.length;+ break;+ }+ if (pendingParts.length > 0) {+ const part = buffer.subarray(start, end);+ pendingParts.push(part);+ const line = Buffer.concat(pendingParts, pendingBytes + part.length);+ pendingParts.length = 0;+ pendingBytes = 0;+ yield line;+ } else {+ yield buffer.subarray(start, end);+ }+ start = end + 1;+ }+ }+ if (pendingParts.length > 0) {+ const line = Buffer.concat(pendingParts, pendingBytes);+ pendingParts.length = 0;+ pendingBytes = 0;+ yield line;+ }+}diff --git a/packages/coding-agent/test/file-lines.test.ts b/packages/coding-agent/test/file-lines.test.tsnew file mode 100644index 0000000000..92421e9f99--- /dev/null+++ b/packages/coding-agent/test/file-lines.test.ts@@ -0,0 +1,61 @@+import { Readable } from "node:stream";+import { afterEach, describe, expect, it, vi } from "vitest";++const fsMocks = vi.hoisted(() => ({+ createReadStream: vi.fn(),+}));++vi.mock("node:fs", async (importOriginal) => {+ const actual = await importOriginal<typeof import("node:fs")>();+ return {+ ...actual,+ createReadStream: fsMocks.createReadStream,+ };+});++const { readLinesAsBuffers } = await import("../src/utils/file-lines.js");++afterEach(() => {+ fsMocks.createReadStream.mockReset();+ vi.restoreAllMocks();+});++describe("readLinesAsBuffers", () => {+ it("releases pending chunks before yielding a newline-terminated multi-chunk record", async () => {+ const chunkSize = 64 * 1024;+ const firstPart = Buffer.alloc(chunkSize, 0x61);+ const secondPart = Buffer.concat([Buffer.alloc(chunkSize, 0x62), Buffer.from("\ntail\n")]);+ fsMocks.createReadStream.mockReturnValue(Readable.from([firstPart, secondPart]));+ const concatSpy = vi.spyOn(Buffer, "concat");++ const lines = readLinesAsBuffers("/unused");+ const first = await lines.next();++ expect(first.done).toBe(false);+ expect(first.value?.toString("utf8")).toBe("a".repeat(chunkSize) + "b".repeat(chunkSize));+ expect(concatSpy).toHaveBeenCalledOnce();+ expect(concatSpy.mock.calls[0]?.[0]).toHaveLength(0);++ const second = await lines.next();+ expect(second.done).toBe(false);+ expect(second.value?.toString("utf8")).toBe("tail");+ expect((await lines.next()).done).toBe(true);+ });++ it("releases pending chunks before yielding an EOF-terminated multi-chunk record", async () => {+ const chunkSize = 64 * 1024;+ const firstPart = Buffer.alloc(chunkSize, 0x61);+ const secondPart = Buffer.alloc(chunkSize, 0x62);+ fsMocks.createReadStream.mockReturnValue(Readable.from([firstPart, secondPart]));+ const concatSpy = vi.spyOn(Buffer, "concat");++ const lines = readLinesAsBuffers("/unused");+ const line = await lines.next();++ expect(line.done).toBe(false);+ expect(line.value?.toString("utf8")).toBe("a".repeat(chunkSize) + "b".repeat(chunkSize));+ expect(concatSpy).toHaveBeenCalledOnce();+ expect(concatSpy.mock.calls[0]?.[0]).toHaveLength(0);+ expect((await lines.next()).done).toBe(true);+ });+});diff --git a/packages/coding-agent/test/session-manager/file-operations.test.ts b/packages/coding-agent/test/session-manager/file-operations.test.tsindex 16ef89e660..28331ca758 100644--- a/packages/coding-agent/test/session-manager/file-operations.test.ts+++ b/packages/coding-agent/test/session-manager/file-operations.test.ts@@ -1,8 +1,14 @@import { mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";import { tmpdir } from "os";import { join } from "path";-import { afterEach, beforeEach, describe, expect, it } from "vitest";-import { findMostRecentSession, loadEntriesFromFile, SessionManager } from "../../src/core/session-manager.js";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import {+ findMostRecentSession,+ loadEntriesFromFile,+ loadEntriesFromFileAsync,+ readSessionInfo,+ SessionManager,+} from "../../src/core/session-manager.js";describe("loadEntriesFromFile", () => {let tempDir: string;@@ -63,6 +69,91 @@ describe("loadEntriesFromFile", () => {const entries = loadEntriesFromFile(file);expect(entries).toHaveLength(2);});++ it("yields while parsing a multi-megabyte session below the streaming threshold", async () => {+ const file = join(tempDir, "buffered.jsonl");+ writeFileSync(+ file,+ [+ '{"type":"session","id":"abc","timestamp":"2025-01-01T00:00:00Z","cwd":"/tmp"}',+ JSON.stringify({+ type: "message",+ id: "1",+ parentId: null,+ timestamp: "2025-01-01T00:00:01Z",+ message: { role: "user", content: "x".repeat(5 * 1024 * 1024), timestamp: 1 },+ }),+ ].join("\n"),+ );+ const setImmediateSpy = vi.spyOn(globalThis, "setImmediate");+ try {+ const entries = await loadEntriesFromFileAsync(file, { streamThresholdBytes: Number.MAX_SAFE_INTEGER });+ expect(entries).toHaveLength(2);+ expect(setImmediateSpy).toHaveBeenCalled();+ } finally {+ setImmediateSpy.mockRestore();+ }+ });++ it("streams large sessions with the same parsing semantics as the Buffer loader", async () => {+ const file = join(tempDir, "streamed.jsonl");+ writeFileSync(+ file,+ '{"type":"session","id":"abc","timestamp":"2025-01-01T00:00:00Z","cwd":"/tmp"}\r\n' ++ "\r\n" ++ "not valid json\n" ++ '{"type":"message","id":"1","parentId":null,"timestamp":"2025-01-01T00:00:01Z","message":{"role":"user","content":"héllo 世界","timestamp":1}}',+ );++ const streamed = await loadEntriesFromFileAsync(file, { streamThresholdBytes: 0 });+ expect(streamed).toEqual(loadEntriesFromFile(file));+ });++ it("only treats LF bytes as JSONL record boundaries", async () => {+ const file = join(tempDir, "unicode-separators.jsonl");+ const content = "before\u2028middle\u2029after";+ writeFileSync(+ file,+ [+ '{"type":"session","id":"abc","timestamp":"2025-01-01T00:00:00Z","cwd":"/tmp"}',+ JSON.stringify({+ type: "message",+ id: "1",+ parentId: null,+ timestamp: "2025-01-01T00:00:01Z",+ message: { role: "user", content, timestamp: 1 },+ }),+ ].join("\n"),+ );++ const streamed = await loadEntriesFromFileAsync(file, { streamThresholdBytes: 0 });+ expect(streamed).toEqual(loadEntriesFromFile(file));+ expect(streamed[1]).toMatchObject({ type: "message", message: { content } });+ expect((await readSessionInfo(file))?.firstMessage).toBe(content);+ });++ it("streams a multi-megabyte JSONL record without losing following entries", async () => {+ const file = join(tempDir, "large-record.jsonl");+ const largeContent = "x".repeat(2 * 1024 * 1024);+ writeFileSync(+ file,+ [+ '{"type":"session","id":"abc","timestamp":"2025-01-01T00:00:00Z","cwd":"/tmp"}',+ JSON.stringify({+ type: "message",+ id: "1",+ parentId: null,+ timestamp: "2025-01-01T00:00:01Z",+ message: { role: "user", content: largeContent, timestamp: 1 },+ }),+ '{"type":"message","id":"2","parentId":"1","timestamp":"2025-01-01T00:00:02Z","message":{"role":"user","content":"after","timestamp":2}}',+ ].join("\n"),+ );++ const entries = await loadEntriesFromFileAsync(file, { streamThresholdBytes: 0 });+ expect(entries).toHaveLength(3);+ expect(entries[2]).toMatchObject({ type: "message", id: "2" });+ });});describe("findMostRecentSession", () => {