fix: emit JSON errors from agents/channels commands in --json mode - #1574
Open
Ramnath0521 wants to merge 1 commit into
Open
fix: emit JSON errors from agents/channels commands in --json mode#1574Ramnath0521 wants to merge 1 commit into
Ramnath0521 wants to merge 1 commit into
Conversation
`opensquilla agents delete|add --json` and `opensquilla channels describe
--json` printed the human renderer's line on the failure path:
Error: 'Agent "qa-does-not-exist" not found'
while `sessions show --json` already emits a structured document. A
caller parsing stdout/stderr as JSON cannot handle the failure path.
Route these through the existing cli.output.emit_error helper, whose
non-JSON branch is byte-identical to the typer.secho call it replaces,
so human output is unchanged. channels certify already had this
if/else and is the in-file precedent.
Error codes follow the registry's own exception types: _require_index
raises KeyError for a missing agent (NOT_FOUND), while a builtin-agent
rejection or duplicate id raises ValueError (INVALID_ARGUMENT).
Refs TokenRhythm#1542
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1542.
What is wrong
agentsandchannelscommands accept--jsonbut their failure paths call the human renderer directly, so a caller parsing the output gets a plain line instead of a document.sessions show --json, which the issue uses as a control, already does the right thing.On
main, Windows 11:After the change, both emit one document on stderr and keep exit 2:
{"error": {"message": "'Agent \"qa-does-not-exist\" not found'", "code": "NOT_FOUND"}} {"error": {"message": "\"unknown channel type: 'qa-does-not-exist'\"", "code": "NOT_FOUND"}}The change
Both sites now go through the existing
cli.output.emit_error. Its non-JSON branch istyper.secho(f"Error: {message}", fg=typer.colors.RED, err=True)— byte-identical to the calls it replaces — so human output does not change at all.channels certifyalready had this exactif json_output / elseshape; this makes the rest of the file consistent with it rather than introducing a new pattern.agents_cmd._fail()gained ajson_outputkeyword and is now the single place that decides. Error codes come from the registry's own exception types rather than from guesswork:_require_indexraisesKeyErrorfor a missing agent ->NOT_FOUND_normalize_user_agent_id(builtin rejection) andcreate_agent(duplicate id) raiseValueError->INVALID_ARGUMENTget_channel_setup_speclikewise raisesKeyErrorfor an unknown type ->NOT_FOUND.Scope: why exactly three call sites
I audited every
Error: ...emitter in both files rather than only the two the issue names. Nine sites exist; six are in commands that do not accept--json(channels add/remove/enable/disable/edit), so they are not part of this defect and are untouched.channels certifywas already correct. That leaves three:agents deleteandchannels describefrom the report, plusagents add, which has the identical defect through the same_failhelper and also advertises--json— fixing the helper without passingjson_outputat one of its two call sites would have been incoherent.One thing deliberately left alone: the message text keeps Python's
KeyErrorrepr quoting ("'Agent \"x\" not found'"). That is whatsessions show --jsonalready produces for its ownNOT_FOUND, and normalising it here would change the plain-text output too, which is out of scope for this issue.Scope
Scope boundary: the
--jsonfailure paths ofagents add,agents delete, andchannels describe, routed through the existingemit_errorhelper.Non-goals: the six
channelssubcommands that do not accept--json;channels certify, which was already correct; the message text/quoting produced bystr(exc); and any success-path output.Branch
Base branch: main
Target exception: N/A
Issue
Linked issue: Refs #1542
Release Note
Release note:
opensquilla agents add|deleteandopensquilla channels describenow emit a structured JSON error document in--jsonmode instead of a plain-text line.Tests
Ruff:
ruff check src tests— All checks passed.Pytest:
pytest tests/test_cli -q— 875 passed, 3 skipped. Full suite (-q -n auto --ignore=tests/functional --ignore=tests/live): 25,139 passed, 194 failed, 728 skipped.Those 194 are pre-existing Windows-environment failures, not introduced here. They concentrate in
test_opensquilla_home_migration.py(61) andtest_turn_ingress_rpc.py(25), neither reachable from a CLI error-formatting change, and none are inagents_cmd/channels_cmd. Three did land intests/test_cli(all intest_migrate_cmd.py); all three pass when run serially, as does the wholetests/test_clidirectory.tests/functionalmust be run serially too — under-n autoits socket-binding gateway tests deadlock.Build:
mypy src/opensquilla --show-error-codes— Success: no issues found in 1547 source files.Regression tests: added
Notes: Five cases across the two existing CLI test modules — three cover the bug, two are controls that pin the plain-text path so the fix cannot silently change human output:
test_agents_delete_json_emits_structured_error_for_missing_agentmaintest_channels_describe_json_emits_structured_error_for_unknown_typemaintest_agents_add_json_emits_structured_error_for_duplicatemaintest_agents_delete_without_json_keeps_plain_text_errortest_channels_describe_without_json_keeps_plain_text_errorAll three bug tests were written first and confirmed failing with
json.decoder.JSONDecodeError: Expecting valuebefore the implementation existed; both controls passed before and after. The pre-existingtest_agents_add_duplicate_failsandtest_agents_delete_main_rejectedassert on combined stdout+stderr and still pass unchanged.Maintainer Live Check
Maintainer live check: no
Surface: N/A
Third-Party Origin
Third-party origin: none
Authored by Claude (an AI coding agent) on the account owner's machine and with their authorization. The reproduction, the failing-test-first sequence and every check above were genuinely executed here rather than asserted; the account owner reviewed the diff before this was opened. Flagging the AI authorship plainly rather than leaving it to be inferred — happy to take any correction in review.
🤖 Generated with Claude Code