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(cross-agent-convert): 存量错误 hooks 结构也能触发修复与升级

Saurabh Pathak591bd864e024
diff --git a/apps/desktop/src/main/cross-agent-convert/__tests__/detector.test.ts b/apps/desktop/src/main/cross-agent-convert/__tests__/detector.test.ts
index f947ad3e89..fb8ccceb24 100644
--- a/apps/desktop/src/main/cross-agent-convert/__tests__/detector.test.ts
+++ b/apps/desktop/src/main/cross-agent-convert/__tests__/detector.test.ts
@@ -243,4 +243,31 @@ describe('detect — hooks', () => {
const r = await detect({ workingDir: tmpDir, agentKind: 'claude-code' });
expect(r.items.find((i) => i.kind === 'hooks')).toBeDefined();
});
+
+ it('to-claude: target has legacy nested settings.hooks.hooks → item so the repair can run', async () => {
+ await fs.mkdir(path.join(tmpDir, '.codex'), { recursive: true });
+ await fs.writeFile(path.join(tmpDir, '.codex', 'hooks.json'), JSON.stringify({ hooks: { Stop: [] } }));
+ await fs.mkdir(path.join(tmpDir, '.claude'), { recursive: true });
+ await fs.writeFile(path.join(tmpDir, '.claude', 'settings.json'), JSON.stringify({ hooks: { hooks: { Stop: [] } } }));
+ const r = await detect({ workingDir: tmpDir, agentKind: 'claude-code' });
+ expect(r.items.find((i) => i.kind === 'hooks')).toBeDefined();
+ });
+
+ it('to-codex: target has legacy flat hooks.json → item so the wrapper upgrade can run', async () => {
+ await fs.mkdir(path.join(tmpDir, '.claude'), { recursive: true });
+ await fs.writeFile(path.join(tmpDir, '.claude', 'settings.json'), JSON.stringify({ hooks: { Stop: [] } }));
+ await fs.mkdir(path.join(tmpDir, '.codex'), { recursive: true });
+ await fs.writeFile(path.join(tmpDir, '.codex', 'hooks.json'), JSON.stringify({ Stop: [] }));
+ const r = await detect({ workingDir: tmpDir, agentKind: 'codex' });
+ expect(r.items.find((i) => i.kind === 'hooks')).toBeDefined();
+ });
+
+ it('to-codex: target already has wrapped hooks.json → no item', async () => {
+ await fs.mkdir(path.join(tmpDir, '.claude'), { recursive: true });
+ await fs.writeFile(path.join(tmpDir, '.claude', 'settings.json'), JSON.stringify({ hooks: { Stop: [] } }));
+ await fs.mkdir(path.join(tmpDir, '.codex'), { recursive: true });
+ await fs.writeFile(path.join(tmpDir, '.codex', 'hooks.json'), JSON.stringify({ hooks: { Stop: [] } }));
+ const r = await detect({ workingDir: tmpDir, agentKind: 'codex' });
+ expect(r.items.find((i) => i.kind === 'hooks')).toBeUndefined();
+ });
});
diff --git a/apps/desktop/src/main/cross-agent-convert/__tests__/hooks.test.ts b/apps/desktop/src/main/cross-agent-convert/__tests__/hooks.test.ts
index 4815d80c5f..e73a6f2390 100644
--- a/apps/desktop/src/main/cross-agent-convert/__tests__/hooks.test.ts
+++ b/apps/desktop/src/main/cross-agent-convert/__tests__/hooks.test.ts
@@ -105,6 +105,17 @@ describe('convertHooks — to-codex', () => {
expect(json.hooks.SessionStart).toBeDefined();
expect(json.hooks.hooks).toBeUndefined();
});
+ it('upgrades a legacy flat hooks.json even when no event or script is added', async () => {
+ await fs.mkdir(path.join(wd, '.claude'), { recursive: true });
+ await fs.writeFile(path.join(wd, '.claude', 'settings.json'), JSON.stringify({ hooks: { Stop: [{ a: 1 }] } }));
+ await fs.mkdir(path.join(wd, '.codex'), { recursive: true });
+ await fs.writeFile(path.join(wd, '.codex', 'hooks.json'), JSON.stringify({ Stop: [{ a: 1 }] }));
+
+ const r = await convertHooks(toCodexItem());
+ expect(r.status).toBe('success');
+ const json = JSON.parse(await fs.readFile(path.join(wd, '.codex', 'hooks.json'), 'utf8'));
+ expect(json).toEqual({ hooks: { Stop: [{ a: 1 }] } });
+ });
});
describe('convertHooks — to-claude', () => {
@@ -171,4 +182,5 @@ describe('convertHooks — to-claude', () => {
expect(settings.permissions).toBe('user-keeps');
expect(settings.hooks.Stop).toBeDefined();
});
+
});
diff --git a/apps/desktop/src/main/cross-agent-convert/converters/hooks.ts b/apps/desktop/src/main/cross-agent-convert/converters/hooks.ts
index d6e6864b44..c4413c5d6f 100644
--- a/apps/desktop/src/main/cross-agent-convert/converters/hooks.ts
+++ b/apps/desktop/src/main/cross-agent-convert/converters/hooks.ts
@@ -175,6 +175,8 @@ async function convertToCodex(item: MigrationItem): Promise<ConvertOutcome> {
}
// Codex 规范:事件放在顶层 "hooks" 对象内;旧的平铺文件就地升级为包装结构
const codexEvents = extractCodexEvents(existing);
+ // 旧平铺文件(有事件但无顶层 hooks 包装)即使没有新增也要写回升级
+ const upgraded = hadExisting && !isPlainObject(existing.hooks) && Object.keys(codexEvents).length > 0;
const codexRoot: Record<string, unknown> = {};
for (const [k, v] of Object.entries(existing)) {
if (k === 'hooks' || k in codexEvents) continue;
@@ -194,7 +196,7 @@ async function convertToCodex(item: MigrationItem): Promise<ConvertOutcome> {
}
}
- if (added === 0 && scriptResult.copied === 0) {
+ if (added === 0 && scriptResult.copied === 0 && !upgraded) {
return { status: 'skipped', detail: '所有 hook 已存在' };
}
@@ -206,6 +208,7 @@ async function convertToCodex(item: MigrationItem): Promise<ConvertOutcome> {
if (scriptResult.copied > 0) parts.push(`脚本 ${scriptResult.copied}`);
if (added > 0) parts.push(`索引 ${added}`);
if (skipped > 0) parts.push(`跳过 ${skipped}`);
+ if (upgraded) parts.push('升级为 hooks 包装结构');
return { status: 'success', detail: parts.join(', ') };
}
diff --git a/apps/desktop/src/main/cross-agent-convert/detector.ts b/apps/desktop/src/main/cross-agent-convert/detector.ts
index 00fc6576de..cc56b76f42 100644
--- a/apps/desktop/src/main/cross-agent-convert/detector.ts
+++ b/apps/desktop/src/main/cross-agent-convert/detector.ts
@@ -213,7 +213,8 @@ async function detectHooks(wd: string, direction: MigrationDirection, items: Mig
const hasSourceJson = await isNonEmptyTextFile(codexHooksJson);
if (!hasSourceDir && !hasSourceJson) return;
// 目标缺失判断:claude settings.json 不存在 / 不含 hooks 段,或 .claude/hooks/ 不存在
- const claudeSettingsHasHooks = await fileHasJsonKey(claudeSettings, 'hooks');
+ // 存量错误嵌套 settings.hooks.hooks 不算完备,需放行让 converter 拍平修复
+ const claudeSettingsHasHooks = await claudeSettingsHooksWellFormed(claudeSettings);
const claudeHooksDirExists = await isDir(claudeHooksDir);
// 按源端资产对齐:源端只有 hooks.json、没有 hooks/ 目录时,目标有 hooks 配置即视为完备
if ((!hasSourceJson || claudeSettingsHasHooks) && (!hasSourceDir || claudeHooksDirExists)) return;
@@ -229,7 +230,8 @@ async function detectHooks(wd: string, direction: MigrationDirection, items: Mig
const hasSourceDir = await isDir(claudeHooksDir);
const hasSourceJson = await fileHasJsonKey(claudeSettings, 'hooks');
if (!hasSourceDir && !hasSourceJson) return;
- const codexHooksJsonExists = await isNonEmptyTextFile(codexHooksJson);
+ // 旧平铺 hooks.json 缺少顶层 hooks 包装,不算完备,需放行让 converter 就地升级
+ const codexHooksJsonExists = await codexHooksJsonWellFormed(codexHooksJson);
const codexHooksDirExists = await isDir(codexHooksDir);
if ((!hasSourceJson || codexHooksJsonExists) && (!hasSourceDir || codexHooksDirExists)) return;
items.push({
@@ -273,6 +275,33 @@ async function detectMcp(wd: string, direction: MigrationDirection, items: Migra
}
}
+/** .claude/settings.json 含 hooks 段,且不是旧版错误产物 hooks.hooks。 */
+async function claudeSettingsHooksWellFormed(p: string): Promise<boolean> {
+ try {
+ const parsed = JSON.parse(await fs.readFile(p, 'utf8'));
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return false;
+ const hooks = (parsed as Record<string, unknown>).hooks;
+ if (!hooks || typeof hooks !== 'object' || Array.isArray(hooks)) return false;
+ const nested = (hooks as Record<string, unknown>).hooks;
+ return !(nested && typeof nested === 'object' && !Array.isArray(nested));
+ } catch {
+ return false;
+ }
+}
+
+/** .codex/hooks.json 非空且不是旧平铺写法(有事件却没有顶层 hooks 包装)。 */
+async function codexHooksJsonWellFormed(p: string): Promise<boolean> {
+ try {
+ const parsed = JSON.parse(await fs.readFile(p, 'utf8'));
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return false;
+ const rec = parsed as Record<string, unknown>;
+ if (rec.hooks && typeof rec.hooks === 'object' && !Array.isArray(rec.hooks)) return true;
+ return Object.keys(rec).filter((k) => k !== 'hooks').length === 0;
+ } catch {
+ return false;
+ }
+}
+
async function fileHasJsonKey(p: string, key: string): Promise<boolean> {
try {
const raw = await fs.readFile(p, 'utf8');