Skip to content

feat(hook): add native Google Antigravity lifecycle hook - #3959

Closed
locoholy wants to merge 2 commits into
rtk-ai:developfrom
locoholy:feat/antigravity-lifecycle-hook
Closed

locoholy wants to merge 2 commits into
rtk-ai:developfrom
locoholy:feat/antigravity-lifecycle-hook

Conversation

@locoholy

@locoholy locoholy commented Sep 9, 2026

Copy link
Copy Markdown

Summary

  • hooks/antigravity/README.md and src/hooks/init.rs state that Antigravity has no programmatic hook, so rtk init --agent antigravity only writes a Markdown rule asking the model to type rtk itself. Antigravity does have one — this PR adds it.
  • New rtk hook antigravity: reads the PreToolUse payload, answers with overwrite.CommandLine, which Antigravity shallow-merges into the tool call before it executes.
  • rtk init [-g] --agent antigravity now installs a real hooks.json into the workspace root (.agents/) or the global one (~/.gemini/config/), merging into any existing file. -g was previously rejected with "Antigravity is project-scoped".

Why I opened this

I use Antigravity CLI daily for cheap agentic work and wanted RTK's savings there. Nothing was being intercepted, so I went looking and found the assumption in your source rather than a bug.

Antigravity ships its own hook documentation inside the agy binary. It is reproducible on any machine with Antigravity installed:

strings $(which agy) | sed -n '/Lifecycle Hooks/,/PostInvocation Contract/p'

Relevant excerpts (verified on agy 1.1.27, macOS arm64):

"Hooks are configured in a single hooks.json file placed in your customization root directory (e.g., .agents/hooks.json)."

PreToolUse | Before a tool step executes. | Matcher target: Tool name (e.g., run_command).

overwrite (object, optional): Key-value pairs merged into the tool call's arguments before it runs… The modified tool call is what actually executes and is recorded.

Two changelog entries in the same binary confirm both roots: one fixes /hooks writing to ~/.gemini/antigravity-cli/hooks.json "instead of the shared ~/.gemini/config/hooks.json", another fixes workspace-local hooks in <workspace>/.agents/hooks.json not loading after trusting a folder.

Evidence it works end-to-end

Not just unit tests — a real headless session that was never told about RTK:

agy --print "Run exactly one shell command: git status. Report its output."
rtk gain --history     # → a new `rtk git status` row

The model typed git status; the hook rewrote it.

Two judgment calls I'd like your read on

1. Everything not denied answers allow. Host::Antigravity carries no rules of its own (like Host::Vibe), so every command lands on Defer/AskRewrite. Mirroring run_gemini_inner_impl's ask_user looked right, but the built binary then answered {"decision":"ask"} to git status — that adds an approval prompt that did not exist before RTK was installed, and hard-fails headless runs.

allow cannot widen permissions here, and that is measured rather than assumed: the headless run above was denied while the hook answered allow, because Antigravity re-checks its own permissions rules against the rewritten call afterwards. So allow reads as "RTK has no objection", not "skip the prompt". Happy to move this behind a config toggle if you'd rather.

2. The post-rewrite permission check is a real user-facing gotcha. Because Antigravity evaluates permissions.allow against the modified tool call, an existing command(git status) rule stops matching the moment RTK prefixes the command — a surprise prompt interactively, a hard denial in CI. PreToolUse output does support permissionOverrides, which would paper over it, but emitting that unconditionally means RTK auto-granting permission for commands the user never approved. I chose not to: rtk init prints a warning and the README documents it. Your call if you'd prefer the override.

Scope

Feature + its own docs only. Two things I deliberately left out, happy to send separately:

  • src/hooks/hook_check.rs::status() only inspects Claude Code, so rtk gain warns "No hook installed" at any user whose agent is Antigravity, Gemini, Cursor or Droid. Independent defect, cross-cutting fix, wrong to bundle here.
  • hooks/codex/README.md carries the same "no programmatic hook" claim, and codex features list reports hooks stable true with a PreToolUse schema that already matches what rtk hook claude emits (hookSpecificOutput + updatedInput). Worth its own PR.

