Files
AgentLab/backend/tests/test_publish_check_api.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

258 lines
14 KiB
Python

"""发布前检查(publish-check)和自动修复(autofix)两个接口。
- publish-check 的问题和真正发布时拦下的完全一致(同一套 validate + 门禁)
- 两个接口都只读:调用前后库里的工作流、版本、状态一个字都不变
- autofix 返回的图(首批里能 auto 的那几类)真的能过发布检查,存下来也真能发布
"""
from __future__ import annotations
import copy
import pytest
from httpx import ASGITransport, AsyncClient
from sqlalchemy import select
from app.db.base import SessionLocal
from app.db.models import Workflow, WorkflowVersion
from app.main import app
@pytest.fixture
async def client():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
yield c
def node(nid, ntype, label=None, **config):
return {"id": nid, "type": ntype, "position": {"x": 40, "y": 80}, "data": {"label": label or nid, "config": config}}
def weekly(**contract_overrides) -> dict:
contract = {"report_from": "write", "metrics_from": ["card"], "required": ["gmv"], "strict": True}
contract.update(contract_overrides)
nodes = [
node("start", "input", "周期"),
node("fetch", "tool", "取数", tool="db_query__shop",
args={"sql": "SELECT SUM(amount) AS gmv, COUNT(*) AS orders FROM orders"}),
node("card", "metrics", "周报口径卡", caliber="周报口径", metrics=[
{"id": "gmv", "name": "销售额", "unit": "元", "expression": "cell(nodes.fetch, 0, 'gmv')"},
{"id": "orders", "name": "订单数", "unit": "单", "expression": "cell(nodes.fetch, 0, 'orders')"}]),
node("write", "report", "报告撰写", instructions="写周报"),
node("done", "output", "出具", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}],
contract={k: v for k, v in contract.items() if v is not None}),
]
return {"nodes": nodes, "edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}
def broken(lib_id: str | None = None) -> dict:
"""首批里 auto 能修的几类问题各来一处。"""
graph = weekly(metrics_from=None, report_from="fetch", strict=False)
graph["nodes"].insert(2, node("ask", "agent", "查数员", tools=["db_query__shop"], approval="never",
prompt="用 db_query__shop 查"))
graph["edges"] += [{"source": "start", "target": "ask"}, {"source": "ask", "target": "card"}]
if lib_id:
graph["nodes"].insert(1, node("lib", "subgraph", "方法卡", workflow_id=lib_id))
graph["edges"] += [{"source": "start", "target": "lib"}, {"source": "lib", "target": "fetch"}]
return graph
async def create(client, graph: dict, name: str = "周报-发布检查") -> str:
r = await client.post("/api/workflows", json={"name": name, "graph": graph})
assert r.status_code == 201, r.text
return r.json()["id"]
async def library(client) -> str:
"""一张已发布过 v1、之后又存了 v2 草稿的上游:当前的发布版本是 v1。"""
lib = await create(client, weekly(), name="方法卡库")
assert (await client.post(f"/api/workflows/{lib}/publish", json={"level": "published"})).json()["ok"]
changed = weekly()
changed["nodes"][3]["data"]["label"] = "改过的报告"
await client.patch(f"/api/workflows/{lib}", json={"graph": changed})
return lib
async def state(*workflow_ids: str) -> list:
"""库里这些工作流和它们全部版本的每一列。"""
async with SessionLocal() as session:
rows = []
for wid in workflow_ids:
wf = await session.get(Workflow, wid)
rows.append({c.name: getattr(wf, c.name) for c in Workflow.__table__.columns})
versions = (await session.execute(select(WorkflowVersion).where(WorkflowVersion.workflow_id == wid)
.order_by(WorkflowVersion.version))).scalars()
rows += [{c.name: getattr(v, c.name) for c in WorkflowVersion.__table__.columns} for v in versions]
count = len((await session.execute(select(WorkflowVersion.id))).all())
return [rows, count]
def shape(issues: list[dict]) -> list[tuple]:
return [(i["level"], i.get("code"), i.get("node_id"), i.get("field"), i["message"]) for i in issues]
@pytest.mark.parametrize("level", ["governed", "published"])
async def test_publish_check_matches_what_publishing_blocks(client, level):
lib = await library(client)
wf = await create(client, broken(lib))
checked = (await client.post(f"/api/workflows/{wf}/publish-check", json={"level": level})).json()
published = (await client.post(f"/api/workflows/{wf}/publish", json={"level": level})).json()
assert checked["level"] == level and checked["ok"] is published["ok"] is False
assert shape(checked["issues"]) == shape(published["issues"])
assert all("fix" in i for i in checked["issues"])
async def test_publish_check_reports_codes_and_fixes(client):
lib = await library(client)
wf = await create(client, broken(lib))
body = (await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "governed"})).json()
fixes = {f["id"]: f for f in body["fixes"]}
assert {"governed.agent_approval_never:ask", "governed.subgraph_unpinned:lib", "contract.metrics_from_missing:done",
"contract.report_from_invalid:done", "contract.strict_off:done"} <= set(fixes)
# 钉的是当前的发布版本 v1,不是最新存的草稿 v2
assert fixes["governed.subgraph_unpinned:lib"]["preview"]["after"] == 1
assert all(not k.startswith("_") for f in body["fixes"] for k in f)
by_code = {i["code"]: i for i in body["issues"] if i.get("code")}
assert by_code["governed.agent_approval_never"]["fix"] == "governed.agent_approval_never:ask"
# 没有修复的问题 fix 是 null(老前端不认这两个字段也不影响)
assert all(i["fix"] is None for i in body["issues"] if not i.get("code"))
async def test_publish_check_takes_an_unsaved_graph(client):
wf = await create(client, weekly())
assert (await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "governed"})).json()["ok"] is True
body = (await client.post(f"/api/workflows/{wf}/publish-check",
json={"level": "governed", "graph": weekly(required=None)})).json()
assert body["ok"] is False and body["fixes"][0]["id"] == "contract.required_missing:done"
async def test_both_endpoints_leave_the_database_alone(client):
lib = await library(client)
wf = await create(client, broken(lib))
before = await state(wf, lib)
await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "governed"})
await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "published", "graph": weekly()})
fixes = (await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "governed"})).json()["fixes"]
out = (await client.post(f"/api/workflows/{wf}/autofix", json={
"level": "governed", "apply": [f["id"] for f in fixes if f["kind"] == "auto"]})).json()
assert out["applied"]
await client.post(f"/api/workflows/{wf}/autofix", json={"level": "governed", "graph": weekly(required=None),
"apply": ["contract.required_missing:done"],
"choices": {"contract.required_missing:done": ["gmv"]}})
assert await state(wf, lib) == before
async def test_autofixed_graph_passes_the_check_and_really_publishes(client):
lib = await library(client)
graph = broken(lib)
wf = await create(client, graph)
fixes = (await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "governed"})).json()["fixes"]
autos = [f["id"] for f in fixes if f["kind"] == "auto"]
assert len(autos) == 5, autos
out = (await client.post(f"/api/workflows/{wf}/autofix", json={"level": "governed", "apply": autos})).json()
assert sorted(out["applied"]) == sorted(autos) and out["rejected"] == [] and out["ok"] is True
assert [i for i in out["remaining"] if i["level"] == "error"] == []
assert {c["fix_id"] for c in out["changes"]} == set(autos)
assert all(op["op"] == "update_node" for op in out["ops"])
# 预览只改了修复涉及的字段
fixed = out["graph"]
assert [n["position"] for n in fixed["nodes"]] == [n["position"] for n in graph["nodes"]]
checked = (await client.post(f"/api/workflows/{wf}/publish-check",
json={"level": "governed", "graph": fixed})).json()
assert checked["ok"] is True, [i for i in checked["issues"] if i["level"] == "error"]
# 人确认后前端走现有的保存接口,再发布
await client.patch(f"/api/workflows/{wf}", json={"graph": fixed, "note": "发布前自动修复"})
published = (await client.post(f"/api/workflows/{wf}/publish", json={"level": "governed"})).json()
assert published["ok"] is True, published["issues"]
async def test_autofix_with_a_choice_and_without_it(client):
wf = await create(client, weekly(required=None))
fid = "contract.required_missing:done"
out = (await client.post(f"/api/workflows/{wf}/autofix", json={"level": "governed", "apply": [fid]})).json()
assert out["applied"] == [] and out["rejected"][0]["fix_id"] == fid and out["ok"] is False
assert [i["fix"] for i in out["remaining"] if i.get("code") == "contract.required_missing"] == [fid]
out = (await client.post(f"/api/workflows/{wf}/autofix", json={
"level": "governed", "apply": [fid], "choices": {fid: ["orders", "gmv"]}})).json()
assert out["ok"] is True
contract = next(n for n in out["graph"]["nodes"] if n["id"] == "done")["data"]["config"]["contract"]
assert contract["required"] == ["gmv", "orders"]
async def test_required_candidates_come_from_the_pinned_card(client):
lib = await create(client, weekly(), name="口径库")
local = weekly(required=None)
card = local["nodes"][2]["data"]["config"]
card.pop("metrics")
card["caliber_from"] = {"workflow_id": lib, "workflow_version": 1, "node_id": "card"}
wf = await create(client, local)
body = (await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "governed"})).json()
[fix] = [f for f in body["fixes"] if f["id"] == "contract.required_missing:done"]
assert fix["kind"] == "choice" and [o["value"] for o in fix["options"]] == ["gmv", "orders"]
async def test_bad_requests(client):
wf = await create(client, weekly())
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "gold"})
assert r.status_code == 400 and "「已发布」" in r.json()["detail"]
r = await client.post(f"/api/workflows/{wf}/autofix", json={"level": "gold", "apply": []})
assert r.status_code == 400
r = await client.post("/api/workflows/nope/publish-check", json={"level": "governed"})
assert r.status_code == 404
r = await client.post("/api/workflows/nope/autofix", json={"level": "governed", "apply": []})
assert r.status_code == 404
body = (await client.post(f"/api/workflows/{wf}/publish-check",
json={"level": "governed", "graph": {"nodes": [{"id": "a", "type": "nope"}]}})).json()
assert body["ok"] is False and body["fixes"] == [] and "读不懂" in body["issues"][0]["message"]
body = (await client.post(f"/api/workflows/{wf}/autofix", json={
"level": "governed", "graph": {"nodes": [{"id": "a", "type": "nope"}]}, "apply": ["x:y"]})).json()
assert body["ok"] is False and body["applied"] == [] and "读不懂" in body["remaining"][0]["message"]
async def test_the_draft_in_the_database_is_the_default(client):
wf = await create(client, weekly(strict=False))
graph = copy.deepcopy(weekly(strict=False))
out = (await client.post(f"/api/workflows/{wf}/autofix",
json={"level": "governed", "apply": ["contract.strict_off:done"]})).json()
assert out["applied"] == ["contract.strict_off:done"]
contract = next(n for n in out["graph"]["nodes"] if n["id"] == "done")["data"]["config"]["contract"]
assert contract["strict"] is True
assert (await client.get(f"/api/workflows/{wf}")).json()["graph"] == graph
@pytest.mark.parametrize("contract", ["abc", ["gmv"], 5])
def test_a_contract_that_is_not_an_object_is_a_validation_error(contract):
"""非空、却不是对象的契约:validate 报一条 error(两档发布、发起运行都挡住),不在 .get 上崩。"""
from app.engine.governance import publish_issues
from app.engine.schema import GraphSpec, validate_graph
graph = weekly()
graph["nodes"][4]["data"]["config"]["contract"] = contract
spec = GraphSpec.model_validate(graph)
[issue] = [i for i in validate_graph(spec).issues if i.code == "contract.not_object"]
assert issue.level == "error" and issue.node_id == "done" and issue.field == "contract"
assert "JSON 对象" in issue.message
for level in ("published", "governed"):
assert any(i.code == "contract.not_object" and i.level == "error" for i in publish_issues(spec, level=level))
@pytest.mark.parametrize("level", ["governed", "published"])
async def test_a_string_contract_is_reported_not_a_crash(client, level):
graph = weekly()
graph["nodes"][4]["data"]["config"]["contract"] = "report_from=write"
wf = await create(client, graph)
before = await state(wf)
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": level})
assert r.status_code == 200, r.text
body = r.json()
assert body["ok"] is False
assert [(i["code"], i["node_id"]) for i in body["issues"] if i["level"] == "error"][0] == (
"contract.not_object", "done")
# 修不了确定性修复:交给 Copilot
[fix] = [f for f in body["fixes"] if f["code"] == "contract.not_object"]
assert fix["kind"] == "assist" and fix["node_id"] == "done"
r = await client.post(f"/api/workflows/{wf}/autofix", json={"level": level, "apply": [fix["id"]]})
assert r.status_code == 200 and r.json()["ok"] is False and r.json()["applied"] == []
r = await client.post(f"/api/workflows/{wf}/publish", json={"level": level})
assert r.status_code == 200 and r.json()["ok"] is False
assert await state(wf) == before