fix(growth): robust json:candidate extraction (#3211)

The sed + tr approach grabbed invalid JSON when Claude's output had
multiple candidate-like blocks or mixed analysis text. Switch to bun
script that tries to JSON.parse each match, keeping the last valid one.

Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Ahmed Abushagur <ahmed@abushagur.com>
This commit is contained in:
A 2026-04-06 16:46:03 -07:00 committed by GitHub
parent 714c29c5a6
commit 1e858503cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -162,9 +162,17 @@ fi
# --- Phase 3: Extract candidate and POST to SPA ---
CANDIDATE_JSON=""
# Extract the json:candidate block from Claude's output (may be multi-line)
# Extract the last valid json:candidate block from Claude's output
if [[ -f "${CLAUDE_OUTPUT_FILE}" ]]; then
CANDIDATE_JSON=$(sed -n '/^```json:candidate$/,/^```$/{/^```/d;p;}' "${CLAUDE_OUTPUT_FILE}" | tr -d '\n')
CANDIDATE_JSON=$(bun -e "
const text = await Bun.file('${CLAUDE_OUTPUT_FILE}').text();
const blocks = [...text.matchAll(/\`\`\`json:candidate\n([\s\S]*?)\n\`\`\`/g)];
let result = '';
for (const block of blocks) {
try { result = JSON.stringify(JSON.parse(block[1].trim())); } catch {}
}
if (result) console.log(result);
" 2>/dev/null)
fi
if [[ -z "${CANDIDATE_JSON}" ]]; then