Test plan

  • cargo fmt --all && cargo clippy --all-targets -- -D warnings && cargo test — clean; 3406 tests pass, 13 of them new
  • Manual testing: rtk hook antigravity against real payloads — rewrite, passthrough for non-rewritable commands, non-run_command tools ignored, idempotent on already-prefixed input, UTF-8 BOM tolerated
  • rtk init --agent antigravity writes a valid .agents/hooks.json; a pre-existing foreign hook namespace in that file survives (covered by test_antigravity_hook_preserves_foreign_namespaces)
  • Re-running init does not duplicate handlers
  • Live confirmation in Antigravity CLI 1.1.27 as shown above

Antigravity was treated as a rules-only target on the assumption that it
has no programmatic hook. It does: `agy` dispatches a `PreToolUse`
lifecycle event and honours a handler that rewrites the tool call. The
contract ships inside the binary itself:

    strings $(which agy) | sed -n '/Lifecycle Hooks/,/PostInvocation Contract/p'

Until now `rtk init --agent antigravity` only wrote a Markdown rule
asking the model to type `rtk` itself, so savings depended on the model
remembering an instruction.

- `rtk hook antigravity` parses the camelCase `toolCall.args.CommandLine`
  payload and answers with a top-level `overwrite.CommandLine`, which
  Antigravity shallow-merges into the tool call before executing it.
- `rtk init [-g] --agent antigravity` installs a real `hooks.json` into
  the workspace root (`.agents/`) or the global one
  (`~/.gemini/config/`), claiming only the `rtk` namespace so a user's
  own hooks survive. `-g` was previously rejected outright.
- Everything RTK does not deny answers `allow`. `Host::Antigravity`
  defers permissions to the host, so every command reaches Defer/Ask;
  answering `ask` there would add an approval prompt that did not exist
  before RTK was installed and would hard-fail headless runs. `allow`
  cannot widen permissions: Antigravity re-checks its own rules against
  the rewritten call afterwards.
- Because that check runs *after* the rewrite, an existing
  `command(git status)` allow-rule stops matching once RTK prefixes the
  command. `init` now warns about this and the README documents it.

Verified end-to-end on Antigravity CLI 1.1.27: a headless `agy --print`
run told to execute `git status`, with no mention of rtk, produced a
`rtk git status` row in `rtk gain --history`.
README, the supported-agents guide and the awareness note all described
Antigravity as a prompt-level integration with no rewrite guarantee.
It now has one, plus the post-rewrite permission caveat users will hit.
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

✨ Type feature
🟡 Risk medium

Summary

Adds a native lifecycle hook for Google Antigravity CLI, replacing the previous assumption that Antigravity has no programmatic hook. It implements rtk hook antigravity to intercept and rewrite PreToolUse tool calls via the overwrite.CommandLine field, and updates rtk init --agent antigravity (including -g global mode) to install a merged hooks.json in the correct location.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@KuSh

KuSh commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

DUPLICATE, recommend closing — #3711 is the survivor

Thank you for this: the strings $(which agy) pointer is what settled the contract for both PRs, and I checked it against agy 1.2.12's embedded docs (namespaced hooks.json, overwrite.CommandLine as a shallow merge, run_command matcher, timeout in seconds). Both PRs implement that contract, and #3711 was opened first (2026-08-25) with the same hook.

Where the two differ, and why #3711 is the one being taken forward:

Two of your judgment calls are the same in #3711 and were checked there: allow for everything RTK does not deny, and no permissionOverrides, with the post-rewrite permission check documented in the init output and the README.

The two things you deliberately left out are tracked: the "No hook installed" warning for non-Claude agents is #913 (the Antigravity arm is noted there now), and the Codex hooks stable claim is worth its own issue if you want to open it.

The recommendation is to close this one in favour of #3711; the CLA is also unsigned here, which would have to be settled before anything could merge from this branch. If you see something in this design that #3711 gets wrong, please say so on #3711.

@KuSh KuSh added the duplicate This issue or pull request already exists label Sep 28, 2026
@KuSh

KuSh commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Closing as a duplicate of #3711, which has now merged (7efbaef). Thank you for the work here.

@KuSh KuSh closed this Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

antigravity duplicate This issue or pull request already exists feature hooks init

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants