Files
AgentLab/backend/app/ledger/store.py
T
ThearchyHelios 2a4086ab73 认口径按答案引用的那几列比:整条比不等价时先投影再交现有的绑定和判等价
真实助手的 SQL 总会多选几列(订单数、日期常量、MIN / MAX、COALESCE),报告只引用其中一列,口径的取数也只选它的指标要的
那几列。以前整条比,「上周的销售额是多少」3/3 只标相关口径、从不自动出具。

做法:sql_semantics 新增 project_select / project_template / output_names。按方言切词、按括号深度认出最外层 SELECT
列表每一项在原文里的起止,只把留下的几项原样拼回去(选择列表前后一字不动,保真守卫照样看得到原文),拼完重新解析、
语法树要和「原树换成留下的几项」一模一样。只在确定去掉不影响时投影,否则照实给原因、照旧整条比:DISTINCT / DISTINCT ON、
出现在 GROUP BY 里、被 ORDER BY / HAVING / QUALIFY / 窗口用到(按别名、同样的写法、按位置)、有 LIMIT / OFFSET 又不是
只出一行的聚合、集合运算、*、可能改变行数或不认识的函数、去掉仅有的聚合、留下按任意一行取值的列、调换先后时引用别的
选择项的名字,一律不去。

ledger/match:整条比不等价时,答案一侧按报告引用了这条查询的哪几列(labels.referenced_columns)投影,口径一侧按口径卡
上每个指标读了这个取数节点的哪几列(labels.card_reads)投影,投影后照样交 match_template(不另开给信任的路)。被引用的
列两边怎么对上写进模块说明:答案按列名在快照结果列里找位置、口径按 cell() 里的列名在输出名里找位置,投影后按位置配对,
答案留下两三列时另试调换先后,每份口径取数最多试 12 种。证明了的候选记 projection;没投影可试的逐对和以前一字不差。
算法版本升到 3;E1 另数 proven_projected;对账按投影后留下的列配对(不用人选)。

测试:tests/test_sql_projection.py(该去的、不该去的各一组,语料里 305 种投影在虚构库上执行守门)、
tests/test_ledger_projection.py(多选一列 / 多几个聚合 / 日期常量列能证明;多一个分组列、DISTINCT、按别的列排序加
LIMIT、HAVING 用到多出来的聚合仍然证明不了且和不投影时一字不差;口径一侧投影、调换先后;真跑一次整题等价;对账配对)。
去掉投影再跑,7 条失败。
2026-10-17 11:47:26 +08:00

