Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 显式导入时复活已软删的本地会话行 (#3659)
fico-hub18cc73ec5d7f
diff --git a/apps/desktop/src/main/__tests__/claudeLocalSessions.test.ts b/apps/desktop/src/main/__tests__/claudeLocalSessions.test.tsindex c626e2f18b..a3d6c987dc 100644--- a/apps/desktop/src/main/__tests__/claudeLocalSessions.test.ts+++ b/apps/desktop/src/main/__tests__/claudeLocalSessions.test.ts@@ -985,6 +985,56 @@ describe('parseClaudeCodeMessageLine', () => {}});+ it('revives a soft-deleted imported session on explicit re-import (#3548)', async () => {+ // 删除是软删且源 JSONL 不随删,扫描会重新出候选;此前 ON CONFLICT 不更新+ // status,重导入命中同主键后行仍是 deleted —— 导入计数更新、侧栏不可见。+ // 删除动作把 updated_at 推到删除时刻(晚于源文件),复活不得受时间门约束。+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-local-home-'));+ const projectsDir = path.join(home, '.claude', 'projects', '-tmp-project');+ fs.mkdirSync(projectsDir, { recursive: true });+ fs.writeFileSync(+ path.join(projectsDir, `${sdkSessionId}.jsonl`),+ `${line({+ type: 'user',+ uuid: 'user-1',+ cwd: '/tmp/project',+ message: { role: 'user', content: 'import me' },+ })}\n`,+ );++ const db = createLocalDb();+ const homedir = vi.spyOn(os, 'homedir').mockReturnValue(home);+ setLocalDb(db);++ try {+ const first = await importExternalClaudeCodeSessions([sdkSessionId]);+ expect(first).toMatchObject({ inserted: 1 });++ const before = db+ .prepare('SELECT title, updated_at AS updatedAt FROM sessions WHERE id = ?')+ .get(`claude-${sdkSessionId}`) as { title: string; updatedAt: number };+ db.prepare(+ "UPDATE sessions SET status = 'deleted', title = 'stale-after-delete', updated_at = updated_at + 999999 WHERE id = ?",+ ).run(`claude-${sdkSessionId}`);++ const again = await importExternalClaudeCodeSessions([sdkSessionId]);+ expect(again).toMatchObject({ scanned: 1, inserted: 0, updated: 1 });+ const row = db+ .prepare('SELECT status, title, updated_at AS updatedAt FROM sessions WHERE id = ?')+ .get(`claude-${sdkSessionId}`) as { status: string; title: string; updatedAt: number };+ expect(row.status).toBe('active');+ // 复活即按新导入对待:元数据与 updated_at 收敛回源值,不残留删除时刻+ // 的旧快照(review 反馈:仅复活 status 会让侧栏行与源会话不一致)。+ expect(row.title).toBe(before.title);+ expect(row.updatedAt).toBe(before.updatedAt);+ } finally {+ homedir.mockRestore();+ resetLocalDb();+ db.close();+ fs.rmSync(home, { recursive: true, force: true });+ }+ });+it('revalidates and rejects internal review sessions imported directly by id', async () => {const home = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-local-home-'));const projectsDir = path.join(home, '.claude', 'projects', '-tmp-project');diff --git a/apps/desktop/src/main/__tests__/codexLocalSessions.test.ts b/apps/desktop/src/main/__tests__/codexLocalSessions.test.tsindex 8d534c6272..d1b213c2e3 100644--- a/apps/desktop/src/main/__tests__/codexLocalSessions.test.ts+++ b/apps/desktop/src/main/__tests__/codexLocalSessions.test.ts@@ -339,6 +339,36 @@ describe('Codex local session import', () => {expect(rows).toEqual([{ id: `codex-${threadId}`, title: 'Top Level Codex Session' }]);}, 15_000);+ it('revives a soft-deleted imported thread on explicit re-import (#3548)', async () => {+ // 删除把 updated_at 推到删除时刻(晚于源 rollout),仅按时间门判定会让+ // 重导入永远停留在 deleted;显式导入命中已软删同主键行必须复活。+ 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: 'Revive Me' });++ const first = await importExternalCodexSessions([threadId]);+ expect(first).toMatchObject({ inserted: 1 });++ const before = currentTestDb()+ .prepare('SELECT title, updated_at AS updatedAt FROM sessions WHERE id = ?')+ .get(`codex-${threadId}`) as { title: string; updatedAt: number };+ currentTestDb()+ .prepare("UPDATE sessions SET status = 'deleted', title = 'stale-after-delete', updated_at = updated_at + 999999 WHERE id = ?")+ .run(`codex-${threadId}`);++ const again = await importExternalCodexSessions([threadId]);+ expect(again).toMatchObject({ inserted: 0, updated: 1 });+ const row = currentTestDb()+ .prepare('SELECT status, title, updated_at AS updatedAt FROM sessions WHERE id = ?')+ .get(`codex-${threadId}`) as { status: string; title: string; updatedAt: number };+ expect(row.status).toBe('active');+ // 复活即按新导入对待:元数据与 updated_at 收敛回源值(review 反馈)。+ expect(row.title).toBe(before.title);+ expect(row.updatedAt).toBe(before.updatedAt);+ });+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/claude-local-sessions.ts b/apps/desktop/src/main/maker-host/claude-local-sessions.tsindex 8b3156956f..0553333992 100644--- a/apps/desktop/src/main/maker-host/claude-local-sessions.ts+++ b/apps/desktop/src/main/maker-host/claude-local-sessions.ts@@ -565,15 +565,20 @@ async function upsertLocalSession(summary: ClaudeCodeSessionSummary): Promise<'i0, '[]', 'project', ?, ?)ON CONFLICT(id) DO UPDATE SET- title = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.title ELSE sessions.title END,- working_dir = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.working_dir ELSE sessions.working_dir END,+ -- 复活语义(#3548):旧行已软删时按全新导入对待。删除动作把 updated_at+ -- 推到删除时刻,若元数据仍按时间门保留旧值,复活行的标题/模型/权限模式/+ -- 令牌统计会停留在首次导入的旧快照,与当前源会话不一致;updated_at 同时+ -- 收敛回源值,后续同步不再被删除时刻挡住。非删除行为完全不变。+ title = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.title ELSE sessions.title END,+ working_dir = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.working_dir ELSE sessions.working_dir END,workspace_kind = excluded.workspace_kind,- model = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.model ELSE sessions.model END,- permission_mode = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.permission_mode ELSE sessions.permission_mode END,+ model = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.model ELSE sessions.model END,+ permission_mode = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.permission_mode ELSE sessions.permission_mode END,sdk_session_id = excluded.sdk_session_id,- total_token_usage = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.total_token_usage ELSE sessions.total_token_usage END,+ total_token_usage = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.total_token_usage ELSE sessions.total_token_usage END,+ status = CASE WHEN sessions.status = 'deleted' THEN excluded.status ELSE sessions.status END,user_send_at = COALESCE(sessions.user_send_at, excluded.user_send_at),- updated_at = MAX(sessions.updated_at, excluded.updated_at)+ updated_at = CASE WHEN sessions.status = 'deleted' THEN excluded.updated_at ELSE MAX(sessions.updated_at, excluded.updated_at) END`, [localId,summary.title,diff --git a/apps/desktop/src/main/maker-host/codex-local-sessions.ts b/apps/desktop/src/main/maker-host/codex-local-sessions.tsindex 02eb637716..128c35d6c9 100644--- a/apps/desktop/src/main/maker-host/codex-local-sessions.ts+++ b/apps/desktop/src/main/maker-host/codex-local-sessions.ts@@ -4824,20 +4824,26 @@ async function upsertLocalSession(thread: CodexThreadSummary): Promise<'inserted0, '[]', ?, ?, ?)ON CONFLICT(id) DO UPDATE SET- title = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.title ELSE sessions.title END,- working_dir = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.working_dir ELSE sessions.working_dir END,+ -- 复活语义(#3548,与 claude 侧同口径):旧行已软删时按全新导入对待,+ -- 元数据与 updated_at 一并收敛回源值,不残留删除时刻的旧快照。+ title = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.title ELSE sessions.title END,+ working_dir = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.working_dir ELSE sessions.working_dir END,-- Classification follows Codex global state, not local edit recency.-- This lets a re-import fix rows previously misclassified as projects-- while preserving newer local title/metadata via the CASE clauses.workspace_kind = excluded.workspace_kind,- model = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.model ELSE sessions.model END,- effort = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.effort ELSE sessions.effort END,- permission_mode = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.permission_mode ELSE sessions.permission_mode END,- status = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.status ELSE sessions.status END,+ model = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.model ELSE sessions.model END,+ effort = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.effort ELSE sessions.effort END,+ permission_mode = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.permission_mode ELSE sessions.permission_mode END,+ status = CASE+ WHEN sessions.status = 'deleted' THEN excluded.status+ WHEN sessions.updated_at <= excluded.updated_at THEN excluded.status+ ELSE sessions.status+ END,sdk_session_id = excluded.sdk_session_id,- total_token_usage = CASE WHEN sessions.updated_at <= excluded.updated_at THEN excluded.total_token_usage ELSE sessions.total_token_usage END,+ total_token_usage = CASE WHEN sessions.status = 'deleted' OR sessions.updated_at <= excluded.updated_at THEN excluded.total_token_usage ELSE sessions.total_token_usage END,user_send_at = COALESCE(sessions.user_send_at, excluded.user_send_at),- updated_at = MAX(sessions.updated_at, excluded.updated_at)+ updated_at = CASE WHEN sessions.status = 'deleted' THEN excluded.updated_at ELSE MAX(sessions.updated_at, excluded.updated_at) END`, [localId,thread.title,