Skip to main content
Glama
README.md
# agy-subagent

`agy-subagent` is a Claude Code plugin that runs Google Antigravity (`agy`)
Gemini 4 and Orchestrator models as warm, multi-turn subagents behind one MCP
tool (`agy_run`).

Claude Code's native subagents run Anthropic models only. When you want a
Gemini code review, a large-context read, or a web-grounded `search_web`
lookup, `agy-subagent` bridges MCP stdio to a single lazily spawned
`agy acp --sandbox` process. Reusing `session_id` keeps prompt caches warm
across turns and recovers transparently after a child crash.

## Prerequisites

- **Node.js >= 20**
- **`agy` CLI** on `PATH`, authenticated via Google OAuth (`agy`)

## Installation

Add the marketplace and install the plugin:

```bash
claude plugin marketplace add davdittrich/agy-subagent
claude plugin install agy-subagent@agy-subagent
```

The repository ships a committed CommonJS bundle (`dist/server.cjs`), so
installation needs no `npm install` or local build.

## Architecture

Three TypeScript modules separate pure routing logic, subprocess lifecycle,
and MCP stdio framing:

```mermaid
flowchart LR
    CC[Claude Code] <-->|MCP stdio| S["src/server.ts (dist/server.cjs)"]
    S -->|run / shutdown| A["AcpRunner (src/acp.ts)"]
    A <-->|pure rules| R["src/routing.ts"]
    A <-->|ACP NDJSON| AGY["agy acp --sandbox / tests/fake-acp.mjs"]
```

### Request Flow

1. **Lazy spawn & handshake:** On the first `agy_run` call, `AcpRunner` spawns
   one `agy acp --sandbox` child and sends `initialize`. Subsequent turns and
   concurrent sessions share that child until MCP `stdin` closes.
2. **Warm follow-ups & crash recovery:** Passing `session_id` reuses the cached
   session directly. If the child exited earlier, `AcpRunner` spawns a fresh
   child, runs `session/load`, drops replayed history notifications, and
   re-applies `model` and `thought_level`.
3. **Sibling-safe cancellation:** When a caller aborts or `timeout_ms` expires
   while `session/prompt` runs, `AcpRunner` sends `session/cancel` and waits
   for `AGY_SUBAGENT_CANCEL_GRACE_MS`. If the child stalls while sibling turns
   are still running, `AcpRunner` retires that child so healthy siblings finish
   and new calls spawn a fresh process.

### Code References

| Component | File | Key Symbols |
| --- | --- | --- |
| Pure routing & error rules | `src/routing.ts` | `validateRequest`, `resolveModel`, `resolveThoughtLevel`, `classifyError` |
| Deep ACP runner | `src/acp.ts` | `AcpRunner`, `ChildConn` |
| MCP stdio adapter | `src/server.ts` | `McpServer`, `StdioServerTransport` |
| Committed bundle | `dist/server.cjs` | Bundled CommonJS entry point |
| Routing skill | `skills/agy-subagent/SKILL.md` | Model routing & capability notice |

## Model Routing

`agy_run` accepts a 3-way `task_type` (`search`, `review`, `code`, defaulting to
`code` on turn 1), an optional `model` ID or alias, and an optional
`thought_level` (`low`, `medium`, `high`, `xhigh`). Only live `^gemini-` models
are allowed.

| `task_type` | Model Fallback Ladder | Default `thought_level` |
| --- | --- | --- |
| `search` | `gemini-4-hydrogen` -> `gemini-4-neon` -> `gemini-4-carbon` | `medium` |
| `review` | `gemini-4-carbon` -> `gemini-4-neon` -> `gemini-4-hydrogen` | `high` |
| `code` | `gemini-4-neon` -> `gemini-4-carbon` -> `gemini-4-hydrogen` | `high` |

Model aliases: `hydrogen` (`gemini-4-hydrogen`), `neon` (`gemini-4-neon`),
`carbon` (`gemini-4-carbon`), and `orchestrator` (`gemini-pro-orchestrator-neon`
with fallback `gemini-pro-orchestrator`).

## Capabilities & Permissions

By default, `agy_run` answers `allow_once` to `session/request_permission`
callbacks (`allow_always` is never selected). The subagent can:

- Read and edit files in the workspace
- Run sandboxed shell commands
- Call external MCP tools configured in `agy`

To reject `execute` permissions (`reject_once`, failing closed to `cancelled`):

- Set `AGY_SUBAGENT_DENY_READONLY_EXEC=1` to deny `execute` on `search`,
  `review`, and uncached cold-resume calls (`code` stays allowed unless
  `deny_exec: true`).
- Pass `deny_exec: true` per call to deny `execute` on any `task_type`, or
  `deny_exec: false` to override the environment variable.

## Configuration

| Environment Variable | Default | Description |
| --- | --- | --- |
| `AGY_SUBAGENT_ACP_CMD` | `["agy", "acp", "--sandbox"]` | JSON string array overriding the ACP subprocess command. |
| `AGY_SUBAGENT_DENY_READONLY_EXEC` | unset | Set to `1` to deny `execute` on non-`code` turns. |
| `AGY_SUBAGENT_CANCEL_GRACE_MS` | `5000` | Grace window in ms after `session/cancel` before retiring `agy`. |
| `AGY_SUBAGENT_LOG_FILE` | unset | Optional path to append JSONL lifecycle events (never logs prompts). |

## Development

```bash
npm install
npm run ci
```

The test suite drives `dist/server.cjs` across the real MCP stdio boundary
against `tests/fake-acp.mjs` via `AGY_SUBAGENT_ACP_CMD`. After editing `src/`,
run `npm run build` (or `npm test`) to keep `dist/server.cjs` current.