Files
AgentLab/backend/tests/test_lint_contract_report_from.py
T
ThearchyHelios d071bf119f 可点击证据第三期:表名字段名和引文也点得开、可疑实体与结论句、受管门禁 G1–G5、证据审计导出
一、表结构、实体与引文

- 数据源工具查库时顺带存一份表结构快照(schema_snapshot),和查询快照一起进台账、进封存;
  台账里的查询记下 SQL 用到的表
- 报告里的表名、字段名可以点:反引号里的名字一律核对,正文里的裸名字只在明显是标识符时才自动链接,
  常见英文单词不链接;点开看到它出现在哪次查询、类型、所属的表,快照被截断时写明
- 找不到的名字标成可疑实体:在快照、SQL、结果列里都没有的记「可能是编造的名字」并给出最接近的 3 个;
  快照被截断、核对不了的记「核对不了」,不冤枉成编造
- 检索引文 [[q:K1|原文]]:原文在检索快照的命中片段里逐字出现(空白归一化后)才算有出处,点开高亮原文位置
- 自动链接的名字不算句子的引用,只有写作者写的引用才算

二、结论句与出具

- 报告节点新增 claims:off(默认,保持原样)或 require_citation(没有引用的结论句计入缺口)
- 受管级别的正式运行里,可疑实体和没有引用的结论句按「有缺口」降档,不拦截;探索运行和已发布级别只标注
- 契约的 claims 和报告节点的设置取更严的那个;受管正式运行里写 ignore 也按降档处理,并给出警告
- 出具横幅除了数字,也写出可疑实体和没有引用的结论句

三、受管门禁 G1–G5(只看图和配置)

- G1 带契约的出口,成果字段的文字只能来自报告撰写节点
- G2 模型写的文字不绕过报告撰写节点直接流进出口
- G3 报告撰写节点写明 numbers: strict、on_violation: fail、claims: require_citation
- G4 口径卡的输入不来自计算角色的沙箱代码
- G5 给口径卡供数的 agent 配 output_schema 并开 cite_fields
- 受管级别是错误、挡住发布;已发布级别只给警告;已经发布的版本照常运行,下次发布时按新规则检查
- 每条规则都接进发布前自动修复(一键修复 / 给选项 / 交给 Copilot);禁止的改动补上关 cite_fields、
  删 output_schema、放宽契约 claims;Copilot 兜底不许自己把代码节点标成 source、不许放宽报告节点
- 发布弹窗里选「交给 Copilot」时明确写「正在交给 Copilot」

四、旧运行与审计

- 没有契约的旧答案:数字和已封存的查询单元格、口径卡值按数值比对,每个数最多 3 个候选,
  默认折叠,写明「猜测的来源,不能当证据」
- 有契约的旧运行:候选只从已封存的口径卡里找,人为插进工件表的行不当证据
- 证据审计:GET /api/runs/{id}/evidence/audit 按「无证据 / 可疑实体 / 有出处 / 旧运行猜测」分组,
  可导出 JSON 和 CSV(带封存状态);记录页和画布上的报告节点显示「有出处 N · 无证据 M」

五、SQL 守卫修正(安全)

- 去注释改成单遍扫描,认得字符串和各方言的引号(单引号转义、MySQL 反斜杠与 #、PostgreSQL 的 $$ 与嵌套注释、
  Oracle 的 q'[…]'、SQLite 的方括号);以前字符串里的 -- 会被当成注释,SELECT '--'; DROP … 这类写法能骗过多语句拦截
- 看不到服务器设置时每种读法都扫一遍,读法之间切分不一致就拒绝,任何一种读法是写操作就按写操作处理
- 执行查询和工具危险判定都按数据源的方言检查

六、其他

- 修掉检查脚本里画布检查的一处竞态,并行估时按实测更新
2026-09-29 05:26:34 +08:00

