fix(core): read a JSON answer fenced as ```JSON or ```jsonc
AA
2h ago+45+45+01234567890123456789-1-1-0123456789
What
A judgment answer read from reply text may be fenced as ```JSON, ```Json, or ```jsonc, as well as ```json or untagged. That is every answer from an executor's answer(), and the built-in model's reply when agent.extract has no JSON Schema to send.
Why
The fence check stripped a lowercase json tag only, so any other spelling stayed in the body and the answer failed as MODEL_OUTPUT_INVALID. A deterministic executor sends the same text each try, so agent.waitFor used up its 25 calls on it.
How it works
parseJsonObjectstrips a fence taggedjsonorjsoncin any case (packages/core/src/agent/model/adapter.ts).fenceTagLengthchecks the tags inJSON_FENCE_TAGS, longest first, lowercased, and keeps main's linear-time slicing with no regex. I kept the tag list closed instead of skipping any info string, because on a one-line fence a bare word can be the value (```true```), and main reads that astrue. Ajsoncbody with comments still fails atJSON.parse. The tag has to end at a character that can't be part of one, so```json{...}```reads as on main, whilejsontrueorjson5stays an unknown tag in the body (main stripped anyjsonprefix).docs/executors.mdxnames the accepted fence tags and saysjsoncdoesn't allow comments.
Issue
Fixes #2720
Verified
Ran it locally: yes
Testbed, built CLI, a custom executor whose answer() returns a valid agent-judgment-2 reply fenced with FENCE_TAG, e2e run on a test that calls agent.waitFor:
| fence tag | main | this PR |
|---|---|---|
json | passes, 1 model call | passes, 1 model call |
JSON | MODEL_OUTPUT_INVALID: response text is not a single JSON value, 25 model calls failed | passes, 1 model call |
Json | MODEL_OUTPUT_INVALID, 25 model calls failed | passes, 1 model call |
jsonc | MODEL_OUTPUT_INVALID, 25 model calls failed | passes, 1 model call |
packages/e2e/tests/unit/parse-validated.test.ts, beside the parser's existing cases:JSON,Json,jsonc,JSONwith CRLF line ends, a one-line```json {...}```, and one-linejson{...}/jsonc{...}with no space all read.jsontrue,jsoncfalse,json5, and text after the closing fence fail asMODEL_OUTPUT_INVALID. With main'sadapter.ts, 6 of the 13 fail (JSON,Json,jsonc, CRLF, one-linejsonc{...},jsontrue).
Checklist
- Changeset added (
pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only. - Breaking change: title carries
!, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md). - Docs updated in the same PR: the
docs/**/*.mdxpage for the behavior, andskills/e2e/if the skill describes it. - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.
Summary by CodeRabbit
- Bug Fixes
- JSON responses now recognize case-insensitive
JSONandjsonccode fences, reducing invalid-output errors when models use these formats. - Fence tags with extra text and responses with trailing text after the closing fence remain invalid.
- JSON responses now recognize case-insensitive
- Documentation
- Clarified support for
jsoncfences in executor responses and noted that comments are not allowed.
- Clarified support for
- Tests
- Added coverage for varied tag casing, line endings, and spacing.