Skip to content

Commit c7c902c

Browse files
authored
Merge pull request #1141 from Ashitaka-Laputa-IV/fix/agent-base-abc-contract
fix(agent): 让 BaseAgent 的构图契约在实例化期生效
2 parents 9fba443 + e8f1df8 commit c7c902c

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""验证 BaseAgent 的构图契约在实例化期生效。"""
2+
3+
import pytest
4+
5+
from yuxi.modules.agents.runtime.base import BaseAgent
6+
7+
8+
def test_incomplete_agent_backend_cannot_be_instantiated():
9+
"""漏实现 get_graph 的子类必须在实例化时失败,而不是等到构图时返回 None。"""
10+
11+
class IncompleteAgent(BaseAgent):
12+
"""只声明继承关系,不实现构图入口。"""
13+
14+
with pytest.raises(TypeError, match=r"abstract method 'get_graph'"):
15+
IncompleteAgent()

‎backend/yuxi/modules/agents/runtime/base.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from abc import abstractmethod
3+
from abc import ABC, abstractmethod
44
from contextlib import aclosing
55
from dataclasses import dataclass
66
from typing import Any
@@ -38,7 +38,7 @@ def _recursion_limit_from_context(context: BaseContext, default: int) -> int:
3838
return int(value) if isinstance(value, int) and value > 0 else default
3939

4040

41-
class BaseAgent:
41+
class BaseAgent(ABC):
4242
"""
4343
定义一个基础 Agent 供 各类 graph 继承
4444
"""
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# BaseAgent 构图契约在实例化期生效
2+
3+
状态:implemented
4+
类型:bug-fix
5+
Owner:backend/yuxi/modules/agents/runtime/base.py
6+
7+
## 问题
8+
9+
`BaseAgent` 以普通类声明,却用 `@abstractmethod` 标注 `get_graph`。缺少 `ABCMeta` 时该装饰器不参与实例化检查:漏实现 `get_graph` 的后端仍能创建,失败推迟到 `_stream_input_with_state` 中 `await self.get_graph(...)` 返回 `None` 后访问 `None.astream_events`,错误指向构图调用点而不是缺失的实现。
10+
11+
## 决策
12+
13+
### 实现方案
14+
15+
`BaseAgent` 继承 `ABC`,`get_graph` 保持抽象方法。后端工厂 `get_agent_backend` 与 `list_agent_backend_info` 继续按稳定 ID 创建轻量对象;未实现 `get_graph` 的后端类在实例化时抛出 `TypeError`,不会进入 `AGENT_BACKENDS` 的注册发现与运行链路。既有后端 `ChatbotAgent` 与测试后端均已实现 `get_graph`,行为不变。
16+
17+
## 替代方案
18+
19+
- 保留普通类并删除 `@abstractmethod`:契约从不可执行的装饰器变成没有契约,与模型、知识库、文档解析器等基类不一致,漏实现仍在运行期暴露。
20+
- 在 `get_graph` 方法体内 `raise NotImplementedError`:失败仍然推迟到调用时,不满足实例化期发现的目的。
21+
- 自定义 `__init_subclass__` 或元类做检查:重复标准库 `ABCMeta` 已有能力,增加维护面。
22+
23+
## 后果
24+
25+
- `BaseAgent` 自身不再可实例化,这是有意的契约表达。
26+
- 漏实现 `get_graph` 的后端类在实例化期失败,`get_agent_backend` 抛出 `TypeError`,而不是构图后的 `AttributeError`。该错误不转换为 `AgentBackendNotFoundError`,因为它属于注册代码的缺陷而不是配置引用了不存在的后端。
27+
- `ABC` 只保证 `get_graph` 存在,不保证返回已编译的图;返回 `None` 的子类仍会在构图调用处失败。
28+
- 仓库外自定义后端如果漏实现 `get_graph`,升级后会在首次实例化或后端信息查询时提前失败。
29+
30+
## 验证
31+
32+
| 验收主张 | 失败面 | 语义 Owner | 直接证据 / 命令 | 负向案例 | 当前结果 |
33+
| --- | --- | --- | --- | --- | --- |
34+
| 漏实现 `get_graph` 的子类无法实例化 | 普通类声明时仍可实例化 | base.py 与 `ABCMeta` | `pytest test/unit/agents/test_base_agent_contract.py` | 把 `BaseAgent` 还原为普通类,测试以 `DID NOT RAISE` 失败 | Passed |
35+
| 已实现 `get_graph` 的后端仍可创建 | 继承 `ABC` 后阻断合法子类 | `agent_backends/__init__.py` | 全量 unit 中的 `test_builtin_discovery.py`、`test_base_agent_langfuse_config.py` | 让 `ChatbotAgent` 实例化失败即变红 | Passed |
36+
| 其他基类的抽象方法都已继承 `ABC` | 同仓库仍存在无强制力的 `@abstractmethod` | 各基类所在模块 | 全仓符号搜索 | 不适用 | Inspected |
37+
38+
`docker run --rm ... ghcr.io/astral-sh/uv:python3.13-bookworm uv run --frozen --group test pytest test/unit -m "not slow" -q`:3530 passed、126 skipped、0 failed;skip 是仓库原有的环境门控用例,不计入通过。同一容器内 `python3 scripts/ci/verify_engineering_contracts.py` 与 `python3 -m unittest scripts.ci.test_verify_engineering_contracts` 通过。本机 Windows 无法运行测试(测试链路使用 `os.O_DIRECTORY`、`fcntl` 等 POSIX 接口),上述结果为 Linux 容器内实际执行。本次不改变 HTTP、PostgreSQL、worker、SSE 或浏览器行为,未运行 integration 与 E2E。

0 commit comments

Comments
 (0)