From 6503b095cb6b630e70776ed411b4957b7a0bc629 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Wed, 21 Oct 2026 16:04:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A5=E5=91=8A=E6=92=B0=E5=86=99=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E7=9A=84=E3=80=8C=E4=B8=8A=E6=B8=B8=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E5=BC=95=E7=94=A8=E7=9A=84=E6=9D=A5=E6=BA=90=E3=80=8D?= =?UTF-8?q?=E8=AE=A4=E5=88=86=E6=9E=90=E7=BB=93=E8=AE=BA=EF=BC=88=5FCITABL?= =?UTF-8?q?E=20=E5=8A=A0=E5=88=86=E6=9E=90=E7=BB=93=E8=AE=BA=EF=BC=8C=5Fno?= =?UTF-8?q?=5Fevidence=20=E7=85=A7=2000-=E6=80=BB=E6=96=B9=E6=A1=88=20D10?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E3=80=8C=E8=AF=81=E6=8D=AE=E6=9D=A5=E8=87=AA?= =?UTF-8?q?=E3=80=8D=E5=8F=AA=E9=80=89=E4=BA=86=E5=88=86=E6=9E=90=E5=8D=A1?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E5=86=8D=E8=AF=AF=E6=8A=A5=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E5=BC=95=E7=94=A8=E7=9A=84=E6=9D=A5=E6=BA=90=EF=BC=9B?= =?UTF-8?q?=E5=88=86=E6=9E=90=E5=8D=A1=E5=81=9A=E7=9A=84=E7=BB=93=E8=AE=BA?= =?UTF-8?q?=E6=80=BB=E8=83=BD=E5=BC=95=E7=94=A8=EF=BC=8C=E5=8F=97=E7=AE=A1?= =?UTF-8?q?=E7=BA=A7=E5=88=AB=E7=9A=84=E6=AD=A3=E5=BC=8F=E5=87=BA=E5=85=B7?= =?UTF-8?q?=E6=B2=A1=E5=BC=80=E3=80=8C=E5=8D=95=E5=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E6=9F=A5=E8=AF=A2=E6=97=B6=E4=B8=80?= =?UTF-8?q?=E5=AD=97=E4=B8=8D=E5=8F=98=E3=80=82=E6=B5=8B=E8=AF=95=EF=BC=9A?= =?UTF-8?q?=E5=8F=AA=E6=9C=89=E5=88=86=E6=9E=90=E5=8D=A1=E7=BB=93=E8=AE=BA?= =?UTF-8?q?=EF=BC=88=E5=BC=80=E6=B2=A1=E5=BC=80=E5=8D=95=E5=85=83=E6=A0=BC?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E9=83=BD=E4=B8=8D=E6=8A=A5=EF=BC=89=E3=80=81?= =?UTF-8?q?Agent=20=E7=9A=84=E5=88=86=E6=9E=90=E6=8C=89=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=A0=BC=E5=BC=95=E7=94=A8=E7=9A=84=E8=A7=84=E7=9F=A9=E3=80=81?= =?UTF-8?q?=E4=BB=80=E4=B9=88=E9=83=BD=E6=B2=A1=E6=9C=89=E6=97=B6=E7=82=B9?= =?UTF-8?q?=E5=90=8D=E5=88=86=E6=9E=90=E7=BB=93=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/engine/nodes/report.py | 18 ++++++---- backend/tests/test_report_analysis_claims.py | 37 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/backend/app/engine/nodes/report.py b/backend/app/engine/nodes/report.py index 8cfa16b1..84a9708b 100644 --- a/backend/app/engine/nodes/report.py +++ b/backend/app/engine/nodes/report.py @@ -54,6 +54,7 @@ from typing import Any from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage from langgraph.func import task +from app.analysis.contract import LEDGER_KIND from app.core.errors import describe_exception, not_configured from app.core.events import EventType from app.db.base import SessionLocal @@ -73,6 +74,7 @@ from app.engine.evidence import ( UNIT_RULE, StreamRenderer, WriterCatalog, + analysis_card, build_catalog, catalog_budget, compose_doc, @@ -434,19 +436,23 @@ def report_cells_allowed(spec: GraphSpec, node: GraphNode, *, governed: bool) -> #: 目录里能被报告引用的几种来源(沙箱代码节点的登记只为说清为什么不能引用,不算) -_CITABLE = {"metric": "口径卡指标", "query": "查询结果", "retrieval": "知识库检索", "input": "运行输入"} +_CITABLE = {"metric": "口径卡指标", "query": "查询结果", "retrieval": "知识库检索", "input": "运行输入", + LEDGER_KIND: "分析结论"} def _no_evidence(catalog: dict[str, Any], cells: bool) -> str | None: - """目录里没有可引用的来源时给一句话,说清缺的是哪种;有就返回 None。""" + """目录里没有可引用的来源时给一句话,说清缺的是哪种;有就返回 None。分析结论按 00-总方案 D10 认:分析卡做的总能引用 + (判定语、具名的数),Agent 现场做的和它查的格同一个规矩——受管级别的正式出具没开「单元格引用」时引用不了。""" kinds = {entry.get("kind") for entry in catalog.values()} if not kinds & set(_CITABLE): return ("报告撰写节点的上游没有可引用的来源(" + "、".join(_CITABLE.values()) + "都没有)," f"报告中的数字都会被判为没有出处。请在它前面接入取数节点或口径卡,或检查「{field_label('metrics_from')}」") - if not cells and not kinds & {"metric", "input"}: - # 只有查询和检索:单元格引用不了,检索片段本来就只能当依据,一个数都没处引 - return (f"报告撰写节点的上游只有查询结果,没有口径卡指标,而{CELLS_REASON},当前无法引用单元格," - "报告中的数字都会被判为没有出处。请在出具契约中开启「单元格引用」,或把要写的数字登记到口径卡") + if not cells and not kinds & {"metric", "input"} and not any(analysis_card(e) for e in catalog.values()): + # 只有查询、检索和 Agent 现场做的分析:单元格和这些分析结论引用不了,检索片段本来就只能当依据,一个数都没处引 + agent = LEDGER_KIND in kinds + return (f"报告撰写节点的上游只有查询结果{'和 Agent 现场做的分析' if agent else ''},没有口径卡指标,而{CELLS_REASON}," + f"当前无法引用单元格{'和这些分析结论' if agent else ''},报告中的数字都会被判为没有出处。" + "请在出具契约中开启「单元格引用」,或把要写的数字登记到口径卡") return None diff --git a/backend/tests/test_report_analysis_claims.py b/backend/tests/test_report_analysis_claims.py index c22eef74..cdfcb074 100644 --- a/backend/tests/test_report_analysis_claims.py +++ b/backend/tests/test_report_analysis_claims.py @@ -265,6 +265,43 @@ async def test_the_scope_is_written_only_when_there_are_conclusions(monkeypatch, assert "analysis_scope" not in doc +# -------------------------------------------------------------------------- +# 上游的可引用来源认分析结论(00-总方案 D10) +# -------------------------------------------------------------------------- + + +def card_entry(alias: str) -> dict: + """分析卡做的结论(台账条目带 item、不带 call_id)。""" + return {**a_entry(alias, fake_result()), "item": "drop", "node_id": "card"} + + +def agent_entry(alias: str) -> dict: + """Agent 的分析工具现场做的结论(带 call_id)。""" + return {**a_entry(alias, fake_result()), "call_id": "call-1", "node_id": "ask"} + + +@pytest.mark.parametrize("cells", [True, False]) +def test_conclusions_from_an_analysis_card_are_citable_evidence(cells): + """「证据来自」只选了分析卡:分析卡的结论总能引用(判定语、具名的数),不报「上游没有可引用的来源」;受管级别的正式 + 出具没开「单元格引用」时也不报「只有查询结果」。""" + assert report_mod._no_evidence({"A1": card_entry("A1")}, cells) is None + assert report_mod._no_evidence({"Q1": {"kind": "query"}, "A1": card_entry("A1")}, cells) is None + + +def test_conclusions_from_an_agent_follow_the_cells_rule(): + catalog = {"Q1": {"kind": "query"}, "A1": agent_entry("A1")} + assert report_mod._no_evidence(catalog, True) is None + warning = report_mod._no_evidence(catalog, False) + assert warning and "只有查询结果和 Agent 现场做的分析" in warning and "无法引用单元格和这些分析结论" in warning + plain = report_mod._no_evidence({"Q1": {"kind": "query"}}, False) + assert plain and "只有查询结果,没有口径卡指标" in plain and "Agent" not in plain + + +def test_nothing_citable_names_analysis_conclusions_too(): + warning = report_mod._no_evidence({}, True) + assert warning and all(kind in warning for kind in ("口径卡指标", "查询结果", "知识库检索", "运行输入", "分析结论")) + + # -------------------------------------------------------------------------- # 判断词不改一句话算不算结论:已发布版本的档位不变 # --------------------------------------------------------------------------