cli/session: add failing test for session export Rich panels contaminating JSON stdout #10502

Open
opened 2026-04-18 10:15:29 +00:00 by HAL9000 · 0 comments
Owner

Metadata

  • Commit message: test(cli/session): add failing BDD tests for session export JSON stdout contamination
  • Branch: test/cli-session-export-json-stdout-contamination

Background and Context

The agents session export <id> command (default --format json, no --output file) currently mixes Rich panel output with the JSON data on stdout. This TDD issue adds failing tests that confirm the bug exists before a fix is applied.

Expected Behavior

agents session export <id> (no --output) should output only valid JSON to stdout — no Rich panels, no ANSI escape codes mixed in. When --output file.json is specified, Rich panels may appear on stdout (since stdout is free).

Acceptance Criteria

  • test_session_export_json_stdout_no_rich_panels fails on the current codebase (JSON parse error due to panel contamination)
  • test_session_export_json_stdout_no_ansi_codes fails on the current codebase (ANSI codes present in output)
  • test_session_export_with_output_file_shows_panels passes on the current codebase (panels shown when writing to file)
  • All three tests pass after the corresponding bug fix (Issue: cli/session: session export Rich panels contaminate JSON stdout) is applied

Subtasks

  • Create tests/cli/test_session_export_json.py with the three test cases below
  • Tag tests with @tdd_issue, @tdd_issue_1, @tdd_expected_fail markers
  • Verify tests fail on the current codebase (confirming the bug exists)
  • Document expected failure reason in test docstrings

Test Specification

# tests/cli/test_session_export_json.py
def test_session_export_json_stdout_no_rich_panels(runner, mock_session_service):
    """agents session export must not mix Rich panels with JSON on stdout."""
    result = runner.invoke(app, ["session", "export", "01HXYZ"])
    assert result.exit_code == 0
    # The output should be valid JSON only - no Rich panel text mixed in
    try:
        data = json.loads(result.output)
    except json.JSONDecodeError:
        pytest.fail(
            f"stdout is not valid JSON - likely contaminated by Rich panels.\n"
            f"stdout: {result.output[:500]}"
        )
    # Verify it's session data, not an envelope
    assert "session_id" in data or "messages" in data

def test_session_export_json_stdout_no_ansi_codes(runner, mock_session_service):
    """agents session export stdout must not contain ANSI escape codes."""
    result = runner.invoke(app, ["session", "export", "01HXYZ"])
    assert result.exit_code == 0
    # No ANSI escape codes in output
    import re
    ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
    assert not ansi_escape.search(result.output), "ANSI codes found in JSON output"

def test_session_export_with_output_file_shows_panels(runner, mock_session_service, tmp_path):
    """agents session export -o file.json should show Rich panels on stdout."""
    output_file = tmp_path / "session.json"
    result = runner.invoke(app, ["session", "export", "01HXYZ", "-o", str(output_file)])
    assert result.exit_code == 0
    # Panels should appear on stdout when writing to file
    assert "Session Export" in result.output or "Export completed" in result.output

Expected Failure

The tests currently fail because export_session in src/cleveragents/cli/commands/session.py calls _render_export_panels() unconditionally after outputting JSON to stdout:

# In export_session():
else:
    typer.echo(content)  # Outputs JSON to stdout

# Then ALWAYS calls _render_export_panels() which calls console.print()
_render_export_panels(
    session_id=session_id,
    output=output,
    content=content,
    export_data=json_data,
    fmt=fmt,
)

When output is None (stdout mode), the Rich panels are printed to stdout AFTER the JSON, contaminating the output.

Definition of Done

  • Test file created with the above test cases
  • Tests are tagged with @tdd_issue, @tdd_issue_1, @tdd_expected_fail
  • Tests fail on current codebase (confirming the bug exists)
  • Tests pass after the corresponding bug fix is applied

Automated by CleverAgents Bot
Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor

