Skip to content

fix(agent-hooks): write managed hook script to shared ~/.orca path - #1546

Merged
brennanb2025 merged 4 commits into
mainfrom
brennanb2025/shared-managed-hook-script-path
May 14, 2026
Merged

brennanb2025 merged 4 commits into
mainfrom
brennanb2025/shared-managed-hook-script-path

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Move the managed claude/codex/cursor/gemini hook script from <userData>/agent-hooks/<agent>-hook.sh to a deterministic shared path ~/.orca/agent-hooks/<agent>-hook.sh, so multiple Orca installs (prod, dev, parallel branches) stop fighting over ~/.claude/settings.json and friends.

This is the durable follow-up I called out in #1536's What this doesn't fix section. #1536 made the failure mode silent (no more red exit-127 spam in transcripts). This change makes the failure mode not happen at all.

The thrash, concretely

  • ~/.claude/settings.json is a single per-user file that pins the absolute path of the script Claude Code will run for each hook event.
  • Today every Orca instance writes that path as its own <userData>/agent-hooks/claude-hook.sh. So when you launch a dev build with --user-data-dir=/tmp/orca-foo-XYZ, its install() overwrites prod's entry with /tmp/orca-foo-XYZ/.... Statuses keep working until that dev shuts down (and macOS or anything else cleans /tmp), at which point the entry points at a missing file. The next prod tool call hits exit 127 (or after fix(agent-hooks): silence exit-127 spam from stale managed hook entries #1536, a silent no-op), and prod statuses don't come back until the user relaunches prod, which re-runs install() and re-claims the entry.
  • I hit this twice today running the agent-status-on worktree alongside prod. Each launch wrote a fresh /tmp/orca-... path; each shutdown left the entry stale.

The fix

~/.orca/agent-hooks/<agent>-hook.sh is the same string regardless of which Orca instance writes it. The script body is identical across versions — it just sources $ORCA_AGENT_HOOK_ENDPOINT (set by the launching Orca per-PTY) to find the live port/token, which means the hook always reaches the daemon that owns the pane. So two installs rewriting the same path is a no-op rather than a conflict.

The endpoint file itself stays at <userData>/agent-hooks/endpoint.env because each instance genuinely needs its own port/token — that's the per-instance concern, and it's already routed correctly via the env var.

Migration

Free. createManagedCommandMatcher matches on the agent-hooks/<filename> substring, which both the legacy per-userData paths AND the new shared path contain. On the next install() from any patched Orca, legacy entries get swept and replaced with the shared entry. No version-skew dance, no migration hook, no cleanup task.

What this doesn't fix

  • Users who never relaunch (or who run two unpatched Orca builds against each other) keep thrashing until at least one install ships this commit. After that, only the patched instance heals the entry — but once any patched instance has run, the entry is stable across all future installs since they all write the same path.
  • Endpoint-file racing across instances is unaffected (already per-instance and already correct).

Test plan

Automated

  • pnpm vitest run src/main/agent-hooks/installer-utils.test.ts — 20/20 pass, including the new regression test confirming the matcher sweeps both legacy and new path forms.
  • Confirmed getSharedManagedScriptPath('claude-hook.sh') resolves to ~/.orca/agent-hooks/claude-hook.sh on this box.
  • Confirmed all 4 hook-services no longer import app from electron and no longer call app.getPath('userData') for the script path.

End-to-end verification (live, this branch @ da2a7b0b, macOS)

Launched a patched dev Orca with isolated ORCA_DEV_USER_DATA_PATH=/tmp/orca-pr1546-userdata-XXXX on CDP :9352, against a real pre-existing config state that already held legacy <userData>/agent-hooks/... entries and several user-authored hooks (terminal-notifier, /hooks/notify.sh, codex-title-hook.py, cursor-hook.sh Start|Stop|PermissionRequest, notify-gemini.py, gemini-hook.sh). Single launch exercised migration + no-regression at the same time.

