Skip to content

fix(triage): harden JSON parsing against model preamble in parseDecisions#165

Merged
keinstn merged 4 commits into
developfrom
agent/keinstn__baton-161
Jun 24, 2026
Merged

fix(triage): harden JSON parsing against model preamble in parseDecisions#165
keinstn merged 4 commits into
developfrom
agent/keinstn__baton-161

Conversation

@keinstn

@keinstn keinstn commented Jun 24, 2026

Copy link
Copy Markdown
Owner

Fixes #161

Summary

  • parseDecisions in scripts/triage/parse.ts now extracts the JSON array by finding the outermost [ and ] before calling JSON.parse, so any conversational preamble emitted by the model (e.g. "I'll check each issue...") no longer causes a SyntaxError
  • Falls back to the full stripped string when no brackets are present, preserving the original JSON.parse error message for non-JSON responses
  • Added test/triage-parse.test.ts with unit tests covering bare JSON, preamble, preamble+postamble, code-fenced preamble, no-JSON, non-array, and invalid field cases

Test plan

  • npm run test — 347 tests pass (29 test files)
  • npm run typecheck — no errors
  • npm run lint — no errors
  • New test: parseDecisions("I'll check each issue.\n[{...}]") parses successfully
  • New test: parseDecisions("no json here") still throws SyntaxError

🤖 Generated with Claude Code

…ions

Extract the JSON array by finding the outermost `[` and `]` before
calling JSON.parse, so conversational preamble emitted by the model
no longer causes a SyntaxError. Falls back to the full string (and
the original error) when no brackets are present.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@keinstn

keinstn commented Jun 24, 2026

Copy link
Copy Markdown
Owner Author

[Baton Reviewer] [must] The bracket scanner can still misparse or fail on bracket characters inside prose or JSON string values. A preamble like See [1,2] can be extracted as the wrong array, and brackets in reason strings can break depth counting because the helper does not track quoted strings/escapes.
Managed by Baton; do not edit the marker line manually.

@keinstn keinstn self-assigned this Jun 24, 2026

@keinstn keinstn left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indexOf/lastIndexOf bracket extraction has two confirmed misfire cases and one silent-wrong-data regression. The core issue is that character-level bracket scanning cannot distinguish JSON delimiters from brackets in natural-language prose (issue refs like [#42], markdown, etc.). See inline comments for specifics.

Comment thread scripts/triage/parse.ts Outdated
Comment thread scripts/triage/parse.ts Outdated
Comment thread scripts/triage/parse.ts Outdated
Comment thread test/triage-parse.test.ts
Comment thread scripts/triage/parse.ts Outdated
keinstn and others added 2 commits June 25, 2026 03:36
…isfires

Replace the indexOf/lastIndexOf bracket extraction with a balanced-bracket
scanner that tries each outermost [...] candidate in order, returning the
first one that parses as valid JSON. Also try JSON.parse on the full clean
string first so well-formed output skips extraction entirely.

Adds tests covering preamble containing '[' and postamble containing ']',
both of which the original indexOf/lastIndexOf approach misfired on.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread scripts/triage/parse.ts
…actJsonArray

Brackets inside JSON string values (e.g. `"reason": "] edge case"`) could
cause the depth counter to hit zero prematurely, making the scanner return
a bad substring rather than the full decisions array. Fix by tracking
quoted-string and escape state so brackets inside strings are ignored.

Also require each candidate to be an array of objects (not scalars), so a
preamble like "See [1,2]" is not mistaken for the decisions array.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@keinstn
keinstn merged commit 75e1ba6 into develop Jun 24, 2026
4 checks passed
@keinstn
keinstn deleted the agent/keinstn__baton-161 branch June 24, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(triage): harden JSON parsing against model preamble in parseDecisions

1 participant