fix(claude-code): fix empty responses from claude -p driver (issue #295)#803
Open
jam676767 wants to merge 5 commits intoRightNow-AI:mainfrom
Open
fix(claude-code): fix empty responses from claude -p driver (issue #295)#803jam676767 wants to merge 5 commits intoRightNow-AI:mainfrom
jam676767 wants to merge 5 commits intoRightNow-AI:mainfrom
Conversation
Without this attribute, serde treats a missing `result` field as a deserialization error even though `Option<T>` implies the field is optional. Some Claude CLI versions emit the response in `content` or `text` rather than `result`; the silent parse failure caused the driver to fall through to a plain-text read which could be empty, triggering the "model returned an empty response" guard in the agent loop. Closes RightNow-AI#295. Co-Authored-By: Claude <noreply@anthropic.com>
…ssistant content Newer Claude CLI versions (≥2.x) emit assistant responses inside a nested `message.content[].text` structure in stream-json events, rather than a flat `content` string. Add ClaudeMessageBlock and ClaudeAssistantMessage structs, plus a new `message` field on ClaudeStreamEvent, so the stream handler can extract text from both layouts. Refs: RightNow-AI#295
…urrent drain When complete() called child.wait() before reading stdout/stderr, large responses (>64 KB) caused a deadlock: the subprocess blocked on write() because the OS pipe buffer was full, and wait() never returned. Fix by spawning two tokio tasks to drain stdout/stderr concurrently with child.wait(), then collecting after the process exits. Also inject HOME from home_dir() so the CLI finds ~/.claude/credentials when OpenFang runs as a service, and set stdin to null so the CLI does not stall waiting for interactive input. Refs: RightNow-AI#295
Mirror the same environment fixes applied to complete(): inject HOME so the CLI locates ~/.claude/credentials when running as a service, and set stdin to null so the process does not block on interactive input. Refs: RightNow-AI#295
…in stream()
Claude CLI ≥2.x emits type=assistant events where the response text is
inside message.content[{"type":"text","text":"..."}] rather than a flat
content string. The old handler only checked event.content, so every
token was silently dropped and streaming always returned an empty response.
The handler now checks the flat content field first (backward-compatible),
then falls back to joining all text blocks from message.content[].
Refs: RightNow-AI#295
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #295 — the
claude-codedriver always returns empty responses when usingclaude -p.Five root causes were identified and each fixed in a separate commit:
Fix 1 —
#[serde(default)]onClaudeJsonOutput.resultserde_jsonfails to deserialize the CLI's JSON output if theresultfield is absent (e.g. when the response is incontentortextinstead). Adding#[serde(default)]makes the field optional and prevents silent deserialization failure that swallows the response.Fix 2 — New structs for nested assistant message content
CLI ≥2.x emits
type=assistantstream-json events where the text is nested inmessage.content[{"type":"text","text":"..."}].ClaudeStreamEventhad nomessagefield, so these events were always skipped. AddedClaudeMessageBlock,ClaudeAssistantMessage, and amessage: Option<ClaudeAssistantMessage>field.Fix 3 — Concurrent pipe drain in
complete()to prevent deadlockcomplete()calledchild.wait()before reading stdout/stderr. For responses larger than the OS pipe buffer (~64 KB), the subprocess blocks onwrite(),wait()never returns, the timeout fires, and stdout is empty. Fixed by spawning twotokio::spawntasks to drain pipes concurrently withchild.wait().Also injects
HOMEfromhome_dir()so the CLI can find~/.claude/credentialswhen OpenFang runs as a service, and setsstdintonullto prevent the CLI from blocking on interactive input.Fix 4 — Inject HOME and null stdin in
stream()Same environment hygiene as Fix 3, applied to the streaming path.
Fix 5 — Extract text from
message.content[].textinstream()handlerThe
type=assistantmatch arm only checkedevent.content(flat string). For CLI ≥2.x this field is null, so every streamed token was dropped and the method returned an empty string. The handler now checksevent.contentfirst (backward-compatible with older CLI), then falls back to joining alltext-type blocks frommessage.content[].Test plan
cargo test -p openfang-runtime --lib— 826 passed, 0 failedclaude -p "Say hello" --output-format jsonreturns non-emptyresulton CLI ≥2.xclaude -p "Say hello" --output-format stream-json --verboseemitstype=assistantevents with nested content on CLI ≥2.x🤖 Generated with Claude Code