refactor: isolate agent history queries

This commit is contained in:
jxxghp
2026-08-23 14:21:53 +08:00
parent d8b8583d49
commit 8fc9a021b0
11 changed files with 121 additions and 61 deletions
+30
View File
@@ -14,6 +14,7 @@ from app.db.oper.agenttask import AgentTaskOper
from app.db.models.agenttask import AgentTask
from app.db.models.agenttaskrun import AgentTaskRun
from app.db.session import SessionFactory
from app.db import decorators
Engine = get_engine()
@@ -134,6 +135,35 @@ def test_begin_run_rejects_unknown_trigger_source() -> None:
assert AgentTaskOper().list_runs(task.id) == []
def test_agenttaskrun_oper_reuses_explicit_query_session(db, monkeypatch):
"""AgentTaskOper 的运行记录查询必须复用调用方同步会话。"""
task = _add_task("run-explicit-query")
run = AgentTaskOper().begin_run(task.id)
assert run
monkeypatch.setattr(
decorators,
"ScopedSession",
lambda: (_ for _ in ()).throw(AssertionError("不应创建额外同步会话")),
)
oper = AgentTaskOper(db.session)
assert oper.get_run(run.run_id) is not None
assert oper.list_runs(task.id)
def test_agenttaskrun_model_legacy_query_keeps_keyword_abi(monkeypatch):
"""旧插件以关键字直调 AgentTaskRun 时仍自动补入短会话。"""
opened = []
monkeypatch.setattr(
decorators,
"ScopedSession",
lambda: (opened.append(True) or SessionFactory()),
)
assert AgentTaskRun.get_by_run_id(run_id="missing-legacy") is None
assert opened == [True]
def test_begin_run_rolls_back_task_claim_when_run_insert_fails() -> None:
"""运行记录插入失败时,任务的 running 投影必须随事务回滚。"""
first_task = _add_task("run-rollback-first")