Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 过滤 Claude 内部审查会话导入 (#1404)

Junshan41021ea282dd
diff --git a/apps/desktop/src/main/__tests__/claudeLocalSessions.test.ts b/apps/desktop/src/main/__tests__/claudeLocalSessions.test.ts
index dff67615f1..3648cdd02f 100644
--- a/apps/desktop/src/main/__tests__/claudeLocalSessions.test.ts
+++ b/apps/desktop/src/main/__tests__/claudeLocalSessions.test.ts
@@ -526,6 +526,55 @@ describe('parseClaudeCodeMessageLine', () => {
}
});
+ it('rejects internal review-channel sessions from scan and full summaries', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-review-channel-'));
+ const file = path.join(dir, `${sdkSessionId}.jsonl`);
+ fs.writeFileSync(
+ file,
+ `${line({
+ type: 'user',
+ uuid: 'user-review-channel',
+ cwd: '/tmp/project',
+ message: {
+ role: 'user',
+ content:
+ '<channel source="review-session-channel" source="local-review" id="review-1">\nReview this change',
+ },
+ })}\n`,
+ );
+
+ try {
+ expect(await readClaudeCodeSessionScanSummary(file)).toBeNull();
+ expect(await readClaudeCodeSessionSummary(file)).toBeNull();
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ it('keeps ordinary user-facing channel sessions importable', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-user-channel-'));
+ const file = path.join(dir, `${sdkSessionId}.jsonl`);
+ fs.writeFileSync(
+ file,
+ `${line({
+ type: 'user',
+ uuid: 'user-feishu-channel',
+ cwd: '/tmp/project',
+ message: {
+ role: 'user',
+ content: '<channel source="feishu" chat_id="chat-1">\nPlease summarize this conversation',
+ },
+ })}\n`,
+ );
+
+ try {
+ expect(await readClaudeCodeSessionScanSummary(file)).toMatchObject({ sdkSessionId });
+ expect(await readClaudeCodeSessionSummary(file)).toMatchObject({ sdkSessionId });
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
it('skips unchanged Claude JSONL files after importing them once', async () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-local-home-'));
const projectsDir = path.join(home, '.claude', 'projects', '-tmp-project');
@@ -882,6 +931,42 @@ describe('parseClaudeCodeMessageLine', () => {
}
});
+ 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');
+ fs.mkdirSync(projectsDir, { recursive: true });
+ const file = path.join(projectsDir, `${sdkSessionId}.jsonl`);
+ fs.writeFileSync(
+ file,
+ `${line({
+ type: 'user',
+ uuid: 'user-local-review',
+ cwd: '/tmp/project',
+ message: {
+ role: 'user',
+ content: '<channel source="local-review" id="review-1">\nReview this change',
+ },
+ })}\n`,
+ );
+
+ const db = createLocalDb();
+ const homedir = vi.spyOn(os, 'homedir').mockReturnValue(home);
+ setLocalDb(db);
+
+ try {
+ const result = await importExternalClaudeCodeSessions([sdkSessionId]);
+
+ expect(result).toMatchObject({ scanned: 1, inserted: 0, updated: 0 });
+ const rows = db.prepare('SELECT id FROM sessions').all();
+ expect(rows).toEqual([]);
+ } finally {
+ homedir.mockRestore();
+ resetLocalDb();
+ db.close();
+ fs.rmSync(home, { recursive: true, force: true });
+ }
+ });
+
it('normalizes Windows backslash cwd to storage form on import (#537)', async () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-local-home-'));
const projectsDir = path.join(home, '.claude', 'projects', 'D--Project-001');
@@ -1076,6 +1161,47 @@ describe('parseClaudeCodeMessageLine', () => {
}
});
+ it('does not surface an unresolved candidate when the first real user input is past the line cap', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-scan-unresolved-cap-'));
+ const file = path.join(dir, `${sdkSessionId}.jsonl`);
+ const noiseRows = Array.from({ length: 399 }, (_, index) =>
+ line({
+ type: 'system',
+ subtype: 'noise',
+ cwd: '/tmp/project',
+ seq: index,
+ }),
+ );
+ fs.writeFileSync(
+ file,
+ [
+ line({
+ type: 'user',
+ uuid: 'user-synthetic',
+ cwd: '/tmp/project',
+ message: { role: 'user', content: '<local-command-caveat>ignore</local-command-caveat>' },
+ }),
+ ...noiseRows,
+ line({
+ type: 'user',
+ uuid: 'user-review-after-cap',
+ cwd: '/tmp/project',
+ message: {
+ role: 'user',
+ content: '<channel source="review-session-channel">\nReview this change',
+ },
+ }),
+ ].join('\n'),
+ );
+
+ try {
+ expect(await readClaudeCodeSessionScanSummary(file)).toBeNull();
+ expect(await readClaudeCodeSessionSummary(file)).toBeNull();
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
it('scan summary is cached by (mtime, size) and re-parsed when the file changes', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-scan-cache-'));
const file = path.join(dir, `${sdkSessionId}.jsonl`);
diff --git a/apps/desktop/src/main/maker-host/claude-local-sessions.ts b/apps/desktop/src/main/maker-host/claude-local-sessions.ts
index ca52353b98..f064b72196 100644
--- a/apps/desktop/src/main/maker-host/claude-local-sessions.ts
+++ b/apps/desktop/src/main/maker-host/claude-local-sessions.ts
@@ -396,6 +396,7 @@ async function readScanSummaryFromHead(file: string, mtimeMs: number): Promise<C
let cwd = projectDirFromClaudeStorageDir(path.basename(path.dirname(file))) ?? '';
let sawTopLevelEvent = false;
let removedIdeContextWithoutTitle = false;
+ let hitLineLimitBeforeTitle = false;
let lineCount = 0;
try {
@@ -407,6 +408,7 @@ async function readScanSummaryFromHead(file: string, mtimeMs: number): Promise<C
!removedIdeContextWithoutTitle &&
!isIdeOnlyUserRecord(obj)
) {
+ hitLineLimitBeforeTitle = true;
break;
}
if (!obj || obj.isSidechain === true) continue;
@@ -422,6 +424,7 @@ async function readScanSummaryFromHead(file: string, mtimeMs: number): Promise<C
if (type === 'user' && !title && isRecord(obj.message)) {
const content = obj.message.content;
const text = extractUserText(content).trim();
+ if (text && isInternalClaudeReviewChannelText(text)) return null;
if (text) title = makeTitle(text);
else if (hasCompleteIdeOpenedFileBlock(content)) removedIdeContextWithoutTitle = true;
}
@@ -435,7 +438,7 @@ async function readScanSummaryFromHead(file: string, mtimeMs: number): Promise<C
input.destroy();
}
- if (!sawTopLevelEvent || !isLikelySessionId(sdkSessionId)) return null;
+ if (!sawTopLevelEvent || hitLineLimitBeforeTitle || !isLikelySessionId(sdkSessionId)) return null;
return {
sdkSessionId,
title: title || 'Claude Code Session',
@@ -483,6 +486,7 @@ export async function readClaudeCodeSessionSummary(file: string): Promise<Claude
if (type === 'user' && !title && isRecord(obj.message)) {
const text = extractUserText(obj.message.content).trim();
+ if (text && isInternalClaudeReviewChannelText(text)) return null;
if (text) title = makeTitle(text);
}
if (type === 'assistant' && isRecord(obj.message)) {
@@ -817,6 +821,18 @@ function hasCompleteIdeOpenedFileBlock(content: unknown): boolean {
));
}
+/** Whether the first real user message belongs to Cindy's internal review runtime. */
+function isInternalClaudeReviewChannelText(text: string): boolean {
+ const openingTag = text.trimStart().match(/^<channel\b[^>]*>/i)?.[0];
+ if (!openingTag) return false;
+
+ for (const match of openingTag.matchAll(/\bsource\s*=\s*(["'])([^"']+)\1/gi)) {
+ const source = match[2]?.trim().toLowerCase();
+ if (source === 'review-session-channel' || source === 'local-review') return true;
+ }
+ return false;
+}
+
/** Whether a scan-limit boundary row is a removable IDE-only user record. */
function isIdeOnlyUserRecord(obj: Record<string, unknown> | null): boolean {
if (!obj || obj.isSidechain === true || stringValue(obj.type) !== 'user' || !isRecord(obj.message)) {