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:
1 parent
fbb01c4c83
commit
5da55f8d17
12 files changed
+531
-40
No files matched your search
+57
-1
@@ -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),
|
||||
]
|
||||
Reference in new issue
Block a user