Skip to content

fix: CLI ErrorCode reuse + result_type→renderer parity contract test#134

Merged
clean6378-max-it merged 6 commits into
masterfrom
fix/cli-errorcode-parity
Jul 15, 2026
Merged

fix: CLI ErrorCode reuse + result_type→renderer parity contract test#134
clean6378-max-it merged 6 commits into
masterfrom
fix/cli-errorcode-parity

Conversation

@clean6378-max-it

@clean6378-max-it clean6378-max-it commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Closes #130

export_engine already records an ExportFailure with an Error Code for each sesison that fails.
The CLI was not using that. It picked exit status from counts alone and printed str(exc) in warnings, so the command line named failures differently than the HTTP API.
This branch routes bulk exit codes through the failure list , prints stable codes and messages on stderr, and updates _die so missing project dirs and unknown sessions report SESSION_NOT_FOUND or INTERNAL_ERROR instead of a free-form string.
The on_export_error callback now passes an ExportFailure to callersl the API looger was updated to match.

A contract test checks that result_type values from the Python dispatch builders stay in sync with TOOL_RESULT_RENDERS in registery.js and the _render_tool_result branches in md_exporter.py.
Before this, drift only showed up as a degraded fallback renderer with no test failure.

Run pytest tests/test_cli_export_exit_codes.py tests/test_tool_dispatch_sync.py tests/test_error_codes.py -q for the targeted pass, or pytest -q for the full suite.
For a quick manual check, bulk-export a tree where every session fails: stderr should show PARSE_ERROR (or another stable code). not the raw exception, and the process should exit 1.

Summary by CodeRabbit

  • Bug Fixes

    • Improved export/CLI failures with consistent, structured, stable error codes and messages (avoids raw exception text).
    • Bulk export now uses standardized POSIX-style exit codes and reports the most common failure when present.
    • Missing projects/sessions and ambiguous session-prefix matches now stop with deterministic, clear stderr output.
  • New Features

    • Added dominant-failure selection and bulk-export exit-code mapping.
    • Introduced new stable error codes for ambiguous session prefixes and missing projects directory.
  • Tests

    • Expanded CLI exit-code and stderr assertions for failure scenarios.
    • Strengthened checks to keep tool-result dispatch rendering in sync.
  • Chores

    • Updated pytest configuration to ensure correct import paths.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3ef05b8c-99c7-4537-b1d8-12e93d8402b0

📥 Commits

Reviewing files that changed from the base of the PR and between 5cee35e and 8f40420.

📒 Files selected for processing (1)
  • tests/test_tool_dispatch_sync.py

📝 Walkthrough

Walkthrough

Bulk export now propagates structured ExportFailure objects, standardizes CLI and API error reporting, maps results to exit codes, and tests failure-code behavior. Additional contract tests synchronize tool-result types across dispatch, frontend rendering, and Markdown export.

Changes

Structured export failure reporting

Layer / File(s) Summary
Structured failure contracts
models/error_codes.py, utils/export_engine.py
Bulk export callbacks receive ExportFailure objects, while helpers derive dominant failure codes and POSIX exit codes.
CLI and API failure reporting
scripts/export.py, api/export_api.py
CLI validation and bulk-export paths emit standardized error codes, and API/CLI callbacks log structured failure fields.
Export error behavior tests and test configuration
tests/test_cli_export_exit_codes.py, tests/test_error_codes.py, pyproject.toml
Tests cover standardized stderr output, exit-code mapping, dominant failure selection, missing-session handling, and pytest import-path configuration.

Tool result dispatch contracts

Layer / File(s) Summary
Tool result type synchronization
tests/test_tool_dispatch_sync.py
Tests extract and compare result types from dispatch builders, frontend renderers, and Markdown exporter branches.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested reviewers: timon0305, wpak-ai

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant run_bulk_export
  participant ExportFailure
  participant _exit_bulk_export
  participant ErrorCode
  CLI->>run_bulk_export: start bulk export
  run_bulk_export->>ExportFailure: classify failed session
  run_bulk_export-->>CLI: invoke callback with failure
  run_bulk_export-->>_exit_bulk_export: return BulkExportResult
  _exit_bulk_export->>ErrorCode: select dominant failure code
  _exit_bulk_export-->>CLI: print standardized message and exit code
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main changes: structured CLI ErrorCode reuse and the new result_type parity contract test.
Linked Issues check ✅ Passed The PR matches #130 by reusing structured CLI ErrorCodes, updating export callbacks/API logging, and adding the renderer parity contract test.
Out of Scope Changes check ✅ Passed No obvious unrelated changes stand out; the pyproject and enum updates support the CLI/test work in scope.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/cli-errorcode-parity

