Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

perf(importers): cap and reverse codex scan traversal to prevent freeze

107\wud8f32feaa8d0
diff --git a/apps/desktop/electron/main/importers/codex.ts b/apps/desktop/electron/main/importers/codex.ts
index 9ccf160d0..7fcd794b3 100644
--- a/apps/desktop/electron/main/importers/codex.ts
+++ b/apps/desktop/electron/main/importers/codex.ts
@@ -156,7 +156,10 @@ async function parseFile(filePath: string): Promise<ParsedCodexFile | null> {
return parsed.items.length > 0 ? parsed : null;
}
-async function listSessionFiles(dir: string = SESSIONS_DIR): Promise<string[]> {
+async function listSessionFiles(
+ dir: string = SESSIONS_DIR,
+ maxFiles: number = CODEX_SCAN_MAX_FILES,
+): Promise<string[]> {
const out: string[] = [];
const walk = async (dir: string, depth: number) => {
let entries: string[] = [];
@@ -165,7 +168,9 @@ async function listSessionFiles(dir: string = SESSIONS_DIR): Promise<string[]> {
} catch {
return;
}
+ entries.sort().reverse();
for (const entry of entries) {
+ if (out.length >= maxFiles) return;
const full = path.join(dir, entry);
if (entry.endsWith(".jsonl")) {
out.push(full);
@@ -195,6 +200,7 @@ async function listSessionFiles(dir: string = SESSIONS_DIR): Promise<string[]> {
// require reading the whole file, which sampling exists to avoid).
export const CODEX_SCAN_FULL_PARSE_MAX_BYTES = 5 * 1024 * 1024;
+export const CODEX_SCAN_MAX_FILES = 250;
const CODEX_SCAN_HEAD_BYTES = 1024 * 1024;
const CODEX_SCAN_TAIL_BYTES = 256 * 1024;
@@ -397,8 +403,9 @@ async function scanFile(filePath: string): Promise<CodexScanMeta | null> {
export async function scanCodexSessions(
dir: string = SESSIONS_DIR,
+ maxFiles: number = CODEX_SCAN_MAX_FILES,
): Promise<ExternalSessionSummary[]> {
- const files = await listSessionFiles(dir);
+ const files = await listSessionFiles(dir, maxFiles);
const summaries: ExternalSessionSummary[] = [];
for (const filePath of files) {
const meta = await scanFile(filePath);
diff --git a/apps/desktop/test/importer-codex-scan.test.mjs b/apps/desktop/test/importer-codex-scan.test.mjs
index 2af4d7dd5..12bcfa57d 100644
--- a/apps/desktop/test/importer-codex-scan.test.mjs
+++ b/apps/desktop/test/importer-codex-scan.test.mjs
@@ -9,7 +9,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
register(pathToFileURL(joinPath(here, "helpers/ts-import-hooks.mjs")));
-const { CODEX_SCAN_FULL_PARSE_MAX_BYTES, scanCodexSessions } = await import(
+const { CODEX_SCAN_FULL_PARSE_MAX_BYTES, CODEX_SCAN_MAX_FILES, scanCodexSessions } = await import(
"../electron/main/importers/codex.ts"
);
@@ -396,3 +396,31 @@ test("the sampled path also falls back to the file mtime", async () => {
},
);
});
+
+test("scans newest sessions first and bounds total files to limit", async () => {
+ assert.equal(CODEX_SCAN_MAX_FILES, 250);
+ await withArchive(
+ [
+ [
+ "2026/01/01/old.jsonl",
+ [
+ metaLine("old-1", "/repo", "2026-01-01T00:00:00Z"),
+ responseItem("user", "很早以前的消息", "2026-01-01T00:00:01Z"),
+ ].join("\n"),
+ ],
+ [
+ "2026/09/18/new.jsonl",
+ [
+ metaLine("new-1", "/repo", "2026-09-18T00:00:00Z"),
+ responseItem("user", "今天的最新消息", "2026-09-18T00:00:01Z"),
+ ].join("\n"),
+ ],
+ ],
+ async (dir) => {
+ const summaries = await scanCodexSessions(dir, 1);
+ assert.equal(summaries.length, 1);
+ assert.equal(summaries[0].externalId, "new-1");
+ assert.equal(summaries[0].title, "今天的最新消息");
+ },
+ );
+});