Files
DeepTutor/tests/services/config/test_book_agent_params.py
Bingxi Zhao (Frank) 5ac8e2c9d3 fix(book): give Book a reachable token budget, and retry a starved plan
A book built with a reasoning model arrives as a single "Overview"
chapter, or with the right chapter count and every learning objective
and summary empty (#1316). Two independent faults compose into that.

`get_agent_params` maps a module name to a section of agents.yaml, and
`book` was not in the map — so the function returned the source-level
`global_defaults` of 4096 *before* ever opening the file. That budget
was therefore not the shipped default and not overridable: adding a
`capabilities.book` block did nothing, and only editing the source could
change it. It is now mapped, seeded at 12000 in DEFAULT_AGENTS_SETTINGS
(so a stale agents.yaml still gets it), and honoured from the yaml.
12000 rather than a larger figure matches `research`, the closest
analogue, and keeps the request under the output cap of providers that
enforce one.

The budget is the trigger, not the fault. A reasoning model pays for its
hidden tokens out of the same `max_tokens` as its answer, so on a large
prompt asking for a whole JSON object it can spend the lot and emit
nothing. The provider does not raise — it returns a truncated body, or
none — and `parse_json_response` hands back the caller's fallback, which
in `SpineSynthesizer._materialise` is one placeholder chapter. Where the
budget ran out *mid*-object, json-repair closes the fragment and the
caller gets chapters whose objectives never arrived: the same failure
wearing a different face.

`blocks/_llm_writer.llm_json` already knew the escape hatch — ask again
with thinking turned down — but only block generators went through it.
The four pipeline agents each parsed their own response and had no retry
at all. `book/json_retry.py` is now the one place that rule lives, and
ideation, both source-explorer calls, all three spine rounds and the
page planner go through it; `llm_json` shares its predicate so the two
cannot drift. Each caller names the key that proves the payload arrived,
which is what lets a repaired-but-gutted object trigger the retry rather
than settle the stage.

`BaseAgent.call_llm` / `stream_llm` gained the `reasoning_effort`
parameter this needs; it was already the shape `llm_text` used.
2026-09-10 22:50:45 +08:00

51 lines
1.9 KiB
Python

"""The Book module's token budget must be resolvable and user-overridable.
``get_agent_params("book")`` returned the 4096 source-level fallback because
``book`` was absent from ``section_map`` — the function returned *before*
opening agents.yaml, so adding a ``capabilities.book`` block did nothing. A
reasoning model spends that budget on hidden tokens and leaves the spine stage
an empty response, which degrades a whole book to one "Overview" chapter
(#1316).
"""
from __future__ import annotations
from pathlib import Path
from typing import Any
import yaml
from deeptutor.services.config import loader as loader_module
from deeptutor.services.config.loader import get_agent_params
from deeptutor.services.setup.init import DEFAULT_AGENTS_SETTINGS
def _write_agents_yaml(tmp_path: Path, content: dict[str, Any]) -> Path:
settings_dir = tmp_path / "data" / "user" / "settings"
settings_dir.mkdir(parents=True, exist_ok=True)
(settings_dir / "agents.yaml").write_text(yaml.dump(content), encoding="utf-8")
return tmp_path
def test_book_falls_back_to_the_shipped_default_not_the_global_4096(
tmp_path: Path, monkeypatch
) -> None:
"""A stale agents.yaml (no ``book`` block) still gets the real budget."""
project_root = _write_agents_yaml(tmp_path, {"capabilities": {"solve": {"temperature": 0.3}}})
monkeypatch.setattr(loader_module, "PROJECT_ROOT", project_root)
params = get_agent_params("book")
assert params["max_tokens"] == DEFAULT_AGENTS_SETTINGS["capabilities"]["book"]["max_tokens"]
assert params["max_tokens"] > 4096
def test_book_budget_is_overridable_from_agents_yaml(tmp_path: Path, monkeypatch) -> None:
project_root = _write_agents_yaml(
tmp_path,
{"capabilities": {"book": {"temperature": 0.2, "max_tokens": 32000}}},
)
monkeypatch.setattr(loader_module, "PROJECT_ROOT", project_root)
assert get_agent_params("book") == {"temperature": 0.2, "max_tokens": 32000}