Parse JSON output from CodingAgent even when it includes spurious text #306
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.
Parse JSON output from CodingAgent even when it includes spurious text
Closes #305
Summary
Improves JSON parsing in
AgentStep
to handle markdown code blocks anywhere in the response, not just at the beginning and end. This fixes issues where CodingAgent provides explanatory text around JSON output blocks.Problem
The previous implementation in
lib/roast/workflow/agent_step.rb
only checked for markdown code block indicators on the first and last lines of the response. This caused parsing failures when agents provided context around JSON blocks, which is common in real-world usage despite giving claude explicit instructions to the contrary.Solution
extract_json_from_markdown
method that finds JSON code blocks anywhere in the response/```(?:json)?\s*\n(.*?)\n```/m
to match both```json` and plain
``` blocksChanges
Code Changes
lib/roast/workflow/agent_step.rb
: Replaced inflexible parsing logic withextract_json_from_markdown(result)
calllib/roast/workflow/agent_step.rb
: Added private methodextract_json_from_markdown
with regex-based extractionTest Coverage
test/roast/workflow/agent_step_test.rb
: Added comprehensive test casetest "agent step parses JSON code blocks anywhere in response"
Test Results
All existing tests pass, plus new test case validates the improved functionality:
Backward Compatibility
✅ Fully backward compatible - existing JSON response formats continue to work:
Example
Before (fails):
Review these findings.