Skip to content

Commit a6a828d

Browse files
committed
refactor(codex): isolate JSONL line scanning
1 parent ac9610a commit a6a828d

2 files changed

Lines changed: 98 additions & 95 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import fs from "node:fs/promises";
2+
3+
const JSONL_STREAM_THRESHOLD_BYTES = 4 * 1024 * 1024;
4+
const JSONL_READ_CHUNK_BYTES = 1024 * 1024;
5+
export const JSONL_FIRST_LINE_CHUNK_BYTES = 64 * 1024;
6+
7+
export async function visitJsonlLines(
8+
file: string,
9+
visitor: (line: string) => boolean | void,
10+
chunkBytes = JSONL_READ_CHUNK_BYTES,
11+
): Promise<{ ok: boolean; lineCount: number }> {
12+
let size: number;
13+
try {
14+
size = (await fs.stat(file)).size;
15+
} catch {
16+
return { ok: false, lineCount: 0 };
17+
}
18+
if (size <= JSONL_STREAM_THRESHOLD_BYTES) {
19+
let content: string;
20+
try {
21+
content = await fs.readFile(file, "utf8");
22+
} catch {
23+
return { ok: false, lineCount: 0 };
24+
}
25+
if (content.length === 0) {
26+
return { ok: true, lineCount: 0 };
27+
}
28+
let lineCount = 0;
29+
for (const line of content.split(/\r?\n/u)) {
30+
lineCount += 1;
31+
if (visitor(line) === false) {
32+
break;
33+
}
34+
}
35+
return { ok: true, lineCount };
36+
}
37+
38+
let handle: Awaited<ReturnType<typeof fs.open>>;
39+
try {
40+
handle = await fs.open(file, "r");
41+
} catch {
42+
return { ok: false, lineCount: 0 };
43+
}
44+
const buffer = Buffer.allocUnsafe(chunkBytes);
45+
const decoder = new TextDecoder();
46+
let pendingFragments: string[] = [];
47+
let lineCount = 0;
48+
try {
49+
while (true) {
50+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, null);
51+
if (bytesRead === 0) {
52+
break;
53+
}
54+
const content = decoder.decode(buffer.subarray(0, bytesRead), { stream: true });
55+
let lineStart = 0;
56+
while (true) {
57+
const newline = content.indexOf("\n", lineStart);
58+
if (newline === -1) {
59+
break;
60+
}
61+
let rawLine = content.slice(lineStart, newline);
62+
if (pendingFragments.length > 0) {
63+
pendingFragments.push(rawLine);
64+
rawLine = pendingFragments.join("");
65+
pendingFragments = [];
66+
}
67+
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
68+
lineCount += 1;
69+
if (visitor(line) === false) {
70+
return { ok: true, lineCount };
71+
}
72+
lineStart = newline + 1;
73+
}
74+
if (lineStart < content.length) {
75+
pendingFragments.push(content.slice(lineStart));
76+
}
77+
}
78+
const decoderTail = decoder.decode();
79+
if (decoderTail.length > 0) {
80+
pendingFragments.push(decoderTail);
81+
}
82+
if (pendingFragments.length > 0) {
83+
const rawLine = pendingFragments.join("");
84+
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
85+
lineCount += 1;
86+
visitor(line);
87+
}
88+
return { ok: true, lineCount };
89+
} catch {
90+
return { ok: false, lineCount: 0 };
91+
} finally {
92+
await handle.close().catch(() => undefined);
93+
}
94+
}

‎extensions/codex/src/node-cli-sessions.ts‎

Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ import {
1919
resolveWindowsSpawnProgram,
2020
} from "openclaw/plugin-sdk/windows-spawn";
2121
import { formatCodexDisplayText } from "./command-formatters.js";
22+
import { JSONL_FIRST_LINE_CHUNK_BYTES, visitJsonlLines } from "./jsonl-lines.js";
2223

2324
const CODEX_CLI_SESSIONS_LIST_COMMAND = "codex.cli.sessions.list";
2425
export const CODEX_CLI_SESSION_RESUME_COMMAND = "codex.cli.session.resume";
2526

2627
const DEFAULT_SESSION_LIMIT = 10;
2728
const MAX_SESSION_LIMIT = 50;
2829
const DEFAULT_RESUME_TIMEOUT_MS = 20 * 60_000;
29-
const JSONL_STREAM_THRESHOLD_BYTES = 4 * 1024 * 1024;
30-
const JSONL_READ_CHUNK_BYTES = 1024 * 1024;
31-
const JSONL_FIRST_LINE_CHUNK_BYTES = 64 * 1024;
3230
const SESSION_ID_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/;
3331
const activeResumeSessions = new Set<string>();
3432

