数字推演复核修正(第 1 轮,后端):排除行时说不准的指标不再报「无变化」,快照读不出来时不给值,遮罩按用到的快照在线程里算

- 排除了行,指标却有输入对不上任何快照(老运行没存完整值的容器、加工过的结果、按位置从整份结果里取的一格、由查询结果
  算出来的数):以前一律报「无变化」、报告引用处报「不受影响」,或者拿原值算出一个不对应任何一份数据的数。现在回
  unsupported 并写明是哪个输入说不准;写它的节点在那份快照交回之前就跑完的影响不到它,照常不算(按事件序号比先后)。
  卡上没记全输入的指标,有任何改动都标 unsupported
- 查询快照文件不在、内容与哈希不一致时,落在它上面的 cell()、agent 字段不给值、不能改、不给代入式,用到它的指标推演
  不了;遮罩照样按台账记的数据源算,不再因为快照坏了就失效
- 遮罩改成用到哪份快照才算哪份:读快照放进重算的工作线程,查库交回事件循环。以前每次请求都在事件循环里把这次运行的
  全部快照读一遍、复验一遍哈希
- 输入带 row_span(实际用到的快照行);新增 GET …/evidence/whatif/rows,按页取封存范围内一份快照的行(遮罩列写
  「已遮罩」,只读)。界面据此给整份结果勾行、只让用到的行可勾
- 不能改、也没有行可勾的输入(对不上快照的列表、文本)列进「不支持推演的输入」并写明原因
- 下游口径卡顺着画布的边找,经中间节点用到卡上值的也标;改动碰到了指标、它却推演不了时也标「可能受影响」
- 超大整数不再让接口 500:新值超出可计算范围回 422,算出浮点放不下的整数按「不是有限的数值」说明;非法值的说明不露出
  None、单引号这些 Python 写法
- 补测试并逐个做过变异:封存核对没过、事后加的遮罩、合并查询的遮罩、指标集被改(409)、两份报告、循环里先算卡后算
  产出、手写事件核对取哪次执行和边界、重算的几处保护、勾行接口。取整和口径卡逐个对拍(含二进制半中间的数、负小数位):
  metrics.py 只许加 value_artifact,取整没法抽成共用函数,先用对拍守着
