Files
AgentLab/backend/tests/test_lint_contract_report_from.py
T
ThearchyHelios fcdcc30d4e 可点击证据第二期:取数链路打通、问数据的答案逐段可点;发布前自动修复
一、可点击证据第二期

取数链路
- 智能体、工具节点、代码节点、知识检索取数时都记进证据台账;报告目录按台账编出 Q1… / K1…
- 报告可以引用快照里的一格([[v:Q1.r0.gmv]]),也可以由系统从快照生成整张表([[table:Q1 cols=… rows=0-4]]),
  表里每一格都是可点的片段;取值一律以快照为准,取回快照时复验哈希
- 口径卡表达式新增 cell(x, row, column),lineage 精确到单元格
- 智能体开 cite_fields(配合 output_schema)后,循环结束做一次结构化抽取,每个字段标出取自哪次查询的哪一格,
  再到快照里核对:一致记 verified;不一致以快照为准并告警;查不到的记为空值,永远不兜底成 0
- 代码节点新增 evidence_role(source / compute)和 code_sha;沙箱算出来的数不能被报告直接引用,要进口径卡
- 查询快照记下列类型,文本列按文本渲染(年份 "2026" 不再显示成「2,026」)
- 升级前发起的运行(停在审批上的)一律走旧逻辑:新的 task 只追加在循环之后,恢复时不会错号

证据接口与面板
- 片段链接上查询步骤:SQL、被引用的行加前后各 2 行、高亮到格;数据源可设 mask_columns 遮罩列,
  遮罩在查询时记进快照,数据源事后改名或删除也照样遮罩(在有身份体系之前,遮罩只减少暴露,不是安全边界)
- 输入来源写明核对结果(与快照一致 / 模型报的值和快照不一致 / 没查到记为空);口径卡缺输入时也给出完整的链
- 受管出具要在契约里声明 cells 才能引用单元格;受管与否记在所钉版本上,改画布不再让受管版本的正式运行降级
- 口径卡可以用 caliber_from 钉住另一个已发布工作流里的口径卡,上游有新版本时按 upgrade_policy 处置,
  和子工作流同一套规则;子工作流节点也补上了升版处置的配置项

Copilot、校验与报错
- 问数据的答案里有数字时,搭 input → 智能体 → 报告 → 出口,报告直接引用查过的单元格
- 写 SQL 前核对数据源的表结构,SQL 里出现表结构里没有的列会被打回
- 不再搭「智能体文字 → 转换节点解析 JSON」这种图;转换节点的 JSON 报错分清是上游模型写坏了、
  模板里裸插了模型文字(给出 | json 的改法),还是模板本身写错了
- 工具节点遇到「查询失败」「SQL 被拒绝」判为失败;发起运行前检查绑定的工具是否存在,不存在时直接拒绝并给出入口

二、发布前自动修复

- 问题带上机器编号(code)和修复 id;新增只读接口 POST /api/workflows/{id}/publish-check 和 /autofix
- 修复分三类:答案唯一、不降低要求的一键修复;要人拿主意的给选项(不替人选);结构性问题交给 Copilot
- 一律先预览,不自动保存、不自动发布;删节点、删契约、放宽审批、关掉 strict、改级别等降低要求的改动一律拒绝,
  Copilot 的方案也要过这一关;应用后重跑检查,错误必须变少且不能冒出新的
- 发布弹窗打开就先检查;问题面板新增「发布前检查」,平时就能看到、就地修掉
- 门禁修正:契约有 report_from 时不再要求 narrative(模板⑨可以发成受管);全图默认审批为「全部放行」时同样拦下
- 修掉画布保存会抹掉全图默认(defaults)的老问题:以前任何一次保存都会把它清空,审批 always 因此被悄悄放宽

验证(沙箱):tsc 干净,生产构建通过,pytest 2268 passed(两遍),check-all 17 项检查、3915 项全过
(新增 check-publish);关键行为做了变异验证、全部抓到。
2026-09-28 21:20:04 +08:00

104 lines
4.2 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 }}"
assert [i for i in lint(graph) if i.level == "error"] == []