Comment @coderabbitai help to get the list of available commands.

@clean6378-max-it clean6378-max-it self-assigned this Jul 14, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@scripts/export.py`:
- Around line 760-763: Update _die and its callers so INTERNAL_ERROR uses a
generic CLI failure message instead of the export-specific
failure_message_for_code text, while preserving existing messages for other
error codes. Ensure list and stats report the generic message when the projects
directory is missing.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d4678000-270d-48c4-b6e2-2f48a4a8d7d3

📥 Commits

Reviewing files that changed from the base of the PR and between baee51a and d1a3642.

📒 Files selected for processing (6)
  • api/export_api.py
  • scripts/export.py
  • tests/test_cli_export_exit_codes.py
  • tests/test_error_codes.py
  • tests/test_tool_dispatch_sync.py
  • utils/export_engine.py

Comment thread scripts/export.py Outdated
Comment thread scripts/export.py Outdated
Comment thread scripts/export.py Outdated
Comment thread scripts/export.py Outdated
Comment thread tests/test_tool_dispatch_sync.py Outdated
Comment thread tests/test_tool_dispatch_sync.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/test_tool_dispatch_sync.py (1)

80-90: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

No fallback when the sliced function is the last top-level def in the file.

The lookahead (?=^\ndef ) requires a subsequent top-level def; if _render_tool_result (or any other target) ever becomes the last function in md_exporter.py, re.search returns None and the helper raises ValueError, breaking the contract test with an opaque error rather than a clear assertion failure.

♻️ Allow end-of-file as a valid boundary
     match = re.search(
-        rf"^def {re.escape(func_name)}\(.*?(?=^\ndef )",
+        rf"^def {re.escape(func_name)}\(.*?(?=^\ndef |\Z)",
         text,
         re.DOTALL | re.MULTILINE,
     )

Note: the static analysis ReDoS hint on this regex is a false positive — func_name is always a hardcoded literal in this file's call sites, and the single non-greedy quantifier with a lookahead boundary runs in linear time, not exponential.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_tool_dispatch_sync.py` around lines 80 - 90, Update
_parse_top_level_function_body so its regex accepts either the next top-level
def or end-of-file as the function boundary. Preserve the existing
missing-function ValueError and slicing behavior, while allowing a target
function such as _render_tool_result to be the final top-level definition.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_error_codes.py`:
- Around line 149-152: Update the stderr assertions in the stats CLI test to
reject SESSION_NOT_FOUND across the complete proc.stderr, rather than only the
substring preceding SESSION_AMBIGUOUS_PREFIX; preserve the existing return-code
and SESSION_AMBIGUOUS_PREFIX checks.

---

Nitpick comments:
In `@tests/test_tool_dispatch_sync.py`:
- Around line 80-90: Update _parse_top_level_function_body so its regex accepts
either the next top-level def or end-of-file as the function boundary. Preserve
the existing missing-function ValueError and slicing behavior, while allowing a
target function such as _render_tool_result to be the final top-level
definition.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0674b0ee-c2ba-4ba8-a31e-1d407a561b8c

📥 Commits

Reviewing files that changed from the base of the PR and between dda2944 and e992d51.

📒 Files selected for processing (5)
  • models/error_codes.py
  • scripts/export.py
  • tests/test_error_codes.py
  • tests/test_tool_dispatch_sync.py
  • utils/export_engine.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • utils/export_engine.py
  • scripts/export.py

Comment thread tests/test_error_codes.py Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_tool_dispatch_sync.py`:
- Around line 82-83: Update the regex boundary in the test’s function-extraction
logic to stop at the next top-level def regardless of whether a blank line
precedes it, while retaining the end-of-file boundary. Add a fixture covering
adjacent top-level definitions and verify parity extraction remains correct.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c737406b-ca4a-4c90-a6b5-55b31e5323a1

📥 Commits

Reviewing files that changed from the base of the PR and between e992d51 and 5cee35e.

📒 Files selected for processing (2)
  • tests/test_error_codes.py
  • tests/test_tool_dispatch_sync.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_error_codes.py

Comment thread tests/test_tool_dispatch_sync.py Outdated
@timon0305 timon0305 requested a review from wpak-ai July 15, 2026 01:43
@clean6378-max-it clean6378-max-it merged commit 0c43bd7 into master Jul 15, 2026
15 checks passed
@clean6378-max-it clean6378-max-it deleted the fix/cli-errorcode-parity branch July 15, 2026 11:08
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.

claude-code-chat-browser: CLI ErrorCode reuse + result_type→renderer parity contract test

3 participants