fix(claudecode): unwrap JSON envelope in agentic responses#218
Merged
Conversation
`SendPromptAgentic` invokes the Claude CLI with `--output-format json`
and was returning the entire envelope (`{"type":"result",...,
"result":"...","usage":{"contextWindow":200000,...}}`) as the agent's
reply. Two consequences:
1. Loop exit-condition regexes saw model metadata. The
`(?i)context[_\s]*(...|window)` pattern matched `"contextWindow":200000`
and exited agentic loops as "context exhaustion" after a single
iteration, even though the agent had only just begun the work.
2. Output files configured for the step received the JSON blob instead
of the agent's prose, making activity logs unreadable.
Add `extractClaudeCodeResult` to parse the envelope and return only the
`result` field, with safe pass-through for non-JSON / malformed /
empty-result inputs so non-agentic call sites and future format changes
degrade gracefully. Wire it into `SendPromptAgentic`; leave `SendPrompt`
and `SendPromptWithFile` alone (they don't request JSON output).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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
Agentic loops were exiting after a single iteration with reason "Agent signaled context exhaustion or documented remaining work" even when the agent had clearly stated next-iteration plans.
Root cause:
SendPromptAgenticcalls the Claude CLI with--output-format jsonbut returned the entire envelope as the response. The exit-condition regex then matched on metadata, not agent prose:(?i)context[_\s]*(limit|exhaust|full|window)matched\"contextWindow\":200000in the envelope'susageobject.Reproduced with the user's
jPOSanalysis loop — the agent's iteration 2 reply ended with "Next iteration focus: ..." yet the loop exited. Confirmed by replaying the patterns over the literal stdout in.comanda/ANALYSIS_LOG.md.A secondary consequence: configured step output files received the JSON blob instead of the agent's prose, making activity logs unreadable.
Fix
Add
extractClaudeCodeResult(stdout string) stringthat JSON-decodes the envelope and returns justresult. Pass-through cases (empty, non-JSON, malformed, emptyresult) return the input unchanged so:SendPrompt/SendPromptWithFile(no--output-format json) are unaffected.Wired into
SendPromptAgenticonly.Test plan
TestExtractClaudeCodeResultcovers: real envelope (incl.contextWindowmetadata), plain text, empty, malformed JSON, envelope with emptyresult, leading whitespacego test ./utils/...green🤖 Generated with Claude Code