Part of the "Add an automated backend test suite" parent issue. Depends on the pytest-scaffolding sub-issue.
Background
backend/app/jsonblock.py extracts a JSON object from raw LLM output. It has four strategies (fenced ```json block, any fenced block, last balanced {...}, and a brace-counting truncation-recovery pass in `_try_truncation_recovery`). All designer/critic/orchestrator/extractor parsing depends on it, so a regression here silently breaks every agent.
Problem / Goal
The module is pure (no I/O) and completely untested. Add thorough unit tests.
Where to look
backend/app/jsonblock.py — extract_json (entry point) and helpers _clean, _try_parse, _try_truncation_recovery, _last_balanced_object.
- The module docstring lists the exact cases it's meant to handle (nested objects, trailing commas,
// and /* */ comments, truncation). Use it as the test checklist.
Suggested approach
Write backend/tests/test_jsonblock.py covering at least:
- Clean fenced ```json block → parsed dict.
- Unfenced JSON with trailing prose after it → last balanced object wins.
- Nested objects like
{"citations": [{"page": 42}]} → returns the full object, not the first inner } (the bug the module was written to fix).
- Trailing commas before
}/] and // / /* */ comments → recovered via _clean.
- A brace inside a string value, e.g.
{"note": "value with } brace"} → not fooled by the }.
- Truncation: input like
{"a": 1, "b": [{"c": 1}, {"c": 2}, {"c": 3 → recovers {"a":1,"b":[{"c":1},{"c":2}]} (the docstring's own example).
- Empty string / no JSON at all → returns
None.
Acceptance criteria
test_jsonblock.py covers all seven cases above and passes.
- Each of the four extraction strategies is exercised by at least one test.
Testing
cd backend && pytest tests/test_jsonblock.py -q.
Out of scope
- Changing
jsonblock.py behavior — this is characterization/coverage only. If a test surfaces a genuine bug, file it separately.
Part of the "Add an automated backend test suite" parent issue. Depends on the pytest-scaffolding sub-issue.
Background
backend/app/jsonblock.pyextracts a JSON object from raw LLM output. It has four strategies (fenced ```json block, any fenced block, last balanced{...}, and a brace-counting truncation-recovery pass in `_try_truncation_recovery`). All designer/critic/orchestrator/extractor parsing depends on it, so a regression here silently breaks every agent.Problem / Goal
The module is pure (no I/O) and completely untested. Add thorough unit tests.
Where to look
backend/app/jsonblock.py—extract_json(entry point) and helpers_clean,_try_parse,_try_truncation_recovery,_last_balanced_object.//and/* */comments, truncation). Use it as the test checklist.Suggested approach
Write
backend/tests/test_jsonblock.pycovering at least:{"citations": [{"page": 42}]}→ returns the full object, not the first inner}(the bug the module was written to fix).}/]and////* */comments → recovered via_clean.{"note": "value with } brace"}→ not fooled by the}.{"a": 1, "b": [{"c": 1}, {"c": 2}, {"c": 3→ recovers{"a":1,"b":[{"c":1},{"c":2}]}(the docstring's own example).None.Acceptance criteria
test_jsonblock.pycovers all seven cases above and passes.Testing
cd backend && pytest tests/test_jsonblock.py -q.Out of scope
jsonblock.pybehavior — this is characterization/coverage only. If a test surfaces a genuine bug, file it separately.