fix: output structured XML errors in subagent mode#1856
fix: output structured XML errors in subagent mode#1856mashraf-222 wants to merge 1 commit intomainfrom
Conversation
When codeflash runs with --subagent (e.g., via the Claude Code plugin), exit_with_message() now outputs <codeflash-error> XML to stdout instead of Rich panel text. This lets the calling agent parse errors programmatically rather than receiving unstructured text. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @mashraf-222's task in 2m 26s —— View job PR Review Summary
Prek Checks✅ All checks passed — Mypy shows 29 errors in the two changed files, but these are all pre-existing issues unrelated to this PR. No new type errors were introduced. Code ReviewThe implementation is correct and follows the existing patterns in the codebase. Observations:
No bugs or security issues found. Duplicate DetectionNo duplicates detected. The Test CoverageAll 3 new tests pass:
Coverage for One minor gap: no test for Overall: Clean implementation, correct behavior, well-tested. The only actionable suggestion is moving the lazy import to the top-level. |
Summary
exit_with_message()now outputs<codeflash-error>XML to stdout in subagent mode instead of Rich panel textProblem
When codeflash encounters errors in
--subagentmode (no git repo, file not found, config errors, etc.), it outputs Rich-formatted panel text. The calling Claude agent expects structured data and may misinterpret or ignore these errors. The success path already outputs structured XML (<codeflash-optimization>,<codeflash-summary>), but the error path didn't.Root cause
exit_with_message()incode_utils.pyis the central error exit function used by ~15 call sites. It had special handling for LSP mode (log instead of exit) but no handling for subagent mode.Solution
Added a subagent mode check in
exit_with_message()that outputs structured XML before exiting:XML special characters are properly escaped via
xml.sax.saxutils.escape. This matches the pattern already used by the success output (<codeflash-optimization>) and the no-results output (<codeflash-summary>).Code changes
codeflash/code_utils/code_utils.py: Addedis_subagent_modeimport and subagent check inexit_with_message()— 4 lines of logictests/test_code_utils.py: 3 new tests covering structured XML output, XML escaping, and non-subagent mode behaviorTesting
$ uv run pytest tests/test_code_utils.py::TestExitWithMessageSubagent -v # 3 passed🤖 Generated with Claude Code