合入期 4 证据面板核查状态与打磨:证据接口带上双盲复算、反方审稿的状态,指标步骤里遮罩列的输入只写「已遮罩」;可验证报告里输入取自遮罩列的口径卡只给 id;推演与口径卡共用取整;运行对比离开后晚到的答复不再跳走

This commit is contained in:
ThearchyHelios
2026-10-06 11:31:02 +08:00
22 changed files with 2673 additions and 96 deletions
+38 -5
View File
@@ -14,8 +14,11 @@
- two-reports:另有一份月报,两份报告各自的引用链
- legacy:早期运行,封存范围按结束事件推算(运行结束事件在范围外,运行结果无法核对)
- failed:失败结束的运行(没有出具判定),封存说明多一句「继续运行会重新封存」
- continued:早期运行先失败、经继续运行跑到成功:范围里有那条运行失败,之后运行又继续了,成功结束的那条在范围外
- degraded:两条出具判定,先完整出具、后降档出具,认最后一条
- missing-doc / missing-card:导出时服务端已没有报告文档 / 口径卡的文件
- masked-card:口径卡上一个指标的输入取自会员消费的号码列(设了遮罩),卡上记着号码原值:整张卡只给 id,引用它的
数字无法核对
- nan:运行开始事件和门店流水快照里有 json.dumps 写出的裸 NaN(快照里那一列报告没有引用)
- stray:报告自查记下的文档是另一次运行写的
"""
@@ -69,14 +72,25 @@ def shop(nan: bool = False) -> dict[str, Any]:
"sql": "SELECT store, amount, ratio FROM orders ORDER BY id", "truncated": False}
class Sample:
"""一次虚构的已封存运行:工件(原字节)、证据目录、报告文档、事件。"""
def masked_card(crm_id: str) -> dict[str, Any]:
"""口径卡多一个指标:联系电话位数,输入是会员消费第 0 行的号码(phone 设了遮罩),卡上记着号码原值和代入式。"""
cell = "cell(nodes.members, 0, 'phone')"
contact = {"id": "contact", "name": "联系电话位数", "value": 8, "unit": "位", "decimals": 0, "format": "thousands",
"expression": f"len({cell})", "substituted": f"len('{CRM['rows'][0][1]}')",
"inputs": [{"path": cell, "value": CRM["rows"][0][1], "node_id": "members", "via": "tool_cell",
"artifact": crm_id, "locator": {"row": 0, "column": "phone"}, "status": "verified"}]}
return {**CARD, "metrics": [*CARD["metrics"], contact]}
def __init__(self, *, nan: bool = False) -> None:
class Sample:
"""一次虚构的已封存运行:工件(原字节)、证据目录、报告文档、事件。masked=True 时口径卡带着取自遮罩列的输入。"""
def __init__(self, *, nan: bool = False, masked: bool = False) -> None:
self.files: dict[str, bytes] = {}
self.shop_id = self.put(shop(nan))
self.crm_id = self.put(CRM)
self.card_id = self.put(CARD)
self.card = masked_card(self.crm_id) if masked else CARD
self.card_id = self.put(self.card)
self.ledger = [
{"kind": "query", "node_id": "fetch", "exec": 1, "artifact": self.shop_id, "tool": "db_query__shop",
"source": "shop", "columns": ["store", "amount", "ratio"], "rows": len(STORES), "truncated": False},
@@ -84,7 +98,7 @@ class Sample:
"source": "crm", "columns": CRM["columns"], "rows": len(CRM["rows"]), "truncated": False},
{"kind": "metric_set", "node_id": "card", "artifact": self.card_id},
]
self.catalog = build_catalog(nodes={"card": CARD}, ledger=self.ledger)
self.catalog = build_catalog(nodes={"card": self.card}, ledger=self.ledger)
def put(self, obj: Any) -> str:
artifact, raw = stored(obj)
@@ -133,6 +147,16 @@ class Sample:
add("issuance", "out", {"tier": tier, "mode": "citations"})
add("node.finished", "out", {"duration_ms": 2, "attempt": 1, "preview": {"周报": "(报告正文)"}})
add("run.finished", None, {"output": {"周报": "(报告正文)"}})
elif ending == "continued":
# 先失败,再从成果节点继续运行到成功(runner.continue_failed 记一条 run.resumed)
add("node.failed", "out", {"error": "出口复核没有通过"})
add("run.failed", None, {"error": "出口复核没有通过"})
add("run.resumed", None, {"from": ["out"], "message": "从「成果」继续运行,保留前面 6 个节点的结果",
"actor": "值班分析员"})
for tier in tiers:
add("issuance", "out", {"tier": tier, "mode": "citations"})
add("node.finished", "out", {"duration_ms": 2, "attempt": 1, "preview": {"周报": "(报告正文)"}})
add("run.finished", None, {"output": {"周报": "(报告正文)"}})
else:
add("node.failed", "out", {"error": "出口复核没有通过"})
add("run.failed", None, {"error": "出口复核没有通过"})
@@ -172,6 +196,11 @@ def pages() -> dict[str, str]:
events, bound = s.events({"write": weekly}, ending="failed")
out["failed"] = render(s, events, bound, run={**RUN, "status": "failed"})
# 早期运行先失败、经继续运行跑到成功:封存范围同样按结束事件推算,到成功结束的前一条为止
events, _ = s.events({"write": weekly}, ending="continued")
finished = next(seq for seq, etype, _, _ in events if etype == "run.finished")
out["continued"] = render(s, events, finished - 1, legacy=True)
events, bound = s.events({"write": weekly}, tiers=("formal", "degraded"))
out["degraded"] = render(s, events, bound)
@@ -182,6 +211,10 @@ def pages() -> dict[str, str]:
n = Sample(nan=True)
events, bound = n.events({"write": n.doc(WEEKLY, "write")}, nan=True)
out["nan"] = render(n, events, bound)
m = Sample(masked=True)
events, bound = m.events({"write": m.doc(WEEKLY, "write")})
out["masked-card"] = render(m, events, bound)
return out
+11 -2
View File
@@ -462,8 +462,8 @@ def test_the_sample_pages_for_the_browser_check_are_sealed_for_real():
from tests.bundle_samples import pages
got = {name: json.loads(data_block(page)) for name, page in pages().items()}
assert set(got) == {"base", "two-reports", "legacy", "failed", "degraded", "missing-doc", "missing-card", "nan",
"stray"}
assert set(got) == {"base", "two-reports", "legacy", "failed", "continued", "degraded", "missing-doc",
"missing-card", "nan", "stray", "masked-card"}
for name, bundle in got.items():
assert browser_manifest(bundle["events"]) == bundle["meta"]["manifest_hash"], name
@@ -490,9 +490,18 @@ def test_the_sample_pages_for_the_browser_check_are_sealed_for_real():
failed = got["failed"]
assert failed["meta"]["status"] == "failed" and failed["meta"]["tier"] is None
assert max(failed["events"], key=lambda e: e["seq"])["type"] == "run.failed"
# 先失败、经继续运行跑到成功的早期运行:范围里有那条运行失败,之后运行又继续了,最后一条不是结束事件
continued = got["continued"]
sealed_types = [e["type"] for e in sorted(continued["events"], key=lambda e: e["seq"])]
assert continued["meta"]["legacy"] is True and continued["meta"]["status"] == "succeeded"
assert "run.failed" in sealed_types and sealed_types[sealed_types.index("run.failed") + 1] == "run.resumed"
assert sealed_types[-1] == "node.finished" and "run.finished" in [a["type"] for a in continued["appended"]]
issuances = [e["seq"] for e in got["degraded"]["events"] if e["type"] == "issuance"]
assert len(issuances) == 2 and got["degraded"]["meta"]["tier"] == {"value": "degraded", "seq": issuances[-1]}
assert [a["kind"] for a in got["stray"]["artifacts"]] == ["report_doc"]
# 口径卡上有输入取自遮罩列:整张卡只给 id,页面里找不到那个号码
card = next(a for a in got["masked-card"]["artifacts"] if a["kind"] == "metric_set")
assert card.get("masked") is True and "b64" not in card
nan = got["nan"]
assert "NaN" in next(e["canonical"] for e in nan["events"] if e["type"] == "run.started")
assert b"NaN" in base64.b64decode(next(a["b64"] for a in nan["artifacts"] if a.get("node_id") == "fetch"))
+107
View File
@@ -0,0 +1,107 @@
"""可验证报告里的口径卡和遮罩(期 4 I2a 复核):口径卡上记着每个输入的值和代入式,有输入取自遮罩列时整张卡只给 id。
以前只有查询快照会因遮罩不随文件附带,口径卡一律原样嵌入:证据面板上已经遮住的号码(cell() 取的遮罩格、按位置取的
vars.res.rows[0][1]),在这份要发给别人的文件里照样看得到。认哪个输入取自哪份查询结果的哪几列,和证据面板、推演同一个
认法(recompute.input_origin);核对不了的宁可多挡。
工件、事件都是手搭的(会员、号码都是编的),照 tests/test_bundle_build.py 的样子。
"""
from __future__ import annotations
import base64
import pytest
from app.bundle.build import build_bundle, render_html
from tests.test_bundle_build import GRAPH, MASKED_ID, RUN, SNAP_MASKED, STORE, doc_of, stored
#: SNAP_MASKED 里的号码(phone 设了遮罩)
PHONE = SNAP_MASKED["rows"][0][1]
CELL = "cell(nodes.members, 0, 'phone')"
def card_with(inputs, substituted="len('000-0001')", expression=f"len({CELL})"):
"""会员号码位数一张卡:报告引用的是它(m:gmv 指过来)。"""
return {"kind": "metric_set", "caliber": "会员口径", "caliber_version": "v1", "metrics": [
{"id": "gmv", "name": "号码位数", "value": 1280.5, "unit": "元", "decimals": 1, "expression": expression,
"substituted": substituted, "inputs": inputs}]}
def bundle_of(card, *, outputs=None, source_masks=None):
"""一次手搭的运行:会员查询(带遮罩的快照)→ 数据整形(outputs 里的产出)→ 口径卡 → 报告。"""
card_id, card_raw = stored(card)
doc = doc_of(extra_catalog={"m:gmv": {"alias": "m:gmv", "kind": "metric", "artifact": card_id, "node_id": "card",
"locator": {"metric": "gmv"}, "value": 1280.5}})
doc_id, doc_raw = stored(doc)
files = {**STORE, card_id: card_raw, doc_id: doc_raw}
events = [
(1, "run.started", None, {"nodes": 7}),
(2, "tool.end", "members", {"tool": "db_query__crm", "query_artifact": MASKED_ID}),
(3, "node.finished", "members", {"evidence": [{"kind": "query", "node_id": "members", "artifact": MASKED_ID}]}),
]
for node_id, content in (outputs or {}).items():
artifact, raw = stored(content) if content is not None else ("ee" * 32, None)
if raw is not None:
files[artifact] = raw
events.append((len(events) + 1, "node.finished", node_id, {"artifact": artifact}))
events += [
(len(events) + 1, "node.started", "card", {}),
(len(events) + 2, "node.finished", "card", {"evidence": [{"kind": "metric_set", "node_id": "card",
"artifact": card_id}]}),
(len(events) + 3, "report.checked", "write", {"doc_artifact": doc_id, "ok": True}),
(len(events) + 4, "run.finished", None, {"output": {}}),
]
bundle = build_bundle(RUN, events, manifest_seq=len(events), manifest_hash="0" * 64, graph=GRAPH,
source_masks=source_masks or {}, read=files.get, exported_at="2026-10-06T08:00:00+00:00")
entry = next(a for a in bundle["artifacts"] if a["id"] == card_id)
return bundle, entry
def cell_input(column, value):
return {"path": f"cell(nodes.members, 0, '{column}')", "value": value, "node_id": "members", "via": "tool_cell",
"artifact": MASKED_ID, "locator": {"row": 0, "column": column}, "status": "verified"}
def test_a_card_reading_a_masked_cell_ships_only_its_id():
bundle, entry = bundle_of(card_with([cell_input("phone", PHONE)]))
assert entry == {"id": entry["id"], "kind": "metric_set", "node_id": "card", "masked": True}
assert PHONE not in render_html(bundle)
def test_a_card_reading_only_unmasked_columns_is_embedded():
"""同一份快照里没遮的列(消费额):卡上没有遮罩列的值,照常嵌入、照常核对。"""
card = card_with([cell_input("spend", 320.0)], substituted="320.0", expression="cell(nodes.members, 0, 'spend')")
_, entry = bundle_of(card)
assert "masked" not in entry and base64.b64decode(entry["b64"]) == stored(card)[1]
def test_a_mask_set_on_the_source_now_counts_too():
card = card_with([cell_input("spend", 320.0)], substituted="320.0", expression="cell(nodes.members, 0, 'spend')")
_, entry = bundle_of(card, source_masks={"crm": ["spend"]})
assert entry.get("masked") is True and "b64" not in entry
PARSED = {"columns": SNAP_MASKED["columns"], "rows": SNAP_MASKED["rows"]}
POSITIONAL = "vars.res.rows[0][1]"
@pytest.mark.parametrize("parsed, masked", [
# 数据整形原样解析的查询结果(带着快照的 id):认得出取自 phone 列
({**PARSED, "artifact": MASKED_ID, "source": "crm"}, True),
# 写变量的节点的产出读不出来:核对不了,宁可多挡
(None, True),
# 结果没存快照、只带着数据源名:查询当时记下的遮罩无从核对,宁可多挡
({**PARSED, "source": "crm"}, True),
# 改过形状、既没带快照的 id 也没带数据源名:认不出出处,照常嵌入(和证据面板、推演一样,是已知的缺口)
(PARSED, False),
])
def test_a_card_reading_a_cell_by_position_is_judged_the_same_way(parsed, masked):
item = {"path": POSITIONAL, "value": PHONE, "node_id": "parse", "via": "transform", "field": "rows[0][1]",
"status": "ok"}
card = card_with([item], substituted=f"len('{PHONE}')", expression=f"len({POSITIONAL})")
bundle, entry = bundle_of(card, outputs={"parse": parsed})
assert (entry.get("masked") is True) is masked and ("b64" in entry) is not masked
if masked:
assert PHONE not in render_html(bundle)
else:
assert PHONE in base64.b64decode(entry["b64"]).decode()
+416
View File
@@ -0,0 +1,416 @@
"""证据面板指标步骤的遮罩(期 4 I2a):取自遮罩列的输入,输入标签和代入式里都不显示它的值。
以前只有查询步骤的结果窗口把遮罩列换成「已遮罩」,指标步骤的输入(「值 ← 路径」)和代入式照样写着原值——同一个
号码,表格里遮着,上面的标签里亮着。现在:
- cell() 取的格、Agent 交来的字段:按定位里的列认
- vars.res.rows[0][2] 这种按位置取自查询结果的标量引用:顺着取值链找到那份查询结果(写变量的节点当时的产出),
按列号认出是哪一列
- 遮的是「查询当时记下的」加「数据源现在设的」,和查询步骤同一套;数据源事后去掉了遮罩,当时遮着的照样遮
- 取自遮罩列的输入值写「已遮罩」,带上是哪几列(masked),模型报的值、快照里的值一并不给;代入式里那一处写「已遮罩」,
遮罩列也进 redacted.columns(面板底下那句「遮罩不是安全边界」)
- 没有遮罩的指标和以前一字不差;推演(api/whatif.py)同样认得出按位置取的遮罩列,不给值、不推演
真跑一遍图(虚构的 SQLite 订单库,号码是编的),报告由 MockChatModel 照剧本写。
"""
from __future__ import annotations
import asyncio
import json
import sqlite3
import pytest
from httpx import ASGITransport, AsyncClient
from langchain_core.messages import AIMessage
from sqlalchemy import select
from app.core import artifact_store
from app.data.engine import engines
from app.db.base import SessionLocal
from app.db.models import DataSource, Run
from app.engine.evidence import iter_segments
from app.engine.runner import run_manager
from app.main import app
SQL = "SELECT id, region, phone, amount FROM orders ORDER BY id"
#: 号码写成带横线的文本:全是数字的文本 cell() 会按数读(口径卡在算数),len() 就算不了
ROWS = [(1, "华东", "139-0000-0001", 120.5), (2, "华南", "139-0000-0002", 80.0), (3, "华北", "139-0000-0003", 99.5)]
PHONES = [r[2] for r in ROWS]
#: 号码那一格按位置取:vars.res 是数据整形解析好的查询结果,第 2 列是 phone
FIRST_PHONE = "vars.res.rows[0][2]"
SECOND_PHONE = "vars.res.rows[1][2]"
PHONE_CELL = "cell(nodes.fetch, 0, 'phone')"
METRICS = [
{"id": "amt", "name": "首单金额", "unit": "元", "decimals": 1, "format": "thousands",
"expression": "cell(nodes.fetch, 0, 'amount')"},
{"id": "plen", "name": "号码位数", "unit": "位", "format": "thousands", "expression": f"len({PHONE_CELL})"},
{"id": "plen2", "name": "号码位数两倍", "unit": "位", "format": "thousands", "expression": f"len({FIRST_PHONE}) * 2"},
{"id": "mix", "name": "混合", "unit": "分", "decimals": 1, "format": "thousands",
"expression": f"round(cell(nodes.fetch, 1, 'amount') + len({SECOND_PHONE}), 1)"},
]
TEXT = "首单金额 [[m:amt]],号码 [[m:plen]],两倍 [[m:plen2]],混合 [[m:mix]]。"
@pytest.fixture(autouse=True)
async def engine_up():
await run_manager.setup()
yield
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 set_source(path, options=None):
async with SessionLocal() as session:
row = (await session.execute(select(DataSource).where(DataSource.name == "shop"))).scalar_one_or_none()
if row is None:
row = DataSource(name="shop", kind="sqlite", database=str(path), readonly=True, enabled=True)
session.add(row)
row.database, row.kind, row.enabled, row.readonly = str(path), "sqlite", True, True
row.options = dict(options or {})
await session.commit()
return row.id
@pytest.fixture
async def shop(tmp_path):
path = tmp_path / "shop.db"
db = sqlite3.connect(path)
db.executescript("CREATE TABLE orders (id INTEGER PRIMARY KEY, region TEXT, phone TEXT, amount REAL);")
db.executemany("INSERT INTO orders VALUES (?, ?, ?, ?)", ROWS)
db.commit()
db.close()
source_id = await set_source(path)
await engines.invalidate(source_id)
yield path
await engines.invalidate(source_id)
def node(nid, ntype, **config):
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}}
def graph():
nodes = [
node("start", "input"),
node("fetch", "tool", tool="db_query__shop", args={"sql": SQL}),
node("parse", "transform", mode="json", template="{{ nodes.fetch }}", assign_to="res"),
node("card", "metrics", caliber="遮罩口径", caliber_version="v1", metrics=METRICS),
node("write", "report", instructions="写一句", on_violation="flag"),
node("out", "output", fields=[{"name": "报告", "value": "{{ nodes.write.text }}"}]),
]
return {"nodes": nodes, "edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}
async def run_graph(monkeypatch) -> Run:
from app.providers import mock_model
monkeypatch.setattr(mock_model.MockChatModel, "_decide", lambda self, messages: AIMessage(content=TEXT))
run_id = (await run_manager.start(graph=graph(), input_payload={})).id
for _ in range(400):
await asyncio.sleep(0.05)
async with SessionLocal() as session:
row = await session.get(Run, run_id)
if row.status in ("succeeded", "failed") and row.manifest_seq is not None:
assert row.status == "succeeded", row.error
return row
raise AssertionError(f"运行 {run_id} 没跑完:{row.status}")
async def segment(client, run_id: str, text: str) -> dict:
graph_body = (await client.get(f"/api/runs/{run_id}/evidence")).json()
[report] = graph_body["reports"]
doc = artifact_store.load(report["doc_artifact"])
seg = next(s for s in iter_segments(doc) if s["text"] == text)
res = await client.get(f"/api/runs/{run_id}/evidence/segments/{seg['id']}")
assert res.status_code == 200, res.text
return res.json()
def inputs_of(body: dict) -> dict[str, dict]:
return {s["path"]: s for s in body["chain"] if s["step"] == "input"}
def leaks(body: dict) -> list[str]:
raw = json.dumps(body, ensure_ascii=False)
return [p for p in PHONES if p in raw]
# --------------------------------------------------------------------------
async def test_a_masked_cell_is_hidden_from_the_input_label_and_the_substituted_expression(client, monkeypatch, shop):
await set_source(shop, {"mask_columns": ["phone"]})
row = await run_graph(monkeypatch)
body = await segment(client, row.id, "13位")
metric = body["chain"][0]
assert metric["step"] == "metric" and metric["expression"] == f"len({PHONE_CELL})"
assert metric["substituted"] == "len(已遮罩)"
[cell] = inputs_of(body).values()
assert cell["value"] == "已遮罩" and cell["masked"] == ["phone"] and cell["via"] == "tool_cell"
assert "snapshot_value" not in cell and "model_value" not in cell
# 那一格所在的查询步骤照旧遮着;面板底下的遮罩说明带上这一列
[query] = [s for s in body["chain"] if s["step"] == "query"]
assert query["masked"] == ["phone"] and body["redacted"]["columns"] == ["phone"]
assert leaks(body) == []
async def test_a_scalar_taken_by_position_from_a_masked_column_is_hidden(client, monkeypatch, shop):
"""vars.res.rows[0][2]:没有 cell() 的定位,顺着取值链认出取自查询结果的 phone 列。"""
await set_source(shop, {"mask_columns": ["phone"]})
row = await run_graph(monkeypatch)
body = await segment(client, row.id, "26位")
metric = body["chain"][0]
assert metric["substituted"] == "len(已遮罩) * 2"
[phone] = inputs_of(body).values()
assert phone["path"] == FIRST_PHONE and phone["value"] == "已遮罩" and phone["masked"] == ["phone"]
assert body["redacted"]["columns"] == ["phone"]
assert leaks(body) == []
async def test_only_the_masked_input_is_hidden_when_inputs_are_mixed(client, monkeypatch, shop):
await set_source(shop, {"mask_columns": ["phone"]})
row = await run_graph(monkeypatch)
body = await segment(client, row.id, "93.0分")
assert body["chain"][0]["substituted"] == "round(80.0 + len(已遮罩), 1)"
inputs = inputs_of(body)
amount = inputs["cell(nodes.fetch, 1, 'amount')"]
assert amount["value"] == 80.0 and "masked" not in amount
assert inputs[SECOND_PHONE]["value"] == "已遮罩" and inputs[SECOND_PHONE]["masked"] == ["phone"]
assert leaks(body) == []
# 没用到遮罩列的指标和以前一样
plain = await segment(client, row.id, "120.5元")
assert plain["chain"][0]["substituted"] == "120.5"
[amt] = inputs_of(plain).values()
assert amt["value"] == 120.5 and "masked" not in amt
async def test_masks_recorded_at_query_time_stay_after_the_source_drops_them(client, monkeypatch, shop):
await set_source(shop, {"mask_columns": ["phone"]})
row = await run_graph(monkeypatch)
await set_source(shop, {})
for text in ("13位", "26位", "93.0分"):
body = await segment(client, row.id, text)
assert "已遮罩" in body["chain"][0]["substituted"] and leaks(body) == [], text
async def test_masks_added_after_the_run_apply_too(client, monkeypatch, shop):
row = await run_graph(monkeypatch)
before = await segment(client, row.id, "26位")
assert before["chain"][0]["substituted"] == "len('139-0000-0001') * 2"
assert inputs_of(before)[FIRST_PHONE]["value"] == "139-0000-0001" and "masked" not in inputs_of(before)[FIRST_PHONE]
await set_source(shop, {"mask_columns": "phone"})
after = await segment(client, row.id, "26位")
assert after["chain"][0]["substituted"] == "len(已遮罩) * 2" and leaks(after) == []
async def test_without_masks_the_metric_step_is_unchanged(client, monkeypatch, shop):
row = await run_graph(monkeypatch)
body = await segment(client, row.id, "93.0分")
assert body["chain"][0]["substituted"] == "round(80.0 + len('139-0000-0002'), 1)"
assert all("masked" not in s for s in body["chain"] if s["step"] == "input")
assert body["redacted"] == {"columns": []}
async def test_the_whatif_withholds_a_masked_scalar_taken_by_position(client, monkeypatch, shop):
"""推演同一套:按位置取自遮罩列的输入不给值,整个指标推演不了,和证据面板说的一致。"""
await set_source(shop, {"mask_columns": ["phone"]})
row = await run_graph(monkeypatch)
res = await client.post(f"/api/runs/{row.id}/evidence/whatif", json={"card": "card", "metric": "plen2"})
assert res.status_code == 200, res.text
[plen2] = res.json()["metrics"]
assert plen2["status"] == "masked" and plen2["substituted"] is None
[phone] = plen2["inputs"]
assert phone["path"] == FIRST_PHONE and phone["masked"] is True and phone["old"] is None
assert not [p for p in PHONES if p in res.text]
# 改它的值同样不行:遮罩列不能拿来推演
res = await client.post(f"/api/runs/{row.id}/evidence/whatif",
json={"card": "card", "metric": "plen2", "overrides": {"inputs": {FIRST_PHONE: 1}}})
assert res.status_code == 422 and "遮罩" in res.json()["detail"]
# --------------------------------------------------------------------------
# 手搭的封存范围:模型报的值、快照里的值,以及遮没遮核对不了的几种
# --------------------------------------------------------------------------
#: 一份带遮罩的快照:phone 在查询当时就设了遮罩(记在快照里)。数据源名是编的,库里没有这个数据源(现在的遮罩为空)
GHOST = "ghost_mask_src"
PHONE_SNAP = {"columns": ["id", "phone"], "rows": [[1, "139-0000-0009"]], "source": GHOST,
"sql": "SELECT id, phone FROM members", "mask_columns": ["phone"]}
#: 这几条用例里出现过的号码,答复里一个都不能有
SECRETS = ["139-0000-0009", "139-0000-0008", "139-0000-0007", "139-0000-0006", "139-0000-0005", "139-0000-0004"]
CELL = "cell(nodes.fetch, 0, 'phone')"
def card_metric(mid, expression, substituted, inputs, value=13):
return {"id": mid, "name": mid, "unit": "位", "value": value, "rendered": f"{value}位", "format": "thousands",
"expression": expression, "substituted": substituted, "recompute_ok": True, "inputs": inputs,
"status": "ok"}
async def hand_chain(metrics, *, queries=(), outputs=None, metric_id=None):
"""手搭一次封存了的运行(取数 → 数据整形 → 口径卡),直接走指标步骤(evidence._metric_chain)。queries 是封存范围内
交回过的查询快照,outputs 是口径卡算之前各节点的产出工件。"""
from app.api import evidence
from app.core.artifact_store import put_json
from app.engine.evidence import make_eid
card = await put_json({"kind": "metric_set", "caliber": "遮罩口径", "caliber_version": "v1", "metrics": metrics},
kind="metric_set")
events = [(i + 1, "node.finished", nid, {"artifact": art}) for i, (nid, art) in enumerate((outputs or {}).items())]
events += [(len(events) + 1, "node.started", "card", {}),
(len(events) + 2, "node.finished", "card", {"evidence": [
{"kind": "metric_set", "node_id": "card", "artifact": card}]})]
sealed = evidence._Sealed(run_id="syn-mask", graph=graph(), events=events,
seal={"sealed": True, "ok": True, "manifest_seq": len(events), "legacy": False})
sealed.queries = {a: {"node_id": "fetch", "tool": f"db_query__{GHOST}", "source": GHOST} for a in queries}
sealed.ledger = {("card", card)}
mid = metric_id or metrics[0]["id"]
eid = make_eid("metric", card, {"metric": mid})
cite = {"alias": f"m:{mid}", "kind": "metric", "locator": {"metric": mid}, "status": "resolved", "eid": eid}
entry = {"alias": f"m:{mid}", "kind": "metric", "artifact": card, "node_id": "card", "locator": {"metric": mid},
"eid": eid}
seg = {"id": "s1", "kind": "number", "text": "13位", "state": "deterministic", "cite": cite}
return await evidence._metric_chain(seg, cite, entry, {"catalog": {f"m:{mid}": entry}}, sealed, evidence._Masks())
def said(chain) -> list[str]:
raw = json.dumps(chain, ensure_ascii=False)
return [p for p in SECRETS if p in raw]
async def test_what_the_model_reported_and_what_the_snapshot_held_are_withheld_too():
"""对不上的输入另记着两边的值:Agent 字段的 model_value(模型报的)、cell() 的 snapshot_value(快照里的)。取自遮罩列
时一并拿掉——界面上不显示它们,接口答复里同样不能带着。"""
from app.core.artifact_store import put_json
snap = await put_json(PHONE_SNAP, kind="query")
cell = card_metric("plen", f"len({CELL})", "len('139-0000-0008')", [
{"path": CELL, "value": "139-0000-0008", "node_id": "fetch", "via": "tool_cell", "artifact": snap,
"locator": {"row": 0, "column": "phone"}, "status": "mismatch", "snapshot_value": "139-0000-0009"}])
agent = card_metric("alen", "len(vars.kpi.phone)", "len('139-0000-0009')", [
{"path": "vars.kpi.phone", "value": "139-0000-0009", "node_id": "bot", "via": "agent_field", "field": "phone",
"artifact": snap, "locator": {"row": 0, "column": "phone"}, "status": "mismatch",
"model_value": "139-0000-0007"}])
for metric in (cell, agent):
chain = await hand_chain([metric], queries=[snap])
[step] = [s for s in chain if s["step"] == "input"]
assert step["value"] == "已遮罩" and step["masked"] == ["phone"], metric["id"]
assert "model_value" not in step and "snapshot_value" not in step, metric["id"]
assert chain[0]["substituted"] == "len(已遮罩)" and said(chain) == [], (metric["id"], said(chain))
async def test_a_substituted_expression_that_does_not_reproduce_is_not_shown():
"""卡上记的代入式和按卡上的输入重新代出来的对不上(老卡、卡上记的不是原值):整条代入式不给,宁可不显示,也不能让
遮着的值从对不上的那一处漏出来。"""
from app.core.artifact_store import put_json
from app.engine import recompute
snap = await put_json(PHONE_SNAP, kind="query")
odd = card_metric("odd", f"len({CELL})", "len('139-0000-0006')", [
{"path": CELL, "value": "139-0000-0005", "node_id": "fetch", "via": "tool_cell", "artifact": snap,
"locator": {"row": 0, "column": "phone"}, "status": "verified"}])
assert recompute.substituted_without(odd, {CELL}, "已遮罩") is None
chain = await hand_chain([odd], queries=[snap])
assert chain[0]["substituted"] is None and said(chain) == [], said(chain)
# 对得上的照常换掉那一处
assert recompute.substituted_without({**odd, "substituted": "len('139-0000-0005')"}, {CELL}, "已遮罩") == "len(已遮罩)"
async def test_a_cell_on_a_snapshot_that_cannot_be_read_is_withheld():
"""cell() 落在的快照读不出来(文件不在了):数据源现在没遮这一列,可查询当时记下的遮罩在快照里,遮没遮核对不了——
宁可多遮,和推演一致。说法另写,不说「这一列设置了遮罩」(不进 masked)。"""
gone = "dd" * 32
metric = card_metric("plen", f"len({CELL})", "len('139-0000-0004')", [
{"path": CELL, "value": "139-0000-0004", "node_id": "fetch", "via": "tool_cell", "artifact": gone,
"locator": {"row": 0, "column": "phone"}, "status": "verified"}])
chain = await hand_chain([metric], queries=[gone])
[step] = [s for s in chain if s["step"] == "input"]
assert step["value"] == "已遮罩" and "masked" not in step
assert step["mask_unverified"] == "查询快照无法读取,核对不了这一格是否取自设置了遮罩的列"
assert chain[0]["substituted"] == "len(已遮罩)" and said(chain) == []
PARSED = {"columns": ["id", "phone"], "rows": [[1, "139-0000-0004"]]}
POSITIONAL = "vars.res.rows[0][1]"
@pytest.mark.parametrize("parsed, why", [
# 写变量的节点的产出读不出来:取值链走不通,那一格取自哪一列核对不了
(None, "查询结果无法读取,核对不了这一格是否取自设置了遮罩的列"),
# 结果没存快照、只带着数据源名:查询当时记下的遮罩在快照里,核对不了
({**PARSED, "source": GHOST}, "查询结果没有保存快照,核对不了这一格是否取自设置了遮罩的列"),
])
async def test_a_positional_cell_that_cannot_be_traced_is_withheld(parsed, why):
from app.core.artifact_store import put_json
out = await put_json(parsed, kind="node_output") if parsed is not None else "ee" * 32
metric = card_metric("plen2", f"len({POSITIONAL}) * 2", "len('139-0000-0004') * 2", [
{"path": POSITIONAL, "value": "139-0000-0004", "node_id": "parse", "via": "transform", "field": "rows[0][1]",
"status": "ok"}], value=26)
chain = await hand_chain([metric], outputs={"parse": out})
[step] = [s for s in chain if s["step"] == "input"]
assert step["value"] == "已遮罩" and step["mask_unverified"] == why and "masked" not in step
assert chain[0]["substituted"] == "len(已遮罩) * 2" and said(chain) == []
async def test_a_reshaped_result_without_a_snapshot_or_a_source_is_shown_as_is():
"""数据整形改过形状的结果(既没带快照的 id,也没带数据源名)认不出出处:照常显示。已知的缺口,写在交付说明里。"""
from app.core.artifact_store import put_json
out = await put_json(PARSED, kind="node_output")
metric = card_metric("plen2", f"len({POSITIONAL}) * 2", "len('139-0000-0004') * 2", [
{"path": POSITIONAL, "value": "139-0000-0004", "node_id": "parse", "via": "transform", "field": "rows[0][1]",
"status": "ok"}], value=26)
chain = await hand_chain([metric], outputs={"parse": out})
[step] = [s for s in chain if s["step"] == "input"]
assert step["value"] == "139-0000-0004" and "mask_unverified" not in step and "masked" not in step
@pytest.mark.parametrize("parsed, withheld", [
(None, True),
({**PARSED, "source": GHOST}, True),
(PARSED, False),
])
def test_the_whatif_withholds_the_same_untraceable_cells(parsed, withheld):
"""推演同一个认法:取值链走不通、结果没存快照的那一格不给值、不推演;认不出出处的照常推演。"""
from app.engine import recompute
store = {"out": parsed}
metric = card_metric("plen2", f"len({POSITIONAL}) * 2", "len('139-0000-0004') * 2", [
{"path": POSITIONAL, "value": "139-0000-0004", "node_id": "parse", "via": "transform", "field": "rows[0][1]",
"status": "ok"}], value=26)
sources = recompute.Sources(load=store.get, outputs={"parse": "out"} if parsed is not None else {})
[result] = recompute.whatif({"metrics": [metric]}, recompute.Changes(), sources)["metrics"]
[inp] = result["inputs"]
if withheld:
assert inp["old"] is None and inp.get("withheld") is True and result["status"] == "unsupported"
assert result["substituted"] is None and "139-0000-0004" not in json.dumps(result, ensure_ascii=False)
else:
assert inp["old"] == "139-0000-0004" and result["status"] == "unchanged"
async def test_the_writer_output_is_read_once_for_all_cells_taken_by_position(monkeypatch):
"""一个指标按位置取了好几格:写变量的节点的产出(可能是几 MB 的查询结果)一次请求里只读一次,快照也只读一次。"""
from app.api import evidence
from app.core.artifact_store import put_json
snap = await put_json({**PHONE_SNAP, "rows": [[1, "139-0000-0009"], [2, "139-0000-0008"]]}, kind="query")
out = await put_json({"columns": ["id", "phone"], "rows": [[1, "139-0000-0009"], [2, "139-0000-0008"]],
"artifact": snap, "source": GHOST}, kind="node_output")
paths = ["vars.res.rows[0][1]", "vars.res.rows[1][1]", "vars.res.rows[0][0]"]
metric = card_metric("many", " + ".join(f"len(str({p}))" for p in paths), "len('139-0000-0009') + 0", [
{"path": p, "value": v, "node_id": "parse", "via": "transform", "field": p[9:], "status": "ok"}
for p, v in zip(paths, ["139-0000-0009", "139-0000-0008", 1])], value=27)
reads: list[str] = []
real = evidence.load
monkeypatch.setattr(evidence, "load", lambda artifact: (reads.append(artifact), real(artifact))[1])
chain = await hand_chain([metric], outputs={"parse": out})
inputs = {s["path"]: s for s in chain if s["step"] == "input"}
assert inputs[paths[0]]["masked"] == ["phone"] and inputs[paths[1]]["masked"] == ["phone"]
assert inputs[paths[2]]["value"] == 1 and "masked" not in inputs[paths[2]]
assert reads.count(out) == 1 and reads.count(snap) == 1, reads
@@ -0,0 +1,504 @@
"""证据接口的核查状态(期 4 I2a):口径卡指标那一步带双盲复算的结果,句子带反方审稿的判断。
- 指标步骤 dual_check:{status, node_id, choice?, blind_rendered?, reason?}。status 是一致(agreed)、不一致(disagreed,
带审核人的选择,没选的不带)、无法复算(unchecked,带原因)、跳过(skipped)。跳过的原因按这个指标自己说:关闭了
(disabled);不在节点配置的「复算的指标」里、或值不是数(not_targeted);这个指标没有来自查询的输入(no_inputs)——
节点直通时产出里的 no_inputs 说的是整张卡,卡上确有来自查询的指标、只是没被选上的不能也说成没有。读双盲复算节点封存的
产出和它的台账条目,按台账里记的指标集工件认它复算的是口径卡的哪一次结果;直通的没有台账条目,按画布上它复算的卡、
和它跑完之前那张卡最后一次的结果认
- 句子 unit.challenge:{verdict, node_id, artifact, on_refuted, report_label, counts, unreviewed?}。读反方审稿节点的产出
和审稿记录,只认审的就是这一份报告文档的;记录没存上时按画布上审的报告、和它跑完之前那份报告最后一次自查的文档认,
判断取产出里列的那几句。counts 是整份报告的审稿计数(节点产出里的那一份),证据面板借审批卡的展示画这一句时,概况那
一行不能凭这一句拼。节点设为「报错并停止运行」、句子被推翻、运行因此失败时,按审完那一刻的日志认
- 没有双盲复算、反方审稿的运行,答复里没有这两个键(老运行和以前一字不差)
双盲复算、反方审稿各真跑一遍图(虚构的 SQLite 库,模型是 MockChatModel 剧本);认「哪一次」的边界情形用手写的封存事件。
"""
from __future__ import annotations
import asyncio
import json
import sqlite3
import uuid
import pytest
from httpx import ASGITransport, AsyncClient
from langchain_core.messages import AIMessage, ToolMessage
from sqlalchemy import delete, select
from app.api import evidence
from app.core import artifact_store
from app.core.artifact_store import put_json
from app.data import introspect
from app.data.engine import engines
from app.db.base import SessionLocal
from app.db.models import Approval, DataSource, Run, Setting
from app.engine import spend, verify_agent
from app.engine.evidence import iter_segments, iter_units
from app.engine.judge import SPEND_KEY as JUDGE_SPEND_KEY
from app.engine.nodes import challenge as challenge_mod
from app.engine.runner import run_manager
from app.main import app
from app.providers.mock_model import MockChatModel
from tests.fixtures.dual_check_shop import DEFINITION, RIGHT, WITH_REFUNDS, Blind, add_source, build
from tests.fixtures.sources import drop_source
@pytest.fixture(autouse=True)
async def engine_up():
await run_manager.setup()
async def clear() -> None:
async with SessionLocal() as session:
await session.execute(delete(Setting).where(Setting.key.in_(
["verify", "judge", spend.SPEND_KEY, JUDGE_SPEND_KEY])))
await session.commit()
await clear()
yield
await clear()
await run_manager.shutdown()
@pytest.fixture
async def client():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
yield c
def node(nid, ntype, label=None, **config):
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": label or nid, "config": config}}
def chain(*nodes):
return {"nodes": list(nodes), "edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}
async def wait(run_id: str, statuses=("succeeded", "failed", "interrupted")) -> Run:
for _ in range(600):
await asyncio.sleep(0.05)
async with SessionLocal() as session:
row = await session.get(Run, run_id)
if row.status in statuses and (row.status == "interrupted" or row.manifest_seq is not None):
await run_manager.wait_idle(run_id)
return row
raise AssertionError(f"运行 {run_id} 停在 {row.status}:{row.error}")
async def start(spec: dict, **kwargs) -> Run:
return await wait((await run_manager.start(graph=spec, input_payload={}, **kwargs)).id)
async def report_of(client, run_id: str) -> dict:
body = (await client.get(f"/api/runs/{run_id}/evidence")).json()
[report] = body["reports"]
return artifact_store.load(report["doc_artifact"])
async def open_segment(client, run_id: str, seg_id: str) -> dict:
res = await client.get(f"/api/runs/{run_id}/evidence/segments/{seg_id}")
assert res.status_code == 200, res.text
return res.json()
async def segment(client, run_id: str, text: str) -> dict:
doc = await report_of(client, run_id)
return await open_segment(client, run_id, next(s for s in iter_segments(doc) if s["text"] == text)["id"])
async def sentence(client, run_id: str, text: str | None = None, *, kind: str | None = None) -> dict:
"""点开一整句(句末徽标)时证据面板请求的那个片段:这一句里第一个不是结构性的片段。按原文认句子,或者按句子的
种类认(标题)。"""
doc = await report_of(client, run_id)
md = doc["markdown"]
unit = next(u for _, u in iter_units(doc)
if (kind is not None and u.get("kind") == kind)
or (text is not None and md[u["span"][0]:u["span"][1]].strip() == text))
first = next(s for s in unit["segments"] if s["kind"] != "structural")
return await open_segment(client, run_id, first["id"])
# --------------------------------------------------------------------------
# 双盲复算:真跑
# --------------------------------------------------------------------------
GMV = {"id": "gmv", "name": "销售额", "unit": "元", "decimals": 1, "format": "thousands",
"expression": "cell(nodes.fetch, 0, 'gmv')"}
#: 不在复算范围里的指标:没有来自查询的输入
DAYS = {"id": "days", "name": "统计天数", "unit": "天", "format": "thousands", "expression": "7"}
WRITER = "本周销售额 [[m:gmv]],统计 [[m:days]]。"
@pytest.fixture
async def shop(tmp_path):
name, source_id = await add_source(build(tmp_path / "shop.db"))
yield name
await drop_source(source_id)
def dual_graph(source: str, *, dual: bool = True, **config) -> dict:
"""取数 → 口径卡 →(双盲复算)→ 报告 → 成果。"""
return chain(
node("start", "input"),
node("fetch", "tool", tool=f"db_query__{source}", args={"sql": RIGHT}),
node("card", "metrics", caliber="销售口径", caliber_version="v1", metrics=[GMV, DAYS]),
*([node("dual", "dual_check", "复核销售额", **{"definitions": {"gmv": DEFINITION}, **config})] if dual else []),
node("write", "report", "写周报", instructions="写一句周报", on_violation="flag"),
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}]),
)
async def decide(client, run_id: str, choices: dict) -> Run:
async with SessionLocal() as session:
approval = (await session.execute(select(Approval).where(
Approval.run_id == run_id, Approval.status == "pending"))).scalar_one()
res = await client.post(f"/api/approvals/{approval.id}/decide",
json={"approved": True, "value": {"choices": choices, "note": ""}})
assert res.status_code == 200, res.text
return await wait(run_id, ("succeeded", "failed"))
async def test_an_agreeing_dual_check_shows_on_the_metric_step(client, monkeypatch, shop):
Blind(monkeypatch, shop, {"销售额": (RIGHT, "gmv")}, writer=WRITER)
run = await start(dual_graph(shop))
assert run.status == "succeeded", run.error
step = (await segment(client, run.id, "330.0元"))["chain"][0]
assert step["step"] == "metric" and step["metric"] == "gmv"
assert step["dual_check"] == {"status": "agreed", "node_id": "dual", "blind_rendered": "330.0元"}
# 节点跑了、这个指标没复算(它没有来自查询的输入):照实说没复算、为什么,不是缺省
days = (await segment(client, run.id, "7天"))["chain"][0]
assert days["dual_check"] == {"status": "skipped", "node_id": "dual", "reason": "no_inputs"}
async def test_a_metric_left_out_of_the_subset_is_not_said_to_lack_query_inputs(client, monkeypatch, shop):
"""「复算的指标」只选了统计天数:它没有来自查询的输入,节点以 no_inputs 直通。销售额明明取自查询,只是没被选上——
它的说法是不在复算范围内,不能套用节点那句「没有来自查询的输入」。"""
Blind(monkeypatch, shop, {}, writer=WRITER)
run = await start(dual_graph(shop, metrics=["days"]))
assert run.status == "succeeded", run.error
gmv = (await segment(client, run.id, "330.0元"))["chain"][0]["dual_check"]
assert gmv == {"status": "skipped", "node_id": "dual", "reason": "not_targeted"}
days = (await segment(client, run.id, "7天"))["chain"][0]["dual_check"]
assert days == {"status": "skipped", "node_id": "dual", "reason": "no_inputs"}
@pytest.mark.parametrize("choice", ["b", None])
async def test_a_disagreement_shows_what_the_reviewer_chose(client, monkeypatch, shop, choice):
Blind(monkeypatch, shop, {"销售额": (WITH_REFUNDS, "gmv")}, writer=WRITER)
run = await start(dual_graph(shop))
assert run.status == "interrupted", run.error
run = await decide(client, run.id, {"gmv": choice} if choice else {})
assert run.status == "succeeded", run.error
check = (await segment(client, run.id, "330.0元"))["chain"][0]["dual_check"]
assert check == {"status": "disagreed", "node_id": "dual", "blind_rendered": "380.0元",
**({"choice": choice} if choice else {})}
async def test_a_dual_check_turned_off_is_skipped(client, monkeypatch, shop):
Blind(monkeypatch, shop, {}, writer=WRITER)
run = await start(dual_graph(shop, enabled=False))
assert run.status == "succeeded", run.error
for text in ("330.0元", "7天"):
step = (await segment(client, run.id, text))["chain"][0]
assert step["dual_check"] == {"status": "skipped", "node_id": "dual", "reason": "disabled"}, text
async def test_a_metric_the_blind_agent_could_not_compute_is_unchecked(client, monkeypatch, shop):
Blind(monkeypatch, shop, {}, writer=WRITER)
run = await start(dual_graph(shop))
assert run.status == "succeeded", run.error
check = (await segment(client, run.id, "330.0元"))["chain"][0]["dual_check"]
assert check["status"] == "unchecked" and check["node_id"] == "dual" and check["reason"]
assert "choice" not in check and "blind_rendered" not in check
async def test_runs_without_verification_nodes_answer_as_before(client, monkeypatch, shop):
Blind(monkeypatch, shop, {}, writer=WRITER)
run = await start(dual_graph(shop, dual=False))
assert run.status == "succeeded", run.error
body = await segment(client, run.id, "330.0元")
assert "dual_check" not in body["chain"][0] and "challenge" not in body["unit"]
# --------------------------------------------------------------------------
# 反方审稿:真跑
# --------------------------------------------------------------------------
PRICED = "claude-sonnet-5"
#: 各区域销售额(含退款):华东 1500、华南 900、华北 400
QUERY = "SELECT region AS 区域, SUM(amount) AS 销售额 FROM sales GROUP BY region ORDER BY 销售额 DESC"
NET = "SELECT region AS 区域, SUM(amount) AS 净销售额 FROM sales WHERE refunded = 0 GROUP BY region ORDER BY 净销售额 DESC"
REFUNDS = "SELECT COUNT(*) AS 退款笔数 FROM sales WHERE refunded = 1"
#: 一句有依据的结论、一句预埋的错误结论(华南其实低于华东),前面一个标题
TEXT = "## 区域周报\n\n华东销售额 [[v:Q1.r0.销售额]],居各区域之首。[[see:Q1]]华南销售额超过华东。[[see:Q1]]"
EAST, SOUTH = "华东销售额 1,500,居各区域之首。", "华南销售额超过华东。"
PLAYS = {
"华南销售额超过华东": ([NET], "refuted", "扣除退款后华南为 [[v:Q1.r1.净销售额]],仍低于华东的 [[v:Q1.r0.净销售额]]。[[see:Q1]]"),
"居各区域之首": ([REFUNDS], "weakened", "华东含 [[v:Q1.r0.退款笔数]] 笔退款,扣除后仍居首,但销售额被高估。"),
}
@pytest.fixture
async def sales(tmp_path):
path = tmp_path / f"{uuid.uuid4().hex[:8]}.db"
db = sqlite3.connect(path)
db.executescript(
"CREATE TABLE sales (id INTEGER PRIMARY KEY, region TEXT, amount INTEGER, refunded INTEGER);"
"INSERT INTO sales VALUES (1, '华东', 1200, 0), (2, '华东', 300, 1), (3, '华南', 900, 0), (4, '华北', 400, 0);")
db.commit()
db.close()
name = f"sales_{uuid.uuid4().hex[:8]}"
async with SessionLocal() as session:
row = DataSource(name=name, kind="sqlite", database=str(path), readonly=True, enabled=True, options={})
session.add(row)
await session.flush()
row.schema_cache = await introspect.introspect(row)
await session.commit()
source_id = row.id
yield name
await engines.invalidate(source_id)
def reviewer(monkeypatch, source: str) -> None:
"""写作者交 TEXT;反方 agent 按任务里的结论认出是哪一句,照 PLAYS 查询、交结论和理由。"""
def decide(model, messages):
if challenge_mod.CHALLENGE_SYSTEM not in str(messages[0].content):
return AIMessage(content=TEXT)
task = str(messages[1].content) if len(messages) > 1 else ""
queries, verdict, rationale = next((p for k, p in PLAYS.items() if k in task), ([], "stands", "没有找到反例。"))
if model.response_format:
return AIMessage(content=json.dumps({"verdict": verdict, "rationale": rationale}, ensure_ascii=False))
done = sum(1 for m in messages if isinstance(m, ToolMessage))
if done < len(queries):
call = {"name": f"db_query__{source}", "args": {"sql": queries[done]}, "id": f"q{done}-{uuid.uuid4().hex[:6]}"}
return AIMessage(content="查一下。", tool_calls=[call])
return AIMessage(content="查完了。")
monkeypatch.setattr(MockChatModel, "_decide", decide)
async def verify_model(session, spec):
return MockChatModel(model_name=PRICED, latency=0.0), PRICED
monkeypatch.setattr(verify_agent, "get_chat_model", verify_model)
def challenge_graph(source: str, *, challenge: bool = True) -> dict:
return chain(
node("start", "input"),
node("fetch", "tool", tool=f"db_query__{source}", args={"sql": QUERY}),
node("write", "report", "写周报", instructions="写周报"),
*([node("chal", "challenge", "审稿", report="write")] if challenge else []),
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}],
contract={"report_from": "write"}),
)
async def challenge_output(run_id: str) -> dict:
from app.db.models import RunEvent
async with SessionLocal() as session:
[finished] = (await session.execute(select(RunEvent).where(
RunEvent.run_id == run_id, RunEvent.type == "node.finished", RunEvent.node_id == "chal"))).scalars()
return artifact_store.load(finished.data["artifact"])
async def test_a_challenged_sentence_carries_the_verdict(client, monkeypatch, sales):
reviewer(monkeypatch, sales)
run = await start(challenge_graph(sales), run_class="formal")
assert run.status == "succeeded", run.error
output = await challenge_output(run.id)
record = output["challenge_artifact"]
south = (await sentence(client, run.id, SOUTH))["unit"]["challenge"]
# counts 是整份报告的审稿计数(节点产出里的那一份):一句被推翻、一句被削弱,不是只数这一句
assert south == {"verdict": "refuted", "node_id": "chal", "artifact": record, "on_refuted": "degrade",
"report_label": "写周报", "counts": {"stands": 0, "weakened": 1, "refuted": 1, "inconclusive": 0}}
assert south["counts"] == output["counts"]
# 点开句子里的数字,片段接口答的这一句同样带着判断
east = (await segment(client, run.id, "1,500"))["unit"]["challenge"]
assert east["verdict"] == "weakened" and east["artifact"] == record and east["counts"] == output["counts"]
# 反方没审的(标题不是结论):没有这个键
heading = await sentence(client, run.id, kind="heading")
assert heading["unit"]["kind"] == "heading" and "challenge" not in heading["unit"]
async def test_a_lost_review_record_still_shows_the_listed_verdicts(client, monkeypatch, sales):
"""审稿记录没存上(challenge_artifact 为空):判断取产出里列的那几句,工件为空(理由和查询看不了)。"""
async def lost(self, record):
return None
monkeypatch.setattr(challenge_mod._Review, "_store", lost)
reviewer(monkeypatch, sales)
run = await start(challenge_graph(sales), run_class="formal")
assert run.status == "succeeded", run.error
assert (await challenge_output(run.id))["challenge_artifact"] is None
south = (await sentence(client, run.id, SOUTH))["unit"]["challenge"]
assert south == {"verdict": "refuted", "node_id": "chal", "artifact": None, "on_refuted": "degrade",
"report_label": "写周报", "counts": {"stands": 0, "weakened": 1, "refuted": 1, "inconclusive": 0}}
async def test_a_review_that_stopped_the_run_still_marks_its_sentences(client, monkeypatch, sales):
"""「结论被推翻时」设为报错并停止运行:节点报错、没有 node.finished,运行失败。被推翻的是哪几句、反方凭什么,
证据面板里照样要看得到——按审完那一刻的日志(challenge_done)认审稿记录,处置写 fail。"""
reviewer(monkeypatch, sales)
graph = challenge_graph(sales)
next(n for n in graph["nodes"] if n["id"] == "chal")["data"]["config"]["on_refuted"] = "fail"
run = await start(graph, run_class="formal")
assert run.status == "failed" and "推翻" in (run.error or ""), run.error
south = (await sentence(client, run.id, SOUTH))["unit"]["challenge"]
assert south["verdict"] == "refuted" and south["on_refuted"] == "fail" and south["node_id"] == "chal"
assert south["artifact"] and south["counts"] == {"stands": 0, "weakened": 1, "refuted": 1, "inconclusive": 0}
east = (await segment(client, run.id, "1,500"))["unit"]["challenge"]
assert east["verdict"] == "weakened" and east["artifact"] == south["artifact"]
async def test_reports_without_a_challenge_have_no_verdicts(client, monkeypatch, sales):
reviewer(monkeypatch, sales)
run = await start(challenge_graph(sales, challenge=False), run_class="formal")
assert run.status == "succeeded", run.error
assert "challenge" not in (await sentence(client, run.id, SOUTH))["unit"]
# --------------------------------------------------------------------------
# 认「哪一次」:手写的封存事件
# --------------------------------------------------------------------------
def sealed_of(graph: dict, events: list[tuple]) -> evidence._Sealed:
return evidence._Sealed(run_id="syn-run", graph=graph, events=events,
seal={"sealed": True, "ok": True, "manifest_seq": events[-1][0], "legacy": False})
def card_set(artifact: str) -> dict:
return {"evidence": [{"kind": "metric_set", "node_id": "card", "artifact": artifact}]}
async def test_the_dual_check_belongs_to_the_card_result_it_checked():
"""口径卡在循环里算了两次:双盲复算只复算了第一次的结果,第二次的指标没有复算状态;之后又跑了一次直通的,第二次的
结果记成跳过,第一次的不受影响。"""
first = await put_json({"kind": "metric_set", "metrics": [{"id": "gmv", "value": 1.5}], "n": 1}, kind="metric_set")
second = await put_json({"kind": "metric_set", "metrics": [{"id": "gmv", "value": 2.5}], "n": 2}, kind="metric_set")
checked = await put_json({"status": "agreed", "counts": {"agreed": 1, "disagreed": 0, "unchecked": 0},
"metrics": [{"metric_id": "gmv", "name": "销售额", "unit": "元", "status": "agreed",
"original_rendered": "1.5元", "blind_rendered": "1.5元", "delta_pct": 0.0}],
"model": "mock", "same_model": False}, kind="node_output")
skipped = await put_json({"status": "skipped", "reason": "disabled"}, kind="node_output")
graph = chain(node("card", "metrics"), node("dual", "dual_check", card="card"))
events = [
(1, "node.started", "card", {}),
(2, "node.finished", "card", card_set(first)),
(3, "node.started", "dual", {}),
(4, "node.finished", "dual", {"artifact": checked, "evidence": [{
"kind": "dual_check", "node_id": "dual", "exec": 1, "card": "card", "status": "agreed",
"counts": {"agreed": 1, "disagreed": 0, "unchecked": 0}, "metrics": [{"id": "gmv", "status": "agreed"}],
"metric_set": first}]}),
(5, "node.started", "card", {}),
(6, "node.finished", "card", card_set(second)),
]
agreed = {"status": "agreed", "node_id": "dual", "blind_rendered": "1.5元"}
assert evidence._dual_check_of(sealed_of(graph, events), "card", first, "gmv") == agreed
assert evidence._dual_check_of(sealed_of(graph, events), "card", second, "gmv") is None
later = [*events, (7, "node.started", "dual", {}), (8, "node.finished", "dual", {"artifact": skipped})]
assert evidence._dual_check_of(sealed_of(graph, later), "card", second, "gmv") == {
"status": "skipped", "node_id": "dual", "reason": "disabled"}
assert evidence._dual_check_of(sealed_of(graph, later), "card", first, "gmv") == agreed
# 别的口径卡、卡上没有的指标:没有
assert evidence._dual_check_of(sealed_of(graph, events), "other", first, "gmv") is None
assert evidence._dual_check_of(sealed_of(graph, events), "card", first, "ghost") == {
"status": "skipped", "node_id": "dual", "reason": "not_targeted"}
async def test_an_unreadable_dual_check_output_falls_back_to_its_ledger_entry():
"""产出的文件不在了:状态和审核人的选择按台账条目说,盲算值说不了就不给。"""
card = await put_json({"kind": "metric_set", "metrics": [{"id": "gmv", "value": 3.5}], "n": 3}, kind="metric_set")
graph = chain(node("card", "metrics"), node("dual", "dual_check", card="card"))
events = [
(1, "node.finished", "card", card_set(card)),
(2, "node.finished", "dual", {"artifact": "f0" * 32, "evidence": [{
"kind": "dual_check", "node_id": "dual", "exec": 1, "card": "card", "status": "resolved",
"metrics": [{"id": "gmv", "status": "disagreed", "choice": "a"}], "metric_set": card}]}),
]
assert evidence._dual_check_of(sealed_of(graph, events), "card", card, "gmv") == {
"status": "disagreed", "node_id": "dual", "choice": "a"}
assert evidence._dual_check_of(sealed_of(graph, events), "card", card, "other") == {
"status": "skipped", "node_id": "dual", "reason": "not_targeted"}
def report(doc_artifact: str) -> evidence._Report:
return evidence._Report(node_id="write", doc_artifact=doc_artifact, ok=True, repairs=0, stats=None, doc=None,
hash_ok=True, fields=[])
@pytest.mark.parametrize("saved", [True, False])
async def test_a_review_of_an_earlier_draft_does_not_mark_the_rewritten_report(saved):
"""报告在反方审稿之后又重写了一次(循环、接着跑):审的是前一稿,新的一稿上同一个句子编号说的可能是另一句话。
审稿记录存上了按记录里的文档认,没存上按它跑完之前那份报告最后一次自查的文档认。"""
draft = await put_json({"markdown": "第一稿", "n": 1}, kind="report_doc")
final = await put_json({"markdown": "第二稿", "n": 2}, kind="report_doc")
claims = [{"unit": "u1", "text": "华南销售额超过华东。", "verdict": "refuted"}]
record = await put_json({"report": "write", "doc_artifact": draft, "model": "mock",
"claims": [{**c, "rationale": "", "rationale_doc": None, "queries": []} for c in claims]},
kind="challenge") if saved else None
out = await put_json({"challenge_artifact": record, "counts": {"stands": 0, "weakened": 0, "refuted": 1,
"inconclusive": 0},
"items": claims, "on_refuted": "flag"}, kind="node_output")
graph = chain(node("write", "report", "写周报"), node("chal", "challenge", "审稿", report="write"))
events = [
(1, "report.checked", "write", {"doc_artifact": draft, "ok": True}),
(2, "node.finished", "write", {}),
(3, "node.finished", "chal", {"artifact": out}),
(4, "report.checked", "write", {"doc_artifact": final, "ok": True}),
]
sealed = sealed_of(graph, events)
assert evidence._challenge_of(sealed, report(draft), "u1") == {
"verdict": "refuted", "node_id": "chal", "artifact": record, "on_refuted": "flag", "report_label": "写周报",
"counts": {"stands": 0, "weakened": 0, "refuted": 1, "inconclusive": 0}}
assert evidence._challenge_of(sealed, report(final), "u1") is None
assert evidence._challenge_of(sealed, report(draft), "u2") is None
async def test_the_counts_are_the_whole_reviews_and_unreviewed_rides_along():
"""counts 照产出(整份报告审了 12 句),没能反查的句数产出里有就带上;产出里的计数认不出来时按审稿记录数。"""
doc = await put_json({"markdown": "周报", "n": 3}, kind="report_doc")
claims = [{"unit": "u1", "text": "华南销售额超过华东。", "verdict": "weakened"},
{"unit": "u2", "text": "华北持平。", "verdict": "stands"}]
record = await put_json({"report": "write", "doc_artifact": doc, "model": "mock",
"claims": [{**c, "rationale": "", "rationale_doc": None, "queries": []} for c in claims]},
kind="challenge")
graph = chain(node("write", "report", "写周报"), node("chal", "challenge", "审稿", report="write"))
async def answer(output: dict) -> dict:
out = await put_json({"challenge_artifact": record, "items": claims, "on_refuted": "degrade", **output},
kind="node_output")
events = [(1, "report.checked", "write", {"doc_artifact": doc, "ok": True}),
(2, "node.finished", "chal", {"artifact": out})]
return evidence._challenge_of(sealed_of(graph, events), report(doc), "u1")
got = await answer({"counts": {"stands": 7, "weakened": 3, "refuted": 2, "inconclusive": 0}, "unreviewed": 1})
assert got["counts"] == {"stands": 7, "weakened": 3, "refuted": 2, "inconclusive": 0} and got["unreviewed"] == 1
# 计数格式不对(一个数都认不出来):按审稿记录里的句子数,不写四个 0
got = await answer({"counts": {"stands": "7"}, "unreviewed": True})
assert got["counts"] == {"stands": 1, "weakened": 1, "refuted": 0, "inconclusive": 0} and "unreviewed" not in got
async def test_a_review_that_failed_the_node_needs_its_record():
"""节点报错:认审完那一刻的日志。重跑(node.started)之后没再审完的不算;日志里没有审稿记录(保存失败)认不出审的
是哪一稿,也不算。"""
doc = await put_json({"markdown": "周报", "n": 4}, kind="report_doc")
claims = [{"unit": "u1", "text": "华南销售额超过华东。", "verdict": "refuted", "rationale": "", "rationale_doc": None,
"queries": []}]
record = await put_json({"report": "write", "doc_artifact": doc, "model": "mock", "claims": claims},
kind="challenge")
graph = chain(node("write", "report", "写周报"), node("chal", "challenge", "审稿", report="write",
on_refuted="fail"))
counts = {"stands": 0, "weakened": 0, "refuted": 1, "inconclusive": 0}
head = [(1, "report.checked", "write", {"doc_artifact": doc, "ok": True}), (2, "node.started", "chal", {})]
done = (3, "log", "chal", {"code": "challenge_done", "counts": counts, "artifact": record})
failed = (4, "node.failed", "chal", {"error": "反方审稿推翻了 1 句结论"})
got = evidence._challenge_of(sealed_of(graph, [*head, done, failed]), report(doc), "u1")
assert got == {"verdict": "refuted", "node_id": "chal", "artifact": record, "on_refuted": "fail",
"report_label": "写周报", "counts": counts}
retried = [*head, done, (4, "node.started", "chal", {}), (5, "node.failed", "chal", {"error": "模型不可用"})]
assert evidence._challenge_of(sealed_of(graph, retried), report(doc), "u1") is None
lost = [*head, (3, "log", "chal", {"code": "challenge_done", "counts": counts, "artifact": None}), failed]
assert evidence._challenge_of(sealed_of(graph, lost), report(doc), "u1") is None
@@ -0,0 +1,113 @@
"""按位置取自遮罩列的一格(vars.res.rows[1][2]、nodes.fetch.rows[0][0])在推演之外的两处:期 4 I2a 把它接进了
recompute 的还原(_positional),用 recompute 的地方都跟着变,这里把变化钉住。
- 双盲复算的预检(dual_check._plan_metric 什么都不改先重算一遍):以前这种指标照常盲算,遮着的那一格还会作为字面量
写进给盲算模型的计算式;现在预检判它用到了遮罩列,按无法复算记,原因不说「推演」(这里不是推演)
- 运行对比(engine/compare.py):这种输入两边的值都不给,标 masked,和 cell() 取的遮罩格一样
库和数字都是编的:号码写成带横线的文本,区域名是「东区」这类。
"""
from __future__ import annotations
import json
from app.engine import compare
from app.engine.runner import run_manager
from tests.fixtures.compare_runs import BASE_ROWS, NEXT_ROWS, metric, scalar_in, week
from tests.test_evidence_metric_masks import ( # noqa: F401 - 夹具按名字注入(engine_up 自动生效)
METRICS,
PHONES,
SQL,
client,
engine_up,
node,
set_source,
shop,
)
def dual_graph():
"""取数 → 数据整形(vars.res)→ 口径卡 → 双盲复算(只复算 mix)→ 成果。"""
nodes = [
node("start", "input"),
node("fetch", "tool", tool="db_query__shop", args={"sql": SQL}),
node("parse", "transform", mode="json", template="{{ nodes.fetch }}", assign_to="res"),
node("card", "metrics", caliber="遮罩口径", caliber_version="v1", metrics=METRICS),
node("dual", "dual_check", metrics=["mix"]),
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.card.text }}"}]),
]
return {"nodes": nodes, "edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}
async def dual_output(run_id: str) -> dict:
from sqlalchemy import select
from app.core import artifact_store
from app.db.base import SessionLocal
from app.db.models import RunEvent
async with SessionLocal() as session:
[finished] = (await session.execute(select(RunEvent).where(
RunEvent.run_id == run_id, RunEvent.type == "node.finished", RunEvent.node_id == "dual"))).scalars()
return artifact_store.load(finished.data["artifact"])
async def test_the_dual_check_does_not_blind_compute_a_metric_reading_a_masked_cell_by_position(shop):
"""mix = round(cell(nodes.fetch, 1, 'amount') + len(vars.res.rows[1][2]), 1):cell() 那一格不在遮罩列上,按位置取的
号码在。预检判它用到了遮罩列,按无法复算记缺口,原因不说「推演」;没遮罩时预检照常通过、指标进入盲算。"""
import asyncio
from app.db.base import SessionLocal
from app.db.models import Run
async def run(options):
await set_source(shop, options)
run_id = (await run_manager.start(graph=dual_graph(), input_payload={})).id
for _ in range(400):
await asyncio.sleep(0.05)
async with SessionLocal() as session:
row = await session.get(Run, run_id)
if row.status in ("succeeded", "failed") and row.manifest_seq is not None:
await run_manager.wait_idle(run_id)
assert row.status == "succeeded", row.error
return await dual_output(run_id)
raise AssertionError(f"运行 {run_id} 没跑完:{row.status}")
masked = await run({"mask_columns": ["phone"]})
[mix] = masked["metrics"]
assert mix["metric_id"] == "mix" and mix["status"] == "unchecked"
assert mix["reason"] == "无法按原表达式重算这个指标(这个指标用到了设置了遮罩的列)"
assert "推演" not in json.dumps(masked, ensure_ascii=False)
assert not [p for p in PHONES if p in json.dumps(masked, ensure_ascii=False)]
assert any("遮罩" in gap for gap in masked["issuance_gaps"])
# 没遮罩:预检通过,原因不再是遮罩(盲算模型没配,按盲算没交回数记,但已经走到了盲算这一步)
plain = await run({})
[mix] = plain["metrics"]
assert "遮罩" not in (mix.get("reason") or "") and "无法按原表达式重算" not in (mix.get("reason") or "")
def region_len(fetch, bot, rows, bot_rows):
"""东区名称的字数:按位置取自取数结果的第 0 行第 0 列(region)。"""
return [metric("rlen", "len(nodes.fetch.rows[0][0])", len(rows[0][0]),
[scalar_in("nodes.fetch.rows[0][0]", rows[0][0], "fetch", "tool")], name="首个区域名字数",
unit="字", decimals=0)]
def test_the_compare_page_withholds_a_masked_cell_read_by_position():
"""运行对比:按位置取自遮罩列(region)的一格两边都不给值、标 masked,和 cell() 取的遮罩格一样;没遮罩时照给。"""
def masked(run):
run.masked = {a: frozenset({"region"}) for a in run.queries}
return run
result = compare.compare(masked(week("r1", metrics=region_len)).side(),
masked(week("r2", NEXT_ROWS, metrics=region_len)).side())
[rlen] = [m for m in result["metrics"] if m["metric_id"] == "rlen"]
[inp] = rlen["inputs"]
assert inp["masked"] is True and inp["base"] is None and inp["target"] is None
assert "东区" not in json.dumps(result, ensure_ascii=False)
shown = compare.compare(week("r1", metrics=region_len).side(), week("r2", NEXT_ROWS, metrics=region_len).side())
[rlen] = [m for m in shown["metrics"] if m["metric_id"] == "rlen"]
[inp] = rlen["inputs"]
assert not inp.get("masked") and inp["base"] == BASE_ROWS[0][0] and inp["target"] == NEXT_ROWS[0][0]
+5 -1
View File
@@ -29,6 +29,9 @@ BOT_OUT = "d4" * 32
FETCH_OUT = "e5" * 32
ITEMS_VALUE = "f6" * 32
RES_ROWS = "a7" * 32
#: 数据整形节点 parse 解析好的 S1(数据源工具交回的结果原样解析,带着快照的 id):vars.res 就是它。按位置取的
#: vars.res.rows[0][1] 顺着它认得出取自 S1 的 amount 列;没有这份产出时那一格遮没遮核对不了,推演不给值
PARSE_OUT = "c9" * 32
STORE = {
**SNAPSHOTS,
BOT_OUT: {"text": "四个区域合计 340", "data": {"amounts": [120.5, 80.0, 99.5, 40.0]},
@@ -37,6 +40,7 @@ STORE = {
FETCH_OUT: {"items": ["甲", "乙", "丙"], "k": 2},
ITEMS_VALUE: {"path": "vars.kpi.items", "value": ["甲", "乙", "丙"]},
RES_ROWS: {"path": "vars.res.rows", "value": SNAPSHOTS[S1]["rows"]},
PARSE_OUT: {**SNAPSHOTS[S1], "artifact": S1},
}
@@ -47,7 +51,7 @@ def load(artifact):
def sources(**kw):
base = {"load": load, "queries": (S1, S2, BAD), "outputs": {"bot": BOT_OUT, "fetch": FETCH_OUT},
base = {"load": load, "queries": (S1, S2, BAD), "outputs": {"bot": BOT_OUT, "fetch": FETCH_OUT, "parse": PARSE_OUT},
"masked": {S2: frozenset({"phone"})}}
return Sources(**{**base, **kw})
+101
View File
@@ -0,0 +1,101 @@
"""数字推演的几处补测(期 4 打磨):
- 取整只有一个函数:推演重算(engine/recompute.py)用的就是口径卡(engine/nodes/metrics.py)那一个,口径卡的取整
规则哪天改了,推演跟着变,不会一边改了、另一边还按旧的算
- _doubt 里 moot 的那一支:一个输入既嵌在下标里、又直接参与计算,包着它的引用给了新值之后,它直接参与的那一处照常
拿它的值——排除行后它变没变说不准,指标仍得说不准,不能借着「嵌在里面的那一处已无关紧要」放过去
- 决定取哪一格的单格照给行段:它自己那一行勾得了,勾掉它确定是排除后无法计算
夹具沿用 test_recompute 的虚构快照和卡。只导入夹具,不导入它的测试函数:导进来的 test_* 会在这里再收集一遍。
"""
from __future__ import annotations
import pytest
from app.engine import recompute
from app.engine.nodes import metrics as metrics_node
from app.engine.recompute import Changes, whatif
from tests.test_recompute import AMOUNTS, FEE, REGION, S1, cell, lone, scalar, sources
# --------------------------------------------------------------------------
# 取整:和口径卡共用一个函数
# --------------------------------------------------------------------------
def test_the_whatif_rounds_with_the_cards_own_function():
assert recompute.round_metric is metrics_node.round_metric
@pytest.mark.parametrize("value, decimals, rounded", [
# 二进制里落在半中间的数照 Python 的 round:口径卡当时就是这么算的
(2.675, 2, 2.67),
(0.125, 2, 0.12),
# 负的小数位:取整到十位、百位
(1234.5, -1, 1230.0),
(-1250.0, -2, -1200.0),
# 只取整浮点:整数、文本、空值原样
(7, 2, 7),
("3.14159", 2, "3.14159"),
(None, 2, None),
# 没写小数位、小数位不是整数(老卡上的怪值):不取整
(1.25, None, 1.25),
(1.25, True, 1.25),
(1.25, 1.0, 1.25),
])
def test_round_metric_rounds_only_floats_by_an_integer_number_of_places(value, decimals, rounded):
got = metrics_node.round_metric(value, decimals)
assert got == rounded and type(got) is type(rounded)
# --------------------------------------------------------------------------
# _doubt 里 moot 的那一支
# --------------------------------------------------------------------------
#: 按区域取费率,区域名本身还拿来数字数。区域名是查询交回之后才算出来的(label 节点,对不上快照)
RATE = "vars.kpi.rates[vars.kpi.region]"
TWICE = lone("fee_twice", f"round({RATE} * sum(vars.kpi.amounts), 1) + len(vars.kpi.region)",
"round(0.05 * sum(vars.kpi.amounts), 1) + len('华南')", 19.0,
[scalar(RATE, 0.05), scalar("vars.kpi.region", "华南", node="label"), AMOUNTS], decimals=1)
TIMED = {"returned": {S1: 10}, "finished": {"fetch": 5, "label": 12}}
def test_an_input_used_directly_and_as_an_index_stays_doubted_after_the_item_has_a_new_value():
timed = sources(**TIMED)
assert recompute.recompute(TWICE["metrics"][0], Changes(), timed)["new"] == 19.0
# 费率给了新值:区域名嵌在下标里的那一处已无关紧要。可它还直接参与计算(len),排除行后它变没变说不准
[fee] = whatif(TWICE, Changes(inputs={RATE: 0.1}, exclude={S1: frozenset({1})}), timed)["metrics"]
assert (fee["status"], fee["new"], fee["changed"]) == ("unsupported", None, None), fee
assert "排除行对 vars.kpi.region 的影响无法确定" in fee["reason"]
# 对照:不排除行,费率的新值照常算进去
[fee] = whatif(TWICE, Changes(inputs={RATE: 0.1}), timed)["metrics"]
assert (fee["status"], fee["new"]) == ("ok", round(0.1 * 340.0, 1) + 2), fee
def test_an_index_used_only_inside_the_item_is_still_moot():
"""对照:同一个区域名只嵌在下标里(不直接参与计算)时,费率给了新值就无关紧要,按剩下的行照常算。"""
card = lone("fee_once", f"round({RATE} * sum(vars.kpi.amounts), 1)", "round(0.05 * sum(vars.kpi.amounts), 1)", 17.0,
[scalar(RATE, 0.05), scalar("vars.kpi.region", "华南", node="label"), AMOUNTS], decimals=1)
[fee] = whatif(card, Changes(inputs={RATE: 0.1}, exclude={S1: frozenset({1})}), sources(**TIMED))["metrics"]
assert (fee["status"], fee["new"]) == ("ok", round(0.1 * (340.0 - 80.0), 1)), fee
# --------------------------------------------------------------------------
# 决定取哪一格的单格照给行段
# --------------------------------------------------------------------------
def test_a_cell_that_picks_the_item_still_offers_its_own_row():
"""区域那一格决定取哪一项费率:它不能改(改它要重新取数),可它自己那一行勾得了——勾掉它,取哪一项不知道,
确定是排除后无法计算,不是说不准。"""
card = lone("fee_pick", f"{FEE} * 100", "0.05 * 100", 5.0, [scalar(FEE, 0.05), cell("pull", 1, "region", "华南")],
decimals=1)
timed = sources(returned={S1: 10}, finished={"fetch": 5})
baseline = whatif(card, Changes(), timed)
inputs = {i["path"]: i for i in baseline["metrics"][0]["inputs"]}
assert inputs[REGION]["row_span"] == [1, 1] and inputs[REGION]["editable"] is False
assert inputs[FEE]["editable"] is True and "row_span" not in inputs[FEE]
assert [(u["input"], u["reason"]) for u in baseline["unsupported"]] == [(REGION, recompute.SELECTOR)]
[fee] = whatif(card, Changes(exclude={S1: frozenset({1})}), timed)["metrics"]
assert (fee["status"], fee["changed"]) == ("excluded", True) and "第 2 行" in fee["reason"], fee
[row] = [i for i in fee["inputs"] if i["path"] == REGION]
assert row["excluded_rows"] == [1]