mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-16 04:22:00 +00:00
出口认分析结论的引用:报告引用的判定语、具名标量、当依据的分析结论按结论工件记进「引用的查询」(结果表的格原来就会走到),顺着结论的输入找到查询,输入查询没通过 SQL 检查的照样计入缺口(受影响的引用写「A1(判定语)」「A1「top_share」」「A1(结论依据)」);逐个数字的出处里具名标量照实记成分析结论(analysis: A1.top_share),不再记成空的运行输入
This commit is contained in:
@@ -4,6 +4,7 @@ import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from app.analysis.contract import LEDGER_KIND
|
||||
from app.core.events import EventType
|
||||
from app.engine.context import NodeContext, NodeError
|
||||
from app.engine.expressions import ExpressionError, eval_expression
|
||||
@@ -568,8 +569,12 @@ def _check_citations(
|
||||
if label:
|
||||
hit["caliber"] = label
|
||||
elif cite["kind"] == "cell":
|
||||
# 查询单元格:Q1.r0.gmv(全局编号,和 doc.catalog 一致)
|
||||
# 查询单元格:Q1.r0.gmv(全局编号,和 doc.catalog 一致);分析结论结果表的一格同一种写法(A3.r0.share)
|
||||
hit["cell"] = f"{cite['alias']}.r{cite['locator']['row']}.{cite['locator']['column']}"
|
||||
elif cite["kind"] == LEDGER_KIND:
|
||||
# 分析结论的具名标量([[a:A3.top_share]]):A3.top_share
|
||||
key = (cite.get("locator") or {}).get("key")
|
||||
hit["analysis"] = f"{cite['alias']}.{key}" if key else str(cite["alias"])
|
||||
else:
|
||||
hit["input"] = cite["locator"].get("field")
|
||||
out["matched"].append({**hit, "segment": seg["id"], "unit": unit["id"], "start": start, "end": end,
|
||||
@@ -1027,37 +1032,51 @@ def _cited_queries(doc: dict[str, Any], catalog: dict[str, Any]) -> tuple[dict[s
|
||||
单元格([[v:Q1.r0.gmv]],[[table:]] 展开后也是一格一格的引用)按查询数:一格写「Q1 第 1 行「gmv」」,
|
||||
几格写「Q1 的 3 格」;只拿它当依据([[see:Q1]])的写「Q1(结论依据)」。出具契约据此判这些查询有没有通过
|
||||
SQL 检查(issuance.sql_check_gaps)。
|
||||
|
||||
分析结论按它的工件记(sql_problems.query_problems 顺着结论的输入找到查询):结果表的格同上(A3 第 1 行「share」);
|
||||
判定语、具名标量写「A3(判定语)」「A3「top_share」」,当依据([[see:A3]])的写「A3(结论依据)」——引用了一份分析
|
||||
结论,它的输入查询上的问题照样计入缺口。
|
||||
"""
|
||||
from app.engine.evidence import iter_units
|
||||
|
||||
cells: dict[str, list[tuple[Any, Any]]] = {}
|
||||
said: dict[str, list[str]] = {} # 分析结论的判定语、具名标量:{A 编号: [键或 None…]}
|
||||
support: list[str] = []
|
||||
for _, unit in iter_units(doc):
|
||||
for seg in unit.get("segments") or []:
|
||||
cite = seg.get("cite") or {}
|
||||
if cite.get("kind") == "cell" and cite.get("status") == "resolved" and cite.get("alias"):
|
||||
if cite.get("status") != "resolved" or not cite.get("alias"):
|
||||
continue
|
||||
if cite.get("kind") == "cell":
|
||||
loc = cite.get("locator") or {}
|
||||
cells.setdefault(str(cite["alias"]), [])
|
||||
if (loc.get("row"), loc.get("column")) not in cells[str(cite["alias"])]:
|
||||
cells[str(cite["alias"])].append((loc.get("row"), loc.get("column")))
|
||||
elif cite.get("kind") == LEDGER_KIND:
|
||||
keys = said.setdefault(str(cite["alias"]), [])
|
||||
key = (cite.get("locator") or {}).get("key")
|
||||
if key not in keys:
|
||||
keys.append(key)
|
||||
for cite in unit.get("see") or []:
|
||||
alias = str((cite or {}).get("alias") or "")
|
||||
if (catalog.get(alias) or {}).get("kind") == "query" and alias not in support:
|
||||
if (catalog.get(alias) or {}).get("kind") in ("query", LEDGER_KIND) and alias not in support:
|
||||
support.append(alias)
|
||||
queries: dict[str, list[str]] = {}
|
||||
# 目录里全部查询的编号:合并查询的输入没被直接引用,缺口里也要写得出它是 Q 几
|
||||
aliases = {str(e["artifact"]): str(e["alias"]) for e in catalog.values()
|
||||
if isinstance(e, dict) and e.get("kind") == "query" and e.get("artifact") and e.get("alias")}
|
||||
for alias in [*cells, *(a for a in support if a not in cells)]:
|
||||
for alias in dict.fromkeys([*cells, *said, *support]):
|
||||
artifact = (catalog.get(alias) or {}).get("artifact")
|
||||
if not isinstance(artifact, str) or not artifact:
|
||||
continue
|
||||
hit = cells.get(alias) or []
|
||||
refs = []
|
||||
if len(hit) == 1 and isinstance(hit[0][0], int):
|
||||
ref = f"{alias} 第 {hit[0][0] + 1} 行「{hit[0][1]}」"
|
||||
else:
|
||||
ref = f"{alias} 的 {len(hit)} 格" if hit else f"{alias}(结论依据)"
|
||||
queries.setdefault(artifact, []).append(ref)
|
||||
refs.append(f"{alias} 第 {hit[0][0] + 1} 行「{hit[0][1]}」")
|
||||
elif hit:
|
||||
refs.append(f"{alias} 的 {len(hit)} 格")
|
||||
refs.extend(f"{alias}「{key}」" if key else f"{alias}(判定语)" for key in said.get(alias) or [])
|
||||
queries.setdefault(artifact, []).extend(refs or [f"{alias}(结论依据)"])
|
||||
return queries, aliases
|
||||
|
||||
|
||||
|
||||
@@ -299,3 +299,74 @@ async def test_a_metric_read_from_a_query_cell_is_traced(client, monkeypatch, sh
|
||||
"expression": "cell(nodes.fetch, 0, 'gmv')"}])
|
||||
issuance = await governed(client, graph)
|
||||
assert "untraced" not in issuance and not any("没追到原始数据" in g for g in issuance["gaps"])
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 引用了分析结论:输入查询的 SQL 检查问题计入缺口,具名标量记进逐个数字的出处
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def analyses(monkeypatch):
|
||||
"""写作目录里多几条分析结论:{A 编号: 结论工件 id}。台账里的分析结论由入口(分析卡、Agent 的分析工具)记,这里直接
|
||||
补进目录:报告节点和出口复核按同一个 report_catalog 建目录,一处补上。"""
|
||||
from app.core import artifact_store
|
||||
from app.engine.evidence import make_eid
|
||||
from app.engine.nodes import report as report_mod
|
||||
|
||||
added: dict[str, str] = {}
|
||||
original = report_mod.report_catalog
|
||||
|
||||
def catalog(state, spec, node):
|
||||
out = original(state, spec, node)
|
||||
for alias, artifact in added.items():
|
||||
out[alias] = {"alias": alias, "kind": "analysis", "artifact": artifact, "item": "drop",
|
||||
"eid": make_eid("analysis", artifact, {}), "locator": {},
|
||||
"result": artifact_store.load(artifact)}
|
||||
return out
|
||||
|
||||
monkeypatch.setattr(report_mod, "report_catalog", catalog)
|
||||
return added
|
||||
|
||||
|
||||
async def stored_driver(*, checks=None) -> str:
|
||||
"""一份「销售额下降主要来自华东」的拆解结论,输入是一份(可能带着 SQL 检查问题的)查询快照,都存进工件库。"""
|
||||
from app.analysis.testing import fake_result
|
||||
from app.core import artifact_store
|
||||
|
||||
query = await artifact_store.put_json({"source": "shop", "sql": "SELECT region, week, SUM(amount) AS gmv FROM orders",
|
||||
"columns": ["region", "week", "gmv"], "rows": [["华东", "W41", 820]],
|
||||
"truncated": False, **({"checks": checks} if checks else {})},
|
||||
kind="query_snapshot")
|
||||
result = fake_result(inputs=[{"ref": "Q2", "artifact": query, "role": "data",
|
||||
"columns": ["region", "week", "gmv"], "rows": "all"}])
|
||||
return await artifact_store.put_json(result, kind="analysis_result")
|
||||
|
||||
|
||||
FANOUT = {"code": "fanout_sum", "level": "error", "message": "一对多关联后求和,订单金额被重复计算。改法:先汇总再关联"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("text", [
|
||||
"[[a:A1]]。[[see:m:gmv]]",
|
||||
"本周销售额 [[m:gmv]],下降主要来自华东。[[see:m:gmv,A1]]",
|
||||
"华东占变化的 [[a:A1.top_share|pct]]。[[see:m:gmv]]",
|
||||
])
|
||||
async def test_an_analysis_built_on_a_failing_query_degrades_like_the_query_itself(monkeypatch, analyses, text):
|
||||
analyses["A1"] = await stored_driver(checks=[FANOUT])
|
||||
writes(monkeypatch, text)
|
||||
issuance = await issued(weekly())
|
||||
assert issuance["tier"] == "degraded"
|
||||
[gap] = [g for g in issuance["gaps"] if "SQL 检查" in g]
|
||||
assert "一对多关联后求和" in gap and "A1" in gap
|
||||
analyses["A1"] = await stored_driver()
|
||||
clean = await issued(weekly())
|
||||
assert clean["tier"] == "formal" and not any("SQL 检查" in g for g in clean["gaps"])
|
||||
|
||||
|
||||
async def test_a_named_scalar_is_listed_as_an_analysis_number(monkeypatch, analyses):
|
||||
analyses["A1"] = await stored_driver()
|
||||
writes(monkeypatch, "华东占变化的 [[a:A1.top_share|pct]],下降主要来自华东。[[see:m:gmv,A1]]")
|
||||
issuance = await issued(weekly())
|
||||
[hit] = [m for m in issuance["matched"] if m.get("analysis")]
|
||||
assert hit["analysis"] == "A1.top_share" and hit["token"] == "61.9%" and "input" not in hit
|
||||
assert "analysis_claims" not in issuance, "判断词挂对了:check 下没有可标注的"
|
||||
|
||||
Reference in New Issue
Block a user