106 lines
4.5 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.
"""发布门禁对引用模式契约(report_from)的判断。
以前 `_lint_contract` 不管有没有 report_from,一律要求 narrative:模板⑨这种只用
report_from 核对报告文档的契约,数字回指由报告撰写节点的文档保证、根本用不上
narrative,却因此发不成受管版本。有 report_from 就不再要 narrative;report_from
指错(不是报告撰写节点、不在出口上游)照样挡住发布。
"""
from __future__ import annotations
import copy
import pytest
from httpx import ASGITransport, AsyncClient
from app.engine.governance import lint_for_publish
from app.engine.schema import GraphSpec
from app.main import app
from app.seed import TEMPLATES
@pytest.fixture
async def client():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
yield c
def traceable() -> dict:
"""模板⑨(可追溯周报)的图:契约只写 report_from,没有 narrative。"""
tpl = next(t for t in TEMPLATES if t["name"].startswith("⑨"))
return copy.deepcopy({"nodes": tpl["nodes"], "edges": tpl["edges"]})
def contract_of(graph: dict) -> dict:
return next(n for n in graph["nodes"] if n["id"] == "done")["data"]["config"]["contract"]
def lint(graph: dict, level: str = "governed") -> list:
return lint_for_publish(GraphSpec.model_validate(graph), level=level).issues
async def publish(client, graph: dict, level: str = "governed") -> dict:
wf = (await client.post("/api/workflows", json={"name": "可追溯周报-门禁", "graph": graph})).json()
body = (await client.post(f"/api/workflows/{wf['id']}/publish", json={"level": level})).json()
await client.delete(f"/api/workflows/{wf['id']}")
return body
def test_the_template_contract_has_no_narrative_and_needs_none():
graph = traceable()
assert "narrative" not in contract_of(graph) and contract_of(graph)["report_from"] == "write"
errors = [i for i in lint(graph) if i.level == "error"]
assert errors == [], [i.message for i in errors]
async def test_the_template_publishes_as_governed(client):
body = await publish(client, traceable())
assert body["ok"] is True, body["issues"]
assert all(i["level"] == "warning" for i in body["issues"])
@pytest.mark.parametrize("target, why", [("fetch", "不是「报告撰写」"), ("ghost", "不存在")])
async def test_a_report_from_pointing_elsewhere_still_blocks(client, target, why):
graph = traceable()
contract_of(graph)["report_from"] = target
body = await publish(client, graph)
assert body["ok"] is False
bad = [i for i in body["issues"] if i["level"] == "error"]
assert [i["code"] for i in bad] == ["contract.report_from_invalid"], bad
assert why in bad[0]["message"] and bad[0]["node_id"] == "done"
async def test_a_report_that_is_not_upstream_still_blocks(client):
graph = traceable()
# 报告撰写节点挪到出口后面:出口核对时那份报告还没写出来
graph["edges"] = [e for e in graph["edges"] if e["target"] != "done"] + [
{"source": "caliber", "target": "done"}, {"source": "done", "target": "write"}]
body = await publish(client, graph)
assert body["ok"] is False
assert any(i["code"] == "contract.report_from_invalid" and "上游" in i["message"]
for i in body["issues"] if i["level"] == "error"), body["issues"]
@pytest.mark.parametrize("level, expected", [("governed", "error"), ("published", "warning")])
def test_without_report_from_or_narrative_the_gate_still_asks(level, expected):
graph = traceable()
del contract_of(graph)["report_from"]
[issue] = [i for i in lint(graph, level) if i.code == "contract.report_from_missing"]
assert issue.level == expected and issue.node_id == "done"
assert "report_from" in issue.message and "narrative" in issue.message
def test_an_empty_report_from_counts_as_missing():
graph = traceable()
contract_of(graph)["report_from"] = ""
assert any(i.code == "contract.report_from_missing" and i.level == "error" for i in lint(graph))
def test_a_narrative_contract_is_unchanged():
graph = traceable()
contract = contract_of(graph)
del contract["report_from"]
contract["narrative"] = "{{ nodes.write.text }}"
# _lint_contract 对叙述模式照旧;受管级别另有门禁 G1 要求核对报告撰写节点的文档
assert [i for i in lint(graph, "published") if i.level == "error"] == []
assert [i.code for i in lint(graph) if i.level == "error"] == ["governed.report_from_required"]