Conversation
The ACPBridge LLM sometimes returns JSON wrapped in markdown code fences or with leading text. Strip code fences and find the JSON object before parsing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Greptile SummaryThis PR fixes a parsing failure in Changes made:
Issue found:
Confidence Score: 3/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[LLM returns result.text] --> B{Contains backtick-json fence?}
B -- Yes --> C[Strip everything before fence opening\nSlice from upperBound]
C --> D{Closing fence found?}
D -- Yes --> E[Slice up to closing fence]
D -- No --> F[Keep remainder as-is]
E --> G
F --> G
B -- No --> H{Contains plain backtick fence?}
H -- Yes --> I[Strip everything before fence opening]
I --> J{Closing fence found?}
J -- Yes --> K[Slice up to closing fence]
J -- No --> L[Keep remainder as-is]
K --> G
L --> G
H -- No --> G
G{First curly brace found?}
G -- Yes --> M[Slice from first brace onward\n⚠️ trailing text NOT removed]
G -- No --> N[responseText unchanged]
M --> O[Trim whitespace]
N --> O
O --> P{JSONSerialization succeeds?}
P -- Yes --> Q[Parse memories, tasks, profile]
P -- No --> R[Log failure, return 0,0,empty]
Last reviewed commit: 9c975c8 |
| if let braceStart = responseText.firstIndex(of: "{") { | ||
| responseText = String(responseText[braceStart...]) | ||
| } |
There was a problem hiding this comment.
Trailing text not stripped — parsing can still fail
The braceStart approach correctly removes content before the first {, but leaves anything after the closing } intact. If the LLM appends trailing prose (e.g., \n\nLet me know if you need more info.) after the JSON object and outside of any code fence, JSONSerialization will still fail because of the unexpected trailing text.
Since the PR description explicitly mentions fixing the "leading text" case, this gap should also be closed. A simple, more robust approach is to pair firstIndex(of: "{") with lastIndex(of: "}"):
| if let braceStart = responseText.firstIndex(of: "{") { | |
| responseText = String(responseText[braceStart...]) | |
| } | |
| if let braceStart = responseText.firstIndex(of: "{"), | |
| let braceEnd = responseText.lastIndex(of: "}"), | |
| braceStart <= braceEnd { | |
| responseText = String(responseText[braceStart...braceEnd]) | |
| } |
This handles responses that have both a preamble and a postamble around the raw JSON object.
## Summary
- LLM (ACPBridge) sometimes returns JSON wrapped in ```json code fences
or with leading text
- This caused "Failed to parse synthesis response" even when events were
fetched successfully
- Fix: strip markdown code fences, find first `{` character, trim before
parsing
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Summary
{character, trim before parsing🤖 Generated with Claude Code