From 066757888c128560f895cc9ec187aa1f960cac20 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 23 Aug 2026 23:50:19 +0800 Subject: [PATCH] refactor: unify scheduler service access --- app/command.py | 2 +- docs/refactor/backend-architecture-next-stage.md | 4 ++++ .../architecture/dependency-baseline.json | 4 ++-- tests/test_architecture_dependencies.py | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/command.py b/app/command.py index fc2782346..755f6844b 100644 --- a/app/command.py +++ b/app/command.py @@ -15,9 +15,9 @@ from app.runtime.events import Event as ManagerEvent, eventmanager, Event from app.application.plugin.runtime import get_plugin_manager as PluginManager from app.application.messaging.message import MessageHelper from app.application.messaging.skill import SkillInteractionHandler +from app.application.scheduling import Scheduler from app.runtime.thread import ThreadHelper from app.runtime.log import logger -from app.scheduler import Scheduler from app.schemas.message import Message from app.schemas.event import CommandRegisterEventData from app.schemas.types import EventType, NotificationChannel, ChainEventType diff --git a/docs/refactor/backend-architecture-next-stage.md b/docs/refactor/backend-architecture-next-stage.md index f4fd0093b..47bb46fc0 100644 --- a/docs/refactor/backend-architecture-next-stage.md +++ b/docs/refactor/backend-architecture-next-stage.md @@ -85,6 +85,10 @@ 3. **Model/Base 的数据库装饰器和隐式会话 ABI 已全部清零。** 查询、写事务和 `legacy_*` 装饰器均为 `0`;所有 Model `db` 参数要求显式 Session,Base CRUD 仅在调用方事务内查询或 stage。可无会话构造的入口统一留在 Oper,经组合根事务执行器运行;插件 SDK 不再导出宿主 Model。后续重点转为减少 ORM 对象跨层流转,并保持 Model 隐式事务零回退。 4. **组合根和全局状态仍形成复杂的隐式运行时图。** Singleton 实例、模块级 provider、`configure_*` 注册函数和兼容 Facade 同时存在;它们解决了旧 ABI 和启动顺序问题,但增加测试污染、重复装配、实例身份和初始化顺序风险。`app/startup/lifecycle/__init__.py` 已有声明式生命周期,`app/startup/initializers/modules.py` 也有分阶段关闭,但尚未做到所有进程级资源都只通过 typed HostRuntime 访问。后续应以“新代码禁止新增 Service Locator/Singleton 依赖、旧入口有命中观测”为 ratchet。 + Scheduler 已先完成一个可验证切片:API、Agent 与 Command 统一经 + `app.application.scheduling` Facade 获取实例,只有 `app.scheduler` 实现本身及 startup 组合根允许 + 依赖 concrete Scheduler;架构测试拒绝普通宿主消费者重新引入第二条实例化路径。 + ### P2:中长期可演进性债务 - **大型职责域仍偏重。** 代表性热点包括 `app/chain/subscribe.py`(约 `4141` 行)、`app/chain/transfer.py`(约 `2944` 行)、`app/agent/orchestrator.py`(约 `3540` 行)、`app/agent/llm/provider.py`(约 `3529` 行)、`app/adapters/external/market.py`(约 `3139` 行)和 `app/api/endpoints/agent.py`(约 `2489` 行)。复杂度 ratchet 只保证不超过当前基线,不代表这些文件已经易维护。只有在行为快照、调用命中和事务边界明确后,才值得按用例拆分。 diff --git a/tests/fixtures/architecture/dependency-baseline.json b/tests/fixtures/architecture/dependency-baseline.json index 7d8a0abb8..90b7cde92 100644 --- a/tests/fixtures/architecture/dependency-baseline.json +++ b/tests/fixtures/architecture/dependency-baseline.json @@ -14,7 +14,7 @@ "workflow_to_db": [] }, "edge_count": 6500, - "edge_sha256": "f16cc04898ae0fc7602f2d154321035040c6e64155c59b0b0d72a7d707f59ec9", + "edge_sha256": "7afa6e61ca564c1ec0c5981304cdb219afa99eb3318bb1703e8acf363cab9aae", "edges": [ "app -> app.runtime", "app -> app.runtime.compat", @@ -3485,6 +3485,7 @@ "app.command -> app.application.messaging.skill", "app.command -> app.application.plugin", "app.command -> app.application.plugin.runtime", + "app.command -> app.application.scheduling", "app.command -> app.chain", "app.command -> app.chain.download", "app.command -> app.chain.message", @@ -3500,7 +3501,6 @@ "app.command -> app.runtime.events", "app.command -> app.runtime.log", "app.command -> app.runtime.thread", - "app.command -> app.scheduler", "app.command -> app.schemas", "app.command -> app.schemas.event", "app.command -> app.schemas.message", diff --git a/tests/test_architecture_dependencies.py b/tests/test_architecture_dependencies.py index 1ca3c4273..6d3d0e418 100644 --- a/tests/test_architecture_dependencies.py +++ b/tests/test_architecture_dependencies.py @@ -861,6 +861,22 @@ def test_agent_tools_do_not_import_entrypoint_internals(): assert violations == {} +def test_runtime_consumers_use_scheduler_application_facade(): + """非组合根消费者必须经 application Facade 访问进程级 Scheduler。""" + allowed = { + "app.scheduler", + "app.startup.initializers.modules", + "app.startup.initializers.scheduler", + } + violations = { + module_name: dependencies & {"app.scheduler"} + for module_name, dependencies in _build_module_graph().items() + if module_name not in allowed and "app.scheduler" in dependencies + } + + assert violations == {} + + def test_api_does_not_import_factory(): """装配器(factory)只允许 app.main 使用,HTTP 端点不得回引。""" violations: dict[str, set[str]] = {}