Files
Bertho Joris d60652a705 feat(cli): native hooks adapter for Antigravity CLI (agy) (#1146)
* feat(cli): native hooks adapter for Antigravity CLI (agy)

Antigravity ships two products with unrelated configuration: the IDE,
already wired by `connect antigravity`, and the `agy` CLI, which reads
its customizations out of ~/.gemini/ and until now was not wired at all.
This adds `connect antigravity-cli` for the latter — MCP via
~/.gemini/config/mcp_config.json, plus optional native auto-capture hooks
behind --with-hooks.

Unlike Droid (#1130), the Codex merge engine could not be reused. The
Antigravity hooks contract differs in three ways:

  * hooks.json is a map of *named* hook bundles at the root, not the
    `{ hooks: { <Event>: [...] } }` envelope, so antigravity-hooks.ts
    implements a merge that owns top-level keys instead of per-event
    entries. User-authored bundles are preserved; a re-install replaces
    only the bundle whose commands point under the bundled plugin dir.
  * only five events exist (PreToolUse, PostToolUse, PreInvocation,
    PostInvocation, Stop) — no SessionStart/SessionEnd/UserPromptSubmit,
    so the session lifecycle is synthesized from the first PreInvocation
    and from Stop. PostInvocation is left unwired to avoid double-capture.
  * the stdin payload is camelCase and nested (`toolCall.args` with
    PascalCase keys, `conversationId`, `workspacePaths`), and stdout must
    be a JSON object — `pre-tool-use.mjs` writes raw prose when context
    injection is on.

plugin/scripts/antigravity-bridge.mjs bridges all three: it normalizes the
payload onto the shape the bundled hooks already accept, maps Cascade tool
names (view_file, replace_file_content, …) onto the read/edit/write/grep
vocabulary the capture heuristics use, pipes to the right script, discards
child stdout and always answers `{}` so Antigravity's own permission
decisions are never overridden.

Event names, tool names and arg keys were verified against the shipped
agy binary rather than docs alone (docs disagree on the global hooks
path); the customization dir is ~/.gemini/config/, matching where agy
already keeps mcp_config.json and plugins/.

Signed-off-by: Bertho Joris <bertho_joris@yahoo.co.id>

* fix(cli): keep $-bearing plugin paths literal when resolving hook commands

resolveBundle() expanded ${CLAUDE_PLUGIN_ROOT} via
String.prototype.replace with a string argument, so a plugin root
containing `$$`, `$&`, "$`" or `$'` was read as a replacement pattern
and rewritten:

  C:/plug$&in  ->  C:/plug${CLAUDE_PLUGIN_ROOT}in/scripts/...
  C:/plug$$in  ->  C:/plug$in/scripts/...

`$1` and `$<name>` are unaffected — the regex has no capture groups.

Switching to a replacer function keeps the path verbatim. The failure
mode this closes is silent: the hook installs with a broken command and
auto-capture simply never fires.

Regression test builds the manifest against a temp plugin root named
`plug$&$$in` and asserts the resolved command contains it literally.

Reported by CodeRabbit on #1146.

Signed-off-by: Bertho Joris <bertho_joris@yahoo.co.id>

* fix(antigravity): emit an explicit allow decision from the PreToolUse hook

Antigravity documents `decision` as a required field of PreToolUse hook
output, and agy treats a response that omits it as a denial: the bare `{}`
the bridge used to write made the agent refuse every matched tool call
(reported against agy 1.0.5 in cmux#5358) instead of passively capturing
it. `responseFor` now answers PreToolUse with `{"decision":"allow"}` and
leaves every other event on `{}`, so no event that carries no permission
decision starts overriding the user's own settings.

The response is written from the `finally` block, so a failed capture or an
unparseable payload still produces the contract rather than empty stdout,
which PreToolUse would read the same way as `{}`.

Tests cover both the pure contract and the built bundled script running
end to end with no server listening. Also extends the ARG_KEY_MAP test to
every mapped key and pins that an explicit canonical key wins over a
PascalCase alias.

* fix(antigravity): match agy's real hooks.json schema, verified against 1.0.15

Three defects found by probing a live agy 1.0.15 with an instrumented hook,
each of which stopped the adapter from capturing anything at all.

Lifecycle events take a flat handler list, not the tool-event wrapper. agy
parses `PreToolUse`/`PostToolUse` as `[{matcher, hooks: [...]}]` but
`PreInvocation`/`PostInvocation`/`Stop` as a bare `[{type, command}]`, since
there is no tool name to match on. Wrapping a lifecycle event makes agy read
the wrapper itself as a handler and reject the *whole file* with
`invalid hook "agentmemory": command hook must specify 'command'` — so the
mis-shaped Stop entry disabled every hook in the bundle, and would have
disabled hooks other tools had written to the same file.

`command` is not run through a shell and quotes are not stripped, so the
quoted path resolved to a module name that literally began with a double
quote: `Cannot find module 'C:\Users\…\.gemini\config\"C:\…\bridge.mjs"'`.
Commands are now bare. That also means a path containing spaces cannot be
expressed at all — quoted and unquoted both fail — so the installer refuses
with an explanation instead of writing hooks that can only fail at tool time.

The merge engine reads both shapes when deciding which bundles agentmemory
owns, so a re-install over the old wrapped layout still replaces it rather
than leaving a second copy behind.

Tests pin both event shapes, the absence of quotes, the space check, and
normalization of a payload captured verbatim from the live run — which also
confirms `conversationId`, PascalCase `toolCall.args`, and that agy sends no
`cwd` key at all.

* refactor(antigravity): cut comment volume to match the sibling adapters

The bundled script carried 24 comment lines where every other script in
plugin/scripts has three. The bundler strips `//` comments but preserves
JSDoc blocks, so the fix is to document the bridge's exported helpers with
line comments: the explanations stay in source and the generated artifact
comes out as clean as its siblings.

The connect adapter and merge engine restated the same facts in a file
header and again in a per-function block. Kept one statement of each,
dropped the repetition, and left the verified agy behaviour in place since
that is the part not derivable from the code.

---------

Signed-off-by: Bertho Joris <bertho_joris@yahoo.co.id>
2026-08-03 18:35:41 +01:00

87 lines
2.3 KiB
TypeScript

import { defineConfig } from "tsdown";
const hookEntries = [
"src/hooks/session-start.ts",
"src/hooks/prompt-submit.ts",
"src/hooks/pre-tool-use.ts",
"src/hooks/post-tool-use.ts",
"src/hooks/post-tool-failure.ts",
"src/hooks/pre-compact.ts",
"src/hooks/subagent-start.ts",
"src/hooks/subagent-stop.ts",
"src/hooks/notification.ts",
"src/hooks/task-completed.ts",
"src/hooks/stop.ts",
"src/hooks/session-end.ts",
"src/hooks/post-commit.ts",
"src/hooks/antigravity-bridge.ts",
];
const shared = {
format: ["esm"] as const,
target: "node20" as const,
// Keep these as node_modules imports (deps.neverBundle). We never import
// onnxruntime-{node,web} or sharp directly; they come in transitively
// through @huggingface/transformers, which is lazy-loaded from
// src/providers/embedding/{clip,local}.ts and src/state/reranker.ts.
// Bundling inlines relative paths like
// `../bin/napi-v3/darwin/arm64/onnxruntime_binding.node` that no longer
// resolve from dist/. @huggingface/transformers is declared as an
// optionalDependency in package.json so users can install it only when
// they enable local embeddings / CLIP / reranker.
deps: {
neverBundle: [
"@huggingface/transformers",
"@anthropic-ai/claude-agent-sdk",
"@anthropic-ai/sdk",
],
},
// Each entry is its own build, so the per-entry dts/deps timing notice
// fires ~30 times and drowns the real output. It is informational only.
inputOptions: {
checks: { pluginTimings: false },
},
};
export default defineConfig([
{
entry: ["src/index.ts"],
outDir: "dist",
...shared,
dts: true,
clean: true,
sourcemap: true,
banner: { js: "#!/usr/bin/env node" },
},
{
entry: ["src/cli.ts"],
outDir: "dist",
...shared,
clean: false,
sourcemap: false,
},
{
entry: ["src/mcp/standalone.ts"],
outDir: "dist",
...shared,
clean: false,
sourcemap: false,
},
// One entry per config block prevents tsdown from hoisting shared
// helpers into hashed chunks across hooks.
...hookEntries.map((entry) => ({
entry: [entry],
outDir: "dist/hooks",
...shared,
clean: false,
sourcemap: false,
})),
...hookEntries.map((entry) => ({
entry: [entry],
outDir: "plugin/scripts",
...shared,
clean: false,
sourcemap: false,
})),
]);