Skip to content

fix(antigravity): make the POSIX hook command spawnable as argv[0] - #16222

Closed
maxmilian wants to merge 2 commits into
stablyai:mainfrom
maxmilian:fix/16087-antigravity-posix-argv0
Closed

maxmilian wants to merge 2 commits into
stablyai:mainfrom
maxmilian:fix/16087-antigravity-posix-argv0

Conversation

@maxmilian

Copy link
Copy Markdown
Contributor

ELI5

Orca writes its Antigravity status hook into ~/.gemini/config/hooks.json as a little shell snippet that starts with if. Zed's Antigravity ACP host doesn't run hooks through a shell — it splits the string and execs the first word. if is a shell builtin, not a program, so the spawn fails, and ACP treats a hook that failed to start as a deny on every tool call. This hands the same snippet to /bin/sh -c so the first word is a real executable.

What Changed

  • New wrapPosixHookCommandForExec() in src/main/agent-hooks/posix-hook-command.ts — wraps the existing wrapPosixHookCommand() output in /bin/sh -c '…'.
  • src/main/antigravity/hook-service.ts uses it for getPosixManagedCommand (covers both the local and the SSH-remote install paths).
  • wrapPosixHookCommand() itself is unchanged. The other callers (codex, cursor, droid, gemini, grok, copilot, devin) keep their current output byte-for-byte.

Why

create_subprocess_exec + shlex.split means argv[0] must be a real executable. Windows already has this contract — wrapWindowsCmdHookCommand carries the comment "Codex/Antigravity/Devin spawn the hook as argv[0], not via cmd.exe, so it must be one spawnable token (#8430)". POSIX never got the twin.

The issue suggests emitting /usr/bin/env ORCA_ANTIGRAVITY_EVENT=… /bin/sh <script> instead. That is spawnable, but it drops the if [ -f/-r/-x ] guard — and with it the printf '{"decision":"ask"}' fallback that #2426 added. With the guard gone, a swept ~/.orca gives exit 127 and empty stdout, which Antigravity reads as a deny: the same bug in a new shape. Wrapping instead of rewriting keeps both properties.

Scope is deliberately narrow: only the Antigravity installer changes. Whether the other POSIX hosts want the same argv[0] contract is a separate question I raised on the issue and left for maintainers.

Linked Issue

Fixes #16087

Visual Proof

N/A — no UI or interaction surface; the change is the command string written into hooks.json.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

Three tests in src/main/antigravity/hook-service.test.ts. Note that every pre-existing POSIX test here runs the command via spawnSync('/bin/sh', ['-c', cmd]) — the path that already worked — so none of them could catch this. The new ones tokenize the command and exec argv[0], mirroring the ACP host:

  1. installs a PreToolUse command that is spawnable as argv[0] without a shell — asserts argv[0] is not if, then execs and expects {"decision":"ask"}. Reverting the fix fails it with expected 'if' not to be 'if'.
  2. keeps answering the PreToolUse gate over exec when the managed script is missing — deletes ~/.orca/agent-hooks first, pinning that fixing [Bug]: POSIX orca-status PreToolUse hook is not argv[0]-spawnable; Zed Antigravity ACP denies all tools #16087 does not regress [Bug]: Can not use antigravity cli in orac (ssh) #2426.
  3. tokenizes single-quote escapes the way a POSIX shlex does — pins the test tokenizer itself on the '\'' sequence the wrapper emits.

Cross-checked the escaping with Python's shlex (the tokenizer the issue names): the current string splits to argv[0] 'if'; the new one splits to exactly ['/bin/sh', '-c', <snippet>] with the snippet byte-identical after unquoting, and exec returns rc 0 with {"decision":"ask"}.

Also updated two pre-existing assertions that matched the raw command string: they now unwrap the /bin/sh -c payload before asserting, so they keep testing the snippet rather than the escaping. That includes the not.toContain('{"decision"') check on PostToolUse — left as-is it would have passed even if a decision leaked in.

remote-hook-service-installers.test.ts covers the SSH-remote install path, which shares getPosixManagedCommand and therefore gets the same wrapping.

