Fix Codex launch-home hook trust repair (#4348)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -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 })
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user