可验证报告认分析结论:报告引用的(含分析范围里点名的)分析结论按原字节嵌入,连同它的输入快照和下钻的上一层结论;任一份输入快照证明不了没碰遮罩列的,结论也只给 id;分析卡的节点完成事件在它的结论都随文件附带时才带内容。导出时服务端按封存的输入复算一遍,结果记在结论的条目上。核对页:③ 遮罩白名单加分析结论,④ 按「已核对的分析结论的输入」认输入快照,⑤ 判定语、具名的数、结果表的格逐字核对是不是结论里写的那几个字(同服务端的渲染规则,不在浏览器里重算统计),数字详情里有分析结论一节(判定语、判定标准、方法与版本、附带说明、Agent 写的假设、输入的核对状态、导出时服务端的复算结论),正文后面渲染分析范围、编号点得开。另在 node 里用最小的 DOM 替身把导出页、核对页点开分析结论的路径各跑一遍

This commit is contained in:
ThearchyHelios
2026-10-21 13:07:55 +08:00
parent 7474fa5030
commit d8375c2251
5 changed files with 633 additions and 21 deletions
+102 -6
View File
@@ -23,6 +23,11 @@
规则同上):点开数字时看得到每个输入的 SQL 和被引用的行,引用链(④)按「已核对的口径卡的输入」认它们。只给 id 的
工件,服务端的文件和 id 对不上的另标 mismatch、解析不出来的另标 unparsable,核对页照实说(对不上的判不通过)。没被
报告引用、也没被附带的口径卡的输入引用的工件不嵌。
- 分析结论([[a:A3]]、[[v:A3.r0.列]]、[[see:A3]] 引用的结论工件)照样按原字节嵌入,连同它的输入快照(遮罩规则同上)和
下钻的上一层结论;任一份输入快照只给 id(证明不了没碰遮罩列)的,结论本身也只给 id——结果表里的维度值取自输入快照,
这份文件宁可多挡。浏览器只核哈希、核判定语和数是不是结论里写的那些,不在页面里重算统计:结论随文件附带时,服务端在
导出时按封存的输入复算一遍(engine/evidence.analysis_recheck),结果记在这份工件的条目上(recheck),页面照它展示。
- 报告文档带着系统写的「分析范围」(analysis_scope)的,页面在正文后面照它渲染,编号点得开那条结论。
- 封存之后追加的事件(按需裁判、审批质询这类批注)单列:类型、序号、内容摘要,不进清单。
它只证明「内部一致」:数字、文档、运行记录彼此对得上,而且没被改过。真伪要拿页面上的封存哈希和系统里这次
@@ -55,9 +60,10 @@ from pathlib import Path
from typing import Any
from urllib.parse import quote
from app.analysis.contract import ARTIFACT_KIND, LEDGER_KIND, NODE_TYPE as ANALYSIS_NODE, is_result
from app.core.artifact_store import canonical_json, content_hash
from app.core.events import EventType
from app.engine.evidence import iter_units, query_entry_fields
from app.engine.evidence import analysis_input_snapshot, analysis_recheck, iter_units, query_entry_fields
from app.engine.lineage import in_scope, scoped_id
from app.engine.masking import OPAQUE_SQL, SYSTEM_SCHEMAS, indirect_reads
from app.engine.merge_query import MERGE_SOURCE
@@ -84,7 +90,9 @@ _SHOWN_EVENTS = frozenset({EventType.RUN_STARTED, EventType.REPORT_CHECKED, Even
#: node.finished 带规范字符串的节点:报告(预览就是嵌进来的那份文档的正文,文档只给哈希的不带)、口径卡(指标集的 id
#: 记在台账里;卡上有值不给的指标、有输入取自遮罩列的不带,见 _card_shown)。成果节点在运行里没有带遮罩的快照时带
#: 目录条目的种类 → 嵌进来的工件种类。运行输入、检索、表和字段的条目不嵌:前者没有工件,后几样不在核对范围里
_CITED_KINDS = {"metric": "metric_set", "query": "query"}
_CITED_KINDS = {"metric": "metric_set", "query": "query", LEDGER_KIND: ARTIFACT_KIND}
#: 下钻的上一层结论最多跟几层(同分析设置里下钻层数的上限)
_PARENT_DEPTH = 6
#: 没挂在工作流上的运行(画布、问数据当场搭的):老数据叫「临时图」,新的叫「未保存的工作流」,也可能是空的。
#: 和前端 lib/terms.ts 的 runName 同一个规则
@@ -202,6 +210,22 @@ class _Store:
self._seen = seen
return self._seen
def analysis_masked(self, artifact: str, depth: int = 0) -> bool:
"""这份分析结论算不算带遮罩(只给 id):结果表、判定语里的维度值取自它的输入快照。遮罩列不能当维度、度量
(app/analysis 在调用时就挡下),可这份文件按自己宁可多挡的认法:任一份输入快照证明不了没碰遮罩列(masked),
结论也只给 id;下钻的上一层结论同样认(最多 _PARENT_DEPTH 层)。读得出字节、却认不出是一份分析结论的,按带遮罩算
(同口径卡)。"""
key = f"analysis:{artifact}"
if key not in self._masked:
result = self.content(artifact)
if not is_result(result):
self._masked[key] = self.raw(artifact) is not None
return self._masked[key]
self._masked[key] = any(
(self.analysis_masked(a, depth + 1) if depth < _PARENT_DEPTH else True) if role == "parent"
else self.masked(a) for a, role in _analysis_inputs(result))
return self._masked[key]
def card_masked(self, artifact: str, events: Sequence[tuple[int, str, str | None, Any]]) -> bool:
"""这张口径卡算不算带遮罩:卡上有一个输入取自遮罩列,或者遮没遮核对不了。卡上记着每个输入的值(cell() 取的格、
Agent 字段、按位置取的 vars.res.rows[0][2])和代入式,整张卡不随文件附带——没法只拿掉几个值,拿掉了哈希就对不上。
@@ -1003,10 +1027,11 @@ def _report_events(events: Iterable[tuple[int, str, str | None, Any]]) -> list[d
def _cited_artifacts(doc: dict[str, Any]) -> list[tuple[str, str, str | None]]:
"""报告正文引用到的口径卡和查询快照:(工件 id, 种类, 来源节点),按正文顺序、去重。
"""报告正文引用到的口径卡、查询快照和分析结论:(工件 id, 种类, 来源节点),按正文顺序、去重。
看每个解析成功的引用(行内的数字、单元格,句末的 [[see:]])在文档目录里指向的条目:指标条目指向口径卡,
查询条目指向快照。整表在组装时已经展开成每格一个单元格引用,不用另看。"""
看每个解析成功的引用(行内的数字、单元格、判定语,句末的 [[see:]])在文档目录里指向的条目:指标条目指向口径卡,
查询条目指向快照,分析结论条目指向结论工件。整表在组装时已经展开成每格一个单元格引用,不用另看。报告文档里系统写的
「分析范围」点名的结论也算(页面上点得开它们)。"""
catalog = doc.get("catalog") if isinstance(doc.get("catalog"), dict) else {}
out: list[tuple[str, str, str | None]] = []
for _, unit in iter_units(doc):
@@ -1021,9 +1046,26 @@ def _cited_artifacts(doc: dict[str, Any]) -> list[tuple[str, str, str | None]]:
if kind and isinstance(entry.get("artifact"), str) and entry["artifact"]:
owner = entry.get("node_id")
out.append((entry["artifact"], kind, str(owner) if owner else None))
scope = doc.get("analysis_scope") if isinstance(doc.get("analysis_scope"), dict) else {}
for group in ("phenomena", "decomposed", "excluded", "refused"):
for item in scope.get(group) or []:
entry = catalog.get(item.get("alias")) if isinstance(item, dict) else None
if isinstance(entry, dict) and entry.get("kind") == LEDGER_KIND and isinstance(entry.get("artifact"), str) \
and entry["artifact"]:
owner = entry.get("node_id")
out.append((entry["artifact"], ARTIFACT_KIND, str(owner) if owner else None))
return list(dict.fromkeys(out))
def _analysis_inputs(result: Any) -> list[tuple[str, str]]:
"""一份分析结论记着的输入:(工件 id, 角色),按先后、按工件去重。角色是 parent 的是下钻的上一层结论,别的是查询快照。"""
out: dict[str, str] = {}
for item in (result.get("inputs") or []) if isinstance(result, dict) else []:
if isinstance(item, dict) and isinstance(item.get("artifact"), str) and item["artifact"]:
out.setdefault(item["artifact"], "parent" if item.get("role") == "parent" else "data")
return list(out.items())
def _card_input_queries(card: Any) -> list[tuple[str, str | None]]:
"""口径卡记着的输入引用的查询快照:(工件 id, 输入来自的节点),按先后、按工件去重。
@@ -1085,7 +1127,8 @@ class _Artifacts:
# 服务端的文件不在了:只给 id,浏览器照实说核对不了
item["missing"] = True
elif masked or (kind == "query" and self.store.masked(artifact)) \
or (kind == "metric_set" and self.store.card_masked(artifact, self.events)):
or (kind == "metric_set" and self.store.card_masked(artifact, self.events)) \
or (kind == ARTIFACT_KIND and self.store.analysis_masked(artifact)):
item["masked"] = True
# 不嵌内容,浏览器就算不出这份文件对不对得上:服务端自己看到的照实带上,核对页据此判不通过、说清原因。
# 以前被改过的卡原样嵌进去,浏览器自己算出对不上;现在不嵌了,这个证据不能跟着丢
@@ -1101,6 +1144,40 @@ class _Artifacts:
return item
def _attach_analysis(artifacts: _Artifacts, store: _Store, item: dict[str, Any] | None, artifact: str,
owner: str | None, cited_queries: list[str], depth: int = 0) -> None:
"""随文件附带的分析结论:它的输入快照(种类 query,遮罩照常认)、下钻的上一层结论(同样附带它的输入,最多
_PARENT_DEPTH 层)一并列进清单,再把服务端的复算结论记在这份结论的条目上(recheck)。只给 id 的结论、被改过的
(哈希对不上)不顺着它去附带别的:被改过的结论说输入是哪几份,不能替它圆。"""
if item is None or "b64" not in item or not store.intact(artifact) or "recheck" in item:
return
result = store.content(artifact)
if not is_result(result):
return
for input_id, role in _analysis_inputs(result):
if role == "parent":
if depth < _PARENT_DEPTH:
parent = artifacts.add(input_id, ARTIFACT_KIND, owner)
_attach_analysis(artifacts, store, parent, input_id, owner, cited_queries, depth + 1)
continue
artifacts.add(input_id, "query", owner)
cited_queries.append(input_id)
item["recheck"] = _recheck(result, store)
def _recheck(result: dict[str, Any], store: _Store) -> dict[str, Any]:
"""导出时服务端按封存的输入把这份分析结论复算一遍(engine/evidence.analysis_recheck,和证据面板同一个函数):输入快照、
上一层结论只认这份文件里哈希对得上的原字节。页面照这个结果写「导出时服务端的复算」,不在浏览器里重算统计。"""
def load(artifact: str) -> Any:
return analysis_input_snapshot(store.content(artifact), artifact) if store.intact(artifact) else None
def load_result(artifact: str) -> Any:
content = store.content(artifact) if store.intact(artifact) else None
return content if is_result(content) else None
return analysis_recheck(result, load, load_result=load_result)
# --------------------------------------------------------------------------
# 事件
# --------------------------------------------------------------------------
@@ -1121,6 +1198,8 @@ def _shows_content(etype: str, node_id: str | None, data: Any, node_types: Mappi
kind = node_types.get(node_id or "")
if kind == NodeType.METRICS:
return _card_shown(data, store, masked_run, events, forced)
if kind == ANALYSIS_NODE:
return _analysis_shown(data, store)
if kind == NodeType.REPORT:
# 预览就是那份文档正文的前一截:文档只给哈希的,这条也只给哈希
preview = data.get("preview") if isinstance(data, dict) else None
@@ -1160,6 +1239,18 @@ def _card_shown(data: Any, store: _Store, masked_run: bool, events: Sequence[tup
return not any(store.card_masked(card, events) for card in known)
def _analysis_shown(data: Any, store: _Store) -> bool:
"""分析卡的节点完成事件带不带规范字符串。产出是每一项的判定、具名的数、判定语(results),台账里记着每份结论的 id:
这几份结论都随文件附带(输入没碰遮罩列、文件在)才带,有一份只给 id、读不出来,或者一份都没记的,这条只给哈希——
和结论本身同一个认法(_Store.analysis_masked),预览里的维度值不比结论里的多。"""
if not isinstance(data, dict):
return False
results = [str(e["artifact"]) for e in data.get("evidence") or []
if isinstance(e, dict) and e.get("kind") == LEDGER_KIND and isinstance(e.get("artifact"), str)
and e["artifact"]]
return bool(results) and all(store.raw(a) is not None and not store.analysis_masked(a) for a in results)
def _masked_cards(doc: dict[str, Any]) -> set[str]:
"""报告文档的证据目录里写成「已遮罩」的指标(withhold_metrics 记了 masked)所在的口径卡。"""
catalog = doc.get("catalog") if isinstance(doc.get("catalog"), dict) else {}
@@ -1318,6 +1409,11 @@ def build_bundle(
if kind == "query":
cited_queries.append(artifact)
continue
if kind == ARTIFACT_KIND:
# 分析结论随文件附带了(输入都没碰遮罩列、文件在、哈希对得上):它的输入快照、下钻的上一层结论一并附上,
# 引用链(④)按「已核对的分析结论的输入」认它们;服务端按封存的输入复算一遍,结果记在条目上(recheck)
_attach_analysis(artifacts, store, item, artifact, owner, cited_queries)
continue
# 口径卡随文件附带了(没遮、文件在):它记着的输入——哪个指标的值取自哪次查询的哪一格——
# 那几次查询的快照也一并附上(设了遮罩的照旧只给 id)。点开数字看得到输入的 SQL 和被引用的行;
# 引用链(④)按「已核对的口径卡的输入」认它们,被改过的卡(intact 不过)不带,免得替它圆
+3
View File
@@ -77,6 +77,9 @@ h1 { margin: 2px 0 0; font-size: 24px; line-height: 1.35; }
.doc pre code { padding: 0; background: none; }
.doc hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }
.doc .link { color: var(--accent); }
.scope { margin-top: 14px; padding: 12px 16px; border: 1px dashed var(--border); border-radius: 10px; background: var(--panel); }
.scope h4 { margin: 0 0 4px; font-size: 15px; }
.scope ul { margin: 0; padding-left: 1.2em; }
.table-wrap { max-width: 100%; margin: 8px 0; overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
.table-wrap table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table-wrap th, .table-wrap td { padding: 5px 9px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
+193 -15
View File
@@ -19,6 +19,10 @@
* 有格的列,伪造的缩进、列号挤不走核对过的数;
* ⑥ 运行信息:运行结果、运行类别、出具档位拿封存范围内已核对的事件对照。工作流名、版本、起止时间、节点名称
* 绑不进封存,页面上写明它们取自运行记录。
* 分析结论(判定语 [[a:A3]]、具名的数 [[a:A3.键]]、结果表的格 [[v:A3.r0.列]])在 ③ ④ 里和查询快照一样核哈希、核引用链
* (它的输入快照按「已核对的分析结论的输入」认);⑤ 里核正文显示的判定语、数是不是结论里写的那几个字(同服务端的渲染
* 规则)。本页不重算统计:结论随文件附带时,出处面板展示导出时服务端按封存的输入复算的结论(工件条目上的 recheck)。
* 报告文档带着系统写的「分析范围」(analysis_scope)的,正文后面照它渲染,编号点得开那条结论。
* 每项给出通过 / 不通过 / 无法核对和原因。服务端导出时不会出现的状态(被引用的工件不在清单里、检索快照这类工件标着
* 遮罩)一律算不通过:只可能是文件被改过。报告文档的证据目录里写着取自遮罩列的指标的值时,服务端只给它的哈希
* (升级前生成的文档,或者生成之后数据源才设的遮罩),页面照实说明,这份报告算未能核对。总览只有一项无法核对都没有、
@@ -46,7 +50,10 @@
const RESULT = { pass: '通过', fail: '不通过', na: '无法核对', partial: '部分无法核对' }
const MARK = { pass: '✓', fail: '✗', na: '?', partial: '?' }
const KIND_LABEL = { report_doc: '报告文档', metric_set: '口径卡', query: '查询快照' }
const KIND_LABEL = { report_doc: '报告文档', metric_set: '口径卡', query: '查询快照', analysis_result: '分析结论' }
/** 分析结论(app/analysis):工件种类、目录条目种类、分析范围里的几组 */
const ANALYSIS = 'analysis_result'
const SCOPE_GROUPS = [['phenomena', '现象'], ['decomposed', '拆过'], ['excluded', '排除'], ['refused', '算不了']]
const TIER_LABEL = { formal: '完整出具', degraded: '降档出具', withheld: '不予出具' }
const CLASS_LABEL = { formal: '正式运行', exploratory: '探索运行' }
// rejected:被审批人驳回结束(运行记录 cancelled + end_reason=rejected)。封存里它和手动停止是同一种结束事件(运行取消),
@@ -111,7 +118,7 @@
sumEvents: (n) => `${n} 条带内容的事件;其余事件只附哈希,参与 ② 的清单核对`,
sumManifest: (n) => `按 ${n} 条事件的哈希重算`,
sumCount: (n, unit) => `${n} ${unit}`,
sumNumbers: (numbers, values) => `数字 ${numbers} 个 · 取值 ${values} 个`,
sumNumbers: (numbers, values, phrases = 0) => `数字 ${numbers} 个 · 取值 ${values} 个${phrases ? ` · 判定语 ${phrases} 个` : ''}`,
sumMeta: '与封存范围内已核对的事件对照',
showAll: (n) => `展开全部 ${n} 项`,
eventMismatch: '内容与记录的哈希不一致',
@@ -136,13 +143,16 @@
maskedUnparsable: '导出时服务端的这份文件无法解析,认不出是否含遮罩列,按含遮罩列处理,未随文件附带',
/** 报告文档只给哈希:证据目录里写着值取自遮罩列的指标的原值(升级前的报告撰写节点生成的,或者生成之后数据源才设的遮罩) */
maskedDoc: '证据目录里写着取自遮罩列的指标的值(生成时没有按遮罩换掉),未随文件附带,只能核对到哈希',
maskedOdd: '只有查询快照、口径卡和报告文档会因遮罩不随文件附带,这份工件却标着遮罩:导出时不会出现这种情况,文件可能被改过',
maskedOdd: '只有查询快照、口径卡、分析结论和报告文档会因遮罩不随文件附带,这份工件却标着遮罩:导出时不会出现这种情况,文件可能被改过',
/** 分析结论只给哈希:它的输入快照有一份证明不了没碰设了遮罩的列(结果表里的维度值取自输入快照) */
maskedAnalysis: '这份分析结论的输入查询用到或可能用到设了遮罩的列,结论未随文件附带,只能核对到哈希',
artifactMissing: '导出时服务端已没有这份工件的文件',
artifactAbsent: '内容未随文件附带:导出时不会出现这种情况,文件可能被改过',
chainDoc: (label) => `报告「${label}」的文档`,
chainByEvent: (seq, what) => `第 ${seq} 条事件(${what})`,
chainByDoc: (label, alias) => `报告「${label}」的证据目录(${alias})`,
chainByCard: (label, metric) => `口径卡「${label}」的输入(指标 ${metric})`,
chainByAnalysis: (label) => `分析结论(${label})的输入`,
chainFrom: '引用自',
chainNoCheckEvent: '找不到这个报告节点的报告自查事件',
chainCheckEventFailed: '报告自查事件的内容没有通过核对',
@@ -159,6 +169,9 @@
srcCell: (alias, row, column) => `查询 ${alias} · 第 ${row + 1} 行 · ${column}`,
srcQuery: (alias) => `查询 ${alias}`,
srcInput: (field) => `运行输入「${field}」`,
srcAnalysis: (alias) => `分析结论 ${alias} 的判定语`,
srcAnalysisValue: (alias, key) => `分析结论 ${alias} 的具名的数「${key}」`,
srcAnalysisCell: (alias, row, column) => `分析结论 ${alias} 的结果表 · 第 ${row + 1} 行 · ${column}`,
srcNone: '没有出处',
srcOther: '其他引用',
numUncited: '该数字未使用引用标记,无法核对其来源',
@@ -175,6 +188,11 @@
numMetricDiffers: '引用的值与口径卡里的值不一致',
numNoCell: (why) => `快照里找不到这一格:${why}`,
numCellDiffers: '引用的值与快照里那一格的值不一致',
numNoPhrase: '分析结论里没有判定语',
numNoValue: (key) => `分析结论里没有名为「${key}」的数`,
numValueDiffers: '引用的值与分析结论里记的值不一致',
numNoTable: '分析结论里没有结果表',
numAnalysisCell: '引用的值与分析结论结果表里那一格的值不一致',
/** cited 已经加好引号(空值写「空值(NULL)」);expected 和引用的值写法一样时不写(文字取值) */
numShownDiffers: (shown, cited, expected) => `正文显示「${shown}」,引用的值是${cited}`
+ (expected == null ? '' : `,按显示规则应显示为「${expected}」`),
@@ -206,7 +224,7 @@
shownLookalike: '紧挨着长得像数字的字母或符号(l、I、O、上标数字、圈码这类),读起来可能多出一位',
shownOdd: '紧挨着这个数的字不在常见写法里,无法确认读者看到的数',
numNotShown: '正文里没有显示这一处:导出时不会出现这种情况,文件可能被改过',
numAria: (kind, text, result) => `${kind === 'value' ? '取值' : '数字'} ${text},${result},查看出处`,
numAria: (kind, text, result) => `${kind === 'value' ? '取值' : kind === 'analysis' ? '判定语' : '数字'} ${text},${result},查看出处`,
// 数字详情里口径卡一节:指标、计算方式(原式、代入式)、输入、复算。遮罩未附带的卡没有这一节(原因见核对明细 ③)
dMetric: '指标', dUnit: '单位', dExpression: '原式(计算方式)', dSubstituted: '代入式(代入输入后的算式)',
dInputs: '输入', dRecompute: '复算', dSourceName: '数据源', dInputValue: '取值', dFrom: '取自',
@@ -236,6 +254,14 @@
dResult: '核对结果', dSource: '出处', dNode: '来源节点', dCaliber: '口径', dCited: '引用的值', dCard: '口径卡里的值',
dCell: '快照里的值', dShown: '正文显示', dShownSame: '与按显示规则渲染的结果一致', dExpected: '按显示规则应为',
dArtifact: '所在工件', dReport: '所在报告',
dPhrase: '判定语', dCriterion: '判定标准', dMethod: '方法与版本', dAnalysis: '分析结论里的值', dRecheck: '复算',
dAnalysisInputs: '输入', dHypothesis: 'Agent 写的假设', dCaveats: '附带说明',
recheckBy: '导出时服务端按封存的输入复算的结论;本页只核哈希,不在浏览器里重算统计',
recheckNone: '导出时没有复算(结论未随文件附带)',
analysisInput: (role, alias) => `${role}${alias ? ` · ${alias}` : ''}`,
scopeTitle: '分析范围', scopeNote: '这一段由系统按这次运行的分析结论写成,写作者改不了;点开编号看判定语、判定标准、输入和复算。',
scopeMissing: '没拆', scopeAll: '候选维度都拆过了', scopeUnknown: '候选维度核对不了', scopeTests: '检验',
scopeTestsText: (n) => `这次做了 ${n} 次差异检验`, scopeParent: (alias) => `下钻自 ${alias}`, scopeDims: (dims) => `按${dims}`,
dSql: 'SQL', dRow: (row) => `快照第 ${row + 1} 行`,
nullValue: '空值(NULL)',
appendedNote: '这些事件追加在封存之后,不在封存范围内,不参与清单核对,也不能当作证据。',
@@ -1260,7 +1286,7 @@
// 内容不随文件附带,浏览器算不出它对不对得上:导出时服务端看到自己的文件和 id 对不上(mismatch)、解析不出来
// (unparsable)的照实说,对不上的判不通过——不然被改过的文件看起来和正常的遮罩一样,篡改的证据就丢了。报告文档
// 只给哈希时页面上没有正文可核,总览不是绿色的通过(overallOf 按报告文档没核对上算)
if (a.kind !== 'query' && a.kind !== 'metric_set' && a.kind !== 'report_doc') {
if (a.kind !== 'query' && a.kind !== 'metric_set' && a.kind !== 'report_doc' && a.kind !== ANALYSIS) {
item.result = FAIL
item.reason = T.maskedOdd
} else if (a.mismatch) {
@@ -1268,7 +1294,7 @@
item.reason = T.maskedMismatch
} else {
item.reason = a.unparsable ? T.maskedUnparsable : a.kind === 'query' ? T.maskedQuery
: a.kind === 'report_doc' ? T.maskedDoc : T.masked
: a.kind === 'report_doc' ? T.maskedDoc : a.kind === ANALYSIS ? T.maskedAnalysis : T.masked
}
} else if (a.missing) {
item.reason = T.artifactMissing
@@ -1337,6 +1363,7 @@
if (refs.length && !out.queryAliases.has(id)) out.queryAliases.set(id, refs[0].alias)
}
out.arts = arts
out.chained = chained
// 已核对的口径卡记着的输入 → 输入指向的查询快照。卡本身核过哈希(③ 通过),它记的引用和文档的证据目录
// 同一个分量:报告只引用指标、不直接引用查询时,查询快照就是从这条路嵌进来、也是从这条路认的。
// 没核对过的卡(哈希对不上)不认:被改过的卡说输入来自哪,不能替它圆
@@ -1352,13 +1379,25 @@
}
}
}
// 已核对的分析结论记着的输入 → 输入快照、下钻的上一层结论。同口径卡:结论本身核过哈希,它记的输入和文档的证据目录
// 同一个分量;没核对过的(哈希对不上)不认
const byAnalysis = new Map() // 工件 id → [{ analysis }]
for (const item of out.artifacts) {
if (item.a.kind !== ANALYSIS || item.result !== PASS) continue
for (const input of Array.isArray(item.json.inputs) ? item.json.inputs : []) {
if (isObj(input) && typeof input.artifact === 'string' && input.artifact && input.artifact !== item.a.id) {
push(byAnalysis, input.artifact, { analysis: item })
}
}
}
for (const item of out.artifacts) {
if (item.a.kind === 'report_doc') continue
const evs = byEvent.get(item.a.id) || []
const docs = byDoc.get(item.a.id) || []
const cards = byCard.get(item.a.id) || []
const ok = evs.length > 0 || docs.length > 0 || cards.length > 0
out.chain.push({ kind: item.a.kind, art: item, events: evs, docs, cards, result: ok ? PASS : FAIL,
const analyses = byAnalysis.get(item.a.id) || []
const ok = evs.length > 0 || docs.length > 0 || cards.length > 0 || analyses.length > 0
out.chain.push({ kind: item.a.kind, art: item, events: evs, docs, cards, analyses, result: ok ? PASS : FAIL,
reason: ok ? '' : T.chainUnreferenced })
if (ok) chained.add(item.a.id)
}
@@ -1371,7 +1410,7 @@
out.docs.push({ report, art, link, doc, trusted: trusted.has(report) })
if (!doc) continue
for (const [seg, path] of segmentsOf(doc)) {
if (seg.kind !== 'number' && seg.kind !== 'value') continue
if (seg.kind !== 'number' && seg.kind !== 'value' && seg.kind !== 'analysis') continue
// 引用的值在文档 JSON 里的原文:超过 2^53 的整数照原文写进原因和出处面板
const citedToken = () => tokenAt(tokensOf(art), [...path, 'cite', 'value'])
const check = trusted.has(report) ? checkNumber(seg, doc, arts, chained, citedToken)
@@ -1394,10 +1433,16 @@
const entry = own(doc.catalog, cite.alias)
const loc = isObj(cite.locator) ? cite.locator : {}
if (cite.kind === 'metric') return T.srcMetric(own(entry, 'name') || loc.metric || cite.ref || '')
if (cite.kind === 'cell' && own(entry, 'kind') === 'analysis' && Number.isInteger(loc.row)) {
return T.srcAnalysisCell(cite.alias, loc.row, String(loc.column ?? ''))
}
if (cite.kind === 'cell') {
return Number.isInteger(loc.row) ? T.srcCell(cite.alias, loc.row, String(loc.column ?? '')) : T.srcQuery(cite.alias)
}
if (cite.kind === 'input') return T.srcInput(loc.field || cite.ref || '')
if (cite.kind === 'analysis') {
return loc.key != null ? T.srcAnalysisValue(cite.alias, String(loc.key)) : T.srcAnalysis(cite.alias)
}
return T.srcOther
}
@@ -1418,10 +1463,14 @@
{ fmt: 'plain', conv: convOf(cite) }))
return shown.result === FAIL ? shown : { ...shown, result: NA, reason: T.numInput }
}
if (cite.kind !== 'metric' && cite.kind !== 'cell') return { result: NA, reason: T.numOther, source }
if (cite.kind !== 'metric' && cite.kind !== 'cell' && cite.kind !== 'analysis') {
return { result: NA, reason: T.numOther, source }
}
const entry = own(doc.catalog, cite.alias)
const info = { source, kind: cite.kind, cite }
const wanted = cite.kind === 'metric' ? 'metric' : 'query'
// 分析结论:判定语、具名的数(cite.kind analysis),和它结果表里的一格(cite.kind cell,目录条目是 analysis)
const analysis = cite.kind === 'analysis' || (cite.kind === 'cell' && own(entry, 'kind') === 'analysis')
const wanted = cite.kind === 'metric' ? 'metric' : analysis ? 'analysis' : 'query'
if (!isObj(entry) || entry.kind !== wanted || typeof entry.artifact !== 'string') {
return { ...info, result: FAIL, reason: T.numNoEntry }
}
@@ -1436,6 +1485,7 @@
}
if (!chained.has(entry.artifact)) return { ...info, result: NA, reason: T.numNotChained }
info.citedToken = citedToken()
if (analysis) return checkAnalysis(info, seg, doc, art)
if (cite.kind === 'metric') {
const metricId = own(cite.locator, 'metric') ?? own(entry.locator, 'metric')
const metrics = Array.isArray(art.json.metrics) ? art.json.metrics : []
@@ -1478,6 +1528,51 @@
/** 引用上记的换算(|万、|pct 这类),没写是 null */
const convOf = (cite) => (own(cite, 'conv') === undefined ? null : cite.conv)
/**
* 分析结论的一处引用(checkNumber 已核过工件的哈希和引用链):引用的值等于结论里记的那个值,正文显示的字等于按服务端
* 的规则渲染出来的字。判定语按文本格渲染(engine/evidence.render_cell 的 kind=text:压成一行、竖线换成 ¦),具名的数按
* 单元格的规则(不做日期时间的改写,同服务端),结果表的一格同查询快照的一格(显示规则照文档记的版本)。不重算统计
*/
function checkAnalysis(info, seg, doc, art) {
const cite = info.cite
const result = art.json
const loc = isObj(cite.locator) ? cite.locator : {}
info.analysis = result
if (cite.kind === 'cell') {
const table = isObj(result.table) ? result.table : null
if (!table) return { ...info, result: FAIL, reason: T.numNoTable }
let cell
try {
const [raw, column, key] = locateCell(table, loc.row, loc.column)
info.row = loc.row
info.column = column
info.truth = cellValue(raw, columnKind(table, column))
info.truthToken = tokenAt(tokensOf(art), ['table', 'rows', loc.row, key])
if (typeof raw === 'string' && typeof info.truth === 'number') info.truthText = pyStrip(raw)
cell = { raw, column }
} catch (e) {
return { ...info, result: FAIL, reason: T.numNoCell(e.message) }
}
if (!sameValue(cite.value, info.truth)) return { ...info, result: FAIL, reason: T.numAnalysisCell }
const display = Object.prototype.hasOwnProperty.call(doc, 'cell_display') ? doc.cell_display : 1
return checkShown(info, seg, () => renderCell(cell.raw, info.truthToken, convOf(cite),
columnKind(table, cell.column), display, own(cite, 'plain') === true))
}
if (loc.key != null) {
const values = isObj(result.values) ? result.values : {}
if (!Object.prototype.hasOwnProperty.call(values, loc.key)) return { ...info, result: FAIL, reason: T.numNoValue(loc.key) }
const raw = values[loc.key]
info.truth = cellValue(raw)
info.truthToken = tokenAt(tokensOf(art), ['values', loc.key])
if (typeof raw === 'string' && typeof info.truth === 'number') info.truthText = pyStrip(raw)
if (!sameValue(cite.value, info.truth)) return { ...info, result: FAIL, reason: T.numValueDiffers }
return checkShown(info, seg, () => renderCell(raw, info.truthToken, convOf(cite), null, 1))
}
if (typeof result.phrase !== 'string' || !result.phrase.trim()) return { ...info, result: FAIL, reason: T.numNoPhrase }
info.truth = result.phrase
return checkShown(info, seg, () => renderCell(result.phrase, null, null, 'text', 1))
}
/** 口径卡的单位:服务端是 str(unit or ""),不是文字的本页不认 */
function unitOf(unit) {
if (!unit) return ''
@@ -1752,7 +1847,7 @@
const eventWhat = (ev) => [EVENT_LABEL[ev.type] || ev.type, ev.node_id ? nodeLabel(ev.node_id) : null].filter(Boolean).join(' · ')
function numberButton(number, body) {
const kind = number.seg.kind === 'value' ? 'value' : 'number'
const kind = number.seg.kind === 'value' ? 'value' : number.seg.kind === 'analysis' ? 'analysis' : 'number'
const btn = h('button', {
type: 'button', class: 'num', 'data-num': number.key, 'data-kind': kind, 'data-result': number.result,
'aria-label': T.numAria(kind, String(number.seg.text ?? ''), RESULT[number.result]),
@@ -1811,10 +1906,12 @@
metric ? [T.dMetric, [metric.name || metric.id || '—', metric.id && metric.name ? h('span', { class: 'muted' }, `(${metric.id})`) : null]] : null,
metric && metric.unit ? [T.dUnit, String(metric.unit)] : null,
cite && cite.status === 'resolved' && 'value' in cite ? [T.dCited, exactText(cite.value, number.citedToken)] : null,
'truth' in number ? [number.kind === 'metric' ? T.dCard : T.dCell,
'truth' in number ? [number.kind === 'metric' ? T.dCard : number.analysis ? T.dAnalysis : T.dCell,
number.truthText ?? exactText(number.truth, number.truthToken)] : null,
checked && !same ? [T.dExpected, number.expected] : null,
[T.dShown, [String(number.seg.text ?? ''), same ? h('span', { class: 'muted' }, `(${T.dShownSame})`) : null]],
// 分析范围里的编号不是正文里的字:没有「正文显示」可核
number.scope ? null
: [T.dShown, [String(number.seg.text ?? ''), same ? h('span', { class: 'muted' }, `(${T.dShownSame})`) : null]],
number.artifact ? [T.dArtifact, [h('code', {}, short(number.artifact)), artifactState(number.artifact)]] : null,
[T.dReport, nodeLabel(number.report.node_id)],
]
@@ -1865,9 +1962,86 @@
const snap = number.snapshot
if (snap) parts.push(...snapshotBlock(snap, number.row, number.column,
view.out.artifacts.find((a) => a.a.id === number.artifact)))
if (number.analysis || number.kind === 'analysis') parts.push(...analysisBlock(number))
return parts
}
/** 数字详情里分析结论一节:判定语、判定标准、方法与版本、附带说明、Agent 写的假设、输入(各自的核对状态)、导出时服务端
* 的复算结论。结论没随文件附带(遮罩、文件不在)时只写复算一句 */
function analysisBlock(number) {
const result = isObj(number.analysis) ? number.analysis : null
const art = view.out.artifacts.find((a) => a.a.id === number.artifact)
const recheck = art && isObj(art.a.recheck) ? art.a.recheck : null
const parts = []
if (result) {
parts.push(kv([
[T.dPhrase, String(result.phrase || '—')],
result.criterion ? [T.dCriterion, String(result.criterion)] : null,
[T.dMethod, String(result.method || '—')],
]))
const caveats = (Array.isArray(result.caveats) ? result.caveats : []).filter(isObj).map((c) => String(c.text || ''))
if (caveats.length) parts.push(h('p', { class: 'sub' }, T.dCaveats), h('ul', { class: 'items' }, caveats.map((c) => h('li', {}, c))))
if (typeof result.hypothesis === 'string' && result.hypothesis.trim()) {
parts.push(h('p', { class: 'sub' }, T.dHypothesis), h('p', {}, result.hypothesis))
}
const inputs = (Array.isArray(result.inputs) ? result.inputs : []).filter((i) => isObj(i) && typeof i.artifact === 'string')
if (inputs.length) {
parts.push(h('p', { class: 'sub' }, T.dAnalysisInputs), h('ul', { class: 'items' }, inputs.map((i) => h('li', {},
h('span', { class: 'what' }, [T.analysisInput(String(i.role || ''), view.out.queryAliases.get(i.artifact)), ' ',
h('code', {}, short(i.artifact))]), artifactState(i.artifact)))))
}
}
parts.push(kv([[T.dRecheck, recheck ? [String(recheck.text || '—'), h('span', { class: 'muted' }, `(${T.recheckBy})`)]
: T.recheckNone]]))
return parts
}
/** 报告文档里系统写的「分析范围」:现象、拆过(下钻按链缩进)、排除、算不了、没拆、检验了几次。编号点得开那条结论 */
function renderScope(doc, report) {
const scope = isObj(doc.analysis_scope) ? doc.analysis_scope : null
if (!scope) return null
const rows = []
for (const [key, title] of SCOPE_GROUPS) {
const items = (Array.isArray(scope[key]) ? scope[key] : []).filter(isObj)
if (!items.length) continue
const depth = new Map()
rows.push(h('dt', {}, title), h('dd', {}, h('ul', {}, items.map((item) => {
const level = item.parent ? (depth.get(String(item.parent)) ?? -1) + 1 : 0
depth.set(String(item.alias || ''), level)
const dims = Array.isArray(item.dims) && item.dims.length ? T.scopeDims(item.dims.join('、')) : null
const lead = [dims, item.parent ? T.scopeParent(String(item.parent)) : null].filter(Boolean).join(',')
const said = item.phrase || item.reason || ''
const li = h('li', {}, scopeButton(String(item.alias || ''), doc, report), lead ? `(${lead})` : '', said ? `:${said}` : '')
if (level) li.style.marginLeft = `${level * 14}px`
return li
}))))
}
if (Array.isArray(scope.not_decomposed)) {
rows.push(h('dt', {}, T.scopeMissing), h('dd', {}, scope.not_decomposed.length ? scope.not_decomposed.join('、') : T.scopeAll))
} else if (Object.prototype.hasOwnProperty.call(scope, 'not_decomposed')) {
rows.push(h('dt', {}, T.scopeMissing), h('dd', {}, String(scope.note || T.scopeUnknown)))
}
if (Number.isInteger(scope.tests) && scope.tests > 0) rows.push(h('dt', {}, T.scopeTests), h('dd', {}, T.scopeTestsText(scope.tests)))
if (!rows.length) return null
return h('section', { class: 'scope', 'data-scope': '' }, h('h4', {}, T.scopeTitle), h('p', { class: 'note' }, T.scopeNote),
h('dl', { class: 'kv' }, rows))
}
/** 分析范围里的一个编号:点开那条结论(工件的核对状态同 ③ ④;没有这条结论、没核对上的照实写) */
function scopeButton(alias, doc, report) {
if (!alias) return ''
const entry = own(doc.catalog, alias)
const artifact = isObj(entry) && typeof entry.artifact === 'string' ? entry.artifact : null
const art = artifact ? view.out.arts.get(artifact) : null
const ok = art && art.result === PASS && view.out.chained && view.out.chained.has(artifact)
const number = { seg: { text: alias, kind: 'analysis', cite: { alias, kind: 'analysis', status: 'resolved', locator: {} } },
key: `${report.node_id}|scope:${alias}`, report, kind: 'analysis', scope: true, artifact,
node: isObj(entry) ? entry.node_id : null,
source: T.srcAnalysis(alias), result: ok ? PASS : art && art.result === FAIL ? FAIL : NA,
reason: ok ? '' : !art ? T.numArtifactNotListed : art.reason || T.numNotChained, analysis: ok ? art.json : null }
return numberButton(number, alias)
}
function openNumber(number) {
const panel = $('detail')
if (panel.hidden) view.back = document.activeElement
@@ -1978,6 +2152,8 @@
box.append(h('p', { class: 'warn', 'data-why': why.key }, why.text))
}
if (item.doc) box.append(renderDoc(item.doc, { report: item.report, numbers }))
const scope = item.doc && item.trusted ? renderScope(item.doc, item.report) : null
if (scope) box.append(scope)
root.append(box)
}
}
@@ -2026,12 +2202,14 @@
}
const refs = [...c.docs.map((d) => T.chainByDoc(nodeLabel(d.report.node_id), d.alias)),
...c.cards.map((k) => T.chainByCard(nodeLabel(k.card.a.node_id), k.metric)),
...(c.analyses || []).map((k) => T.chainByAnalysis(view.out.queryAliases.get(k.analysis.a.id) || short(k.analysis.a.id))),
...c.events.map((ev) => T.chainByEvent(ev.seq, eventWhat(ev)))]
return [KIND_LABEL[c.kind] || c.kind, ' ', h('code', {}, short(c.art.a.id)),
refs.length ? ` ← ${T.chainFrom}${refs.join('、')}` : '']
}))
const values = out.numbers.filter((n) => n.seg.kind === 'value').length
root.append(checkBlock('numbers', T.checkNumbers, T.sumNumbers(out.numbers.length - values, values), out.numbers,
const phrases = out.numbers.filter((n) => n.seg.kind === 'analysis').length
root.append(checkBlock('numbers', T.checkNumbers, T.sumNumbers(out.numbers.length - values - phrases, values, phrases), out.numbers,
(n) => h('button', { type: 'button', 'data-num-item': n.key, onclick: () => {
if (n.button) n.button.scrollIntoView({ block: 'center' })
openNumber(n)
+175
View File
@@ -0,0 +1,175 @@
"""分析结论进可验证报告(第 1 波 W1-b):bundle/build.py 带上结论工件和它的输入快照,verifier.js 在 node 里核对。
- 报告引用的分析结论按原字节嵌入,连同输入快照;服务端导出时按封存的输入复算一遍,结果记在工件条目上(recheck)
- 页面只核哈希、核判定语和数是不是结论里写的那几个字(不在浏览器里重算统计):原样导出全部通过,只改显示文字的伪造
点名不通过
- 输入快照只给 id(证明不了没碰遮罩列)的,结论本身也只给 id,引用它的判定语、数无法核对
- 报告文档里系统写的「分析范围」点名的结论也随文件附带
"""
from __future__ import annotations
import base64
import json
from typing import Any
import pytest
from app import analysis
from app.analysis.contract import ARTIFACT_KIND, ledger_entry
from app.analysis.testing import fake_result
from app.bundle.build import build_bundle
from app.core.artifact_store import canonical_json, content_hash, manifest_hash
from app.engine.evidence import build_catalog, compose_doc, iter_segments
from tests.test_analysis_evidence import SNAPSHOT
from tests.test_bundle_shown import NODE, node, reseal
pytestmark = pytest.mark.skipif(NODE is None, reason="没有 node,无法执行页面上的核对脚本")
RUN_ID = "7b1e0c3a5d9f4a2e8c6b4d2f0a1e3c5b"
RUN = {"id": RUN_ID, "workflow_id": "wf-analysis-weekly", "workflow_name": "异动周报", "version": 2,
"version_hash": "cd" * 32, "run_class": "formal", "status": "succeeded",
"started_at": "2026-10-12T01:00:00+00:00", "finished_at": "2026-10-12T01:01:00+00:00"}
GRAPH = {"nodes": [{"id": n, "type": t, "data": {"label": label}} for n, t, label in [
("fetch", "tool", "按地区取数"), ("an", "agent", "分析"), ("write", "report", "写周报"), ("out", "output", "成果")]]}
TEXT = "## 异动\n\n[[a:A1]]。华东占 [[a:A1.top_share|pct]],变化 [[v:A1.r0.delta]]。[[see:A1]]"
class Files(dict):
def put(self, obj: Any) -> str:
raw = canonical_json(obj).encode()
sha = content_hash(raw)
self[sha] = raw
return sha
def load(self, artifact: str) -> Any:
raw = self.get(artifact)
return json.loads(raw) if raw is not None else None
def bundle_of(text: str = TEXT, *, snapshot: dict[str, Any] | None = None, doc_extra: dict[str, Any] | None = None,
source_masks: dict[str, list[str]] | None = None,
graph: dict[str, Any] | None = None) -> tuple[dict[str, Any], Files, str]:
"""一次封存好的运行的页面数据:(bundle, 工件, 分析结论的 id)。"""
files = Files()
snap = snapshot or SNAPSHOT
q = files.put(snap)
result = fake_result(inputs=[{"ref": "Q2", "artifact": q, "role": "data", "columns": ["region", "week", "gmv"],
"rows": "all"}])
a = files.put(result)
qentry = {"kind": "query", "node_id": "fetch", "exec": 1, "artifact": q, "tool": "db_query__shop",
"source": snap.get("source"), "columns": snap["columns"], "rows": len(snap["rows"]), "truncated": False}
aentry = ledger_entry(result, node_id="an", exec_no=1, artifact=a, item="drop")
catalog = build_catalog(nodes={}, ledger=[qentry, aentry], loader=files.load)
doc = compose_doc(text, catalog, node_id="write", run_id=RUN_ID, loader=files.load)
doc.update(doc_extra or {})
doc_id = files.put(doc)
events: list[tuple[int, str, str | None, Any]] = []
def add(etype: str, node_id: str | None, data: dict[str, Any]) -> None:
events.append((len(events) + 1, etype, node_id, data))
add("run.started", None, {"nodes": 4, "resumed": False, "memory_scope": "default", "collection": "default",
"approval_default": None, "tool_trust": "formal", "agent_limits": {},
"data_versions": None, "replay_protocol": 2})
add("tool.end", "fetch", {"tool": "db_query__shop", "duration_ms": 5, "query_artifact": q, "preview": "…"})
add("node.finished", "fetch", {"duration_ms": 6, "attempt": 1, "evidence": [qentry]})
add("node.finished", "an", {"duration_ms": 8, "attempt": 1, "evidence": [aentry]})
add("report.checked", "write", {"doc_artifact": doc_id, "ok": True})
add("node.finished", "write", {"duration_ms": 9, "attempt": 1, "preview": {"text": "(报告正文)", "doc_artifact": doc_id}})
add("issuance", "out", {"tier": "formal", "mode": "citations"})
add("run.finished", None, {"output": {"周报": "(报告正文)"}})
bundle = build_bundle(RUN, events, manifest_seq=len(events), manifest_hash=manifest_hash(events), read=files.get,
exported_at="2026-10-21T08:00:00+00:00", graph=graph or GRAPH,
source_masks=source_masks or {})
return bundle, files, a
def item(bundle: dict[str, Any], artifact: str) -> dict[str, Any]:
return next(a for a in bundle["artifacts"] if a["id"] == artifact)
def verdicts(bundle: dict[str, Any]) -> dict[str, Any]:
return node("verify", data=bundle, seal=bundle["meta"]["manifest_hash"])
def test_the_analysis_and_its_input_travel_with_the_file_and_verify():
bundle, files, a = bundle_of()
analysis_item = item(bundle, a)
assert analysis_item["kind"] == ARTIFACT_KIND and "b64" in analysis_item and analysis_item["node_id"] == "an"
assert analysis_item["recheck"] == {"status": "failed", "method": "decompose.additive@1",
"text": "复算失败:系统里还没有这种分析的复算实现"}
[query] = [x for x in bundle["artifacts"] if x["kind"] == "query"]
assert "b64" in query
got = verdicts(bundle)
assert all(r == "pass" for key in ("events", "manifest", "artifacts", "chain", "meta") for r in got[key]), got["why"]
numbers = {n["ref"]: n for n in got["numbers"]}
assert set(numbers) == {"a:A1", "a:A1.top_share|pct", "v:A1.r0.delta"}
assert all(n["result"] == "pass" and n["expected"] == n["text"] for n in numbers.values()), numbers
assert numbers["a:A1"]["text"] == "销售额下降 194 万(-8.7%),主要来自华东(-120 万,占 62%)"
assert got["overall"]["result"] == "pass"
def test_the_server_recheck_is_recorded_when_the_operators_answer(monkeypatch):
monkeypatch.setattr(analysis, "recompute", lambda result, load, *, load_result=None: dict(result))
bundle, _, a = bundle_of()
assert item(bundle, a)["recheck"]["text"] == "复算一致:按封存的输入重新计算,结论逐项相同"
@pytest.mark.parametrize("ref, text", [("a:A1", "销售额下降 194 万(-8.7%),主要来自华南(-120 万,占 62%)"),
("a:A1.top_share|pct", "71.9%"), ("v:A1.r0.delta", "-220")])
def test_forging_the_shown_text_fails_by_name(ref, text):
bundle, _, _ = bundle_of()
def change(doc: dict[str, Any]) -> None:
seg = next(s for s in iter_segments(doc) if s.get("ref") == ref)
doc["markdown"] = doc["markdown"].replace(seg["text"], text, 1)
seg["cite"]["rendered"] = seg["text"] = text
art = next(x for x in bundle["artifacts"] if x["kind"] == "report_doc")
doc = json.loads(base64.b64decode(art["b64"]))
change(doc)
got = verdicts(reseal(bundle, art, doc))
forged = next(n for n in got["numbers"] if n["ref"] == ref)
assert forged["result"] == "fail" and forged["reason"].startswith(f"正文显示「{text}」")
def test_an_analysis_on_a_masked_query_travels_only_as_a_hash():
masked = {**SNAPSHOT, "columns": ["region", "phone", "week", "gmv"], "mask_columns": ["phone"],
"sql": "SELECT region, phone, week, SUM(gmv) AS gmv FROM orders GROUP BY 1, 2, 3",
"rows": [[r[0], "000-0001", r[1], r[2]] for r in SNAPSHOT["rows"]]}
bundle, _, a = bundle_of(snapshot=masked)
assert item(bundle, a) == {"id": a, "kind": ARTIFACT_KIND, "node_id": "an", "masked": True}
got = verdicts(bundle)
why = next(w for w in got["why"]["artifacts"] if w["kind"] == ARTIFACT_KIND)
assert why["result"] == "na" and why["reason"].startswith("这份分析结论的输入查询用到或可能用到设了遮罩的列")
assert all(n["result"] == "na" for n in got["numbers"])
assert got["overall"]["result"] != "pass"
def test_analyses_named_only_in_the_scope_travel_too():
scope = {"analysis_scope": {"phenomena": [{"alias": "A1", "op": "decompose", "phrase": "主要来自华东"}],
"decomposed": [], "excluded": [], "refused": [], "not_decomposed": None,
"note": "拆解用的列对不上目录,候选维度核对不了", "tests": 0}}
bundle, _, a = bundle_of("本周销售额有变化。", doc_extra=scope)
assert "b64" in item(bundle, a)
got = verdicts(bundle)
assert all(r == "pass" for r in got["chain"]), got["why"]["chain"]
def test_an_analysis_card_event_shows_its_content_only_when_its_conclusions_travel():
"""分析卡(节点类型 analysis)的节点完成事件:结论都随文件附带才带规范字符串;结论只给 id 的,这条也只给哈希。
Agent 节点的照旧只给哈希。"""
card = {"nodes": [{**n, "type": "analysis"} if n["id"] == "an" else n for n in GRAPH["nodes"]]}
def shown(bundle: dict[str, Any]) -> bool:
return "canonical" in next(e for e in bundle["events"] if e["type"] == "node.finished" and e["node_id"] == "an")
assert shown(bundle_of(graph=card)[0])
assert not shown(bundle_of()[0])
masked = {**SNAPSHOT, "mask_columns": ["phone"], "columns": ["region", "phone", "week", "gmv"],
"sql": "SELECT region, phone, week, SUM(gmv) AS gmv FROM orders GROUP BY 1, 2, 3",
"rows": [[r[0], "000-0001", r[1], r[2]] for r in SNAPSHOT["rows"]]}
assert not shown(bundle_of(graph=card, snapshot=masked)[0])
got = verdicts(bundle_of(graph=card)[0])
assert all(r == "pass" for key in ("events", "manifest", "artifacts", "chain") for r in got[key])
+160
View File
@@ -0,0 +1,160 @@
"""分析结论在两份自包含页面里的渲染(第 1 波 W1-b):报告导出(report_export/runtime.js)点开判定语、分析范围里的编号;
可验证报告(bundle/verifier.js)点开判定语看分析结论一节、正文后面的分析范围。
浏览器里的完整检查(check-report-export、check-bundle)第 2 波由编排者跑;这里在 node 里用一个最小的 DOM 替身把两份脚本
真正跑一遍,挡住渲染分析结论那几步时的脚本错误(引用了不存在的文案、函数),并核对面板里写出来的字。
"""
from __future__ import annotations
import json
import re
import shutil
import subprocess
from pathlib import Path
from typing import Any
import pytest
from httpx import ASGITransport, AsyncClient
import app.bundle
import app.report_export
from app.main import app as asgi
from tests.test_analysis_bundle import bundle_of
from tests.test_analysis_evidence_api import SCOPE, TEXT, sealed_run, seg_id
NODE = shutil.which("node")
pytestmark = pytest.mark.skipif(NODE is None, reason="没有 node,无法执行页面上的脚本")
RUNTIME = Path(app.report_export.__file__).parent / "runtime.js"
VERIFIER = Path(app.bundle.__file__).parent / "verifier.js"
#: 最小的 DOM 替身:元素、文字、片段,getElementById 按 id 建元素;textContent 递归拼接
DOM = r"""
const camel = (s) => s.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
class Node_ {
constructor() { this.children = []; this.parent = null }
append(...kids) {
for (const k of kids) {
const n = typeof k === 'string' ? new Text_(k) : k
if (n instanceof Frag_) { for (const c of n.children) { c.parent = this; this.children.push(c) } n.children = [] }
else { n.parent = this; this.children.push(n) }
}
}
replaceChildren(...kids) { this.children = []; this.append(...kids) }
get textContent() { return this.children.map((c) => c.textContent).join('') }
set textContent(v) { this.children = [new Text_(String(v))] }
walk(f) { f(this); for (const c of this.children) c.walk(f) }
}
class Text_ extends Node_ {
constructor(t) { super(); this.text = t }
get textContent() { return this.text }
set textContent(v) { this.text = String(v) }
}
class Frag_ extends Node_ {}
class El_ extends Node_ {
constructor(tag) {
super(); this.tag = tag; this.attrs = {}; this.dataset = {}; this.style = {}; this.hidden = false; this.listeners = {}
this.className = ''; const cls = new Set(); this.classList = { add: (c) => cls.add(c), remove: (c) => cls.delete(c), contains: (c) => cls.has(c) }
}
setAttribute(k, v) { this.attrs[k] = String(v); if (k.startsWith('data-')) this.dataset[camel(k.slice(5))] = String(v) }
getAttribute(k) { return this.attrs[k] ?? null }
removeAttribute(k) { delete this.attrs[k] }
addEventListener(t, f) { (this.listeners[t] ||= []).push(f) }
focus() {} scrollIntoView() {}
getBoundingClientRect() { return { top: 0, bottom: 0 } }
querySelector() { return new El_('span') }
closest() { return this }
}
globalThis.Node = Node_
const ids = {}
globalThis.window = { scrollBy() {} }
globalThis.document = {
readyState: 'complete', activeElement: null, documentElement: new El_('html'),
getElementById(id) { return ids[id] ||= new El_('div') },
createElement(tag) { return new El_(tag) }, createTextNode(t) { return new Text_(t) },
createDocumentFragment() { return new Frag_() }, addEventListener() {}, querySelectorAll() { return [] },
}
const fs = require('fs')
const input = JSON.parse(fs.readFileSync(0, 'utf8'))
const find = (root, pred) => { const out = []; root.walk((n) => { if (n instanceof El_ && pred(n)) out.push(n) }); return out }
"""
RUN_RUNTIME = DOM + r"""
document.getElementById('report-data').textContent = input.data
new Function(fs.readFileSync(process.argv[1], 'utf8'))()
const out = {}
for (const seg of input.segs) {
const el = new El_('button')
for (const [k, v] of Object.entries(seg)) el.setAttribute(`data-${k}`, v)
el.textContent = seg.text
for (const f of document.getElementById('report').listeners.click || []) f({ target: el })
out[seg.seg] = { title: document.getElementById('detail-title').textContent,
body: document.getElementById('detail-body').textContent,
steps: find(document.getElementById('detail-body'), (n) => n.attrs['data-step']).map((n) => n.attrs['data-step']) }
}
process.stdout.write(JSON.stringify(out))
"""
RUN_VERIFIER = DOM + r"""
document.getElementById('bundle-data').textContent = input.data
document.getElementById('seal-hash').textContent = input.seal
new Function(fs.readFileSync(process.argv[1], 'utf8'))()
setTimeout(() => {
const reports = document.getElementById('reports')
const out = { scope: find(reports, (n) => 'data-scope' in n.attrs).map((n) => n.textContent), opened: {} }
for (const btn of find(reports, (n) => n.attrs['data-num'])) {
for (const f of btn.listeners.click || []) f()
out.opened[btn.attrs['data-num']] = document.getElementById('detail-body').textContent
}
out.verified = document.documentElement.attrs['data-verified'] ?? null
process.stdout.write(JSON.stringify(out))
}, 200)
"""
def run_node(script: str, path: Path, payload: dict[str, Any]) -> Any:
done = subprocess.run([NODE, "-e", script, str(path)], input=json.dumps(payload), capture_output=True, text=True,
timeout=120)
assert done.returncode == 0, done.stderr[-3000:]
return json.loads(done.stdout)
async def test_the_export_page_opens_the_analysis_steps_and_the_scope_numbers():
made = await sealed_run(TEXT, doc_extra=SCOPE)
async with AsyncClient(transport=ASGITransport(app=asgi), base_url="http://test") as client:
html = (await client.get(f"/api/runs/{made['run']}/report.html")).text
data = re.search(r'<script type="application/json" id="report-data">(.*?)</script>', html, re.S).group(1)
phrase = seg_id(made["doc"], "a:A1")
cell = seg_id(made["doc"], "v:A1.r0.delta")
segs = [{"seg": phrase, "kind": "analysis", "state": "deterministic", "text": made["result"]["phrase"]},
{"seg": cell, "kind": "number", "state": "deterministic", "text": "-120"},
{"seg": "scope:A1", "kind": "analysis", "state": "deterministic", "text": "A1"}]
out = run_node(RUN_RUNTIME, RUNTIME, {"data": json.loads(json.dumps(data)), "segs": segs})
opened = out[phrase]
assert opened["steps"] == ["analysis", "analysis_method", "analysis_calc", "analysis_input", "query",
"analysis_recompute", "analysis_seal"]
assert opened["title"].startswith("判定「销售额下降")
body = opened["body"]
for text in ("判定标准一段占总变化一半以上算主要来源", "Agent 写的假设地区拖累", "实际用的阈值", "「主要来源」的占比",
"华东的占比 = 华东的变化 ÷ 总变化 = -120 ÷ -194 = 61.9%", "结果表(列出 4 行,共 4 行)",
"复算失败:系统里还没有这种分析的复算实现", "封存范围内的台账记过这份分析结论"):
assert text in body, text
assert "结果表第 1 行「delta」" in out[cell]["body"]
assert out["scope:A1"]["steps"] == opened["steps"]
async def test_the_bundle_page_shows_the_analysis_detail_and_the_scope():
bundle, _, _ = bundle_of(doc_extra=SCOPE)
out = run_node(RUN_VERIFIER, VERIFIER, {"data": json.dumps(bundle), "seal": bundle["meta"]["manifest_hash"]})
assert out["verified"] == ""
[scope] = out["scope"]
assert scope.startswith("分析范围") and "现象A1:销售额下降" in scope and "拆过A1(按地区):主要来自华东" in scope
assert "没拆渠道" in scope and "这次做了 2 次差异检验" in scope
phrase = next(text for text in out["opened"].values() if "出处分析结论 A1 的判定语" in text)
assert "核对结果✓ 通过" in phrase and "分析结论里的值「销售额下降 194 万" in phrase
assert "判定语销售额下降 194 万" in phrase and "方法与版本decompose.additive@1" in phrase
assert "复算失败:系统里还没有这种分析的复算实现(导出时服务端按封存的输入复算的结论;本页只核哈希" in phrase
value = next(text for text in out["opened"].values() if "具名的数「top_share」" in text)
assert "引用的值0.6186" in value and "正文显示61.9%(与按显示规则渲染的结果一致)" in value
scope_item = next(text for key, text in out["opened"].items() if key.endswith("|scope:A1"))
assert "核对结果✓ 通过" in scope_item and "正文显示" not in scope_item