fix(flows): 步数预算与图硬上限分离,预算耗尽时优雅收尾

max_steps 以前同时充当软预算和 recursion_limit,于是预算耗尽的那一刻
必然触发 GraphRecursionError:模型连一句收尾都来不及说,用户只收到
「处理消息时出错,请稍后再试」,已产出的内容也不会落库。

- 新增 StepBudgetMiddleware:按模型调用轮数计账(agent_rounds =
  max_steps // 2),剩余 <=4 轮时注入一次「立刻停止调用工具、给出结论」
  的收尾提示,只提醒一次
- 新增 STEP_GRACE(20),recursion_limit = max_steps + STEP_GRACE,把余量
  留给收尾动作
- GraphRecursionError 统一转成 StepBudgetExceeded,ainvoke 与流式两条
  路径都覆盖
- AgentReply 把「步数用尽」渲染成可操作说明,接在已产出内容之后一并
  下发并落库;用 _reported 标记让 on_error 不再补发通用错误文案,任务
  状态仍如实记 failed
- 同步 README / docs/architecture.md / db/schema.py 中 max_steps 的语义

测试:新增 tests/test_budget.py(7 个用例),全量 241 passed。
This commit is contained in:
openbot committed 2026-11-09 21:32:29 +08:00
1 parent fbb01c4c83
commit 5da55f8d17
12 files changed
+531 -40

No files matched your search

+139
View File
@@ -0,0 +1,139 @@
"""步数预算测试:预算换算、收尾提示注入、硬上限转成可读失败。"""
from __future__ import annotations
import asyncio
from typing import Any
import pytest
from langgraph.errors import GraphRecursionError
from bot042.agent.middlewares.budget import (
BUDGET_PREFIX,
BUDGET_WARN_MARGIN,
StepBudget,
StepBudgetExceeded,
StepBudgetMiddleware,
bind,
current_budget,
unbind,
)
from bot042.config import STEP_GRACE, Settings
from bot042.flows import agent_task as agent_task_module
from bot042.flows.agent_task import run_task, stream_task
class RecordingAgent:
"""记录调用配置,并顺带把当时绑定的预算读出来。"""
def __init__(self) -> None:
self.config: dict[str, Any] | None = None
self.remaining: int | None = None
async def ainvoke(
self, state: dict[str, Any], config: dict[str, Any] | None = None
) -> dict[str, Any]:
_ = state
self.config = config
budget = current_budget()
self.remaining = budget.remaining if budget is not None else None
return {"messages": []}
class RecursionBoomAgent:
"""模拟 LangGraph 撞上硬上限。"""
async def ainvoke(
self, state: dict[str, Any], config: dict[str, Any] | None = None
) -> dict[str, Any]:
_ = state, config
raise GraphRecursionError("Recursion limit of 27 reached")
async def astream(self, *args: Any, **kwargs: Any) -> Any:
_ = args, kwargs
raise GraphRecursionError("Recursion limit of 27 reached")
yield # pragma: no cover - 让本方法成为异步生成器
def test_settings_derive_recursion_limit_and_rounds() -> None:
"""硬上限必须大于软预算,轮数预算按「一次往返约 2 步」换算。"""
settings = Settings(max_steps=200)
assert settings.recursion_limit == 200 + STEP_GRACE
assert settings.agent_rounds == 100
# 极小预算也要留出至少一轮,否则中间件永远来不及提醒
assert Settings(max_steps=1).agent_rounds == 1
def test_budget_take_counts_down() -> None:
budget = StepBudget(rounds=2)
assert budget.take() == 1
assert budget.take() == 0
assert budget.take() == 0 # 不为负
assert budget.used == 3
assert budget.remaining == 0
def test_middleware_noop_without_budget() -> None:
middleware = StepBudgetMiddleware()
assert asyncio.run(middleware.abefore_model({}, None)) is None # type: ignore[arg-type]
def test_middleware_warns_once_when_budget_runs_low() -> None:
budget = StepBudget(rounds=BUDGET_WARN_MARGIN + 2)
token = bind(budget)
try:
middleware = StepBudgetMiddleware()
# 剩余 5 → 安静;剩余 4 → 注入收尾提示
assert asyncio.run(middleware.abefore_model({}, None)) is None # type: ignore[arg-type]
update = asyncio.run(middleware.abefore_model({}, None)) # type: ignore[arg-type]
assert update is not None
message = update["messages"][0]
assert isinstance(message.content, str)
assert message.content.startswith(BUDGET_PREFIX)
assert "剩余约 4 轮" in message.content
assert budget.warned is True
# 只提醒一次:反复打断只会让模型更答不完
assert asyncio.run(middleware.abefore_model({}, None)) is None # type: ignore[arg-type]
finally:
unbind(token)
def test_run_task_passes_derived_limit_and_binds_budget(monkeypatch) -> None:
agent = RecordingAgent()
monkeypatch.setattr(
agent_task_module, "create_agent", lambda settings=None: agent
)
settings = Settings(max_steps=7)
asyncio.run(run_task("hi", settings))
assert agent.config == {"recursion_limit": 7 + STEP_GRACE}
assert agent.remaining == settings.agent_rounds # 预算已绑定且未被计数
# 运行结束必须解绑,否则下一个任务会读到上一轮的预算
assert current_budget() is None
def test_run_task_translates_recursion_error(monkeypatch) -> None:
monkeypatch.setattr(
agent_task_module, "create_agent", lambda settings=None: RecursionBoomAgent()
)
with pytest.raises(StepBudgetExceeded) as info:
asyncio.run(run_task("hi", Settings(max_steps=7)))
assert info.value.max_steps == 7
assert "max_steps=7" in str(info.value)
assert current_budget() is None
def test_stream_task_translates_recursion_error(monkeypatch) -> None:
monkeypatch.setattr(
agent_task_module, "create_agent", lambda settings=None: RecursionBoomAgent()
)
async def _consume() -> None:
async for _ in stream_task("hi", Settings(max_steps=7)):
pass
with pytest.raises(StepBudgetExceeded):
asyncio.run(_consume())
assert current_budget() is None
+57 -1
View File
@@ -9,6 +9,7 @@ from typing import Any
import pytest
from bot042.agent.middlewares.budget import StepBudgetExceeded
from bot042.channels.base import BaseChannel
from bot042.channels.manager import ChannelManager
from bot042.channels.models import (
@@ -20,7 +21,13 @@ from bot042.channels.models import (
from bot042.config import Settings
from bot042.db import repo
from bot042.flows import builtin
from bot042.flows.builtin import ERROR_REPLY, RPC_SEND, AgentReply, ChannelServices
from bot042.flows.builtin import (
BUDGET_REPLY,
ERROR_REPLY,
RPC_SEND,
AgentReply,
ChannelServices,
)
from bot042.flows.bus import EventBus
from bot042.flows.rpc import RpcHub
from bot042.flows.scheduler import CN_TZ
@@ -209,3 +216,52 @@ def test_agent_reply_sends_error_hint_on_failure(
sent, statuses = asyncio.run(_run())
assert sent == [ERROR_REPLY]
assert statuses == [RunStatus.FAILED]
def test_agent_reply_explains_step_budget_without_generic_error(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""步数用尽:给出可操作说明并落库,且不再补发通用错误文案。"""
async def exhausted(task, settings=None, history=None, *, cache=None, steering=None):
raise StepBudgetExceeded(200)
monkeypatch.setattr(builtin, "run_task", exhausted)
async def _run() -> tuple[list[str], list[RunStatus]]:
bus = EventBus()
bus.bind_loop()
hub = RpcHub(bus)
manager = ChannelManager()
channel = EchoChannel()
await manager.add_channel(channel)
await manager.start()
_ = hub.serve(RPC_SEND, manager.rpc_send)
services = ChannelServices(
channels=manager, settings=Settings(), db=tmp_path / "bot.db", hub=hub
)
runner = TaskRunner(bus, deps=services)
runner.register(AgentReply)
_ = runner.add_task(AgentReply.name, EventTrigger(event="channel.inbound"))
await runner.start()
bus.emit("channel.inbound", _envelope("把整站重构一遍"))
await runner.wait_idle(timeout=2)
await bus.wait_for_complete()
statuses = [run.status for run in runner.runs(AgentReply.name)]
await runner.stop()
hub.close()
await manager.stop()
return channel.sent, statuses
sent, statuses = asyncio.run(_run())
notice = BUDGET_REPLY.format(steps=200)
assert sent == [notice] # 只有说明,没有多余的「处理消息时出错」
assert statuses == [RunStatus.FAILED]
# 失败轮也要落库:用户已经看到这段内容,历史缺了它会前后不接
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1"))
assert [(r["role"], r["content"]) for r in rows] == [
("user", "把整站重构一遍"),
("assistant", notice),
]
+3 -2
View File
@@ -8,7 +8,7 @@ from typing import Any
from bot042.channels.base import BaseChannel
from bot042.channels.models import ChannelSubject, InboundMessage, TextContent
from bot042.config import Settings
from bot042.config import STEP_GRACE, Settings
from bot042.flows.agent_task import last_message_text, run_task
from bot042.flows.session import run_session
@@ -86,7 +86,8 @@ def test_run_task_uses_async_ainvoke(monkeypatch) -> None:
assert last_message_text(result) == "done"
state, config = fake.calls[0]
assert state["messages"][-1] == {"role": "user", "content": "hi"}
assert config == {"recursion_limit": 7}
# 硬步数上限 = 软预算 + 收尾余量(见 Settings.recursion_limit)
assert config == {"recursion_limit": 7 + STEP_GRACE}
def test_last_message_text_multimodal_content() -> None:
+66 -2
View File
@@ -9,6 +9,7 @@ from typing import Any, ClassVar
import pytest
from bot042.agent.middlewares.budget import StepBudgetExceeded
from bot042.channels.base import BaseChannel
from bot042.channels.manager import ChannelManager
from bot042.channels.models import (
@@ -21,12 +22,13 @@ from bot042.channels.models import (
)
from bot042.channels.registry import register_channel
from bot042.config import Settings
from bot042.db import repo
from bot042.flows import builtin
from bot042.flows.builtin import RPC_STREAM, AgentReply, ChannelServices
from bot042.flows.builtin import BUDGET_REPLY, RPC_STREAM, AgentReply, ChannelServices
from bot042.flows.bus import EventBus
from bot042.flows.rpc import RpcHub
from bot042.flows.stream import ReplyStream
from bot042.flows.tasks import EventTrigger, TaskRunner
from bot042.flows.tasks import EventTrigger, RunStatus, TaskRunner
def test_stream_sends_ack_and_throttles() -> None:
@@ -220,6 +222,68 @@ def test_agent_reply_acks_then_streams(
assert frames[-1] == ("你好", True)
def test_agent_reply_streams_budget_notice_after_partial(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""步数用尽:说明补在已流出的内容后面,落库,且不再补发通用错误文案。"""
StreamChannel.instances.clear()
async def exhausted(task, settings=None, history=None, *, cache=None, steering=None):
yield "做到一半"
raise StepBudgetExceeded(200)
monkeypatch.setattr(builtin, "stream_task", exhausted)
async def _run() -> tuple[list[tuple[str, bool]], list[RunStatus]]:
bus = EventBus()
bus.bind_loop()
hub = RpcHub(bus)
manager = ChannelManager()
channel = StreamChannel(channel_id="s1")
await manager.add_channel(channel)
await manager.start()
_ = hub.serve(RPC_STREAM, manager.rpc_stream)
services = ChannelServices(
channels=manager,
settings=Settings(),
db=tmp_path / "bot.db",
hub=hub,
)
runner = TaskRunner(bus, deps=services)
runner.register(AgentReply)
_ = runner.add_task(AgentReply.name, EventTrigger(event="channel.inbound"))
await runner.start()
bus.emit(
"channel.inbound",
ChannelMessage.of(
InboundMessage(
channel_id="s1",
channel_type="stream-test",
channel_subject=ChannelSubject(subject_id="u1"),
content=[TextContent(text="把整站重构一遍")],
)
),
)
await asyncio.sleep(0.05)
await runner.wait_idle()
statuses = [run.status for run in runner.runs(AgentReply.name)]
await runner.stop()
hub.close()
await manager.stop()
return channel.frames, statuses
frames, statuses = asyncio.run(_run())
final = "做到一半\n\n" + BUDGET_REPLY.format(steps=200)
assert frames[0] == (builtin.ACK_TEXT, False)
assert frames[-1] == (final, True) # 收尾帧保留了部分内容 + 说明
assert statuses == [RunStatus.FAILED]
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1:u1"))
assert rows[-1]["role"] == "assistant"
assert rows[-1]["content"] == final
def test_outbound_stream_payload_defaults() -> None:
payload = OutboundStream(
channel_id="c", subject_id="u", stream_id="s", text="x"