Platforms: macOS (node 24). pnpm typecheck, pnpm build, and the agent-hooks / antigravity / codex hook suites (127 passed, 6 skipped) pass locally. pnpm lint in full did not complete on my machine — the linter process was killed (terminated abnormally, possibly out of memory), which looks like a local resource limit rather than anything in this diff. What did run clean: oxlint and oxfmt --check over src/main/agent-hooks/ and src/main/antigravity/ (125 files), plus the pre-commit hook's oxlint + react-doctor + oxfmt over the six changed files. Please let CI cover the remaining pnpm lint steps.

Windows paths are untouched (getManagedCommand still returns wrapWindowsCmdHookCommand on win32, and the Windows assertions in the same test file are unchanged).

I do not have a Zed + Antigravity ACP setup, so I have not verified the end-to-end tool-call recovery myself. @jrignacio, who reported the issue, offered to validate it end to end on macOS including the missing-script case.

AI Disclosure

Written with Claude Opus 5 (Claude Code). Root cause, the rejected env alternative, and the escaping were verified by hand against the source and by executing the generated commands; the tests were run locally.

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Security: no new interpolation. The script path is still single-quote escaped by the existing quotePosixShellString, and the outer layer escapes the whole snippet the same way.
  • Cross-platform: Windows unchanged; Linux/macOS both go through /bin/sh, which is required by POSIX to exist.
  • Remote SSH: getPosixManagedCommand is also the remote install path (hook-service.ts), so remote hooks.json gets the same spawnable form — the ACP host reads it identically.
  • Backwards compatibility: hosts that already ran the command through a shell (Gemini CLI, agy) now run sh -c 'sh -c …' — one extra process per hook event, same semantics. Stale entries from older versions are replaced by the existing PreToolUse replacement logic, which the pre-existing test still covers.
  • Performance: one extra sh per hook invocation on the Antigravity path only.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm typecheck, pnpm test, and pnpm build pass locally; pnpm lint ran clean over the touched directories but the full chain OOM'd locally — see Testing

Zed's Antigravity ACP host runs hook commands with shlex.split +
create_subprocess_exec, so argv[0] must be a real executable. The
installed command starts with the shell builtin `if`, so the spawn
fails with ENOENT and ACP reads a hook that never started as a deny
on every tool call.

Wrap the existing snippet in `/bin/sh -c` instead of rewriting it to
the `env ... /bin/sh <script>` form suggested on the issue: that form
is spawnable but drops the `if [ -f/-r/-x ]` guard and with it the
`{"decision":"ask"}` fallback from stablyai#2426, trading one deny-everything
bug for another.

wrapPosixHookCommand itself is unchanged, so the other POSIX hosts
keep their current output byte-for-byte.

Fixes stablyai#16087
@coderabbitai

coderabbitai Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 37e4869c-f0ac-40dd-be5f-af7d83996137

📥 Commits

Reviewing files that changed from the base of the PR and between ef82516 and 73a5cc6.

📒 Files selected for processing (2)
  • src/main/agent-hooks/posix-hook-exec-command.test-fixture.ts
  • src/main/antigravity/hook-service.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The change adds and re-exports wrapPosixHookCommandForExec. Antigravity uses this wrapper for managed POSIX hook commands. New fixtures tokenize wrapped commands and extract inner commands. Tests verify direct execution, event variables, malformed shell syntax, ask decisions, and missing-script fallback behavior.

Merge Risk: ⚪ Minimal · up to 73a5c

This localized change makes Antigravity POSIX hook commands directly spawnable while preserving existing guarded behavior, including the missing-script fallback. No actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: making the Antigravity POSIX hook command spawnable as argv[0].
Description check ✅ Passed The description covers the required sections, explains the problem and solution, documents testing and limitations, and confirms that no visual proof applies.
Linked Issues check ✅ Passed The changes satisfy issue #16087 by making Antigravity POSIX hooks directly spawnable while preserving fallback behavior for local and remote installations.
Out of Scope Changes check ✅ Passed The changes remain focused on Antigravity POSIX command wrapping, related fixtures, and tests; other POSIX hosts and Windows behavior remain unchanged.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.

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/agent-hooks/posix-hook-exec-command.test-fixture.ts (1)

1-4: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Shorten the fixture comments.

Keep each comment to one concise statement of the non-obvious test constraint.

As per coding guidelines, comments must be concise, non-obvious, and brief; prefer one line.

