← Back to PRs

#11996: fix: CLI backend JSON array parsing and clearEnv merge

by shanemmattner open 2026-02-08 17:42 View on GitHub →
agents stale
## Summary - **Parse JSON array output**: CLI backends like Claude Code (`claude -p --output-format json`) return a JSON array `[{init}, {assistant}, {result}]` instead of a single object. `parseCliJson` now iterates array entries to extract text, session ID, and usage — matching the same priority order as the single-object path (message → content → result). - **Fix `clearEnv` merge**: Config override now replaces base instead of union-merging. This allows `clearEnv: []` to actually clear the defaults, which is needed when the CLI subprocess should inherit env vars or use its own auth (e.g. OAuth). - **Add `toolsEnabled` option**: Skips "Tools are disabled" system prompt injection when the CLI backend handles its own tool execution. - **Add `extraArgs` config option**: Allows appending additional CLI arguments from backend config. ## Motivation When using `claude -p` as a CLI backend with `--tools "" --mcp-config`, the overhead is ~951 tokens. With CC built-in tools (`--tools Bash,Read,Edit,Write,Glob,Grep`), it's ~15,943 tokens. These changes enable lightweight MCP-based setups for Discord bots and other integrations. ``` Config TOTAL tokens ------------------------------------------------------- No tools (baseline) 123 MCP 4 tools (--tools "") 951 ← enabled by this PR CC: Bash only 3,849 CC: Bash,Read,Grep,Edit 5,923 CC: all defaults 15,943 ← before this PR ``` ## Test plan - [x] Unit tests for `parseCliJson` (array input, object input, edge cases) - [x] Unit tests for `clearEnv` merge (override replaces, empty clears) - [x] Unit tests for `buildCliArgs` with `extraArgs` - [x] `pnpm build && pnpm check && pnpm test` — all 247 tests pass - [x] E2E: Discord bot responds with clean text via Claude CLI runtime at ~951 tokens ## AI disclosure 🤖 AI-assisted (Claude Code / Opus 4.6). Fully tested locally and E2E via Discord bot integration. 🤖 Generated with [Claude Code](https://claude.com/claude-code)

Most Similar PRs