Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

[RSI, performance] count tool-result message entries from their serialized header (#2416)

Seth Karten0597614e030b
diff --git a/packages/coding-agent/.changes/session-scan-count-only-message-entries.md b/packages/coding-agent/.changes/session-scan-count-only-message-entries.md
new file mode 100644
index 0000000000..e7c3a01559
--- /dev/null
+++ b/packages/coding-agent/.changes/session-scan-count-only-message-entries.md
@@ -0,0 +1 @@
+- Changed the session catalog scan to count tool-result (and extension-role) message entries from their serialized header instead of parsing their payloads, cutting the CPU a cold scan spends on transcripts whose tool output dwarfs everything else.
diff --git a/packages/coding-agent/src/core/session-manager.ts b/packages/coding-agent/src/core/session-manager.ts
index 7ca9043d91..07d2c54201 100644
--- a/packages/coding-agent/src/core/session-manager.ts
+++ b/packages/coding-agent/src/core/session-manager.ts
@@ -1252,10 +1252,64 @@ async function scanSessionLines(filePath: string, state: SessionScanState, size:
return undefined;
}
+// Entry headers are serialized before any payload, so a message entry's own
+// type key and its message role both land in the first few hundred bytes.
+const SESSION_LIST_HEADER_PREFIX_MAX_CHARS = 512;
+const SESSION_LIST_MESSAGE_TYPE_HEADER = '"type":"message"';
+const SESSION_LIST_MESSAGE_ROLE_HEADER = '"message":{"role":"';
+const SESSION_LIST_USER_ROLE = 'user"';
+const SESSION_LIST_ASSISTANT_ROLE = 'assistant"';
+
+/**
+ * Return true when a line's serialized header proves the entry is a message
+ * whose role can only contribute its message count.
+ *
+ * Tool results (and extension message roles) carry no usage, model, name, state,
+ * activity timestamp, or search text, yet records commit them verbatim: they are
+ * the largest single share of the bytes a cold catalog scan parses for nothing.
+ * Counting them from the header also spares the oversize branch its preview
+ * scans for multi-megabyte tool results.
+ *
+ * `"` is escaped inside a JSON string, so a header that matches here can only be
+ * structural. Every layout the file writer does not produce — spacing, another
+ * key order, a nested container before the role marker, a role that runs past the
+ * prefix — falls through to the full parse.
+ */
+function isCountOnlyMessageLine(line: string): boolean {
+ // Only the entry header decides this, and the header always fits in the
+ // prefix: bounding the search keeps a file written in another layout
+ // (spacing, a different key order) from paying a full scan per line.
+ const prefix =
+ line.length > SESSION_LIST_HEADER_PREFIX_MAX_CHARS ? line.slice(0, SESSION_LIST_HEADER_PREFIX_MAX_CHARS) : line;
+ const typeIndex = prefix.indexOf(SESSION_LIST_MESSAGE_TYPE_HEADER);
+ if (typeIndex < 0) return false;
+ const roleMarkerIndex = prefix.indexOf(SESSION_LIST_MESSAGE_ROLE_HEADER, typeIndex);
+ if (roleMarkerIndex < 0) return false;
+ // The role marker carries the message object's own brace, so any earlier
+ // container belongs to another object: an entry that nests a payload before
+ // its type key, or one that quotes this header before its own message key.
+ // Either way the role found below is not the entry's message role.
+ const containerIndex = prefix.indexOf("{", 1);
+ if (containerIndex >= 0 && containerIndex < roleMarkerIndex) return false;
+ const roleIndex = roleMarkerIndex + SESSION_LIST_MESSAGE_ROLE_HEADER.length;
+ // A role that reaches past the prefix cannot be compared, so it takes the full parse.
+ if (roleIndex + SESSION_LIST_ASSISTANT_ROLE.length > prefix.length) return false;
+ return (
+ !prefix.startsWith(SESSION_LIST_USER_ROLE, roleIndex) &&
+ !prefix.startsWith(SESSION_LIST_ASSISTANT_ROLE, roleIndex)
+ );
+}
+
function foldSessionScanLine(acc: SessionScanAccumulator, lineBuffer: Buffer): void {
const line = lineBuffer.toString("utf8");
if (!line.trim()) return;
+ // Guarded on the header so damaged-file detection still sees the first entry parsed.
+ if (acc.header !== undefined && isCountOnlyMessageLine(line)) {
+ acc.messageCount++;
+ return;
+ }
+
// Large tool-result entries can be many MB. They do not carry the
// session-list metadata we need, and parsing them during every refresh
// can exhaust the daemon heap.
diff --git a/packages/coding-agent/test/session-manager/file-operations.test.ts b/packages/coding-agent/test/session-manager/file-operations.test.ts
index 4ec4f1a87b..81499c3af9 100644
--- a/packages/coding-agent/test/session-manager/file-operations.test.ts
+++ b/packages/coding-agent/test/session-manager/file-operations.test.ts
@@ -64,9 +64,7 @@ describe("loadEntriesFromFile", () => {
it.each<[string, string | undefined, string[]]>([
["a missing file", undefined, []],
- ["an empty file", "", []],
["a file without a session header", `${MESSAGE}\n`, []],
- ["malformed JSON", "not json\n", []],
["a valid session file", `${HEADER}\n${MESSAGE}\n`, ["session", "message"]],
[
"a file with a malformed line between valid ones",
@@ -377,8 +375,6 @@ describe("findMostRecentSession", () => {
}
it.each<[string, () => { dir: string; expected: string | null }]>([
- ["returns null for an empty directory", () => ({ dir: tempDir, expected: null })],
- ["returns null for a non-existent directory", () => ({ dir: join(tempDir, "nonexistent"), expected: null })],
[
"ignores non-jsonl files",
() => {
@@ -800,8 +796,34 @@ describe("readSessionInfo incremental scans", () => {
utimesSync(file, fixedTime, fixedTime);
expect((await readSessionInfo(file))?.firstMessage).toBe("after recreate");
});
+ describe("tool-result entries counted from their headers", () => {
+ const prompt = line(header) + line(msg("u", null, "user", "hello")) + line(msg("a", "u", "assistant", "reply"));
+ const tool = (idFirst = false, tear = false, payload = "x") =>
+ `${idFirst ? `{"id":"t1","parentId":"a1","timestamp":"${header.timestamp}","type":"message",` : `{"type":"message","id":"t1","parentId":"a1","timestamp":"${header.timestamp}",`}"message":{"role":"toolResult","content":"${payload}"${tear ? "" : "}\n"}`;
+ const scan = (body: string) => {
+ const file = join(tempDir, "counted.jsonl");
+ writeFileSync(file, body);
+ return readSessionInfo(file);
+ };
+ const shadowed = `{"type":"message","meta":{"message":{"role":"toolResult"}},"message":{"role":"user","content":"kept","timestamp":1}}`;
+ const boundary = (length: number) =>
+ `{"type":"message","id":"${"x".repeat(length)}","parentId":null,"timestamp":"${header.timestamp}","message":{"role":"user","content":"kept","timestamp":1}}`;
+ const boundaryId = 512 - 19 - boundary(0).indexOf('"message":{"role":"');
+ it.each([
+ ["an unparsed tool result", prompt + tool(false, true), 3, "hello reply"],
+ ["an oversized tool result", prompt + tool(false, false, "y".repeat(1024 * 1024)), 3, "hello reply"],
+ ["id-first tool results", prompt + tool(true) + tool(true, true), 4, "hello reply"],
+ ["a container before the role marker", `${line(header)}${shadowed}\n`, 1, "kept"],
+ ["the role at the prefix boundary", `${line(header)}${boundary(boundaryId)}\n`, 1, "kept"],
+ ])("counts %s from its header", async (_case, body, messageCount, allMessagesText) => {
+ expect(await scan(body)).toMatchObject({ messageCount, allMessagesText });
+ });
+ it("drops a damaged session whose first entry is a tool result", async () => {
+ expect(await scan(tool())).toBeNull();
+ expect(await SessionManager.listAll(undefined, tempDir)).toEqual([]);
+ });
+ });
});
-
describe("migrateSessionEntries", () => {
const assistant = {
role: "assistant",