diff --git a/src/persist.ts b/src/persist.ts index 5aa2e6f80f..b2a079b414 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -3736,7 +3736,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 870bc484da..795bd6df29 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"; @@ -1233,10 +1233,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 diff --git a/tests/bounded-storage.test.ts b/tests/bounded-storage.test.ts index 1271703edd..1f951796ad 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 = [ + { 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); + 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");