Skip to content

Fix Codex hook trust hash for matcherless events (UserPromptSubmit/Stop) - #7110

Merged
nwparker merged 1 commit into
stablyai:mainfrom
m11y:fix/codex-trust-hash-matcherless-events
Jul 4, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
m11y:fix/codex-trust-hash-matcherless-events

Conversation

@m11y

@m11y m11y commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes an endless hook re-trust loop for Codex user hooks on the UserPromptSubmit and Stop events.

Symptom. A user hook registered in ~/.codex/hooks.json with "matcher": "" (a shape some third-party hook installers write on every event) can never be durably trusted inside Orca's managed CODEX_HOME. Every launch shows:

Stop hooks
1 hook needs review before it can run.
[!] Hook 2 · new
Trust    New hook - review required

Pressing t trusts it for that session, but the prompt is back on the next launch — even when the same hook is already trusted in system-level Codex.

Root cause. Codex does not support matchers on UserPromptSubmit/Stop and drops them from the hook trust identity before hashing (codex-rs hooks/src/events/common.rs, matcher_pattern_for_event: UserPromptSubmit | Stop => None; the trust check in discovery.rs is a strict hash equality). Orca's computeTrustedHash reproduces command_hook_hash but folds a present matcher into the identity for every event. For a definition carrying "matcher": "" on those two events, Orca therefore computes a hash Codex never stores, and two things go wrong on every launch:

  1. getTrustedSystemUserHookSignatures compares its expected hash against the system config's trusted_hash (which Codex wrote matcher-omitted), misses, and refuses to mirror the system-level trust into the runtime home.
  2. removeStaleRuntimeHookTrustEntries deletes the runtime trust entry that Codex itself wrote when the user pressed t (matcher-omitted, so it never matches Orca's expected hash) → the review prompt reappears next launch, forever.

The other events (SessionStart/PreToolUse/PostToolUse) are unaffected because Codex keeps the matcher there, so both sides already agree — which is exactly the asymmetry observed in the wild.

Verified by recomputing both hash variants against a real Codex 0.140 config.toml: the stored user_prompt_submit/stop hashes match only the matcher-omitted identity, while the other events' hashes match only the matcher-included identity.

Fix. Replicate matcher_pattern_for_event in config-toml-trust.ts (matcherPatternForEvent) and apply it in computeTrustedHash, so the matcher is dropped from user_prompt_submit/stop identities exactly like Codex does. The switch is written exhaustively over CodexEventLabel to satisfy lint:switch-exhaustiveness and to fail compilation if Codex adds an event label.

Screenshots

No visual change.

Testing

  • pnpm lint — fails identically on unmodified main in my environment (verify:localization-catalog wants a catalog sync unrelated to this PR, and tsgolint crashes with a Go runtime panic). Scoped checks on the two changed files pass: oxlint src/main/codex/config-toml-trust.ts src/main/codex/config-toml-trust.test.ts is clean, and pnpm run lint:switch-exhaustiveness passes across the repo.
  • pnpm typecheck — passes.
  • Affected suites: vitest run src/main/codex/config-toml-trust.test.ts src/main/codex/hook-service.test.ts — 106 passed / 7 skipped (and the trust suite alone after the final revision: 83 passed / 2 skipped).
  • Full pnpm test / pnpm build — not runnable in my environment: the native-runtime preflight requires Node 24 prebuilds (node-pty) and I'm on Node 26; failures are all Electron/node-pty environment errors also present without this change. CI should exercise these.
  • Added high-quality tests:
    • drops the matcher on user_prompt_submit/stop like matcher_pattern_for_event — equality of matcher: ''/matcher: 'anything'/absent for both events (this is the regression that caused the loop).
    • A pinned-constant test for the matcher-omitted Stop identity hash (shape observed in a real config, path anonymized), so serialization/normalization drift fails loudly — same spirit as the existing REAL_APPROVED_HASH fixture.
  • Manually verified end-to-end on a live setup: with hashes aligned, the mirrored trust entries survive removeStaleRuntimeHookTrustEntries, Codex accepts them, and the re-trust prompt is gone across launches.

AI Review Report

Review performed with Claude Code against the real Codex source as ground truth:

  • Correctness: the normalization is byte-for-byte aligned with matcher_pattern_for_event (only UserPromptSubmit/Stop drop the matcher; PreCompact/PostCompact/SessionStart/tool events keep it, including empty string). Confirmed the fix converges both failure paths (system-trust mirroring and stale-entry sweep) because both flow through computeTrustedHash.
  • Blast radius: computeTrustedHash is the single choke point for hash derivation; getTrustSignature (definition↔definition matching) intentionally untouched since both sides always come from the same hooks.json shapes. No behavior change for events where Codex keeps the matcher.
  • Cross-platform: pure string/hash logic, no paths, shells, or shortcuts involved; applies identically on macOS/Linux/Windows. Windows path-separator handling in trust keys is untouched.
  • SSH/remote: the remote hook detection path reads definitions only (no hash comparison against remote state), so behavior there is unchanged.
  • Agent/integration compatibility: Codex-specific module; no other agent's hook flow imports it.
  • Performance: one switch per hash computation — negligible.

Security Audit

  • No new input parsing, command execution, or file I/O; the change only alters which fields feed an SHA-256 identity.
  • Trust semantics get stricter in the right direction: Orca now computes the same identity Codex verifies, so it neither deletes valid trust (availability issue fixed) nor trusts anything Codex wouldn't (the mirrored hash still has to match the system config's Codex-written value).
  • Test fixtures use anonymized paths; no secrets or personal data.