@@ -350,7 +348,7 @@ async function readHistorySessions(
350348
): Promise<Map<string, CodexCliSessionSummary>> {
351349
const summaries = new Map<string, CodexCliSessionSummary>();
352350
const historyPath = path.join(codexHome, "history.jsonl");
353-
const result = await visitLines(historyPath, (line) => {
351+
const result = await visitJsonlLines(historyPath, (line) => {
354352
const trimmed = line.trim();
355353
if (!trimmed) {
356354
return;
@@ -450,7 +448,7 @@ async function readSessionFileSummary(file: string): Promise<CodexCliSessionSumm
450448
let updatedAt: string | undefined;
451449
let lastMessage: string | undefined;
452450
let messageCount = 0;
453-
const result = await visitLines(file, (line) => {
451+
const result = await visitJsonlLines(file, (line) => {
454452
const trimmed = line.trim();
455453
if (!trimmed) {
456454
return;
@@ -665,98 +663,9 @@ function resolveCodexHome(): string {
665663
return process.env.CODEX_HOME?.trim() || path.join(os.homedir(), ".codex");
666664
}
667665

668-
async function visitLines(
669-
file: string,
670-
visitor: (line: string) => boolean | void,
671-
chunkBytes = JSONL_READ_CHUNK_BYTES,
672-
): Promise<{ ok: boolean; lineCount: number }> {
673-
let size: number;
674-
try {
675-
size = (await fs.stat(file)).size;
676-
} catch {
677-
return { ok: false, lineCount: 0 };
678-
}
679-
if (size <= JSONL_STREAM_THRESHOLD_BYTES) {
680-
let content: string;
681-
try {
682-
content = await fs.readFile(file, "utf8");
683-
} catch {
684-
return { ok: false, lineCount: 0 };
685-
}
686-
if (content.length === 0) {
687-
return { ok: true, lineCount: 0 };
688-
}
689-
let lineCount = 0;
690-
for (const line of content.split(/\r?\n/u)) {
691-
lineCount += 1;
692-
if (visitor(line) === false) {
693-
break;
694-
}
695-
}
696-
return { ok: true, lineCount };
697-
}
698-
699-
let handle: Awaited<ReturnType<typeof fs.open>>;
700-
try {
701-
handle = await fs.open(file, "r");
702-
} catch {
703-
return { ok: false, lineCount: 0 };
704-
}
705-
const buffer = Buffer.allocUnsafe(chunkBytes);
706-
const decoder = new TextDecoder();
707-
let pendingFragments: string[] = [];
708-
let lineCount = 0;
709-
try {
710-
while (true) {
711-
const { bytesRead } = await handle.read(buffer, 0, buffer.length, null);
712-
if (bytesRead === 0) {
713-
break;
714-
}
715-
const content = decoder.decode(buffer.subarray(0, bytesRead), { stream: true });
716-
let lineStart = 0;
717-
while (true) {
718-
const newline = content.indexOf("\n", lineStart);
719-
if (newline === -1) {
720-
break;
721-
}
722-
let rawLine = content.slice(lineStart, newline);
723-
if (pendingFragments.length > 0) {
724-
pendingFragments.push(rawLine);
725-
rawLine = pendingFragments.join("");
726-
pendingFragments = [];
727-
}
728-
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
729-
lineCount += 1;
730-
if (visitor(line) === false) {
731-
return { ok: true, lineCount };
732-
}
733-
lineStart = newline + 1;
734-
}
735-
if (lineStart < content.length) {
736-
pendingFragments.push(content.slice(lineStart));
737-
}
738-
}
739-
const decoderTail = decoder.decode();
740-
if (decoderTail.length > 0) {
741-
pendingFragments.push(decoderTail);
742-
}
743-
if (pendingFragments.length > 0) {
744-
const rawLine = pendingFragments.join("");
745-
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
746-
lineCount += 1;
747-
visitor(line);
748-
}
749-
return { ok: true, lineCount };
750-
} catch {
751-
return { ok: false, lineCount: 0 };
752-
} finally {
753-
await handle.close().catch(() => undefined);
754-
}
755-
}
756-
757666
async function readFirstLine(file: string): Promise<string | undefined> {
758667
let firstLine: string | undefined;
759-
const result = await visitLines(
668+
const result = await visitJsonlLines(
760669
file,
761670
(line) => {
762671
firstLine = line;

0 commit comments

Comments
 (0)