Proposed simplification
-// Why: Zed's Antigravity ACP host runs hook commands with `shlex.split` + `create_subprocess_exec`,
-// so the installed string is tokenized and then exec'd directly — no shell. This mirrors that
-// tokenizer (POSIX mode: no expansion inside single quotes, backslash escapes outside them) so tests
-// can exercise the exec path instead of the `/bin/sh -c` path they would otherwise take.
+// Emulate ACP POSIX tokenization for direct-exec tests.
...
-// Why: POSIX Antigravity commands are handed to `/bin/sh -c` so they stay exec-spawnable (`#16087`);
-// assertions about the shell snippet itself read the payload back out instead of matching its escaping.
+// Extract the shell payload from an exec-wrapped hook command.

Also applies to: 47-48

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3f917567-140c-425c-b2b6-daf947ed7a12

📥 Commits

Reviewing files that changed from the base of the PR and between 4ee41fe and ef82516.

📒 Files selected for processing (6)
  • src/main/agent-hooks/installer-utils.ts
  • src/main/agent-hooks/posix-hook-command.ts
  • src/main/agent-hooks/posix-hook-exec-command.test-fixture.ts
  • src/main/agent-hooks/remote-hook-service-installers.test.ts
  • src/main/antigravity/hook-service.test.ts
  • src/main/antigravity/hook-service.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

A fixture that silently accepts an unbalanced quote or a dangling
backslash would let a broken quoting change pass as green. Real
shlex raises on both, so this one does too, with regression cases
for each.

Also shortens the fixture comments per the repo's guideline.
@maxmilian

Copy link
Copy Markdown
Contributor Author

Good catch on the malformed-input case — taken in 73a5cc69f.

You're right that it matters here rather than being hypothetical: the fixture exists to prove the wrapper's quoting survives tokenization, so a version that silently guesses at an unbalanced quote would let a broken escaping change pass as green. That is the exact failure mode this PR already guards against elsewhere — one of the pre-existing assertions I had to unwrap was a not.toContain, which would have stayed green even if the decision string leaked in.

shlexSplit now throws on both an open quote and a dangling backslash, with a regression case for each. Verified the test bites: removing the unbalanced-quote throw fails rejects incomplete shell syntax instead of guessing.

Also shortened the fixture comments per the guideline.

@jrignacio

Copy link
Copy Markdown

Validated end to end at 73a5cc69f5f27529bb08a65a47f87785003a61f4.

Environment:

  • macOS 26.6.2 (25G83), arm64
  • Zed 1.16.1 (build 20260819.153150)
  • Zed antigravity-acp v1.0.0

Method:

  1. Checked out this PR, used the repo-pinned Node 24 / pnpm 10.24.0, built the CLI, and ran the focused Antigravity + remote installer tests: 35/35 passed.
  2. Ran that built CLI's real agent hooks on installer against an isolated HOME, then installed its generated ~/.gemini/config/hooks.json byte-for-byte as the active Zed config. This avoided hand-reconstructing the command.
  3. Confirmed the generated PreToolUse command tokenizes to ['/bin/sh', '-c', <guard>]. Direct exec and an outer /bin/sh -c both returned rc 0 and {"decision":"ask"} with the script present and missing.
  4. In a fresh Zed Antigravity ACP thread with the generated managed script present, asked it to run pwd and read a nonce file. Both tools completed, returned the expected cwd/content, and there was no external-hook failure.
  5. Moved the exact managed script referenced by the generated config aside, started another fresh ACP thread, and repeated both tool calls with a changed nonce. Both completed successfully again; no External hook 'orca-status' failed to execute and no hook-based denial.

So the PR closes the end-to-end gap for both the normal and stale-entry/missing-script cases on macOS + Zed ACP. I restored my original config and test files afterward.

@maxmilian

Copy link
Copy Markdown
Contributor Author

@jrignacio Thank you — this closes the one gap I flagged in the PR description ("I do not have a Zed + Antigravity ACP setup"), and it closes it more thoroughly than I could have.