148 lines
7.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""口径账本的记法(docs/design/next-round/02-口径账本.md 做法 2,拍板 L8):比对结果存成内容寻址工件,不建新表。
一次比对 = 一份工件 kind=caliber_match(格式 agentlab.caliber_match/1)+ 一条封存之后追加的事件 ledger.matched
{artifact, manifest_hash, algo, counts, trigger}(engine/run_append 的 sealed 方式,同按需裁判的 evidence.judged):
- 工件内容只放比对本身:运行 id、对着哪一次封存(manifest_hash / manifest_seq)、算法版本、考虑过的口径版本(工作流、版本、
级别、graph_hash)、每条查询的状态和绑定、差异条目、列映射、每个数的标签、整题判断。不放时间、耗时、工作流名(名字会改,
界面读的时候再取):同一份封存、同一个算法版本、同一批口径,再比一次得到的工件一字不差、哈希一样。
- 工件引用的 meta 记 {calibers: [等价已证明的「工作流@版本」], related: [相关的], counts}:召回按旧版本反查、统计按种类数,
都只扫 artifacts(kind 有索引),不逐份读内容。
- 事件说的是「这一次封存的这份比对」:读的时候只认 manifest_hash 等于运行现在封存的那几条,取最后一条(之后口径有变、补比
过的,以最新的为准)。工件读不出、被改过、不是这次运行这次封存的,照实说读不出,不拿它标数。
trigger 是谁触发的:sealed(运行封存后马上比的)、backfill(启动时补比)、on_demand(打开账本时按需比的)。后两种是事后按
那时的口径回看的,界面据此写「按现在的口径回看」。它只在事件里,不进工件内容(不然同一份比对因为触发方式不同哈希就不同)。
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from sqlalchemy import select
from app.core.events import EventType
from app.db.base import SessionLocal
from app.db.models import Run, RunEvent
#: 工件的种类和内容格式
KIND = "caliber_match"
FORMAT = "agentlab.caliber_match/1"
#: 比对算法的版本:绑定、证明、标签的规则改了就加一,界面和统计据此分得清是哪一版算法比的。
#: 2(第 3 波):认出助手选用的口径(查询 composed、标签 how=composed),指标的标签记它取自哪几条查询(queries,认证队列
#: 认「被报告引用的查询」用)
#: 3:整条比不等价时按报告引用的列投影再比(ledger/match 的 _projected),候选记 projection;没投影的照旧
ALGO = "3"
#: 触发方式(见模块说明)。certified(第 3 波):从问数据固化出的口径发布之后,对来源运行补比的(ledger/certify)
TRIGGERS = ("sealed", "backfill", "on_demand", "certified")
@dataclass(frozen=True)
class Recorded:
"""一次运行现在这次封存的最新一份比对。content 读不出时为 None,problem 写为什么。"""
artifact: str
seq: int
ts: Any
trigger: str
algo: str | None
content: dict[str, Any] | None
problem: str | None = None
@property
def retro(self) -> bool:
"""事后按那时的口径回看的(启动时补比、打开时按需比的),不是运行一封存就比的。"""
return self.trigger != "sealed"
#: 读不出比对工件时的说法
UNREADABLE = "比对记录读不出来(文件缺失)"
TAMPERED = "比对记录与哈希不一致,疑似被修改,不能用来标口径"
FOREIGN = "比对记录不属于这次运行的这次封存,不能用来标口径"
def counts_of(queries: list[dict[str, Any]]) -> dict[str, int]:
"""每种查询状态各几条(proven / composed / related / none / skipped),0 也写。"""
out = {status: 0 for status in ("proven", "composed", "related", "none", "skipped")}
for query in queries:
status = str(query.get("status") or "")
if status in out:
out[status] += 1
return out
def meta_of(content: dict[str, Any]) -> dict[str, Any]:
"""工件引用的 meta:{calibers: [等价已证明的「工作流@版本」], related: [相关的], counts}。"""
proven: list[str] = []
related: list[str] = []
for query in content.get("queries") or []:
for cand in query.get("candidates") or []:
key = str(cand.get("caliber") or "")
if not key:
continue
bucket = proven if cand.get("status") == "proven" else related
if key not in bucket:
bucket.append(key)
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 [])}
async def save(run_id: str, content: dict[str, Any], *, seal: tuple[int | None, str | None],
trigger: str) -> str | None:
"""存比对工件、追加 ledger.matched。返回工件 id;运行在比对期间换了封存(没抢到)时只存了工件、没追加事件,返回 None
(不算这次运行的比对:读的时候只认事件)。"""
from app.core.artifact_store import put_json
from app.engine.run_append import append_events
if trigger not in TRIGGERS:
raise ValueError(f"不认识的比对触发方式:{trigger}")
meta = meta_of(content)
artifact = await put_json(content, kind=KIND, run_id=run_id, meta=meta)
written = await append_events(run_id, [(EventType.LEDGER_MATCHED, None, {
"artifact": artifact, "manifest_hash": content.get("manifest_hash"), "algo": content.get("algo"),
"counts": meta["counts"], "trigger": trigger,
})], require="sealed", seal=seal)
return artifact if written else None
async def latest(run_id: str) -> Recorded | None:
"""这次运行现在这次封存的最新一份比对;没比对过(或者比对的是以前的封存)为 None。"""
from app.core.artifact_store import load
async with SessionLocal() as session:
manifest = await session.scalar(select(Run.manifest_hash).where(Run.id == run_id))
if not manifest:
return None
rows = (await session.execute(
select(RunEvent.seq, RunEvent.ts, RunEvent.data).where(
RunEvent.run_id == run_id, RunEvent.type == EventType.LEDGER_MATCHED)
.order_by(RunEvent.seq.desc()))).all()
for seq, ts, data in rows:
if not isinstance(data, dict) or data.get("manifest_hash") != manifest or not data.get("artifact"):
continue
artifact = str(data["artifact"])
trigger = str(data.get("trigger") or "sealed")
try:
content = load(artifact)
except ValueError:
return Recorded(artifact, seq, ts, trigger, data.get("algo"), None, TAMPERED)
if not isinstance(content, dict):
return Recorded(artifact, seq, ts, trigger, data.get("algo"), None, UNREADABLE)
if content.get("format") != FORMAT or content.get("run_id") != run_id \
or content.get("manifest_hash") != manifest:
return Recorded(artifact, seq, ts, trigger, data.get("algo"), None, FOREIGN)
return Recorded(artifact, seq, ts, trigger, data.get("algo"), content)
return None
async def matched_run_ids(session: Any, run_ids: list[str]) -> set[str]:
"""这几次运行里有过比对事件的(不管是哪一次封存的):启动补比据此跳过比过的。"""
if not run_ids:
return set()
rows = await session.execute(select(RunEvent.run_id).where(
RunEvent.run_id.in_(run_ids), RunEvent.type == EventType.LEDGER_MATCHED).distinct())
return {r for (r,) in rows}
__all__ = ["ALGO", "FORMAT", "KIND", "TRIGGERS", "Recorded", "counts_of", "latest", "matched_run_ids", "meta_of", "save"]