Skip to content

Commit baa7581

Browse files
authored
perf(sessions): avoid joining complete JSONL lines (#133611)
1 parent 055e65f commit baa7581

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎src/commands/doctor-session-sqlite-readers.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import fs from "node:fs";
22
import os from "node:os";
33
import path from "node:path";
44
import { afterEach, beforeEach, describe, expect, it } from "vitest";
5-
import { createTranscriptEventReader } from "./doctor-session-sqlite-readers.js";
5+
import {
6+
countTranscriptEventsForPath,
7+
createTranscriptEventReader,
8+
} from "./doctor-session-sqlite-readers.js";
69

710
describe("legacy transcript row classification", () => {
811
let directory: string;
@@ -13,6 +16,36 @@ describe("legacy transcript row classification", () => {
1316
});
1417
afterEach(() => fs.rmSync(directory, { recursive: true, force: true }));
1518

19+
it.each([1, 2, 3])("preserves records when UTF-8 splits after byte %s", (splitAfter) => {
20+
const header = '\uFEFF{"type":"session","version":3}\r\n \t\r\n';
21+
const prefix = '{"type":"plugin_state","payload":"';
22+
const padding = "x".repeat(65_536 - Buffer.byteLength(header + prefix) - splitAfter);
23+
const firstLine = `${prefix}${padding}🦞"}`;
24+
const giant = { type: "plugin_state", payload: "λ".repeat(100_000) };
25+
const tail = { type: "plugin_state", payload: "final unterminated row" };
26+
fs.writeFileSync(
27+
transcriptPath,
28+
`${header}${firstLine}\r\n\n${JSON.stringify(giant)}\r\n${JSON.stringify(tail)}`,
29+
);
30+
31+
const events: unknown[] = [];
32+
const validate = createTranscriptEventReader(
33+
transcriptPath,
34+
"target",
35+
)((event) => events.push(event));
36+
validate();
37+
38+
expect(events[0]).toEqual({
39+
type: "session",
40+
version: 3,
41+
id: "target",
42+
timestamp: "",
43+
cwd: "",
44+
});
45+
expect(events.slice(1)).toEqual([JSON.parse(firstLine), giant, tail]);
46+
expect(countTranscriptEventsForPath(transcriptPath)).toEqual({ status: "ok", events: 4 });
47+
});
48+
1649
it.each([1, 2, 3, 4])("preserves recognized and opaque rows from version %s", (version) => {
1750
const unindexedHook = { type: "message", message: { role: "custom", content: "context" } };
1851
const opaqueRows = [

‎src/commands/doctor-session-sqlite-readers.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,16 @@ function* iterateJsonlLinesSync(filePath: string): Generator<{ lineNumber: numbe
540540
break;
541541
}
542542
const parts = decoder.decode(buffer.subarray(0, bytesRead), { stream: true }).split("\n");
543+
// Only the first complete line can span chunks. Keep unterminated giant
544+
// records fragmented until a newline arrives instead of repeatedly joining them.
545+
if (parts.length > 1 && fragments.length > 0) {
546+
fragments.push(parts[0]!);
547+
parts[0] = fragments.join("");
548+
fragments = [];
549+
}
543550
for (let index = 0; index < parts.length - 1; index++) {
544-
fragments.push(parts[index]!);
545551
lineNumber += 1;
546-
const text = fragments.join("").trim();
547-
fragments = [];
552+
const text = parts[index]!.trim();
548553
if (text) {
549554
yield { lineNumber, text };
550555
}

0 commit comments

Comments
 (0)