BUG-HUNT: [output-corruption] session export renders Rich panels to stdout corrupting JSON/Markdown output #7758

Open
opened 2026-04-12 03:25:22 +00:00 by HAL9000 · 3 comments
Owner

Bug Report: [Output Corruption] session export Renders Rich Panels to stdout — Corrupts JSON and Markdown Output

Severity Assessment

  • Impact: agents session export without --output writes JSON/Markdown content to stdout via typer.echo(content), then ALSO renders three Rich panels (Session Export, Contents, Integrity) to stdout via console.print(). Any tool piping the output will receive Rich markup interleaved with the data content, making it unparseable.
  • Likelihood: Triggered on every agents session export <id> invocation without --output
  • Priority: High

Location

  • File: src/cleveragents/cli/commands/session.py
  • Function: export_session, _render_export_panels
  • Lines: 618–630

Description

The export command unconditionally calls _render_export_panels() regardless of whether output goes to a file or stdout:

# session.py lines 618-629
if output is not None:
    output.write_text(content, encoding="utf-8")
else:
    typer.echo(content)          # <-- writes JSON/Markdown to stdout

# Render Rich panels for both file and stdout export paths
_render_export_panels(           # <-- ALSO writes Rich panels to stdout!
    session_id=session_id,
    output=output,
    content=content,
    export_data=json_data,
    fmt=fmt,
)

When output is None (stdout mode), the sequence is:

  1. typer.echo(content) → writes {...JSON...} to stdout
  2. _render_export_panels() → writes Rich panel markup to stdout

Both go to the same stdout, producing something like:

{"session_id": "01HXYZ...", "messages": [...]}  <- JSON content
╭─ Session Export ──────────────────────────────╮
│ Session:  01HXYZ...                           │
│ Output:   (stdout)                             │
...

This output cannot be parsed as JSON by any downstream tool.

Evidence

# session.py lines 619-630
if output is not None:
    ...
    output.write_text(content, encoding="utf-8")
else:
    typer.echo(content)  # data to stdout

# BUG: panels always rendered, even when output=None (stdout mode)
_render_export_panels(
    session_id=session_id,
    output=output,
    content=content,
    export_data=json_data,
    fmt=fmt,
)

Expected Behavior

  • When --output <file> is given: write data to file, render panels to terminal (stdout/stderr)
  • When no --output: write only the data to stdout, render panels to stderr OR omit them entirely

Actual Behavior

Both data content AND Rich panels are written to stdout, producing invalid/unparseable output.

Suggested Fix

if output is not None:
    output.parent.mkdir(parents=True, exist_ok=True)
    output.write_text(content, encoding="utf-8")
    # Panels go to terminal when writing to file
    _render_export_panels(...)
else:
    typer.echo(content)  # data only to stdout
    # Skip panels when stdout is the data stream,
    # OR redirect panel console to stderr:
    # err_console = Console(stderr=True)
    # _render_export_panels(..., use_console=err_console)

Category

output-corruption

TDD Note

After this bug issue is verified, a corresponding Type/Testing issue will be created for TDD.


Automated by CleverAgents Bot
Supervisor: Bug Hunting | Agent: bug-hunter

## Bug Report: [Output Corruption] `session export` Renders Rich Panels to stdout — Corrupts JSON and Markdown Output ### Severity Assessment - **Impact**: `agents session export` without `--output` writes JSON/Markdown content to stdout via `typer.echo(content)`, then ALSO renders three Rich panels (Session Export, Contents, Integrity) to stdout via `console.print()`. Any tool piping the output will receive Rich markup interleaved with the data content, making it unparseable. - **Likelihood**: Triggered on every `agents session export <id>` invocation without `--output` - **Priority**: High ### Location - **File**: `src/cleveragents/cli/commands/session.py` - **Function**: `export_session`, `_render_export_panels` - **Lines**: 618–630 ### Description The export command unconditionally calls `_render_export_panels()` regardless of whether output goes to a file or stdout: ```python # session.py lines 618-629 if output is not None: output.write_text(content, encoding="utf-8") else: typer.echo(content) # <-- writes JSON/Markdown to stdout # Render Rich panels for both file and stdout export paths _render_export_panels( # <-- ALSO writes Rich panels to stdout! session_id=session_id, output=output, content=content, export_data=json_data, fmt=fmt, ) ``` When `output is None` (stdout mode), the sequence is: 1. `typer.echo(content)` → writes `{...JSON...}` to stdout 2. `_render_export_panels()` → writes Rich panel markup to stdout Both go to the same stdout, producing something like: ``` {"session_id": "01HXYZ...", "messages": [...]} <- JSON content ╭─ Session Export ──────────────────────────────╮ │ Session: 01HXYZ... │ │ Output: (stdout) │ ... ``` This output cannot be parsed as JSON by any downstream tool. ### Evidence ```python # session.py lines 619-630 if output is not None: ... output.write_text(content, encoding="utf-8") else: typer.echo(content) # data to stdout # BUG: panels always rendered, even when output=None (stdout mode) _render_export_panels( session_id=session_id, output=output, content=content, export_data=json_data, fmt=fmt, ) ``` ### Expected Behavior - When `--output <file>` is given: write data to file, render panels to terminal (stdout/stderr) - When no `--output`: write only the data to stdout, render panels to stderr OR omit them entirely ### Actual Behavior Both data content AND Rich panels are written to stdout, producing invalid/unparseable output. ### Suggested Fix ```python if output is not None: output.parent.mkdir(parents=True, exist_ok=True) output.write_text(content, encoding="utf-8") # Panels go to terminal when writing to file _render_export_panels(...) else: typer.echo(content) # data only to stdout # Skip panels when stdout is the data stream, # OR redirect panel console to stderr: # err_console = Console(stderr=True) # _render_export_panels(..., use_console=err_console) ``` ### Category output-corruption ### TDD Note After this bug issue is verified, a corresponding Type/Testing issue will be created for TDD. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunting | Agent: bug-hunter
HAL9000 added this to the v3.2.0 milestone 2026-04-12 03:45:10 +00:00
Author
Owner

Verified — Bug: session export corrupts JSON/Markdown output by rendering Rich panels to stdout. MoSCoW: Must-have. Priority: High — output corruption.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Bug: session export corrupts JSON/Markdown output by rendering Rich panels to stdout. MoSCoW: Must-have. Priority: High — output corruption. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Author
Owner

Verified — Bug: session export corrupts JSON/Markdown output by rendering Rich panels to stdout. MoSCoW: Must-have. Priority: High — output corruption.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Bug: session export corrupts JSON/Markdown output by rendering Rich panels to stdout. MoSCoW: Must-have. Priority: High — output corruption. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Author
Owner

Verified — Bug: session export corrupts JSON/Markdown output by rendering Rich panels to stdout. MoSCoW: Must-have. Priority: High — output corruption.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Bug: session export corrupts JSON/Markdown output by rendering Rich panels to stdout. MoSCoW: Must-have. Priority: High — output corruption. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
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.

Dependencies

No dependencies set.

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