Two things about your method are worth calling out for anyone reading this later:

  • You didn't hand-reconstruct the command. You ran the real agent hooks on installer against an isolated HOME and installed the generated ~/.gemini/config/hooks.json byte-for-byte as Zed's active config. That removes the "I assume the installer emits this string" layer, which is exactly where a fix like this could quietly be wrong.
  • You covered the stale-entry / missing-script case too — moving the managed script away and re-running with a fresh nonce still passed, with no External hook 'orca-status' failed to execute and no hook rejection. That's the property the second test pins down: fixing [Bug]: POSIX orca-status PreToolUse hook is not argv[0]-spawnable; Zed Antigravity ACP denies all tools #16087 must not regress the [Bug]: Can not use antigravity cli in orac (ssh) #2426 fallback. Nice to see it hold in a real ACP session rather than only in the fixture.

So as of 73a5cc69f5f27529bb08a65a47f87785003a61f4 the end-to-end behavior is verified on macOS + Zed ACP for both the normal and the stale-entry paths, and I'm treating the description's unverified-end-to-end caveat as resolved. Thanks also for restoring your config and test files afterward.

Nothing outstanding on my end — happy to rebase or split this up if that makes review easier.

@maxmilian

Copy link
Copy Markdown
Contributor Author

@nwparker — gentle nudge on this one when you have a moment. It's small (+170/−13), still mergeable, and has been validated end to end by @jrignacio on a real Zed 1.16.1 + antigravity-acp setup at the current head (73a5cc69f), which closed the only gap I'd flagged in the description. The fork workflows haven't been approved to run yet, so there are no CI results on it — happy to rebase or adjust anything if that helps it along.

@nwparker

Copy link
Copy Markdown
Contributor

Verified the wrapper from exact head 73a5cc69f5f27529bb08a65a47f87785003a61f4 on macOS with real Python shlex.split + subprocess execution, alongside the shell execution path. The script used was the currently generated Antigravity POSIX hook (including the separate stdin change in #21634), in an isolated path containing spaces, single quotes, $, and backticks. User hooks/authentication were unchanged.

ELI5: a program launcher cannot execute the shell keyword if. Explicitly launching the shell lets it interpret the guard while keeping the missing-file safety response.

Wrapper Script Direct exec Through shell
Current Present ENOENT: if exit 0, {"decision":"ask"}
PR head Present exit 0, {"decision":"ask"} exit 0, same response
Current Missing ENOENT: if exit 0, {"decision":"ask"}
PR head Missing exit 0, {"decision":"ask"} exit 0, same response

Before/after evidence, rendered in a hidden Orca terminal:

Real direct-exec and shell comparisons

This confirms the launch contract and escaping, not successful Zed/ACP tool execution. The installed agy CLI has no advertised ACP subcommand and still fails model generation with HTTP 401. Actual Zed integration, Linux/SSH and Windows validation remain outstanding. The existing PR remains the focused implementation; no duplicate PR or issue closure from this check.

Rebase note: current wrapPosixHookCommand also accepts requiredEnvVar; preserve that existing implementation when resolving its conflict. The Antigravity call does not currently pass that option. The missing-script fallback still reads to EOF; #21634 bounds the managed script's reader, not this fallback.

@nwparker

Copy link
Copy Markdown
Contributor

The verified approach is now carried onto current code in draft #21635 (stacked on #21634), with credit to this PR. It preserves the newer requiredEnvVar guard, adds executable coverage for missing/non-executable files and missing environment, and keeps local/remote installer assertions. The remote tests reuse the canonical Antigravity event list rather than exceed the file-size gate.

41 focused tests, node typecheck, changed-code quality, and pre-commit lint passed. Updated before/after evidence is attached there. Actual Zed/ACP end-to-end recovery and Linux/SSH/Windows execution remain unverified, so both issue closure and merge readiness are still pending.

@maxmilian

Copy link
Copy Markdown
Contributor Author

Thanks @nwparker for carrying this onto current code in #21635 and crediting this PR. I checked the new diff: it keeps the explicit shell launch and missing-script gate response, preserves the newer requiredEnvVar guard, and adds direct-exec coverage alongside the local and remote installer assertions. I’m closing this older PR so review can focus on #21635. The Zed/ACP validation on this PR was for 73a5cc6; the new head still needs its own integration validation as noted there.

@maxmilian maxmilian closed this Sep 19, 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.

[Bug]: POSIX orca-status PreToolUse hook is not argv[0]-spawnable; Zed Antigravity ACP denies all tools

3 participants