Skip to main content

JSON Output Contract

JSON Output Contract (agent-consumable --json)

RouteRTL commands that emit --json wrap their payload in one envelope so an agent (or any script) can chain commands without parsing prose or reverse-engineering each command's ad-hoc dict shape.

The envelope

{ "schema_version": 1, "ok": true, "data": { "...": "command-specific payload (any JSON value)" }, "errors": [], "warnings": [] }
FieldTypeMeaning
schema_versionintOne integer for the whole CLI. Bumped only on a breaking change to the envelope itself — never for additive changes to a command's data.
okboolDid the command succeed? Mirrors the exit code: ok == (exit_code == 0).
dataanyThe command-specific payload — whatever the command used to print bare. Object, array, scalar, or null.
errorsstring[]Human-readable error messages. Empty when ok is true.
warningsstring[]Non-fatal advisories. May be present even when ok is true.

Rules

  1. ok tracks the exit code. A command that emits ok: false MUST also exit non-zero. emit_json never calls sys.exit — the command owns its exit code. An agent can therefore branch on either signal.
  2. schema_version is global and stable. Adding a key to a command's data does not bump it; only a change an existing parser could choke on does. Consumers should tolerate unknown keys inside data.
  3. Adoption is additive. New and backfilled commands use the envelope. The ~30 commands that already emit ad-hoc JSON migrate opportunistically breaking sweep. Until a command is migrated, treat its --json as command-specific (un-enveloped). See decision record for rationale.
  4. rr hw bit-sha --json is an identity-object surface. Its top-level object is the vendor-neutral identity contract (outcome, confirmed, vendor, mechanism, expected, observed, reason), not this generic envelope. Hardware providers consume that object directly; wrapping it in data.identity caused confirmed boards to be reported as having no identity. A real readback exits 0 only for CONFIRMED; MISMATCH and UNCONFIRMED are non-zero. The diagnostic --dry-run exception exits 0 with an UNCONFIRMED object because it deliberately reads no device.

Producing it (command authors)

from sdk.cli.output import emit_json @click.option("--json", "as_json", is_flag=True, help="Machine-readable output.") def mycmd(as_json): result = {"widgets": [...]} if as_json: emit_json(result) # ok=True by default return # ... human-readable rendering ...

For a failure path: emit_json(None, ok=False, errors=["no project found"]) and then exit non-zero.

Consuming it (agents / scripts)

import json, subprocess out = subprocess.run(["rr", "rules", "check", "--json"], capture_output=True, text=True) env = json.loads(out.stdout) if env["ok"]: use(env["data"]) else: handle(env["errors"])

sdk.cli.output.is_envelope(obj) validates the shape before you trust it.

Enforcement

sdk/cli/tests/test_json_output_contract_p2765.py guards the contract:

  • Regression — read-only query commands that already emit --json (JSON_CONTRACT_STABLE) must never lose it.
  • Self-cleaning worklist — the backfill rows in JSON_BACKFILL_PENDING is derived from them. The moment a pending command starts emitting --json, the guard fails until its one authoritative row is removed, so the worklist cannot silently rot or contradict a second list.

The MCP _CLI_SEMANTIC_ANNOTATIONS table is intentionally not the read-only oracle here — it is opt-in and largely unannotated. The agent-consumable surface is an explicit curated registry in the test.