refactor(sdk): 插件契约基类迁入 SDK 并由 Compat 承接旧包根符号 (#6701)

This commit is contained in:
Aqr-K
2026-09-15 07:01:00 +08:00
committed by GitHub
parent 1030028dfe
commit 61aa3cd649
26 changed files with 805 additions and 464 deletions

View File

@@ -0,0 +1,60 @@
# ADR-0008:插件契约基类归属 SDK
- 状态:Accepted
- 日期:2026-09-14
- 对应任务:ARCH-262
## 决策
`_PluginBase` 与插件处理链的实现本体落在 `app/sdk/plugin/base.py`。`app/plugins/__init__.py`
只保留包文档,旧的包根符号 `app.plugins._PluginBase` 和 `app.plugins.PluginChian` 由
`app/runtime/compat` 的精确符号叠加承接。
`app/sdk/plugins.py` 拆为 `app/sdk/plugin/` 包:`base.py` 拥有契约基类,`manager.py` 拥有
`ModuleManager` 与 `PluginManager` 门面,包根只有文档。旧路径 `app.sdk.plugins` 登记为精确
模块别名,指向 `app.sdk.plugin.manager`。
处理链类在 SDK 中命名为 `PluginChain`。历史拼写 `PluginChian` 不进入 SDK,只作为 Compat
符号映射存在。
## 理由
`_PluginBase` 的构造依赖 `PluginDataOper`、`SystemConfigOper`、`MessageHelper` 和
`PluginDatabaseHandle`,横跨 `app.db`、`app.application` 两个方向。
`ChainBase` 的先例(canonical 在 `app.chain.base`,SDK 薄导出,包根交给 Compat)在这里走不通:
`app.runtime` 被 `FORBIDDEN_IMPORT_PREFIXES` 禁止导入 `app.adapters`、`app.application` 和
`app.sdk`,又被入口层门禁禁止导入 `app.db`。把契约基类放进 `app/runtime/extensions/plugin/`
必须先把四项依赖改成宿主注入的端口,等于改写插件构造语义和全部既装插件的 ABI。
`app/sdk` 是宿主中唯一没有 forbidden-import 约束的层,`app/sdk/_legacy/transferpending.py`
已有实现本体落在 SDK 的先例。契约基类是插件 ABI 本身而不是业务实现,由契约层拥有与
「sdk 为目标契约、compat 设版本期限」的既定方向一致。
## 影响
`docs/rules/05-architecture.md` 的落点决策第 10 条由「never move implementation there」改为
区分契约与实现:SDK 拥有插件契约类型本体,业务实现仍留 canonical 层。
`app/plugins/__init__.py` 此前直接导入 `app.core.config`、`app.core.event` 和
`app.helper.message` 三个幻影名,即宿主源码自己消费兼容层。迁移后依赖改为
`app.runtime.config`、`app.runtime.events`、`app.application.messaging.message` 和
`app.chain.base`,宿主对 Compat 的反向依赖清零。
包根不再在导入插件时拉起 Chain、数据库和消息栈,`app.plugins` 恢复为纯粹的插件安装命名空间。
既装插件的 `from app.plugins import _PluginBase` 保持可用,Compat 按既有规则在 DEBUG 下
每插件每符号发一次可执行的迁移提示。新插件使用 `from app.sdk.plugin import _PluginBase`。
## 包根遮蔽兼容符号的防护
模块级 `__getattr__` 只在属性查找失败时触发。物理包根一旦自带同名定义,符号叠加就完全
不会被调用:旧版本的 `app/plugins/__init__.py` 被挂载或复制进运行目录时,
`app.plugins._PluginBase` 会命中那份旧实现,而宿主判定插件用的是
`hasattr(module, "init_plugin") and hasattr(module, "plugin_name")` 这样的鸭子判据,
不看基类身份,于是插件照常加载、配置读写却落到与宿主不同的存储上,全程没有任何报错。
`detect_shadowed_exports()` 因此在符号叠加安装前比对身份:物理定义与 canonical 是同一
对象时属于无害的重新导入(`app.agent.orchestrator.AgentChain` 等既有情况),身份不同才是
遮蔽,此时直接抛 `ImportError` 并指出覆盖了哪个 canonical 路径。宁可插件装载失败也不接受
静默分裂——后者会让用户在界面上改的配置对插件不可见,且难以归因。

View File

@@ -667,7 +667,11 @@ flowchart TB
`app/api/` 的宿主端点。插件若已经自行返回 `Response`、字典、列表或其它可序列化值,宿主不再二次包裹。
- `app/runtime/extensions/plugin/manager.py` 是 canonical 管理器 owner,发现、加载、生命周期、
目录、同步等实现共同归入 `app/runtime/extensions/plugin/`。旧插件仍从 `app.core.plugin` 或
`app.sdk.plugins` 进入,并由 Compat 精确路由到同一个 `PluginManager` 身份。
`app.sdk.plugins` 进入,并由 Compat 精确路由到同一个 `PluginManager` 身份;新入口是
`app.sdk.plugin.manager`。
- `app/sdk/plugin/base.py` 是插件契约基类 `_PluginBase` 与 `PluginChain` 的 owner。
`app/plugins/` 只是插件安装命名空间,包根不再导出符号,`app.plugins._PluginBase` 与历史拼写
`app.plugins.PluginChian` 由 Compat 精确叠加承接。
- 插件可参与 `run_module` 方法分发(同名方法优先响应)并注册事件处理器。
---
@@ -757,7 +761,7 @@ flowchart LR
| 指标 | 当前值 |
|---|---:|
| Python 模块 | 1020 |
| 内部导入边 | 8,678 |
| 内部导入边 | 8,698 |
| 非平凡 SCC | 1(精确 containment 的 TMDB 移植包环) |
| Application / Chain 具体 Adapter 直连 | 0 / 0 |
| Direct egress | 53(债务已清零,53 条精确 containment) |

View File

@@ -94,7 +94,7 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁
| 指标 | 当前值 | 解释 |
|---|---:|---|
| 宿主 Python 模块 / 内部依赖边 | 1020 / 8,678 | `dependency-baseline.json` 当前快照;分类、下载资源归类、订阅搜索、整理恢复、Agent 计划、工具视觉、终端生命周期与终端作用域模块的受控依赖 |
| 宿主 Python 模块 / 内部依赖边 | 1022 / 8,698 | `dependency-baseline.json` 当前快照;分类、下载资源归类、订阅搜索、整理恢复、Agent 计划、工具视觉、终端生命周期与终端作用域模块的受控依赖 |
| 非平凡 SCC | 1 | 仅保留精确 containment 的 29 模块 TMDB 移植包环 |
| 跨层 DB 边界债务 | 0 | Application、Chain、API、Agent、Runtime、Workflow 到 DB 的受控债务均为零 |
| Model/Oper 事务债务 | 0 | 自建 Session、自动事务装饰器、直接 commit/rollback 等基线均为零 |
@@ -102,8 +102,8 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁
| Event Contract | 53 | 均已有 payload model,但当前全部是 diagnostic enforcement |
| Python 源码量 | 305,884 行 | 排除 `app/plugins/**`;61 个文件超过 1,000 行,11 个超过 2,000 行 |
| 长方法 | 290 个超过 80 行 | AST 统计排除 `app/plugins/**`;65 个超过 150 行,21 个超过 250 行 |
| 全量 mypy 历史债务 | 9,360 / 509 文件 | Agent API 重构后的现状基线;canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降 |
| Ruff 历史诊断 | 518 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` |
| 全量 mypy 历史债务 | 9,349 / 509 文件 | Agent API 重构后的现状基线;canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降 |
| Ruff 历史诊断 | 517 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` |
| 覆盖率固定基线 | Application 80.00%,Domain 80.00% | Chain、Runtime、Agent、Adapter、Startup 未进入包级覆盖率门禁 |
### 3.3 热点文件
@@ -687,7 +687,7 @@ Pylint 10/10、Ruff、mypy/复杂度 ratchet、宿主与最新官方插件基线
- [x] `PluginHelper` 继续作为兼容入口,但市场、包、依赖、备份/恢复、健康修复分别委托现有 owner。
- [x] `PluginManager` 的服务图构造移到 startup typed `PluginRuntime` factory,Facade 只保留稳定 API。
- [x] `PluginHelper` 不进入推荐 SDK,仅保留 `app.helper.plugin` 精确 Compat;`app.sdk.plugins` 只保留
- [x] `PluginHelper` 不进入推荐 SDK,仅保留 `app.helper.plugin` 精确 Compat;`app.sdk.plugin.manager` 只保留
审计过的 Manager ABI,不重复导出 canonical 实现。
- [x] 用最新官方插件仓 baseline 和真实插件导入/兼容探针决定退场,不按宿主“无人引用”判断。

View File

@@ -540,7 +540,10 @@ Use these questions in order before creating or moving a migrated capability:
9. Does it implement a named external product/ecosystem? Put it in
`app/adapters/external`.
10. Is it public to plugins or only preserving an old path? Curate it in
`app/sdk` or map it in `app/runtime/compat`; never move implementation there.
`app/sdk` or map it in `app/runtime/compat`. `app/sdk` owns plugin contract
types themselves, such as the `_PluginBase` abstract base class plugins
inherit; business implementation still belongs to its canonical layer and
never moves there.
Do not create generic `common`, `helper` or `utils` buckets. Reuse does not erase
ownership.
@@ -1008,7 +1011,10 @@ or stale policy entry fails. The current policy has one classification:
The target remains zero canonical host cycles except the precisely contained
vendor component. `ChainBase` lives in `app.chain.base`; the physical package root
has no eager export, and the old package-root symbol is available only through the
exact Compat overlay backed by `app.sdk.chain`.
exact Compat overlay backed by `app.sdk.chain`. `_PluginBase` and `PluginChain`
live in `app.sdk.plugin.base`; `app/plugins/` is the plugin install namespace and
its package root only documents that, so `app.plugins._PluginBase` and the historic
spelling `app.plugins.PluginChian` resolve solely through the exact Compat overlay.
The same fact/policy split governs direct Adapter imports. The generated
dependency baseline records the original runtime imports from `app.application`
@@ -1191,6 +1197,7 @@ driven workflow registration.
| `app/application/mediaserver.py` | Configured media-server discovery and identity matching |
| `app/runtime/compat/manifest.py` | Exact legacy-to-canonical import manifest |
| `app/sdk/` | Stable plugin imports, including provider-neutral browser launch functions |
| `app/sdk/plugin/` | Plugin contract base class (`base.py`) and runtime manager facade (`manager.py`); the package root is a lazy export map so importing the contract does not load the managers |
Run `tests/test_architecture_dependencies.py` after every ownership or import
change. It rejects physical legacy or retired canonical sources, forbidden