Skip to content
Closed
2 changes: 1 addition & 1 deletion src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 12 additions & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/bounded-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading