Implement the following plan: Fix Agent Subprocess Error Handling Context The Claude CLI…
Private research annotation
Sign in to add a private research label or note.
- User
Implement the following plan:
Fix Agent Subprocess Error Handling
Context
The Claude CLI subprocess (
fang/agent.rb) produces garbled error messages like:codeAgent error: Agent exited with code : {"level":"warn","message":"[BashTool] Pre-flight check is taking longer than expected..."}Two bugs combine to produce this:
- Blank exit code:
status.exitstatusreturnsnilwhen the process is killed by a signal (e.g. timeout/SIGTERM). The code interpolates this nil directly, producing "code :". - Raw JSON in error message: The
claudeCLI emits structured NDJSON to stderr (including warn-level log lines). The code dumps this raw JSON into the user-facing error instead of extracting the human-readable message.
File to Modify
fang/agent.rb— two locations:run_claude(line ~122) andrun_claude_streaming(line ~174-176)Changes
1. Add
exit_code_labelhelper (private method)rubydef exit_code_label(status) if status.exitstatus status.exitstatus.to_s elsif status.termsig "signal #{status.termsig}" else "unknown" end end2. Add
extract_stderr_messagehelper (private method)Parse NDJSON stderr lines and extract human-readable messages. Fall back to raw text if not JSON.
rubydef extract_stderr_message(stderr_output) return "(no output)" if stderr_output.to_s.strip.empty? lines = stderr_output.strip.split("\n") messages = lines.filter_map do |line| parsed = JSON.parse(line) parsed["message"] rescue JSON::ParserError line.strip.empty? ? nil : line.strip end messages.empty? ? "(no output)" : messages.join("; ") end3. Update
run_claudeerror path (~line 122)Replace:
- Blank exit code:
Assistant
Let me read the current agent.rb to see the exact code.
Assistant
Now I'll make all the changes:
Assistant
Now let me run the tests to verify nothing is broken.
Assistant
All 187 tests pass with 0 failures. The changes are complete:
exit_code_label— returns the exit code number,"signal N"for signal kills, or"unknown"when both are nilextract_stderr_message— parses NDJSON lines and extracts"message"fields, falls back to raw text for non-JSON lines- Updated
run_claudeerror path to use both helpers - Updated
run_claude_streamingerror path to use both helpers
- User
commit this
Assistant
Committed as
e79f608.