Skip to content

[Bug]: Claude Code hooks spawn powershell.exe on every tool call (~360 ms overhead per call, even outside Orca panes) - #7131

Merged
nwparker merged 3 commits into
stablyai:mainfrom
sudoeren:fix/claude-hook-powershell-overhead
Jul 2, 2026
Merged

nwparker merged 3 commits into
stablyai:mainfrom
sudoeren:fix/claude-hook-powershell-overhead

Conversation

@sudoeren

@sudoeren sudoeren commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #7116

Claude Code hooks on Windows were wrapped in \powershell.exe -EncodedCommand, adding ~180ms per PowerShell spawn. With \PreToolUse\ + \PostToolUse\ both firing on every tool call (via \matcher: '*' ), each call paid ~360ms overhead — adding minutes of dead time over an agentic session.

Change

Follows the same approach Codex already uses (\WINDOWS_CMD_SAFE_PATH\ in \src/main/codex/hook-service.ts):

  • Safe path (no spaces, no cmd metacharacters): bare forward-slash .cmd\ path → ~60ms per hook
  • Unsafe path (spaces/special chars): PowerShell -EncodedCommand\ fallback → ~180ms (unchanged behavior)

Git Bash/MSYS executes .cmd\ files natively, so the PowerShell wrapper was unnecessary overhead for the common case.

Files changed

Verification

  • \pnpm test: 42 tests passed (39 + 3 skipped)
  • Existing oxlint failure is pre-existing (\mobile/.oxlintrc.json\ unknown rule)

Cross-platform notes

  • Windows: primary target — hooks now use bare .cmd\ path via Git Bash
  • macOS/Linux: \wrapPosixHookCommand()\ unchanged
  • SSH/remote: \getRemoteManagedCommand()\ unchanged (POSIX-only)
  • Other agents: Codex already has this optimization; other agents (Command Code, Cursor, Gemini, Grok, Droid) still use \wrapWindowsHookCommand()\ and could be updated separately

@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 84083b34-25b8-4255-8332-5fffb48dcc2e

📥 Commits

Reviewing files that changed from the base of the PR and between c1a82aba07aa6119db963ef5bba426459f6781c1 and d96b010.

📒 Files selected for processing (5)
  • src/main/agent-hooks/installer-utils.test.ts
  • src/main/agent-hooks/installer-utils.ts
  • src/main/claude/hook-service.test.ts
  • src/main/claude/hook-settings.ts
  • src/main/codex/hook-service.ts

📝 Walkthrough

Walkthrough

This change replaces a single Windows hook command wrapper with two platform-tailored wrappers: wrapWindowsCmdHookCommand for cmd.exe-safe paths and wrapWindowsGitBashHookCommand for Git Bash-safe paths, both falling back to the existing PowerShell-encoded wrapper when paths are unsafe. Codex's hook service now uses the cmd wrapper, while Claude's hook-settings uses the Git Bash wrapper. Corresponding tests are updated with new coverage for the wrappers and refactored managed-command matchers replacing platform-conditional assertions.

Changes

Cohort / File(s) Summary
Windows wrapper helpers — installer-utils.ts Added WINDOWS_CMD_SAFE_PATH, WINDOWS_GIT_BASH_SAFE_PATH regexes and wrapWindowsCmdHookCommand, wrapWindowsGitBashHookCommand functions
Wrapper test coverage — installer-utils.test.ts Renamed regex, hoisted decoder helper, added new test suites for the two wrappers
Claude hook-settings — hook-settings.ts Switched to wrapWindowsGitBashHookCommand, refactored getManagedCommand to a ternary
Codex hook-service — hook-service.ts (codex) Switched to wrapWindowsCmdHookCommand, removed inline safe-path regex logic
Claude hook-service tests — hook-service.test.ts (claude) Introduced createManagedCommandMatcher-based matchers, removed win32 branching in assertions

Sequence Diagram(s)

Not applicable; changes are localized wrapper selection and test refactors without a multi-component interaction flow.

Estimated code review effort: 3/5

Related PRs: None specified.

Suggested labels: windows, hooks, tests

Suggested reviewers: None specified.

🐰 A rabbit hopped through cmd and bash,
picked the safest path to dash,
no more one wrapper ruling all,
each shell now gets its tailored call,
tests confirm the fallback stash.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description does not follow the required template and omits several mandatory sections. Rewrite the PR description using the repository template and include Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes.
Out of Scope Changes check ⚠️ Warning The PR also changes Codex, Cursor, Gemini, Grok, and Droid hook services/tests, which are outside the Claude-specific scope of #7116. Split or remove the non-Claude hook-service changes unless they are explicitly required by the linked issue.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the Windows Claude Code hook PowerShell overhead fix.
Linked Issues check ✅ Passed The Claude hook change satisfies #7116 by using a bare safe .cmd path on Windows with fallback for unsafe paths.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

@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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/claude/hook-settings.ts (1)

77-89: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Share the Windows cmd-safe path check with Codex
This repeats the Codex cmd-safe allow-list in src/main/codex/hook-service.ts. Extract the regex/decision helper so both agents stay in sync if the accepted characters or fallback behavior changes.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f93132c1-9164-458f-9e1a-93c27f8d47d5

📥 Commits

Reviewing files that changed from the base of the PR and between dbf8086 and 568ab31.

