Files
AgentLab/backend/tests/test_ledger_match.py
T
ThearchyHelios 5b4bd91476 口径账本内核:运行封存后在后台比对已发布口径、逐个数标口径、按运行读的账本接口、来源链和导出留痕、统计 E1–E4
为什么:问数据的答案要和已治理口径接起来(next-round 02 做法 2、3、9、10;总方案 X1、X2、X3、X12,拍板 L2、L4、L8)。

- 比对(ledger/match.py):探索运行(chat、canvas、trial、personal_app)成功结束、封存完、有查询快照时,在 runner 封存之后和
  notify_run 同一个位置放到后台;候选口径是每个工作流最新的已发布未退役版本里查库的调用工具节点(受管、已发布都算),按数据源
  和表预筛;逐个用 match_template 比,schema 用查询当时冻结的表结构、列名取同一份快照里冻结的数据目录。每次运行限时 8 秒、在
  线程里、同时最多 2 个;超时、快照被改过或读不出、SQL 解析不了照实记 skipped。启动时(run_manager.setup,和调度循环同一个开关)
  补比最近 7 天封存了却没比过的运行,只读工件和版本表。
- 记法(ledger/store.py,拍板 L8):内容寻址工件 kind=caliber_match(agentlab.caliber_match/1)+ run_append 的 sealed 方式追加
  ledger.matched(core/events.py 登记);工件里不放时间和工作流名,同一份封存再比一次哈希一样;meta 记 calibers / related / counts。
- 标签(ledger/labels.py):单元格按查询的比对状态;指标要每个输入都等价于同一份口径同一版、绑定一致,式子按「哪个取数节点、
  第几行、哪一列」换完和口径卡上的一字不差。caliber(how:independent 等价已证明;composed 助手选用留给第 3 波)/ related /
  reasoning / unknown;整题判断附还缺的入口字段。
- 读(ledger/view.py):GET /api/runs/{id}/ledger(ledger_api.runs_router,和证据同一道门),没比对过、能比的按需排上;证据
  片段接口加 ledger 键(没比对过的运行没有这个键,返回一字不差);报告导出的面板最上面多「口径」一步并写明「封存后比对,不在
  封存范围内」。遮罩按读的那一刻重滤:绑定的值是这次运行查到过的遮罩值时不给值,差异条目和逐行对照走 _Shown。
- 来源链:POST /api/runs 加 origin_ref(只在 origin=chat 时收,服务端核对对话访问权和轮次归属,写进 Run.origin 和封存的
  run.origin);WorkflowIn 加 origin(run_id、conversation_id、turn_id,核对规则同 from-run);报告导出和可验证报告下载各记一条
  审计 report.exported。可验证报告列封存后追加的事件时给 ledger.matched 一句只有条数的说明。
- 统计(ledger/stats.py):E1–E4 从工件、运行、查询快照、审计现算,GET /api/ledger/stats(搭建者);E3 用 app/chat/questions
  的聚类加按(数据源、normalize_sql 文本)的确定性重复;评测直接调 compute_stats(可点名 run_ids)。对账反馈、认证转化、按发布
  的召回在后面几波,照实写没有数据。

测试:test_ledger_match.py(真跑探索运行:等价、相关、没有相关口径、写死的码值、非探索运行、超时、快照被改、解析失败、预筛、
指标两种、级别、只认最新版、补比)、test_ledger_api.py(账本接口、按需比、遮罩、证据片段、导出、审计、统计)、
test_run_origin_ref.py(origin_ref 与画布来源的核对)。
2026-10-16 14:02:55 +08:00

