UAT: render_error() JSON/YAML output goes to stdout instead of stderr — error output should be on stderr #4004

Open
opened 2026-04-06 08:28:52 +00:00 by freemo · 0 comments
Owner

Metadata

  • Branch: fix/render-error-json-yaml-stderr
  • Commit Message: fix(cli): route render_error() JSON/YAML output to stderr
  • Milestone: None (Backlog)
  • Parent Epic: #936

Background and Context

The render_error() function in src/cleveragents/cli/renderers.py is designed to write error output to stderr via _get_err_console(). However, when the output format is json or yaml, the actual content is silently routed to sys.stdout instead, because format_output() always writes to sys.stdout regardless of the calling context.

This breaks the contract that error output belongs on stderr, and causes machine-readable error messages to be mixed with normal stdout output when using --format json or --format yaml.

Current Behavior

In render_error():

console = console or _get_err_console()  # stderr console

if fmt in (OutputFormat.JSON.value, OutputFormat.YAML.value):
    envelope = {...}
    console.print(format_output(envelope, fmt))  # format_output writes to sys.stdout, returns ""
    # This prints "" to stderr (just a blank line), while JSON/YAML goes to stdout
    return

In format_output():

if fmt in (OutputFormat.JSON.value, OutputFormat.YAML.value, OutputFormat.PLAIN.value):
    sys.stdout.write(rendered + "\n")  # Always writes to stdout!
    sys.stdout.flush()
    return ""

Result: JSON/YAML error content goes to stdout; only an empty line goes to stderr.

Expected Behavior

Error output in JSON/YAML format should go to stderr, consistent with the intent of render_error() using _get_err_console(). Scripts capturing stdout for machine-readable output must not receive error messages mixed with normal output.

Acceptance Criteria

  • render_error() with --format json writes JSON to stderr, not stdout
  • render_error() with --format yaml writes YAML to stderr, not stdout
  • Normal (non-error) format_output() calls continue to write to stdout as before
  • Existing tests pass; new tests cover the stderr routing for JSON/YAML error output

Subtasks

  • Investigate format_output() in src/cleveragents/cli/formatting.py and decide on fix approach (add file parameter vs. inline serialization in render_error())
  • Implement fix so render_error() routes JSON/YAML serialized output to stderr
  • Ensure non-error callers of format_output() are unaffected
  • Tests (unit): Add/update tests asserting JSON/YAML error output appears on stderr
  • Tests (unit): Confirm stdout is clean (no error content) when render_error() is called with JSON/YAML format
  • Verify coverage ≥ 97% via nox -s coverage_report
  • Run nox (all default sessions), fix any errors

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly (fix(cli): route render_error() JSON/YAML output to stderr), followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly (fix/render-error-json-yaml-stderr).
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass
  • Coverage >= 97%

Backlog note: This issue was discovered during autonomous operation
on milestone related to the Output Rendering Pipeline Integration (Epic #936). It does not block milestone completion and has been
placed in the backlog for human review and future milestone assignment.


Automated by CleverAgents Bot
Supervisor: UAT Testing | Agent: ca-new-issue-creator

## Metadata - **Branch**: `fix/render-error-json-yaml-stderr` - **Commit Message**: `fix(cli): route render_error() JSON/YAML output to stderr` - **Milestone**: None (Backlog) - **Parent Epic**: #936 ## Background and Context The `render_error()` function in `src/cleveragents/cli/renderers.py` is designed to write error output to stderr via `_get_err_console()`. However, when the output format is `json` or `yaml`, the actual content is silently routed to `sys.stdout` instead, because `format_output()` always writes to `sys.stdout` regardless of the calling context. This breaks the contract that error output belongs on stderr, and causes machine-readable error messages to be mixed with normal stdout output when using `--format json` or `--format yaml`. ## Current Behavior In `render_error()`: ```python console = console or _get_err_console() # stderr console if fmt in (OutputFormat.JSON.value, OutputFormat.YAML.value): envelope = {...} console.print(format_output(envelope, fmt)) # format_output writes to sys.stdout, returns "" # This prints "" to stderr (just a blank line), while JSON/YAML goes to stdout return ``` In `format_output()`: ```python if fmt in (OutputFormat.JSON.value, OutputFormat.YAML.value, OutputFormat.PLAIN.value): sys.stdout.write(rendered + "\n") # Always writes to stdout! sys.stdout.flush() return "" ``` **Result**: JSON/YAML error content goes to stdout; only an empty line goes to stderr. ## Expected Behavior Error output in JSON/YAML format should go to stderr, consistent with the intent of `render_error()` using `_get_err_console()`. Scripts capturing stdout for machine-readable output must not receive error messages mixed with normal output. ## Acceptance Criteria - `render_error()` with `--format json` writes JSON to stderr, not stdout - `render_error()` with `--format yaml` writes YAML to stderr, not stdout - Normal (non-error) `format_output()` calls continue to write to stdout as before - Existing tests pass; new tests cover the stderr routing for JSON/YAML error output ## Subtasks - [ ] Investigate `format_output()` in `src/cleveragents/cli/formatting.py` and decide on fix approach (add `file` parameter vs. inline serialization in `render_error()`) - [ ] Implement fix so `render_error()` routes JSON/YAML serialized output to stderr - [ ] Ensure non-error callers of `format_output()` are unaffected - [ ] Tests (unit): Add/update tests asserting JSON/YAML error output appears on stderr - [ ] Tests (unit): Confirm stdout is clean (no error content) when `render_error()` is called with JSON/YAML format - [ ] Verify coverage ≥ 97% via `nox -s coverage_report` - [ ] Run `nox` (all default sessions), fix any errors ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly (`fix(cli): route render_error() JSON/YAML output to stderr`), followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly (`fix/render-error-json-yaml-stderr`). - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - All nox stages pass - Coverage >= 97% > **Backlog note:** This issue was discovered during autonomous operation > on milestone related to the Output Rendering Pipeline Integration (Epic #936). It does not block milestone completion and has been > placed in the backlog for human review and future milestone assignment. --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: ca-new-issue-creator
HAL9000 added this to the v3.5.0 milestone 2026-04-09 03:12:03 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveragents-core#4004
No description provided.