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

+5 -3
View File
@@ -154,8 +154,10 @@ bot serve --channel wecom
规则:
- 未设置 `BOT042_MODEL` 时,使用 `models` 表中 `is_default=1` 的模型;
- 单次任务步数上限同理:`settings.agent.max_steps`(默认 200),`BOT042_MAX_STEPS` 优先;
联网检索类任务步数消耗快,触顶会抛 `GraphRecursionError`,表现为「处理消息时出错」;
- 单次任务步数预算同理:`settings.agent.max_steps`(默认 200),`BOT042_MAX_STEPS` 优先;
预算将尽时智能体先收到一条「立刻收尾」提示,把已完成的部分总结出来;交给 LangGraph
的硬上限是 `max_steps + 20` 个图步,真撞上时用户看到的是「本轮执行步数用尽(上限 N 步)」
加已产出的内容,而不是笼统的「处理消息时出错」;
- `OPENAI_API_KEY` / `OPENAI_BASE_URL` 为空时,从该模型所属 `providers` 行补齐;
- 环境变量指定的模型与库中默认模型**不一致**时,不会套用库里的密钥
(错用别的 key 只会得到 401);
@@ -169,7 +171,7 @@ bot serve --channel wecom
| `BOT042_MODEL` | langchain 模型标识(`provider:model`) | `openai:gpt-4o-mini` |
| `OPENAI_API_KEY` | OpenAI 兼容 API Key | — |
| `OPENAI_BASE_URL` | 兼容网关地址(DeepSeek / Kimi 等均可) | 官方地址 |
| `BOT042_MAX_STEPS` | 单次任务最大步数(recursion_limit) | `200` |
| `BOT042_MAX_STEPS` | 单次任务步数预算(软上限,硬上限为其 +20) | `200` |
| `BOT042_LOG_LEVEL` | 日志级别 | `INFO` |
使用 DeepSeek 等 OpenAI 兼容服务示例:
+13 -4
View File
@@ -457,13 +457,22 @@ langchain 从环境变量取凭据。
一次 `ainvoke`,但缓存由持有者(`ChannelServices`)决定生命周期,不做模块级单例,
避免配置不同或测试替换 `create_agent` 时串味。
`run_task()` 把历史转成 `{"messages": [...]}`,以 `recursion_limit=settings.max_steps` 调用 `ainvoke`,
`run_task()` 把历史转成 `{"messages": [...]}`,以 `recursion_limit=settings.recursion_limit` 调用 `ainvoke`,
返回最终 state;`last_message_text()` 负责从多模态 content 中只取文本块(按行拼接)。
`max_steps` 默认 200,优先级 **环境变量 `BOT042_MAX_STEPS` > `settings.agent.max_steps` > 默认**:
联网检索类任务一轮要连续消耗十几个图步数(搜索 → 抓取 → 再搜索 → 汇总),上限过低时
`GraphRecursionError` 会被渠道兜底成用户看到的「处理消息时出错,请稍后再试。」,
所以它和模型配置一起由 `resolve_model_settings()` 解析进 `Settings`。
联网检索类任务一轮要连续消耗十几个图步数(搜索 → 抓取 → 再搜索 → 汇总)。它是**软预算**,
不是 `recursion_limit` 本身——
- `StepBudgetMiddleware`(`agent/middlewares/budget.py`)按模型调用轮数计账(`agent_rounds = max_steps // 2`,
一次「模型 → 工具」往返约耗 2 个图步),剩余 ≤4 轮时注入一条「立刻停止调用工具、给出结论」
的提示,且只提醒一次;
- 交给 LangGraph 的硬上限是 `recursion_limit = max_steps + STEP_GRACE(20)`,留给收尾动作;
- 真撞上硬上限(模型不听话、或子智能体的步数不在计数内)时,`GraphRecursionError` 会被转成
`StepBudgetExceeded`;`AgentReply` 把它渲染成「本轮执行步数用尽(上限 N 步)」+ 已产出的部分内容,
一并落库为 assistant 消息,并标记 `_reported` 让 `on_error` 不再补发通用错误文案。
两者以前是同值,于是预算耗尽的那一刻必然抛异常,模型连一句话都来不及说——这是 v0.1 的 bug。
## 7. 存储层(`db/`)
+4 -2
View File
@@ -11,6 +11,7 @@ from deepagents.backends import LocalShellBackend
from langchain.agents.middleware.types import AgentMiddleware
from langgraph.graph.state import CompiledStateGraph
from bot042.agent.middlewares.budget import StepBudgetMiddleware
from bot042.agent.middlewares.steering import SteeringMiddleware
from bot042.agent.prompts import SYSTEM_PROMPT
from bot042.agent.tools import get_tools
@@ -90,12 +91,13 @@ def create_agent(
"openai:gpt-4o-mini"、"openai:deepseek-chat"(配合 OPENAI_BASE_URL)等。
"""
settings = settings or Settings.from_env()
# SteeringMiddleware 放最前:每轮模型调用前都先消化插话
# SteeringMiddleware 放最前:每轮模型调用前都先消化插话;
# StepBudgetMiddleware 紧随其后:预算将尽时注入收尾提示,避免撞硬上限
return create_deep_agent(
model=_build_model(settings),
tools=get_tools(),
system_prompt=SYSTEM_PROMPT,
backend=_build_backend(settings),
middleware=[SteeringMiddleware(), *middleware],
middleware=[SteeringMiddleware(), StepBudgetMiddleware(), *middleware],
interrupt_on=interrupt_on,
)
+126
View File
@@ -0,0 +1,126 @@
"""步数预算:在硬上限之前提醒模型收尾。
LangGraph 的 ``recursion_limit`` 是**硬中断**——撞上就抛
``GraphRecursionError``,模型没有任何机会整理已做的工作、给出结论。所以这里
在触到硬上限之前先注入一条收尾提示,让模型自己把半成品收口:说清做到哪一步、
结论是什么、还差什么。
预算按**模型调用轮数**计(一个「模型 → 工具」往返约耗 2 个图步),轮数预算由
:attr:`bot042.config.Settings.agent_rounds` 从 ``max_steps`` 换算而来;硬上限
则是 :attr:`bot042.config.Settings.recursion_limit`,比预算多留
:data:`bot042.config.STEP_GRACE` 步给收尾动作。
与插话一样用 :mod:`contextvars` 绑定:``AgentCache`` 让同一张图被多个会话复用,
轮数记在实例属性上会串台。
限制:中间件只数**主智能体**的模型调用;子智能体(``task`` 工具派发的)消耗的
步数不在计数内。所以硬上限不能省——它是唯一能兜住子智能体跑飞的东西。
"""
from __future__ import annotations
import contextvars
from dataclasses import dataclass
from typing import Any
from langchain.agents.middleware.types import AgentMiddleware
from langchain_core.messages import HumanMessage
__all__ = [
"BUDGET_PREFIX",
"BUDGET_PROMPT",
"BUDGET_WARN_MARGIN",
"StepBudget",
"StepBudgetExceeded",
"StepBudgetMiddleware",
"bind",
"current_budget",
"unbind",
]
BUDGET_WARN_MARGIN = 4
"""剩余轮数 ≤ 该值时开始提醒收尾(留够 4 轮写下结论)。"""
BUDGET_PREFIX = "[系统 · 步数预算] "
"""收尾提示的前缀,让模型能识别这不是用户的新指令。"""
BUDGET_PROMPT = (
"本次任务的执行预算即将用尽(剩余约 {remaining} 轮模型调用)。"
"请立刻停止调用工具,基于已经完成的工作给出最终答复:"
"说明做到了哪一步、结论是什么、还差什么没做。不要再发起新的探索或修改。"
)
class StepBudgetExceeded(RuntimeError):
"""图步数硬上限(``recursion_limit``)被耗尽。
这是**兜底**路径:正常情况下预算中间件会先提醒模型收尾;走到这里说明模型
没有遵守提醒,或子智能体吃掉了大量步数。
"""
def __init__(self, max_steps: int) -> None:
self.max_steps = max_steps
super().__init__(f"任务步数用尽(max_steps={max_steps})")
@dataclass
class StepBudget:
"""一次运行的轮数预算(绑定单个 asyncio 任务,不加锁)。"""
rounds: int
"""允许的模型调用轮数。"""
used: int = 0
"""已发生的模型调用次数。"""
warned: bool = False
"""是否已经注入过收尾提示(只提醒一次,避免反复打断)。"""
@property
def remaining(self) -> int:
"""剩余轮数(不为负)。"""
return max(0, self.rounds - self.used)
def take(self) -> int:
"""登记一次模型调用,返回登记后的剩余轮数。"""
self.used += 1
return self.remaining
_current: contextvars.ContextVar[StepBudget | None] = contextvars.ContextVar(
"bot042_step_budget", default=None
)
def current_budget() -> StepBudget | None:
"""当前 asyncio 上下文的步数预算(无运行中任务时为 ``None``)。"""
return _current.get()
def bind(budget: StepBudget) -> contextvars.Token[StepBudget | None]:
"""把预算绑定到当前上下文,返回用于还原的 token。"""
return _current.set(budget)
def unbind(token: contextvars.Token[StepBudget | None]) -> None:
"""还原上下文绑定。"""
_current.reset(token)
class StepBudgetMiddleware(AgentMiddleware):
"""在预算将尽时注入一次收尾提示。"""
async def abefore_model(self, state: Any, runtime: Any) -> dict[str, Any] | None:
_ = state, runtime
budget = _current.get()
if budget is None or budget.warned:
return None
remaining = budget.take()
if remaining > BUDGET_WARN_MARGIN:
return None
budget.warned = True
return {
"messages": [
HumanMessage(content=BUDGET_PREFIX + BUDGET_PROMPT.format(remaining=remaining))
]
}
+27 -3
View File
@@ -8,6 +8,13 @@ from pathlib import Path
from bot042.home import db_path, resolve_home
STEP_GRACE = 20
"""图步数硬上限相对 ``max_steps`` 的余量。
留给「预算将尽」提示被遵守的空间:预算用尽与硬上限如果相等,撞上限的那一刻
直接抛 ``GraphRecursionError``,模型连一句话都来不及说。
"""
@dataclass(frozen=True)
class Settings:
@@ -21,10 +28,13 @@ class Settings:
model: str = "openai:gpt-4o-mini"
max_steps: int = 200
"""单次任务的图步数上限(recursion_limit)。
"""单次任务的**软**步数预算。
联网检索类任务一轮要跑很多次工具调用(搜索 → 抓取 → 再搜索),50 步很容易
在给出答案前耗尽并抛 ``GraphRecursionError``,因此默认给到 200。
这是给模型看的预算:预算将尽时中间件会注入一条「立刻收尾」提示
(见 :mod:`bot042.agent.middlewares.budget`),交给 LangGraph 的硬上限是
:attr:`recursion_limit`(比它多 :data:`STEP_GRACE` 步)。联网检索类任务一轮
要跑很多次工具调用(搜索 → 抓取 → 再搜索),50 步很容易在给出答案前耗尽,
因此默认给到 200。
"""
log_level: str = "INFO"
home: Path = field(default_factory=resolve_home)
@@ -40,6 +50,20 @@ class Settings:
"""home 目录下的 bot.db 路径。"""
return db_path(self.home)
@property
def recursion_limit(self) -> int:
"""交给 LangGraph 的硬步数上限。
硬上限必须**大于**软预算:预算用尽时模型需要几步把结论写完,直接等值会
让收尾提示和中断同时到达,退化回「抛异常、无结论」。
"""
return self.max_steps + STEP_GRACE
@property
def agent_rounds(self) -> int:
"""模型调用轮数预算:一次「模型 → 工具」往返约耗 2 个图步。"""
return max(1, self.max_steps // 2)
@classmethod
def from_env(cls) -> Settings:
return cls(
+1 -1
View File
@@ -100,7 +100,7 @@ _CHANNEL_SEEDS = [
_SETTING_SEEDS = [
("agent.default_model", "openai:gpt-4o-mini", "默认模型"),
("agent.max_steps", "200", "单次任务最大步数"),
("agent.max_steps", "200", "单次任务步数预算"),
("log.level", "INFO", "日志级别"),
]
+34 -7
View File
@@ -8,9 +8,15 @@ from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any
from langchain_core.messages import AIMessageChunk
from langgraph.errors import GraphRecursionError
from bot042.agent import create_agent
from bot042.agent.middlewares.steering import SteeringBuffer, bind, unbind
from bot042.agent.middlewares.budget import StepBudget, StepBudgetExceeded
from bot042.agent.middlewares.budget import bind as bind_budget
from bot042.agent.middlewares.budget import unbind as unbind_budget
from bot042.agent.middlewares.steering import SteeringBuffer
from bot042.agent.middlewares.steering import bind as bind_steering
from bot042.agent.middlewares.steering import unbind as unbind_steering
from bot042.config import Settings
if TYPE_CHECKING:
@@ -37,6 +43,11 @@ def _content_text(content: object) -> str:
return ""
def _run_config(settings: Settings) -> dict[str, Any]:
"""图调用配置:硬步数上限 = 软预算 + 余量(见 ``Settings.recursion_limit``)。"""
return {"recursion_limit": settings.recursion_limit}
def last_message_text(result: dict[str, Any]) -> str:
"""从智能体最终状态中提取最后一条消息的文本(多模态只取文本块)。"""
messages = result.get("messages") or []
@@ -84,6 +95,9 @@ async def run_task(
history: 历史消息(不含本轮)。
cache: 智能体缓存;给定则复用其中的实例,缺省每次新建(一次性调用用)。
steering: 插话缓冲;给定则该次运行期间的插话会在下一轮模型调用前注入。
Raises:
StepBudgetExceeded: 图步数硬上限被耗尽(软预算的收尾提示没起作用)。
"""
settings = settings or Settings.from_env()
agent = cache.get(settings) if cache is not None else create_agent(settings)
@@ -91,15 +105,20 @@ async def run_task(
messages.append({"role": "user", "content": task})
logger.debug("执行任务:%s", task)
token = bind(steering) if steering is not None else None
token = bind_steering(steering) if steering is not None else None
budget_token = bind_budget(StepBudget(settings.agent_rounds))
try:
return await agent.ainvoke(
{"messages": messages},
config={"recursion_limit": settings.max_steps},
config=_run_config(settings),
)
except GraphRecursionError as exc:
# 硬上限只是兜底:走到这里说明收尾提示没被遵守,或子智能体吃掉了步数
raise StepBudgetExceeded(settings.max_steps) from exc
finally:
unbind_budget(budget_token)
if token is not None:
unbind(token)
unbind_steering(token)
@dataclass
@@ -133,6 +152,9 @@ async def stream_task(
history: 历史消息(不含本轮)。
cache: 智能体缓存;给定则复用其中的实例。
steering: 插话缓冲;给定则该次运行期间的插话会在下一轮模型调用前注入。
Raises:
StepBudgetExceeded: 图步数硬上限被耗尽;调用方可用已产出的部分文本拼提示。
"""
settings = settings or Settings.from_env()
agent = cache.get(settings) if cache is not None else create_agent(settings)
@@ -140,13 +162,14 @@ async def stream_task(
messages.append({"role": "user", "content": task})
logger.debug("流式执行任务:%s", task)
token = bind(steering) if steering is not None else None
token = bind_steering(steering) if steering is not None else None
budget_token = bind_budget(StepBudget(settings.agent_rounds))
rounds: dict[str, _Round] = {}
order: list[str] = []
try:
async for chunk, _meta in agent.astream(
{"messages": messages},
config={"recursion_limit": settings.max_steps},
config=_run_config(settings),
stream_mode="messages",
):
# messages 模式同时会吐出 ToolMessage(工具返回值)与 HumanMessage,
@@ -166,6 +189,10 @@ async def stream_task(
visible = "".join(rounds[k].text for k in order if not rounds[k].tool)
if visible:
yield visible
except GraphRecursionError as exc:
# 已产出的文本由调用方持有(累积式产出),这里只把异常换成可读的错误类型
raise StepBudgetExceeded(settings.max_steps) from exc
finally:
unbind_budget(budget_token)
if token is not None:
unbind(token)
unbind_steering(token)
+56 -15
View File
@@ -20,6 +20,7 @@ from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
from bot042.agent.middlewares.budget import StepBudgetExceeded
from bot042.agent.middlewares.steering import SteeringBuffer
from bot042.channels.manager import ChannelManager
from bot042.channels.models import (
@@ -62,10 +63,24 @@ ACK_TEXT = "收到,正在处理…"
ERROR_REPLY = "处理消息时出错,请稍后再试。"
BUDGET_REPLY = (
"本轮执行步数用尽(上限 {steps} 步),我先停在这里。\n"
"要继续的话,把需求拆小一点重新发一条,或者直接说「接着做」,我从当前进度往下推。"
)
"""步数用尽时的说明(接在已完成的部分回答之后)。"""
REPLY_META_KEYS = ("_frame", "response_url", "chat_id", "_ws_client")
"""入站 metadata 中需要回传给渠道的回复上下文(IM 被动回复用)。"""
def _with_notice(answer: str, aborted: StepBudgetExceeded | None) -> str:
"""把「步数用尽」的说明接到部分回答后面;没有部分回答时只给说明。"""
if aborted is None:
return answer
notice = BUDGET_REPLY.format(steps=aborted.max_steps)
return f"{answer}\n\n{notice}" if answer.strip() else notice
def reply_metadata(message: InboundMessage) -> dict[str, Any]:
"""挑出入站消息里的回复上下文,随出站 RPC 回传给渠道。
@@ -205,6 +220,8 @@ class AgentReply(Task):
super().__init__(context)
self._steering = SteeringBuffer()
self._steered: list[str] = []
self._reported = False
"""是否已自行回话并落库(失败时 ``on_error`` 不再发通用文案)。"""
@property
def steering(self) -> SteeringBuffer:
@@ -294,26 +311,36 @@ class AgentReply(Task):
)
if stream is None:
result = await run_task(
text,
services.settings,
history=history,
cache=services.cache,
steering=self._steering,
)
answer = last_message_text(result)
answer = ""
aborted: StepBudgetExceeded | None = None
try:
result = await run_task(
text,
services.settings,
history=history,
cache=services.cache,
steering=self._steering,
)
answer = last_message_text(result)
except StepBudgetExceeded as exc:
aborted = exc
await self._persist_steered(services, session_id, channel)
if answer:
reply = _with_notice(answer, aborted)
if reply:
await repo.add_message(
services.db,
session_id=session_id,
role="assistant",
content=answer,
content=reply,
channel=channel,
)
await services.send_text(
payload.channel_id, payload.subject_id, answer or EMPTY_REPLY, metadata=meta
payload.channel_id, payload.subject_id, reply or EMPTY_REPLY, metadata=meta
)
if aborted is not None:
# 已回话并落库:任务仍记失败(步数用尽是真的没做完),但不再发通用错误
self._reported = True
raise aborted
return
# 支持流式:先给回执,再边生成边覆盖同一条消息。
@@ -321,6 +348,7 @@ class AgentReply(Task):
await stream.update(ACK_TEXT, force=True)
answer = ""
first = True
aborted = None
try:
async for partial in stream_task(
text,
@@ -333,20 +361,33 @@ class AgentReply(Task):
# 首段跳过节流:让用户在 ack 之后立刻看到内容开始出现
await stream.update(partial, force=first)
first = False
except StepBudgetExceeded as exc:
aborted = exc
finally:
await stream.finish(answer or EMPTY_REPLY)
# 步数用尽时把说明补在已经流出的内容后面,不必再单独发一条
final = _with_notice(answer, aborted)
await stream.finish(final or EMPTY_REPLY)
await self._persist_steered(services, session_id, channel)
if answer:
if final:
await repo.add_message(
services.db,
session_id=session_id,
role="assistant",
content=answer,
content=final,
channel=channel,
)
if aborted is not None:
self._reported = True
raise aborted
async def on_error(self, exc: BaseException) -> None:
"""失败时尽量回一句可读提示(发送本身失败只记日志)。"""
"""失败时尽量回一句可读提示(发送本身失败只记日志)。
已经自行回话并落库的失败(如步数用尽)跳过——否则用户会先收到准确说明,
再收到一句没信息量的「处理消息时出错」。
"""
if self._reported:
return
services = self.deps
payload = self.context.payload
if not (isinstance(services, ChannelServices) and isinstance(payload, ChannelMessage)):
+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"