📒 Files selected for processing (2)
  • src/main/claude/hook-service.test.ts
  • src/main/claude/hook-settings.ts

Comment thread src/main/claude/hook-settings.ts Resolved
@sudoeren

sudoeren commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the CodeRabbit nitpick: extracted the shared \WINDOWS_CMD_SAFE_PATH\ regex and \wrapHookCommand\ into \src/main/agent-hooks/installer-utils.ts.

Both Claude (\hook-settings.ts) and Codex (\hook-service.ts) now delegate to the shared \wrapHookCommand. Also updated Droid, Gemini, Command Code, Cursor, and Grok — they all had the same \wrapWindowsHookCommand\ / \wrapPosixHookCommand\ pattern and benefit from the cmd-safe fast path.

All 67 affected tests pass.

@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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/command-code/hook-service.test.ts (1)

56-61: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Test can no longer detect if the fast path silently stops firing.

Accepting either the PowerShell-launcher pattern or the bare script filename means these tests pass regardless of which path is taken. Since the entire point of this PR is to skip the PowerShell wrapper for safe paths, consider adding one assertion (with a known-safe mock path) that specifically expects the bare script path — otherwise a regression that always falls back to the launcher (defeating the ~180ms savings) would go unnoticed.

Also applies to: 88-91


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 8e2fa22f-bb03-471d-9dbc-4c68acb16aa3

📥 Commits

Reviewing files that changed from the base of the PR and between 568ab31 and c1a82aba07aa6119db963ef5bba426459f6781c1.

📒 Files selected for processing (13)
  • src/main/agent-hooks/installer-utils.ts
  • src/main/claude/hook-settings.ts
  • src/main/codex/hook-service.ts
  • src/main/command-code/hook-service.test.ts
  • src/main/command-code/hook-service.ts
  • src/main/cursor/hook-service.test.ts
  • src/main/cursor/hook-service.ts
  • src/main/droid/hook-service.test.ts
  • src/main/droid/hook-service.ts
  • src/main/gemini/hook-service.test.ts
  • src/main/gemini/hook-service.ts
  • src/main/grok/hook-service.test.ts
  • src/main/grok/hook-service.ts

Comment thread src/main/droid/hook-service.test.ts Outdated
Comment on lines +77 to +82
const command = config.hooks.PreToolUse[0].hooks[0].command
expect(
process.platform === 'win32'
? WINDOWS_POWERSHELL_LAUNCHER.test(command) || command.includes(DROID_SCRIPT_FILE_NAME)
: /droid-hook/.test(command)
).toBe(true)

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.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Relaxed assertions can no longer catch a fallback regression on unsafe (spaced) paths.

Per the line-range change details, lines 108-110 sit in "the Windows-spaces regression test loop" — the test that verifies a path containing spaces still uses the PowerShell fallback rather than a bare invocation. OR-ing in command.includes(DROID_SCRIPT_FILE_NAME) means this assertion now also passes if the implementation incorrectly treats a spaced path as "cmd-safe" and emits the bare path directly — exactly the regression this test exists to catch, since an unescaped bare path with spaces would be mis-parsed by cmd.exe/MSYS. The same weakening at 77-82 similarly means the "happy path" test can no longer detect a regression where the new bare-path optimization silently never engages (i.e., code always falls back to PowerShell).

Suggest keeping the strict launcher-only assertion for the spaces-loop case (108-110), and asserting the bare path explicitly for the safe-path case (77-82), while still allowing platform variance.

🧪 Suggested tightening
-          expect(
-            WINDOWS_POWERSHELL_LAUNCHER.test(command) || command.includes(DROID_SCRIPT_FILE_NAME)
-          ).toBe(true)
+          // Spaced paths must always fall back to the PowerShell wrapper.
+          expect(WINDOWS_POWERSHELL_LAUNCHER.test(command)).toBe(true)

Also worth noting: this appears to conflict with the PR summary's claim that "the space-in-path fallback test remains unchanged."

Also applies to: 108-110

sudoeren and others added 3 commits July 2, 2026 15:39
…er (stablyai#7116)

Claude Code hooks on Windows were wrapped in powershell.exe
-EncodedCommand, adding ~180ms per PowerShell spawn. With
PreToolUse + PostToolUse both firing on every tool call, each
call paid ~360ms overhead.

Use the bare forward-slash .cmd path directly when the path
contains no spaces or cmd metacharacters (the common case).
Git Bash/MSYS executes .cmd files natively, dropping each
hook invocation to ~60ms. Fall back to the PowerShell encoded
launcher only for paths with spaces/special chars.

Follows the same approach Codex already uses (WINDOWS_CMD_SAFE_PATH).
Move the WINDOWS_CMD_SAFE_PATH regex and safe-path fallback logic from
Codex's getManagedCommand into a shared wrapHookCommand utility in
installer-utils.ts. Update Claude, Codex, Droid, Gemini, Command Code,
Cursor, and Grok agents to delegate to the shared function, giving every
agent the ~180ms PowerShell startup skip when the hook script path is
cmd.exe-safe.
@AmethystLiang

Copy link
Copy Markdown
Contributor

looking into it

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.

[Bug]: Claude Code hooks spawn powershell.exe on every tool call (~360 ms overhead per call, even outside Orca panes)

4 participants