#11996: fix: CLI backend JSON array parsing and clearEnv merge
agents
stale
Cluster:
Session Management and Fixes
## 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
#21701: fix(agents): handle JSON array output from Claude Code CLI
by kenantan32 · 2026-02-20
73.6%
#2352: feat(cli): CLI feature parity phase 2 - usage tracking and streamin...
by rmorse · 2026-01-26
72.6%
#18670: feat: add first-class Claude Code CLI auth path + CLI model UX hard...
by SmithLabsLLC · 2026-02-16
70.9%
#19136: feat(claude-code): implement spawn mode for Claude Code sub-agents
by botverse · 2026-02-17
70.0%
#23720: Feat/cli backend runtime tuning
by wanmorebot · 2026-02-22
68.3%
#17770: refactor(cli): reuse shared option builders
by iyoda · 2026-02-16
67.2%
#21054: fix(cli): fix memory search hang — close undici pool + destroy QMD ...
by BinHPdev · 2026-02-19
66.9%
#16099: feat: add opencode-cli as CLI backend provider
by imwxc · 2026-02-14
66.7%
#19394: fix(agents): normalize tool call arguments dropped to {} (#19261)
by DevvGwardo · 2026-02-17
66.0%
#22591: feat(cli): expose Discord channel lifecycle management through unif...
by yinghaosang · 2026-02-21
65.9%