## Metadata - **Commit message:** `test(cli/session): add failing BDD tests for session export JSON stdout contamination` - **Branch:** `test/cli-session-export-json-stdout-contamination` ## Background and Context The `agents session export <id>` command (default `--format json`, no `--output` file) currently mixes Rich panel output with the JSON data on stdout. This TDD issue adds failing tests that confirm the bug exists before a fix is applied. ## Expected Behavior `agents session export <id>` (no `--output`) should output **only** valid JSON to stdout — no Rich panels, no ANSI escape codes mixed in. When `--output file.json` is specified, Rich panels may appear on stdout (since stdout is free). ## Acceptance Criteria - [ ] `test_session_export_json_stdout_no_rich_panels` fails on the current codebase (JSON parse error due to panel contamination) - [ ] `test_session_export_json_stdout_no_ansi_codes` fails on the current codebase (ANSI codes present in output) - [ ] `test_session_export_with_output_file_shows_panels` passes on the current codebase (panels shown when writing to file) - [ ] All three tests pass after the corresponding bug fix (Issue: `cli/session`: `session export` Rich panels contaminate JSON stdout) is applied ## Subtasks - [ ] Create `tests/cli/test_session_export_json.py` with the three test cases below - [ ] Tag tests with `@tdd_issue`, `@tdd_issue_1`, `@tdd_expected_fail` markers - [ ] Verify tests fail on the current codebase (confirming the bug exists) - [ ] Document expected failure reason in test docstrings ## Test Specification ```python # tests/cli/test_session_export_json.py def test_session_export_json_stdout_no_rich_panels(runner, mock_session_service): """agents session export must not mix Rich panels with JSON on stdout.""" result = runner.invoke(app, ["session", "export", "01HXYZ"]) assert result.exit_code == 0 # The output should be valid JSON only - no Rich panel text mixed in try: data = json.loads(result.output) except json.JSONDecodeError: pytest.fail( f"stdout is not valid JSON - likely contaminated by Rich panels.\n" f"stdout: {result.output[:500]}" ) # Verify it's session data, not an envelope assert "session_id" in data or "messages" in data def test_session_export_json_stdout_no_ansi_codes(runner, mock_session_service): """agents session export stdout must not contain ANSI escape codes.""" result = runner.invoke(app, ["session", "export", "01HXYZ"]) assert result.exit_code == 0 # No ANSI escape codes in output import re ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') assert not ansi_escape.search(result.output), "ANSI codes found in JSON output" def test_session_export_with_output_file_shows_panels(runner, mock_session_service, tmp_path): """agents session export -o file.json should show Rich panels on stdout.""" output_file = tmp_path / "session.json" result = runner.invoke(app, ["session", "export", "01HXYZ", "-o", str(output_file)]) assert result.exit_code == 0 # Panels should appear on stdout when writing to file assert "Session Export" in result.output or "Export completed" in result.output ``` ## Expected Failure The tests currently fail because `export_session` in `src/cleveragents/cli/commands/session.py` calls `_render_export_panels()` unconditionally after outputting JSON to stdout: ```python # In export_session(): else: typer.echo(content) # Outputs JSON to stdout # Then ALWAYS calls _render_export_panels() which calls console.print() _render_export_panels( session_id=session_id, output=output, content=content, export_data=json_data, fmt=fmt, ) ``` When `output is None` (stdout mode), the Rich panels are printed to stdout AFTER the JSON, contaminating the output. ## Definition of Done - [ ] Test file created with the above test cases - [ ] Tests are tagged with `@tdd_issue`, `@tdd_issue_1`, `@tdd_expected_fail` - [ ] Tests fail on current codebase (confirming the bug exists) - [ ] Tests pass after the corresponding bug fix is applied --- **Automated by CleverAgents Bot** Supervisor: Bug Hunt Pool | Agent: bug-hunt-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#10502
No description provided.