Skip to content

feat: auto-compact JSON output in agent contexts#108

Merged
rhuanbarreto merged 1 commit into
mainfrom
feat/auto-compact-json-agent-context
Mar 21, 2026
Merged

feat: auto-compact JSON output in agent contexts#108
rhuanbarreto merged 1 commit into
mainfrom
feat/auto-compact-json-agent-context

Conversation

@rhuanbarreto

Copy link
Copy Markdown
Contributor

Summary

  • When stdout is not a TTY and CI env is unset, the CLI auto-switches to compact JSON (no whitespace/indentation) to reduce token usage for AI agents (~30-50% reduction)
  • Explicit --json flag still pretty-prints; --ci flag takes precedence over auto-detection
  • New src/helpers/output.ts centralizes detection (isAgentContext()) and formatting (formatJSON())

Detection precedence

--ci flag       → human-readable + GH annotations
--json flag     → pretty JSON (explicit request)
no TTY + no CI  → compact JSON (agent auto-detect)
TTY             → human-readable
CI env set      → human-readable

Files changed

  • New: src/helpers/output.ts, tests/helpers/output.test.ts, tests/preload.ts
  • Updated: all commands with --json support, always-JSON commands, reporter, integration test harness, bunfig.toml, CLAUDE.md, ARCH-003 ADR

Test plan

  • bun run validate passes (438 tests, 21/21 ADR checks, build compiles)
  • Verify agent context: echo '' | bun run src/cli.ts check outputs compact JSON
  • Verify TTY context: running bun run src/cli.ts check directly outputs human-readable
  • Verify CI context: CI=1 bun run src/cli.ts check outputs human-readable
  • Verify explicit --json: bun run src/cli.ts check --json outputs pretty JSON

When stdout is not a TTY and the CI env var is unset, the CLI now
auto-switches to compact JSON output to reduce token usage for AI
agents. Explicit --json still pretty-prints, --ci takes precedence.

- Add src/helpers/output.ts with isAgentContext() and formatJSON()
- Update all commands with --json support to auto-detect agent context
- Update always-JSON commands to use formatJSON() for compact output
- Add test preload to set CI=1 so tests don't trigger agent mode
- Update ARCH-003 ADR with new convention and Do's/Don'ts
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying archgate-cli with  Cloudflare Pages  Cloudflare Pages

Latest commit: 763a28b
Status: ✅  Deploy successful!
Preview URL: https://526a1391.archgate-cli.pages.dev
Branch Preview URL: https://feat-auto-compact-json-agent.archgate-cli.pages.dev

View logs

@rhuanbarreto rhuanbarreto merged commit 314e909 into main Mar 21, 2026
8 checks passed
@rhuanbarreto rhuanbarreto deleted the feat/auto-compact-json-agent-context branch March 21, 2026 00:54
@github-actions github-actions Bot mentioned this pull request Mar 21, 2026
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