mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-16 18:11:59 +00:00
召回复核修正(后端):人工节点不再挡召回、读不出召回卡记失败、旧版不再接着算、团队渠道一次召回只推一条、目录核对按数据源合并排队;账本接口带上召回标记,对账结果可以反馈
- W4R-01:召回只查新版图里会不会写外部系统(apps/backfill 抽出 graph_external_write,graph_side_effect 复用它)。召回图只有两版
的取数和口径卡,图里别处的人工节点、按流程使用的问题碰不到它;口径卡上游的人工节点 derive 本来就认作数据部分不能复用。
「按新版重新出具」走完整的正式运行,仍按 graph_side_effect 全套拒绝。原来带审批的口径升版后一组都不召回,L6 对它们失效。
- W4R-09:收尾时召回卡读不出、或缺了某个比较的旧值 / 新值,照实记重算失败(读不出召回卡),原答案一个不动。原来两边都是空,
按渲染比被当成「没变」,给每个原答案永久盖上「已核对,不影响」,收过尾之后再也不核对。
- W4R-12:这一版已经不是当前发布版(之后又发布了更新的版本)时不再发起召回、不给接着算,批次里写 continue_reason,接着算的
接口 409 带同一句。同启动时接着做只认当前发布版:新版本的召回覆盖更早的所有版本,再按旧版开「口径升到 vN」的待办只是添乱。
- W4R-13:开登录时一组答案几个主人就几条待办,全局渠道却按这一批(recall.batch_of_key:同一次召回、同一个来由)只在最早开的
那条上推一次,答案、待办按这一批汇总,几条的链到待办箱;别的几条连 task.opened 也不再推全局渠道。个人渠道照旧每人一条。
- W4R-11:目录审阅之后的核对按数据源排队、批量审阅的几张表合成一遍(catalog_pass,同一数据源一把锁);查询快照按数据源在库里
筛、按运行结束时间分页、每张表凑够 BASIS_MAX_RUNS 次运行就停;冻结的表结构快照这一遍里按工件 id 只读一次。原来批量审阅
几十张表就同时起几十个后台任务,每个都全表扫查询快照、反复读同一份几 MB 的表结构快照。
- W4R-03(本包那一半):GET /api/runs/{id}/ledger 加 recall:checked(已按 vN 核对,不影响)、basis(依据有变:哪一种、几项、
新出现几条错误级问题)、tasks(看的这个人手上说的是这个答案、还没处理的 recall 待办)。不放数、不放表名。几种状态都带。
- W4R-10(本包那一半):新事件 ledger.feedback(对账运行上的封存后事件),POST /api/ledger/answers/{run}/reconcile/{rid}/feedback
{verdict: useful | unclear} 记反馈,同一个人改主意只算最后一次(app/ledger/feedback.py);账本接口的 reconciled 每一项带
feedback {mine, useful, unclear};统计 E2 的 feedback 数到它,去掉「第 4 波接上」的占位说法。离线核对页、封存后事件摘要
都认得这个事件。
测试:test_ledger_recall 新增七个用例、改了人工节点那一个(召回照算、重新出具照实拒绝);新增 test_ledger_feedback;
test_ledger_api 的 E2 断言跟上。每个修复都先写失败的测试、去掉修复回跑确认抓得住。后端全量 14237 通过。
This commit is contained in:
@@ -233,7 +233,8 @@ async def test_stats_count_what_the_fixtures_did(client, monkeypatch):
|
||||
assert e1["with_candidates"] == 3 and e1["rates"]["proven"] == round(1 / 3, 4)
|
||||
assert e1["diff_aspects"]["filter"] == 1 and e1["labels"]["caliber"] == 1 and e1["labels"]["unknown"] == 1
|
||||
assert e1["whole_equivalent"] == 1
|
||||
assert body["e2"]["launched"] == 0 and body["e2"]["feedback"]["available"] is False
|
||||
assert body["e2"]["launched"] == 0 and body["e2"]["feedback"] == {"available": True, "useful": 0, "unclear": 0,
|
||||
"runs": 0, "useful_rate": None}
|
||||
assert body["e3"]["chat_runs"] == 0 and body["e3"]["threshold"] == {"runs": 3, "conversations": 2}
|
||||
usage = {u["caliber"]: u for u in body["e4"]["usage"]}
|
||||
assert usage[f"{wf}@1"]["proven_runs"] == 1
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
"""对账结果的「说明有用 / 看不懂」(next-round 02 做法 10 的 E2;app/ledger/feedback.py)。
|
||||
|
||||
真的跑:虚构店铺库里的问数据答案(算上了退款)和受管口径「周销售」(只算已支付)对一次账,再在结果上给反馈:
|
||||
- 反馈记成对账运行上的封存后事件 ledger.feedback(批注不是证据,不进封存范围);同一个人改主意只算最后一次;
|
||||
- 账本接口 reconciled 每一项带上反馈(看的这个人给过的、各几个),界面照着画选中的那一个;
|
||||
- 统计 E2 数到它(不再写「第 4 波接上」);
|
||||
- 不认的反馈、不是这个答案的对账、还没跑完的对账照实拒绝。
|
||||
数据都是编的。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.events import EventType
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import RunEvent
|
||||
from app.engine.runner import run_manager
|
||||
from app.ledger import match as ledger_match
|
||||
from app.main import app
|
||||
from tests import ledger_samples as S
|
||||
|
||||
CHECKED = {"start": "2026-09-01", "end": "2026-09-08"}
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def engine_up(tmp_path):
|
||||
await run_manager.setup()
|
||||
path = tmp_path / "shop.db"
|
||||
await S.fresh_source(path)
|
||||
yield path
|
||||
await ledger_match.drain()
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client():
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
|
||||
yield c
|
||||
|
||||
|
||||
async def reconciled(client, monkeypatch) -> tuple[str, str]:
|
||||
"""问一次(算上了退款)、按核对过的参数和「周销售」对一次账,等它跑完。返回 (答案, 对账运行)。"""
|
||||
wf = await S.publish()
|
||||
row = await S.explore(monkeypatch, S.WITH_REFUNDS)
|
||||
body = (await client.get(f"/api/runs/{row.id}/ledger")).json()
|
||||
[action] = [a for a in body["actions"] if a["kind"] == "reconcile"]
|
||||
res = await client.post(f"/api/ledger/answers/{row.id}/reconcile",
|
||||
json={"query": action["query"], "workflow_id": wf, "version": 1, "inputs": CHECKED,
|
||||
"confirm": True})
|
||||
assert res.status_code == 201, res.text
|
||||
done = await S.wait(res.json()["run_id"])
|
||||
assert done.status == "succeeded", done.error
|
||||
return row.id, done.id
|
||||
|
||||
|
||||
async def test_feedback_is_a_post_seal_note_counted_by_e2(client, monkeypatch):
|
||||
answer, rec = await reconciled(client, monkeypatch)
|
||||
before = (await client.get("/api/ledger/stats")).json()["e2"]["feedback"]
|
||||
assert before["available"] is True and "第 4 波" not in str(before)
|
||||
|
||||
res = await client.post(f"/api/ledger/answers/{answer}/reconcile/{rec}/feedback", json={"verdict": "useful"})
|
||||
assert res.status_code == 200, res.text
|
||||
assert res.json() == {"run_id": rec, "feedback": {"mine": "useful", "useful": 1, "unclear": 0}}
|
||||
async with SessionLocal() as session:
|
||||
[data] = (await session.execute(select(RunEvent.data).where(
|
||||
RunEvent.run_id == rec, RunEvent.type == EventType.LEDGER_FEEDBACK))).scalars()
|
||||
assert data["verdict"] == "useful" and data["post_seal"] is True and data["source_run"] == answer
|
||||
|
||||
# 改主意:同一个人只算最后一次
|
||||
res = await client.post(f"/api/ledger/answers/{answer}/reconcile/{rec}/feedback", json={"verdict": "unclear"})
|
||||
assert res.json()["feedback"] == {"mine": "unclear", "useful": 0, "unclear": 1}
|
||||
[item] = (await client.get(f"/api/runs/{answer}/ledger")).json()["reconciled"]
|
||||
assert item["run_id"] == rec and item["feedback"] == {"mine": "unclear", "useful": 0, "unclear": 1}
|
||||
|
||||
after = (await client.get("/api/ledger/stats")).json()["e2"]["feedback"]
|
||||
assert after["useful"] - before["useful"] == 0 and after["unclear"] - before["unclear"] == 1
|
||||
assert after["runs"] - before["runs"] == 1 and after["available"] is True
|
||||
|
||||
|
||||
async def test_feedback_refusals(client, monkeypatch):
|
||||
answer, rec = await reconciled(client, monkeypatch)
|
||||
bad = await client.post(f"/api/ledger/answers/{answer}/reconcile/{rec}/feedback", json={"verdict": "great"})
|
||||
assert bad.status_code == 422
|
||||
other = await S.explore(monkeypatch, S.ELSEWHERE, text="门店 [[v:Q1.r0.n]] 家。")
|
||||
res = await client.post(f"/api/ledger/answers/{other.id}/reconcile/{rec}/feedback", json={"verdict": "useful"})
|
||||
assert res.status_code == 404 and res.json()["code"] == "ledger_feedback_refused", "不是这个答案的对账"
|
||||
res = await client.post(f"/api/ledger/answers/{answer}/reconcile/{answer}/feedback", json={"verdict": "useful"})
|
||||
assert res.status_code == 404, "答案自己不是对账运行"
|
||||
# 还没封存的对账(正在跑、刚发起):没有结果可评
|
||||
from app.db.models import Run
|
||||
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, rec)
|
||||
sealed = row.manifest_hash
|
||||
row.manifest_hash = None
|
||||
await session.commit()
|
||||
try:
|
||||
res = await client.post(f"/api/ledger/answers/{answer}/reconcile/{rec}/feedback", json={"verdict": "useful"})
|
||||
assert res.status_code == 409 and "还没运行完" in res.json()["detail"], res.text
|
||||
finally:
|
||||
async with SessionLocal() as session:
|
||||
(await session.get(Run, rec)).manifest_hash = sealed
|
||||
await session.commit()
|
||||
|
||||
|
||||
def test_offline_page_and_summary_know_the_feedback_event():
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from app.bundle.build import _summary
|
||||
|
||||
script = (Path(__file__).resolve().parents[1] / "app" / "bundle" / "verifier.js").read_text(encoding="utf-8")
|
||||
block = re.search(r"const EVENT_LABEL = \{(.*?)\n \}", script, re.S)
|
||||
labels = dict(re.findall(r"'([a-z_.]+)': '([^']+)'", block.group(1)))
|
||||
assert labels["ledger.feedback"] == "对账说明的反馈"
|
||||
assert _summary("ledger.feedback", {"verdict": "useful"}) == "反馈:说明有用"
|
||||
assert _summary("ledger.feedback", {"verdict": "unclear"}) == "反馈:看不懂"
|
||||
@@ -7,7 +7,8 @@
|
||||
- 召回运行:派生图里新旧两版的取数和口径卡各一份(前缀不撞),召回卡里算两版之差;同一份数据上两版各算一次;
|
||||
- 收尾:数变了才开 recall 待办(挂在召回运行上,旧值、新值、差值、原答案当时的值都给),推 ledger.recall,消息里没有数;
|
||||
数没变的在原答案上记 ledger.recalled(已按 vN 核对,不影响),不开待办;
|
||||
- 不能重算的照实记:新版入口要的参数这一组里没有、图里有人工节点(有副作用);
|
||||
- 不能重算的照实记:新版入口要的参数这一组里没有、图里会写外部系统;图里别处有人工节点的照样重算(召回图碰不到它),
|
||||
只是「按新版重新出具」不代人审批;
|
||||
- 待办「按新版重新出具」走按口径出具同一套判法、「知道了」;
|
||||
- 目录里影响数字的一项被改了:冻结目录里看得到它的答案标「依据有变」,按新目录多出错误级问题的才开待办;
|
||||
- 表结构变了:用过这张表的答案标「依据的表结构已变」,口径试算算不出来才开待办;
|
||||
@@ -146,6 +147,32 @@ async def test_limit_continue_and_idempotent_replanning(monkeypatch):
|
||||
assert await recall.launch(wf, 2, batch="manual") == []
|
||||
|
||||
|
||||
async def test_superseded_version_offers_no_continue(client, monkeypatch):
|
||||
"""v2 自动算了一批、还剩几组没算,接着又发布了 v3:v2 那一版不再给「接着算」(v3 的召回覆盖更早的所有版本,同启动时
|
||||
接着做只认当前发布版),写明去 v3 那一版看;手动点也照样拒绝,不再开「口径升到 v2」的待办。"""
|
||||
await ledger_settings(auto_issue=False, issue_daily_limit=100_000, recall_limit=1)
|
||||
wf = await S.publish()
|
||||
await answer(monkeypatch)
|
||||
await answer(monkeypatch, "2026-09-08", "2026-09-15")
|
||||
await publish_v2(wf)
|
||||
assert len(await run_recall(wf)) == 1
|
||||
async with SessionLocal() as session:
|
||||
view = await recall.batches(session, wf, 2)
|
||||
assert view["remaining"] == 1 and view["can_continue"] is True and view["continue_reason"] is None
|
||||
await S.publish(graph=S.caliber_graph(WITH_REFUNDED), version=3, workflow_id=wf)
|
||||
async with SessionLocal() as session:
|
||||
view = await recall.batches(session, wf, 2)
|
||||
assert view["remaining"] == 1 and view["can_continue"] is False
|
||||
assert view["continue_reason"] == "已有更新的发布版 v3,召回在 v3 那一版接着看"
|
||||
res = await client.post(f"/api/ledger/recall/{wf}/2/continue")
|
||||
assert res.status_code == 409 and res.json()["detail"] == view["continue_reason"], res.text
|
||||
assert await recall.launch(wf, 2, batch="manual") == [], "直接调也不再发起"
|
||||
async with SessionLocal() as session:
|
||||
assert len(await recall.recall_runs(session, wf, 2)) == 1
|
||||
current = await recall.batches(session, wf, 3)
|
||||
assert current["can_continue"] is True and current["continue_reason"] is None
|
||||
|
||||
|
||||
async def test_zero_limit_lists_but_does_not_recompute(monkeypatch):
|
||||
await ledger_settings(auto_issue=False, issue_daily_limit=100_000, recall_limit=0)
|
||||
wf = await S.publish()
|
||||
@@ -189,6 +216,9 @@ async def test_changed_numbers_open_a_recall_task_with_all_three_values(client,
|
||||
assert await events(a1.id, EventType.LEDGER_RECALLED) == [], "数变了的不在原答案上记「不影响」"
|
||||
[task] = await recall_tasks(done.id)
|
||||
assert task.title == "口径「周销售」升到 v2,答案的数变了"
|
||||
# 问数据的轮次、证据面板读的账本接口:这个答案有一条没处理的召回待办(界面链到待办箱),没有「已核对」
|
||||
marks = (await client.get(f"/api/runs/{a1.id}/ledger")).json()["recall"]
|
||||
assert marks == {"checked": [], "basis": [], "tasks": [{"id": task.id, "reason": "caliber", "to_version": 2}]}
|
||||
res = await client.get(f"/api/tasks/{task.id}")
|
||||
assert res.status_code == 200, res.text
|
||||
payload = res.json()["payload"]
|
||||
@@ -220,11 +250,43 @@ async def test_unchanged_numbers_are_only_booked_on_the_answer(monkeypatch):
|
||||
[booked] = await events(a1.id, EventType.LEDGER_RECALLED)
|
||||
assert booked == {"role": "answer", "changed": False, "recall_run": done.id, "workflow_id": wf,
|
||||
"from_version": 1, "to_version": 2, "post_seal": True}
|
||||
# 账本接口带上「已按 v2 核对,不影响」:问数据的轮次、证据面板不用再去翻运行时间线
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
|
||||
marks = (await c.get(f"/api/runs/{a1.id}/ledger")).json()["recall"]
|
||||
[checked] = marks["checked"]
|
||||
assert {k: checked[k] for k in ("workflow_id", "workflow_name", "from_version", "to_version", "changed",
|
||||
"recall_run")} == {"workflow_id": wf, "workflow_name": "周销售", "from_version": 1,
|
||||
"to_version": 2, "changed": False, "recall_run": done.id}
|
||||
assert checked["at"] and marks["basis"] == [] and marks["tasks"] == []
|
||||
# 再收一次尾不会重复记
|
||||
assert await recall.settle(done.id) is None
|
||||
assert len(await events(a1.id, EventType.LEDGER_RECALLED)) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("values", [{}, {"old_0": {"value": 1, "rendered": "1.00元"}}], ids=["none", "half"])
|
||||
async def test_unreadable_recall_card_is_a_failure_not_unchanged(monkeypatch, values):
|
||||
"""召回运行成功了、召回卡却读不出来(封存里找不到 recall 节点、读不出封存,或少了某个比较的旧值 / 新值):照实记重算
|
||||
失败,不当成「数没变」去给原答案盖「已核对,不影响」,也不开待办。"""
|
||||
wf = await S.publish()
|
||||
a1 = await answer(monkeypatch)
|
||||
await publish_v2(wf)
|
||||
|
||||
async def unreadable(run_id):
|
||||
return dict(values)
|
||||
|
||||
monkeypatch.setattr(recall, "card_values", unreadable)
|
||||
[done] = await run_recall(wf)
|
||||
assert done.status == "succeeded", done.error
|
||||
[settled] = [d for d in await events(done.id, EventType.LEDGER_RECALLED) if d.get("role") == "recall"]
|
||||
assert settled["outcome"] == "failed" and settled["changed"] is None and settled["error"] == "读不出召回卡"
|
||||
assert await events(a1.id, EventType.LEDGER_RECALLED) == [], "读不出的不在原答案上记「不影响」"
|
||||
assert await recall_tasks(done.id) == []
|
||||
async with SessionLocal() as session:
|
||||
view = await recall.batches(session, wf, 2)
|
||||
[group] = view["groups"]
|
||||
assert group["state"] == "failed" and group["reason"] == "重算失败:读不出召回卡"
|
||||
|
||||
|
||||
async def test_incompatible_entry_and_side_effects_are_not_recomputed(monkeypatch):
|
||||
wf = await S.publish()
|
||||
await answer(monkeypatch)
|
||||
@@ -240,14 +302,59 @@ async def test_incompatible_entry_and_side_effects_are_not_recomputed(monkeypatc
|
||||
assert view["counts"]["not_recomputable"] == 1 and view["groups"][0]["reason"] == group.problem
|
||||
assert view["can_continue"] is False
|
||||
|
||||
wf2 = await S.publish(name="周销售(要审批)")
|
||||
|
||||
|
||||
async def test_human_node_elsewhere_still_recomputes_but_reissue_refuses(client, monkeypatch):
|
||||
"""新版图里在口径卡下游有审批(取数 → 口径卡 → 报告 → 人工确认 → 出口):召回图只有两版的取数和口径卡,人工节点碰不到,
|
||||
照样重算、数变了照样开待办(做法 7 不重算的只有三种:缺入口字段、数据部分不能复用、会写外部系统);「按新版重新出具」
|
||||
走完整的正式运行,仍然不代人审批,照实写不能出具。"""
|
||||
wf = await S.publish(name="周销售(要审批)")
|
||||
await answer(monkeypatch)
|
||||
human = S.caliber_graph(WITH_REFUNDED)
|
||||
human["nodes"].insert(3, S.node("ok", "human", mode="approve", title="确认"))
|
||||
await S.publish(graph=human, version=2, workflow_id=wf2)
|
||||
human["nodes"].insert(4, S.node("ok", "human", mode="approve", title="确认"))
|
||||
human["edges"] = [{"source": a["id"], "target": b["id"]} for a, b in zip(human["nodes"], human["nodes"][1:])]
|
||||
await S.publish(graph=human, version=2, workflow_id=wf)
|
||||
async with SessionLocal() as session:
|
||||
plan = await recall.build_plan(session, wf2, 2)
|
||||
assert plan.groups and all("人工节点" in (g.problem or "") for g in plan.groups)
|
||||
plan = await recall.build_plan(session, wf, 2)
|
||||
assert plan.groups and all(g.problem is None for g in plan.groups), [g.problem for g in plan.groups]
|
||||
assert plan.version_problem == {}
|
||||
[done] = await run_recall(wf)
|
||||
assert done.status == "succeeded", done.error
|
||||
assert not [n for n in done.graph["nodes"] if n["type"] == "human"], "召回图里没有人工节点"
|
||||
[task] = await recall_tasks(done.id)
|
||||
payload = (await client.get(f"/api/tasks/{task.id}")).json()["payload"]
|
||||
reissue = next(a for a in payload["actions"] if a["decision"] == "reissue")
|
||||
assert reissue["available"] is False and "人工节点" in reissue["reason"], reissue
|
||||
res = await client.post(f"/api/tasks/{task.id}/respond", json={"decision": "reissue"})
|
||||
assert res.status_code == 409 and "人工节点" in res.json()["detail"], res.text
|
||||
|
||||
|
||||
async def test_writing_to_an_external_system_is_not_recomputed(monkeypatch):
|
||||
"""新版图里调用工具节点绑的能力会写外部系统(capabilities.registry.writes_externally,同按新口径补算、按口径出具):
|
||||
照实记「无法重算」,不发起。"""
|
||||
from types import SimpleNamespace
|
||||
|
||||
from app.capabilities import registry
|
||||
|
||||
wf = await S.publish()
|
||||
await answer(monkeypatch)
|
||||
graph = S.caliber_graph(WITH_REFUNDED)
|
||||
graph["nodes"].insert(4, S.node("pay", "tool", tool="erp_pay", args={}))
|
||||
graph["edges"] = [{"source": a["id"], "target": b["id"]} for a, b in zip(graph["nodes"], graph["nodes"][1:])]
|
||||
await publish_v2(wf, graph=graph)
|
||||
real = registry.get_capability
|
||||
|
||||
async def get_capability(session, name):
|
||||
if name == "erp_pay":
|
||||
return SimpleNamespace(name=name, effect="write", effect_system="write", endpoint_id=None)
|
||||
return await real(session, name)
|
||||
|
||||
monkeypatch.setattr(registry, "get_capability", get_capability)
|
||||
async with SessionLocal() as session:
|
||||
plan = await recall.build_plan(session, wf, 2)
|
||||
assert plan.groups and all("会写外部系统(erp_pay)" in (g.problem or "") for g in plan.groups), \
|
||||
[g.problem for g in plan.groups]
|
||||
assert await recall.launch(wf, 2) == []
|
||||
|
||||
|
||||
async def test_plan_from_formal_runs_and_exporters_as_owners(monkeypatch):
|
||||
@@ -362,6 +469,74 @@ async def test_recall_notification_has_no_numbers(monkeypatch):
|
||||
assert "周销售" in mine[0]["title"] and "v2" in mine[0]["title"]
|
||||
|
||||
|
||||
async def test_recall_pushes_the_shared_channel_once_per_recall_run(monkeypatch):
|
||||
"""开登录时一组答案有几个主人就开几条待办,但全局渠道(团队群)按召回运行只收一条(答案、待办都按这次召回汇总,链到
|
||||
待办箱);订阅了 task.opened 的全局渠道也不再按每条待办各推一次。每个主人自己的个人渠道各收他那一条。"""
|
||||
from app.core.crypto import encrypt
|
||||
from app.db.models import NotifyChannel, User
|
||||
from app.notify import service
|
||||
|
||||
sent: list[dict] = []
|
||||
|
||||
async def capture(channel, message):
|
||||
sent.append({"channel": channel.name, **message})
|
||||
from app.notify.core import Outcome
|
||||
|
||||
return Outcome(True, 200, 1, None)
|
||||
|
||||
monkeypatch.setattr("app.notify.send", capture)
|
||||
wf = await S.publish()
|
||||
first = await answer(monkeypatch)
|
||||
second = await answer(monkeypatch) # 同一周:同一组,两个主人
|
||||
made: list[str] = []
|
||||
async with SessionLocal() as session:
|
||||
for name, run_id in (("analyst_a", first.id), ("analyst_b", second.id)):
|
||||
user = User(username=name, role="user")
|
||||
session.add(user)
|
||||
await session.flush()
|
||||
(await session.get(Run, run_id)).started_by = name
|
||||
channel = NotifyChannel(name=f"个人-{name}", scope="user", owner_id=user.id, format="generic",
|
||||
url=encrypt(f"https://hooks.example.com/{name}"), events=["ledger.recall"])
|
||||
session.add(channel)
|
||||
await session.flush()
|
||||
made.append(channel.id)
|
||||
for name, events_ in (("团队-召回", ["ledger.recall", "task.opened"]), ("团队-待办", ["task.opened"])):
|
||||
channel = NotifyChannel(name=name, scope="global", url=encrypt("https://hooks.example.com/team"),
|
||||
events=events_)
|
||||
session.add(channel)
|
||||
await session.flush()
|
||||
made.append(channel.id)
|
||||
await session.commit()
|
||||
monkeypatch.setenv("AGENTLAB_AUTH_MODE", "local")
|
||||
try:
|
||||
await publish_v2(wf)
|
||||
[done] = await run_recall(wf)
|
||||
await service.drain()
|
||||
finally:
|
||||
async with SessionLocal() as session:
|
||||
for channel_id in made:
|
||||
await session.delete(await session.get(NotifyChannel, channel_id))
|
||||
await session.commit()
|
||||
tasks = await recall_tasks(done.id)
|
||||
assert len(tasks) == 2, "每个主人一条待办"
|
||||
team = [m for m in sent if m["channel"] == "团队-召回"]
|
||||
assert [m["event"] for m in team] == ["ledger.recall"], [m["event"] for m in team]
|
||||
lines = dict(team[0]["lines"])
|
||||
assert lines["答案"] == "2 个" and lines["待办"] == "2 条", lines
|
||||
assert [label for label, _ in team[0]["links"]] == ["去待办箱"] or team[0]["links"] == []
|
||||
assert [m["event"] for m in sent if m["channel"] == "团队-待办"] == ["task.opened"], "只订阅了新待办的团队渠道也只收一条"
|
||||
for name in ("analyst_a", "analyst_b"):
|
||||
mine = [m for m in sent if m["channel"] == f"个人-{name}"]
|
||||
assert [m["event"] for m in mine] == ["ledger.recall"] and dict(mine[0]["lines"])["答案"] == "1 个", mine
|
||||
# 账本接口里的召回待办只列看的这个人手上的:第一个答案的主人看得到自己那条,另一个主人看不到
|
||||
from app.api.deps import Principal
|
||||
|
||||
async with SessionLocal() as session:
|
||||
own = await recall.answer_marks(session, first.id, Principal(username="analyst_a", role="user", mode="local"))
|
||||
other = await recall.answer_marks(session, first.id, Principal(username="analyst_b", role="user", mode="local"))
|
||||
assert len(own["tasks"]) == 1 and own["tasks"][0]["reason"] == "caliber" and other["tasks"] == []
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 目录事实被改、表结构变了
|
||||
# --------------------------------------------------------------------------
|
||||
@@ -411,6 +586,13 @@ async def test_catalog_fact_change_marks_answers_and_notifies_only_on_new_errors
|
||||
assert await events(unrelated.id, EventType.LEDGER_BASIS_CHANGED) == [], "没查过这张表的不标"
|
||||
[task] = await recall_tasks(row.id)
|
||||
assert task.correlation_key.startswith("catalog:")
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
|
||||
marks = (await c.get(f"/api/runs/{row.id}/ledger")).json()["recall"]
|
||||
quiet = (await c.get(f"/api/runs/{unrelated.id}/ledger")).json()["recall"]
|
||||
[basis] = marks["basis"]
|
||||
assert {k: basis[k] for k in ("kind", "items", "new_errors")} == {"kind": "catalog", "items": 1, "new_errors": 1}
|
||||
assert basis["at"] and marks["tasks"] == [{"id": task.id, "reason": "catalog"}] and marks["checked"] == []
|
||||
assert quiet == {"checked": [], "basis": [], "tasks": []}
|
||||
async with SessionLocal() as session:
|
||||
from app.api.deps import Principal
|
||||
|
||||
@@ -427,6 +609,89 @@ async def test_catalog_fact_change_marks_answers_and_notifies_only_on_new_errors
|
||||
assert len(await events(row.id, EventType.LEDGER_BASIS_CHANGED)) == 1
|
||||
|
||||
|
||||
async def test_catalog_edits_on_one_source_share_one_pass(engine_up, monkeypatch):
|
||||
"""批量审阅一口气改了同一个数据源的几张表:只在后台做一遍(同一个数据源排着做,几张表合在一次里查),查询快照按数据源
|
||||
在库里筛,冻结的表结构快照(大库的能有几 MB)同一份只读一次。"""
|
||||
from collections import Counter
|
||||
|
||||
from app.core import artifact_store
|
||||
from app.data import catalog
|
||||
|
||||
source_id = await S.set_source(engine_up)
|
||||
await _catalog(source_id, {"business_date": _item({"column": "paid_at"}),
|
||||
"columns": {"amount": {"measure": _item("flow")}}})
|
||||
async with SessionLocal() as session:
|
||||
await catalog.write_entry(session, source_id, "stores", {"valid_filter": _item("id > 0")}, if_version=0,
|
||||
actor="tester")
|
||||
week1 = await answer(monkeypatch)
|
||||
week2 = await answer(monkeypatch, "2026-09-08", "2026-09-15")
|
||||
shops = await S.explore(monkeypatch, S.ELSEWHERE, text="门店 [[v:Q1.r0.n]] 家。", origin="chat")
|
||||
scans: list[list[str]] = []
|
||||
loads: list[str] = []
|
||||
real_scan, real_load = recall._explore_snapshots, artifact_store.load
|
||||
|
||||
async def scan(session, source, tables, **kw):
|
||||
scans.append(sorted(tables))
|
||||
return await real_scan(session, source, tables, **kw)
|
||||
|
||||
def load(artifact_id):
|
||||
loads.append(artifact_id)
|
||||
return real_load(artifact_id)
|
||||
|
||||
monkeypatch.setattr(recall, "_explore_snapshots", scan)
|
||||
monkeypatch.setattr(artifact_store, "load", load)
|
||||
async with SessionLocal() as session:
|
||||
orders = await catalog.read_entry(session, source_id, "orders")
|
||||
stores = await catalog.read_entry(session, source_id, "stores")
|
||||
rejected = {**orders.notes, "business_date": {**orders.notes["business_date"], "status": "rejected"}}
|
||||
# 批量审阅逐表写完逐表调审阅之后的钩子(catalog.after_review → recall.after_review),中间不让出事件循环
|
||||
assert recall.after_review(source_id, "orders", orders.notes, rejected, orders.version + 1) == 1
|
||||
assert recall.after_review(source_id, "stores", stores.notes,
|
||||
{"valid_filter": {**stores.notes["valid_filter"], "status": "rejected"}},
|
||||
stores.version + 1) == 1
|
||||
await recall.drain()
|
||||
assert scans == [["orders", "stores"]], "同一个数据源的几张表合成一遍"
|
||||
for run in (week1, week2):
|
||||
[marked] = await events(run.id, EventType.LEDGER_BASIS_CHANGED)
|
||||
assert marked["table"] == "orders" and marked["items"][0]["path"] == "business_date"
|
||||
[marked] = await events(shops.id, EventType.LEDGER_BASIS_CHANGED)
|
||||
assert marked["table"] == "stores" and marked["items"][0]["path"] == "valid_filter"
|
||||
schemas = set()
|
||||
for snapshot in {a for a in loads}:
|
||||
content = real_load(snapshot)
|
||||
if isinstance(content, dict) and content.get("schema_artifact"):
|
||||
schemas.add(content["schema_artifact"])
|
||||
assert schemas and all(Counter(loads)[sid] == 1 for sid in schemas), Counter(loads)
|
||||
|
||||
|
||||
async def test_snapshot_scan_pages_newest_first_and_stops_when_full(engine_up, monkeypatch):
|
||||
"""翻查询快照:只取这个数据源的、新的在前,每张表凑够 BASIS_MAX_RUNS 次运行就不再往下翻。"""
|
||||
from app.db.models import DataSource
|
||||
|
||||
source_id = await S.set_source(engine_up)
|
||||
runs = [await answer(monkeypatch, f"2026-09-0{d}", "2026-09-08") for d in (1, 2, 3)]
|
||||
pages: list[int] = []
|
||||
monkeypatch.setattr(recall, "BASIS_MAX_RUNS", 2)
|
||||
monkeypatch.setattr(recall, "SNAPSHOT_PAGE", 1)
|
||||
async with SessionLocal() as session:
|
||||
source = await session.get(DataSource, source_id)
|
||||
real_execute = session.execute
|
||||
|
||||
async def counted(stmt, *args, **kwargs):
|
||||
pages.append(1)
|
||||
return await real_execute(stmt, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(session, "execute", counted)
|
||||
found = await recall._explore_snapshots(session, source, ["ORDERS"])
|
||||
read = len(pages)
|
||||
other = DataSource(id="elsewhere", name="elsewhere")
|
||||
nothing = await recall._explore_snapshots(session, other, ["orders"])
|
||||
assert [r for r, _ in found["ORDERS"]] == [runs[2].id, runs[1].id], "新的在前、凑够两次运行就停"
|
||||
# 一页一行:第三页读到第三次运行时两次已经凑够,停下,不再翻第四页
|
||||
assert read == 3, f"凑够了不再往下翻(翻了 {read} 页)"
|
||||
assert nothing == {"orders": []}, "别的数据源的快照不读回来"
|
||||
|
||||
|
||||
def test_fact_changes_only_counts_items_that_affect_numbers():
|
||||
before = {"valid_filter": _item("status <> 'open'"), "label": _item("订单"),
|
||||
"columns": {"amount": {"measure": _item("flow"), "label": _item("金额")}}}
|
||||
|
||||
Reference in New Issue
Block a user