Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(llm-json): a ``` inside a JSON string no longer truncates fenced output (#6093)

time-attackc02fba95123d
diff --git a/src/core/llm-json.ts b/src/core/llm-json.ts
index 67e9cb7969..a7c9fb4d5f 100644
--- a/src/core/llm-json.ts
+++ b/src/core/llm-json.ts
@@ -7,8 +7,11 @@
* 1. Strip ```json...``` fences if present, then JSON.parse.
* 2. Direct JSON.parse.
* 3. Find the first {...} substring (or [...] when array=true) and parse.
- * 4. Retry 1-3 with reasoning blocks stripped (see stripReasoningBlocks).
- * 5. Return null.
+ * 4. When a fence was found and 1-3 failed on its extract, retry 2-3 from
+ * the opening fence (or on the whole text): a ``` inside a JSON string
+ * value ends the non-greedy fence extract early.
+ * 5. Retry 1-4 with reasoning blocks stripped (see stripReasoningBlocks).
+ * 6. Return null.
*
* Adversarial input throws are swallowed; callers get null on any failure.
*/
@@ -95,7 +98,20 @@ export function parseLlmJson<T>(raw: string, opts: { array?: boolean } = {}): T
function parseLlmJsonInner<T>(raw: string, opts: { array?: boolean } = {}): T | null {
if (typeof raw !== 'string' || !raw.trim()) return null;
const fenceMatch = raw.match(/```(?:json)?\s*\n?([\s\S]*?)```/i);
- const cleaned = (fenceMatch ? fenceMatch[1] : raw).trim();
+ if (fenceMatch) {
+ const fenced = parseJsonCandidate<T>(fenceMatch[1].trim(), opts);
+ if (fenced !== null) return fenced;
+ // Only after the extract failed (so every payload that parsed before parses
+ // the same way): retry from the opening fence when it starts a line, else
+ // on the whole text, because the ``` sat inside a JSON string.
+ const at = fenceMatch.index ?? 0;
+ const startsLine = raw.slice(raw.lastIndexOf('\n', at - 1) + 1, at).trim() === '';
+ return parseJsonCandidate<T>((startsLine ? raw.slice(at) : raw).trim(), opts);
+ }
+ return parseJsonCandidate<T>(raw.trim(), opts);
+}
+
+function parseJsonCandidate<T>(cleaned: string, opts: { array?: boolean }): T | null {
try {
const direct = JSON.parse(cleaned);
if (opts.array && Array.isArray(direct)) return direct as T;
diff --git a/test/llm-json-fenced-quotes.test.ts b/test/llm-json-fenced-quotes.test.ts
new file mode 100644
index 0000000000..868348385c
--- /dev/null
+++ b/test/llm-json-fenced-quotes.test.ts
@@ -0,0 +1,81 @@
+/**
+ * #6069: a ``` inside a JSON string value (a verbatim quote of a code block,
+ * common in conversation transcripts) must not cut the payload short.
+ *
+ * 1. Protects: parseLlmJson returns the model's object or array when a string
+ * value holds ``` (a pair or a single one), fenced or not, for the object
+ * shape (significance judge, gateway expansion, subagent oneshot) and the
+ * array shape (conversation-parser LLM fallback).
+ * 2. Fails when: the fenced extract is again the only candidate, so the
+ * non-greedy fence regex ends the payload at the first inner ```.
+ * 3. Not covered before: the reasoning-ladder tests never put ``` inside a
+ * string value.
+ * 4. No new seam: every assertion goes through the public entry points that
+ * exist before the fix (importing a new helper here would turn a
+ * reverted-source run into a module-load failure).
+ */
+import { describe, expect, test } from 'bun:test';
+import { parseLlmJson } from '../src/core/llm-json.ts';
+import { judgeSignificance, type JudgeClient } from '../src/core/cycle/synthesize.ts';
+import type { DiscoveredTranscript } from '../src/core/cycle/transcript-discovery.ts';
+
+const F = '```';
+const verdict = (quote: string) => ({
+ score: 0.8, content_type: 'technical', segments: [{ quote, note: 'why' }], entities: [], reasons: ['a', 'b'],
+});
+const pretty = (v: unknown) => JSON.stringify(v, null, 2);
+const pair = `run ${F}bash gbrain doctor${F} first`;
+const single = `the fix starts with ${F}ts`;
+
+describe('parseLlmJson: ``` inside a JSON string value (#6069)', () => {
+ test.each([
+ ['fenced object, quote holds a ``` pair', `${F}json\n${pretty(verdict(pair))}\n${F}`, verdict(pair)],
+ ['fenced object, quote holds one ``` (a clipped code block)', `${F}json\n${pretty(verdict(single))}\n${F}`, verdict(single)],
+ ['unfenced object, quote holds a ``` pair', pretty(verdict(pair)), verdict(pair)],
+ ['fenced object with prose around it', `Here is the verdict:\n${F}json\n${pretty(verdict(pair))}\n${F}\nDone.`, verdict(pair)],
+ ['reasoning block before a fenced object with a ``` pair', `<think>draft {"score": 0.1}</think>\n${F}json\n${pretty(verdict(pair))}\n${F}`, verdict(pair)],
+ ['a brace in prose before a fenced object with a ``` pair', `Note {see below}:\n${F}json\n${pretty(verdict(pair))}\n${F}`, verdict(pair)],
+ ])('%s', (_name, raw, expected) => {
+ expect(parseLlmJson<unknown>(raw)).toEqual(expected);
+ });
+
+ test('array shape (conversation-parser fallback): fenced array whose strings hold ```', () => {
+ const turns = [{ role: 'user', text: `try ${F}ls -la${F}` }, { role: 'assistant', text: `${F}py` }];
+ expect(parseLlmJson<unknown>(`${F}json\n${pretty(turns)}\n${F}`, { array: true })).toEqual(turns);
+ });
+
+ test.each([
+ // Both pass before and after the fix: the retry runs only after the fenced extract failed.
+ ['a plain fenced object', `${F}json\n{"score": 0.5}\n${F}`, { score: 0.5 }],
+ ['a fenced object followed by prose holding another object', `${F}json\n{"score": 0.5}\n${F}\nSee {"note": 1}.`, { score: 0.5 }],
+ ])('unchanged success path: %s', (_name, raw, expected) => {
+ expect(parseLlmJson<unknown>(raw)).toEqual(expected);
+ });
+
+ test.each([
+ ['a fenced object truncated inside a quote', `${F}json\n{"score": 0.8, "segments": [{"quote": "run ${F}bash`],
+ // The retry starts at the fence, so a draft in the reasoning block cannot stand in for the cut answer.
+ ['a reasoning draft before a truncated fenced answer', `<think>draft {"score": 0.1}</think>\n${F}json\n{"score": 0.8, "segments": [{"quote": "run ${F}bash`],
+ ])('still null: %s', (_name, raw) => {
+ expect(parseLlmJson(raw)).toBeNull();
+ });
+});
+
+describe('judgeSignificance: a quoted code block no longer makes the verdict unparseable (#6069)', () => {
+ test('a fenced verdict whose quote holds a ``` pair is a scored, reliable verdict', async () => {
+ const judge: JudgeClient = {
+ create: async () => ({
+ content: [{ type: 'text', text: `${F}json\n${pretty(verdict(pair))}\n${F}` }],
+ stop_reason: 'end_turn',
+ } as never),
+ };
+ const t: DiscoveredTranscript = {
+ filePath: '/corpus/fenced.txt', contentHash: 'hash-fenced'.padEnd(20, '0'),
+ content: `user: ${pair}\n`.repeat(20), basename: 'fenced', inferredDate: null,
+ };
+ const r = await judgeSignificance(judge, t, 'anthropic:claude-haiku-4-5-20251001');
+ expect(r.unreliable).toBeUndefined();
+ expect(r.score).toBe(0.8);
+ expect(r.segments).toEqual([{ quote: pair, note: 'why' }]);
+ });
+});