Skip to content

feat(cli): add the output format join point and restore colour negotiation - #391

Merged
dan-petty merged 1 commit into
release/v0.2.22from
feat/output-format-negotiation
Sep 22, 2026
Merged

dan-petty merged 1 commit into
release/v0.2.22from
feat/output-format-negotiation

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

Closes #326.

The serialization primitives already existed -- format_json, format_yaml, format_serialized -- but nothing joined them to the commands. Each of the 53 commands offering --json emitted its own write_stdout(json.dumps(...)), deciding for itself whether to call model_dump() or to_dict() first, so adding a second format meant editing every one of them. None offered YAML, although format_yaml had been there the whole time.

emit_serialized(payload, fmt) is the join. It takes a Pydantic model, an object exposing to_dict(), or a plain structure. Ten emission sites across ai_gateway, ai_controller and ai_chaos now route through it and gained working YAML with no signature change, because they already exposed --format.

YAML was not portable. yaml.dump writes a Python constructor tag for anything it does not recognise, so QuiesceState.QUIESCED rendered as !!python/object/apply:devops_cli.ai.controller.models.QuiesceState and yaml.safe_load refused to read it back -- the output was unusable by any consumer that is not this interpreter. Values now pass through JSON coercion before safe_dump, so both formats describe the same data, and a test asserts that for enums, paths and datetimes rather than for a payload that was already plain.

Terminal capability negotiation was defeated by its own wrapper. get_console defaulted color_system to None, and None is not "unspecified" to Rich -- it means no colour at all. Any caller passing file= or force_terminal= therefore lost colour silently. Only tests do that today, which is why nobody noticed, but it is the opposite of what the function is for. The default now inherits Rich's negotiation, so NO_COLOR, a non-TTY destination and TERM=dumb all take effect, and passing color_system=None explicitly still disables colour, which is what this suite wants. Four tests pin each case; the colour one has to describe a colour-capable terminal first, since the suite runs under NO_COLOR=1 and TERM=dumb.

The issue asked for --json / --yaml across 100% of subcommands. That target is wrong and the task file records why: an interactive chat, a port-forward daemon and a TUI have no result to serialise, and a --format flag that emits an empty object is worse than no flag. The remaining adoption is roadmapped as every command whose output is a value, enumerated rather than assumed. Measured at close: 306 commands, 53 with --json, 17 with --format, 10 on the join point.

…ation

Closes #326.

The serialization primitives already existed -- `format_json`, `format_yaml`, `format_serialized` -- but nothing joined them to the commands. Each of the 53 commands offering `--json` emitted its own `write_stdout(json.dumps(...))`, deciding for itself whether to call `model_dump()` or `to_dict()` first, so adding a second format meant editing every one of them. None offered YAML, although `format_yaml` had been there the whole time.

`emit_serialized(payload, fmt)` is the join. It takes a Pydantic model, an object exposing `to_dict()`, or a plain structure. Ten emission sites across `ai_gateway`, `ai_controller` and `ai_chaos` now route through it and gained working YAML with no signature change, because they already exposed `--format`.

YAML was not portable. `yaml.dump` writes a Python constructor tag for anything it does not recognise, so `QuiesceState.QUIESCED` rendered as `!!python/object/apply:devops_cli.ai.controller.models.QuiesceState` and `yaml.safe_load` refused to read it back -- the output was unusable by any consumer that is not this interpreter. Values now pass through JSON coercion before `safe_dump`, so both formats describe the same data, and a test asserts that for enums, paths and datetimes rather than for a payload that was already plain.

Terminal capability negotiation was defeated by its own wrapper. `get_console` defaulted `color_system` to `None`, and `None` is not "unspecified" to Rich -- it means no colour at all. Any caller passing `file=` or `force_terminal=` therefore lost colour silently. Only tests do that today, which is why nobody noticed, but it is the opposite of what the function is for. The default now inherits Rich's negotiation, so `NO_COLOR`, a non-TTY destination and `TERM=dumb` all take effect, and passing `color_system=None` explicitly still disables colour, which is what this suite wants. Four tests pin each case; the colour one has to describe a colour-capable terminal first, since the suite runs under `NO_COLOR=1` and `TERM=dumb`.

The issue asked for `--json` / `--yaml` across 100% of subcommands. That target is wrong and the task file records why: an interactive chat, a port-forward daemon and a TUI have no result to serialise, and a `--format` flag that emits an empty object is worse than no flag. The remaining adoption is roadmapped as every command whose output is a value, enumerated rather than assumed. Measured at close: 306 commands, 53 with `--json`, 17 with `--format`, 10 on the join point.
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