From 70bba5fec75af12f45eccdce60a26de2f67b3b2b Mon Sep 17 00:00:00 2001 From: Aldo Adirajasa Fathoni Date: Tue, 27 Oct 2026 19:35:32 +0700 Subject: [PATCH 1/3] fix(session): discharge raw lastMessages AST on request release to prevent OOM --- src/persist.ts | 2 +- src/session.ts | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/persist.ts b/src/persist.ts index 53e42ff1e3..f63dcab349 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -3215,7 +3215,7 @@ function persistTailTokens(): number { * PersistedSession.messages. Truncation keeps whole messages from the NEWEST * end; at least one message always survives (even if it alone exceeds the * budget — a handoff doc with an empty tail is useless). */ -function boundedFoldedSnapshot(session: Session): CoreMessage[] | undefined { +export function boundedFoldedSnapshot(session: Session): CoreMessage[] | undefined { const msgs = session.lastMessages; if (!msgs || msgs.length === 0) return undefined; const budget = persistTailTokens(); diff --git a/src/session.ts b/src/session.ts index 3caa7de069..e7ccacf4ae 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1,7 +1,7 @@ import { createInitialState, defaultConfig, highestUsedIndex, indexToRef, type CompressionState, type Config, type CoreMessage, type MessageContentStore, type NudgeDecision } from "acp-kernel"; import { createHash } from "node:crypto"; import { log as loggerLog } from "./logger.js"; -import { getStore, effectiveFileStorageMode } from "./persist.js"; +import { getStore, effectiveFileStorageMode, boundedFoldedSnapshot } from "./persist.js"; import { VERSION } from "./version.js"; import { maxSessions as knobMaxSessions, sessionWarmMax, sessionWarmMaxBytes, sessionResidentMaxBytes } from "./knobs.js"; import { configFileEpoch, loadConfigFile } from "./config.js"; @@ -1221,10 +1221,21 @@ export function acquireInFlight(session: Session): void { /** Release an in-use marker. Decrements; the session becomes evictable again. */ export function releaseInFlight(session: Session): void { if (session.inFlight > 0) session.inFlight--; + if (session.inFlight === 0 && session.lastMessages && session.lastMessagesFolded !== true) { + // Discharge raw request AST memory once the request cycle has finished + const folded = boundedFoldedSnapshot(session); + if (folded) { + session.lastMessages = folded; + session.lastMessagesFolded = true; + } + } residentWeights.delete(session); // Active sessions are pinned. Once a request finishes, discharge its // growth before admitting more historical sessions into the cache. while (residentBytes() > MAX_RESIDENT_BYTES && evictOldest()) { /* bounded cache */ } + if (typeof (globalThis as unknown as { gc?: () => void }).gc === "function") { + (globalThis as unknown as { gc: () => void }).gc(); + } } /** Total in-flight requests across all sessions (sum of per-session From 6364fd4650e0e669e473508040ed1d3ebc36d065 Mon Sep 17 00:00:00 2001 From: Aldo Adirajasa Fathoni Date: Tue, 27 Oct 2026 19:37:25 +0700 Subject: [PATCH 2/3] test(session): cover releaseInFlight folding of lastMessages (#3050) --- tests/bounded-storage.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/bounded-storage.test.ts b/tests/bounded-storage.test.ts index 1271703edd..c2c173caa6 100644 --- a/tests/bounded-storage.test.ts +++ b/tests/bounded-storage.test.ts @@ -116,6 +116,25 @@ test("already-persisted sessions are retained when saving newer mutations fails" assert.equal(session.metadata.newer, "unsaved work"); })); +test("releaseInFlight folds raw lastMessages to bounded view when request completes (#3050)", async () => fixture(async (_dir, store) => { + const session = getSession("ast-fold-test"); + acquireInFlight(session); + session.lastMessages = [ + { role: "user", text: "hello" }, + { role: "assistant", text: "world" }, + { role: "user", text: "more messages here" }, + ]; + session.lastMessagesFolded = false; + assert.equal(session.lastMessages.length, 3); + assert.equal(session.lastMessagesFolded, false); + + releaseInFlight(session); + + assert.equal(session.inFlight, 0); + assert.equal(session.lastMessagesFolded, true); + assert.ok(session.lastMessages && session.lastMessages.length > 0); +})); + test("byte budget pins active state, admits concurrent requests, and releases idle state to disk", async () => fixture(async (_dir, store) => { _resetSessionsForTest(256, 32000); const session = getSession("large-active"); From fd09ee7f101369c1c8ff5b18f14c5a1799a70ae5 Mon Sep 17 00:00:00 2001 From: Aldo Adirajasa Fathoni Date: Tue, 27 Oct 2026 20:14:35 +0700 Subject: [PATCH 3/3] test(session): fix CoreMessage shape in bounded-storage test --- tests/bounded-storage.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/bounded-storage.test.ts b/tests/bounded-storage.test.ts index c2c173caa6..1f951796ad 100644 --- a/tests/bounded-storage.test.ts +++ b/tests/bounded-storage.test.ts @@ -120,9 +120,9 @@ test("releaseInFlight folds raw lastMessages to bounded view when request comple const session = getSession("ast-fold-test"); acquireInFlight(session); session.lastMessages = [ - { role: "user", text: "hello" }, - { role: "assistant", text: "world" }, - { role: "user", text: "more messages here" }, + { id: "m1", role: "user", contentType: "text", text: "hello" }, + { id: "m2", role: "assistant", contentType: "text", text: "world" }, + { id: "m3", role: "user", contentType: "text", text: "more messages here" }, ]; session.lastMessagesFolded = false; assert.equal(session.lastMessages.length, 3);