Check Result
Scripts in ~/.orca/agent-hooks/{claude,codex,cursor,gemini}-hook.sh, 0755 ✓ all four (-rwxr-xr-x, 826–841 bytes)
~/.claude/settings.json managed entries point at shared path on all 6 events (UserPromptSubmit, Stop, PreToolUse, PostToolUse, PostToolUseFailure, PermissionRequest) ✓ 6/6, all guarded form
~/.codex/hooks.json shared-path entries on SessionStart/Stop/UserPromptSubmit/PreToolUse/PostToolUse ✓ 5/5
~/.cursor/hooks.json shared-path entries on all 8 cursor events ✓ 8/8
~/.gemini/settings.json shared-path entries on BeforeAgent/AfterAgent/AfterTool/PreToolUse ✓ 4/4
Migration sweep — legacy Library/Application Support/orca-dev/... strings remaining claude 0, codex 0, cursor 0, gemini 1 (see note below)
User-authored hooks preserved ✓ terminal-notifier (Notification + Stop), /hooks/notify.sh (5 events), codex-title-hook.py (SessionStart/Stop), /Users/thebr/hooks/notify.sh (codex), `cursor-hook.sh Start
.bak rotation only on changed content ✓ ~/.claude/settings.json.bak matches the pre-launch byte-for-byte snapshot; diff against new file is exactly the 6 script-path swaps, nothing else
Per-instance endpoint routing intact ✓ isolated dev's <userData>/agent-hooks/endpoint.env got its own port (54738) and token; shared ~/.orca/agent-hooks/claude-hook.sh sources $ORCA_AGENT_HOOK_ENDPOINT so each PTY routes to the right daemon — packaged Orca's endpoint file untouched

Spawned dev instances were torn down cleanly afterward; other concurrent dev Orcas on :9347/:9349 and packaged Orca were not interfered with.

One pre-existing finding unrelated to this PR: ~/.gemini/settings.json has an unguarded legacy entry on the PostToolUse event. GEMINI_EVENTS deliberately excludes PostToolUse (see src/main/gemini/hook-service.ts:23-26 — AfterTool already covers "back to working"), so this entry is invisible to both install() and remove() on this branch and on main. It was either hand-added or seeded by a pre-#1019 build. Not a regression of this PR; flagging only so a reviewer doesn't trip on it during step F below.

Manual checks before merging / on first build with this change

A. Fresh-install path (the happy path)

  1. Wipe state: delete ~/.orca/agent-hooks/, then strip every entry whose command references agent-hooks/(claude|codex|cursor|gemini)-hook.sh from ~/.claude/settings.json, ~/.codex/hooks.json, ~/.cursor/hooks.json, ~/.gemini/settings.json. (Keep your other hooks!)
  2. Launch the patched Orca.
  3. Verify on disk: ls -la ~/.orca/agent-hooks/ shows claude-hook.sh, codex-hook.sh, cursor-hook.sh, gemini-hook.sh, all chmod 755.
  4. Verify settings.json: jq '.hooks' ~/.claude/settings.json shows the managed command for every Claude hook event (PreToolUse, PostToolUse, PostToolUseFailure, PermissionRequest, UserPromptSubmit, Stop) using the guarded form if [ -x '/Users/<you>/.orca/agent-hooks/claude-hook.sh' ]; then /bin/sh ...; fi.
  5. Run a Claude tool call in any pane. Confirm: (a) no exit 127 / Failed with non-blocking status code lines in the Claude transcript, (b) the sidebar agent status updates (PreToolUse → PostToolUse → Stop visible in the Orca UI).

B. Migration path (existing users get healed automatically)

  1. Seed a stale state: with the patched Orca closed, hand-edit ~/.claude/settings.json so the Stop event holds a managed command pointing at a <userData> path that no longer exists, e.g. /bin/sh "/tmp/orca-pretend-stale/agent-hooks/claude-hook.sh". (Or just rerun an unpatched dev build first to seed it naturally.)
  2. Launch the patched Orca. It runs install() once on boot.
  3. Verify settings.json: the stale /tmp/orca-pretend-stale/... entry is gone, replaced by the new ~/.orca/agent-hooks/claude-hook.sh entry under the same event. The .bak file (~/.claude/settings.json.bak) holds the prior version.
  4. Run a tool call. No spam, statuses flow.

C. No regression on user-authored hooks

  1. Add a non-managed hook by hand to ~/.claude/settings.json, e.g. a Stop hook that runs terminal-notifier ... or your own notify.sh.
  2. Launch the patched Orca.
  3. Confirm the user-authored hook entry is still present in settings.json (the matcher only sweeps agent-hooks/<file> paths). Run a tool call and confirm the user-authored hook still fires alongside the managed one.

D. Multi-instance, no thrash

  1. With the patched Orca prod running, also pnpm dev from a worktree that includes this PR. Both instances boot, both run install().
  2. Verify settings.json: there is exactly one managed claude entry per event, and it is ~/.orca/agent-hooks/claude-hook.sh regardless of which instance booted last. Run tool calls in both instances and confirm statuses route to whichever one owns the pane.
  3. Quit the dev. Run a tool call in prod. No spam, statuses still flow — this is the regression we are explicitly killing.

E. Endpoint-file routing still works (sanity)

  1. With prod running, cat "$HOME/Library/Application Support/orca/agent-hooks/endpoint.env" shows a live PORT/TOKEN.
  2. The exported ORCA_AGENT_HOOK_ENDPOINT in any Orca-managed PTY points at the per-instance endpoint.env for that instance (verify with printenv ORCA_AGENT_HOOK_ENDPOINT from inside an Orca terminal). The hook script under ~/.orca/agent-hooks/ will source whatever path that env var points at, so dev panes route to the dev daemon and prod panes route to the prod daemon — even though the script is shared.

F. Cross-agent coverage

Repeat A–B for codex, cursor, gemini if you use them. Specifically:

  • ~/.codex/hooks.json after install: managed entry references ~/.orca/agent-hooks/codex-hook.sh.
  • ~/.cursor/hooks.json after install: managed entry references ~/.orca/agent-hooks/cursor-hook.sh.
  • ~/.gemini/settings.json after install: managed entry references ~/.orca/agent-hooks/gemini-hook.sh.

G. Uninstall flow (agent-hooks settings UI → Uninstall)

  1. From the Orca settings UI, click Uninstall for each agent's hooks.
  2. Verify: the managed entry is removed from the corresponding config file. Other (user-authored) hooks remain. The ~/.orca/agent-hooks/<agent>-hook.sh script may still exist on disk — that is fine and intentional, since remove() only sweeps the JSON entry, not the script. (If you re-install, it gets reused.)

H. Windows (only if you're testing on a Windows box)

  1. Repeat A. Confirm ~/.orca/agent-hooks/claude-hook.cmd is created (note: .cmd not .sh) and that settings.json holds the forward-slash-normalized path (C:/Users/.../.orca/agent-hooks/claude-hook.cmd) so Git Bash can launch it.
  2. Confirm no EPERM errors at first launch (writeManagedScript retries with grantDirAcl on EPERM, but ~/.orca/ is outside userData so the Chromium DACL reset shouldn't apply).

Stop signs (do not merge if any of these happen)

  • A user-authored, non-managed hook entry is removed or modified by install(). The matcher only sweeps agent-hooks/<file> paths; if it ever sweeps something else, that's a regression.
  • ~/.orca/agent-hooks/<agent>-hook.sh is created with the wrong permissions (anything other than 0755 on POSIX).
  • Status reporting is intermittent or pane-leaked across instances — the script is shared but ORCA_AGENT_HOOK_ENDPOINT should still route per-PTY to the right daemon.
  • Existing .bak files under ~/.claude/, ~/.codex/, ~/.cursor/, ~/.gemini/ are clobbered or rotated unnecessarily on a no-op install.

brennanb2025 and others added 4 commits May 7, 2026 15:05
Each Orca instance previously wrote its managed hook script under
`<userData>/agent-hooks/<agent>-hook.sh` and embedded that absolute
path into `~/.claude/settings.json` (and the codex/cursor/gemini
equivalents). Because there is exactly one such config file per agent
across all Orca installs, every instance fought to be the last one
that wrote it: prod's path won until a dev launch overwrote it, and
the next time prod started it overwrote dev's path. Whichever
userData was wiped, moved, or running on a tmp dir cleaned by macOS
left a stale entry that fired `/bin/sh "<missing>"` on every tool
call (the bug PR #1536 silenced).

Move the script to a deterministic shared path
(`~/.orca/agent-hooks/<agent>-hook.sh`) so every Orca install writes
the same JSON entry. The script body is identical across versions
(it sources the per-PTY env var ORCA_AGENT_HOOK_ENDPOINT to find the
live port/token), so two instances rewriting the same path is a
no-op rather than a conflict. The endpoint file itself stays
per-userData since each instance needs its own port/token.

Migration is automatic: createManagedCommandMatcher already matches
on the `agent-hooks/<filename>` substring, which the legacy
per-userData paths AND the new shared path both contain. On the next
install() from any patched Orca, the legacy entry is swept and
replaced with the shared entry.

Tests: added regression coverage for matcher sweeping both old and
new path forms, plus assertions that getSharedManagedScriptPath is
deterministic (does not depend on Electron app.getPath) so a future
refactor cannot silently re-introduce the multi-instance thrash.

Co-authored-by: Orca <[email protected]>
Now that managed hook scripts live at a single shared `~/.orca/agent-hooks/`
path, two Orca instances calling `install()` concurrently both target the
same file. The previous `writeFileSync` (with O_TRUNC) was not atomic, so
a `/bin/sh <scriptPath>` fired by an in-flight tool call could read a torn
mid-write script. Mirror the temp+rename pattern already used by
`writeHooksJson`: skip the write if on-disk bytes already match, otherwise
write to a tmp path, chmod-before-rename so the canonical path is never
visible in a non-executable state (which `wrapPosixHookCommand`'s `[ -x ]`
guard would silently skip), then rename atomically.

Also fix the cross-platform `getSharedManagedScriptPath` test (was
hardcoding forward slashes — would fail on Windows CI), merge the
duplicate `os` import, and update the now-stale `writeScriptWithAclRetry`
comment that cited userData-specific Chromium DACL behavior no longer
relevant to the new path.

Co-authored-by: Orca <[email protected]>
@brennanb2025
brennanb2025 merged commit 727226f into main May 14, 2026
2 checks passed
@brennanb2025
brennanb2025 deleted the brennanb2025/shared-managed-hook-script-path branch May 14, 2026 18:57
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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.

1 participant