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
+13
-4
@@ -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/`)
|
||||
|
||||
|
||||
Reference in new issue
Block a user