fix(backend): 多章链设显式 recursion_limit 防御硬化(CR-C2)
This commit is contained in:
@@ -572,6 +572,62 @@ async def test_run_chain_job_no_conflict_completes_done(_patched_runner: None) -
|
||||
assert [a["chapter_no"] for a in fakes["accepted"]] == [1, 2]
|
||||
|
||||
|
||||
async def test_run_chain_job_sets_recursion_limit_scaled_to_count(
|
||||
_patched_runner: None, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""CR-C2:run_chain_job 给 graph.ainvoke 传显式 top-level recursion_limit(防御硬化)。
|
||||
|
||||
langgraph 默认上界已足够(无实时崩溃),但每链上界应显式 = count*NODES_PER_CHAPTER +
|
||||
HEADROOM。断言 config 键值 + 仍正常跑完(行为不变)。用 PROXY 包裹真 graph 记录 config
|
||||
(不 setattr 到可能冻结的 CompiledStateGraph 上)。
|
||||
"""
|
||||
from ww_core.orchestrator.chain import build_chain_graph as real_build
|
||||
|
||||
captured: dict[str, Any] = {}
|
||||
|
||||
class _SpyGraph:
|
||||
def __init__(self, inner: Any) -> None:
|
||||
self._inner = inner
|
||||
|
||||
async def ainvoke(self, inp: Any, *, config: Any = None, **kw: Any) -> Any:
|
||||
captured["config"] = config
|
||||
return await self._inner.ainvoke(inp, config=config, **kw)
|
||||
|
||||
def _spy_build(*args: Any, **kwargs: Any) -> Any:
|
||||
return _SpyGraph(real_build(*args, **kwargs))
|
||||
|
||||
monkeypatch.setattr("ww_api.services.chain_runner.build_chain_graph", _spy_build)
|
||||
|
||||
fakes = _chain_runner_fakes()
|
||||
gateway = _FakeChainGateway(conflicts=[])
|
||||
job_id = uuid.uuid4()
|
||||
saver = MemorySaver()
|
||||
|
||||
await run_chain_job(
|
||||
_session_factory,
|
||||
job_id,
|
||||
project_id=uuid.uuid4(),
|
||||
user_id=USER,
|
||||
chain_key="draft_volume",
|
||||
start_chapter_no=1,
|
||||
count=7,
|
||||
chain_gateway_builder=_async_returning(gateway),
|
||||
checkpointer_ctx=lambda: _memsaver_ctx(saver),
|
||||
accept_op=fakes["accept_op"],
|
||||
chapter_repo_factory=fakes["chapter_repo_factory"],
|
||||
review_repo_factory=fakes["review_repo_factory"],
|
||||
)
|
||||
|
||||
# (1) 显式上界 = count*NODES_PER_CHAPTER + HEADROOM = 7*4+10 = 38。
|
||||
assert captured["config"]["recursion_limit"] == 7 * 4 + 10 == 38
|
||||
# (2) recursion_limit 是 top-level 键(不在 configurable 下——langgraph 只认 top-level)。
|
||||
assert "recursion_limit" not in captured["config"]["configurable"]
|
||||
# (3) 仍正常跑完(行为不变)。
|
||||
rec = _RecordingJobRepo.state[job_id]
|
||||
assert rec["status"] == STATUS_DONE
|
||||
assert rec["result"]["written"] == list(range(1, 8))
|
||||
|
||||
|
||||
async def test_run_chain_job_conflict_sets_awaiting_then_resume_done(
|
||||
_patched_runner: None,
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user