diff --git a/src/main/codex/hook-service.test.ts b/src/main/codex/hook-service.test.ts index 62b80555b..27941a52f 100644 --- a/src/main/codex/hook-service.test.ts +++ b/src/main/codex/hook-service.test.ts @@ -74,6 +74,10 @@ function hookTrustHeader(key: string): string { return `[hooks.state."${escapeTomlBasicString(canonicalizeHookTrustKeyForTest(key))}"]` } +function literalHookTrustHeader(key: string): string { + return `[hooks.state."${escapeTomlBasicString(key)}"]` +} + function canonicalizeHookTrustKeyForTest(key: string): string { const lastColon = key.lastIndexOf(':') const secondLast = lastColon === -1 ? -1 : key.lastIndexOf(':', lastColon - 1) @@ -1050,10 +1054,37 @@ describe('CodexHookService', () => { const trustConfig = readFileSync(join(launchHome, 'config.toml'), 'utf-8') const launchHooksTrustPath = join(realpathSync.native(launchHome), 'hooks.json') - expect(trustConfig).toContain(hookTrustHeader(`${launchHooksTrustPath}:session_start:0:0`)) + expect(trustConfig).toContain( + literalHookTrustHeader(`${launchHooksTrustPath}:session_start:0:0`) + ) expect(trustConfig).toContain(hookTrustHeader(`${managedHooksPath}:session_start:0:0`)) }) + it('mirrors runtime hook trust when launch-home config is stale', () => { + const service = new CodexHookService() + expect(service.install().state).toBe('installed') + + const managedCodexHome = join(userDataDir, 'codex-runtime-home', 'home') + const managedHooksPath = join(managedCodexHome, 'hooks.json') + const launchHome = join(userDataDir, 'codex-runtime-home', 'launch', 'host', 'system', 'home') + mkdirSync(launchHome, { recursive: true }) + if (process.platform === 'win32') { + writeFileSync(join(launchHome, 'hooks.json'), readFileSync(managedHooksPath, 'utf-8')) + } else { + symlinkSync(managedHooksPath, join(launchHome, 'hooks.json')) + } + writeFileSync(join(launchHome, 'config.toml'), 'model = "stale-launch-config"\n', 'utf-8') + + trustCodexLaunchHomeHooks(launchHome) + + const trustConfig = readFileSync(join(launchHome, 'config.toml'), 'utf-8') + const launchHooksTrustPath = join(realpathSync.native(launchHome), 'hooks.json') + expect(trustConfig).toContain('model = "stale-launch-config"') + expect(trustConfig).toContain( + literalHookTrustHeader(`${launchHooksTrustPath}:session_start:0:0`) + ) + }) + it('repairs duplicate managed SessionStart trust tables on restart install', () => { const systemCodexHome = join(tmpHome, '.codex') mkdirSync(systemCodexHome, { recursive: true }) diff --git a/src/main/codex/hook-service.ts b/src/main/codex/hook-service.ts index d04985be9..99e5af8fc 100644 --- a/src/main/codex/hook-service.ts +++ b/src/main/codex/hook-service.ts @@ -518,6 +518,7 @@ function getLaunchHomeHookTrustSourcePath(launchHomePath: string): string { export function trustCodexLaunchHomeHooks(launchHomePath: string): void { const runtimeConfigPath = getConfigPath() + const runtimeTomlPath = getCodexConfigTomlPath() const launchHooksPath = join(launchHomePath, 'hooks.json') const launchTrustSourcePath = getLaunchHomeHookTrustSourcePath(launchHomePath) const tomlPath = getLaunchHomeCodexConfigTomlPath(launchHomePath) @@ -526,7 +527,10 @@ export function trustCodexLaunchHomeHooks(launchHomePath: string): void { return } - const trustEntries = readHookTrustEntries(tomlPath) + // Why: account launch homes isolate auth.json, but hook trust is shared + // runtime config. A launch config.toml can be a stale mutable copy, while + // Codex keys trust by the selected launch home's hooks.json path. + const trustEntries = readHookTrustEntries(runtimeTomlPath) const launchTrustBlocks: { key: string; trustedHash: string }[] = [] const launchTrustStates: { key: string; enabled: boolean }[] = [] for (const [eventName, definitions] of Object.entries(config.hooks)) {