8.8 KiB
CLI --json Output Standardization
Overview
Add standardized --json flag support across all CLI commands that return structured data or action results. When --json is passed, the command outputs a single line of JSON to stdout and exits with an appropriate exit code. This enables automation, scripting, and app integration.
JSON Envelope
All commands use a consistent envelope:
Success (exit 0):
{ "success": true, "data": { ... } }
Error (exit non-zero):
{ "success": false, "error": { "code": "ERROR_CODE", "message": "Human-readable description" } }
Rules:
- Output is always a single line of valid JSON on stdout
- Exit code 0 for success, non-zero for errors
- No ANSI escape codes, progress bars, spinners, or other text in stdout when
--jsonis active
Core Module: src/cli/output.ts
Types
interface JsonSuccess<T = unknown> {
success: true;
data: T;
}
interface JsonError {
success: false;
error: {
code: string;
message: string;
};
}
type JsonOutput<T = unknown> = JsonSuccess<T> | JsonError;
Functions
function isJsonMode(args: string[]): boolean;
function jsonSuccess(data: unknown): never; // JSON to stdout + process.exit(0)
function jsonError(code: string, message: string): never; // JSON to stdout + process.exit(1)
function muteForJson(): void; // Mute pino logger to suppress stderr noise
jsonSuccess and jsonError are terminal — they call process.exit() and never return. This naturally prevents any subsequent console.log noise.
Error Codes
const ErrorCodes = {
DAEMON_NOT_RUNNING: 'DAEMON_NOT_RUNNING',
INSTANCE_NOT_FOUND: 'INSTANCE_NOT_FOUND',
PLUGIN_NOT_FOUND: 'PLUGIN_NOT_FOUND',
AGENT_NOT_FOUND: 'AGENT_NOT_FOUND',
CONFIG_INVALID: 'CONFIG_INVALID',
CONFIG_NOT_FOUND: 'CONFIG_NOT_FOUND',
SETUP_FAILED: 'SETUP_FAILED',
API_ERROR: 'API_ERROR',
TUNNEL_ERROR: 'TUNNEL_ERROR',
INSTALL_FAILED: 'INSTALL_FAILED',
UNINSTALL_FAILED: 'UNINSTALL_FAILED',
MISSING_ARGUMENT: 'MISSING_ARGUMENT',
UNKNOWN_COMMAND: 'UNKNOWN_COMMAND',
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
} as const;
New codes may be added as needed during implementation.
Noise Handling
| Source | Stream | Solution |
|---|---|---|
| Pino logger | stderr | Call muteForJson() at command start to suppress |
console.log in commands |
stdout | jsonSuccess/jsonError call process.exit(), so subsequent logs never execute |
Progress bars (process.stdout.write) |
stdout | Guard with if (!json) or place after jsonSuccess call |
| Spinners (ora) | stdout | Not used in CLI commands currently; guard if added |
Command Pattern
export async function cmdExample(args: string[], instanceRoot?: string): Promise<void> {
const json = isJsonMode(args);
if (json) muteForJson();
try {
// ... command logic ...
if (json) jsonSuccess(data); // exits, nothing below runs
// Human-readable output
console.log('...');
} catch (err) {
if (json) jsonError(ErrorCodes.SOME_ERROR, err.message); // exits
console.error('...');
process.exit(1);
}
}
Commands In Scope
Group A — Data/Query Commands
| Command | data shape |
|---|---|
agents list |
{ agents: [{ key, name, version, distribution, description, installed, available, missingDeps }] } |
agents info <key> |
{ key, name, version, distribution, description, installed, binaryPath?, command?, registryId? } |
plugins / plugin list |
{ plugins: [{ name, version, enabled, source, description }] } |
plugin search <q> |
{ results: [{ name, version, description }] } |
status |
{ instances: [{ id, name, status, pid, dir, mode, channels, apiPort, tunnelPort }] } |
status --id <id> |
{ id, name, status, pid, dir, mode, channels, apiPort, tunnelPort } |
config set <k> <v> |
{ path, value, needsRestart } |
doctor --json |
{ categories: [{ name, results: [{ status, message }] }], summary: { passed, warnings, failed } } |
tunnel list |
{ tunnels: [{ port, label?, publicUrl?, status }] } |
api * (20+ subcommands) |
Normalized wrap of API response |
version |
{ version: "2026.401.1" } |
Notes:
config(no args) opens interactive editor — not JSON-compatible. Onlyconfig setsupports--json.doctor --jsonimplies--dry-run(no interactive fix prompts). Reports checks only. Always exits 0 withsuccess: true— caller checkssummary.failedto determine health.
Group B — Action/Result Commands
| Command | data shape |
|---|---|
start |
{ pid, instanceId, dir } |
stop |
{ stopped: true, pid } |
restart |
{ pid, instanceId, dir } |
install <plugin> |
{ plugin, version, installed: true } |
uninstall <plugin> |
{ plugin, uninstalled: true } |
agents install <key> |
{ key, version, installed: true } |
agents uninstall <key> |
{ key, uninstalled: true } |
setup |
{ configPath } |
adopt |
{ sessionId, threadId, agent, status } |
remote |
{ code, name, role, expiresAt, urls: { local, tunnel?, app? } } |
tunnel add |
{ port, publicUrl } |
tunnel stop |
{ port, stopped: true } |
tunnel stop-all |
{ stopped: true } |
plugin enable/disable |
{ plugin, enabled: boolean } |
Notes:
restart --foregroundstarts a blocking server — incompatible with--json. When--jsonis passed,restartalways uses daemon mode regardless of config.plugin configureandplugin createare interactive — not JSON-compatible.
Out of Scope
logs, attach, onboard, reset, dev, help, default, config (interactive editor), plugin configure, plugin create, agents run — these are interactive, streaming, or blocking commands where JSON output is not applicable.
Help Text
Commands that support --json must document it in their --help output. Add a line like:
--json Output result as JSON
Migration: Existing --json Commands
Two commands already support --json with non-standard formats. Both will be migrated to the new envelope format.
agents list --json (breaking change):
- Before:
[{ key, name, ... }](raw array) - After:
{ "success": true, "data": { "agents": [{ key, name, ... }] } }
setup --json (breaking change):
- Before:
{ success: false, error: "string" }/{ success: true, configPath } - After:
{ "success": false, "error": { "code": "SETUP_FAILED", "message": "..." } }/{ "success": true, "data": { "configPath": "..." } }
Testing Strategy
Layer 1: Unit Tests — output.ts helpers
File: src/cli/commands/__tests__/output.test.ts
isJsonMode()returns correct boolean for various arg combinationsjsonSuccess()outputs valid single-line JSON with{ success: true, data }and exits 0jsonError()outputs valid single-line JSON with{ success: false, error: { code, message } }and exits non-zeromuteForJson()callsmuteLogger()- All
ErrorCodesvalues are unique strings
Layer 2: Integration Tests — per-command JSON output
File: src/cli/commands/__tests__/json-output.test.ts
For each command with --json support:
- Success case: mock dependencies, call with
--json, verify:- Output is valid JSON (parseable by
JSON.parse) success === truedatamatches expected shape- Exit code 0
- Output is valid JSON (parseable by
- Error case: trigger error condition, verify:
- Output is valid JSON (not ANSI text or stack trace)
success === falseerror.codeis a known error code stringerror.messageis a non-empty string- Exit code non-zero
- No noise: stdout contains exactly one line of JSON, no progress bars, ANSI codes, or extraneous text
Layer 3: Contract Tests — JSON schema stability
File: src/cli/commands/__tests__/json-contract.test.ts
Snapshot-style tests that verify the shape of data for each command does not change unexpectedly. These protect consumers from silent breaking changes.
it('agents list --json contract', () => {
const output = JSON.parse(stdout);
expect(output).toHaveProperty('success', true);
expect(output).toHaveProperty('data.agents');
expect(output.data.agents[0]).toMatchObject({
key: expect.any(String),
name: expect.any(String),
installed: expect.any(Boolean),
});
});
Test Helpers
Shared utilities in src/cli/commands/__tests__/helpers/json-test-utils.ts:
function captureJsonOutput(fn: () => Promise<void>): { stdout: string; exitCode: number };
function expectValidJsonSuccess(stdout: string, dataShape?: object): void;
function expectValidJsonError(stdout: string, expectedCode?: string): void;
Estimated Test Count
- ~15-20 tests for output helpers (Layer 1)
- ~2-3 tests per command × ~20 commands = ~40-60 tests (Layer 2)
- ~1 test per command × ~20 commands = ~20 tests (Layer 3)
- Total: ~75-100 test cases