Notes

  • The installer that motivated this (TMA1) also writes "matcher": "" on events that can't have one; I've sent the complementary fix upstream there too (fix(hooks/codex): omit matcher key on UserPromptSubmit/Stop entries tma1-ai/tma1#63). Orca's fix stands on its own: any hooks.json in this shape, from any installer or hand-edit, currently loops.
  • If Codex ever adds new event labels, the exhaustive switch fails typecheck/lint here, forcing a conscious decision about the new event's matcher semantics.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

computeTrustedHash() now derives identity.matcher through a new matcherPatternForEvent() helper that omits matcher data for user_prompt_submit and stop. The hash flow still uses handler normalization, canonical JSON, and SHA-256 formatting. Tests were added to verify identical hashes when matcher is omitted, empty, or non-empty for those event labels, plus a regression case that pins the expected hash for a stop entry with an empty matcher string.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title concisely describes the main change: fixing Codex hook trust hashing for matcherless UserPromptSubmit/Stop events.
Description check ✅ Passed The description follows the template with all required sections and sufficient detail on summary, testing, review, security, and notes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Codex drops the matcher from UserPromptSubmit and Stop hook identities
before hashing (codex-rs matcher_pattern_for_event), but
computeTrustedHash folded a present matcher into the identity for every
event. A hooks.json entry carrying "matcher": "" on those events —
a shape some third-party hook installers write — therefore hashed to a
value Codex never stores. The system-trust mirror then missed, and
removeStaleRuntimeHookTrustEntries deleted the trust entry Codex wrote
after the user pressed t, re-prompting the hook review on every launch.

Replicate the per-event normalization in computeTrustedHash so both
sides agree for all events.

Co-Authored-By: Claude Fable 5 <[email protected]>
@nwparker
nwparker force-pushed the fix/codex-trust-hash-matcherless-events branch from b9d7135 to 2fc2684 Compare July 3, 2026 20:56
@nwparker

nwparker commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Maintainer take-over. A maintainer has adopted this PR, independently verified it end-to-end, rebased it onto latest main, and pushed the result. It awaits final human review before merge. Thanks @m11y for the precise root-cause writeup and the upstream-aligned fix — it held up under scrutiny.

Description

Codex derives a hook's trusted_hash from a normalized identity, and matcher_pattern_for_event (codex-rs hooks/src/events/common.rs) drops the matcher for the two events that don't support one — UserPromptSubmit and Stop — before hashing. Orca's computeTrustedHash reproduced command_hook_hash but folded a present matcher into the identity for every event. So a ~/.codex/hooks.json carrying "matcher": "" on those two events (a shape some third-party installers write) made Orca compute a hash Codex never stores. Two things then broke on every launch:

  1. getTrustedSystemUserHookSignatures couldn't match its expected hash against Codex's matcher-omitted trusted_hash, so it refused to mirror system-level trust into the runtime home.
  2. removeStaleRuntimeHookTrustEntries deleted the runtime entry Codex itself wrote when the user pressed t — so the "review required" prompt returned forever.

Fix: replicate matcher_pattern_for_event as matcherPatternForEvent and apply it inside computeTrustedHash, dropping the matcher on user_prompt_submit/stop exactly as Codex does. The switch is exhaustive over CodexEventLabel, so it fails typecheck/lint if Codex adds an event. computeTrustedHash is the single choke point both failure paths flow through, so the one fix converges both. getTrustSignature is correctly left untouched — it matches system-def against runtime-def (both copied from the same hooks.json shape), never against a Codex-written hash.

Evidence

Reproduction — fix reverted (pre-PR behavior), the two new tests FAIL:

× drops the matcher on user_prompt_submit/stop like matcher_pattern_for_event
   expected 'sha256:82d61117…' to be 'sha256:bd91d3dc…'
× pins the matcher-omitted hash for a Stop entry that carries an empty matcher
   expected 'sha256:b570ca09…' to be 'sha256:f8b48c31…'
 Tests  2 failed | 2 passed | 81 skipped (85)

Independent hash recompute (standalone script, does NOT import the module):

PINNED constant in test : sha256:f8b48c31eabfba63f117b8570b839a5f6efc1d67867512d661775b5312df946f
AFTER (matcher omitted) : sha256:f8b48c31eabfba63f117b8570b839a5f6efc1d67867512d661775b5312df946f  → matches pinned
  serialized: {"event_name":"stop","hooks":[{"async":false,"command":"…/agent-hook.sh","timeout":600,"type":"command"}]}
BEFORE (matcher:"" kept): sha256:b570ca094890780e92c777a91ef8e29a4363c1c443f4d8a9cf0ada6f445455d6  → the loop-causing hash Codex never stores

The buggy sha256:b570ca09… here matches byte-for-byte the "received" value in the failing pinned test above — cross-validated.

Upstream ground truth (openai/codex codex-rs/hooks/src/events/common.rs):

match event_name {
    PreToolUse | PermissionRequest | PostToolUse | SessionStart
      | SubagentStart | SubagentStop | PreCompact | PostCompact => matcher,
    UserPromptSubmit | Stop => None,
}

The PR's mapping returns undefined for exactly user_prompt_submit/stop and the matcher for every other label Orca models — byte-for-byte aligned. (Orca doesn't model SubagentStart/SubagentStop; both keep matchers upstream, so no trust asymmetry exists for them — a separate, pre-existing gap out of scope here.)