This commit is contained in:
ThearchyHelios
2026-10-05 05:43:17 +08:00
parent ca09aed612
commit 09237a4c99
5 changed files with 989 additions and 72 deletions
+161 -27
View File
@@ -4,6 +4,9 @@ POST /api/runs/{run_id}/evidence/whatif:证据面板里点开口径卡上的
几行,看这个指标和报告里引用它的地方会变成多少。推演永远不是出具:不追加任何事件(last_seq 不变)、不落工件、
不改报告,结果只交回给发请求的人。
GET /api/runs/{run_id}/evidence/whatif/rows:推演时勾行用,按页取封存范围内一份查询快照的行(遮罩列写「已遮罩」)。
证据面板的查询步骤只给被引用的行前后各 2 行,整份结果作输入(len(vars.res.rows) 这种)时每一行都可能要勾。
只认这次运行封存范围内的工件,和证据接口同一套白名单(api/evidence.py 的 _sealed / _sealed_loader):口径卡的指标集
从封存的 node.finished 台账找,排除行只认封存范围内交回过的查询快照,nodes.* 的完整值从封存的节点产出工件取,
vars.* 的完整值(value_artifact)只认封存的指标集里写着的。还在跑、停在审批的运行也能推演,结果里 sealed 为假:
@@ -15,6 +18,7 @@ vars.* 的完整值(value_artifact)只认封存的指标集里写着的。
from __future__ import annotations
import asyncio
from collections.abc import Iterator, Mapping
from dataclasses import replace
from typing import Any
@@ -22,7 +26,10 @@ from fastapi import APIRouter, Body
from app.api.coded import CodedHTTPException
from app.api.evidence import (
MASKED,
MISSING_QUERY,
REPORT_NOT_FOUND,
TAMPERED_QUERY,
_Masks,
_merge_masks,
_recorded_masks,
@@ -50,6 +57,10 @@ BAD_REQUEST_TEXT = ("请求格式有误:card 应为口径卡节点的 ID;rep
MAX_INPUTS = 200
MAX_SNAPSHOTS = 50
MAX_ROWS = 10000
#: 勾行时一页给几行、最多几行
ROWS_PAGE = 50
MAX_ROWS_PAGE = 200
OUT_OF_SCOPE = "这份查询快照不属于这次运行,或不在封存范围内,不能用于推演"
@router.post("/{run_id}/evidence/whatif")
@@ -69,17 +80,18 @@ async def evidence_whatif(run_id: str, payload: Any = Body(None)) -> dict[str, A
if metric is not None and not any(isinstance(m, dict) and str(m.get("id")) == metric
for m in card.get("metrics") or []):
raise CodedHTTPException(404, f"口径卡中没有指标「{metric}」", METRIC_NOT_FOUND)
loader = _loader(sealed, cards)
sources = recompute.Sources(load=loader, queries=tuple(sealed.queries),
outputs=_outputs_before(sealed, started), masked=await _hidden(sealed, loader))
sources = _sources(sealed, cards, started, asyncio.get_running_loop())
docs = [(r.node_id, r.doc) for r in reports
if r.doc is not None and r.hash_ok is True and (report is None or r.node_id == report)]
try:
# 纯计算,连同读工件放进线程:快照大的时候不挡住同一进程里别的请求
# 重算连同读工件(快照、完整值、节点产出)都在线程里:快照大的时候不挡住同一进程里别的请求。
# 遮罩用到哪份快照才算哪份(_Hidden),查库那一步交回事件循环
result, segments = await asyncio.to_thread(_compute, card, changes, sources, metric, docs, card_node, artifact)
except recompute.WhatIfError as e:
raise CodedHTTPException(422, str(e), BAD_INPUT) from None
changed = any(m.get("changed") for m in result["metrics"])
# 改动可能改变了某个指标——算出来变了,或者碰到了它却推演不了(还原不了、遮罩),新值不知道——下游才可能受影响
maybe = any(m.get("changed") is not False for m in result["metrics"])
edges = [(e.get("source"), e.get("target")) for e in sealed.graph.get("edges") or [] if isinstance(e, dict)]
return {
"run_id": run_id,
"card": {"node_id": card_node, "artifact": artifact, "caliber": card.get("caliber") or "",
@@ -91,11 +103,66 @@ async def evidence_whatif(run_id: str, payload: Any = Body(None)) -> dict[str, A
"metrics": result["metrics"],
"segments": segments,
"unsupported": result["unsupported"],
# 下游口径卡只在这次推演确实改变了指标时才标:什么都没变,下游也不会变
"affected_cards": recompute.downstream_cards(cards, card_node) if changed else [],
# 什么都没变,下游也不会变;顺着画布的边找,经中间节点用到卡上值的下游卡也标
"affected_cards": recompute.downstream_cards(cards, card_node, edges=edges) if maybe else [],
}
@router.get("/{run_id}/evidence/whatif/rows")
async def evidence_whatif_rows(run_id: str, artifact: str | None = None, offset: str | None = None,
limit: str | None = None) -> dict[str, Any]:
"""一页查询快照的行:{artifact, columns, rows, row_offset, row_index, total_rows, masked, hash_ok, sealed, note?}。
形状和证据面板的查询步骤一致(界面用同一个函数读)。只认这次运行封存范围内交回过的快照;遮罩列(查询当时记下的
加数据源现在设的)写「已遮罩」;读不出来、与哈希不一致的不给行,note 照实说。只读:不追加事件、不落工件。
"""
if not artifact or not artifact.strip():
raise CodedHTTPException(422, "请求格式有误:artifact 应为查询快照的工件 ID", BAD_REQUEST)
artifact = artifact.strip()
start, size = _count(offset, "offset", 0), min(_count(limit, "limit", ROWS_PAGE), MAX_ROWS_PAGE)
sealed = await _sealed(run_id)
info = sealed.queries.get(artifact)
if info is None:
raise CodedHTTPException(422, OUT_OF_SCOPE, BAD_INPUT)
try:
snap = await asyncio.to_thread(load, artifact)
hash_ok: bool | None = True if snap is not None else None
except ValueError:
snap, hash_ok = None, False
page: dict[str, Any] = {
"run_id": run_id, "artifact": artifact, "node_id": info.get("node_id"), "tool": info.get("tool"),
"source": info.get("source") or (snap.get("source") if isinstance(snap, dict) else None),
"columns": [], "rows": [], "row_offset": start, "row_index": [], "total_rows": None, "truncated": None,
"masked": [], "hash_ok": hash_ok, "sealed": sealed.trusted,
}
if not isinstance(snap, dict) or not isinstance(snap.get("rows"), list):
return {**page, "note": TAMPERED_QUERY if hash_ok is False else MISSING_QUERY}
hidden = await _mask_of(snap, info, _Masks())
columns = [str(c) for c in snap.get("columns") or []]
masked = {i for i, c in enumerate(columns) if c.lower() in hidden}
rows = snap["rows"]
picked = range(start, min(start + size, len(rows)))
return {**page, "columns": columns, "total_rows": len(rows), "truncated": bool(snap.get("truncated")),
"masked": [columns[i] for i in sorted(masked)],
"column_types": snap.get("column_types") if isinstance(snap.get("column_types"), dict) else {},
"row_index": list(picked),
"rows": [[MASKED if i in masked else v for i, v in enumerate(rows[r])] if isinstance(rows[r], list)
else rows[r] for r in picked]}
def _count(raw: str | None, name: str, default: int) -> int:
"""查询参数里的非负整数;没给用默认值。写错了 422,说清是哪一个。"""
if raw is None or not raw.strip():
return default
try:
value = int(raw)
except ValueError:
value = -1
if value < 0:
raise CodedHTTPException(422, f"请求格式有误:{name} 应为不小于 0 的整数", BAD_REQUEST)
return value
def _compute(card: dict[str, Any], changes: recompute.Changes, sources: recompute.Sources, metric: str | None,
docs: list[tuple[str, dict[str, Any]]], card_node: str, artifact: str
) -> tuple[dict[str, Any], list[dict[str, Any]]]:
@@ -170,6 +237,16 @@ def _card_of(sealed: _Sealed, cards: list[tuple[str, str, dict[str, Any]]], node
return artifact, good[artifact], started
def _sources(sealed: _Sealed, cards: list[tuple[str, str, dict[str, Any]]], started: int,
loop: asyncio.AbstractEventLoop) -> recompute.Sources:
"""重算要用的东西(recompute.Sources):封存范围内的工件、口径卡那次执行之前的节点产出和跑完的时刻、快照交回的
时刻、用到哪份才算哪份的遮罩。拿到的 Sources 要在工作线程里用(_Hidden 在线程里等事件循环查库)。"""
loader = _loader(sealed, cards)
return recompute.Sources(load=loader, queries=tuple(sealed.queries), outputs=_outputs_before(sealed, started),
masked=_Hidden(sealed, loader, loop), returned=_returned(sealed),
finished=_finished_before(sealed, started))
def _outputs_before(sealed: _Sealed, started: int) -> dict[str, str]:
"""口径卡那次执行开始之前,每个节点最后一次的产出工件:口径卡当时读到的 nodes.* 就是它们。"""
out: dict[str, str] = {}
@@ -181,6 +258,33 @@ def _outputs_before(sealed: _Sealed, started: int) -> dict[str, str]:
return out
def _finished_before(sealed: _Sealed, started: int) -> dict[str, int]:
"""口径卡那次执行开始之前,每个节点最后一次跑完的事件序号:口径卡读到的 vars.*、nodes.* 是那一刻写下的。"""
out: dict[str, int] = {}
for seq, etype, node_id, _ in sealed.events:
if seq >= started:
break
if etype == "node.finished" and node_id:
out[node_id] = seq
return out
def _returned(sealed: _Sealed) -> dict[str, int]:
"""封存范围内每份查询快照第一次交回的事件序号:tool.end 的 query_artifact,或者 node.finished 台账里的 query
条目(合并查询只有后者)。排除这份快照里的行,只可能影响在这之后才算出来的值。"""
out: dict[str, int] = {}
for seq, etype, _, data in sealed.events:
if not isinstance(data, dict):
continue
if etype == "tool.end" and data.get("query_artifact"):
out.setdefault(str(data["query_artifact"]), seq)
elif etype == "node.finished":
for entry in data.get("evidence") or []:
if isinstance(entry, dict) and entry.get("kind") == "query" and entry.get("artifact"):
out.setdefault(str(entry["artifact"]), seq)
return out
def _loader(sealed: _Sealed, cards: list[tuple[str, str, dict[str, Any]]]) -> Any:
"""只认这次运行封存范围内的工件:证据接口的白名单(查询、表结构、检索快照和台账记过的),加上封存的节点产出工件,
以及封存的指标集里写着的完整值(value_artifact——写在内容寻址、又在封存范围内的指标集里,属于封存链的延伸)。
@@ -204,23 +308,53 @@ def _loader(sealed: _Sealed, cards: list[tuple[str, str, dict[str, Any]]]) -> An
return loader
async def _hidden(sealed: _Sealed, loader: Any) -> dict[str, frozenset[str]]:
"""封存范围内每份查询快照要遮的列(小写):查询当时记下的加数据源现在设的,合并查询按它合并的那几个数据源算。
和证据面板查询步骤的遮罩同一套(api/evidence.py 的 _query_step)。取不回来、被改过的快照不算:重算也不会用它。
快照经推演用的同一个 loader 取,读过的重算时不再读盘。"""
masks = _Masks()
out: dict[str, frozenset[str]] = {}
for artifact, info in sealed.queries.items():
try:
snap = loader(artifact)
except ValueError:
continue
if not isinstance(snap, dict):
continue
if snap.get("source") == MERGE_SOURCE:
hidden, _ = await _merge_masks(snap, masks)
else:
hidden = (await masks.of(info.get("source") or snap.get("source")))[0] | _recorded_masks(snap)
if hidden:
out[artifact] = frozenset(hidden)
return out
class _Hidden(Mapping[str, frozenset[str]]):
"""封存范围内的查询快照 → 要遮的列(小写),推演用到哪份才算哪份。
遮的列和证据面板查询步骤同一套(api/evidence.py 的 _query_step):查询当时记下的加数据源现在设的,合并查询按它
合并的那几个数据源算。快照读不出来、被改过时照样按台账记的数据源算:遮罩不能因为快照坏了就失效(那一格的值另由
重算不给)。
在重算的工作线程里取用:快照经推演用的同一个 loader 读(重算本来也要读这一份,读过的不再读盘);查数据源现在
设的遮罩要访问库,交回事件循环去查(_Masks 是异步的),线程在这里等它。以前每次请求在事件循环里把这次运行的
全部快照读一遍、复验一遍哈希,不管卡上用没用到。
"""
def __init__(self, sealed: _Sealed, loader: Any, loop: asyncio.AbstractEventLoop) -> None:
self._sealed, self._loader, self._loop = sealed, loader, loop
self._masks = _Masks()
self._memo: dict[str, frozenset[str]] = {}
def __getitem__(self, artifact: str) -> frozenset[str]:
key = str(artifact)
if key not in self._sealed.queries:
raise KeyError(key)
if key not in self._memo:
try:
running = asyncio.get_running_loop()
except RuntimeError:
running = None
if running is self._loop:
raise RuntimeError("遮罩只能在工作线程里取:在事件循环里同步等它查库会卡死")
try:
snap = self._loader(key)
except ValueError:
snap = None
found = _mask_of(snap if isinstance(snap, dict) else None, self._sealed.queries[key], self._masks)
self._memo[key] = frozenset(asyncio.run_coroutine_threadsafe(found, self._loop).result())
return self._memo[key]
def __iter__(self) -> Iterator[str]:
return iter(self._sealed.queries)
def __len__(self) -> int:
return len(self._sealed.queries)
async def _mask_of(snap: dict[str, Any] | None, info: dict[str, Any], masks: _Masks) -> set[str]:
"""一份查询快照要遮的列(小写)。snap 为 None(读不出来)时只能按台账记的数据源现在设的遮罩算。"""
if snap is not None and snap.get("source") == MERGE_SOURCE:
hidden, _ = await _merge_masks(snap, masks)
return hidden
hidden, _ = await masks.of(info.get("source") or (snap or {}).get("source"))
return hidden | (_recorded_masks(snap) if snap is not None else set())
+163 -37
View File
@@ -15,14 +15,18 @@ round(和 metrics.run_metrics 一样只 round 浮点),渲染走 evidence.r
- vars.*:有 value_artifact(口径卡当时另存的完整值)才还原得出来;老运行照实说当时没有保存
- 容器本身是一份查询结果(带 artifact 的 {columns, rows},或者和某份快照的 rows 一模一样的列表):排除行就去掉那几行
- 遮罩列取来的输入不给值、也不拿来推演:改一个输入、排除一行,新值和原值一比就能算出遮住的那一格
- 落在读不出来(文件不在、与哈希不一致)的查询快照上的输入同样不给值、不推演:那一格遮没遮核对不了,宁可多遮
同一张口径卡的指标不能互相引用(算这张卡时卡上的值还不存在),所以只有用到了被改的输入、被排除的行的指标需要重算,
别的原样交回。
别的原样交回。排除行时有一种例外:指标的某个输入对不上任何快照(完整值找不回来的容器、加工过的结果、按位置从结果里
取的一格、由查询结果算出来的数),它可能就是从被排除了行的那份结果来的,重算不了,只能说不知道(unsupported),
不能报「无变化」。写它的节点在那份快照交回之前就跑完了的,影响不到它,照常不算。
"""
from __future__ import annotations
import ast
import copy
import json
import math
import re
from collections.abc import Callable, Iterable, Mapping
@@ -50,6 +54,16 @@ MASKED_TABLE = "这份查询结果里有设置了遮罩的列,不能用于推
MASKED_METRIC = "这个指标用到了设置了遮罩的列,不能推演"
SELECTOR = "这个输入决定取哪一行、哪一项,修改它需要重新取数,不能推演"
EXCLUDED = "排除后无法计算"
UNREADABLE = "查询快照无法读取"
TAMPERED = "查询快照与哈希不一致,疑似被修改"
#: 不能改、也没有行可勾的输入(_fixed)
FIXED_CONTAINER = "列表或对象不能直接修改,也没有对应的查询结果可以排除行"
NOT_NUMBER = "不是数值,不能修改"
#: 排除了行,却说不准这个输入受不受影响(_doubt)
DOUBT = "排除行对 {path} 的影响无法确定:{why}"
DOUBT_UNMATCHED = "它对不上任何查询快照,可能是由查询结果加工而来"
DOUBT_DERIVED = "它不是直接取自查询结果的格,可能由查询结果算出"
NOT_FINITE = "计算结果不是有限的数值(溢出或非数),无法推演"
#: 卡上给容器记的大概(metrics._brief):「[12 项]」「{3 个字段}」
_BRIEF = re.compile(r"^(?:\[\d+ 项\]|\{\d+ 个字段\})$")
@@ -77,13 +91,18 @@ class Sources:
(artifact_store.load 的约定)
- queries:封存范围内的查询快照,按在事件里第一次出现的顺序。排除行只认这些快照;容器按内容对快照时也按这个顺序
- outputs:节点 id → 口径卡执行之前它最后一次的产出工件(nodes.*、input.* 的容器、agent 的数组字段从这里还原)
- masked:查询快照 → 要遮的列(小写):查询当时记下的加数据源现在设的
- masked:查询快照 → 要遮的列(小写):查询当时记下的加数据源现在设的。只用到 get,调用方可以给一个用到哪份
才算哪份的映射(快照读不出来时照样按台账记的数据源算)
- returned:查询快照 → 它第一次交回时的事件序号;finished:节点 → 口径卡执行之前它最后一次跑完的事件序号。
排除行只可能影响在那份快照交回之后才算出来的值(_doubt)。不知道的一律当可能受影响
"""
load: Callable[[str], Any]
queries: tuple[str, ...] = ()
outputs: Mapping[str, str] = field(default_factory=dict)
masked: Mapping[str, frozenset[str]] = field(default_factory=dict)
returned: Mapping[str, int] = field(default_factory=dict)
finished: Mapping[str, int] = field(default_factory=dict)
class _Store:
@@ -127,6 +146,8 @@ class _Input:
rows: tuple[int, int] | None = None
table: bool = False
masked: bool = False
#: 落在的查询快照读不出来(文件不在、与哈希不一致):遮没遮核对不了,值不给,也不拿来推演
unreadable: bool = False
#: 出现在别的输入的下标、cell() 的行列里:它决定取哪一格,改它要重新取数
selector: bool = False
@@ -150,7 +171,8 @@ def whatif(card: dict[str, Any], changes: Changes, sources: Sources, *, metric:
"""在一张口径卡上推演:{metrics: [每个指标的旧值、新值、输入…], unsupported: [不能用于推演的输入和原因]}。
metric 给了就只推演这一个指标。改动不合法时抛 WhatIfError,一个指标都不算。没碰到改动的指标不重算,
原值原样交回(status: unchanged);unsupported 不论有没有改动都列全,界面一打开就能说清哪些输入不能动。
原值原样交回(status: unchanged);unsupported 不论有没有改动都列全(遮罩的、还原不了的、不能改也没有行可勾的),
界面一打开就能说清哪些输入不能动。
"""
store = _Store(sources)
metrics = [m for m in card.get("metrics") or [] if isinstance(m, dict) and m.get("id") is not None]
@@ -160,12 +182,12 @@ def whatif(card: dict[str, Any], changes: Changes, sources: Sources, *, metric:
raise WhatIfError(f"口径卡中没有指标「{metric}」")
plans = [_plan(m, store) for m in metrics]
_check(changes, plans, store, metric=metric)
return {"metrics": [_outcome(p, changes) for p in plans], "unsupported": _unsupported(plans)}
return {"metrics": [_outcome(p, changes, sources) for p in plans], "unsupported": _unsupported(plans)}
def recompute(metric: dict[str, Any], changes: Changes, sources: Sources) -> dict[str, Any]:
"""重算一个指标,不管改动碰没碰到它,也不校验改动。核对还原是否忠实时用:什么都不改,重算出来应当和卡上记的一样。"""
return _outcome(_plan(metric, _Store(sources)), changes, force=True)
return _outcome(_plan(metric, _Store(sources)), changes, sources, force=True)
# --------------------------------------------------------------------------
@@ -256,6 +278,13 @@ def _restore(item: dict[str, Any], remaining: set[str], nested: set[str], store:
elif inp.problem is None and inp.snapshot is None:
inp.snapshot = _table_of(inp.value, store)
inp.table = inp.snapshot is not None
if inp.snapshot is not None:
_, trouble = store.get(inp.snapshot)
if trouble or store.snapshot(inp.snapshot) is None:
# 卡上记着这一格的值,可快照读不出来:这一列遮没遮核对不了(查询当时记下的遮罩在快照里),值不给,
# 也不拿来推演。数据源现在设的遮罩照样认(_masked),认出来了就按遮罩说
inp.unreadable = True
inp.problem = inp.problem or (TAMPERED if trouble == "tampered" else UNREADABLE)
inp.masked = _masked(inp, locator, store)
return inp
@@ -311,7 +340,7 @@ def _table_of(value: Any, store: _Store) -> str | None:
if isinstance(value, dict) and isinstance(value.get("rows"), list):
artifact = value.get("artifact")
if isinstance(artifact, str) and artifact in store.sources.queries:
return artifact
return artifact # 快照读不读得出来由 _restore 核对
rows = value["rows"]
elif isinstance(value, list):
rows = value
@@ -373,7 +402,14 @@ def _span(rows: Any) -> tuple[int, int] | None:
def _number(value: Any) -> bool:
return is_number(value) and math.isfinite(value)
"""算得动的数:bool 不算;无穷大、非数不算;浮点放不下的大整数(10 ** 400)也不算——math.isfinite 对它抛
OverflowError,JSON 写得出来,界面却只能读成 Infinity。"""
if not is_number(value):
return False
try:
return math.isfinite(value)
except OverflowError:
return False
def _check(changes: Changes, plans: list[_Plan], store: _Store, *, metric: str | None) -> None:
@@ -385,10 +421,14 @@ def _check(changes: Changes, plans: list[_Plan], store: _Store, *, metric: str |
found = known.get(path)
if not found:
raise WhatIfError(f"指标「{metric}」没有输入「{path}」" if metric is not None else f"口径卡里没有输入「{path}」")
if not is_number(value):
raise WhatIfError(f"输入「{path}」的新值需要是数值,当前为{_shown(value)}")
if not _number(value):
raise WhatIfError(f"输入「{path}」的新值需要是数值,当前为「{_shown(value)}」")
raise WhatIfError(f"输入「{path}」的新值超出可计算的范围,需要是有限的数值")
if any(i.masked for i in found):
raise WhatIfError(f"输入「{path}」取自设置了遮罩的列,不能用于推演")
if unreadable := next((i for i in found if i.unreadable), None):
raise WhatIfError(f"输入「{path}」所在的{unreadable.problem},不能用于推演")
if any(i.selector for i in found):
raise WhatIfError(f"输入「{path}」决定取哪一行、哪一项,修改它需要重新取数,不能推演")
if found[0].shape != "scalar":
@@ -411,8 +451,11 @@ def _check(changes: Changes, plans: list[_Plan], store: _Store, *, metric: str |
def _shown(value: Any) -> str:
text = str(value)
return text if len(text) <= 40 else text[:40] + "…"
"""不合法的新值写给人看:空值写「空值」,文本照原样,别的按 JSON 的写法——不露出 None、单引号这些 Python 写法。"""
if value is None:
return "空值"
text = value if isinstance(value, str) else json.dumps(value, ensure_ascii=False, default=str)
return f"「{text if len(text) <= 40 else text[:40] + '…'}」"
# --------------------------------------------------------------------------
@@ -444,7 +487,7 @@ def _apply(inp: _Input, changes: Changes) -> tuple[Any, list[int] | None, bool]:
return inp.value, None, False
def _outcome(plan: _Plan, changes: Changes, *, force: bool = False) -> dict[str, Any]:
def _outcome(plan: _Plan, changes: Changes, sources: Sources, *, force: bool = False) -> dict[str, Any]:
values: dict[str, Any] = {}
notes: dict[str, list[int]] = {}
touched = False
@@ -453,15 +496,47 @@ def _outcome(plan: _Plan, changes: Changes, *, force: bool = False) -> dict[str,
if excluded:
notes[path] = excluded
touched = touched or overridden or bool(excluded)
if not touched and not force:
doubt = _doubt(plan, changes, sources)
# 卡上没记全输入的指标:不知道它用了什么,有任何改动都说不准它变不变
touched = touched or (plan.problem is not None and bool(changes.inputs or changes.exclude))
if not touched and doubt is None and not force:
return _result(plan, "unchanged", plan.metric.get("value"), None, values, notes)
status, new, reason = _compute(plan, values, notes, changes)
status, new, reason = _compute(plan, values, notes, changes, doubt)
return _result(plan, status, new, reason, values, notes)
def _compute(plan: _Plan, values: dict[str, Any], notes: dict[str, list[int]],
changes: Changes) -> tuple[str, Any, str | None]:
"""(status, 新值, 原因)。还原不了的、遮罩的、排除后缺了的先说,都没有才求值。"""
def _doubt(plan: _Plan, changes: Changes, sources: Sources) -> str | None:
"""排除了行,这个指标却有输入说不准受不受影响:返回原因;没排除行、或者输入都对得上号的返回 None。
对得上号的输入(cell() 那一格、agent 的单格字段和数组字段、整份查询结果)排除行怎么影响它是确定的,_apply 照办。
别的输入——完整值找不回来的容器、对不上任何快照的容器(加工过的结果)、不是直接取自快照的数(按位置从整份结果里
取的一格、transform 由查询结果算出来的数)——都可能来自被排除了行的那份结果,又重算不了,只能说不知道,不能拿
原值算出一个「无变化」或者一个不对应任何一份数据的数。给了新值的以新值为准;写它的节点在那份快照交回之前就跑完了
的,影响不到它(_after)。
"""
if not changes.exclude:
return None
for path, inp in plan.inputs.items():
if path in changes.inputs or inp.snapshot is not None:
continue
if any(_after(snapshot, inp, sources) for snapshot in changes.exclude):
why = inp.problem or (DOUBT_UNMATCHED if inp.shape == "container" else DOUBT_DERIVED)
return DOUBT.format(path=path, why=why)
return None
def _after(snapshot: str, inp: _Input, sources: Sources) -> bool:
"""这个输入可能是在这份快照交回之后才算出来的:写它的节点不早于快照交回跑完(合并查询的快照就记在它自己跑完的
那条事件里),或者两样有一样不知道。"""
writer = inp.item.get("node_id")
returned = sources.returned.get(snapshot)
finished = sources.finished.get(str(writer)) if writer else None
return returned is None or finished is None or finished >= returned
def _compute(plan: _Plan, values: dict[str, Any], notes: dict[str, list[int]], changes: Changes,
doubt: str | None) -> tuple[str, Any, str | None]:
"""(status, 新值, 原因)。还原不了的、遮罩的、排除后缺了的、说不准受不受影响的先说,都没有才求值。"""
if plan.problem:
return "unsupported", None, plan.problem
for path in plan.tops:
@@ -472,10 +547,12 @@ def _compute(plan: _Plan, values: dict[str, Any], notes: dict[str, list[int]],
for path, inp in plan.inputs.items():
if inp.row is not None and path in notes and path not in changes.inputs:
return "excluded", None, f"{EXCLUDED}:{path} 所在的第 {inp.row + 1} 行已排除"
if doubt:
return "unsupported", None, doubt
value, reason = _evaluate(plan, values)
if reason is None and isinstance(value, float) and not math.isfinite(value):
# 溢出成无穷大、算出非数:口径卡渲染成「—」,这里也说清算不出来(JSON 里也放不下这种值)
reason = "计算结果不是有限的数值(溢出或非数),无法推演"
if reason is None and is_number(value) and not _number(value):
# 溢出成无穷大、算出非数、乘出浮点放不下的整数:口径卡渲染成「—」,这里也说清算不出来(JSON 里也放不下)
reason = NOT_FINITE
return ("ok", value, None) if reason is None else ("error", None, reason)
@@ -543,8 +620,8 @@ def _result(plan: _Plan, status: str, new: Any, reason: str | None, values: dict
delta, rendered_delta = _delta(metric, old, new) if known else (None, None)
substituted = metric.get("substituted") if status == "unchanged" \
else substitute(plan.tree, values) if status == "ok" and plan.tree is not None else None
if any(inp.masked for inp in plan.inputs.values()):
substituted = None # 代入式里代进去的就是遮罩列的那一格
if any(inp.masked or inp.unreadable for inp in plan.inputs.values()):
substituted = None # 代入式里代进去的就是遮罩列(或者遮没遮核对不了)的那一格
return {
"id": metric.get("id"), "name": metric.get("name") or metric.get("id"), "unit": metric.get("unit") or "",
"decimals": metric.get("decimals"), "format": metric.get("format") or DEFAULT_FORMAT,
@@ -566,8 +643,8 @@ def _render(metric: dict[str, Any], value: Any, conv: str | None = None) -> str:
def _finite(value: Any) -> Any:
"""无穷大、非数换成 None:老卡上可能记着(口径卡渲染成「—」),JSON 里放不下。"""
return None if isinstance(value, float) and not math.isfinite(value) else value
"""无穷大、非数、浮点放不下的整数换成 None:老卡上可能记着(口径卡渲染成「—」),JSON 里放不下或界面读不准。"""
return None if is_number(value) and not _number(value) else value
def _same(a: Any, b: Any) -> bool:
@@ -610,19 +687,37 @@ def _kind(inp: _Input) -> str:
def _editable(inp: _Input) -> bool:
return inp.shape == "scalar" and not inp.masked and not inp.selector \
return inp.shape == "scalar" and not inp.masked and not inp.selector and inp.problem is None \
and (inp.value is None or _number(inp.value))
def _row_span(inp: _Input) -> tuple[int, int] | None:
"""这个输入实际用到的快照行(含两端):单格一行、数组字段一段、整份结果全部。排除别的行影响不到它,界面据此只让
这几行可勾。遮罩的、快照读不出来的不给:排除它们的行什么也推演不出来。"""
if inp.snapshot is None or inp.masked or inp.unreadable:
return None
if inp.row is not None:
return inp.row, inp.row
if inp.rows is not None:
return inp.rows
rows = inp.value.get("rows") if isinstance(inp.value, dict) else inp.value
return (0, len(rows) - 1) if inp.table and isinstance(rows, list) and rows else None
def _input_view(inp: _Input, values: dict[str, Any], notes: dict[str, list[int]]) -> dict[str, Any]:
"""一个输入在推演里的样子:原值、新值、排除了哪几行。遮罩列取来的不给值。容器写大概(「[12 项]」)。"""
"""一个输入在推演里的样子:原值、新值、排除了哪几行、用到快照的哪几行(row_span)。遮罩列取来的、落在读不出来的
快照上的不给值。容器写大概(「[12 项]」)。"""
item = inp.item
span = _row_span(inp)
view: dict[str, Any] = {"path": inp.path, "via": item.get("via"), "node_id": item.get("node_id"),
**({"artifact": inp.snapshot} if inp.snapshot else {}),
**({"locator": item["locator"]} if isinstance(item.get("locator"), dict) else {}),
**({"row_span": list(span)} if span else {}),
"kind": _kind(inp), "editable": _editable(inp)}
if inp.masked:
return {**view, "old": None, "new": None, "changed": False, "masked": True}
if inp.unreadable and inp.shape == "scalar":
return {**view, "old": None, "new": None, "changed": False, "withheld": True}
if inp.shape == "container":
old = _outline(inp.value, inp.table) if inp.problem is None else item.get("value")
new = _outline(values.get(inp.path), inp.table) if inp.problem is None else None
@@ -644,7 +739,8 @@ def _outline(value: Any, table: bool) -> Any:
def _unsupported(plans: list[_Plan]) -> list[dict[str, Any]]:
"""不能用于推演的输入和原因:遮罩的、完整值找不回来的、决定取哪一格的。卡本身没记输入的,整个指标一条。"""
"""不能用于推演的输入和原因:遮罩的、完整值找不回来的(含快照读不出来的)、决定取哪一格的、既不能改也没有行可勾的。
卡本身没记输入的,整个指标一条。"""
out: list[dict[str, Any]] = []
for plan in plans:
metric_id = plan.metric.get("id")
@@ -654,12 +750,20 @@ def _unsupported(plans: list[_Plan]) -> list[dict[str, Any]]:
for inp in plan.inputs.values():
reason = (MASKED_TABLE if inp.table else MASKED_INPUT) if inp.masked \
else f"无法推演:{inp.problem}" if inp.problem \
else SELECTOR if inp.selector else None
else SELECTOR if inp.selector else _fixed(inp)
if reason:
out.append({"metric": metric_id, "input": inp.path, "reason": reason})
return out
def _fixed(inp: _Input) -> str | None:
"""还原得出、没遮罩,却既不能改值、也没有行可勾的输入:说清为什么,不让人对着一个改不了的值干看。"""
if inp.shape == "container":
return None if _row_span(inp) is not None else FIXED_CONTAINER
value = inp.item.get("value")
return None if value is None or is_number(value) else NOT_NUMBER
# --------------------------------------------------------------------------
# 报告里引用的地方、下游口径卡
# --------------------------------------------------------------------------
@@ -703,11 +807,15 @@ def _cites(seg: dict[str, Any], cite: dict[str, Any], entry: dict[str, Any]) ->
return cite.get("status") == "unresolved" and entry.get("value") is None and entry.get("status") == "missing_input"
def downstream_cards(cards: Iterable[tuple[str, str, dict[str, Any]]], node_id: str) -> list[dict[str, Any]]:
"""引用了这张口径卡的下游口径卡(直接的和经别的卡间接的):[{node_id, artifact, caliber, version, metrics, direct}]。
def downstream_cards(cards: Iterable[tuple[str, str, dict[str, Any]]], node_id: str, *,
edges: Iterable[tuple[Any, Any]] = ()) -> list[dict[str, Any]]:
"""引用了这张口径卡的下游口径卡:[{node_id, artifact, caliber, version, metrics, direct}]。
只标「可能受影响」,不重算:下游卡怎么用这张卡的值(取第几个指标、和别的什么一起算)要整条链重放才说得准。
认引用看下游指标的输入从哪个节点来(node_id,或者 nodes.<卡> 这样的路径)。同一张卡执行过几次的按最后一次。
认引用看下游指标的输入从哪个节点来(node_id,或者 nodes.<卡> 这样的路径):直接来自这张卡的标 direct。
经别的卡间接用到的也标;给了画布上的边(edges)时,从这张卡顺着边走得到的节点也算上游——下游卡经 transform 之类
的中间节点用到卡上的值(输入写的是 vars.t.g,来自中间节点),同样可能受影响。循环的回边会让循环里的节点都走得到,
宁可多标。同一张卡执行过几次的按最后一次。
"""
latest: dict[str, tuple[str, dict[str, Any]]] = {}
for nid, artifact, card in cards:
@@ -725,15 +833,33 @@ def downstream_cards(cards: Iterable[tuple[str, str, dict[str, Any]]], node_id:
break
return found
upstream = {str(node_id)} | _reach(edges, str(node_id))
flagged: dict[str, dict[str, Any]] = {}
frontier, direct = {str(node_id)}, True
while frontier:
reached: set[str] = set()
grew = True
while grew:
# 标出来的卡本身也成了上游:经它间接用到的再标一轮,直到没有新的
grew = False
for nid, (artifact, card) in latest.items():
if nid in flagged or not (metrics := uses(card, frontier)):
if nid in flagged or not (metrics := uses(card, upstream)):
continue
flagged[nid] = {"node_id": nid, "artifact": artifact, "caliber": card.get("caliber") or "",
"version": card.get("caliber_version") or "", "metrics": metrics, "direct": direct}
reached.add(nid)
frontier, direct = reached, False
"version": card.get("caliber_version") or "", "metrics": metrics,
"direct": bool(uses(card, {str(node_id)}))}
upstream.add(nid)
grew = True
return [flagged[nid] for nid in latest if nid in flagged]
def _reach(edges: Iterable[tuple[Any, Any]], start: str) -> set[str]:
"""从 start 顺着边走得到的节点(不含 start 自己,除非绕回来)。"""
after: dict[str, list[str]] = {}
for a, b in edges:
after.setdefault(str(a), []).append(str(b))
seen: set[str] = set()
todo = [start]
while todo:
for nxt in after.get(todo.pop(), []):
if nxt not in seen:
seen.add(nxt)
todo.append(nxt)
return seen
+239 -2
View File
@@ -143,7 +143,8 @@ def test_a_baseline_lists_what_cannot_be_used_without_recomputing_anything():
assert "完整值" in unsupported[("n", "vars.kpi.items")]
assert "遮罩" in unsupported[("phone_len", "cell(nodes.crm, 0, 'phone')")]
assert "取哪" in unsupported[("pick", "vars.kpi.i")]
assert ("n2", "vars.kpi.items") not in unsupported and ("n3", "nodes.fetch.items") not in unsupported
# 还原得出的容器不算「完整值找不回来」,只是不能直接改、也没有行可勾
assert "完整值" not in unsupported[("n2", "vars.kpi.items")] and "完整值" not in unsupported[("n3", "nodes.fetch.items")]
# 遮罩列的值不给
[phone] = metrics["phone_len"]["inputs"]
assert phone["masked"] is True and phone["old"] is None and phone["new"] is None and phone["editable"] is False
@@ -173,7 +174,8 @@ def test_changing_an_input_recomputes_only_the_metrics_that_use_it():
def test_excluding_rows_drops_them_from_an_agent_array_field():
result = whatif(CARD, Changes(exclude={S1: frozenset({1})}), sources())
# vars.kpi.k 是 fetch 节点在快照交回之前写的:排除行影响不到它(不知道先后时说不准,见下面几条)
result = whatif(CARD, Changes(exclude={S1: frozenset({1})}), sources(returned={S1: 10}, finished={"fetch": 5}))
metrics = by_id(result)
total = metrics["total"]
assert total["new"] == 340.0 - 80.0 and total["rendered_new"] == "260.0元" and total["delta"] == -80.0
@@ -396,3 +398,238 @@ def test_downstream_cards_that_use_this_card_are_flagged_not_recomputed():
{"node_id": "yearly", "artifact": "B1", "caliber": "年报口径", "version": "v1", "metrics": ["y"], "direct": True},
{"node_id": "board", "artifact": "C1", "caliber": "看板口径", "version": "v2", "metrics": ["b"], "direct": False},
]
# --------------------------------------------------------------------------
# 排除行:说不准受不受影响的指标不能报「无变化」
# --------------------------------------------------------------------------
def lone(mid, expression, substituted, value, inputs, **kw):
"""只有一个指标的卡(同一套快照、产出)。"""
return {**CARD, "metrics": [metric(mid, expression, substituted, value, inputs, **kw)]}
#: 老运行:cell() 一格加一个没存完整值的 vars 容器(写它的是 parse 节点)
OLD_MIX = lone("mix", "round(cell(nodes.pull, 0, 'amount') / len(vars.res.rows), 2)",
"round(120.5 / len(vars.res.rows), 2)", 30.13,
[cell("pull", 0, "amount", 120.5), scalar("vars.res.rows", "[4 项]", node="parse")], decimals=2)
def test_excluding_rows_never_reports_an_unrestorable_metric_as_unchanged():
"""排除的是 cell 那一格以外的行:容器的完整值找不回来,它会不会少几行说不准,指标不能报「无变化」。"""
[mix] = whatif(OLD_MIX, Changes(exclude={S1: frozenset({2})}), sources())["metrics"]
assert mix["status"] == "unsupported" and mix["changed"] is None and mix["rendered_new"] is None
assert mix["new"] is None and "vars.res.rows" in mix["reason"] and "完整值" in mix["reason"]
# 只改输入值、不排除行时照旧:容器没被改,也就不用重算
[mix] = whatif(OLD_MIX, Changes(), sources())["metrics"]
assert mix["status"] == "unchanged" and mix["changed"] is False
def test_a_container_matching_no_snapshot_is_doubted_when_rows_are_excluded():
"""能还原、却对不上任何快照的容器(加工过的结果):排除行后它会不会变说不准。"""
card = lone("mixed", "cell(nodes.pull, 0, 'amount') + len(vars.kpi.items)", "120.5 + len(vars.kpi.items)", 123.5,
[cell("pull", 0, "amount", 120.5),
{**scalar("vars.kpi.items", "[3 项]"), "value_artifact": ITEMS_VALUE}], decimals=1)
[mixed] = whatif(card, Changes(exclude={S1: frozenset({3})}), sources())["metrics"]
assert mixed["status"] == "unsupported" and mixed["changed"] is None
assert "排除行对 vars.kpi.items 的影响无法确定" in mixed["reason"] and "对不上任何查询快照" in mixed["reason"]
def test_a_scalar_taken_by_position_from_the_excluded_table_is_doubted():
"""vars.res.rows[0][1] 按位置取自整份结果:排除第 0 行后,len 变了,分子却还是原来第 0 行的值——不能算出一个
不对应任何一份数据的数。"""
card = lone("first_share", "round(vars.res.rows[0][1] / len(vars.res.rows), 2)",
"round(120.5 / len(vars.res.rows), 2)", 30.12,
[scalar("vars.res.rows[0][1]", 120.5, node="parse"),
{**scalar("vars.res.rows", "[4 项]", node="parse"), "value_artifact": RES_ROWS}], decimals=2)
assert recompute(card["metrics"][0], Changes(), sources())["new"] == round(120.5 / 4, 2) == 30.12
[share] = whatif(card, Changes(exclude={S1: frozenset({0})}), sources())["metrics"]
assert share["status"] == "unsupported" and share["new"] is None and share["changed"] is None
assert "vars.res.rows[0][1]" in share["reason"]
def test_inputs_written_before_the_snapshot_came_back_are_not_doubted():
"""写这个输入的节点在快照交回之前就跑完了:排除这份快照里的行影响不到它。"""
early = sources(returned={S1: 10}, finished={"parse": 5, "fetch": 5})
[mix] = whatif(OLD_MIX, Changes(exclude={S1: frozenset({2})}), early)["metrics"]
assert mix["status"] == "unchanged" and mix["changed"] is False
late = sources(returned={S1: 10}, finished={"parse": 12})
[mix] = whatif(OLD_MIX, Changes(exclude={S1: frozenset({2})}), late)["metrics"]
assert mix["status"] == "unsupported"
def test_a_metric_without_lineage_is_unsupported_once_anything_changes():
legacy = {"id": "old", "name": "旧指标", "unit": "", "value": 3, "expression": "vars.x + 1"}
card = {**CARD, "metrics": [legacy, CARD["metrics"][0]]}
metrics = by_id(whatif(card, Changes(), sources()))
assert metrics["old"]["status"] == "unchanged"
for changes in (Changes(inputs={"vars.kpi.gmv": 50000}), Changes(exclude={S1: frozenset({1})})):
old = by_id(whatif(card, changes, sources()))["old"]
assert old["status"] == "unsupported" and old["changed"] is None and "没有记录输入" in old["reason"]
# --------------------------------------------------------------------------
# 查询快照读不出来:值不给、不推演
# --------------------------------------------------------------------------
@pytest.mark.parametrize("broken, words", [
(lambda a: None if a == S1 else load(a), "无法读取"),
(lambda a: (_ for _ in ()).throw(ValueError("篡改")) if a == S1 else load(a), "哈希"),
])
def test_an_unreadable_snapshot_withholds_its_cells(broken, words):
"""快照读不出来,遮没遮核对不了:卡上记着的那一格也不给、不能改,用到它的指标推演不了。"""
path = "cell(nodes.pull, 0, 'amount')"
result = whatif(CARD, Changes(), sources(load=broken), metric="east")
[east] = result["metrics"]
[inp] = east["inputs"]
assert inp["old"] is None and inp["new"] is None and inp["editable"] is False and inp["withheld"] is True
assert east["substituted"] is None and "120.5" not in str(east["inputs"]) + str(east["substituted"])
[(input_path, reason)] = [(u["input"], u["reason"]) for u in result["unsupported"]]
assert input_path == path and words in reason
with pytest.raises(WhatIfError, match="快照"):
whatif(CARD, Changes(inputs={path: 100}), sources(load=broken), metric="east")
# 别的指标照样推演,只是用到这份快照的推演不了
total = by_id(whatif(CARD, Changes(inputs={"vars.kpi.gmv": 50000}), sources(load=broken)))["total"]
assert total["status"] == "unchanged" and "120.5" not in str(total["inputs"])
def test_a_masked_column_stays_masked_when_its_snapshot_is_unreadable():
phone = by_id(whatif(CARD, Changes(), sources(load=lambda a: None if a == S2 else load(a))))["phone_len"]
[inp] = phone["inputs"]
assert inp["masked"] is True and inp["old"] is None and phone["substituted"] is None
# --------------------------------------------------------------------------
# 超大的数、给人看的值
# --------------------------------------------------------------------------
def test_numbers_too_large_to_compute_are_rejected_not_crashed():
with pytest.raises(WhatIfError, match="范围"):
whatif(CARD, Changes(inputs={"vars.kpi.gmv": 10 ** 400}), sources())
# 1e308 本身放得下,乘出来的整数放不下:说清算不出来
[n2] = whatif(CARD, Changes(inputs={"vars.kpi.k": 10 ** 308}), sources(), metric="n2")["metrics"]
assert n2["status"] == "error" and n2["new"] is None and "有限" in n2["reason"]
import json
json.dumps(n2, allow_nan=False)
@pytest.mark.parametrize("value, shown", [(None, "空值"), ({"a": 1}, '{"a": 1}'), ([1, "两"], '[1, "两"]')])
def test_invalid_values_are_shown_the_way_a_person_writes_them(value, shown):
with pytest.raises(WhatIfError) as caught:
whatif(CARD, Changes(inputs={"vars.kpi.gmv": value}), sources())
assert shown in str(caught.value) and "None" not in str(caught.value) and "'a'" not in str(caught.value)
# --------------------------------------------------------------------------
# 推演里能动什么:可勾的行、不能动的原因
# --------------------------------------------------------------------------
def test_inputs_say_which_snapshot_rows_they_use():
"""输入实际落在快照的哪几行(row_span,含两端):单格一行、数组字段一段、整份结果全部。遮罩的不给。"""
result = whatif(CARD, Changes(), sources())
spans = {(m["id"], i["path"]): i.get("row_span") for m in result["metrics"] for i in m["inputs"]}
assert spans[("east", "cell(nodes.pull, 0, 'amount')")] == [0, 0]
assert spans[("total", "vars.kpi.amounts")] == [0, 3]
assert spans[("rows", "vars.res.rows")] == [0, 3]
assert spans[("phone_len", "cell(nodes.crm, 0, 'phone')")] is None
assert spans[("wow", "vars.kpi.gmv")] is None and spans[("n2", "vars.kpi.items")] is None
def test_inputs_that_cannot_be_adjusted_say_why():
"""不能改、也没有行可勾的输入都列进「不支持推演的输入」,不让人对着一个改不了的值干看。"""
card = {**CARD, "metrics": [
*CARD["metrics"],
metric("label", "len(vars.kpi.name) + vars.kpi.k", "len('华东区') + 2", 5,
[scalar("vars.kpi.name", "华东区"), scalar("vars.kpi.k", 2)]),
metric("flag", "vars.kpi.on * 3", "True * 3", 3, [scalar("vars.kpi.on", True)])]}
reasons = {(u["metric"], u["input"]): u["reason"] for u in whatif(card, Changes(), sources())["unsupported"]}
assert "列表或对象不能直接修改" in reasons[("n2", "vars.kpi.items")]
assert "列表或对象不能直接修改" in reasons[("n3", "nodes.fetch.items")]
assert "不是数值" in reasons[("label", "vars.kpi.name")] and "不是数值" in reasons[("flag", "vars.kpi.on")]
# 能排除行的容器、能改的数不在其中
assert ("total", "vars.kpi.amounts") not in reasons and ("rows", "vars.res.rows") not in reasons
assert ("label", "vars.kpi.k") not in reasons and ("wow", "vars.kpi.gmv") not in reasons
# --------------------------------------------------------------------------
# 还原时的几处保护
# --------------------------------------------------------------------------
def test_an_array_whose_length_does_not_match_its_rows_is_not_excluded_by_row():
"""数组字段 4 行的出处、产出里却只有 3 个元素:元素和行对不上号,不能按行去掉元素(会去错)。"""
short = sources(load=lambda a: {**STORE[BOT_OUT], "data": {"amounts": [120.5, 80.0, 99.5]}} if a == BOT_OUT
else load(a))
card = lone("total3", "sum(vars.kpi.amounts)", "sum(vars.kpi.amounts)", 300.0, [AMOUNTS], decimals=1, unit="元")
assert recompute(card["metrics"][0], Changes(), short)["new"] == 300.0
[total] = whatif(card, Changes(exclude={S1: frozenset({1})}), short)["metrics"]
assert total["status"] == "unsupported" and total["new"] is None
assert "excluded_rows" not in total["inputs"][0] and total["inputs"][0].get("row_span") is None
def test_a_node_path_with_a_variable_index_is_not_restored():
card = lone("pick2", "len(nodes.fetch.lists[vars.kpi.i])", "len(nodes.fetch.lists[vars.kpi.i])", 2,
[{**scalar("nodes.fetch.lists[vars.kpi.i]", "[2 项]"), "field": "lists[vars.kpi.i]"},
scalar("vars.kpi.i", 0)])
out = recompute(card["metrics"][0], Changes(), sources())
assert out["status"] == "unsupported" and "变量下标" in out["reason"]
def test_text_that_merely_looks_like_a_container_summary_is_a_plain_value():
"""值恰好是「[3 项]」这几个字的文本,代入式里被代成了字面量:它就是当时的值,不是容器。"""
card = lone("tag", "len(vars.kpi.tag)", "len('[3 项]')", 5, [scalar("vars.kpi.tag", "[3 项]")])
out = recompute(card["metrics"][0], Changes(), sources())
assert out["status"] == "ok" and out["new"] == 5 and out["inputs"][0]["kind"] == "text"
def test_a_query_result_carrying_its_snapshot_id_is_mapped_by_it():
"""容器带着封存范围内快照的 artifact(数据源工具交回的结果原样解析):按它认快照,哪怕行的写法和快照不同。"""
parsed = "b8" * 32
rows = [["华东", "120.5", "p1"], ["华南", "80.0", "p2"], ["华北", "99.5", "p3"], ["西南", "40.0", "p4"]]
store = {**STORE, parsed: {"path": "vars.res", "value": {"columns": ["region", "amount", "phone"], "rows": rows,
"artifact": S1}}}
card = lone("cnt", "len(get(vars.res, 'rows'))", "len(get(vars.res, 'rows'))", 4,
[{**scalar("vars.res", "{3 个字段}", node="parse"), "value_artifact": parsed}])
mapped = sources(load=lambda a: store.get(a) if a != BAD else load(a))
assert recompute(card["metrics"][0], Changes(), mapped)["new"] == 4
[cnt] = whatif(card, Changes(exclude={S1: frozenset({0, 2})}), mapped)["metrics"]
assert cnt["status"] == "ok" and cnt["new"] == 2
assert cnt["inputs"][0]["artifact"] == S1 and cnt["inputs"][0]["excluded_rows"] == [0, 2]
def test_a_segment_whose_metric_had_no_value_is_listed_when_the_whatif_supplies_one():
"""缺输入、当时显示「—」的引用也列上:推演里补上了值,它会变。"""
card = {**CARD, "metrics": [metric("cost", "vars.kpi.cost / vars.kpi.orders", "vars.kpi.cost / 1234", None,
[scalar("vars.kpi.cost", None), scalar("vars.kpi.orders", 1234)], decimals=2,
rendered="—")]}
result = whatif(card, Changes(inputs={"vars.kpi.cost": 2468}), sources())
doc = doc_with({"id": "s7", "text": "—", "kind": "value", "state": "flagged",
"cite": {"alias": "m:cost", "kind": "metric", "status": "unresolved"}})
doc["catalog"]["m:cost"] = {"alias": "m:cost", "kind": "metric", "node_id": "card", "artifact": "CARD",
"locator": {"metric": "cost"}, "value": None, "status": "missing_input"}
[found] = report_segments([("write", doc)], card_node="card", card_artifact="CARD", results=result["metrics"])
assert (found["sid"], found["rendered_old"], found["rendered_new"], found["changed"]) == ("s7", "—", "2.00", True)
# --------------------------------------------------------------------------
# 下游口径卡:经中间节点引用的也标
# --------------------------------------------------------------------------
def test_downstream_cards_reached_through_other_nodes_are_flagged():
"""card → mid(transform 用了卡上的值)→ yearly:yearly 的输入写的是 vars.t.g,来自 mid,不是卡本身。
按画布上的边从这张卡往下找:经别的节点间接用到的也标「可能受影响」。"""
upstream = ("card", "A1", CARD)
yearly = ("yearly", "B1", {"caliber": "年报口径", "caliber_version": "v1", "metrics": [
metric("y", "vars.t.g * 26", "17.4 * 26", 452.4,
[{"path": "vars.t.g", "value": 17.4, "node_id": "mid", "via": "transform", "status": "ok"}])]})
before = ("before", "C1", {"caliber": "早口径", "caliber_version": "v1", "metrics": [
metric("b", "vars.kpi.k", "2", 2, [scalar("vars.kpi.k", 2, node="fetch")])]})
edges = [("fetch", "before"), ("before", "card"), ("card", "mid"), ("mid", "yearly")]
assert downstream_cards([upstream, yearly, before], "card") == []
assert downstream_cards([upstream, yearly, before], "card", edges=edges) == [
{"node_id": "yearly", "artifact": "B1", "caliber": "年报口径", "version": "v1", "metrics": ["y"], "direct": False}]
+339 -6
View File
@@ -216,7 +216,7 @@ async def test_changing_an_input_of_one_metric(client, monkeypatch):
async def faithful(run_id: str) -> list[tuple]:
"""真跑出来的每张卡、每个指标,什么都不改、强制重算一遍:(卡, 指标, 状态, 重算值, 卡上的值)。
输入的还原(标量、cell()、整份查询结果、agent 数组、nodes.*)是否忠实,在真卡上对一遍。"""
输入的还原(标量、cell()、整份查询结果、agent 数组、nodes.*)是否忠实,在真卡上对一遍。和接口一样在工作线程里算。"""
from app.api import whatif as api_whatif
from app.api.evidence import _reports, _sealed, _sealed_cards
from app.engine import recompute
@@ -226,12 +226,9 @@ async def faithful(run_id: str) -> list[tuple]:
out = []
for node_id in dict.fromkeys(nid for nid, _, _ in cards):
_, card, started = api_whatif._card_of(sealed, cards, node_id, _reports(sealed), None)
loader = api_whatif._loader(sealed, cards)
sources = recompute.Sources(load=loader, queries=tuple(sealed.queries),
outputs=api_whatif._outputs_before(sealed, started),
masked=await api_whatif._hidden(sealed, loader))
sources = api_whatif._sources(sealed, cards, started, asyncio.get_running_loop())
for metric in card["metrics"]:
again = recompute.recompute(metric, recompute.Changes(), sources)
again = await asyncio.to_thread(recompute.recompute, metric, recompute.Changes(), sources)
out.append((node_id, metric["id"], again["status"], again["new"], metric["value"]))
return out
@@ -496,3 +493,339 @@ async def test_downstream_cards_are_flagged_only_when_something_changes(client,
[flagged] = body["affected_cards"]
assert (flagged["node_id"], flagged["caliber"], flagged["version"], flagged["metrics"], flagged["direct"]) == (
"yearly", "年口径", "v2", ["year"], True)
# --------------------------------------------------------------------------
# 排除行:说不准受不受影响的指标不报「无变化」
# --------------------------------------------------------------------------
async def test_excluding_rows_of_an_old_run_never_says_unchanged(client, monkeypatch):
"""老运行的 vars.res.rows 没存完整值:排除 cell 那一格以外的行,平均每单会变(分母少了一行),却算不出来。
不能报「60.05元 → 60.05元 无变化」,报告里引用它的地方也不能报「不受影响」。"""
from app.engine.nodes import metrics as metrics_node
async def nothing(*args, **kwargs):
return None
monkeypatch.setattr(metrics_node, "_store_value", nothing)
row = await run_graph(monkeypatch, query_graph())
snapshot = await snapshot_of(row.id)
body = await whatif(client, row.id, {"card": "card", "metric": "per", "report": "write",
"overrides": {"exclude_rows": {snapshot: [3]}}})
[per] = body["metrics"]
assert per["status"] == "unsupported" and per["changed"] is None and per["new"] is None, per
assert "vars.res.rows" in per["reason"]
[seg] = body["segments"]
assert seg["metric"] == "per" and seg["rendered_new"] is None and seg["changed"] is None
cnt = by_id(await whatif(client, row.id, {"card": "card", "overrides": {"exclude_rows": {snapshot: [3]}}}))["cnt"]
assert cnt["status"] == "unsupported" and cnt["changed"] is None
async def test_only_inputs_computed_after_the_snapshot_came_back_are_doubted(client, monkeypatch):
"""写在查询之前的容器(early)排除行影响不到它;查询之后才算出来、又对不上任何快照的(late)说不准。
按位置取自整份结果的一格(vars.res.rows[0][2])同样说不准:排除第 0 行后它取到的是另一行。"""
metrics = [
{"id": "m_early", "name": "加早", "decimals": 1, "expression": f"round({AMOUNT5} + sum(vars.early.items), 1)"},
{"id": "m_late", "name": "加晚", "decimals": 1, "expression": f"round({AMOUNT5} + sum(vars.late.items), 1)"},
{"id": "first", "name": "首单占比", "decimals": 2,
"expression": "round(vars.res.rows[0][2] / len(vars.res.rows), 2)"},
]
row = await run_graph(monkeypatch, chain(
node("start", "input"),
node("early_n", "transform", mode="expression", expression="{'items': [1.5, 2.5]}", assign_to="early"),
node("fetch", "tool", tool="db_query__shop", args={"sql": SQL}),
node("parse", "transform", mode="json", template="{{ nodes.fetch }}", assign_to="res"),
node("late_n", "transform", mode="expression", expression="{'items': [1.5, 2.5]}", assign_to="late"),
node("card", "metrics", caliber="推演口径", metrics=metrics),
node("write", "report", instructions="写周报", on_violation="flag"),
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}])),
text="加早 [[m:m_early]],加晚 [[m:m_late]],首单占比 [[m:first]]。")
snapshot = await snapshot_of(row.id)
assert [(m, s, new == old) for _, m, s, new, old in await faithful(row.id)] == [
("m_early", "ok", True), ("m_late", "ok", True), ("first", "ok", True)]
metrics = by_id(await whatif(client, row.id, {"card": "card", "overrides": {"exclude_rows": {snapshot: [0]}}}))
assert metrics["m_early"]["status"] == "unchanged" and metrics["m_early"]["changed"] is False
assert metrics["m_late"]["status"] == "unsupported" and "vars.late.items" in metrics["m_late"]["reason"]
assert "对不上任何查询快照" in metrics["m_late"]["reason"]
assert metrics["first"]["status"] == "unsupported" and metrics["first"]["new"] is None
assert "vars.res.rows[0][2]" in metrics["first"]["reason"]
# --------------------------------------------------------------------------
# 查询快照读不出来、封存核对没过、事后加的遮罩、被改过的指标集
# --------------------------------------------------------------------------
def artifact_file(artifact: str):
return settings.data_dir / "artifacts" / artifact[:2] / f"{artifact}.json"
@pytest.fixture
def damage():
"""删掉、改坏工件文件,测完放回原样:工件按内容寻址,同样的查询、同样的卡在后面的测试里还是这一份文件,
put_json 见文件在就不再写——不放回去,后面的测试拿到的就是坏的。"""
saved: list[tuple] = []
def apply(artifact: str, change=None) -> None:
path = artifact_file(artifact)
text = path.read_text("utf-8")
saved.append((path, text))
if change is None:
path.unlink()
return
content = json.loads(text)
change(content)
path.write_text(json.dumps(content, ensure_ascii=False), encoding="utf-8")
yield apply
for path, text in reversed(saved):
path.write_text(text, encoding="utf-8")
def tamper_cell(content):
content["rows"][5][2] = 1.0
@pytest.mark.parametrize("change, words", [(None, "无法读取"), (tamper_cell, "哈希")])
async def test_an_unreadable_snapshot_withholds_the_cells_on_it(client, monkeypatch, damage, change, words):
"""快照文件不在了、被改过:那一格遮没遮核对不了,卡上记着的值也不给、不能改,代入式也不给。"""
row = await run_graph(monkeypatch, query_graph())
damage(await snapshot_of(row.id), change)
body = await whatif(client, row.id, {"card": "card"})
metrics = by_id(body)
[cell] = metrics["amount5"]["inputs"]
assert cell["old"] is None and cell["new"] is None and cell["editable"] is False and cell.get("withheld") is True
assert metrics["amount5"]["substituted"] is None and metrics["per"]["substituted"] is None
assert "600.5" not in json.dumps([m["inputs"] for m in body["metrics"]])
reasons = {(u["metric"], u["input"]): u["reason"] for u in body["unsupported"]}
assert words in reasons[("amount5", AMOUNT5)]
resp = await client.post(f"/api/runs/{row.id}/evidence/whatif",
json={"card": "card", "overrides": {"inputs": {AMOUNT5: 1}}})
assert resp.status_code == 422 and "快照" in resp.json()["detail"], resp.text
async def test_a_mask_set_after_the_run_applies_even_if_the_snapshot_is_gone(client, monkeypatch, shop, damage):
row = await run_graph(monkeypatch, query_graph())
damage(await snapshot_of(row.id))
await set_source(shop, {"mask_columns": ["amount"]})
[cell] = by_id(await whatif(client, row.id, {"card": "card"}))["amount5"]["inputs"]
assert cell["masked"] is True and cell["old"] is None
async def test_masks_added_after_the_run_still_apply(client, monkeypatch, shop):
"""遮罩是查询当时记下的加数据源现在设的:跑完之后才给 amount 加的遮罩照样生效。"""
row = await run_graph(monkeypatch, query_graph())
snapshot = await snapshot_of(row.id)
await set_source(shop, {"mask_columns": ["amount"]})
body = await whatif(client, row.id, {"card": "card"})
metrics = by_id(body)
[cell] = metrics["amount5"]["inputs"]
assert cell["masked"] is True and cell["old"] is None and cell["editable"] is False
assert metrics["amount5"]["substituted"] is None and "row_span" not in cell
resp = await client.post(f"/api/runs/{row.id}/evidence/whatif",
json={"card": "card", "overrides": {"inputs": {AMOUNT5: 1}}})
assert resp.status_code == 422 and "遮罩" in resp.json()["detail"]
cnt = by_id(await whatif(client, row.id, {"card": "card", "overrides": {"exclude_rows": {snapshot: [1]}}}))["cnt"]
assert cnt["status"] == "masked" and cnt["new"] is None
async def test_a_broken_seal_is_not_called_sealed(client, monkeypatch):
"""封存范围里随便一条事件被改过:照样能推演,但 sealed 为假,界面据此说「封存核对未通过」。"""
row = await run_graph(monkeypatch, query_graph())
async with SessionLocal() as session:
started = (await session.execute(select(RunEvent).where(
RunEvent.run_id == row.id, RunEvent.type == "node.started", RunEvent.node_id == "start"))).scalar_one()
started.data = {**(started.data or {}), "note": "事后改过"}
await session.commit()
body = await whatif(client, row.id, {"card": "card", "overrides": {"inputs": {AMOUNT5: 1000}}})
assert body["sealed"] is False and body["seal"] == {"sealed": True, "ok": False}
assert by_id(body)["amount5"]["new"] == 1000
page = (await client.get(f"/api/runs/{row.id}/evidence/whatif/rows",
params={"artifact": await snapshot_of(row.id), "limit": 1})).json()
assert page["sealed"] is False and len(page["rows"]) == 1
async def test_a_tampered_metric_set_is_refused(client, monkeypatch, damage):
row = await run_graph(monkeypatch, query_graph())
card = (await whatif(client, row.id, {"card": "card"}))["card"]["artifact"]
damage(card, lambda content: content["metrics"][0].update(value=1.5))
resp = await client.post(f"/api/runs/{row.id}/evidence/whatif", json={"card": "card"})
assert resp.status_code == 409 and resp.json()["code"] == "whatif_card_tampered", resp.text
# --------------------------------------------------------------------------
# 多份报告、合并查询、循环
# --------------------------------------------------------------------------
async def test_two_reports_are_kept_apart(client, monkeypatch):
"""两个报告节点都引用了这张卡:指定 report 时只列那一份里的地方,不指定时两份都列。"""
row = await run_graph(monkeypatch, chain(
*query_graph()["nodes"][:4],
node("write", "report", instructions="写周报", on_violation="flag"),
node("brief", "report", instructions="写简报", on_violation="flag"),
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"},
{"name": "简报", "value": "{{ nodes.brief.text }}"}])))
changes = {"overrides": {"inputs": {AMOUNT5: 1000}}}
both = await whatif(client, row.id, {"card": "card", **changes})
assert sorted({s["report"] for s in both["segments"]}) == ["brief", "write"]
one = await whatif(client, row.id, {"card": "card", "report": "brief", **changes})
assert one["report"] == "brief" and one["segments"] and {s["report"] for s in one["segments"]} == {"brief"}
assert len(one["segments"]) == len([s for s in both["segments"] if s["report"] == "brief"])
async def test_a_merged_query_with_a_masked_column(client, monkeypatch, shop):
"""合并查询不是数据源:遮罩按它合并的那几个数据源算,跑完之后才加的遮罩也一样。遮着的列不给值、不能推演。"""
merged = "cell(nodes.merged, 5, 'amount')"
row = await run_graph(monkeypatch, chain(
node("start", "input"),
node("q_amount", "tool", tool="db_query__shop", args={"sql": "SELECT id, amount FROM orders ORDER BY id"}),
node("q_region", "tool", tool="db_query__shop", args={"sql": "SELECT id, region FROM orders ORDER BY id"}),
node("merged", "merge", inputs={"a": "q_amount", "r": "q_region"},
sql="SELECT a.id, r.region, a.amount FROM a JOIN r ON a.id = r.id ORDER BY a.id"),
node("card", "metrics", caliber="推演口径", metrics=[
{"id": "m5", "name": "第六单", "decimals": 1, "expression": merged}]),
node("write", "report", instructions="写周报", on_violation="flag"),
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}])),
text="第六单 [[m:m5]]。")
[cell] = by_id(await whatif(client, row.id, {"card": "card"}))["m5"]["inputs"]
assert cell["editable"] is True and cell["old"] == 600.5
await set_source(shop, {"mask_columns": ["amount"]})
body = await whatif(client, row.id, {"card": "card"})
[cell] = by_id(body)["m5"]["inputs"]
assert cell["masked"] is True and cell["old"] is None and by_id(body)["m5"]["substituted"] is None
resp = await client.post(f"/api/runs/{row.id}/evidence/whatif",
json={"card": "card", "overrides": {"inputs": {merged: 1}}})
assert resp.status_code == 422 and "遮罩" in resp.json()["detail"]
async def test_a_card_inside_a_loop_uses_the_node_outputs_from_before_that_execution(client, monkeypatch):
"""循环体里口径卡先算、fill 后算:第二轮的卡读到的是第一轮 fill 的产出([1, 10]),不是循环结束时的([2, 20])。
推演按那次执行开始之前的产出还原。"""
row = await run_graph(monkeypatch, {
"nodes": [
node("start", "input"),
node("lp", "loop", mode="foreach", items="[1, 2]", max_iterations=5),
node("card", "metrics", caliber="循环口径", on_missing="null", metrics=[
{"id": "s", "name": "合计", "expression": "sum(nodes.fill.items)"}]),
node("fill", "transform", mode="expression", expression="{'items': [vars.item, vars.item * 10]}"),
node("write", "report", instructions="写周报", on_violation="flag"),
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}]),
],
"edges": [
{"source": "start", "target": "lp"},
{"source": "lp", "target": "card", "sourceHandle": "body"},
{"source": "card", "target": "fill"},
{"source": "fill", "target": "lp"},
{"source": "lp", "target": "write", "sourceHandle": "done"},
{"source": "write", "target": "out"},
],
}, text="合计 [[m:s]]。")
body = await whatif(client, row.id, {"card": "card", "report": "write"})
[s] = body["metrics"]
assert s["old"] == 11 and s["inputs"][0]["old"] == "[2 项]"
assert [(m, st, new == old) for _, m, st, new, old in await faithful(row.id)] == [("s", "ok", True)]
# --------------------------------------------------------------------------
# 推演里能勾的行
# --------------------------------------------------------------------------
async def test_inputs_carry_the_rows_they_use(client, monkeypatch):
row = await run_graph(monkeypatch, query_graph())
metrics = by_id(await whatif(client, row.id, {"card": "card"}))
[cell] = metrics["amount5"]["inputs"]
[rows] = metrics["cnt"]["inputs"]
assert cell["row_span"] == [5, 5] and rows["row_span"] == [0, 9]
async def test_rows_of_a_sealed_snapshot_can_be_paged_with_masks_applied(client, monkeypatch, shop):
"""推演区里给整份结果勾行:按页取封存范围内快照的行,遮罩列写「已遮罩」。只读。"""
row = await run_graph(monkeypatch, query_graph())
snapshot = await snapshot_of(row.id)
url = f"/api/runs/{row.id}/evidence/whatif/rows"
before = await footprint(row.id)
page = (await client.get(url, params={"artifact": snapshot, "offset": 4, "limit": 3})).json()
assert page["artifact"] == snapshot and page["columns"] == ["id", "region", "amount"]
assert page["row_index"] == [4, 5, 6] and page["row_offset"] == 4 and page["total_rows"] == 10
assert page["rows"] == [[5, "华东", 500.5], [6, "华南", 600.5], [7, "华北", 700.5]] and page["masked"] == []
assert page["sealed"] is True and page["hash_ok"] is True
await set_source(shop, {"mask_columns": ["amount"]})
page = (await client.get(url, params={"artifact": snapshot, "limit": 2})).json()
assert page["rows"] == [[1, "华南", "已遮罩"], [2, "华北", "已遮罩"]] and page["masked"] == ["amount"]
assert await footprint(row.id) == before
# 越过末行的页是空的;不属于这次运行的快照、写错的参数说清楚
assert (await client.get(url, params={"artifact": snapshot, "offset": 10})).json()["rows"] == []
other = await run_graph(monkeypatch, query_graph(sql="SELECT id, region, amount FROM orders ORDER BY id DESC"))
for params, words in [({"artifact": await snapshot_of(other.id)}, "封存范围"), ({}, "artifact"),
({"artifact": snapshot, "limit": "很多"}, "limit"), ({"artifact": snapshot, "offset": -1}, "offset")]:
resp = await client.get(url, params=params)
assert resp.status_code == 422 and words in resp.json()["detail"], (params, resp.text)
async def test_rows_of_an_unreadable_snapshot_are_not_served(client, monkeypatch, damage):
row = await run_graph(monkeypatch, query_graph())
snapshot = await snapshot_of(row.id)
damage(snapshot)
page = (await client.get(f"/api/runs/{row.id}/evidence/whatif/rows", params={"artifact": snapshot})).json()
assert page["rows"] == [] and page["hash_ok"] is None and "无法读取" in page["note"]
# --------------------------------------------------------------------------
# 下游口径卡、取整
# --------------------------------------------------------------------------
async def test_downstream_cards_through_an_intermediate_node_are_flagged(client, monkeypatch):
row = await run_graph(monkeypatch, chain(
node("start", "input"),
node("fetch", "transform", mode="expression", expression="{'gmv': 100.0}", assign_to="kpi"),
node("card", "metrics", caliber="周口径", caliber_version="v1", metrics=[
{"id": "gmv", "name": "销售额", "decimals": 1, "expression": "vars.kpi.gmv"}]),
node("mid", "transform", mode="expression", expression="{'g': nodes.card.metrics[0].value * 2}", assign_to="t"),
node("yearly", "metrics", caliber="年口径", caliber_version="v2", metrics=[
{"id": "year", "name": "年化", "decimals": 1, "expression": "vars.t.g * 26"}]),
node("out", "output", fields=[{"name": "r", "value": "{{ nodes.yearly.text }}"}])))
body = await whatif(client, row.id, {"card": "card", "overrides": {"inputs": {"vars.kpi.gmv": 120}}})
[flagged] = body["affected_cards"]
assert (flagged["node_id"], flagged["metrics"], flagged["direct"]) == ("yearly", ["year"], False)
async def test_downstream_cards_are_flagged_when_the_change_cannot_be_followed(client, monkeypatch):
"""改动碰到了指标、它却推演不了(还原不了、遮罩):新值不知道,下游同样可能受影响。"""
from app.engine.nodes import metrics as metrics_node
async def nothing(*args, **kwargs):
return None
monkeypatch.setattr(metrics_node, "_store_value", nothing)
row = await run_graph(monkeypatch, query_graph(between=[node("yearly", "metrics", caliber="年口径", metrics=[
{"id": "year", "name": "年化", "decimals": 1, "expression": "nodes.card.metrics[2].value * 52"}])]))
body = await whatif(client, row.id, {"card": "card", "metric": "per", "overrides": {"inputs": {AMOUNT5: 1000}}})
assert body["metrics"][0]["status"] == "unsupported"
assert [c["node_id"] for c in body["affected_cards"]] == ["yearly"]
TRICKY = [(2.675, 2), (0.125, 2), (-1.5, 0), (1234.5, -1), (-1250.0, -2), (0.0005, 3), (99.995, 2), (-0.045, 2)]
async def test_rounding_matches_the_card_for_tricky_values(monkeypatch):
"""取整和口径卡逐个对上:二进制里落在半中间的数、负数、负的小数位(取整到十位、百位)。run_metrics 的取整规则哪天
改了(比如换成四舍五入),这里会对不上。"""
import random
rng = random.Random(20261005)
pairs = TRICKY + [(round(rng.uniform(-1e6, 1e6), rng.randint(0, 6)), rng.randint(-3, 6)) for _ in range(24)]
kpi = "{" + ", ".join(f"'v{i}': {v!r}" for i, (v, _) in enumerate(pairs)) + "}"
row = await run_graph(monkeypatch, chain(
node("start", "input"),
node("fetch", "transform", mode="expression", expression=kpi, assign_to="kpi"),
node("card", "metrics", caliber="取整口径", metrics=[
{"id": f"m{i}", "name": f"m{i}", "format": "plain", "decimals": d, "expression": f"vars.kpi.v{i} * 1.0"}
for i, (_, d) in enumerate(pairs)]),
node("out", "output", fields=[{"name": "r", "value": "{{ nodes.card.text }}"}])))
again = await faithful(row.id)
assert len(again) == len(pairs)
assert [(m, s) for _, m, s, new, old in again if s != "ok" or new != old] == []
+87
View File
@@ -0,0 +1,87 @@
"""数字推演取哪一份:同一张口径卡执行过几次时推演哪一次、那次执行之前各节点的产出和跑完的时刻、快照什么时候交回。
照 api/evidence.py 的 _Sealed 手写事件(虚构的工件 id),不起运行、不碰库:真跑的循环图里报告总是引用最后一次执行,
节点也常常不会在口径卡之后再跑,这几条边界只有手写的事件才拿得到。
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
import pytest
from app.api.coded import CodedHTTPException
from app.api.evidence import _Sealed
from app.api.whatif import _card_of, _finished_before, _outputs_before, _returned
FILL1, FILL2, A1, A2, OUT1, OUT2, Q1 = ("f1" * 32, "f2" * 32, "a1" * 32, "a2" * 32, "c1" * 32, "c2" * 32, "d1" * 32)
@dataclass
class Report:
node_id: str
doc: dict[str, Any] | None = field(default_factory=dict)
def sealed(events: list[tuple[int, str, str | None, dict[str, Any]]]) -> _Sealed:
return _Sealed(run_id="r1", graph={"nodes": [{"id": "card", "type": "metrics"}]},
seal={"sealed": True, "ok": True, "manifest_seq": 99, "legacy": False}, events=events)
#: 循环两轮:fill → card,fill → card。第一轮的卡读到 FILL1,第二轮读到 FILL2
LOOP = [
(1, "node.started", "fill", {}),
(2, "tool.end", "fill", {"query_artifact": Q1}),
(3, "node.finished", "fill", {"artifact": FILL1}),
(4, "node.started", "card", {}),
(5, "node.finished", "card", {"artifact": OUT1, "evidence": [{"kind": "metric_set", "artifact": A1}]}),
(6, "node.started", "fill", {}),
(7, "node.finished", "fill", {"artifact": FILL2, "evidence": [{"kind": "query", "artifact": Q1}]}),
(8, "node.started", "card", {}),
(9, "node.finished", "card", {"artifact": OUT2, "evidence": [{"kind": "metric_set", "artifact": A2}]}),
]
CARDS = [("card", A1, {"metrics": [{"id": "s", "value": 1}]}), ("card", A2, {"metrics": [{"id": "s", "value": 2}]})]
def cites(*artifacts: str, node: str = "write") -> Report:
return Report(node, {"catalog": {f"m:{i}": {"kind": "metric", "node_id": "card", "artifact": a}
for i, a in enumerate(artifacts)}})
def test_the_execution_the_report_cites_is_chosen_over_the_last_one():
artifact, card, started = _card_of(sealed(LOOP), CARDS, "card", [cites(A1)], None)
assert (artifact, card["metrics"][0]["value"], started) == (A1, 1, 4)
def test_without_a_citation_the_last_execution_is_used():
assert _card_of(sealed(LOOP), CARDS, "card", [cites()], None)[0] == A2
assert _card_of(sealed(LOOP), CARDS, "card", [], None)[0] == A2
def test_a_chosen_report_decides_which_execution_is_used():
"""两份报告各引用一次执行:指定了 report 就按那一份,不指定按任意一份引用的(取后一次)。"""
reports = [cites(A1, node="weekly"), cites(A2, node="daily")]
assert _card_of(sealed(LOOP), CARDS, "card", reports, "weekly")[0] == A1
assert _card_of(sealed(LOOP), CARDS, "card", reports, "daily")[0] == A2
assert _card_of(sealed(LOOP), CARDS, "card", reports, None)[0] == A2
def test_a_card_whose_content_does_not_verify_is_refused():
with pytest.raises(CodedHTTPException) as caught:
_card_of(sealed(LOOP), CARDS[1:], "card", [cites(A1)], None)
assert caught.value.status_code == 409
def test_node_outputs_and_finish_times_stop_at_the_chosen_execution():
"""第一轮的卡(第 4 条开始)读到的是 FILL1:第二轮 fill 的产出在它之后,不算。"""
events = sealed(LOOP)
assert _outputs_before(events, 4) == {"fill": FILL1}
assert _finished_before(events, 4) == {"fill": 3}
assert _outputs_before(events, 8) == {"fill": FILL2, "card": OUT1}
assert _finished_before(events, 8) == {"fill": 7, "card": 5}
def test_a_snapshot_counts_as_returned_the_first_time_it_shows_up():
assert _returned(sealed(LOOP)) == {Q1: 2}
merged_only = [(3, "node.finished", "merged", {"artifact": OUT1, "evidence": [{"kind": "query", "artifact": Q1}]})]
assert _returned(sealed(merged_only)) == {Q1: 3}