Repository navigation
fix(agent-hooks): write managed hook script to shared ~/.orca path - #1546
Merged
Merged
Conversation
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]>
…anaged-hook-script-path
Co-authored-by: Orca <[email protected]>
enrick-dev
pushed a commit
to enrick-dev/orca
that referenced
this pull request
Oct 15, 2026
…tablyai#1546) Co-authored-by: Orca <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Move the managed claude/codex/cursor/gemini hook script from
<userData>/agent-hooks/<agent>-hook.shto a deterministic shared path~/.orca/agent-hooks/<agent>-hook.sh, so multiple Orca installs (prod, dev, parallel branches) stop fighting over~/.claude/settings.jsonand 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.jsonis a single per-user file that pins the absolute path of the script Claude Code will run for each hook event.<userData>/agent-hooks/claude-hook.sh. So when you launch a dev build with--user-data-dir=/tmp/orca-foo-XYZ, itsinstall()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-runsinstall()and re-claims the entry.agent-status-onworktree alongside prod. Each launch wrote a fresh/tmp/orca-...path; each shutdown left the entry stale.The fix
~/.orca/agent-hooks/<agent>-hook.shis 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.envbecause 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.
createManagedCommandMatchermatches on theagent-hooks/<filename>substring, which both the legacy per-userData paths AND the new shared path contain. On the nextinstall()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
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.getSharedManagedScriptPath('claude-hook.sh')resolves to~/.orca/agent-hooks/claude-hook.shon this box.appfrom electron and no longer callapp.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-XXXXon 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.~/.orca/agent-hooks/{claude,codex,cursor,gemini}-hook.sh,0755-rwxr-xr-x, 826–841 bytes)~/.claude/settings.jsonmanaged entries point at shared path on all 6 events (UserPromptSubmit,Stop,PreToolUse,PostToolUse,PostToolUseFailure,PermissionRequest)6/6, all guarded form~/.codex/hooks.jsonshared-path entries onSessionStart/Stop/UserPromptSubmit/PreToolUse/PostToolUse5/5~/.cursor/hooks.jsonshared-path entries on all 8 cursor events8/8~/.gemini/settings.jsonshared-path entries onBeforeAgent/AfterAgent/AfterTool/PreToolUse4/4Library/Application Support/orca-dev/...strings remaining0, codex0, cursor0, gemini1(see note below)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.bakrotation only on changed content~/.claude/settings.json.bakmatches the pre-launch byte-for-byte snapshot; diff against new file is exactly the 6 script-path swaps, nothing else<userData>/agent-hooks/endpoint.envgot its own port (54738) and token; shared~/.orca/agent-hooks/claude-hook.shsources$ORCA_AGENT_HOOK_ENDPOINTso each PTY routes to the right daemon — packaged Orca's endpoint file untouchedSpawned dev instances were torn down cleanly afterward; other concurrent dev Orcas on
:9347/:9349and packaged Orca were not interfered with.One pre-existing finding unrelated to this PR:
~/.gemini/settings.jsonhas an unguarded legacy entry on thePostToolUseevent.GEMINI_EVENTSdeliberately excludesPostToolUse(seesrc/main/gemini/hook-service.ts:23-26—AfterToolalready covers "back to working"), so this entry is invisible to bothinstall()andremove()on this branch and onmain. 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)
~/.orca/agent-hooks/, then strip every entry whosecommandreferencesagent-hooks/(claude|codex|cursor|gemini)-hook.shfrom~/.claude/settings.json,~/.codex/hooks.json,~/.cursor/hooks.json,~/.gemini/settings.json. (Keep your other hooks!)ls -la ~/.orca/agent-hooks/showsclaude-hook.sh,codex-hook.sh,cursor-hook.sh,gemini-hook.sh, allchmod 755.jq '.hooks' ~/.claude/settings.jsonshows the managed command for every Claude hook event (PreToolUse, PostToolUse, PostToolUseFailure, PermissionRequest, UserPromptSubmit, Stop) using the guarded formif [ -x '/Users/<you>/.orca/agent-hooks/claude-hook.sh' ]; then /bin/sh ...; fi.exit 127/Failed with non-blocking status codelines 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)
~/.claude/settings.jsonso 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.)install()once on boot./tmp/orca-pretend-stale/...entry is gone, replaced by the new~/.orca/agent-hooks/claude-hook.shentry under the same event. The.bakfile (~/.claude/settings.json.bak) holds the prior version.C. No regression on user-authored hooks
~/.claude/settings.json, e.g. a Stop hook that runsterminal-notifier ...or your ownnotify.sh.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
pnpm devfrom a worktree that includes this PR. Both instances boot, both runinstall().~/.orca/agent-hooks/claude-hook.shregardless of which instance booted last. Run tool calls in both instances and confirm statuses route to whichever one owns the pane.E. Endpoint-file routing still works (sanity)
cat "$HOME/Library/Application Support/orca/agent-hooks/endpoint.env"shows a live PORT/TOKEN.ORCA_AGENT_HOOK_ENDPOINTin any Orca-managed PTY points at the per-instanceendpoint.envfor that instance (verify withprintenv ORCA_AGENT_HOOK_ENDPOINTfrom 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.jsonafter install: managed entry references~/.orca/agent-hooks/codex-hook.sh.~/.cursor/hooks.jsonafter install: managed entry references~/.orca/agent-hooks/cursor-hook.sh.~/.gemini/settings.jsonafter install: managed entry references~/.orca/agent-hooks/gemini-hook.sh.G. Uninstall flow (
agent-hookssettings UI → Uninstall)~/.orca/agent-hooks/<agent>-hook.shscript may still exist on disk — that is fine and intentional, sinceremove()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)
~/.orca/agent-hooks/claude-hook.cmdis created (note:.cmdnot.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.writeManagedScriptretries withgrantDirAclon EPERM, but~/.orca/is outside userData so the Chromium DACL reset shouldn't apply).Stop signs (do not merge if any of these happen)
install(). The matcher only sweepsagent-hooks/<file>paths; if it ever sweeps something else, that's a regression.~/.orca/agent-hooks/<agent>-hook.shis created with the wrong permissions (anything other than0755on POSIX).ORCA_AGENT_HOOK_ENDPOINTshould still route per-PTY to the right daemon..bakfiles under~/.claude/,~/.codex/,~/.cursor/,~/.gemini/are clobbered or rotated unnecessarily on a no-op install.