fix: tolerate stderr noise prefix before CLI JSON output
ober
c7d0339c188d9d013acd8b666d59ef314ca076a1
--- a/src/jcode/tool/external-llm.ss +++ b/src/jcode/tool/external-llm.ss @@ -378,8 +378,25 @@ ;; ---------- per-provider parsers ---------- +(def (find-char-index s c) + (let ((n (string-length s))) + (let lp ((i 0)) + (cond + ((>= i n) #f) + ((char=? (string-ref s i) c) i) + (else (lp (+ i 1))))))) + (def (safe-parse-json s) - (try (string->json-object s) (catch (e) #f))) + ;; Try strict parse first. If it fails (e.g. CLI leaked stderr lines + ;; into the captured output before the JSON body), retry from the + ;; first '{' so a leading noise prefix doesn't break extraction. + (try (string->json-object s) + (catch (e) + (let ((start (find-char-index s #\{))) + (if start + (try (string->json-object (substring s start (string-length s))) + (catch (e2) #f)) + #f))))) (def (parse-claude-json text session-id-in) ;; claude --output-format=json produces a single JSON object with