291 lines
15 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""认口径:运行封存之后在后台比对已发布口径(app/ledger/match.py、store.py、labels.py,next-round 02 做法 2、3)。
真的跑一遍探索运行(虚构店铺库、mock 模型写报告),等后台比对做完,看比对工件和 ledger.matched:
- 等价的写法:查询 proven、绑定取对、列按位置映射,报告里引用它的数标「来自口径 · 等价已证明」,整题等价、写出还缺的入口字段;
工件 meta 记 calibers,事件追加在封存之后、封存核对照旧一致;同一份封存再比一次工件哈希一样;
- 少一个条件:related,差在哪写「口径只保留……这次没有这个条件」,数标相关口径;
- 没有相关口径:none,数标这次的推理;正式运行、接口发起的运行、失败的运行不比;
- 超时、快照被改过、SQL 解析不了:skipped,照实写原因,数标没比对;
- 指标:输入等价、式子和口径卡一样才算来自口径,式子不同的是相关口径;
- 启动补比:只补没比过的,trigger=backfill。
"""
from __future__ import annotations
import json
import pytest
from sqlalchemy import select
from app.core.config import settings
from app.core.events import EventType
from app.db.base import SessionLocal
from app.db.models import Artifact, Run, RunEvent
from app.engine.runner import run_manager, verify_manifest
from app.ledger import match as ledger_match
from app.ledger import store
from tests import ledger_samples as S
@pytest.fixture(autouse=True)
async def engine_up(tmp_path):
await run_manager.setup()
path = tmp_path / "shop.db"
S.build_shop(path)
await S.set_source(path)
# 每个用例自己发布口径:先把别的用例发布的退役掉,候选口径只看这一个用例的
from app.db.models import WorkflowVersion
from app.db.base import utcnow
async with SessionLocal() as session:
for row in (await session.execute(select(WorkflowVersion).where(WorkflowVersion.retired_at.is_(None)))).scalars():
row.retired_at = utcnow()
await session.commit()
yield path
await ledger_match.drain()
await run_manager.shutdown()
async def matched(run_id: str) -> tuple[store.Recorded, list[RunEvent]]:
recorded = await store.latest(run_id)
assert recorded is not None and recorded.content is not None, recorded
async with SessionLocal() as session:
events = list((await session.execute(select(RunEvent).where(
RunEvent.run_id == run_id, RunEvent.type == EventType.LEDGER_MATCHED).order_by(RunEvent.seq))).scalars())
return recorded, events
def only_label(content: dict) -> dict:
[labels] = [r["labels"] for r in content["reports"].values()]
[label] = labels.values()
return label
async def test_an_equivalent_query_is_proven_and_its_number_comes_from_the_caliber(monkeypatch):
wf = await S.publish()
row = await S.explore(monkeypatch, S.EQUIVALENT, text="本周销售额 [[v:Q1.r0.销售额]]。")
recorded, [event] = await matched(row.id)
content = recorded.content
assert content["format"] == store.FORMAT and content["algo"] == store.ALGO and content["run_id"] == row.id
assert content["manifest_hash"] == row.manifest_hash
[query] = content["queries"]
assert query["status"] == "proven" and query["source"] == "shop" and query["tables"] == ["orders"]
[cand] = query["candidates"]
assert cand["caliber"] == f"{wf}@1" and cand["level"] == "governed" and cand["node_id"] == "fetch"
assert cand["binding"] == {"start": "2026-09-01", "end": "2026-09-08"}
assert cand["columns"] == [{"index": 0, "ours": "销售额", "caliber": "gmv"}]
[caliber] = content["calibers"]
assert caliber["caliber"] == f"{wf}@1" and caliber["fetches"] == ["fetch"] and caliber["cards"] == ["card"]
assert "name" not in caliber, "工件里不存工作流名(名字会改)"
label = only_label(content)
assert label["kind"] == "caliber" and label["how"] == "independent" and label["caliber"] == f"{wf}@1"
assert label["column"] == "gmv" and label["binding"] == {"start": "2026-09-01", "end": "2026-09-08"}
whole = content["whole"]
assert whole["equivalent"] is True and whole["caliber"] == f"{wf}@1" and whole["how"] == "independent"
assert whole["missing"] == ["store"] and whole["missing_required"] == []
# 事件在封存之后,封存核对照旧一致
assert event.seq > row.manifest_seq and event.data["post_seal"] is True
assert event.data["artifact"] == recorded.artifact and event.data["trigger"] == "sealed"
assert event.data["counts"]["proven"] == 1
assert (await verify_manifest(row.id))["ok"] is True
async with SessionLocal() as session:
ref = await session.get(Artifact, (recorded.artifact, row.id))
assert ref.kind == store.KIND and ref.meta["calibers"] == [f"{wf}@1"] and ref.meta["related"] == []
# 同一份封存再比一次:工件一字不差
again = await ledger_match.match_run(row.id, trigger="on_demand")
assert again == recorded.artifact
latest = await store.latest(row.id)
assert latest.trigger == "on_demand" and latest.retro is True
async def test_a_missing_condition_is_related_and_says_where_it_differs(monkeypatch):
wf = await S.publish()
row = await S.explore(monkeypatch, S.WITH_REFUNDS)
recorded, _ = await matched(row.id)
[query] = recorded.content["queries"]
assert query["status"] == "related"
[cand] = query["candidates"]
assert cand["status"] == "related" and cand["caliber"] == f"{wf}@1"
assert cand["binding"] == {"start": "2026-09-01", "end": "2026-09-08"} and cand["via"] == "heuristic"
texts = [item["text"] for item in cand["diff"]["items"]]
assert any("口径只保留" in t and "这次没有这个条件" in t for t in texts), texts
label = only_label(recorded.content)
assert label["kind"] == "related" and label["related"][0]["caliber"] == f"{wf}@1"
assert recorded.content["whole"]["equivalent"] is False
async def test_without_a_related_caliber_the_number_is_this_runs_reasoning(monkeypatch):
await S.publish()
row = await S.explore(monkeypatch, S.ELSEWHERE, text="一共 [[v:Q1.r0.n]] 家店。")
recorded, _ = await matched(row.id)
[query] = recorded.content["queries"]
assert query["status"] == "none" and query["candidates"] == []
assert only_label(recorded.content)["kind"] == "reasoning"
assert recorded.content["calibers"] == []
async def test_a_value_differs_in_the_hard_coded_status_is_not_proven(monkeypatch):
"""写死的码值不同('paid' 对 'refunded'):normalize_sql 文本相同,照样不等价。"""
await S.publish()
sql = S.EQUIVALENT.replace("'paid'", "'refunded'")
row = await S.explore(monkeypatch, sql, text="退款 [[v:Q1.r0.销售额]]。")
recorded, _ = await matched(row.id)
assert recorded.content["queries"][0]["status"] == "related"
@pytest.mark.parametrize("origin, run_class", [("api", "exploratory"), (None, "exploratory")])
async def test_runs_that_are_not_exploration_are_not_compared(monkeypatch, origin, run_class):
await S.publish()
row = await S.explore(monkeypatch, S.EQUIVALENT, origin=origin, run_class=run_class,
text="本周销售额 [[v:Q1.r0.销售额]]。")
assert await store.latest(row.id) is None
async with SessionLocal() as session:
run = await session.get(Run, row.id)
assert ledger_match.ineligible(run) == ledger_match.NOT_EXPLORE
assert await ledger_match.match_run(row.id) is None
def test_ineligible_reasons():
from types import SimpleNamespace
base = dict(run_class="exploratory", origin_kind="chat", status="succeeded", manifest_hash="h")
assert ledger_match.ineligible(SimpleNamespace(**base)) is None
assert ledger_match.ineligible(None) == ledger_match.NOT_FOUND
assert ledger_match.ineligible(SimpleNamespace(**{**base, "run_class": "formal"})) == ledger_match.FORMAL
assert ledger_match.ineligible(SimpleNamespace(**{**base, "origin_kind": "eval"})) == ledger_match.NOT_EXPLORE
assert ledger_match.ineligible(SimpleNamespace(**{**base, "status": "failed"})) == ledger_match.NOT_SUCCEEDED
assert ledger_match.ineligible(SimpleNamespace(**{**base, "manifest_hash": None})) == ledger_match.NOT_SEALED
assert all(ledger_match.wanted("exploratory", o) for o in ("chat", "canvas", "trial", "personal_app"))
assert not ledger_match.wanted("formal", "chat") and not ledger_match.wanted("exploratory", "reconcile")
async def test_a_timeout_is_recorded_as_skipped(monkeypatch):
await S.publish()
monkeypatch.setattr(ledger_match, "schedule", lambda run_id, **kw: False)
row = await S.explore(monkeypatch, S.EQUIVALENT, text="本周销售额 [[v:Q1.r0.销售额]]。")
assert await store.latest(row.id) is None
artifact = await ledger_match.match_run(row.id, timeout=0)
assert artifact
recorded, _ = await matched(row.id)
[query] = recorded.content["queries"]
assert query["status"] == "skipped" and query["reason"] == ledger_match.TIMEOUT
assert query["candidates"][0]["status"] == "skipped"
label = only_label(recorded.content)
assert label["kind"] == "unknown" and label["reason"] == ledger_match.TIMEOUT
async def test_a_tampered_snapshot_is_skipped(monkeypatch):
await S.publish()
monkeypatch.setattr(ledger_match, "schedule", lambda run_id, **kw: False)
# 工件按内容寻址、各用例共用一个工件库:换一种只在这里用的写法,改的那份快照不会被别的用例读到;改完放回去
row = await S.explore(monkeypatch, S.EQUIVALENT + " -- 改快照用", text="本周销售额 [[v:Q1.r0.销售额]]。")
async with SessionLocal() as session:
[end] = (await session.execute(select(RunEvent).where(
RunEvent.run_id == row.id, RunEvent.type == "tool.end"))).scalars()
snap_id = end.data["query_artifact"]
path = settings.data_dir / "artifacts" / snap_id[:2] / f"{snap_id}.json"
original = path.read_text("utf-8")
snap = json.loads(original)
snap["rows"] = [[999999]]
path.write_text(json.dumps(snap, ensure_ascii=False), "utf-8")
try:
await ledger_match.match_run(row.id)
finally:
path.write_text(original, "utf-8")
recorded, _ = await matched(row.id)
[query] = recorded.content["queries"]
assert query["status"] == "skipped" and query["reason"] == ledger_match.SNAPSHOT_TAMPERED
assert only_label(recorded.content)["kind"] == "unknown"
def test_unparseable_sql_is_skipped_with_the_reason():
caliber = ledger_match.Caliber("wf", 1, "governed", "h", (ledger_match.Fetch("fetch", "shop", S.TEMPLATE,
frozenset({"orders"})),))
query = ledger_match.Query("a1", "q", "shop", "SELEC amount FROM orders WHERE", tables=frozenset({"orders"}),
dialect="sqlite")
[row] = ledger_match.compare_queries([query], [caliber])
assert row["status"] == "skipped" and row["reason"] == ledger_match.PARSE_FAILED
def test_candidates_are_prefiltered_by_source_and_table():
fetches = (ledger_match.Fetch("a", "shop", S.TEMPLATE, frozenset({"orders"})),
ledger_match.Fetch("b", "crm", S.TEMPLATE, frozenset({"orders"})),
ledger_match.Fetch("c", "shop", "SELECT COUNT(*) FROM stores", frozenset({"stores"})))
caliber = ledger_match.Caliber("wf", 1, "published", "h", fetches)
query = ledger_match.Query("a1", "q", "shop", S.EQUIVALENT, tables=frozenset({"orders"}), dialect="sqlite")
[row] = ledger_match.compare_queries([query], [caliber])
assert [c["node_id"] for c in row["candidates"]] == ["a"] and row["status"] == "proven"
async def test_a_metric_from_equivalent_inputs_with_the_same_formula_comes_from_the_caliber(monkeypatch):
wf = await S.publish()
row = await S.explore(monkeypatch, S.EQUIVALENT, text="本周销售额 [[m:gmv]]。",
card_expression="cell(nodes.q, 0, '销售额')")
recorded, _ = await matched(row.id)
label = only_label(recorded.content)
assert label["kind"] == "caliber" and label["caliber"] == f"{wf}@1"
assert label["card"] == "card" and label["metric"] == "gmv"
assert recorded.content["whole"]["equivalent"] is True
async def test_a_metric_with_a_different_formula_is_only_related(monkeypatch):
wf = await S.publish()
row = await S.explore(monkeypatch, S.EQUIVALENT, text="本周销售额 [[m:gmv]]。",
card_expression="cell(nodes.q, 0, '销售额') * 2")
recorded, _ = await matched(row.id)
label = only_label(recorded.content)
assert label["kind"] == "related" and label["reason"] == "取数和口径等价,但指标的算法和口径卡上的不同"
assert label["related"][0]["caliber"] == f"{wf}@1"
async def test_published_and_governed_both_count_and_the_label_says_which(monkeypatch):
wf = await S.publish(level="published")
row = await S.explore(monkeypatch, S.EQUIVALENT, text="本周销售额 [[v:Q1.r0.销售额]]。")
recorded, _ = await matched(row.id)
label = only_label(recorded.content)
assert label["kind"] == "caliber" and label["level"] == "published" and label["caliber"] == f"{wf}@1"
async def test_only_the_latest_published_version_is_a_candidate(monkeypatch):
wf = await S.publish()
await S.publish(workflow_id=wf, version=2, graph=S.caliber_graph(S.TEMPLATE.replace("'paid'", "'open'")))
row = await S.explore(monkeypatch, S.EQUIVALENT, text="本周销售额 [[v:Q1.r0.销售额]]。")
recorded, _ = await matched(row.id)
[query] = recorded.content["queries"]
assert [c["caliber"] for c in query["candidates"]] == [f"{wf}@2"] and query["status"] == "related"
async def test_backfill_matches_only_runs_never_matched(monkeypatch):
wf = await S.publish()
monkeypatch.setattr(ledger_match, "schedule", lambda run_id, **kw: False)
row = await S.explore(monkeypatch, S.EQUIVALENT, text="本周销售额 [[v:Q1.r0.销售额]]。")
assert await store.latest(row.id) is None
assert await ledger_match.backfill() >= 1
recorded, [event] = await matched(row.id)
assert event.data["trigger"] == "backfill" and recorded.retro is True
assert recorded.content["queries"][0]["candidates"][0]["caliber"] == f"{wf}@1"
# 再补一次:比过的不再比
await ledger_match.backfill()
_, events = await matched(row.id)
assert len(events) == 1
async def test_backfill_follows_the_scheduler_switch(monkeypatch):
monkeypatch.setattr(settings, "scheduler", False)
assert ledger_match.start_backfill() is False
async def test_only_sealed_query_snapshots_are_compared(monkeypatch):
"""比对只读封存范围内交回过的查询快照(api/evidence._sealed 的读法):事后往工件表里插一份属于这次运行的快照,不比它。"""
from app.core.artifact_store import put_json
await S.publish()
row = await S.explore(monkeypatch, S.EQUIVALENT, text="本周销售额 [[v:Q1.r0.销售额]]。")
await put_json({"sql": S.WITH_REFUNDS, "source": "shop", "columns": ["gmv"], "rows": [[1]]},
kind="query_snapshot", run_id=row.id, node_id="q")
await ledger_match.match_run(row.id, trigger="on_demand")
recorded, _ = await matched(row.id)
assert [q["status"] for q in recorded.content["queries"]] == ["proven"]