After — fix in place, rebased onto latest main:

 Test Files  2 passed (2)
      Tests  106 passed | 7 skipped (113)
  • pnpm tc (typecheck): clean across all three tsgo projects.
  • oxlint on both changed files + pnpm run lint:switch-exhaustiveness: clean.
  • Existing REAL_APPROVED_HASH (pre_tool_use, matcher-carrying) still passes → no regression for matcher-keeping events.

ELI5

Codex needs a "fingerprint" to remember you already approved a hook. For two kinds of hooks Codex leaves one detail out of the fingerprint, but Orca was including it — so Orca's fingerprint never matched Codex's, and Orca kept forgetting your approval and asking again every single launch. This makes Orca leave that detail out too, exactly like Codex, so the approval sticks.

Trade-offs

No user-visible regression. The change only affects the identity hash for user_prompt_submit/stop — and only makes it match what Codex actually stores. Hashes for all matcher-keeping events (pre_tool_use, post_tool_use, session_start, etc.) are byte-for-byte unchanged (proven by the untouched REAL_APPROVED_HASH fixture). Trust becomes stricter in the right direction: Orca now computes the same identity Codex verifies, so it neither deletes valid trust nor trusts anything Codex wouldn't. Pure string/switch logic feeding an existing SHA-256 — no new I/O, network, shell, deps, or platform-specific paths, so macOS/Linux/Windows and SSH/remote behavior are unaffected.

Maintainer take-over — not merging; leaving the final call to a human reviewer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/main/codex/config-toml-trust.ts (1)

111-127: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider an explicit exhaustiveness guard.

The switch relies on the TS compiler flagging an added CodexEventLabel member via missing-return analysis, which only triggers when noImplicitReturns is enabled. An explicit never guard enforces the "classify every new event" intent independent of tsconfig.

♻️ Optional exhaustiveness guard
     case 'session_start':
       return matcher
+    default: {
+      const _exhaustive: never = eventLabel
+      return _exhaustive
+    }
   }
 }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 8e8a7fad-44ad-4a25-863e-db88a791d8ea

📥 Commits

Reviewing files that changed from the base of the PR and between b9d7135f407d46d36f24565187d906474ebff0b6 and 2fc2684.

📒 Files selected for processing (2)
  • src/main/codex/config-toml-trust.test.ts
  • src/main/codex/config-toml-trust.ts

@nwparker
nwparker merged commit 4c78440 into stablyai:main Jul 4, 2026
@m11y
m11y deleted the fix/codex-trust-hash-matcherless-events branch July 4, 2026 06:21
brennanb2025 pushed a commit to innocarpe/orca that referenced this pull request Aug 27, 2026
Approved system hooks still match by signature (stablyai#7110), but the
managed runtime trust entry is written with Codex's hooks/list
currentHash instead of the system trusted_hash. Both Windows
separator variants of the key are cleared before that write so
slash/backslash copies cannot stay conflicted.

Fixes stablyai#16428
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
innocarpe added a commit to innocarpe/orca that referenced this pull request Oct 29, 2026
Approved system hooks still match by signature (stablyai#7110), but the
managed runtime trust entry is written with Codex's hooks/list
currentHash instead of the system trusted_hash. Both Windows
separator variants of the key are cleared before that write so
slash/backslash copies cannot stay conflicted.

Fixes stablyai#16428
innocarpe added a commit to innocarpe/orca that referenced this pull request Nov 10, 2026
Approved system hooks still match by signature (stablyai#7110), but the
managed runtime trust entry is written with Codex's hooks/list
currentHash instead of the system trusted_hash. Both Windows
separator variants of the key are cleared before that write so
slash/backslash copies cannot stay conflicted.

Fixes stablyai#16428
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants