合入 W2-c 前端:分析卡节点与分析项编辑器、报告节点两个新字段、设置「分析」一节、问数据时间线的分析步骤与出具横幅三项、运行对比的分析卡一区、证据面板与正文接上分析结论(六步出处链、闭合核对、两期可比性、说法违规标注、分析范围)、可验证报告核对页与分析样本页、口径账本摘要防 NaN;服务端补分析工具事件里的判定与分析结论的单独出处接口

# Conflicts:
#	backend/tests/test_eval_schedule.py
This commit is contained in:
ThearchyHelios
2026-10-21 19:34:24 +08:00
50 changed files with 15055 additions and 105 deletions
+124
View File
@@ -38,6 +38,10 @@
原样导出照样全部通过
- prose:同经营月报的口径卡和流水,正文是写作者自己写在引用旁边的普通文字(「运费统一[[m:…]]」「合计——[[m:…]]」
「SKU-[[v:…]]」「共[[m:…]]家」这类):原样导出全部通过、总览是绿的,紧挨着核对的规则不误判正常写法
- analysis:异动周报(分析能力一轮)。Agent 在虚构零售库 shop 上查了两周的总数(Q1)和按地区两周的销售额(Q2),做了按地区的
变化拆解(A1,带总数核对)、波动基线(A2,两周历史不够,算不了)、两期可比性检查(A3),结论都是 app.analysis 真算的;
报告引用 A1 的判定语、具名的数(占比)、结果表的格,正文后面是系统写的「分析范围」(engine/analysis_scope 按虚构的数据
目录算出没拆的渠道)。结论和输入快照随文件附带、导出时复算过,原样导出全部通过;分析范围里的编号点得开(A2、A3 正文没引用)
"""
from __future__ import annotations
@@ -313,6 +317,125 @@ def formats_bundle(*, card: dict[str, Any] = FORMATS_CARD, table: dict[str, Any]
return build(f, events, bound)
#: 分析样本(analysis)的两份查询:虚构零售库 shop 两周的总数(Q1)、四个地区两周的销售额(Q2)
AN_TOTALS = {"columns": ["week", "gmv"], "column_types": {"week": "text", "gmv": "number"},
"rows": [["2026-W41", 2220], ["2026-W42", 2026]], "source": "shop",
"sql": "SELECT week, SUM(amount) AS gmv FROM orders GROUP BY week ORDER BY week", "truncated": False,
"row_count": 2}
AN_REGIONS = {"columns": ["region", "week", "gmv"], "column_types": {"region": "text", "week": "text", "gmv": "number"},
"rows": [["华东", "2026-W41", 820], ["华东", "2026-W42", 700], ["华北", "2026-W41", 600],
["华北", "2026-W42", 560], ["华南", "2026-W41", 700], ["华南", "2026-W42", 666],
["西南", "2026-W41", 100], ["西南", "2026-W42", 100]], "source": "shop",
"sql": "SELECT region, week, SUM(amount) AS gmv FROM orders GROUP BY region, week ORDER BY region, week",
"truncated": False, "row_count": 8}
#: Agent 的三次分析调用(工具名、请求),照分析工具交给 app.analysis.run 的写法
AN_CALLS = [
("analysis_decompose", {"op": "decompose", "mode": "additive", "hypothesis": "下降是不是集中在某个地区",
"params": {"query": "Q2", "by": ["region"], "period": "week", "base": "2026-W41",
"target": "2026-W42", "measure": "gmv", "label": "销售额",
"headline": {"base": "Q1.r0.gmv", "target": "Q1.r1.gmv"}}}),
("analysis_baseline", {"op": "baseline", "mode": None, "hypothesis": "这周的下降算不算异常",
"params": {"query": "Q1", "period": "week", "measure": "gmv", "label": "销售额"}}),
("analysis_comparable", {"op": "comparable", "mode": None, "hypothesis": "两周能不能直接比",
"params": {"query": "Q2", "period": "week", "base": "2026-W41", "target": "2026-W42",
"measure": "gmv"}}),
]
#: 异动周报:引用 A1 的判定语、具名的数(占比)、结果表的格;A2、A3 只在系统写的分析范围里
AN_REPORT = ("## 异动周报\n\n[[a:A1]]。华东占总变化的 [[a:A1.top_share|pct]],华东的变化是 [[v:A1.r0.delta]]。[[see:A1]]\n\n"
"各地区的变化见下表。\n\n[[table:A1 cols=region,base,target,delta rows=0-3]]")
def _shop_notes() -> dict[str, Any]:
"""虚构零售库 shop 的数据目录(orders 是事实表;地区、渠道是属性列,码值表列全):分析范围按它算没拆的候选维度。"""
from app.data.catalog import make_item
def column(label: str, measure: str, codes: tuple[str, ...] = ()) -> dict[str, Any]:
out = {"label": make_item(label, "human"), "measure": make_item(measure, "human")}
if codes:
out["codes"] = make_item({c: "" for c in codes}, "profile", "verified")
return out
return {"orders": {"kind": make_item("fact", "human"), "columns": {
"region": column("地区", "attribute", ("华东", "华北", "华南", "西南")),
"channel": column("渠道", "attribute", ("线上", "线下")),
"week": column("周", "attribute"), "amount": column("实收金额", "flow")}}}
def analysis_bundle() -> dict[str, Any]:
"""异动周报(analysis)的页面数据:结论按两份快照真算(app.analysis.run),Agent 节点的事件照分析工具真实的写法
(tool.end 带结论工件和判定那一段),报告文档按写作目录真实组装,分析范围按虚构的数据目录真实算出。"""
import asyncio
from datetime import date
from app import analysis
from app.analysis.contract import effective_settings, ledger_entry
from app.engine import analysis_scope
from app.tools.analysis import event_view, input_snapshot
run = {**RUN, "id": "9c2e4a6b8d0f4e1a3c5b7d9f1e3a5c7b", "workflow_id": "wf-sample-anomaly", "workflow_name": "异动周报",
"started_at": "2026-10-19T01:00:00+00:00", "finished_at": "2026-10-19T01:01:30+00:00"}
graph = {"nodes": [{"id": n, "type": t, "data": {"label": label}} for n, t, label in [
("start", "input", "开始"), ("bot", "agent", "问数据"), ("write", "report", "写周报"), ("out", "output", "成果")]]}
files: dict[str, bytes] = {}
def put(obj: Any) -> str:
artifact, raw = stored(obj)
files[artifact] = raw
return artifact
def load(artifact: str) -> Any:
raw = files.get(artifact)
return json.loads(raw) if raw is not None else None
queries = [(put(snap), snap) for snap in (AN_TOTALS, AN_REGIONS)]
# 照分析工具交给算子的写法:查询快照原样加上出处;目录事实(列的叫法、度量类型)按虚构的数据目录给,结论里记下
snapshots = {f"Q{i}": input_snapshot(snap, ref=f"Q{i}", artifact=sha, node_id="bot")
for i, (sha, snap) in enumerate(queries, 1)}
context = {"today": date(2026, 10, 19), "catalogs": {"shop": _shop_notes()}}
ledger: list[dict[str, Any]] = [
{"kind": "query", "node_id": "bot", "exec": 1, "artifact": sha, "call_id": f"call_{i}", "tool": "db_query__shop",
"source": "shop", "columns": snap["columns"], "rows": len(snap["rows"]), "truncated": False}
for i, (sha, snap) in enumerate(queries)]
results = []
for n, (tool, request) in enumerate(AN_CALLS, len(queries)):
result = dict(asyncio.run(analysis.run(request, snapshots.get, settings=effective_settings(None), context=context)))
artifact = put(result)
results.append((tool, f"call_{n}", artifact, result))
ledger.append(ledger_entry(result, node_id="bot", exec_no=1, artifact=artifact, call_id=f"call_{n}"))
catalog = build_catalog(nodes={}, ledger=ledger, loader=load)
doc = compose_doc(AN_REPORT, catalog, node_id="write", run_id=run["id"], loader=load)
doc["analysis_scope"] = analysis_scope.build(catalog, sources={"shop": {"tables": _shop_notes(), "masked": []}},
load=load)
doc_id = 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": len(graph["nodes"]), "resumed": False, "memory_scope": "default",
"collection": "default", "approval_default": None, "tool_trust": "formal",
"agent_limits": {}, "data_versions": None, "replay_protocol": 2})
add("node.started", "bot", {"node_type": "agent", "label": "问数据"})
for i, (sha, snap) in enumerate(queries):
add("tool.end", "bot", {"tool": "db_query__shop", "call_id": f"call_{i}", "duration_ms": 9, "query_artifact": sha,
"preview": json.dumps({"columns": snap["columns"], "rows": snap["rows"][:2]},
ensure_ascii=False)})
for k, (tool, call_id, artifact, result) in enumerate(results, 1):
add("tool.end", "bot", {"tool": tool, "call_id": call_id, "duration_ms": 6, "analysis_artifact": artifact,
"preview": json.dumps({"analysis_artifact": artifact}), "analysis": event_view(result, f"A{k}")})
add("node.finished", "bot", {"duration_ms": 240, "attempt": 1, "preview": {"text": "销售额下降,主要来自华东。"},
"evidence": ledger})
add("llm.end", "write", {"model": "mock", "output": "(写作草稿)"})
add("report.checked", "write", {"doc_artifact": doc_id, "ok": True})
add("node.finished", "write", {"duration_ms": 20, "attempt": 1, "preview": {"text": "(报告正文)", "doc_artifact": doc_id}})
add("issuance", "out", {"tier": "formal", "mode": "citations"})
add("node.finished", "out", {"duration_ms": 2, "attempt": 1, "preview": {"周报": "(报告正文)"}})
add("run.finished", None, {"output": {"周报": "(报告正文)"}})
return build_bundle(run, events, manifest_seq=len(events), manifest_hash=manifest_hash(events), read=files.get,
exported_at=EXPORTED_AT, graph=graph, source_masks={})
def bundles() -> dict[str, dict[str, Any]]:
"""{页面名: 页面数据}"""
out: dict[str, dict[str, Any]] = {}
@@ -368,6 +491,7 @@ def bundles() -> dict[str, dict[str, Any]]:
out["formats"] = formats_bundle()
out["formats-unit"] = formats_bundle(card=FORMATS_UNIT_CARD)
out["prose"] = formats_bundle(text=PROSE)
out["analysis"] = analysis_bundle()
return out
+101
View File
@@ -0,0 +1,101 @@
"""报告末尾「分析范围」里的编号点得开(GET /runs/{id}/evidence/analyses/{alias}):那份分析结论的出处链和点开正文里它的判定语
是同一条(api/evidence._scope_chain),正文没引用的结论(算不了的、可比性检查)也点得开;证据目录里没有的编号 404。
真运行、真算子:照 tests/test_analysis_e2e_claims.py 的虚构零售库 shop(数据目录标了地区、渠道是属性),Agent 查两次库、
做一次变化拆解、一次波动基线(两周历史不够,算不了)、一次可比性检查;写作模型只引用拆解的判定语。模型都换成脚本。
"""
from __future__ import annotations
from httpx import ASGITransport, AsyncClient
from langchain_core.messages import AIMessage, ToolMessage
from app.analysis import contract
from app.core import artifact_store
from app.engine.evidence import iter_segments
from app.main import app
from tests.test_analysis_e2e_claims import ( # noqa: F401 - 夹具按名字引入
ARGS, BY_REGION, DECOMPOSE, TOOL, TOTALS, _finish, _graph, _report, engine_up, shop,
)
BASELINE = contract.TOOL_NAMES["baseline"]
COMPARABLE = contract.TOOL_NAMES["comparable"]
TEXT = "## 本周销售\n\n[[a:A1]]。[[see:A1]]"
CHAIN = ["analysis", "analysis_method", "analysis_calc", "analysis_input", "analysis_recompute", "analysis_seal"]
def _script(monkeypatch):
from app.providers import mock_model
calls = [(TOOL, {"sql": TOTALS}), (TOOL, {"sql": BY_REGION}), (DECOMPOSE, ARGS),
(BASELINE, {"query": "Q1", "period": "week", "measure": "gmv", "label": "销售额"}),
(COMPARABLE, {"query": "Q2", "period": "week", "base": "2026-W41", "target": "2026-W42", "measure": "gmv"})]
def decide(model, messages):
if not model.tools:
return AIMessage(content=TEXT)
done = sum(isinstance(m, ToolMessage) for m in messages)
if done < len(calls):
name, args = calls[done]
return AIMessage(content="", tool_calls=[{"name": name, "args": args, "id": f"call_{done}"}])
return AIMessage(content="销售额下降,主要来自华东。")
monkeypatch.setattr(mock_model.MockChatModel, "_decide", decide)
def _graph_with_tools():
graph = _graph()
bot = next(n for n in graph["nodes"] if n["id"] == "bot")
bot["data"]["config"]["tools"] = [TOOL, DECOMPOSE, BASELINE, COMPARABLE]
return graph
def _steps(chain):
"""同一步连着几条(几份输入、输入后面接的查询步骤)算一步。"""
steps = [s["step"] for s in chain]
return [s for i, s in enumerate(steps) if s.startswith("analysis") and steps[i - 1:i] != [s]]
async def test_scope_aliases_open_the_same_chain_as_the_phrase_in_the_text(monkeypatch):
_script(monkeypatch)
row = await _finish(_graph_with_tools())
assert row.status == "succeeded", row.error
_, doc, _ = await _report(row)
scope = doc["analysis_scope"]
assert [i["alias"] for i in scope["decomposed"]] == ["A1"]
assert [i["alias"] for i in scope["refused"]] == ["A2"] and [i["alias"] for i in scope["phenomena"]] == ["A3"]
phrase = next(s for s in iter_segments(doc) if s.get("ref") == "a:A1")
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
cited = (await client.get(f"/api/runs/{row.id}/evidence/segments/{phrase['id']}")).json()
body = (await client.get(f"/api/runs/{row.id}/evidence/analyses/A1")).json()
# 和点开正文里的判定语同一条链(同一份结论、同样复算一致),只是没有这一处引用的那几样
assert _steps(body["chain"]) == CHAIN
assert [s["step"] for s in body["chain"]] == [s["step"] for s in cited["chain"]]
assert body["rendered"] == phrase["text"] == body["chain"][0]["phrase"]
assert body["seal"]["covered"] is True and body["report"]["node_id"] == "write"
# 正文没引用的也点得开:算不了的波动基线照实给原因,可比性检查照样复算
refused = (await client.get(f"/api/runs/{row.id}/evidence/analyses/A2", params={"report": "write"})).json()
judged = refused["chain"][0]
assert (judged["op"], judged["status"], judged["reason_code"]) == ("baseline", "refused", "history_short")
assert judged["phrase"] == scope["refused"][0]["phrase"]
assert next(s for s in refused["chain"] if s["step"] == "analysis_recompute")["status"] == "same"
comparable = (await client.get(f"/api/runs/{row.id}/evidence/analyses/A3")).json()
assert comparable["chain"][0]["method_label"] == contract.method_label("comparable")
# 目录里没有的编号、不是分析结论的编号(Q1):404,说清是哪种
for alias in ("A9", "Q1"):
missing = await client.get(f"/api/runs/{row.id}/evidence/analyses/{alias}")
assert missing.status_code == 404 and missing.json()["code"] == "evidence_analysis_not_found"
async def test_a_tampered_report_does_not_open_its_scope(monkeypatch):
_script(monkeypatch)
row = await _finish(_graph_with_tools())
checked, _, _ = await _report(row)
# 报告文档被改过(哈希对不上):和片段接口同一个错误码,不给出处
path = artifact_store._path_of(checked["doc_artifact"])
path.write_bytes(path.read_bytes().replace("本周销售".encode(), "本周营业".encode(), 1))
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
res = await client.get(f"/api/runs/{row.id}/evidence/analyses/A1")
assert res.status_code == 409 and res.json()["code"] == "evidence_doc_tampered"
@@ -0,0 +1,88 @@
"""分析工具的 tool.end 带的那一小段判定(app/tools/analysis.event_view):时间线照它显示算子、判定语和编号,不从 preview 里拼。
preview 只是结论 JSON 的前一截(截到 2000 个字符),可加拆解、可比性检查的判定语都在截掉的那段里;tool.end 的 analysis 另带
编号、算子、模式、状态、判定码、方向、判定语、算不了的原因码。编号和交回模型的「【分析 An】」是同一个;写错的调用(tool.error)
不带这一段、不占编号。接真算子(不用替身),库是虚构的零售库 shop,模型换成脚本(tests/test_analysis_tools.Script)。
"""
from __future__ import annotations
import json
from langchain_core.messages import ToolMessage
from app.analysis import contract
from app.core import artifact_store
from app.engine.runner import run_manager
from app.tools.analysis import event_view
from tests.test_analysis_tools import ( # noqa: F401 - 夹具按名字引入
DECOMPOSE, DECOMPOSE_ARGS, SQL, TOOL, Script, agent_graph, engine_up, events, node, run_graph, shop, wait,
)
BASELINE = contract.TOOL_NAMES["baseline"]
COMPARABLE = contract.TOOL_NAMES["comparable"]
WRONG = {**DECOMPOSE_ARGS, "measure": "sales"}
#: 整体的周序列(Q2):波动基线、两周的可比性读它
TOTALS = "SELECT week, SUM(amount) AS gmv FROM orders GROUP BY week ORDER BY week"
WEEKLY = {"query": "Q2", "period": "week", "measure": "gmv", "label": "销售额"}
TWO_WEEKS = {"query": "Q2", "period": "week", "base": "2026-W41", "target": "2026-W42", "measure": "gmv"}
def _replies(script: Script) -> list[str]:
return [m.content for m in script.seen[-1] if isinstance(m, ToolMessage)]
def test_event_view_has_the_judgment_without_the_hypothesis():
result = {"op": "decompose", "mode": "additive", "status": "ok", "reason_code": None, "phrase": "销售额下降 194",
"verdict": {"code": "driver", "subjects": [["华东"]], "direction": "down"}, "hypothesis": "华东拖累",
"table": {"columns": ["region"], "rows": [["华东"]]}}
assert event_view(result, "A2") == {"alias": "A2", "op": "decompose", "mode": "additive", "status": "ok",
"verdict": "driver", "direction": "down", "phrase": "销售额下降 194",
"reason_code": None}
refused = {"op": "baseline", "mode": None, "status": "refused", "reason_code": "history_short",
"phrase": "波动基线没有做成:只有 1 期历史,至少要 8 期", "verdict": None}
assert event_view(refused) == {"alias": None, "op": "baseline", "mode": None, "status": "refused", "verdict": None,
"direction": None, "phrase": "波动基线没有做成:只有 1 期历史,至少要 8 期",
"reason_code": "history_short"}
async def test_tool_end_carries_the_phrase_and_the_same_number_the_model_got(monkeypatch, shop):
script = Script(monkeypatch, [(TOOL, {"sql": SQL}), (TOOL, {"sql": TOTALS}), (DECOMPOSE, WRONG),
(DECOMPOSE, DECOMPOSE_ARGS), (BASELINE, WEEKLY), (COMPARABLE, TWO_WEEKS)])
row = await run_graph(agent_graph(tools=(TOOL, DECOMPOSE, BASELINE, COMPARABLE)))
ends = [e for e in await events(row.id, "tool.end", "bot") if e.get("analysis_artifact")]
assert [e["analysis"]["alias"] for e in ends] == ["A1", "A2", "A3"]
# 交回模型的编号同一套:写错的那次不占编号
replies = [r.split("\n", 1)[0] for r in _replies(script) if r.startswith("【分析")]
assert replies == ["【分析 A1】", "【分析 A2】", "【分析 A3】"]
for end in ends:
result = artifact_store.load(end["analysis_artifact"])
assert end["analysis"] == event_view(result, end["analysis"]["alias"])
decompose, baseline, comparable = (e["analysis"] for e in ends)
assert (decompose["op"], decompose["mode"], decompose["status"], decompose["verdict"]) == (
"decompose", "additive", "ok", "driver")
assert decompose["phrase"].startswith("销售额下降") and decompose["phrase"] not in ends[0]["preview"], \
"可加拆解的判定语在 preview 截掉的那段里,只能从这一段读"
assert (baseline["status"], baseline["reason_code"]) == ("refused", "history_short")
assert baseline["phrase"].startswith(contract.OP_LABELS["baseline"] + "没有做成")
assert comparable["op"] == "comparable" and comparable["phrase"].startswith("两期")
# 写错的调用:tool.error 带调用错误的代码,不带判定
[error] = [e for e in await events(row.id, "tool.error", "bot")]
assert error["call_error"] == "unknown_column" and "analysis" not in error
async def test_a_team_member_numbers_its_own_analyses(monkeypatch, shop):
Script(monkeypatch, [(TOOL, {"sql": SQL}), (DECOMPOSE, DECOMPOSE_ARGS)],
route={"assignments": [{"agent": "分析员", "instruction": "拆一下销售额的变化"}], "done": False})
nodes = [node("start", "input"),
node("team", "supervisor", goal="销售额为什么下降", approval="never", max_rounds=2, agents=[
{"name": "分析员", "system": "你负责拆解", "tools": [TOOL, DECOMPOSE], "max_steps": 4}]),
node("out", "output", fields=[{"name": "a", "value": "{{ nodes.team.text }}"}])]
run = await run_manager.start(graph={"nodes": nodes, "edges": [{"source": "start", "target": "team"},
{"source": "team", "target": "out"}]},
input_payload={})
row = await wait(run.id)
assert row.status == "succeeded", row.error
[end] = [e for e in await events(row.id, "tool.end", "team") if e.get("analysis_artifact")]
assert end["agent"] == "分析员"
assert (end["analysis"]["alias"], end["analysis"]["op"], end["analysis"]["verdict"]) == ("A1", "decompose", "driver")
assert json.dumps(end["analysis"], ensure_ascii=False).count("下降是不是集中在某个地区") == 0, "Agent 写的假设不带"
+13 -1
View File
@@ -470,7 +470,7 @@ def test_the_sample_pages_for_the_browser_check_are_sealed_for_real():
got = {name: json.loads(data_block(page)) for name, page in rendered.items()}
assert set(got) == {"base", "two-reports", "metric-only", "legacy", "failed", "rejected", "continued", "degraded",
"missing-doc", "missing-card", "nan", "stray", "masked-card", "masked-old-doc", "masked-source",
"formats", "formats-unit", "prose"}
"formats", "formats-unit", "prose", "analysis"}
for name, bundle in got.items():
assert browser_manifest(bundle["events"]) == bundle["meta"]["manifest_hash"], name
@@ -557,3 +557,15 @@ def test_the_sample_pages_for_the_browser_check_are_sealed_for_real():
# 口径卡单位带尾随空白:服务端把「元 」原样接在数后面,正文里的销售额带着那个空白
shown = {s.get("ref"): s["text"] for s in segments(got["formats-unit"])}
assert shown["m:gmv"] == "74,613.65元 " and shown["m:gmv|万"] == "7.46万元 "
# 异动周报:三份分析结论(导出时都复算一致)和两份输入快照随文件附带;文档带着系统写的分析范围,正文没引用的 A2、A3
# 只在范围里点名;分析工具的 tool.end 结论随文件附带了,带规范字符串
analysed = got["analysis"]
kinds = sorted((a["kind"], a.get("node_id"), "b64" in a) for a in analysed["artifacts"])
assert kinds == [("analysis_result", "bot", True)] * 3 + [("query", "bot", True)] * 2 + [("report_doc", "write", True)]
assert {a["recheck"]["status"] for a in analysed["artifacts"] if a["kind"] == "analysis_result"} == {"same"}
doc = json.loads(base64.b64decode(next(a["b64"] for a in analysed["artifacts"] if a["kind"] == "report_doc")))
scope = doc["analysis_scope"]
assert [[i["alias"] for i in scope[k]] for k in ("phenomena", "decomposed", "refused")] == [["A3"], ["A1"], ["A2"]]
assert scope["not_decomposed"] == ["渠道"] and scope["decomposed"][0]["dims"] == ["地区"]
assert {s["ref"] for s in segments(analysed) if s["ref"].startswith("a:")} == {"a:A1.top_share|pct"}
assert all("canonical" in e for e in analysed["events"] if e["type"] == "tool.end")