refactor: unify oper transaction dispatch

This commit is contained in:
jxxghp
2026-08-24 00:04:35 +08:00
parent e9a226af02
commit 25ad924aac
5 changed files with 26 additions and 11 deletions
+2 -3
View File
@@ -13,8 +13,8 @@
"runtime_to_db": [],
"workflow_to_db": []
},
"edge_count": 6500,
"edge_sha256": "c27b649d471e40e8580426bdee28fdd927be03b7868a1b28bd8d9374c10be280",
"edge_count": 6499,
"edge_sha256": "f21eea0878e60d4b955a87da15cf0653eaecf07d147dc43e9eabc9f9573bb793",
"edges": [
"app -> app.runtime",
"app -> app.runtime.compat",
@@ -3660,7 +3660,6 @@
"app.db.oper.agenttask -> app.db.models",
"app.db.oper.agenttask -> app.db.models.agenttask",
"app.db.oper.agenttask -> app.db.models.agenttaskrun",
"app.db.oper.agenttask -> app.db.uow",
"app.db.oper.downloadfailure -> app.db",
"app.db.oper.downloadfailure -> app.db.base",
"app.db.oper.downloadfailure -> app.db.models",
+17
View File
@@ -432,6 +432,23 @@ def test_database_internals_do_not_import_db_facades():
assert violations == []
def test_database_opers_use_dboper_transaction_dispatchers():
"""Oper 不得绕过 DbOper 的统一 Session 类型分派直接调用事务 runner。"""
runner_names = {"run_sync_transaction", "run_async_transaction"}
violations: list[str] = []
for path in (APP_ROOT / "db" / "oper").rglob("*.py"):
tree = ast.parse(path.read_text(encoding="utf-8-sig"), filename=str(path))
for node in ast.walk(tree):
if not isinstance(node, ast.ImportFrom) or node.module != "app.db.uow":
continue
imported = {alias.name for alias in node.names} & runner_names
if imported:
relative = path.relative_to(PROJECT_ROOT).as_posix()
violations.append(f"{relative}:{node.lineno}:{','.join(sorted(imported))}")
assert violations == []
def test_models_and_base_require_explicit_database_sessions():
"""Model/Base 不得装饰事务,且所有 db 参数必须由调用方显式传入。"""
decorator_names = {
@@ -366,7 +366,8 @@ def test_agenttask_oper_reads_with_explicit_session(db, monkeypatch):
task_id = AgentTask.add_task(db.session, **_task("canonical", user_id="alice"))
monkeypatch.setattr(
"app.db.oper.agenttask.run_sync_transaction",
db_base,
"run_sync_transaction",
lambda _query: pytest.fail("显式 Session 查询不应创建兼容事务"),
)