mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-17 06:31:58 +00:00
口径增加内容指纹:指标集与账本候选记录定义哈希
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
"""口径的内容指纹:同一位置的口径定义变没变,按定义本身的哈希认,不看运行时算出来的值。
|
||||
|
||||
口径的身份原先是「工作流 id@版本」(ledger/labels.caliber_key)。同一版不会变(版本快照不可变),但复制、改名、迁移之后
|
||||
就认不回是同一份口径了;后续做「定义变更影响分析」也需要一份内容指纹。这里给的就是这份指纹:
|
||||
|
||||
- definition_hash:一张口径卡的指标定义(caliber、caliber_version 和每个指标的语义字段)的 sha256;
|
||||
- version_hash:一份口径版本里各卡的内容哈希排序后再取 sha256,也就是整份版本的定义指纹;
|
||||
- card_hash_of:从 {node_id, metrics, caliber?, caliber_version?} 这种卡形状取哈希(运行时指标卡、账本里源头口径卡的
|
||||
两种写法共用)。
|
||||
|
||||
**只取语义字段。** 指标只留 id / name / expression / unit / decimals / format / kind / definition / params 里存在的键,
|
||||
按 id 排序(顺序不影响哈希);空字符串、None、空的列表和字典不写进规范 JSON。值、渲染后的字、输入出处、状态这些都是
|
||||
运行时产物,不进哈希——同一份定义重跑一次,或者补了无关的字段,哈希不变。
|
||||
|
||||
**这是内容指纹,不是跨图稳定的 ID。** 表达式里写的是节点 id(cell(nodes.fetch, 0, 'gmv')),取数节点改了名,式子跟着改,
|
||||
哈希就会变;复制到另一张图、指标定义一字不差时哈希相同,正是用来认回同一份定义。要跨图认同一份口径,仍靠版本引用
|
||||
(caliber_from)和「工作流@版本」。
|
||||
|
||||
**口径名、版本进不进哈希。** 两个函数的入参可以是空串:运行时指标卡和账本两侧算指标集指纹时都不查库、不拿名字,
|
||||
只按 metrics 算(空串时规范 JSON 里省略这两个键),这样两边的哈希才对得上。名字、版本要不要算进去由调用方决定。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
from typing import Any
|
||||
|
||||
#: 指标的语义字段:只有这几样算定义,运行时值(value、rendered、inputs……)不参与哈希
|
||||
SEMANTIC_FIELDS = ("id", "name", "expression", "unit", "decimals", "format", "kind", "definition", "params")
|
||||
|
||||
|
||||
def _blank(value: Any) -> bool:
|
||||
"""空值、缺省键:None、空字符串、空的列表和字典不写进规范 JSON。0、False 是有意义的取值,照写。"""
|
||||
return value is None or value == "" or (isinstance(value, (list, dict, tuple)) and not value)
|
||||
|
||||
|
||||
def _metric_content(metric: Any) -> dict[str, Any]:
|
||||
"""一个指标的语义内容:只留 SEMANTIC_FIELDS 里存在的键,空值略去。"""
|
||||
if not isinstance(metric, dict):
|
||||
return {}
|
||||
return {field: metric[field] for field in SEMANTIC_FIELDS if field in metric and not _blank(metric[field])}
|
||||
|
||||
|
||||
def definition_hash(caliber: str, caliber_version: str, metrics: Iterable[dict[str, Any]]) -> str:
|
||||
"""一张口径卡的指标定义 → 内容指纹。指标按 id 排序,顺序不影响哈希;caliber / caliber_version 为空时不进规范 JSON
|
||||
(运行时和账本两侧只按 metrics 算,见模块说明)。"""
|
||||
from app.core.artifact_store import canonical_json, content_hash
|
||||
|
||||
content: dict[str, Any] = {}
|
||||
if not _blank(caliber):
|
||||
content["caliber"] = str(caliber)
|
||||
if not _blank(caliber_version):
|
||||
content["caliber_version"] = str(caliber_version)
|
||||
kept = [_metric_content(metric) for metric in metrics or []]
|
||||
content["metrics"] = sorted(kept, key=lambda metric: str(metric.get("id") or ""))
|
||||
return content_hash(canonical_json(content))
|
||||
|
||||
|
||||
def card_hash_of(card: dict[str, Any]) -> str:
|
||||
"""一张口径卡(card)的内容指纹:{node_id, metrics, caliber?, caliber_version?}。没有 caliber 时按空串算
|
||||
(即只按 metrics)。"""
|
||||
if not isinstance(card, dict):
|
||||
return definition_hash("", "", [])
|
||||
return definition_hash(str(card.get("caliber") or ""), str(card.get("caliber_version") or ""),
|
||||
card.get("metrics") or [])
|
||||
|
||||
|
||||
def version_hash(card_hashes: Iterable[str]) -> str:
|
||||
"""一份口径版本的内容指纹:各卡的内容哈希(card_hash_of 的结果)排序后取 sha256,和卡片的先后无关。"""
|
||||
from app.core.artifact_store import content_hash
|
||||
|
||||
return content_hash("\n".join(sorted(str(h) for h in card_hashes or () if h)))
|
||||
|
||||
|
||||
__all__ = ["SEMANTIC_FIELDS", "card_hash_of", "definition_hash", "version_hash"]
|
||||
@@ -10,6 +10,7 @@ from typing import Any
|
||||
from app.core import artifact_store
|
||||
from app.core.errors import describe_exception
|
||||
from app.core.events import EventType
|
||||
from app.data.caliber_identity import card_hash_of
|
||||
from app.engine.context import NodeContext, NodeError
|
||||
from app.engine.evidence import (
|
||||
DEFAULT_FORMAT,
|
||||
@@ -71,6 +72,9 @@ async def run_metrics(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
caliber_raw, caliber_version = pinned["caliber"], pinned["caliber_version"]
|
||||
if not definitions:
|
||||
raise NodeError(ctx.node.id, "口径卡没有定义任何指标")
|
||||
# 定义的内容指纹(data/caliber_identity):同一位置的口径定义变没变按它认,运行时算出来的值不进哈希。
|
||||
# 口径名和版本不参与(这里只按指标算),和账本里的源头口径卡两侧对得上
|
||||
definition_hash = card_hash_of({"metrics": definitions})
|
||||
|
||||
tctx = template_context(state)
|
||||
caliber = ctx.render_str(caliber_raw, state)
|
||||
@@ -236,6 +240,7 @@ async def run_metrics(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
"kind": "metric_set",
|
||||
"caliber": caliber,
|
||||
"caliber_version": caliber_version,
|
||||
"definition_hash": definition_hash,
|
||||
"metrics": metrics,
|
||||
}
|
||||
if source:
|
||||
@@ -262,6 +267,7 @@ async def run_metrics(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
"artifact": artifact,
|
||||
"caliber": caliber,
|
||||
"version": caliber_version,
|
||||
"definition_hash": definition_hash,
|
||||
"metrics": [m["id"] for m in metrics],
|
||||
**({"source": source} if source else {}),
|
||||
}]
|
||||
@@ -1279,7 +1285,8 @@ async def _store(card: dict[str, Any], caliber: str, version: str, ctx: NodeCont
|
||||
|
||||
try:
|
||||
return await put_json(card, kind="metric_set", run_id=ctx.run.run_id, node_id=ctx.node.id,
|
||||
meta={"caliber": caliber, "version": version})
|
||||
meta={"caliber": caliber, "version": version,
|
||||
"definition_hash": str(card.get("definition_hash") or "")})
|
||||
except Exception as e: # noqa: BLE001
|
||||
ctx.emit(EventType.LOG, level="warn", code="evidence_store_failed",
|
||||
message=f"口径卡的指标保存失败({describe_exception(e)}),报告将无法引用这些指标")
|
||||
|
||||
@@ -86,10 +86,17 @@ def _related(query: Mapping[str, Any], *, reason: str | None = None) -> dict[str
|
||||
|
||||
|
||||
def _caliber(cand: Mapping[str, Any], **extra: Any) -> dict[str, Any]:
|
||||
return {"kind": "caliber", "how": "composed" if cand.get("how") == "composed" else "independent",
|
||||
"caliber": cand.get("caliber"),
|
||||
"workflow_id": cand.get("workflow_id"), "version": cand.get("version"), "level": cand.get("level"),
|
||||
"binding": dict(cand.get("binding") or {}), **extra}
|
||||
"""来自口径的标签。定义的内容指纹取自候选:版本级的 caliber_hash,或者候选自带 / extra 覆盖的 definition_hash
|
||||
(指标标签按它所在的那张卡的哈希覆盖版本级,见 metric_label)。"""
|
||||
out: dict[str, Any] = {
|
||||
"kind": "caliber", "how": "composed" if cand.get("how") == "composed" else "independent",
|
||||
"caliber": cand.get("caliber"),
|
||||
"workflow_id": cand.get("workflow_id"), "version": cand.get("version"), "level": cand.get("level"),
|
||||
"binding": dict(cand.get("binding") or {}),
|
||||
}
|
||||
if hash := cand.get("definition_hash") or cand.get("caliber_hash"):
|
||||
out["definition_hash"] = hash
|
||||
return {**out, **extra}
|
||||
|
||||
|
||||
def _proven(query: Mapping[str, Any]) -> list[Mapping[str, Any]]:
|
||||
@@ -358,7 +365,10 @@ def metric_label(card: Mapping[str, Any] | None, metric_id: Any, queries: Mappin
|
||||
if theirs == mine), None)
|
||||
if hit is None:
|
||||
continue
|
||||
label = {**_caliber(chosen[0], card=hit[0], metric=hit[1]), "binding": binding,
|
||||
# 指标按它对上的那张卡的哈希记(版本里不止一张卡时版本级哈希不够细);版本信息里没有这张卡的哈希就保留候选里的
|
||||
card_hash = ((calibers.get(key) or {}).get("card_hashes") or {}).get(hit[0])
|
||||
label = {**_caliber(chosen[0], card=hit[0], metric=hit[1],
|
||||
**({"definition_hash": card_hash} if card_hash else {})), "binding": binding,
|
||||
"queries": [str(i["artifact"]) for i in from_queries]}
|
||||
if any(c.get("how") == "composed" for c in chosen):
|
||||
# 几个输入里有一个是助手选用的,整个指标就按助手选用说(不能说成都是独立写出来的)
|
||||
|
||||
@@ -69,6 +69,7 @@ from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.data.caliber_identity import card_hash_of, version_hash
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import DataSource, Run, Workflow, WorkflowVersion
|
||||
from app.ledger import labels as ledger_labels
|
||||
@@ -154,7 +155,9 @@ class Fetch:
|
||||
class Caliber:
|
||||
"""一个工作流最新的已发布、未退役版本(只收里面有查库节点的)。fields 是入口节点的字段原样(取缺省值用,不进工件),
|
||||
cards 是源头口径卡的节点 id,expressions 是这些卡上每个指标换成记号后的式子(labels.card_expressions),reads 是每个
|
||||
指标读了哪几个取数节点的哪几列(labels.card_reads:口径一侧按它投影,不进工件)。"""
|
||||
指标读了哪几个取数节点的哪几列(labels.card_reads:口径一侧按它投影,不进工件)。card_hashes 是每张源头口径卡的内容
|
||||
指纹(节点 id → data/caliber_identity.card_hash_of),definition_hash 是整份版本的定义指纹(version_hash),用于发现
|
||||
同一位置的定义变化。"""
|
||||
|
||||
workflow_id: str
|
||||
version: int
|
||||
@@ -165,6 +168,8 @@ class Caliber:
|
||||
cards: tuple[str, ...] = ()
|
||||
expressions: tuple[tuple[str, str, str], ...] = ()
|
||||
reads: tuple[tuple[str, str, tuple[tuple[str, str], ...]], ...] = ()
|
||||
card_hashes: dict[str, str] = field(default_factory=dict)
|
||||
definition_hash: str = ""
|
||||
|
||||
@property
|
||||
def key(self) -> str:
|
||||
@@ -174,6 +179,7 @@ class Caliber:
|
||||
"""工件里记的「考虑过的口径版本」:不放工作流名(会改)、不放缺省值。"""
|
||||
return {"caliber": self.key, "workflow_id": self.workflow_id, "version": self.version, "level": self.level,
|
||||
"graph_hash": self.graph_hash, "fetches": [f.node_id for f in self.fetches], "cards": list(self.cards),
|
||||
"definition_hash": self.definition_hash, "card_hashes": dict(self.card_hashes),
|
||||
"fields": [{"name": f.get("name"), "required": bool(f.get("required")),
|
||||
"has_default": f.get("default") not in (None, "")} for f in self.fields]}
|
||||
|
||||
@@ -207,7 +213,10 @@ def _table_keys(sql: str) -> frozenset[str]:
|
||||
|
||||
|
||||
def caliber_of(workflow_id: str, row: Any) -> Caliber | None:
|
||||
"""一个已发布版本 → Caliber;里面没有查库的调用工具节点(SQL 写在 args.sql 里)的不算口径的取数,返回 None。"""
|
||||
"""一个已发布版本 → Caliber;里面没有查库的调用工具节点(SQL 写在 args.sql 里)的不算口径的取数,返回 None。
|
||||
|
||||
顺手算定义的内容指纹:每张源头口径卡的哈希(data/caliber_identity.card_hash_of,只按指标定义算,卡上没有
|
||||
口径名)和整份版本的指纹(version_hash)。不查库、不拿名字。"""
|
||||
from app.apps.tree import node_map
|
||||
from app.tools.datasource import QUERY_PREFIX
|
||||
|
||||
@@ -229,11 +238,13 @@ def caliber_of(workflow_id: str, row: Any) -> Caliber | None:
|
||||
cards.append({"node_id": node_id, "metrics": [m for m in cfg.get("metrics") or [] if isinstance(m, dict)]})
|
||||
if not fetches:
|
||||
return None
|
||||
card_hashes = {str(card["node_id"]): card_hash_of(card) for card in cards}
|
||||
return Caliber(workflow_id=workflow_id, version=int(row.version), level=str(getattr(row, "level", None) or ""),
|
||||
graph_hash=getattr(row, "graph_hash", None), fetches=tuple(fetches), fields=tuple(fields),
|
||||
cards=tuple(c["node_id"] for c in cards),
|
||||
expressions=tuple(ledger_labels.card_expressions(cards)),
|
||||
reads=tuple(ledger_labels.card_reads(cards)))
|
||||
reads=tuple(ledger_labels.card_reads(cards)),
|
||||
card_hashes=card_hashes, definition_hash=version_hash(card_hashes.values()))
|
||||
|
||||
|
||||
async def load_calibers(session: Any) -> list[Caliber]:
|
||||
@@ -324,7 +335,7 @@ def _candidate(caliber: Caliber, fetch: Fetch, m: Any) -> dict[str, Any]:
|
||||
"""一份口径取数的比对结果,进工件。"""
|
||||
out: dict[str, Any] = {
|
||||
"caliber": caliber.key, "workflow_id": caliber.workflow_id, "version": caliber.version,
|
||||
"level": caliber.level, "node_id": fetch.node_id,
|
||||
"level": caliber.level, "caliber_hash": caliber.definition_hash, "node_id": fetch.node_id,
|
||||
"status": "proven" if m.equivalent else "related", "comparable": m.comparable,
|
||||
"via": m.level, "binding": dict(m.binding), "unbound": list(m.unbound),
|
||||
}
|
||||
@@ -340,7 +351,8 @@ def _candidate(caliber: Caliber, fetch: Fetch, m: Any) -> dict[str, Any]:
|
||||
|
||||
def _skipped(caliber: Caliber, fetch: Fetch, reason: str) -> dict[str, Any]:
|
||||
return {"caliber": caliber.key, "workflow_id": caliber.workflow_id, "version": caliber.version,
|
||||
"level": caliber.level, "node_id": fetch.node_id, "status": "skipped", "reason": reason}
|
||||
"level": caliber.level, "caliber_hash": caliber.definition_hash, "node_id": fetch.node_id,
|
||||
"status": "skipped", "reason": reason}
|
||||
|
||||
|
||||
def composed_nodes(graph: Any) -> dict[str, tuple[str, str]]:
|
||||
@@ -654,7 +666,8 @@ def compute(sealed: Any, calibers: list[Caliber], kinds: dict[str, str | None],
|
||||
referenced=referenced)
|
||||
used = {c["caliber"] for q in results for c in q["candidates"]}
|
||||
considered = [c for c in calibers if c.key in used]
|
||||
by_key = {c.key: {"level": c.level, "fields": c.summary()["fields"], "expressions": list(c.expressions)}
|
||||
by_key = {c.key: {"level": c.level, "fields": c.summary()["fields"], "expressions": list(c.expressions),
|
||||
"card_hashes": dict(c.card_hashes)}
|
||||
for c in considered}
|
||||
by_artifact = {q["artifact"]: q for q in results}
|
||||
# 合并查询的快照不比(read_queries 跳过了它),引用了它的格照实写为什么没比对;不进工件的 queries,统计不数它
|
||||
|
||||
@@ -74,11 +74,14 @@ def counts_of(queries: list[dict[str, Any]]) -> dict[str, int]:
|
||||
|
||||
|
||||
def meta_of(content: dict[str, Any]) -> dict[str, Any]:
|
||||
"""工件引用的 meta:{calibers: [等价已证明的「工作流@版本」], related: [相关的], counts}。"""
|
||||
"""工件引用的 meta:{calibers: [等价已证明的「工作流@版本」], related: [相关的], definition_hashes: {「工作流@版本」:
|
||||
定义的内容指纹}, counts}。calibers 保持「W@V」的写法(召回按旧版本反查、统计按种类数都照旧);定义指纹只收非空的,
|
||||
供后续做定义变更影响分析时按版本找。"""
|
||||
from app.ledger.labels import candidates_of
|
||||
|
||||
proven: list[str] = []
|
||||
related: list[str] = []
|
||||
hashes: dict[str, str] = {}
|
||||
for query in content.get("queries") or []:
|
||||
# 逐格证明了的口径也算(召回按旧版本反查、统计按种类数都只读这一份 meta)
|
||||
for cand in candidates_of(query):
|
||||
@@ -88,8 +91,12 @@ def meta_of(content: dict[str, Any]) -> dict[str, Any]:
|
||||
bucket = proven if cand.get("status") == "proven" else related
|
||||
if key not in bucket:
|
||||
bucket.append(key)
|
||||
if value := str(cand.get("caliber_hash") or ""):
|
||||
hashes.setdefault(key, value)
|
||||
related = [key for key in related if key not in proven]
|
||||
return {"calibers": sorted(proven), "related": sorted(related), "counts": counts_of(content.get("queries") or [])}
|
||||
return {"calibers": sorted(proven), "related": sorted(related),
|
||||
"definition_hashes": dict(sorted(hashes.items())),
|
||||
"counts": counts_of(content.get("queries") or [])}
|
||||
|
||||
|
||||
async def save(run_id: str, content: dict[str, Any], *, seal: tuple[int | None, str | None],
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
"""口径的内容指纹(app/data/caliber_identity.py、engine/nodes/metrics.py、ledger/match.py、ledger/labels.py)。
|
||||
|
||||
- definition_hash / card_hash_of:同一份定义两次哈希相同,指标顺序不影响;改 expression、name、unit 哈希就变;值、渲染
|
||||
后的字、输入出处这些运行时字段不进哈希;空的语义字段和缺省一样不写。
|
||||
- version_hash:一份版本里各卡哈希排序后取 sha256,和卡片先后无关。
|
||||
- 跑一张真的口径卡:metric_set 工件、节点产出、证据条目都带 definition_hash,工件 meta 也记。
|
||||
- caliber_of 对一个 WorkflowVersion 形状算出每张卡的哈希、整份版本的指纹;候选里记 caliber_hash,工件 meta 记
|
||||
definition_hashes;标签用候选里的哈希。
|
||||
- 已知边界:表达式里写节点 id,取数节点改名,式子跟着改,哈希就变(内容指纹,不是跨图稳定 ID)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from app.core import artifact_store
|
||||
from app.data.caliber_identity import card_hash_of, definition_hash, version_hash
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Artifact, Run, RunEvent
|
||||
from app.engine.runner import run_manager
|
||||
from app.ledger import labels as ledger_labels
|
||||
from app.ledger import match as ledger_match
|
||||
from app.ledger import store as ledger_store
|
||||
from tests import ledger_samples as S
|
||||
|
||||
#: 两张卡共用的指标定义(语义字段齐全,够看出每一栏进不进哈希)
|
||||
METRICS = [
|
||||
{"id": "gmv", "name": "销售额", "unit": "元", "decimals": 2, "format": "thousands",
|
||||
"expression": "cell(nodes.fetch, 0, 'gmv')"},
|
||||
{"id": "orders", "name": "订单数", "unit": "单", "format": "thousands",
|
||||
"expression": "cell(nodes.fetch, 0, 'n')"},
|
||||
]
|
||||
#: 运行时口径卡的定义:从 vars 取数,不碰数据源,跑一次很便宜
|
||||
KPI = "{'gmv': 45678.5, 'orders': 1234}"
|
||||
STANDARD = [
|
||||
{"id": "gmv", "name": "销售额", "unit": "元", "decimals": 1, "format": "thousands", "expression": "vars.kpi.gmv"},
|
||||
{"id": "orders", "name": "订单数", "unit": "单", "format": "thousands", "expression": "vars.kpi.orders"},
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def engine_up():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
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) -> Run:
|
||||
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:
|
||||
return row
|
||||
raise AssertionError(f"run {run_id} 没跑完:{row.status}")
|
||||
|
||||
|
||||
async def events(run_id: str, etype: str, node_id: str | None = None) -> list[RunEvent]:
|
||||
from sqlalchemy import select
|
||||
|
||||
async with SessionLocal() as session:
|
||||
q = select(RunEvent).where(RunEvent.run_id == run_id, RunEvent.type == etype)
|
||||
if node_id:
|
||||
q = q.where(RunEvent.node_id == node_id)
|
||||
return list((await session.execute(q.order_by(RunEvent.seq))).scalars())
|
||||
|
||||
|
||||
def caliber() -> ledger_match.Caliber:
|
||||
found = ledger_match.caliber_of("wf", SimpleNamespace(graph=S.caliber_graph(), version=1, level="governed",
|
||||
graph_hash="h"))
|
||||
assert found is not None
|
||||
return found
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 纯函数:什么进哈希、什么不进
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_the_same_definitions_hash_the_same():
|
||||
assert definition_hash("", "", METRICS) == definition_hash("", "", METRICS)
|
||||
assert definition_hash("周销售", "v1", METRICS) == definition_hash("周销售", "v1", METRICS)
|
||||
|
||||
|
||||
def test_metric_order_does_not_matter():
|
||||
assert definition_hash("", "", METRICS) == definition_hash("", "", list(reversed(METRICS)))
|
||||
|
||||
|
||||
def test_semantic_changes_change_the_hash():
|
||||
base = definition_hash("", "", METRICS)
|
||||
changed_expression = [{**METRICS[0], "expression": "cell(nodes.fetch, 0, 'gmv') * 2"}, METRICS[1]]
|
||||
changed_name = [{**METRICS[0], "name": "成交额"}, METRICS[1]]
|
||||
changed_unit = [{**METRICS[0], "unit": "万元"}, METRICS[1]]
|
||||
changed_decimals = [{**METRICS[0], "decimals": 0}, METRICS[1]]
|
||||
hashes = [base] + [definition_hash("", "", metrics) for metrics in
|
||||
(changed_expression, changed_name, changed_unit, changed_decimals)]
|
||||
assert len(set(hashes)) == len(hashes), "每一处语义字段的改动都应改变哈希"
|
||||
|
||||
|
||||
def test_runtime_values_do_not_change_the_hash():
|
||||
"""值、渲染后的字、输入出处这些是运行时产物:同一份定义重跑一次哈希不变。"""
|
||||
runtime = [{**metric, "value": 1.0, "rendered": "1.0元", "inputs": [{"path": "x"}], "substituted": "1 + 1",
|
||||
"recompute_ok": True, "status": "ok", "missing_reason": "无关字段"} for metric in METRICS]
|
||||
assert card_hash_of({"metrics": runtime}) == card_hash_of({"node_id": "card", "metrics": METRICS})
|
||||
|
||||
|
||||
def test_empty_semantic_fields_are_the_same_as_missing():
|
||||
"""空串、None 不写进规范 JSON:同一份定义补了个空的无关字段,哈希不变。"""
|
||||
sparse = [{"id": "gmv", "expression": "vars.kpi.gmv"}, {"id": "orders", "expression": "vars.kpi.orders"}]
|
||||
padded = [{**metric, "unit": "", "name": None, "params": {}} for metric in sparse]
|
||||
assert definition_hash("", "", sparse) == definition_hash("", "", padded)
|
||||
|
||||
|
||||
def test_the_caliber_name_and_version_only_count_when_given():
|
||||
assert card_hash_of({"metrics": METRICS}) == definition_hash("", "", METRICS)
|
||||
assert definition_hash("周销售", "", METRICS) != definition_hash("", "", METRICS)
|
||||
assert definition_hash("周销售", "v1", METRICS) != definition_hash("周销售", "v2", METRICS)
|
||||
|
||||
|
||||
def test_renaming_the_fetch_node_changes_the_hash():
|
||||
"""已知边界:表达式里写节点 id,改名后哈希就变——这是内容指纹,不是跨图稳定 ID。"""
|
||||
renamed = [{**METRICS[0], "expression": "cell(nodes.orders_query, 0, 'gmv')"}, METRICS[1]]
|
||||
assert definition_hash("", "", renamed) != definition_hash("", "", METRICS)
|
||||
|
||||
|
||||
def test_version_hash_ignores_card_order():
|
||||
a, b = definition_hash("", "", METRICS), definition_hash("", "", METRICS[:1])
|
||||
assert version_hash([a, b]) == version_hash([b, a])
|
||||
assert version_hash([a, b]) != version_hash([a])
|
||||
assert len(version_hash([])) == 64
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 跑一张真的口径卡:工件、节点产出、证据、meta
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_a_running_card_records_its_definition_hash():
|
||||
graph = chain(node("start", "input"),
|
||||
node("fetch", "transform", mode="expression", expression=KPI, assign_to="kpi"),
|
||||
node("card", "metrics", "周报口径卡", caliber="周报口径", caliber_version="v1", metrics=STANDARD),
|
||||
node("out", "output", fields=[{"name": "r", "value": "{{ nodes.card.text }}"}]))
|
||||
row = await wait((await run_manager.start(graph=graph, input_payload={})).id)
|
||||
assert row.status == "succeeded", row.error
|
||||
|
||||
[finished] = await events(row.id, "node.finished", "card")
|
||||
output = artifact_store.load(finished.data["artifact"]) # 节点产出(结果字典原样)
|
||||
card = artifact_store.load(output["artifact"]) # metric_set 工件
|
||||
expected = card_hash_of({"metrics": STANDARD})
|
||||
# 运行时算出来的 value / rendered / inputs 不进哈希:和只拿原始定义算的一样
|
||||
assert card["kind"] == "metric_set" and card["definition_hash"] == expected
|
||||
assert output["definition_hash"] == expected
|
||||
[entry] = finished.data["evidence"]
|
||||
assert entry["kind"] == "metric_set" and entry["definition_hash"] == expected
|
||||
async with SessionLocal() as session:
|
||||
ref = await session.get(Artifact, (output["artifact"], row.id))
|
||||
assert ref.meta["definition_hash"] == expected
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 账本一侧:caliber_of、候选、工件 meta、标签
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_caliber_of_fingerprints_each_source_card():
|
||||
found = caliber()
|
||||
metrics = S.caliber_graph()["nodes"][2]["data"]["config"]["metrics"]
|
||||
card_hash = card_hash_of({"metrics": metrics})
|
||||
assert found.card_hashes == {"card": card_hash}
|
||||
assert found.definition_hash == version_hash([card_hash])
|
||||
summary = found.summary()
|
||||
assert summary["card_hashes"] == {"card": card_hash} and summary["definition_hash"] == found.definition_hash
|
||||
|
||||
|
||||
def test_candidates_and_meta_carry_the_version_hash():
|
||||
found = caliber()
|
||||
query = ledger_match.Query("a1", "q", S.CURRENT["source"], S.EQUIVALENT, tables=frozenset({"orders"}),
|
||||
dialect="sqlite")
|
||||
[row] = ledger_match.compare_queries([query], [found])
|
||||
assert row["status"] == "proven"
|
||||
[cand] = row["candidates"]
|
||||
assert cand["caliber_hash"] == found.definition_hash
|
||||
meta = ledger_store.meta_of({"queries": [row]})
|
||||
assert meta["calibers"] == [found.key] and meta["definition_hashes"] == {found.key: found.definition_hash}
|
||||
|
||||
|
||||
def test_the_label_uses_the_candidate_hash_when_there_is_one():
|
||||
label = ledger_labels._caliber({"caliber": "wf@1", "workflow_id": "wf", "version": 1, "caliber_hash": "abc"})
|
||||
assert label["definition_hash"] == "abc"
|
||||
assert "definition_hash" not in ledger_labels._caliber({"caliber": "wf@1", "workflow_id": "wf", "version": 1})
|
||||
@@ -4,8 +4,8 @@
|
||||
的完整值在那个节点封存的产出工件里,agent 字段能按出处从查询快照还原;vars 是谁写的、写进去的是不是节点产出
|
||||
本身说不准,只能在口径卡算的那一刻存下来。
|
||||
|
||||
守的是「只多一个键」:只有标量输入的卡逐字节和改动之前一样(工件 id 不变,引用它的报告也就不变);带容器型
|
||||
vars 输入的卡,去掉 value_artifact 之后和改动之前一样。金样是改动之前的代码真跑出来的(fixtures/whatif)。
|
||||
守的是「新增的只有点名的键」:去掉 value_artifact 和 definition_hash(口径的内容指纹,data/caliber_identity,后加的键,
|
||||
按设计会改变卡的内容与工件 id)之后,卡和改动之前一字不差。金样是改动之前的代码真跑出来的(fixtures/whatif)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -82,12 +82,19 @@ def without_value_artifacts(card: dict) -> dict:
|
||||
return out
|
||||
|
||||
|
||||
async def test_a_card_with_only_scalar_inputs_is_byte_identical_to_before():
|
||||
def without_new_keys(card: dict) -> dict:
|
||||
"""去掉这个功能之前没有的两个键:value_artifact 和 definition_hash(口径的内容指纹)。"""
|
||||
out = without_value_artifacts(card)
|
||||
out.pop("definition_hash", None)
|
||||
return out
|
||||
|
||||
|
||||
async def test_a_card_with_only_scalar_inputs_keeps_the_old_shape():
|
||||
row, data = await finished(graph(SCALAR_KPI, SCALAR))
|
||||
[entry] = data["evidence"]
|
||||
assert entry["artifact"] == GOLDEN["scalar"]["artifact"], "只有标量输入的卡,工件 id 不能变"
|
||||
assert artifact_store.load(entry["artifact"]) == GOLDEN["scalar"]["card"]
|
||||
assert "value_artifact" not in json.dumps(artifact_store.load(entry["artifact"]))
|
||||
card = artifact_store.load(entry["artifact"])
|
||||
assert without_new_keys(card) == GOLDEN["scalar"]["card"]
|
||||
assert "value_artifact" not in json.dumps(card)
|
||||
|
||||
|
||||
async def test_container_vars_inputs_carry_their_full_value():
|
||||
@@ -95,7 +102,7 @@ async def test_container_vars_inputs_carry_their_full_value():
|
||||
[entry] = data["evidence"]
|
||||
card = artifact_store.load(entry["artifact"])
|
||||
# 去掉新加的键,和改动之前一字不差:值、渲染、代入式、复算、其余字段都不变
|
||||
assert without_value_artifacts(card) == GOLDEN["box"]["card"]
|
||||
assert without_new_keys(card) == GOLDEN["box"]["card"]
|
||||
|
||||
by_path = {(m["id"], i["path"]): i for m in card["metrics"] for i in m["inputs"]}
|
||||
items, meta = by_path[("n", "vars.kpi.items")], by_path[("m", "vars.kpi.meta")]
|
||||
@@ -135,4 +142,4 @@ async def test_a_value_too_large_to_keep_is_skipped(monkeypatch):
|
||||
monkeypatch.setattr(metrics, "VALUE_ARTIFACT_MAX", 10)
|
||||
row, data = await finished(graph(BOX_KPI, BOX))
|
||||
card = artifact_store.load(data["evidence"][0]["artifact"])
|
||||
assert card == GOLDEN["box"]["card"]
|
||||
assert without_new_keys(card) == GOLDEN["box"]["card"]
|
||||
|
||||
Reference in New Issue
Block a user