Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix: append codex hooks to preserve trust

leonguyene265f9df8ccc
diff --git a/src/core/agents/hooks/codex.test.ts b/src/core/agents/hooks/codex.test.ts
index f852454ad..c0bb001dd 100644
--- a/src/core/agents/hooks/codex.test.ts
+++ b/src/core/agents/hooks/codex.test.ts
@@ -18,15 +18,15 @@ describe('buildCodexHooksAndTrust', () => {
expect(buildCodexHooksAndTrust(null, 'cmd', '/h/hooks.json')).toBeNull()
})
- it('prepends the managed handler to all six events + emits one trust entry per event', () => {
+ it('appends the managed handler to all six events + emits one trust entry per event', () => {
const command = buildManagedCommand('/home/u/.nodeterm/agent-hooks/codex.sh')
const built = buildCodexHooksAndTrust({}, command, '/home/u/.codex/hooks.json')
expect(built).not.toBeNull()
const { config, trustEntries } = built!
- // one definition per subscribed event, our managed handler first
+ // one definition per subscribed event, our managed handler last
for (const ev of CODEX_EVENTS) {
const defs = config.hooks?.[ev]
- expect(defs?.[0]?.hooks?.[0]?.command).toBe(command)
+ expect(defs?.at(-1)?.hooks?.[0]?.command).toBe(command)
}
expect(trustEntries).toHaveLength(CODEX_EVENTS.length)
expect(trustEntries.every((e) => e.command === command)).toBe(true)
@@ -38,20 +38,40 @@ describe('buildCodexHooksAndTrust', () => {
const first = buildCodexHooksAndTrust({}, command, '/x/hooks.json')!
const second = buildCodexHooksAndTrust(first.config, command, '/x/hooks.json')!
for (const ev of CODEX_EVENTS) {
- // exactly one managed handler at the head, none trailing
+ // exactly one managed handler, still at the tail
const defs = second.config.hooks?.[ev] ?? []
const managed = defs.filter((d) => d.hooks?.some((h) => h.command === command))
expect(managed).toHaveLength(1)
+ expect(defs.at(-1)?.hooks?.[0]?.command).toBe(command)
}
})
- it('preserves a user-authored hook in a subscribed event (kept AFTER the managed handler)', () => {
+ it('preserves a user-authored hook at its original index before the managed handler', () => {
const command = buildManagedCommand('/x/agent-hooks/codex.sh')
const userDef = { hooks: [{ type: 'command' as const, command: 'echo mine' }] }
const built = buildCodexHooksAndTrust({ hooks: { Stop: [userDef] } }, command, '/x/hooks.json')!
const stop = built.config.hooks?.Stop ?? []
- expect(stop[0]?.hooks?.[0]?.command).toBe(command)
- expect(stop.some((d) => d.hooks?.some((h) => h.command === 'echo mine'))).toBe(true)
+ expect(stop[0]).toEqual(userDef)
+ expect(stop.at(-1)?.hooks?.[0]?.command).toBe(command)
+ })
+
+ it('keeps two user definitions at their trust-key indices and trusts the managed tail', () => {
+ const command = buildManagedCommand('/x/agent-hooks/codex.sh')
+ const firstUserDef = { hooks: [{ type: 'command' as const, command: 'echo first' }] }
+ const secondUserDef = { hooks: [{ type: 'command' as const, command: 'echo second' }] }
+ const built = buildCodexHooksAndTrust(
+ { hooks: { SessionStart: [firstUserDef, secondUserDef] } },
+ command,
+ '/x/hooks.json'
+ )!
+ const sessionStart = built.config.hooks?.SessionStart ?? []
+ expect(sessionStart[0]).toEqual(firstUserDef)
+ expect(sessionStart[1]).toEqual(secondUserDef)
+ expect(sessionStart[2]?.hooks?.[0]?.command).toBe(command)
+ expect(built.trustEntries.find((entry) => entry.eventLabel === 'session_start')).toMatchObject({
+ groupIndex: 2,
+ handlerIndex: 0
+ })
})
it('sweeps a stale managed handler out of an event we no longer subscribe to', () => {
diff --git a/src/core/agents/hooks/codex.ts b/src/core/agents/hooks/codex.ts
index 324e3ca2a..02c05273a 100644
--- a/src/core/agents/hooks/codex.ts
+++ b/src/core/agents/hooks/codex.ts
@@ -160,19 +160,20 @@ export function buildCodexHooksAndTrust(
}
}
- // Prepend our managed handler to each subscribed event (idempotent — strip any
- // prior managed copy first) and record the matching trust entry at
- // groupIndex 0 / handlerIndex 0.
+ // Why: Codex keys hook trust by index, so prepending shifts user hooks out
+ // from under their stored hashes and silently disables them. Append our
+ // managed handler to keep existing indices stable (idempotent — strip any
+ // prior managed copy first) and trust its actual index.
const trustEntries: CodexTrustEntry[] = []
for (const eventName of CODEX_EVENTS) {
const current = Array.isArray(nextHooks[eventName]) ? nextHooks[eventName] : []
const cleaned = removeManagedFromDefinitions(current)
const definition: HookDefinition = { hooks: [{ type: 'command', command }] }
- nextHooks[eventName] = [definition, ...cleaned]
+ nextHooks[eventName] = [...cleaned, definition]
trustEntries.push({
sourcePath,
eventLabel: CODEX_EVENT_LABEL[eventName],
- groupIndex: 0,
+ groupIndex: cleaned.length,
handlerIndex: 0,
command
})
@@ -263,9 +264,12 @@ export function installCodexHooks(): void {
}
try {
- // Prepending keeps the status hook ahead of any user hooks so a slow user
- // hook can't leave the badge stale. The trust entry is keyed by the local
- // hooks.json path (computeTrustKey realpath's it to match how codex keys it).
+ // Why: appending keeps user-hook indices stable so their existing Codex
+ // trust entries remain valid. The cost is the one prepending used to buy —
+ // a slow user hook ahead of ours can delay the badge — which is the lesser
+ // failure next to silently disabling the user's whole hook chain. The
+ // managed trust entry is keyed by the local hooks.json path (computeTrustKey
+ // realpath's it to match how codex keys it).
const built = buildCodexHooksAndTrust(config, command, hooksFile)
if (!built) return
writeHooksJson(hooksFile, built.config)