Skip to content

feat(layer3): run Claude meta-agent via Agent SDK#58

Merged
jonathansantilli merged 1 commit intomainfrom
feat/claude-agent-sdk
Apr 23, 2026
Merged

feat(layer3): run Claude meta-agent via Agent SDK#58
jonathansantilli merged 1 commit intomainfrom
feat/claude-agent-sdk

Conversation

@jonathansantilli
Copy link
Copy Markdown
Owner

Summary

  • Replaces the claude CLI spawn in layer 3 with @anthropic-ai/claude-agent-sdk@^0.2.118, so Claude meta-agent analysis uses the typed SDK instead of argv + stdout JSON parsing.
  • Auth keeps working the same way for users who already ran claude login — the SDK spawns the bundled Claude binary which reads ~/.claude/settings.json. ANTHROPIC_API_KEY still overrides if set.
  • Codex and generic/OpenCode stay on the existing CLI-spawn path. Codex SDK migration is deferred while openai/codex#7144 (401s from the auth fallback) is investigated.

What changed

  • src/layer3-dynamic/claude-sdk-provider.ts — new module, wraps query({ prompt, options }), maps our readOnly flag to { maxTurns, permissionMode, tools, allowedTools } that mirror the old CLI flag set, collects SDKResultSuccess.result as the stdout-shaped string so parseMetaAgentOutput keeps working unchanged, uses AbortController for the same 30s timeout budget.
  • src/layer3-dynamic/command-builder.tsMetaAgentCommand gains prompt + readOnly fields so SDK runners can pick them up without re-parsing argv. The CLI-shape preview string is unchanged, so the consent prompt still shows the familiar claude --print … command.
  • src/cli.tsrunMetaAgentCommandWithSandbox branches on context.agent.metaTool: 'claude' → SDK, everything else → existing spawn.

Test plan

  • npm test — all 722 tests pass (18 layer-3 test files, 99 layer-3 tests included)
  • npx tsc --noEmit — clean
  • Manual: run a deep scan (codegate scan --deep) on a machine with claude login already done, verify Claude path works without ANTHROPIC_API_KEY set
  • Manual: run the same scan with ANTHROPIC_API_KEY set and no CLI login, verify API-key fallback works
  • Manual: run in --read-only mode, verify the Read/Glob/Grep tools are usable and writes are blocked (plan permission mode)
  • Manual: induce a timeout (e.g. set CODEGATE_META_AGENT_TIMEOUT_MS=1), verify we get a 124 exit-shape result and don't hang

Previously layer 3's Claude path shelled out to the `claude` binary
via `spawn()`, passing the prompt as argv and parsing the
`--output-format json` stdout envelope. That worked but limited us to
whatever the CLI exposes and double-encoded the prompt through shell
argument escaping. It also obscured auth: the user had to run `claude
login` out of band and we had no visibility into which account was in
use.

Switching Claude over to `@anthropic-ai/claude-agent-sdk`:

- New provider `src/layer3-dynamic/claude-sdk-provider.ts` calls
  `query({ prompt, options })` and collects the `SDKResultSuccess.result`
  string as stdout, mapping our readOnly flag to
  `{ maxTurns, permissionMode, tools, allowedTools }` that mirrors the
  old CLI flag set (no tools / max-turns 1 for regular scans;
  Read+Glob+Grep under plan mode / max-turns 10 for read-only).
- Timeout preserved: an AbortController aborts the query after the
  same budget the sandbox spawn used (30s default, overridable).
- Auth follows the SDK's default precedence — ANTHROPIC_API_KEY if
  set, otherwise the SDK reuses whatever the bundled Claude binary
  reads from `~/.claude/settings.json`. No explicit key plumbing,
  which is the whole point.

Dispatcher in `cli.ts` routes `metaTool === 'claude'` to the SDK and
leaves Codex + generic/OpenCode on the existing spawn path. The
consent preview in `command-builder.ts` still produces the CLI-shape
preview string so the user sees a familiar command when approving the
scan; only execution changed.

Codex SDK migration is deferred — the Codex SDK's auth fallback has
a live bug (openai/codex#7144 — 401s despite fresh ~/.codex/auth.json)
that needs validation in our environment before we adopt it.
@jonathansantilli jonathansantilli merged commit 5e3c6c8 into main Apr 23, 2026
16 of 17 checks passed
@jonathansantilli jonathansantilli deleted the feat/claude-agent-sdk branch April 23, 2026 16:35
github-actions Bot pushed a commit that referenced this pull request Apr 23, 2026
# [0.16.0](v0.15.2...v0.16.0) (2026-04-23)

### Features

* **layer3:** run Claude meta-agent via Agent SDK instead of CLI spawn ([#58](#58)) ([5e3c6c8](5e3c6c8)), closes [openai/codex#7144](openai/codex#7144)
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.

1 participant