Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): Codex 导入统一采用 session_index 的用户自定义标题 (#3673)
fico-hub6af9dcd36e54
diff --git a/apps/desktop/src/main/__tests__/codexLocalSessions.test.ts b/apps/desktop/src/main/__tests__/codexLocalSessions.test.tsindex d1b213c2e3..d01be11c8b 100644--- a/apps/desktop/src/main/__tests__/codexLocalSessions.test.ts+++ b/apps/desktop/src/main/__tests__/codexLocalSessions.test.ts@@ -369,6 +369,86 @@ describe('Codex local session import', () => {expect(row.updatedAt).toBe(before.updatedAt);});+ it('explicit import prefers session_index thread_name over stale state DB title (#3482)', async () => {+ // Codex 侧重命名写入 session_index.jsonl 的 thread_name;state DB 行的 title+ // 仍是旧自动标题。导入必须按 thread_name > state DB 标题取值,且索引+ // updated_at 合入有效更新时间。+ const dbPath = createStateDb(externalHome);+ const rolloutPath = path.join(externalHome, 'sessions', `rollout-2026-05-13-${threadId}.jsonl`);+ fs.mkdirSync(path.dirname(rolloutPath), { recursive: true });+ fs.writeFileSync(rolloutPath, '');+ insertThread(dbPath, threadId, rolloutPath, { updatedAt: 1_000, title: 'Auto Title' });+ fs.writeFileSync(+ path.join(externalHome, 'session_index.jsonl'),+ `${JSON.stringify({ id: threadId, thread_name: 'My Renamed Session', updated_at: 2_000 })}\n`,+ );++ const first = await importExternalCodexSessions([threadId]);+ expect(first).toMatchObject({ inserted: 1 });+ const row = currentTestDb()+ .prepare('SELECT title, updated_at AS updatedAt FROM sessions WHERE id = ?')+ .get(`codex-${threadId}`) as { title: string; updatedAt: number };+ expect(row.title).toBe('My Renamed Session');+ // timestampFromAny 把小数值按秒解释并归一为毫秒:2_000s → 2_000_000ms。+ expect(row.updatedAt).toBe(2_000_000);+ });++ it('re-import after Codex-side rename refreshes the stored title (#3482)', async () => {+ const dbPath = createStateDb(externalHome);+ const rolloutPath = path.join(externalHome, 'sessions', `rollout-2026-05-13-${threadId}.jsonl`);+ fs.mkdirSync(path.dirname(rolloutPath), { recursive: true });+ fs.writeFileSync(rolloutPath, '');+ insertThread(dbPath, threadId, rolloutPath, { updatedAt: 1_000, title: 'Auto Title' });++ const first = await importExternalCodexSessions([threadId]);+ expect(first).toMatchObject({ inserted: 1 });++ fs.writeFileSync(+ path.join(externalHome, 'session_index.jsonl'),+ `${JSON.stringify({ id: threadId, thread_name: 'Renamed Later', updated_at: 2_000 })}\n`,+ );+ const again = await importExternalCodexSessions([threadId]);+ expect(again).toMatchObject({ inserted: 0, updated: 1 });+ const row = currentTestDb()+ .prepare('SELECT title, updated_at AS updatedAt FROM sessions WHERE id = ?')+ .get(`codex-${threadId}`) as { title: string; updatedAt: number };+ expect(row.title).toBe('Renamed Later');+ expect(row.updatedAt).toBe(2_000_000);+ });++ it('batch import parses session_index.jsonl once per home while unchanged (#3673 review P2)', async () => {+ // 按 ID 导入对每个所选会话都会解析一次索引;(mtimeMs, size) 缓存把同一+ // home 未变化的索引收敛到单次读取,批量导入不再随会话数线性重复 IO。+ const dbPath = createStateDb(externalHome);+ for (const id of [threadId, execThreadId]) {+ const rolloutPath = path.join(externalHome, 'sessions', `rollout-2026-05-13-${id}.jsonl`);+ fs.mkdirSync(path.dirname(rolloutPath), { recursive: true });+ fs.writeFileSync(rolloutPath, '');+ insertThread(dbPath, id, rolloutPath, { updatedAt: 1_000, title: 'Auto Title' });+ }+ fs.writeFileSync(+ path.join(externalHome, 'session_index.jsonl'),+ `${[+ JSON.stringify({ id: threadId, thread_name: 'Named A', updated_at: 2_000 }),+ JSON.stringify({ id: execThreadId, thread_name: 'Named B', updated_at: 2_000 }),+ ].join('\n')}\n`,+ );++ const readSpy = vi.spyOn(fs, 'readFileSync');+ const result = await importExternalCodexSessions([threadId, execThreadId]);+ const indexReads = readSpy.mock.calls.filter(+ ([target]) => typeof target === 'string' && target.endsWith('session_index.jsonl'),+ );+ readSpy.mockRestore();++ expect(result).toMatchObject({ inserted: 2 });+ expect(indexReads).toHaveLength(1);+ const titles = (currentTestDb()+ .prepare('SELECT title FROM sessions ORDER BY title')+ .all() as Array<{ title: string }>).map((r) => r.title);+ expect(titles).toEqual(['Named A', 'Named B']);+ });+it('filters Codex source JSON subagent rows even when thread_source is missing', async () => {const dbPath = createStateDb(externalHome);const rolloutPath = path.join(externalHome, 'sessions', `rollout-2026-05-13-${threadId}.jsonl`);diff --git a/apps/desktop/src/main/maker-host/codex-local-sessions.ts b/apps/desktop/src/main/maker-host/codex-local-sessions.tsindex 128c35d6c9..dbe13d30cc 100644--- a/apps/desktop/src/main/maker-host/codex-local-sessions.ts+++ b/apps/desktop/src/main/maker-host/codex-local-sessions.ts@@ -316,6 +316,8 @@ interface CodexThreadSummary {interface CodexSessionIndexEntry {title: string;+ /** 用户真实自定义标题(thread_name > title,无 fallback);无则 null(#3482)。 */+ customTitle: string | null;updatedAt: number | null;}@@ -3780,6 +3782,7 @@ function readThreads(try {db = openReadonlyDb(dbPath);if (!tableExists(db, 'threads')) return null;+ const index = readSessionIndex(home);const orderSql = buildThreadOrderSql(db);const rows = db.prepare(`SELECT *@@ -3794,7 +3797,7 @@ function readThreads(continue;}if (threads.length >= MAX_THREADS_PER_HOME) continue;- const thread = normalizeThreadRow(home, dbPath, row, projectlessThreadIds);+ const thread = normalizeThreadRow(home, dbPath, row, projectlessThreadIds, index);if (thread) threads.push(thread);}return { threads, rejectedThreadIds: [...rejectedThreadIds] };@@ -3834,16 +3837,50 @@ async function readThreadsFromRollouts(continue;}if (out.length >= MAX_THREADS_PER_HOME) continue;- const thread = normalizeThreadRow(home, null, row, projectlessThreadIds);+ const thread = normalizeThreadRow(home, null, row, projectlessThreadIds, index);if (thread) out.push(thread);}return { threads: out, rejectedThreadIds: [...rejectedThreadIds] };}+/**+ * session_index.jsonl 解析缓存(review #3673 P2):按 ID 导入路径对每个所选+ * 会话 × 每个候选 home 都要读一次索引,批量导入时主进程同步 IO 随会话数 ×+ * home 数 × 索引大小线性增长。以 (mtimeMs, size) 做新鲜度判据 —— 索引是+ * append-only jsonl,追加必然改变 size,同秒内 mtime 粒度不足由 size 兜住;+ * appendSessionIndexEntry 写入后下一次读取自然失效重解析。命中时直接复用+ * 解析结果,跨扫描/导入批次同样生效。返回的 Map 视为只读,调用方只做 .get()。+ */+const sessionIndexCache = new Map<+ string,+ { mtimeMs: number; size: number; entries: Map<string, CodexSessionIndexEntry> }+>();+function readSessionIndex(home: string): Map<string, CodexSessionIndexEntry> {const indexPath = path.join(home, 'session_index.jsonl');+ let stat: fs.Stats;+ try {+ stat = fs.statSync(indexPath);+ } catch {+ // 索引缺席(或不可 stat):行为与旧实现的 existsSync 早退一致。不缓存+ // 空结果 —— 文件随时可能出现,缺席路径本身已是零解析成本。+ sessionIndexCache.delete(home);+ return new Map();+ }+ const cached = sessionIndexCache.get(home);+ if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {+ return cached.entries;+ }+ const entries = parseSessionIndexFile(indexPath, home);+ sessionIndexCache.set(home, { mtimeMs: stat.mtimeMs, size: stat.size, entries });+ return entries;+}++function parseSessionIndexFile(+ indexPath: string,+ home: string,+): Map<string, CodexSessionIndexEntry> {const out = new Map<string, CodexSessionIndexEntry>();- if (!fs.existsSync(indexPath)) return out;try {const lines = fs.readFileSync(indexPath, 'utf-8').split(/\r?\n/);for (const line of lines) {@@ -3857,8 +3894,10 @@ function readSessionIndex(home: string): Map<string, CodexSessionIndexEntry> {if (!isRecord(obj)) continue;const id = stringValue(obj.id);if (!isLikelyThreadId(id)) continue;+ const customTitle = firstNonEmpty(stringValue(obj.thread_name), stringValue(obj.title), '');out.set(id, {- title: firstNonEmpty(stringValue(obj.thread_name), stringValue(obj.title), 'Codex Session'),+ title: customTitle || 'Codex Session',+ customTitle: customTitle || null,updatedAt: timestampFromAny(obj.updated_at),});}@@ -3995,7 +4034,7 @@ function normalizeRolloutFile(): CodexThreadSummary | null {const row = readRolloutThreadRow(file, index);if (!row || !isTopLevelThreadRow(row)) return null;- return normalizeThreadRow(home, null, row, projectlessThreadIds);+ return normalizeThreadRow(home, null, row, projectlessThreadIds, index);}function readRolloutThreadRow(@@ -4634,7 +4673,8 @@ function findThreadByIdInHome(home: string, threadId: string): CodexThreadSummarif (!tableExists(db, 'threads')) return findExternalThreadByIdFromRollouts(home, threadId);const row = db.prepare('SELECT * FROM threads WHERE id = ? LIMIT 1').get(threadId) as SqlRow | undefined;if (!row || !isTopLevelThreadRow(row)) return findExternalThreadByIdFromRollouts(home, threadId);- return normalizeThreadRow(home, dbPath, row, projectlessThreadIds) ?? findExternalThreadByIdFromRollouts(home, threadId);+ return normalizeThreadRow(home, dbPath, row, projectlessThreadIds, readSessionIndex(home))+ ?? findExternalThreadByIdFromRollouts(home, threadId);} catch {return findExternalThreadByIdFromRollouts(home, threadId);} finally {@@ -4755,17 +4795,27 @@ function normalizeThreadRow(dbPath: string | null,row: SqlRow,projectlessThreadIds: ReadonlySet<string>,+ index?: ReadonlyMap<string, CodexSessionIndexEntry>,): CodexThreadSummary | null {const threadId = stringValue(row.id);if (!isLikelyThreadId(threadId)) return null;+ // #3482:session_index.jsonl 的 thread_name 是用户在 Codex 侧的重命名,+ // state DB 行里的 title 可能仍是旧的自动标题;索引记录存在时按+ // thread_name > title(index) > state DB/rollout 标题 兜底链取值,并把索引+ // updated_at 合入有效更新时间(重命名要能通过导入 upsert 的时间门)。+ const indexEntry = index?.get(threadId);const archived = numberValue(row.archived) === 1;const baseUpdatedAt = timestampMs(row.updated_at_ms, row.updated_at) ?? Date.now();const archivedAt = timestampFromAny(row.archived_at);- const updatedAt = archived && archivedAt ? Math.max(baseUpdatedAt, archivedAt) : baseUpdatedAt;+ const rowUpdatedAt = archived && archivedAt ? Math.max(baseUpdatedAt, archivedAt) : baseUpdatedAt;+ const updatedAt = indexEntry?.updatedAt+ ? Math.max(rowUpdatedAt, indexEntry.updatedAt)+ : rowUpdatedAt;const createdAt = timestampMs(row.created_at_ms, row.created_at) ?? updatedAt;const cwd = stringValue(row.cwd) || os.homedir();const rolloutPath = stringValue(row.rollout_path);const title = firstNonEmpty(+ indexEntry?.customTitle ?? '',stringValue(row.title),stringValue(row.preview),stringValue(row.first_user_message).split(/\r?\n/)[0],