parse_json_response extracted only the first fenced block, so a reply
that shows a code snippet (for example a ```python block) before the
fenced JSON payload lost the payload: it returned a list literal from the
snippet or the fallback. Prefer the first fenced block that holds a JSON
object or array, and keep the first block otherwise.
The raw_decode scan added for trailing brace-prose returned the first
decodable value in the response. DeepSeek-style reasoning preludes often
embed a small schema example (e.g. {"chapters": []}) before the real
payload, so the example shadowed the actual object and BookEngine's
SpineAgent fell back to a single Overview chapter. Scan every top-level
candidate and keep the longest decode instead.
Reasoning models (Qwen, DeepSeek, etc.) emit chain-of-thought inside
<think>...</think> blocks before the JSON payload. parse_json_response()
did not account for this: a brace inside the reasoning was picked up by
the raw_decode scan and returned instead of the real object, silently
yielding wrong or empty data (e.g. empty BookEngine proposal fields).
Strip <think> blocks only on the direct-parse failure path, so a valid
JSON payload whose string values legitimately contain the substring
'<think>' is preserved exactly. Also drop an unclosed leading <think>
prelude up to the first JSON opener, and return the fallback when the
response is reasoning-only with no payload.
Fixes#673
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
json-repair wrapped a valid object plus trailing brace-prose into an array,
corrupting book/question callers that expect a single object. Prefer
raw_decode of the first JSON value before repair.
- Add stage_failure_counts/stage_failure_notes to LearningProgress for persistent failure tracking
- Wrap RAG retrieval with 10s independent timeout in _call_llm_impl
- Add _call_llm_with_timeout (default 60s) and _call_llm_with_degradation (bounded retry + skip)
- Apply degradation to all 12 stage handlers with stage-specific fallback paths
- Extract _run_interactive_quiz_loop shared helper for practice/practice_quiz
- Extract StructuredStageContent component from page.tsx
- Add TypeError to JSON parse except clauses across 7 files
- Simplify _record_attempt_and_update_mastery to single save exit
- Move import logging to module top in service.py
The original implementation converted `fallback=None` to `{}`,
making `if data is None:` checks dead code in data_structures,
note_agent and reporting_agent. Use a `_UNSET` sentinel so callers
can explicitly request `None` as the failure value.
Also simplify control flow in data_structures and reporting_agent
to avoid raising ValueError inside bare try/except that wouldn't
catch it.
Made-with: Cursor