From bcee050c4a687173b4d791a22257ba37593f6317 Mon Sep 17 00:00:00 2001 From: rebuildup Date: Mon, 23 Nov 2026 13:41:16 +0900 Subject: [PATCH] fix(agent): recover verdict from duplicated JSON objects in agy response (#227) claude-sonnet-5-5-high via agy returns the verdict object twice in one response; JSON.parse fails on the concatenation and every run classified invalid-output. extractJsonValues now scans top-level JSON values and the last complete object is used as the verdict candidate; unparseable responses still fail closed. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- script/agent.agy-review.test.ts | 18 +++++++++++ script/agent/agy-review.ts | 57 ++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/script/agent.agy-review.test.ts b/script/agent.agy-review.test.ts index 5d1a27a..3bee8ea 100644 --- a/script/agent.agy-review.test.ts +++ b/script/agent.agy-review.test.ts @@ -279,6 +279,24 @@ describe("runLocal pipeline (stub agy)", () => { expect(existsSync(join(result.isolation.workdir, "inputs", "00-shot.png"))).toBe(true); }); + it("recovers the verdict when the response duplicates the JSON object", async () => { + // Observed with claude-sonnet-5-5-high: agy returns two complete verdict + // objects concatenated; the last one is the final answer (#227). + const dupResponse = [ + JSON.stringify({ verdict: "approve", summary: "draft", findings: [] }), + JSON.stringify({ + verdict: "request-changes", + summary: "final", + findings: [{ severity: "minor", description: "x", location: "top-left" }], + }), + ].join("\n"); + const dup = JSON.stringify({ status: "SUCCESS", response: dupResponse, denied_actions: [] }); + const stub = makeStub("dup.sh", `cat <<'EOF'\n${dup}\nEOF`); + const result = await runAgyReview({ ...base(), agyBin: stub }); + expect(result.classification).toBe("review-completed"); + expect(result.verdict).toMatchObject({ verdict: "request-changes", summary: "final" }); + }); + it("readonly-violation when the model hits deny rules", async () => { const denied = JSON.stringify({ status: "SUCCESS", diff --git a/script/agent/agy-review.ts b/script/agent/agy-review.ts index 42474cd..06f1991 100644 --- a/script/agent/agy-review.ts +++ b/script/agent/agy-review.ts @@ -616,6 +616,50 @@ export function releaseReviewLock(path: string, id: string): void { } } +/** + * Extract every complete top-level `{...}` JSON value from `text`, in + * order. Tolerates prose/fences/duplicated objects around the payload; + * unparseable candidates are skipped. + */ +export function extractJsonValues(text: string): unknown[] { + const values: unknown[] = []; + let i = 0; + while (i < text.length) { + const start = text.indexOf("{", i); + if (start === -1) break; + let depth = 0; + let inStr = false; + let esc = false; + let end = -1; + for (let j = start; j < text.length; j++) { + const c = text[j]; + if (inStr) { + if (esc) esc = false; + else if (c === "\\") esc = true; + else if (c === '"') inStr = false; + continue; + } + if (c === '"') inStr = true; + else if (c === "{") depth++; + else if (c === "}") { + depth--; + if (depth === 0) { + end = j; + break; + } + } + } + if (end === -1) break; + try { + values.push(JSON.parse(text.slice(start, end + 1))); + } catch { + // Not a complete JSON object — skip past it. + } + i = end + 1; + } + return values; +} + // --------------------------------------------------------------------------- // Orchestration // --------------------------------------------------------------------------- @@ -871,20 +915,23 @@ async function runLocal( let verdict: unknown = null; let schemaValid = false; if (envelope?.response) { - try { - const parsed = JSON.parse(envelope.response); + // Some models (observed: claude-sonnet via agy) emit the verdict object + // more than once in one response. Take the LAST complete top-level JSON + // value — earlier copies are drafts/duplicates — and keep failing closed + // when nothing parses. + const candidates = extractJsonValues(envelope.response); + const parsed = candidates.length > 0 ? candidates[candidates.length - 1] : null; + if (parsed !== null) { // Minimal schema check: required keys present with right primitive types. // The model-facing --json-schema enforces the full contract; this layer // guards against a harness/CLI drift producing plausible-but-wrong JSON. if (isPlainObject(schema) && Array.isArray((schema as Record)["required"])) { const required = (schema as Record)["required"] as string[]; - schemaValid = required.every((k) => k in parsed); + schemaValid = required.every((k) => k in (parsed as Record)); } else { schemaValid = true; } verdict = schemaValid ? parsed : null; - } catch { - schemaValid = false; } } result.verdict = verdict;