完善口径管理、主动调查与后台任务,修复通用问题

- 完善口径编辑、只读试运行、统一质量验证与执行语义指纹
- 增加可执行指标约束、有预算的主动调查和过程评测
- 提取发布检查、证据读取与起草服务,支持任务停止、重试和检查点恢复
- 增加版本传递影响分析,修复并发覆盖、敏感值遮罩、异常评分及页面状态问题
- 修复 SQLite 复合查询追溯和 microVM 可选依赖缺失处理
- 纳入当前设计文档及部署配置调整

验证:相关主回归 3822 项通过,4 项跳过、8 项预期失败;补充回归通过。
前端构建、文案检查及 git diff --check 通过。
全量检查仍有环境权限、PostgreSQL 可选依赖和既有部署配置、SQLite 文件基线差异导致的未通过项。
This commit is contained in:
ThearchyHelios
2026-11-06 17:14:45 +08:00
parent 5f0b2359ac
commit 9d8b41430e
94 changed files with 9730 additions and 957 deletions
+38 -2
View File
@@ -5,10 +5,11 @@
- definition_hash:一张口径卡的指标定义(caliber、caliber_version 和每个指标的语义字段)的 sha256;
- version_hash:一份口径版本里各卡的内容哈希排序后再取 sha256,也就是整份版本的定义指纹;
- semantic_hash:口径卡及上游执行依赖的指纹,含 SQL、数据源与绑定,排除布局;
- card_hash_of:从 {node_id, metrics, caliber?, caliber_version?} 这种卡形状取哈希(运行时指标卡、账本里源头口径卡的
两种写法共用)。
**只取语义字段。** 指标只留 id / name / expression / unit / decimals / format / kind / definition / params 里存在的键,
**定义指纹只取声明字段。** 指标只留 SEMANTIC_FIELDS 中存在的键(含类型配置、粒度、过滤和可执行约束),
按 id 排序(顺序不影响哈希);空字符串、None、空的列表和字典不写进规范 JSON。值、渲染后的字、输入出处、状态这些都是
运行时产物,不进哈希——同一份定义重跑一次,或者补了无关的字段,哈希不变。
@@ -26,7 +27,8 @@ from typing import Any
#: 指标的语义字段:只有这几样算定义,运行时值(value、rendered、inputs……)不参与哈希
SEMANTIC_FIELDS = ("id", "name", "expression", "unit", "decimals", "format", "kind", "definition", "params", "external",
"rule", "assessment")
"rule", "assessment", "grain", "time_field", "numerator", "denominator", "dimensions", "filters",
"contract")
def _blank(value: Any) -> bool:
@@ -72,4 +74,38 @@ def version_hash(card_hashes: Iterable[str]) -> str:
return content_hash("\n".join(sorted(str(h) for h in card_hashes or () if h)))
def semantic_hash(graph: dict[str, Any], node_id: str) -> str:
"""口径及其上游执行依赖的指纹:包含 SQL、数据源、入口绑定和固定版本引用。
definition_hash 继续表示指标定义自身;此指纹才覆盖取数语义。保守保留 SQL 原文,
不把未证明等价的改写判为相同;画布坐标和无关的下游写作节点不参与。
"""
import re
from app.core.artifact_store import canonical_json, content_hash, execution_content
content = execution_content(graph)
nodes = {n["id"]: n for n in content["nodes"]}
wanted, pending = set(), [node_id]
while pending:
current = pending.pop()
if current in wanted:
continue
wanted.add(current)
pending.extend(e["source"] for e in content["edges"] if e["target"] == current)
node = nodes.get(current)
if node:
# 非相邻节点也能被表达式引用。保守扫描配置,包含节点参数中的模板引用。
config = canonical_json(node["data"].get("config") or {})
pending.extend(re.findall(r"\bnodes\.([\w-]+)", config))
pending.extend(re.findall(r"\bnodes\[['\"]([\w-]+)['\"]\]", config.replace('\\"', '"')))
inputs = {n["id"] for n in nodes.values() if n["type"] == "input"}
wanted |= inputs
return content_hash(canonical_json({
"schema_version": 1, "defaults": content["defaults"],
"nodes": [n for n in content["nodes"] if n["id"] in wanted],
"edges": [e for e in content["edges"] if e["source"] in wanted and e["target"] in wanted],
}))
__all__ = ["SEMANTIC_FIELDS", "card_hash_of", "definition_hash", "version_hash"]
+7 -3
View File
@@ -93,8 +93,8 @@ def view_of(snapshot: SourceSnapshot, *, source_name: str) -> SourceView:
async def compile_facts(view: SourceView, sql: str) -> CompileFacts:
"""在查询读过的同一个文件上编译原 SQL(EXPLAIN,不执行),给出与解析无关的三项事实(R15):
授权回调报出读了哪些表、字节码里 ResultRow 和 Yield 各有几个。
"""在查询读过的同一个文件上编译原 SQL(EXPLAIN,不执行),给出与解析无关的事实(R15):
授权回调报出读了哪些表、编译了几个 SELECT,字节码里 ResultRow 和 Yield 各有几个。
步骤:经 EngineCache 取引擎(首次时整份核对哈希)→ **同步**取 engines.pinned(view)(中间没有 await,不会被 LRU
挤掉)→ 在线程里:比指纹 → 单开 immutable 只读连接(和 EngineCache 打开快照的方式相同)→ 装授权回调、EXPLAIN
@@ -115,12 +115,16 @@ def _compile(path: str, pinned: engine_mod.Fingerprint, sql: str) -> CompileFact
if _fingerprint(path) != pinned:
raise SnapshotTampered(_REPLACED)
reads: set[str] = set()
selects = 0
def observe(action: int, arg1: str | None, _arg2: str | None, _db: str | None, _trigger: str | None) -> int:
nonlocal selects
# SQLITE_READ 的 arg1 是表名(建表时的写法)。子查询、自连接读到的表都会报出来,改名、CTE 报的和合法写法
# 一样(P4-SPEC 2.4 末尾),所以这只是兜底,主判据仍是识别器
if action == sqlite3.SQLITE_READ and arg1:
reads.add(arg1)
if action == sqlite3.SQLITE_SELECT:
selects += 1
return sqlite3.SQLITE_OK
conn = open_checked_sqlite(path, readonly=True, immutable=True)
@@ -131,7 +135,7 @@ def _compile(path: str, pinned: engine_mod.Fingerprint, sql: str) -> CompileFact
conn.close()
if _fingerprint(path) != pinned:
raise SnapshotTampered(_REPLACED)
return CompileFacts(tables=reads, result_rows=opcodes.count("ResultRow"), yields=opcodes.count("Yield"))
return CompileFacts(tables=reads, result_rows=opcodes.count("ResultRow"), yields=opcodes.count("Yield"), selects=selects)
# --------------------------------------------------------------------------
+2 -1
View File
@@ -659,11 +659,12 @@ class RelatedCheckPlan:
@dataclass
class CompileFacts:
"""R15 的第二道核对(EXPLAIN 编译、不执行):授权回调读到的表、字节码里 ResultRow 和 Yield 的个数。"""
"""R15 的第二道核对:授权回调读到的表、SELECT 次数和字节码的输出指令数。"""
tables: set[str]
result_rows: int
yields: int
selects: int = 1
# ==========================================================================