可点击证据第五期:老工作流一键升级为可追溯结构;受管问数据图的 cells 一键选;pytest 并行

一、一键升级(只给预览,不自动保存)

- engine/upgrade.py:确定性改写,纯函数、重复执行结果不变
  - R1 契约的叙述指向模型调用节点 → 换成报告撰写节点(system 和 prompt 合成写作要求,模型配置照搬),契约改成 report_from
  - R2 没有契约、但模型写的文字进了出口,而且上游有取数 → 同样换成报告撰写
  - R3 input → 智能体 → 出口(问数据的典型图)→ 中间插入报告撰写节点,出口字段接报告的正文;
    只改直接相连的出口,经过别的节点读它的出口原样保留并写明原因
  - R4 配了 output_schema 的智能体 → 打开 cite_fields
  - R5 代码节点的产出被口径卡读 → 不改,只建议标成取数,等人确认
  - 一个叙述里混了几个写作者的,不猜,整步不做并写明原因
- POST /api/copilot/upgrade-evidence:返回改动、说明和改完的检查结果;assist 时再交给 Copilot 把纯算术的代码节点
  改写成口径卡表达式,不是纯算术的保留并给警告
- 升级和发布前修复走同一道「不许降低要求」的检查,只多两个由升级点名的例外:模型调用换成报告撰写、R3 改接的那条连线;
  改完不能更糟
- 校验对符合 R1–R4 的旧结构给一条建议「可以升级为可追溯结构」,不算提示、不挡发布
- Copilot 自查:报告撰写节点上游没有任何能引用数字的来源时打回

二、界面

- 问题面板的快速修复「升级为可追溯结构(预览改动)」:逐项写「节点 · 字段:原值 → 新值」,节点类型的变化写成
  「模型调用 → 报告撰写」,新插的节点和改接的连线另外写明;「应用」后走现有的保存,全图默认不丢
- 画布改过、存失败、画布被锁都有明确状态;保存失败后手动存成功会清掉「没存上」的提示,
  画布和预览对不上时「重试保存」不再盲存;校验接口失败时升级块照样在
- 记录页里没有逐段证据的运行给横幅「这次的报告没有逐段证据 → 升级这张图」,跳到编排页并打开升级预览
- 修掉一处旧缺陷:应用修复时只换配置、不换节点类型,模型调用换成报告撰写后存下去的还是模型调用

三、受管门禁:cells 一键选

- 图里没有口径卡、契约又没写 cells: true 的受管图,以前能过门禁、正式运行却每次失败;现在门禁拦下,
  发布弹窗给两个选项:写 cells: true,或交给 Copilot 改成口径卡指标(人亲手选的不算替人打开,Copilot 自己打开照样拦)
- 「修完要更好」的复核不再把改完才冒出来、只能由人拍板的这一处算作改坏:Copilot 补上契约、把 cells 留给人问,
  这一步会被采用,不再被判「冒出新的问题」作废

四、测试

- 开发依赖加上 pytest-xdist(锁文件只多了 pytest-xdist 和 execnet):全量后端测试 -n auto 约 16 秒,原来串行约 2 分钟
- 修掉并行下暴露的一处测试竞态:运行先落终态、再另一个事务封存,测试改成等封存完成再核对
This commit is contained in:
ThearchyHelios
2026-09-29 13:27:27 +08:00
parent 9727c21d8d
commit 050bea4b72
22 changed files with 3870 additions and 72 deletions
+319 -4
View File
@@ -3,9 +3,10 @@ from __future__ import annotations
import asyncio
import copy
import json
import math
import re
import time
from typing import Any
from typing import Any, Literal
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field
@@ -738,7 +739,7 @@ async def generate(
return GenerateOut(
graph=out,
explanation=str(raw.get("explanation", "")),
issues=[i.model_dump() for i in report.issues]
issues=[i.model_dump() for i in report.issues if i.level != "info"]
+ _issue_dicts(scope_issues(out["nodes"], scope), "datasource_out_of_scope")
+ dropped_tool_warnings(changes, payload.instruction),
layout=layout,
@@ -1033,6 +1034,62 @@ def _changed(spec: GraphSpec, baseline: list[dict[str, Any]] | None) -> set[str]
return {n.id for n in spec.nodes if n.id not in before or before[n.id] != n.config}
_NUMERIC_TYPES = ("number", "integer", "float")
def _numeric_input(field: Any) -> bool:
"""入口的这个字段看得出是数:写了数值类型,或者默认值是数(画布上填的默认值是字符串,能读成数的也算)。"""
if not isinstance(field, dict) or not str(field.get("name") or "").strip():
return False
if str(field.get("type") or "").strip().lower() in _NUMERIC_TYPES:
return True
default = field.get("default")
if isinstance(default, bool):
return False
if isinstance(default, (int, float)):
return math.isfinite(default)
if isinstance(default, str) and default.strip():
try:
return math.isfinite(float(default.strip().replace(",", "")))
except ValueError:
return False
return False
def _evidence_source(node: Any) -> bool:
"""报告撰写能从这个节点拿到可引用的证据:口径卡、调用工具、配了工具的 agent、知识检索,以及声明了数值字段的
入口(运行输入进证据目录,报告用 [[i:字段]] 引用)。子工作流、协作团队里面是什么看不见,一律算有——这条会把图
打回去让模型改,宁可漏判不可误判。
入口只声明了文字字段的(问题、主题)不算:报告能照引的只有问题本身,要写的数照样没有出处。画布上的入口字段
不写类型,没写默认值的字段看不出是不是数,也不算——提示里说明了怎么声明(写数值默认值)。"""
if node.type == NodeType.AGENT:
return bool(node.config.get("tools"))
if node.type == NodeType.INPUT:
fields = node.config.get("fields")
return isinstance(fields, list) and any(_numeric_input(f) for f in fields)
return node.type in (NodeType.METRICS, NodeType.TOOL, NodeType.RETRIEVE, NodeType.SUBGRAPH, NodeType.SUPERVISOR)
def _unsourced_reports(spec: GraphSpec, changed: set[str]) -> list[dict[str, Any]]:
"""这一轮新加或改过的报告撰写节点,上游一个证据来源都没有:运行时只给一条警告,报告里的数全被判成没有出处。"""
from app.engine.schema import _ancestors
nodes = spec.node_map()
out = []
for node in spec.nodes:
if node.id not in changed or node.type != NodeType.REPORT:
continue
if any(_evidence_source(nodes[a]) for a in _ancestors(spec, node.id) if a in nodes):
continue
out.append({"level": "error", "node_id": node.id, "edge_id": None, "field": None, "code": "report_no_source",
"message": f"「{node.title}」(报告撰写)的上游没有能引用数字的证据来源:没有口径卡、查库的 Agent 或调用工具、"
"知识检索,入口也没有看得出是数的字段,报告里写的数都会被判成没有出处。在它前面接取数的节点"
"(要算比率、增幅再加口径卡),连线连到它;要写的数本来就是运行输入的话,在入口字段上写明数值默认值,"
"报告用 [[i:字段名]] 引用"})
return out
def authored_issues(spec: GraphSpec, sources: list[Any], baseline: list[dict[str, Any]] | None = None,
) -> list[dict[str, Any]]:
"""助手搭图时要打回、运行时却不挡的写法,按 error 交回模型改:
@@ -1040,9 +1097,12 @@ def authored_issues(spec: GraphSpec, sources: list[Any], baseline: list[dict[str
- sql_unknown_column:调用工具节点的 SQL 里有数据源结构(schema_cache)里查不到的列——凭空
猜的列名跑起来只会「no such column」,还白跑前面的步骤
- parse_model_text:整形节点按 JSON 解析 agent / llm 写的文字(validate 里只是 warning:旧图照跑)
- report_no_source:报告撰写节点的上游没有任何证据来源(运行时只给一条警告,报告里的数全没有出处)。
metrics_from 指向不是口径卡的节点,validate 本来就报 error,自查照样交回去
"""
changed = _changed(spec, baseline)
out: list[dict[str, Any]] = []
out += _unsourced_reports(spec, changed)
for issue in text_parse_issues(spec):
if issue.node_id in changed:
# 模型的文字放在模板引号里的那种,改法是 | json,不是换结构化输出
@@ -1591,7 +1651,8 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
spec, layout = _layout_keeping(
GraphSpec.model_validate({"nodes": list(nodes.values()), "edges": edges}), pinned,
)
issues = [i.model_dump() for i in validate_graph(spec).issues]
# 建议(info,比如「可以升级为可追溯结构」)不进这一轮的问题清单:图刚按规则搭好,画布自己的校验会列出来
issues = [i.model_dump() for i in validate_graph(spec).issues if i.level != "info"]
issues += _issue_dicts(scope_issues(list(nodes.values()), scope),
"datasource_out_of_scope")
# 自查没改好的猜列名留在画布上:运行时不挡(结构缓存可能旧了),但要看得见
@@ -1633,8 +1694,12 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
_LEVEL_WORD = {"published": "已发布", "governed": "受管"}
_ASSIST_RULES = """\
#: 发布前修复和一键升级共用的硬性约定。拆开写是为了升级的兜底换掉「只修下面列出的问题」那一条,
#: 其余的(不许降低要求、要人拿主意的不替人选、只许用哪几种操作)一字不差
_RULES_HEAD = """\
硬性约定(违反任何一条,这次修改整个作废):
"""
_NO_LOWERING = """\
- 不许降低要求:不删节点、不删连线,add_node 不许用图里已有的节点 id(那等于把旧节点删了重建),也不换节点类型;
不删掉或清空出具契约,不删 required 里的指标,不把 strict 改成 false,不往 allow_numbers 里加数,不替人打开 cells;
审批策略只能往严里改(每次调用都审批 > 仅危险工具需要审批 > 全部自动放行),不放宽、不删掉,更不改成 never;
@@ -1645,14 +1710,24 @@ _ASSIST_RULES = """\
judge.on_unsupported 是 withhold 的不改回 degrade、不删掉;update_node 会把 judge 整个换掉,补预算时把原来的
judge(包括从全图默认继承来的)整份带上,只加 max_cost_usd;契约里的 claims 也一样;
不关 agent 的 cite_fields,不删它的 output_schema
"""
_ASK_HUMAN = """\
- 要人拿主意的,不替人选、不编:required 该包括哪些指标、几张口径卡该用哪张、钉哪个版本、协作团队拆成哪几个
固定步骤、沙箱代码节点是在取数还是在计算(不许自己把 evidence_role 设成 source)……每一处输出一行
{"op":"question","node_id":"节点 id","text":"要问的话,把候选列出来"},那一处不改
"""
_ONLY_LISTED = """\
- 只修下面列出的问题,别的不要动
"""
_OPS_ONLY = """\
- 可用操作只有 update_node(config 只写要改的字段,没写的原样保留)、add_node、add_edge 和 question;
不要输出 remove_node、remove_edge、reply
"""
_PLAN_DONE = """\
- 第一行 plan,最后一行 done,done 的 explanation 用一两句话说清改了什么、为什么"""
_ASSIST_RULES = _RULES_HEAD + _NO_LOWERING + _ASK_HUMAN + _ONLY_LISTED + _OPS_ONLY + _PLAN_DONE
def _publish_fix_request(graph: dict[str, Any], errors: list[dict[str, Any]], level: str) -> str:
return (
@@ -1777,6 +1852,246 @@ def _proposed_graph(base: dict[str, Any], nodes: dict[str, dict[str, Any]],
return out
# --------------------------------------------------------------------------
# 一键升级为可追溯结构:先按确定的规则改写(engine/upgrade.py),assist 时再请 Copilot 把纯算术的沙箱代码
# 改写成口径卡表达式。和发布前修复一样只给预览:不改库、不保存,人确认后前端走现有的保存
# --------------------------------------------------------------------------
class UpgradeIn(BaseModel):
graph: dict[str, Any]
#: 确定性改写之后,再请 Copilot 做语义层面的改写(纯算术的沙箱代码改成口径卡表达式),同样只是预览
assist: bool = False
#: 复核门禁用的发布级别。受管级别下新写出来的报告撰写节点另写明门禁 G3 要的三项
level: Literal["published", "governed"] = "published"
provider: str | None = None
model: str | None = None
_UPGRADE_TASK = """\
逐个判断上面这些沙箱代码节点:
- 代码只是对上游已有的数做纯算术(加减乘除、round、min / max、比较、按条件取值),没有查库、调接口、读写文件,
也不用随机数、当前时间:把口径卡里读它的那几个指标的 expression 改成直接用上游的值算——上游是查库的调用工具
节点就写 cell(nodes.查库节点id, 行, '列名'),是开了 cite_fields 的 agent 就写 vars.<它的 assign_to>.字段。
改完口径卡不再读这个代码节点;代码节点本身留在图上(不删节点、不删连线,要不要删由人决定)
- 不是纯算术的(在取数、调接口、解析文件、用了随机数或时间……)保留原样,输出一行 question 说明为什么没改,
并请人确认它是不是在取数
"""
def _upgrade_assist_request(graph: dict[str, Any], spec: GraphSpec, feeders: list[Any]) -> str:
from app.engine.upgrade import metric_feeders
lines = []
for code, cards in feeders:
# 和 compute_feeders 同一条追法:指标读的是整形、校验节点的,追到头是这段代码也要列出来
reads = [f"口径卡「{card.title}」({card.id})的指标 {card.config['metrics'][i].get('id')}:"
f"{card.config['metrics'][i].get('expression')}"
for card in cards for i, producer in metric_feeders(spec, card) if producer.id == code.id]
lines.append(f"- 「{code.title}」({code.id})被这些指标读到:" + ";".join(dict.fromkeys(reads)))
return (
"这是一张已经按确定的规则升级成可追溯结构的工作流:\n"
f"{json.dumps(_slim(graph), ensure_ascii=False, indent=2)}\n\n"
"口径卡读的是下面这些沙箱代码节点的产出(evidence_role 不是 source):沙箱里算出来的数核对不了出处。\n"
+ "\n".join(lines) + f"\n\n{_UPGRADE_TASK}\n"
+ _RULES_HEAD + _NO_LOWERING + _ASK_HUMAN
+ "- 只改上面这些口径卡指标的 expression,别的不要动\n" + _OPS_ONLY + _PLAN_DONE
)
def _kept_code(spec: GraphSpec) -> list[str]:
"""assist 之后仍然喂着口径卡的计算角色沙箱代码:不是纯算术、或者 Copilot 拿不准的,保留原样并警告。"""
from app.engine.upgrade import compute_feeders
return [f"「{code.title}」(沙箱代码)没有改写成口径卡表达式,保留原样:口径卡"
+ "、".join(f"「{c.title}」" for c in cards)
+ "读到的数核对不了出处。它在取数的话,把 evidence_role 标成 source;在做计算的话,把计算写进口径卡"
for code, cards in compute_feeders(spec)]
async def assist_upgrade(
session: AsyncSession, graph: dict[str, Any], *, level: str,
provider: str | None = None, model: str | None = None,
) -> dict[str, Any]:
"""升级的语义层:请 Copilot 把喂口径卡的纯算术沙箱代码改写成口径卡表达式,不是纯算术的保留并警告。
模型、工具目录、操作流协议都和画布上改图是同一套;改完按搭图时同一套自查循环修正(只把这一轮冒出来的
error 交回去,图上原来就有的不算),最后复核:没有降低要求的改动(autofix.forbidden_changes,这里没有例外:
Copilot 不换类型、不删连线)、没有比确定性改写的结果更糟(autofix.worse)才采纳,否则整个作废并写明原因。
返回 {ok, accepted, reason, summary, questions, warnings, graph, ops}。不落库。
"""
from app.engine.autofix import _sig, forbidden_changes, worse
from app.engine.upgrade import compute_feeders
base = copy.deepcopy(graph)
spec = GraphSpec.model_validate(base)
out: dict[str, Any] = {"ok": True, "accepted": False, "reason": None, "summary": "", "questions": [],
"warnings": [], "graph": graph, "ops": []}
feeders = compute_feeders(spec)
if not feeders:
out["summary"] = "口径卡的输入都不来自做计算的沙箱代码,没有要交给 Copilot 改写的"
return out
sources = await _sources(session, None)
before_nodes = {str(n["id"]): n for n in copy.deepcopy(base.get("nodes") or []) if isinstance(n, dict)}
before_edges = [e for e in copy.deepcopy(base.get("edges") or []) if isinstance(e, dict)]
baseline = list(copy.deepcopy(before_nodes).values())
defaults = base.get("defaults")
base_errors = _blocking_issues(before_nodes, before_edges, sources=sources, baseline=baseline, level=level,
defaults=defaults) or []
known = {_sig(e) for e in base_errors}
out["warnings"] = _kept_code(spec)
try:
spec_ = await copilot_model_spec(
session, GenerateIn(instruction="升级为可追溯结构", provider=provider, model=model))
chat, _ = await get_chat_model(session, spec_)
except ProviderNotConfigured as e:
out.update(ok=False, summary=_unconfigured(e))
return out
system = (
"你是一个 agent 工作流编排专家。现在要做的是:把一张工作流里喂给口径卡的纯算术沙箱代码改写成口径卡表达式,"
"以操作流的方式输出修改。\n\n"
f"{NODE_REFERENCE}\n\n可用工具:\n{_tool_catalog(sources)}{_datasource_section(sources)}\n\n"
f"{_STREAM_PROTOCOL}\n\n另外可以输出 question 操作:{{\"op\":\"question\",\"node_id\":\"…\",\"text\":\"…\"}}"
)
nodes = copy.deepcopy(before_nodes)
edges = copy.deepcopy(before_edges)
tried: list[dict[str, Any]] = []
effective: list[dict[str, Any]] = []
plan = ""
async def collect(request: str, *, first: bool) -> None:
nonlocal plan
async for op in _iter_ops(chat, [("system", system), ("human", request)]):
kind = op.get("op")
if kind in ("thinking", "heartbeat"):
continue
if kind in ("plan", "done"):
# 修补轮的开场和收尾不是新方案,不覆盖第一轮的说明
if first and kind == "plan":
plan = str(op.get("summary") or "")
elif first:
out["summary"] = str(op.get("explanation") or "")
elif kind in ("question", "reply"):
text = str(op.get("text") or "").strip()
where = before_nodes.get(str(op.get("node_id") or ""))
title = str((where.get("data") or {}).get("label") or where.get("id")) if where else ""
if text:
out["questions"].append(f"「{title}」:{text}" if title and title not in text else text)
else:
tried.append(op)
if _apply_op(nodes, edges, op):
effective.append(op)
try:
await collect(_upgrade_assist_request(base, spec, feeders), first=True)
# 自查:和搭图同一套规则,只把这一轮冒出来的 error 交回去改(原来就有的不归这一轮管)
for _ in range(_SELF_CHECK_ROUNDS):
now = _blocking_issues(nodes, edges, sources=sources, baseline=baseline, level=level, defaults=defaults)
fresh = [e for e in now or [] if _sig(e) not in known]
if not effective or not fresh:
break
await collect(_repair_request(nodes, edges, fresh), first=False)
except Exception as e: # noqa: BLE001 - 改不成就照实说,确定性改写的结果照样交给人
out.update(ok=False, summary=f"Copilot 这一轮没跑完:{explain_error(e)[0]}")
return out
out["summary"] = out["summary"] or plan
if not effective:
out["summary"] = out["summary"] or ("Copilot 认为这些代码不是纯算术,都保留原样" if out["questions"]
else "Copilot 这一轮没有给出修改")
return out
proposed = _proposed_graph(base, nodes, edges)
reasons = forbidden_changes(base, proposed, tried)
after = _blocking_issues(_nodes_by_id(proposed), proposed.get("edges") or [], sources=sources,
baseline=baseline, level=level, defaults=proposed.get("defaults"))
if reasons:
out["reason"] = "Copilot 的修改降低了要求,已作废:" + ";".join(reasons)
elif after is None:
out["reason"] = "Copilot 改完的图结构读不懂,已作废"
elif why := worse(base_errors, after):
out["reason"] = f"Copilot 的修改让图变糟了,已作废:{why}"
else:
out.update(accepted=True, graph=proposed, ops=effective, warnings=_kept_code(GraphSpec.model_validate(proposed)))
out["ok"] = out["reason"] is None
return out
def _placed(base: dict[str, Any], graph: dict[str, Any]) -> dict[str, Any]:
"""升级新插进来的节点找空处摆,原有节点一个都不挪(坐标常带着业务上的分区)。"""
known = {n.get("id") for n in base.get("nodes") or [] if isinstance(n, dict)}
fresh = {n["id"] for n in graph.get("nodes") or [] if isinstance(n, dict) and n.get("id") not in known}
if not fresh:
return graph
out = copy.deepcopy(graph)
try:
laid, _ = _layout_keeping(GraphSpec.model_validate(out), _pinned_positions(base))
except Exception: # noqa: BLE001 - 摆不好就留在规则给的位置上
return out
where = {n.id: n.position.model_dump() for n in laid.nodes}
for n in out["nodes"]:
if n.get("id") in fresh:
n["position"] = where.get(n["id"], n.get("position"))
return out
def _node_changes(changes: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""Copilot 那一段的逐项变更和确定性改写同一个形状:新加的节点记成 field 为 node、after 是整个节点。"""
out = []
for c in changes:
if c.get("field") is None and isinstance(c.get("after"), dict):
c = {**c, "field": "node", "after": {"id": c["node_id"], "label": c["node_title"], **c["after"]}}
out.append({**c, "rule": "assist"})
return out
@router.post("/upgrade-evidence")
async def upgrade_evidence(payload: UpgradeIn, session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
"""一键升级为可追溯结构(证据五期):返回升级后的整张图和逐项变更——只是预览,不改库、不保存。
返回 {graph, changes, ops, notes, issues, applied, rejected, assist, ok}:changes / notes / rejected 的形状见
engine/upgrade.upgrade_for_evidence;issues 是升级后的图按 level 重跑 validate 和门禁的结果(和发布同一套口径);
assist 只在请求带 assist: true 时有:{ok, summary, questions, warnings},warnings 是保留没改的沙箱代码。
"""
from app.engine.autofix import diff_changes
from app.engine.governance import publish_issues
from app.engine.upgrade import upgrade_for_evidence
try:
GraphSpec.model_validate(payload.graph)
except Exception as e: # noqa: BLE001
return {"graph": payload.graph, "changes": [], "ops": [], "notes": [], "applied": [], "rejected": [],
"assist": None, "ok": False,
"issues": [{"level": "error", "node_id": None, "edge_id": None, "field": None, "code": None,
"fix": None, "message": f"工作流的结构读不懂:{graph_error(e)}"}]}
out = upgrade_for_evidence(payload.graph, level=payload.level)
out["graph"] = _placed(payload.graph, out["graph"])
out["assist"] = None
if payload.assist:
helped = await assist_upgrade(session, out["graph"], level=payload.level, provider=payload.provider,
model=payload.model)
out["assist"] = {"ok": helped["ok"], "summary": helped["summary"], "questions": helped["questions"],
"warnings": helped["warnings"]}
if helped["accepted"]:
from app.engine.upgrade import compute_feeders
out["changes"] += _node_changes(
diff_changes(out["graph"], helped["graph"], fix_id="assist", label="Copilot 的修改"))
out["ops"] += helped["ops"]
out["applied"].append("assist")
out["graph"] = helped["graph"]
# R5 的建议是改写之前给的:口径卡已经不读的那几个代码节点,不再建议人去标 source
still = {code.id for code, _ in compute_feeders(GraphSpec.model_validate(out["graph"]))}
out["notes"] = [n for n in out["notes"] if n["rule"] != "R5" or n["node_id"] in still]
elif helped["reason"]:
out["rejected"].append({"fix_id": "assist", "reason": helped["reason"]})
issues = publish_issues(GraphSpec.model_validate(out["graph"]), level=payload.level)
out["issues"] = [i.model_dump() for i in issues]
out["ok"] = not any(i.level == "error" for i in issues)
return out
class FromRunIn(BaseModel):
run_id: str
name: str = ""
+67 -17
View File
@@ -48,7 +48,7 @@ _ORDER = (
"governed.no_contract", "contract.metrics_from_missing", "contract.metrics_from_invalid",
"contract.report_from_missing", "contract.report_from_invalid", "governed.report_from_required",
"governed.exit_text_source", "report.metrics_from_invalid",
"contract.required_missing", "contract.strict_off", "contract.claims_ignored", "report.claims_invalid",
"contract.required_missing", "contract.cells_undeclared", "contract.strict_off", "contract.claims_ignored", "report.claims_invalid",
"governed.report_policy", "governed.judge_budget",
"governed.caliber_agent_cite_fields", "governed.caliber_compute_input", "governed.agent_approval_never",
"governed.default_approval_never", "governed.subgraph_unpinned", "governed.supervisor", "contract.not_object",
@@ -282,6 +282,21 @@ def _plan_judge_budget(ctx: _Ctx, issue: dict[str, Any], node: GraphNode | None)
options, _judge=base)
def _plan_cells_undeclared(ctx: _Ctx, issue: dict[str, Any], node: GraphNode | None) -> dict[str, Any] | None:
"""没有口径卡的受管图没写 cells:报告里的数要么直接引用查询单元格(写 cells: true),要么登记成口径卡
指标(交给 Copilot 加卡、改引用)。受管级别打开 cells 要作者显式决定,所以只给选项,不替人选。"""
if node is None:
return None
options = [
{"value": True, "label": "写 cells: true(保留单元格引用)",
"hint": "报告里的数直接点得开查询结果里的那一格,每一格都对照快照核对"},
{"value": HANDOFF, "label": "交给 Copilot:加一张口径卡,把数登记成指标",
"hint": "Copilot 改的同样只是预览,要人拿主意的会原样问你", "handoff": True},
]
return _choice(f"「{node.title}」的报告只能引用查询单元格:写 cells: true,还是改成口径卡指标?",
"contract.cells", options)
def _plan_compute_input(ctx: _Ctx, issue: dict[str, Any], node: GraphNode | None) -> dict[str, Any] | None:
"""G4:沙箱代码到底是在取数还是在算,只有作者知道。取数的标成 source;在算的交给 Copilot 把计算
挪进口径卡——两样都不替人选,也不给建议。"""
@@ -458,6 +473,7 @@ _PLANNERS: dict[str, Callable[[_Ctx, dict[str, Any], GraphNode | None], dict[str
"governed.report_policy": _plan_report_policy,
"report.claims_invalid": _plan_claims,
"governed.judge_budget": _plan_judge_budget,
"contract.cells_undeclared": _plan_cells_undeclared,
"governed.caliber_compute_input": _plan_compute_input,
"governed.caliber_agent_cite_fields": _plan_cite_fields,
"governed.caliber_agent_schema": _plan_agent_schema,
@@ -752,22 +768,48 @@ def _sig(issue: dict[str, Any]) -> tuple[Any, ...]:
return head if issue.get("code") else (*head, issue.get("message"))
def judge(before: list[Any], after: list[Any], *, target: tuple[str, str | None] | None = None) -> str | None:
"""改完是不是更好了:不能冒出新的 error,error 不能变多;要修的那一处必须没了。
#: 只有人能拍板、修复和 Copilot 都不许代做的 error(受管级别打开 cells)。改动让它冒出来不算更糟:它有自己的
#: choice 修复,发布弹窗里一键选。拿它判作废的话,Copilot 能做的最好的一步(补上契约、把 cells 留给人问)
#: 反而会被拒掉——用户就撞过这个死局。原来就有的照常计数
HUMAN_CHOICE = frozenset({"contract.cells_undeclared"})
target 是 (问题编号, 节点 id):修的是一条警告(strict)时,error 不增加、那条警告消失就算更好;
修的是 error,或者不指定 target(Copilot 兜底),error 必须变少。返回 None 表示更好,否则是原因。
def _errors(before: list[Any], after: list[Any]) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
"""改动前后的 error;改完新冒出来的 HUMAN_CHOICE 不算进改完的那份。"""
b_err = [i for i in map(_as_dict, before) if i.get("level") == "error"]
seen = {_sig(i) for i in b_err}
a_err = [i for i in map(_as_dict, after) if i.get("level") == "error"
and not (i.get("code") in HUMAN_CHOICE and _sig(i) not in seen)]
return b_err, a_err
def worse(before: list[Any], after: list[Any]) -> str | None:
"""改完是不是更糟了:冒出新的 error,或者 error 变多了。返回原因,没有就是 None。
judge 在这之上还要求「修的那一处没了、错误变少」;一键升级(engine/upgrade.py)只要求不更糟——
升级换的是结构,本来就不一定消掉哪条 error。
"""
b = [_as_dict(i) for i in before]
a = [_as_dict(i) for i in after]
b_err = [i for i in b if i.get("level") == "error"]
a_err = [i for i in a if i.get("level") == "error"]
b_err, a_err = _errors(before, after)
seen = {_sig(i) for i in b_err}
new = [i for i in a_err if _sig(i) not in seen]
if new:
return f"改完会冒出新的问题:{new[0].get('message')}"
if len(a_err) > len(b_err):
return f"改完错误反而变多了({len(b_err)} → {len(a_err)})"
return None
def judge(before: list[Any], after: list[Any], *, target: tuple[str, str | None] | None = None) -> str | None:
"""改完是不是更好了:不能冒出新的 error,error 不能变多;要修的那一处必须没了。
target 是 (问题编号, 节点 id):修的是一条警告(strict)时,error 不增加、那条警告消失就算更好;
修的是 error,或者不指定 target(Copilot 兜底),error 必须变少。返回 None 表示更好,否则是原因。
"""
if why := worse(before, after):
return why
b = [_as_dict(i) for i in before]
a = [_as_dict(i) for i in after]
b_err, a_err = _errors(before, after)
if target is not None:
code, node_id = target
@@ -849,9 +891,10 @@ def _numbers(value: Any) -> list[str]:
return [str(v).strip() for v in _as_list(value) if str(v).strip()]
def _contract_loosened(name: str, prior: Any, contract: Any) -> list[str]:
def _contract_loosened(name: str, prior: Any, contract: Any, *, cells_chosen: bool = False) -> list[str]:
"""出具契约里替作者放宽口子的改动:allow_numbers 多出来的数(白名单里的数不用引用也能过),
cells 从没开变成开(受管级别要作者显式声明,不由模型替人打开)。原来没有契约也一样查。"""
cells 从没开变成开(受管级别要作者显式声明,不由模型替人打开;cells_chosen 是人在 choice 里亲手选的,
不算)。原来没有契约也一样查。"""
if not isinstance(contract, dict):
return []
prior = prior if isinstance(prior, dict) else {}
@@ -860,7 +903,7 @@ def _contract_loosened(name: str, prior: Any, contract: Any) -> list[str]:
extra = [n for n in dict.fromkeys(_numbers(contract.get("allow_numbers"))) if n not in had]
if extra:
out.append(f"往「{name}」出具契约的 allow_numbers 里加了 {'、'.join(extra)}(不带出处也能放行的数)")
if contract.get("cells") is True and prior.get("cells") is not True:
if contract.get("cells") is True and prior.get("cells") is not True and not cells_chosen:
out.append(f"替你打开了「{name}」出具契约的 cells(允许报告直接引用查询单元格),这要你自己决定")
was, now = _claims_rank(prior.get("claims")), _claims_rank(contract.get("claims"))
if was is not None and now is not None:
@@ -895,13 +938,15 @@ def _claims_rank(value: Any) -> tuple[int, int] | None:
def forbidden_changes(before: dict[str, Any], after: dict[str, Any],
ops: list[dict[str, Any]] | None = None, *,
chosen: set[tuple[str, str]] | frozenset[tuple[str, str]] = frozenset()) -> list[str]:
chosen: set[tuple[str, str]] | frozenset[tuple[str, str]] = frozenset(),
retyped: set[tuple[str, str, str]] | frozenset[tuple[str, str, str]] = frozenset(),
rewired: set[tuple[str, str]] | frozenset[tuple[str, str]] = frozenset()) -> list[str]:
"""降低要求的改动,一条一句。空列表表示没有。确定性修复和 Copilot 兜底过的是同一道检查。
禁止:
- 删节点、删连线;用 add_node 覆盖已有的节点、把节点换成别的类型(等于删了重建)
- 删掉或清空出具契约、删掉 required 里的指标、把 strict 改成 false;
往 allow_numbers 里加数、替人打开 cells
往 allow_numbers 里加数、替人打开 cells(chosen 里有 (出口 id, "contract.cells") 的是人在 choice 里亲手选的,不算)
- 审批策略往宽里改(每次调用都审批 > 仅危险工具需要审批 > 全部自动放行)、删掉写明的审批策略,
把审批写成(或新加一个)「全部自动放行」;全图默认同理
- 取消子工作流钉住的版本
@@ -911,6 +956,10 @@ def forbidden_changes(before: dict[str, Any], after: dict[str, Any],
(节点 id, 字段) 是人在 choice 里亲手选的,不算
- 改发布级别——级别不在图里,修复接口也从不写工作流的 status,所以只可能以一个不认识的
操作出现,按不认识的操作拒掉
一键升级(engine/upgrade.py)走同一道检查,只多两个由升级自己点名的例外:retyped 里的
(节点 id, 原类型, 新类型) 允许换类型(模型调用换成报告撰写,这是升级的核心);rewired 里的
(source, target) 允许删掉那条连线(中间插了报告撰写节点、改接的那一条)。发布前修复不传,照旧全拦。
"""
old, new = _nodes_of(before), _nodes_of(after)
reasons: list[str] = []
@@ -932,14 +981,15 @@ def forbidden_changes(before: dict[str, Any], after: dict[str, Any],
kept = {_edge_key(e) for e in after.get("edges") or [] if isinstance(e, dict)}
reasons += [f"删了连线 {s} → {t}" for s, t, _ in
dict.fromkeys(_edge_key(e) for e in before.get("edges") or [] if isinstance(e, dict))
if (s, t, _) not in kept]
if (s, t, _) not in kept and (s, t) not in rewired]
old_default = _approval((before.get("defaults") or {}).get("approval"))
new_default = _approval((after.get("defaults") or {}).get("approval"))
for nid, node in new.items():
was = old.get(nid)
cfg, prev = _cfg(node), _cfg(was) if was else {}
name = _name(node)
if was is not None and was.get("type") != node.get("type"):
if was is not None and was.get("type") != node.get("type") \
and (nid, str(was.get("type")), str(node.get("type"))) not in retyped:
from app.engine.schema import type_label
reasons.append(f"把「{_name(was)}」从{_type_word(was.get('type'), type_label)}换成了"
@@ -952,7 +1002,7 @@ def forbidden_changes(before: dict[str, Any], after: dict[str, Any],
fallback=new_default):
reasons.append(why)
contract, prior = cfg.get("contract"), prev.get("contract")
reasons += _contract_loosened(name, prior, contract)
reasons += _contract_loosened(name, prior, contract, cells_chosen=(nid, "contract.cells") in chosen)
if node.get("type") == "code" and cfg.get("evidence_role") == "source" \
and prev.get("evidence_role") != "source" and (nid, "evidence_role") not in chosen:
reasons.append((f"替你把「{name}」标成了取数(evidence_role: source)" if was
+8
View File
@@ -189,6 +189,14 @@ def _lint_contract(
"受管模板的出具契约必须声明 required(必需指标),否则「不予出具」这一档永远触发不了",
code="contract.required_missing", node_id=node_id, hard=True, field="contract.required",
)
# 没有口径卡的图,报告里的数只能直接引用查询单元格;受管正式运行不认没声明的单元格引用,
# 报告撰写节点(numbers strict、on_violation fail)每次都会失败。写不写要作者定:修复给选项
if strict and cells_only and contract.get("cells") is not True:
flag(
"这张图没有口径卡,报告里的数只能直接引用查询单元格:受管出具要在契约里写 cells: true,"
"否则正式运行里一个数都引用不了,报告撰写节点每次都会失败(也可以加一张口径卡,把数登记成指标)",
code="contract.cells_undeclared", node_id=node_id, hard=True, field="contract.cells",
)
if strict and not contract.get("strict"):
flag(
"受管模板建议把出具契约设为 strict:非 strict 下未回指的数字只降档不拦截",
+10 -1
View File
@@ -148,7 +148,9 @@ class GraphSpec(BaseModel):
class ValidationIssue(BaseModel):
level: Literal["error", "warning"] = "error"
#: info 是建议、不是问题(evidence.upgrade_available「可以升级为可追溯结构」):不挡运行、不挡发布,
#: 自查、门禁、修复复核都只数 error
level: Literal["error", "warning", "info"] = "error"
node_id: str | None = None
edge_id: str | None = None
message: str
@@ -683,6 +685,13 @@ def validate_graph(spec: GraphSpec) -> ValidationResult:
for issue in [*text_parse_issues(spec), *evidence_issues(spec)]:
result.add(issue.message, level=issue.level, node_id=issue.node_id, field=issue.field, code=issue.code)
# 旧结构(报告是模型调用写的、问数据的答案是 agent 的原话…)能一键升级成可追溯的:只给一条建议,
# 画布上的快速修复据此打开升级预览。放在函数里导入:upgrade 反过来要用这里的 GraphSpec
from app.engine.upgrade import HINT_CODE, upgrade_hint
if hint := upgrade_hint(spec):
result.add(hint, level="info", code=HINT_CODE)
# 孤儿节点
for node in spec.nodes:
if node.type in (NodeType.INPUT, NodeType.OUTPUT):
+679
View File
@@ -0,0 +1,679 @@
"""一键升级为可追溯结构(证据五期)。
旧图里给人看的报告是模型调用(llm)写的自由文本,问数据的答案是 agent 的原话:数字是模型转写的,
出处只能在出口按数值回头猜。这里按确定的规则把它们改写成可追溯的结构,只给预览,人确认了才保存:
- R1 出具契约的 narrative 指向一个模型调用节点:把它换成报告撰写(system 加 prompt 合成 instructions,
模型配置照搬),契约改成 report_from
- R2 没有契约,模型调用写的文字进了出口,上游又有取数(查库、口径卡):同样换成报告撰写
- R3 input → agent → output(问数据页的典型图):在 agent 和出口之间插一个报告撰写,出口改取它的正文
- R4 配了 output_schema 的 agent:打开 cite_fields(不开的话 Schema 不生效)
- R5 沙箱代码的产出喂给口径卡:不改,只在 notes 里建议标成 source——它在取数还是在算只有作者知道
纯函数、幂等:同一张图升级两次,第二次什么都不改。每一步都在副本上做,过一遍和发布前修复同一套禁止
规则(autofix.forbidden_changes,只多「模型调用换成报告撰写」「R3 改接的那条连线」两个例外),再重跑
validate 和门禁:冒出新 error 的那一步丢弃,写明原因。和 autofix 一样直接改原始的图字典,坐标、连线 id、
视口这些前端的东西原样留着。
受管出具的契约要不要写 cells(报告直接引用查询单元格)、required 列哪些指标,都是作者的决定:R3 不替人
加契约,只在 notes 里把两条路说清楚。
"""
from __future__ import annotations
import copy
import re
from dataclasses import dataclass, field, replace
from typing import Any, Callable
from app.engine.autofix import forbidden_changes, worse
from app.engine.governance import (
REPORT_POLICY,
REPORT_POLICY_ALSO,
Ref,
_card_feeders,
_exit_refs,
_Graph,
_refs,
publish_issues,
)
from app.engine.schema import GraphNode, GraphSpec, NodeType, _ancestors, _feeds_caliber, innermost_loops
#: 会改图的规则,按这个顺序应用:先换写字的节点,再插报告,最后开 cite_fields(R3 要先把出口从 agent 的
#: 原话改接到报告上,R4 打开以后 vars.<assign_to> 就是核对过的对象、不再是文字了)
RULES = ("R1", "R2", "R3", "R4")
#: validate 给的那条建议(info)的编号和开头
HINT_CODE = "evidence.upgrade_available"
HINT = "可以升级为可追溯结构"
LEVELS = ("published", "governed")
#: 模型调用有、报告撰写用不上的配置。system、prompt 合进 instructions;其余的换成报告撰写后不再生效
_LLM_ONLY = ("prompt", "system", "skills", "use_history", "output_schema")
#: 新的报告撰写节点照搬过去的模型配置(R3 从 agent 带过去的只有 provider、model:温度、上限是查数用的)
_MODEL_KEYS = ("provider", "model")
#: 报告的指令里照搬了这几类节点的文字,就要提醒:报告撰写自己会收集它们的证据
_EVIDENCE_TYPES = (NodeType.METRICS, NodeType.TOOL, NodeType.AGENT, NodeType.RETRIEVE, NodeType.CODE,
NodeType.SUBGRAPH)
#: 写整段文字的节点:出具契约的 narrative 拼了其中几个的文字,就说不准该换哪一个
_WRITER_TYPES = (NodeType.LLM, NodeType.AGENT)
_QUESTION = re.compile(r"(?<![\w.])input\s*\.\s*question(?![\w-])")
_SPAN = re.compile(r"\{\{(.*?)\}\}", re.S)
@dataclass(frozen=True)
class Match:
"""一处符合 R1–R4 的旧结构。blocked 不为空的是看着像、但说不准怎么改的,只进 notes。
exits 是这一步要改的出口;skipped 是认出来了、但这一步不碰的出口各自的原因(R3 里要经过别的节点才读到
agent 原话的出口),这一步照改其余的出口,skipped 进 notes。"""
rule: str
node_id: str
exits: tuple[str, ...] = ()
summary: str = ""
blocked: str | None = None
skipped: tuple[str, ...] = ()
@dataclass
class _Step:
label: str
changes: list[dict[str, Any]] = field(default_factory=list)
ops: list[dict[str, Any]] = field(default_factory=list)
notes: list[dict[str, Any]] = field(default_factory=list)
retyped: set[tuple[str, str, str]] = field(default_factory=set)
rewired: set[tuple[str, str]] = field(default_factory=set)
created: bool = False
# --------------------------------------------------------------------------
# 认旧结构
# --------------------------------------------------------------------------
def _queries(node: GraphNode) -> bool:
"""这个节点查库:报告撰写能把它查到的结果编成 Q1、Q2… 引用。只认数据源的查询工具,别的工具的
返回进不了目录。"""
if node.type == NodeType.TOOL:
return str(node.config.get("tool") or "").startswith("db_query__")
if node.type == NodeType.AGENT:
tools = node.config.get("tools")
return isinstance(tools, list) and any(str(t).startswith("db_query__") for t in tools)
return False
def _fetches(node: GraphNode) -> bool:
"""上游的取数:查库、口径卡——报告撰写的目录里就有能当值引用的数。知识库检索不算:文档里的话只能当引文,
知识库问答的答案换成报告撰写,里面转述的数全会被判成没有出处。"""
return node.type == NodeType.METRICS or _queries(node)
def _cited(node: GraphNode) -> bool:
return bool(node.config.get("output_schema")) and node.config.get("cite_fields") is True
def _text_ref(node: GraphNode, ref: Ref) -> bool:
"""顺着这个引用拿到的是不是它写的整段文字:nodes.X、nodes.X.text、vars.<assign_to>、最后一条消息。
开了 cite_fields 的 agent,assign_to 拿到的是核对过的字段,那是数据不是原话。"""
root, _, path = ref
if root in ("last_message", "messages"):
return True
if root == "nodes":
return path in ((), ("text",))
if root == "vars":
return not path and not (node.type == NodeType.AGENT and _cited(node))
return False
def _writers(graph: _Graph, consumer: str, refs: list[Ref], types: tuple[NodeType, ...]) -> list[GraphNode]:
"""这些引用读到了哪些节点写的整段文字,按画布上的节点顺序。"""
found: set[str] = set()
for ref in refs:
found |= {src.id for src in graph.sources(consumer, ref) if src.type in types and _text_ref(src, ref)}
return [n for n in graph.spec.nodes if n.id in found]
def _exit_text_refs(exit_node: GraphNode) -> list[Ref]:
return [ref for _, _, refs in _exit_refs(exit_node) for ref in refs]
def _contract(node: GraphNode) -> dict[str, Any]:
contract = node.config.get("contract")
return contract if isinstance(contract, dict) else {}
def _names(nodes: list[GraphNode]) -> str:
return "、".join(f"「{n.title}」" for n in nodes)
def _llm_blocked(spec: GraphSpec, node: GraphNode, loops: dict[str, str]) -> str | None:
"""这个模型调用节点能不能原样换成报告撰写;不能的说清为什么。"""
who = f"「{node.title}」(模型调用)"
if node.config.get("output_schema"):
return f"{who}配了 output_schema,交的是结构化数据、不是给人看的文字,没有换成报告撰写"
if node.id in loops:
return f"{who}在循环体里,换成报告撰写就是每一轮写一份报告:循环怎么改要你来定,升级没有动它"
if _feeds_caliber(node, spec):
return (f"{who}写的数还喂给了口径卡:换成报告撰写,口径卡就读不到它了。先把取数改由 Agent(配 output_schema、"
"开 cite_fields)或「调用工具」节点来做,再升级")
return None
def _add(found: dict[tuple[str, str], Match], match: Match) -> None:
"""同一个节点从几个出口认出来的合成一处:出口、跳过的原因并起来;任何一个出口说不准,整处都不改。
结果不能看出口在画布上谁排前面。"""
key = (match.rule, match.node_id)
was = found.get(key)
if was is None:
found[key] = match
return
found[key] = Match(was.rule, was.node_id, tuple(dict.fromkeys([*was.exits, *match.exits])),
was.summary or match.summary, was.blocked or match.blocked,
tuple(dict.fromkeys([*was.skipped, *match.skipped])))
def upgrade_candidates(spec: GraphSpec) -> list[Match]:
"""图上符合 R1–R4 的旧结构,按规则、再按节点在画布上的顺序。只看结构,不改图、不跑门禁:
validate 每改一下画布就调一次,要便宜;真改的时候 upgrade_for_evidence 再逐步复核。"""
graph = _Graph(spec)
nodes = spec.node_map()
loops = innermost_loops(spec)
order = {n.id: i for i, n in enumerate(spec.nodes)}
found: dict[tuple[str, str], Match] = {}
outputs = [n for n in spec.nodes if n.type == NodeType.OUTPUT]
for exit_node in outputs:
contract = _contract(exit_node)
narrative = contract.get("narrative")
if contract.get("report_from") not in (None, "") or not isinstance(narrative, str) or not narrative.strip():
continue
writers = _writers(graph, exit_node.id, _refs(narrative, template=True), _WRITER_TYPES)
llms = [w for w in writers if w.type == NodeType.LLM]
if len(writers) > 1:
# 叙述拼了几个节点写的文字:换哪一个都会让其余的文字悄悄离开契约的核对。拼进来的每个模型调用都不换,
# 哪怕它另有一个只读它的出口——不然结果要看两个出口在画布上谁排前面
why = (f"「{exit_node.title}」的出具契约 narrative 拼了{_names(writers)}几个节点写的文字,该换哪一个说不准,"
"没有升级:让一个节点写完整篇(其余的内容交给它当素材),再升级")
for llm in llms:
_add(found, Match("R1", llm.id, blocked=why))
elif llms:
llm = llms[0]
_add(found, Match("R1", llm.id, (exit_node.id,),
f"「{llm.title}」(模型调用)写的叙述换成报告撰写,出具契约改为核对它的文档",
_llm_blocked(spec, llm, loops)))
r1 = {m.node_id for m in found.values()}
for exit_node in outputs:
if _contract(exit_node):
continue # 有契约的归 R1(叙述模式)或者已经是引用模式
for llm in _writers(graph, exit_node.id, _exit_text_refs(exit_node), (NodeType.LLM,)):
if llm.id in r1 or not any(_fetches(nodes[a]) for a in _ancestors(spec, llm.id) if a in nodes):
continue
_add(found, Match("R2", llm.id, (exit_node.id,),
f"「{llm.title}」(模型调用)写的文字直接进了出口,换成报告撰写",
_llm_blocked(spec, llm, loops)))
for exit_node in outputs:
contract = _contract(exit_node)
if contract.get("report_from") not in (None, ""):
continue
refs = _exit_text_refs(exit_node)
narrated: list[GraphNode] = []
if isinstance(contract.get("narrative"), str):
story = _refs(contract["narrative"], template=True)
refs += story
narrated = _writers(graph, exit_node.id, story, _WRITER_TYPES)
for agent in _writers(graph, exit_node.id, refs, (NodeType.AGENT,)):
# 喂口径卡的 agent 是取数的,不是作答的:它的原话漏进出口由门禁 G2 管,不在这里插报告
if not _queries(agent) or _feeds_caliber(agent, spec):
continue
# 说不准的只跳过这个出口:同一个 agent 直连的出口照样插报告撰写,结果不看出口在画布上的顺序
if agent.id in loops:
_add(found, Match("R3", agent.id, blocked=(
f"「{agent.title}」在循环体里,升级不替你改循环结构,没有插入报告撰写")))
elif not any(e.source == agent.id and e.target == exit_node.id for e in spec.edges):
_add(found, Match("R3", agent.id, skipped=(
f"「{agent.title}」的原话要经过别的节点才到「{exit_node.title}」,升级不替你改接中间的连线:在它后面"
f"接一个报告撰写,「{exit_node.title}」改取报告的正文",)))
elif agent.id in {n.id for n in narrated} and len(narrated) > 1:
_add(found, Match("R3", agent.id, skipped=(
f"「{exit_node.title}」的出具契约 narrative 拼了{_names(narrated)}几个节点写的文字,改成核对报告撰写的"
"文档,其余的文字就悄悄离开了核对:这个出口升级没有动。让一个节点写完整篇,再升级",)))
else:
_add(found, Match("R3", agent.id, (exit_node.id,)))
for key, m in found.items():
if m.rule != "R3":
continue
agent = nodes[m.node_id]
if m.exits:
found[key] = replace(m, summary=f"在「{agent.title}」和{_names([nodes[x] for x in m.exits])}之间插入报告撰写,"
"答案里的数逐个点得开出处")
elif not m.blocked:
found[key] = replace(m, blocked=";".join(m.skipped), skipped=())
for n in spec.nodes:
if n.type == NodeType.AGENT and n.config.get("output_schema") and n.config.get("cite_fields") is not True:
_add(found, Match("R4", n.id, (), f"打开「{n.title}」的 cite_fields,output_schema 才生效"))
return sorted(found.values(), key=lambda m: (RULES.index(m.rule), order.get(m.node_id, len(order))))
def upgrade_hint(spec: GraphSpec) -> str | None:
"""validate 的那条建议的文案;没有能升级的旧结构返回 None。
升级是整张图一起改的,建议挂在图上、不挂在哪个节点上:挂上去就混进了那个节点自己的问题清单。
要改哪几个节点,预览里逐项列着。"""
ready = [m for m in upgrade_candidates(spec) if not m.blocked]
if not ready:
return None
parts = [m.summary for m in ready[:3]] + ([f"另有 {len(ready) - 3} 处"] if len(ready) > 3 else [])
return f"{HINT}:{';'.join(parts)}。先预览改动,确认了才保存"
# --------------------------------------------------------------------------
# 改图(原地改副本)
# --------------------------------------------------------------------------
def _raw(graph: dict[str, Any], node_id: str) -> dict[str, Any]:
return next(n for n in graph.get("nodes") or [] if isinstance(n, dict) and n.get("id") == node_id)
def _config(node: dict[str, Any]) -> dict[str, Any]:
data = node.setdefault("data", {})
if not isinstance(data.get("config"), dict):
data["config"] = {}
return data["config"]
def _note(text: str, rule: str, node_id: str | None, level: str = "info") -> dict[str, Any]:
return {"text": text, "rule": rule, "node_id": node_id, "level": level}
def _change(node_id: str | None, title: str, field_name: str, before: Any, after: Any) -> dict[str, Any]:
return {"node_id": node_id, "node_title": title, "field": field_name, "before": copy.deepcopy(before),
"after": copy.deepcopy(after)}
def _config_changes(node_id: str, title: str, before: dict[str, Any], after: dict[str, Any]) -> list[dict[str, Any]]:
return [_change(node_id, title, key, before.get(key), after.get(key))
for key in dict.fromkeys([*after, *before]) if before.get(key) != after.get(key)]
def _policy(level: str, config: dict[str, Any], defaults: dict[str, Any]) -> dict[str, Any]:
"""受管级别下新写出来的报告撰写节点,照门禁 G3 写明最严的三项:已经(按节点、再按全图默认)是这个
值、或者更严的(claims: judge)不动。已发布级别不替探索运行拧紧,照缺省走。"""
if level != "governed":
return {}
out = {}
for key, want, _ in REPORT_POLICY:
value = config.get(key)
effective = defaults.get(key) if value in (None, "") else value
if effective not in (want, *REPORT_POLICY_ALSO.get(key, ())):
out[key] = want
return out
def _report_from(contract: dict[str, Any], node_id: str) -> dict[str, Any]:
"""narrative 换成 report_from,放在 narrative 原来的位置,其余的键原样留着。原来写着的空 report_from(等于没声明,
upgrade_candidates 也这么认)丢掉:它排在 narrative 后面的话,逐键照抄会把新值盖成空的,契约的核对就悄悄关了。"""
out: dict[str, Any] = {}
for key, value in contract.items():
if key == "narrative":
out["report_from"] = node_id
elif key != "report_from":
out[key] = copy.deepcopy(value)
out.setdefault("report_from", node_id)
return out
def _span_refs(text: str) -> list[tuple[str, list[Ref]]]:
return [(m.group(0), _refs(m.group(1), template=False)) for m in _SPAN.finditer(text)]
def _echoed_evidence(graph: _Graph, consumer: str, text: str) -> list[str]:
"""指令里照搬了哪几段上游取数、口径卡的文字(原样的 {{ … }})。"""
out = []
for span, refs in _span_refs(text):
if any(src.type in _EVIDENCE_TYPES for ref in refs for src in graph.sources(consumer, ref)):
out.append(span.strip())
return list(dict.fromkeys(out))
def _retype(graph: dict[str, Any], spec: GraphSpec, match: Match, level: str) -> _Step:
"""R1 / R2:模型调用换成报告撰写。R1 另把契约的 narrative 换成 report_from。"""
node = spec.node_map()[match.node_id]
raw = _raw(graph, node.id)
before = copy.deepcopy(_config(raw))
text = "\n\n".join(p for p in (str(before.get("system") or "").strip(), str(before.get("prompt") or "").strip()) if p)
after: dict[str, Any] = {"instructions": text} if text else {}
after.update({k: copy.deepcopy(v) for k, v in before.items() if k not in _LLM_ONLY})
after.update(_policy(level, after, spec.defaults))
raw["type"] = NodeType.REPORT.value
raw["data"]["config"] = after
title = node.title
step = _Step(label=(f"把「{title}」换成报告撰写,出具契约改为核对它的文档" if match.rule == "R1"
else f"把「{title}」换成报告撰写"),
retyped={(node.id, NodeType.LLM.value, NodeType.REPORT.value)}, created=True)
step.changes = [_change(node.id, title, "type", NodeType.LLM.value, NodeType.REPORT.value),
*_config_changes(node.id, title, before, after)]
step.ops = [{"op": "replace_node", "id": node.id,
"node": {"id": node.id, "type": NodeType.REPORT.value, "label": raw["data"].get("label") or "",
"config": copy.deepcopy(after)}}]
dropped = [k for k in _LLM_ONLY if k not in ("prompt", "system") and before.get(k) not in (None, "", [], {})]
if before.get("skills"):
skills = before["skills"] if isinstance(before["skills"], list) else [before["skills"]]
step.notes.append(_note(f"「{title}」挂的技能 {'、'.join(f'「{s}」' for s in skills)} 不会再加载:报告撰写不挂技能,"
"其中的写作要求要保留的话,写进 instructions", match.rule, node.id, "warning"))
if other := [k for k in dropped if k != "skills"]:
step.notes.append(_note(f"「{title}」的 {'、'.join(other)} 报告撰写用不上,已去掉", match.rule, node.id))
if echoed := _echoed_evidence(_Graph(spec), node.id, text):
step.notes.append(_note(
f"「{title}」的 instructions 里照搬了 {'、'.join(echoed)}:报告撰写会自己收集上游的口径卡指标和查询结果,数字由"
"系统渲染;直接塞进指令的这段文字里的数,模型容易照抄成不带出处的裸数字(会被打回重写)。确认用不上就删掉,"
"升级没有替你删", match.rule, node.id))
if match.rule == "R1":
for exit_id in match.exits:
exit_raw = _raw(graph, exit_id)
cfg = _config(exit_raw)
was = copy.deepcopy(cfg.get("contract"))
cfg["contract"] = _report_from(was or {}, node.id)
step.changes.append(_change(exit_id, spec.node_map()[exit_id].title, "contract", was, cfg["contract"]))
step.ops.append({"op": "update_node", "id": exit_id, "config": {"contract": copy.deepcopy(cfg["contract"])}})
return step
def _fresh_id(graph: dict[str, Any], base: str) -> str:
taken = {str(n.get("id")) for n in graph.get("nodes") or [] if isinstance(n, dict)}
if base not in taken:
return base
return next(f"{base}_{i}" for i in range(2, len(taken) + 3) if f"{base}_{i}" not in taken)
def _answer_instructions(agent: GraphNode) -> str:
"""插进来的报告撰写写什么。问数据的图里问题在 input.question;别的图照搬 agent 的要求。agent 的原话附在
后面当思路(和 Copilot 手册里问数据的写法一致),明说里面的数不能照抄。"""
prompt = str(agent.config.get("prompt") or "").strip()
if _QUESTION.search(prompt):
head = "回答:{{ input.question }}"
elif prompt:
head = f"按下面的要求写给读的人看的回答(查数已经由上游「{agent.title}」做完,这里只写):\n{prompt}"
else:
head = "根据证据目录里的查询结果,写一份简洁的回答"
return (f"{head}\n\n「{agent.title}」查数时的分析过程附在下面,只作思路参考:里面的数字不能照抄,一律用引用标记"
f"从证据目录里取。\n{{{{ nodes.{agent.id}.text }}}}")
def _agent_text_head(agent: GraphNode) -> re.Pattern[str]:
"""模板里「这个 agent 写的整段文字」的写法:nodes.X、nodes.X.text、vars.<assign_to>(没开 cite_fields 时)。"""
nid = re.escape(agent.id)
heads = [rf"nodes\s*(?:\.\s*{nid}|\[\s*['\"]{nid}['\"]\s*\])(?:\s*(?:\.\s*text|\[\s*['\"]text['\"]\s*\]))?"]
var = str(agent.config.get("assign_to") or "").strip()
if var and not _cited(agent):
heads.append(rf"vars\s*(?:\.\s*{re.escape(var)}|\[\s*['\"]{re.escape(var)}['\"]\s*\])")
return re.compile("|".join(f"(?:{h})" for h in heads))
def _swap_text(value: str, agent: GraphNode, report_id: str) -> str:
head_re = _agent_text_head(agent)
def repl(m: re.Match[str]) -> str:
head, bar, rest = m.group(1).partition("|")
if not head_re.fullmatch(head.strip()):
return m.group(0)
return f"{{{{ nodes.{report_id}.text{(' |' + rest.rstrip()) if bar else ''} }}}}"
return _SPAN.sub(repl, value)
def _insert(graph: dict[str, Any], spec: GraphSpec, match: Match, level: str) -> _Step:
"""R3:agent 和出口之间插一个报告撰写。agent → 出口的连线改接成 agent → 报告 → 出口,出口字段里 agent 的
原话改取报告的正文;没配字段的出口交的是最后一条消息,插了报告以后就是报告写的,不用改。"""
nodes = spec.node_map()
agent = nodes[match.node_id]
raw_agent = _raw(graph, agent.id)
rid = _fresh_id(graph, "report")
config: dict[str, Any] = {"instructions": _answer_instructions(agent)}
config.update({k: copy.deepcopy(agent.config[k]) for k in _MODEL_KEYS if agent.config.get(k) not in (None, "")})
config.update(_policy(level, config, spec.defaults))
here = raw_agent.get("position") or {}
there = _raw(graph, match.exits[0]).get("position") or {}
position = {"x": round((float(here.get("x") or 0) + float(there.get("x") or 0)) / 2, 1),
"y": round((float(here.get("y") or 0) + float(there.get("y") or 0)) / 2 + 120, 1)}
new = {"id": rid, "type": NodeType.REPORT.value, "position": position,
"data": {"label": "报告撰写", "config": config}}
graph["nodes"].insert(graph["nodes"].index(raw_agent) + 1, new)
exits = [nodes[x] for x in match.exits]
step = _Step(label=f"在「{agent.title}」和{_names(exits)}之间插入报告撰写,出口改取报告的正文",
rewired={(agent.id, x.id) for x in exits}, created=True)
step.changes.append(_change(rid, "报告撰写", "node", None,
{"id": rid, "type": NodeType.REPORT.value, "label": "报告撰写", "config": config}))
step.ops.append({"op": "add_node", "node": {"id": rid, "type": NodeType.REPORT.value, "label": "报告撰写",
"config": copy.deepcopy(config)}})
def link(source: str, target: str) -> dict[str, Any]:
return {"id": f"{source}:->{target}", "source": source, "target": target}
converted: list[dict[str, Any]] = [] # 这一步从叙述模式改成 report_from 的契约
graph["edges"] = [e for e in graph.get("edges") or []
if not (isinstance(e, dict) and e.get("source") == agent.id and e.get("target") in match.exits)]
graph["edges"].append(link(agent.id, rid))
step.changes.append(_change(None, "", "edge", None, {"source": agent.id, "target": rid}))
step.ops.append({"op": "add_edge", "edge": {"source": agent.id, "target": rid}})
for x in exits:
graph["edges"].append(link(rid, x.id))
step.changes.append(_change(None, "", "edge", {"source": agent.id, "target": x.id},
{"source": rid, "target": x.id}))
step.ops += [{"op": "remove_edge", "source": agent.id, "target": x.id},
{"op": "add_edge", "edge": {"source": rid, "target": x.id}}]
cfg = _config(_raw(graph, x.id))
updates: dict[str, Any] = {}
fields = cfg.get("fields")
if isinstance(fields, list):
swapped = [{**f, "value": _swap_text(f["value"], agent, rid)}
if isinstance(f, dict) and isinstance(f.get("value"), str) else f for f in fields]
if swapped != fields:
step.changes.append(_change(x.id, x.title, "fields", fields, swapped))
updates["fields"] = swapped
cfg["fields"] = swapped
exact = f"{{{{ nodes.{rid}.text }}}}"
mixed = [str(f.get("name")) for f, old in zip(swapped, fields)
if isinstance(f, dict) and f != old and f.get("value", "").strip() != exact]
if mixed:
step.notes.append(_note(
f"「{x.title}」的成果字段{'、'.join(f'「{n}」' for n in mixed)}在报告正文前后还拼了别的内容:这样没法"
f"逐段对应证据。要每个数都点得开,字段里只放 {exact}", "R3", x.id))
contract = cfg.get("contract")
narrative = contract.get("narrative") if isinstance(contract, dict) else None
if isinstance(narrative, str) and agent.id in {
n.id for n in _writers(_Graph(spec), x.id, _refs(narrative, template=True), (NodeType.AGENT,))}:
was = copy.deepcopy(contract)
cfg["contract"] = _report_from(contract, rid)
converted.append(cfg["contract"])
step.changes.append(_change(x.id, x.title, "contract", was, cfg["contract"]))
updates["contract"] = cfg["contract"]
if updates:
step.ops.append({"op": "update_node", "id": x.id, "config": copy.deepcopy(updates)})
if note := _cells_note(spec, exits, converted, level):
step.notes.append(_note(note[0], "R3", rid, note[1]))
return step
def _cells_note(spec: GraphSpec, exits: list[GraphNode], converted: list[dict[str, Any]],
level: str) -> tuple[str, str] | None:
"""插进来的报告撰写会直接引用查询单元格,受管级别的正式运行只在出口契约写了 cells: true 时才认
(governance.cells_allowed)。已经写了的不提;按受管级别升级的,或者把原有契约改成了 report_from、图上又没有
口径卡可走的,说成警告:不写的话报告里的数一个都引用不了,报告节点每次正式运行都会失败。"""
if converted and all(c.get("cells") is True for c in converted):
return None
where = _names(exits)
choose = (f"二选一:在{where}的出具契约里写 cells: true;或者改走口径卡,把要写的数登记成指标,报告只引用 "
"[[m:指标id]]。选哪条要你拿主意,升级没有替你写")
cards = any(n.type == NodeType.METRICS for n in spec.nodes)
if level == "governed" or (converted and not cards):
lead = "按受管级别" if level == "governed" else "现在(已发布级别)照常;以后按受管级别发布的话"
return (f"报告会直接引用查询单元格([[v:Q1.r0.列]]),{lead},正式运行只在出口的出具契约写了 cells: true 时才认单元格"
"引用:不写的话报告里的数一个都引用不了,报告节点(numbers: strict、on_violation: fail)每次正式运行都会失败。"
+ choose, "warning")
return ("报告会直接引用查询单元格([[v:Q1.r0.列]])。探索运行和已发布级别照常;要按受管级别正式出具,得在出口的出具契约里写 "
"cells: true,或者改走口径卡:把要写的数登记成指标,报告只引用 [[m:指标id]]。选哪条要你拿主意,升级没有替你加契约",
"info")
def _whole_var_readers(spec: GraphSpec, var: str) -> list[tuple[GraphNode, str]]:
"""模板里把 vars.<var> 整个拿来用的地方:(节点, 原样的 {{ … }})。取字段的(vars.kpi.gmv)不算。"""
head_re = re.compile(rf"vars\s*(?:\.\s*{re.escape(var)}|\[\s*['\"]{re.escape(var)}['\"]\s*\])")
def strings(value: Any) -> list[str]:
if isinstance(value, str):
return [value]
if isinstance(value, dict):
return [s for v in value.values() for s in strings(v)]
if isinstance(value, list):
return [s for v in value for s in strings(v)]
return []
out: dict[tuple[str, str], tuple[GraphNode, str]] = {}
for n in spec.nodes:
for text in strings(n.config):
for m in _SPAN.finditer(text):
if head_re.fullmatch(m.group(1).partition("|")[0].strip()):
out.setdefault((n.id, m.group(0).strip()), (n, m.group(0).strip()))
return list(out.values())
def _cite(graph: dict[str, Any], spec: GraphSpec, match: Match, level: str) -> _Step:
"""R4:打开 cite_fields。打开以后 assign_to 拿到的是按 Schema 核对过的对象,不再是文字:把它整个当文字
用的地方说出来,不替人改。"""
agent = spec.node_map()[match.node_id]
cfg = _config(_raw(graph, agent.id))
before = cfg.get("cite_fields")
cfg["cite_fields"] = True
step = _Step(label=f"打开「{agent.title}」的 cite_fields:每个字段都核对到查询结果里的那一格")
step.changes = [_change(agent.id, agent.title, "cite_fields", before, True)]
step.ops = [{"op": "update_node", "id": agent.id, "config": {"cite_fields": True}}]
var = str(agent.config.get("assign_to") or "").strip()
for reader, span in _whole_var_readers(spec, var) if var else []:
step.notes.append(_note(
f"「{agent.title}」开了 cite_fields 以后,vars.{var} 拿到的是核对过的对象,不再是文字:「{reader.title}」里的 "
f"{span} 会显示成对象。要原话就改成 {{{{ nodes.{agent.id}.text }}}}", "R4", reader.id, "warning"))
return step
_APPLY: dict[str, Callable[[dict[str, Any], GraphSpec, Match, str], _Step]] = {
"R1": _retype, "R2": _retype, "R3": _insert, "R4": _cite}
def compute_feeders(spec: GraphSpec) -> list[tuple[GraphNode, list[GraphNode]]]:
"""喂口径卡、角色又不是取数(evidence_role 不是 source)的沙箱代码:(代码节点, 读它的口径卡)。
R5 只建议;assist 时交给 Copilot 看是不是纯算术。"""
graph = _Graph(spec)
found: dict[str, tuple[GraphNode, list[GraphNode]]] = {}
for card in spec.nodes:
if card.type != NodeType.METRICS:
continue
for feeder in _card_feeders(graph, card):
if feeder.type == NodeType.CODE and feeder.config.get("evidence_role") != "source":
found.setdefault(feeder.id, (feeder, []))[1].append(card)
return list(found.values())
def metric_feeders(spec: GraphSpec, card: GraphNode) -> list[tuple[int, GraphNode]]:
"""口径卡每个指标追到头读的是谁:(指标下标, 来源节点)。和 compute_feeders 同一条追法(穿过整形、校验节点,
governance._card_feeders),只是逐个指标追:code → 整形 → 口径卡的,指标记在这段代码名下。"""
graph = _Graph(spec)
metrics = card.config.get("metrics")
out: list[tuple[int, GraphNode]] = []
for i, metric in enumerate(metrics if isinstance(metrics, list) else []):
one = card.model_copy(update={"data": card.data.model_copy(update={"config": {**card.config,
"metrics": [metric]}})})
out += [(i, feeder) for feeder in _card_feeders(graph, one)]
return out
def _r5_notes(spec: GraphSpec) -> list[dict[str, Any]]:
return [_note(
f"「{code.title}」(沙箱代码)的产出喂给了口径卡{_names(cards)}:它如果是在取数(查库、调接口、读文件),把 "
"evidence_role 标成 source,口径卡读它时记得下出处;如果是在做计算,把计算挪进口径卡的表达式(交给 Copilot "
"可以请它把纯算术的代码改写成口径卡表达式)。它在取数还是在算只有你知道,升级没有替你改", "R5", code.id)
for code, cards in compute_feeders(spec)]
# --------------------------------------------------------------------------
# 入口
# --------------------------------------------------------------------------
def forbidden(before: dict[str, Any], after: dict[str, Any], *,
retyped: set[tuple[str, str, str]] | frozenset[tuple[str, str, str]] = frozenset(),
rewired: set[tuple[str, str]] | frozenset[tuple[str, str]] = frozenset()) -> list[str]:
"""升级的禁止规则:和发布前修复同一套(删节点、删连线、删契约、放宽审批、关 strict、改级别…一律拦),
只放过这一步自己点名的换类型(模型调用 → 报告撰写)和改接的连线。"""
return forbidden_changes(before, after, retyped=frozenset(retyped), rewired=frozenset(rewired))
def upgrade_for_evidence(graph: dict[str, Any] | GraphSpec, *, level: str = "published") -> dict[str, Any]:
"""按 R1–R4 改写,R5 只给建议。纯函数,不动传进来的图;同一张图升级两次,第二次什么都不改。
level 是复核门禁用的发布级别:受管级别下新写出来的报告撰写节点另写明门禁 G3 要的三项。
返回 {graph, changes, ops, notes, applied, rejected}:
- changes:[{fix_id, rule, label, node_id, node_title, field, before, after}];field 为 type 是节点换了类型,
node 是新插入的节点(after 是节点),edge 是连线(before / after 是 {source, target},改接的两边都有)
- ops:形状同 Copilot 的操作流,另有 replace_node {id, node}(换类型,node 是整个新节点)
- notes:[{text, rule, node_id, level}],不改、只建议的,以及看着像旧结构、但没法确定怎么改的
- applied:采用的 "R1:<节点 id>"…;rejected:[{fix_id, reason}],碰了禁止规则或者会冒出新 error 的
"""
if level not in LEVELS:
raise ValueError(f"level 只能是 {' / '.join(LEVELS)},写的是 {level!r}")
raw = graph.model_dump(mode="json") if isinstance(graph, GraphSpec) else graph
current = copy.deepcopy(raw)
spec = GraphSpec.model_validate(current)
issues = publish_issues(spec, level=level)
changes: list[dict[str, Any]] = []
ops: list[dict[str, Any]] = []
notes: list[dict[str, Any]] = []
applied: list[str] = []
rejected: list[dict[str, Any]] = []
created: str | None = None # 第一处写出报告撰写节点的规则
for rule in RULES:
tried: set[str] = set()
while True:
# 每一步之后重新认:前一步可能改了后一步要看的结构(插了报告的出口不再读 agent 的原话)
match = next((m for m in upgrade_candidates(spec) if m.rule == rule and m.node_id not in tried), None)
if match is None:
break
tried.add(match.node_id)
if match.blocked:
# 一个说不准的叙述会挡下拼进去的每个模型调用,原因只说一遍
if not any(n["text"] == match.blocked for n in notes):
notes.append(_note(match.blocked, rule, match.node_id, "warning"))
continue
notes += [_note(why, rule, match.node_id, "warning") for why in match.skipped]
fix_id = f"{rule}:{match.node_id}"
trial = copy.deepcopy(current)
step = _APPLY[rule](trial, spec, match, level)
try:
trial_spec = GraphSpec.model_validate(trial)
except Exception: # noqa: BLE001 - 规则把图改坏了:丢弃,不交给人一张存不回去的图
rejected.append({"fix_id": fix_id, "reason": "改完的图结构读不懂,这一步没有采用"})
continue
trial_issues = publish_issues(trial_spec, level=level)
why = ";".join(forbidden(current, trial, retyped=step.retyped, rewired=step.rewired)) \
or worse(issues, trial_issues)
if why:
rejected.append({"fix_id": fix_id, "reason": why})
notes.append(_note(f"{step.label}:没有采用,{why}", rule, match.node_id, "warning"))
continue
current, spec, issues = trial, trial_spec, trial_issues
applied.append(fix_id)
changes += [{"fix_id": fix_id, "rule": rule, "label": step.label, **c} for c in step.changes]
ops += step.ops
notes += step.notes
created = created or (rule if step.created else None)
notes += _r5_notes(spec)
if created and level != "governed":
notes.append(_note(
"报告撰写重写之后仍有没出处的数字时:探索运行照常产出、把问题标在报告里;正式运行里 on_violation 缺省是 fail,节点"
"会失败。想先标注着看,把 on_violation 设成 flag", created, None))
return {"graph": current, "changes": changes, "ops": ops, "notes": notes, "applied": applied, "rejected": rejected}
+1 -1
View File
@@ -38,7 +38,7 @@ dependencies = [
# environment.yml 里已经带上了;单独装用 `pip install 'agentlab-backend[microvm]'`。
microvm = ["microsandbox>=0.6"]
# 测试:conda 环境默认不装,跑测试前 pip install 'agentlab-backend[dev]'
dev = ["pytest>=8.0", "pytest-asyncio>=0.24"]
dev = ["pytest>=8.0", "pytest-asyncio>=0.24", "pytest-xdist>=3.6"]
# 文档解析。PDF 和 Word 的解析库加起来几十兆,只用纯文本的人不该为此
# 付出安装时间——和 microvm、db 一样做成可选。HTML 用已在依赖里的 bs4。
docs = ["pypdf>=5.0", "python-docx>=1.1", "python-pptx>=1.0", "openpyxl>=3.1"]
+5
View File
@@ -56,6 +56,8 @@ docstring-parser==0.18.0
# via anthropic
et-xmlfile==2.0.0
# via openpyxl
execnet==2.1.2
# via pytest-xdist
fastapi==0.141.1
# via agentlab-backend (pyproject.toml)
greenlet==3.5.5
@@ -210,8 +212,11 @@ pytest==9.1.1
# via
# agentlab-backend (pyproject.toml)
# pytest-asyncio
# pytest-xdist
pytest-asyncio==1.4.0
# via agentlab-backend (pyproject.toml)
pytest-xdist==3.8.0
# via agentlab-backend (pyproject.toml)
python-docx==1.2.0
# via agentlab-backend (pyproject.toml)
python-dotenv==1.2.3
+80
View File
@@ -397,3 +397,83 @@ def test_a_graph_with_a_caliber_card_still_needs_required_even_without_metrics_f
validate, gate = contract_codes(graph)
assert "contract.required_missing" in gate, gate # 不写 metrics_from 绕不过必需指标
assert validate == [], validate
# --------------------------------------------------------------------------
# 没有口径卡、又没写 cells: true:受管门禁要拦下,发布弹窗里一键选
#
# 放开 metrics_from / required 之后留下的口子:这种契约以前被 metrics_from 挡着,现在能过门禁,
# 可受管正式运行里单元格引用一律不认,报告撰写节点(numbers strict、on_violation fail)每次都失败。
# 写不写 cells 要作者定(模型不许替人打开),所以修复是 choice:写 cells: true,或交给 Copilot 改成口径卡
# --------------------------------------------------------------------------
def _plan(graph: dict, level: str = "governed") -> dict[str, dict]:
from app.engine.autofix import plan_fixes
from app.engine.governance import publish_issues
from app.engine.schema import GraphSpec
spec = GraphSpec.model_validate(graph)
return {f["id"]: f for f in plan_fixes(spec, publish_issues(spec, level=level), level=level)}
def test_a_governed_graph_without_cards_must_declare_cells():
graph = cells_only()
graph["nodes"][-1]["data"]["config"]["contract"].pop("cells")
_, gate = contract_codes(graph)
assert gate == ["contract.cells_undeclared"], gate
_, gate = contract_codes(cells_only(cells=False))
assert gate == ["contract.cells_undeclared"], gate
_, gate = contract_codes(graph, level="published") # 已发布级别照样允许直接引用单元格
assert "contract.cells_undeclared" not in gate
def test_a_graph_with_a_caliber_card_does_not_need_cells():
_, gate = contract_codes(weekly()) # 有口径卡:数可以走 [[m:]],cells 是可选的
assert "contract.cells_undeclared" not in gate, gate
def test_the_fix_is_a_choice_and_picking_cells_is_not_loosening():
from app.engine.autofix import HANDOFF, apply_fixes
graph = cells_only()
graph["nodes"][-1]["data"]["config"]["contract"].pop("cells")
fid = "contract.cells_undeclared:out"
fix = _plan(graph)[fid]
assert fix["kind"] == "choice" and fix["field"] == "contract.cells"
assert [o["value"] for o in fix["options"]] == [True, HANDOFF]
assert [o.get("handoff") is True for o in fix["options"]] == [False, True]
assert apply_fixes(graph, [fid], level="governed")["applied"] == [] # 不替人选
out = apply_fixes(graph, [fid], {fid: True}, level="governed")
assert out["applied"] == [fid] and out["rejected"] == [], out["rejected"]
assert out["graph"]["nodes"][-1]["data"]["config"]["contract"]["cells"] is True and out["ok"] is True
out = apply_fixes(graph, [fid], {fid: HANDOFF}, level="governed")
assert out["handoff"] == [fid] and out["applied"] == []
def test_turning_cells_on_unasked_is_still_loosening():
from app.engine.autofix import forbidden_changes
before = cells_only()
before["nodes"][-1]["data"]["config"]["contract"].pop("cells")
after = cells_only()
[why] = forbidden_changes(before, after)
assert "cells" in why
assert forbidden_changes(before, after, chosen={("out", "contract.cells")}) == []
def test_only_a_newly_raised_human_choice_error_is_let_through():
from app.engine.autofix import judge, worse
no_contract = {"level": "error", "code": "governed.no_contract", "node_id": "out", "message": "没有契约"}
cells = {"level": "error", "code": "contract.cells_undeclared", "node_id": "out", "field": "contract.cells",
"message": "要写 cells"}
other = {"level": "error", "code": "contract.strict_off", "node_id": "out", "message": "strict 关了"}
assert judge([no_contract], [cells]) is None # 补上契约、cells 留给人:算更好
assert worse([], [cells]) is None
assert "冒出新的问题" in (judge([no_contract], [other]) or "") # 别的新 error 照样作废
assert judge([no_contract], [cells, other]) is not None
# 原来就有的 cells 照常计数:修它的那一步,改完还在就是没修好
assert "还在" in (judge([cells], [cells], target=("contract.cells_undeclared", "out")) or "")
assert judge([cells], [], target=("contract.cells_undeclared", "out")) is None
assert judge([cells], [cells]) == "改完错误没有变少" # 原来就有、改完还在:Copilot 兜底不算修好
@@ -0,0 +1,265 @@
"""Copilot 搭图自查拦得下配错的报告撰写节点(证据五期)。
- metrics_from 指向的不是口径卡:validate 本来就报 error,自查交回去改
- 报告撰写的上游没有任何证据来源(没有口径卡、查库的 agent / 调用工具、知识检索):运行时只会给一条警告、
报告里每个数都被判成没有出处——搭图时就打回去,按 error 交给模型改
- 配对了的报告撰写节点不多花一次自查调用
- 旧结构「可以升级为可追溯结构」的建议不混进这一轮的问题清单
"""
from __future__ import annotations
import json
import pytest
from httpx import ASGITransport, AsyncClient
from app.api.copilot import authored_issues
from app.engine.schema import GraphSpec
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 add(nid, ntype, label, **config):
return {"op": "add_node", "node": {"id": nid, "type": ntype, "label": label, "config": config}}
def link(a, b):
return {"op": "add_edge", "edge": {"source": a, "target": b}}
class Scripted:
"""第一轮吐 first;收到自查请求(human 里有「上线前自查」)时吐 fix。"""
def __init__(self, first: list[dict], fix: list[dict] | None = None) -> None:
self.first = [json.dumps(o, ensure_ascii=False) for o in first]
self.fix = [json.dumps(o, ensure_ascii=False) for o in fix or []]
self.calls: list[str] = []
async def astream(self, messages):
from langchain_core.messages import AIMessageChunk
human = next(text for role, text in messages if role == "human")
self.calls.append(human)
for line in (self.fix if "上线前自查" in human else self.first):
yield AIMessageChunk(content=line + "\n")
async def generate(client, monkeypatch, model: Scripted) -> list[dict]:
import app.api.copilot as copilot
async def _model(*_a, **_k):
return model, "mock-fast"
monkeypatch.setattr(copilot, "get_chat_model", _model)
events: list[dict] = []
async with client.stream("POST", "/api/copilot/generate-stream", json={"instruction": "写一份订单周报"}) as r:
async for line in r.aiter_lines():
if line.startswith("data: "):
events.append(json.loads(line[6:]))
return events
def wrap(*ops) -> list[dict]:
return [{"op": "plan", "summary": "周报"}, *ops, {"op": "done", "explanation": "周报", "run": False}]
FETCH = add("fetch", "tool", "查库", tool="db_query__shop", args={"sql": "SELECT COUNT(*) AS n FROM orders"})
CARD = add("card", "metrics", "口径卡", metrics=[{"id": "n", "name": "订单数", "expression": "cell(nodes.fetch, 0, 'n')"}])
OUT = add("out", "output", "成果", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}])
def issues_of(events, status):
return [i for e in events if e["op"] == "check" and e.get("status") == status for i in e["issues"]]
async def test_metrics_from_pointing_at_a_non_card_is_sent_back(client, monkeypatch):
first = wrap(add("start", "input", "输入"), FETCH, link("start", "fetch"), CARD, link("fetch", "card"),
add("write", "report", "报告撰写", instructions="写周报", metrics_from=["fetch"]),
link("card", "write"), OUT, link("write", "out"))
fix = [{"op": "update_node", "id": "write", "config": {"metrics_from": ["card"]}}, {"op": "done"}]
model = Scripted(first, fix)
events = await generate(client, monkeypatch, model)
sent = issues_of(events, "repairing")
assert any(i["code"] == "report.metrics_from_invalid" and i["node_id"] == "write" for i in sent), sent
assert "不是「口径卡」" in model.calls[1]
assert [e["status"] for e in events if e["op"] == "check"] == ["repairing", "passed"]
async def test_a_report_with_nothing_to_cite_upstream_is_sent_back(client, monkeypatch):
first = wrap(add("start", "input", "输入"), add("write", "report", "报告撰写", instructions="写周报"),
link("start", "write"), OUT, link("write", "out"))
fix = [FETCH, link("start", "fetch"), link("fetch", "write"), {"op": "done"}]
model = Scripted(first, fix)
events = await generate(client, monkeypatch, model)
[sent] = [i for i in issues_of(events, "repairing") if i["code"] == "report_no_source"]
assert sent["level"] == "error" and sent["node_id"] == "write"
assert "没有能引用数字的证据来源" in model.calls[1]
assert [e["status"] for e in events if e["op"] == "check"] == ["repairing", "passed"]
final = events[-1]
assert final["op"] == "final" and not [i for i in final["issues"] if i.get("code") == "report_no_source"]
async def test_a_well_wired_report_costs_no_extra_call(client, monkeypatch):
first = wrap(add("start", "input", "输入"),
add("ask", "agent", "查数", tools=["db_query__shop"], prompt="查本周订单数"),
link("start", "ask"), add("write", "report", "报告撰写", instructions="写周报"),
link("ask", "write"), OUT, link("write", "out"))
model = Scripted(first)
events = await generate(client, monkeypatch, model)
assert len(model.calls) == 1 and [e["status"] for e in events if e["op"] == "check"] == ["passed"]
async def test_the_upgrade_hint_is_not_part_of_the_copilot_issue_list(client, monkeypatch):
# 只问清单的问数据图就是 agent → output:validate 会建议升级,但这一轮的问题清单里不该有它
first = wrap(add("start", "input", "输入"),
add("ask", "agent", "查数", tools=["db_query__shop"], prompt="列出订单表里的店铺"),
link("start", "ask"),
add("out", "output", "成果", fields=[{"name": "answer", "value": "{{ nodes.ask.text }}"}]),
link("ask", "out"))
events = await generate(client, monkeypatch, Scripted(first))
final = events[-1]
assert final["op"] == "final"
assert not [i for i in final["issues"] if i.get("code") == "evidence.upgrade_available" or i["level"] == "info"]
def spec_of(nodes, edges):
return GraphSpec.model_validate({"nodes": nodes, "edges": edges})
def n(nid, ntype, **config):
return {"id": nid, "type": ntype, "data": {"label": nid, "config": config}}
@pytest.mark.parametrize("source", [
n("fetch", "tool", tool="db_query__shop"),
n("fetch", "agent", tools=["db_query__shop"]),
n("fetch", "retrieve", collection="docs"),
n("fetch", "metrics", metrics=[{"id": "a", "expression": "1"}]),
n("fetch", "subgraph", workflow_id="w1"),
n("fetch", "supervisor", goal="查清楚", agents=[{"name": "analyst", "tools": ["db_query__shop"]}]),
# 运行输入也是能引用的来源([[i:字段]]):声明了数值字段的入口算
n("fetch", "input", fields=[{"name": "target", "default": 100}]),
n("fetch", "input", fields=[{"name": "week"}, {"name": "rate", "default": "0.35"}]),
n("fetch", "input", fields=[{"name": "budget", "type": "number"}]),
])
def test_any_evidence_source_upstream_is_enough(source):
spec = spec_of([n("start", "input"), source, n("write", "report"), n("out", "output")],
[{"source": "start", "target": "fetch"}, {"source": "fetch", "target": "write"},
{"source": "write", "target": "out"}])
assert not [i for i in authored_issues(spec, []) if i["code"] == "report_no_source"]
@pytest.mark.parametrize("fields", [[], [{"name": "question", "required": True}], [{"name": "topic", "default": "本周"}]])
def test_an_input_without_numeric_fields_is_not_a_source(fields):
# 只声明了文字字段的入口:报告只能照引问题本身,要写的数照样没有出处
spec = spec_of([n("start", "input", fields=fields), n("write", "report"), n("out", "output")],
[{"source": "start", "target": "write"}, {"source": "write", "target": "out"}])
[issue] = [i for i in authored_issues(spec, []) if i["code"] == "report_no_source"]
assert issue["node_id"] == "write" and issue["level"] == "error"
assert "运行输入" in issue["message"] and "[[i:" in issue["message"]
def test_a_numeric_input_upstream_is_enough_for_the_copilot_self_check():
spec = spec_of([n("start", "input", fields=[{"name": "question"}, {"name": "gmv", "default": 1200.5}]),
n("write", "report"), n("out", "output")],
[{"source": "start", "target": "write"}, {"source": "write", "target": "out"}])
assert not [i for i in authored_issues(spec, []) if i["code"] == "report_no_source"]
def test_a_tool_less_agent_or_a_downstream_source_does_not_count():
spec = spec_of([n("start", "input"), n("chat", "agent", prompt="随便聊"), n("write", "report"),
n("late", "tool", tool="db_query__shop"), n("out", "output")],
[{"source": "start", "target": "chat"}, {"source": "chat", "target": "write"},
{"source": "write", "target": "late"}, {"source": "late", "target": "out"}])
[issue] = [i for i in authored_issues(spec, []) if i["code"] == "report_no_source"]
assert issue["node_id"] == "write" and issue["level"] == "error"
def test_reports_this_round_did_not_touch_are_left_alone():
nodes = [n("start", "input"), n("write", "report", instructions="写"), n("out", "output")]
spec = spec_of(nodes, [{"source": "start", "target": "write"}, {"source": "write", "target": "out"}])
assert not [i for i in authored_issues(spec, [], baseline=nodes) if i["code"] == "report_no_source"]
# --------------------------------------------------------------------------
# 问数据的图发成受管:Copilot 补契约(用户报过的那一幕)
# --------------------------------------------------------------------------
def ask_report_graph() -> dict:
"""input → agent(查库)→ 报告撰写(受管三项都写了、正文引单元格)→ 出口,出口还没有契约。"""
def g(nid, ntype, label, **config):
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": label, "config": config}}
return {
"nodes": [
g("start", "input", "问题"),
g("ask", "agent", "查数", tools=["db_query__shop"], prompt="查活跃用户数和用户总数", approval="dangerous"),
g("report", "report", "写答案", instructions="回答:{{ input.question }}", numbers="strict",
on_violation="fail", claims="require_citation"),
g("out", "output", "输出答案", fields=[{"name": "answer", "value": "{{ nodes.report.text }}"}]),
],
"edges": [{"id": f"e{i}", "source": a, "target": b}
for i, (a, b) in enumerate([("start", "ask"), ("ask", "report"), ("report", "out")])],
}
class Assist:
def __init__(self, ops: list[dict]) -> None:
self.lines = [json.dumps(o, ensure_ascii=False) for o in ops]
async def astream(self, messages):
from langchain_core.messages import AIMessageChunk
for line in self.lines:
yield AIMessageChunk(content=line + "\n")
async def governed_assist(client, monkeypatch, ops: list[dict]) -> dict:
import app.api.copilot as copilot
async def _model(*_a, **_k):
return Assist(ops), "mock-fast"
monkeypatch.setattr(copilot, "get_chat_model", _model)
wf = (await client.post("/api/workflows", json={"name": "问数据-受管", "graph": ask_report_graph()})).json()["id"]
r = await client.post(f"/api/workflows/{wf}/autofix", json={"level": "governed", "apply": [], "assist": True})
assert r.status_code == 200, r.text
return r.json()
CELLS_QUESTION = {"op": "question", "node_id": "out",
"text": "报告正文直接引用了查询单元格:契约里写 cells: true,还是改成口径卡指标?"}
async def test_the_copilots_contract_for_a_cells_only_graph_is_adopted(client, monkeypatch):
contract = {"report_from": "report", "strict": True}
out = await governed_assist(client, monkeypatch, [
{"op": "plan", "summary": "补出具契约"},
{"op": "update_node", "id": "out", "config": {"contract": contract}}, CELLS_QUESTION,
{"op": "done", "explanation": "给出口加上契约,cells 要你拿主意"}])
assert out["assist"]["ok"] is True and "assist" in out["applied"], out["rejected"]
assert not [r for r in out["rejected"] if "metrics_from" in r["reason"]]
assert next(n for n in out["graph"]["nodes"] if n["id"] == "out")["data"]["config"]["contract"] == contract
assert out["assist"]["questions"] == ["「输出答案」:" + CELLS_QUESTION["text"]]
# 没有契约那一处没了;cells 要人拍板(Copilot 不许替人打开),改完冒出来的这一处不算 Copilot 改坏的,
# 发布弹窗里给一键选项:写 cells: true,或交给 Copilot 改成口径卡
assert out["ok"] is False
assert [i["code"] for i in out["remaining"] if i["level"] == "error"] == ["contract.cells_undeclared"]
[fix] = [f for f in out["fixes"] if f["code"] == "contract.cells_undeclared"]
assert fix["kind"] == "choice" and [o["value"] for o in fix["options"]] == [True, "copilot"]
async def test_the_copilot_still_may_not_switch_cells_on_by_itself(client, monkeypatch):
out = await governed_assist(client, monkeypatch, [
{"op": "plan", "summary": "补出具契约"},
{"op": "update_node", "id": "out", "config": {"contract": {"report_from": "report", "strict": True,
"cells": True}}},
{"op": "done", "explanation": "补上契约并打开 cells"}])
[rejected] = [r for r in out["rejected"] if r["fix_id"] == "assist"]
assert "cells" in rejected["reason"] and out["ok"] is False
+4 -1
View File
@@ -60,7 +60,10 @@ async def _run(graph, *, timeout=20.0) -> Run:
while asyncio.get_event_loop().time() < deadline:
async with SessionLocal() as session:
row = await session.get(Run, run.id)
if row and row.status in ("succeeded", "failed", "cancelled", "interrupted"):
# 终态先落库、清单随后另一个事务封上(runner._finalize):只等状态的话,负载一高就会
# 读到「跑完了但还没封」的那一瞬。中断态不封存
if row and (row.status == "interrupted"
or row.status in ("succeeded", "failed", "cancelled") and row.manifest_seq is not None):
return row
await asyncio.sleep(0.05)
raise AssertionError("运行没有按时结束")
+216
View File
@@ -0,0 +1,216 @@
"""一键升级的语义层(assist: true):请 Copilot 把喂口径卡的纯算术沙箱代码改写成口径卡表达式。
模型一律用剧本,不调真接口。守这几件事:
- 纯算术的改写被采纳:口径卡的表达式改成直接读上游,代码节点留在图上,只是预览、不改库
- 删节点、放宽要求的改写整个作废,确定性改写的结果照样交出去
- 不是纯算术的保留原样并给警告;模型要人拿主意的原样放进 questions
- 改出新 error 的按自查循环交回去改,改不好就作废
- 没有要改写的代码节点时不调模型
"""
from __future__ import annotations
import json
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, x=0, **config):
return {"id": nid, "type": ntype, "position": {"x": x, "y": 80}, "data": {"label": label or nid, "config": config}}
def graph(*, summary_llm: bool = False) -> dict:
"""查库 → 沙箱代码算比率 → 口径卡读代码的产出 → 报告撰写(或者模型调用写总结)→ 出口。"""
writer = (node("write", "llm", "写总结", x=1120, prompt="写一句总结") if summary_llm
else node("write", "report", "报告撰写", x=1120, instructions="写一句话"))
return {
"nodes": [
node("start", "input", "输入", x=0),
node("fetch", "tool", "查库", x=280, tool="db_query__shop",
args={"sql": "SELECT SUM(amount) AS a, COUNT(*) AS b FROM orders"}),
node("calc", "code", "算比率", x=560, language="python", assign_to="calc",
code="import json\nprint(json.dumps({'ratio': 3 / 4}))"),
node("card", "metrics", "口径卡", x=840, metrics=[
{"id": "ratio", "name": "客单价", "expression": "vars.calc.ratio"}]),
writer,
node("out", "output", "成果", x=1400, fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}]),
],
"edges": [{"id": f"e{i}", "source": a, "target": b} for i, (a, b) in enumerate(
[("start", "fetch"), ("fetch", "calc"), ("calc", "card"), ("card", "write"), ("write", "out")])],
}
class Scripted:
"""第一轮吐 first;收到自查请求(human 里有「上线前自查」)时吐 fix。记下每次的 human。"""
def __init__(self, first: list[dict], fix: list[dict] | None = None) -> None:
self.first = [json.dumps(o, ensure_ascii=False) for o in first]
self.fix = [json.dumps(o, ensure_ascii=False) for o in fix or []]
self.calls: list[str] = []
async def astream(self, messages):
from langchain_core.messages import AIMessageChunk
human = next(text for role, text in messages if role == "human")
self.calls.append(human)
for line in (self.fix if "上线前自查" in human else self.first):
yield AIMessageChunk(content=line + "\n")
def script(monkeypatch, first: list[dict], fix: list[dict] | None = None) -> Scripted:
import app.api.copilot as copilot
model = Scripted(first, fix)
async def _model(*_a, **_k):
return model, "mock-fast"
monkeypatch.setattr(copilot, "get_chat_model", _model)
return model
def wrap(*ops) -> list[dict]:
return [{"op": "plan", "summary": "把纯算术的代码挪进口径卡"}, *ops,
{"op": "done", "explanation": "客单价改由口径卡直接算"}]
REWRITE = {"op": "update_node", "id": "card", "config": {"metrics": [
{"id": "ratio", "name": "客单价", "expression": "cell(nodes.fetch, 0, 'a') / cell(nodes.fetch, 0, 'b')"}]}}
def by_id(g: dict) -> dict:
return {n["id"]: n for n in g["nodes"]}
async def upgrade(client, g: dict, **extra) -> dict:
r = await client.post("/api/copilot/upgrade-evidence", json={"graph": g, "assist": True, **extra})
assert r.status_code == 200, r.text
return r.json()
async def rows() -> list:
async with SessionLocal() as session:
return [sorted(repr({c.name: getattr(r, c.name) for c in m.__table__.columns})
for r in (await session.execute(select(m))).scalars())
for m in (Workflow, WorkflowVersion)]
async def test_a_pure_arithmetic_rewrite_is_adopted_as_a_preview(client, monkeypatch):
model = script(monkeypatch, wrap(REWRITE))
await client.post("/api/workflows", json={"name": "升级-兜底", "graph": graph()})
before = await rows()
body = await upgrade(client, graph())
assert body["assist"]["ok"] is True and body["assist"]["summary"] == "客单价改由口径卡直接算"
assert body["assist"]["warnings"] == [] and "assist" in body["applied"]
g = body["graph"]
assert by_id(g)["card"]["data"]["config"]["metrics"][0]["expression"] == REWRITE["config"]["metrics"][0]["expression"]
assert by_id(g)["calc"]["data"]["config"] == graph()["nodes"][2]["data"]["config"] # 代码节点留着、不替人标 source
assert [c for c in body["changes"] if c["rule"] == "assist"] == [
{"fix_id": "assist", "rule": "assist", "node_id": "card", "node_title": "口径卡", "field": "metrics",
"before": graph()["nodes"][3]["data"]["config"]["metrics"], "after": REWRITE["config"]["metrics"],
"label": "Copilot 的修改"}]
assert body["ops"][-1] == REWRITE
assert not [n for n in body["notes"] if n["rule"] == "R5"] # 口径卡不再读它,建议随之作废
assert await rows() == before # 只是预览
human = model.calls[0]
assert "纯算术" in human and "「算比率」(calc)" in human and "vars.calc.ratio" in human
assert "不许降低要求" in human and "不替人选" in human and "只改上面这些口径卡指标的 expression" in human
assert "只修下面列出的问题" not in human
async def test_the_prompt_names_the_metrics_that_read_the_code_through_a_reshaper(client, monkeypatch):
# 代码 → 整形 → 口径卡:口径卡的指标读的是整形节点,追到头才是这段代码,要改写的表达式照样得列给模型
g = graph()
g["nodes"].insert(3, node("shape", "transform", "整形", x=700, template="{{ vars.calc }}", assign_to="shaped"))
g["nodes"][4]["data"]["config"]["metrics"] = [
{"id": "ratio", "name": "客单价", "expression": "vars.shaped.ratio"},
{"id": "orders", "name": "订单数", "expression": "cell(nodes.fetch, 0, 'b')"}]
g["edges"] = [{"id": f"e{i}", "source": a, "target": b} for i, (a, b) in enumerate(
[("start", "fetch"), ("fetch", "calc"), ("calc", "shape"), ("shape", "card"), ("card", "write"),
("write", "out")])]
model = script(monkeypatch, wrap({"op": "question", "node_id": "calc", "text": "拿不准"}))
await upgrade(client, g)
[line] = [ln for ln in model.calls[0].splitlines() if ln.startswith("- 「算比率」(calc)")]
assert "ratio" in line and "vars.shaped.ratio" in line
assert "orders" not in line # 不读这段代码的指标不列
async def test_deleting_the_code_node_is_refused_and_the_deterministic_result_stays(client, monkeypatch):
script(monkeypatch, wrap(REWRITE, {"op": "remove_node", "id": "calc"}))
body = await upgrade(client, graph(summary_llm=True))
assert body["assist"]["ok"] is False and "assist" not in body["applied"]
[rejected] = [r for r in body["rejected"] if r["fix_id"] == "assist"]
assert "降低" in rejected["reason"] and "算比率" in rejected["reason"]
g = body["graph"]
assert "calc" in by_id(g) and by_id(g)["write"]["type"] == "report" # R2 照样生效
assert by_id(g)["card"]["data"]["config"]["metrics"][0]["expression"] == "vars.calc.ratio"
assert body["applied"] == ["R2:write"]
assert any("算比率" in w for w in body["assist"]["warnings"])
async def test_non_arithmetic_code_is_kept_with_a_warning(client, monkeypatch):
script(monkeypatch, wrap({"op": "question", "node_id": "calc", "text": "它读了一个文件,像是在取数,请确认"}))
body = await upgrade(client, graph())
assert body["assist"]["ok"] is True and body["applied"] == []
assert body["assist"]["questions"] == ["「算比率」:它读了一个文件,像是在取数,请确认"]
[warning] = body["assist"]["warnings"]
assert "算比率" in warning and "evidence_role" in warning and "口径卡" in warning
assert body["graph"] == graph()
assert [n["node_id"] for n in body["notes"] if n["rule"] == "R5"] == ["calc"]
async def test_nothing_to_rewrite_means_no_model_call(client, monkeypatch):
g = graph()
g["nodes"][2]["data"]["config"]["evidence_role"] = "source"
model = script(monkeypatch, wrap(REWRITE))
body = await upgrade(client, g)
assert model.calls == [] and body["assist"]["ok"] is True and body["assist"]["warnings"] == []
assert "没有要交给 Copilot 改写的" in body["assist"]["summary"]
async def test_a_new_error_is_sent_back_once_and_fixed(client, monkeypatch):
broken = {"op": "update_node", "id": "card", "config": {"metrics": [
{"id": "ratio", "name": "客单价", "expression": "cell(nodes.fetch, 0, 'a') /"}]}}
model = script(monkeypatch, wrap(broken), [REWRITE, {"op": "done"}])
body = await upgrade(client, graph())
assert len(model.calls) == 2 and "上线前自查" in model.calls[1] and "表达式" in model.calls[1]
assert body["assist"]["ok"] is True and "assist" in body["applied"]
assert body["assist"]["summary"] == "客单价改由口径卡直接算" # 修补轮的 done 不盖掉说明
expr = by_id(body["graph"])["card"]["data"]["config"]["metrics"][0]["expression"]
assert expr == REWRITE["config"]["metrics"][0]["expression"]
async def test_a_rewrite_that_stays_broken_is_refused(client, monkeypatch):
broken = {"op": "update_node", "id": "card", "config": {"metrics": [
{"id": "ratio", "name": "客单价", "expression": "cell(nodes.fetch, 0, 'a') /"}]}}
model = script(monkeypatch, wrap(broken), [broken, {"op": "done"}])
body = await upgrade(client, graph())
assert len(model.calls) == 3 # 最多交回去改两轮
assert body["assist"]["ok"] is False and "assist" not in body["applied"]
[rejected] = [r for r in body["rejected"] if r["fix_id"] == "assist"]
assert "变糟" in rejected["reason"]
assert by_id(body["graph"])["card"]["data"]["config"]["metrics"][0]["expression"] == "vars.calc.ratio"
async def test_marking_the_code_as_a_source_is_not_the_copilots_call(client, monkeypatch):
script(monkeypatch, wrap({"op": "update_node", "id": "calc", "config": {"evidence_role": "source"}}))
body = await upgrade(client, graph())
[rejected] = [r for r in body["rejected"] if r["fix_id"] == "assist"]
assert "source" in rejected["reason"] and body["graph"] == graph()
async def test_without_assist_there_is_no_model_call(client, monkeypatch):
model = script(monkeypatch, wrap(REWRITE))
r = await client.post("/api/copilot/upgrade-evidence", json={"graph": graph()})
assert r.json()["assist"] is None and model.calls == []
+590
View File
@@ -0,0 +1,590 @@
"""一键升级为可追溯结构(证据五期)。
确定性改写 upgrade_for_evidence 是纯函数、幂等:
- R1 契约的 narrative 指向模型调用节点 → 换成报告撰写,契约改成 report_from
- R2 没有契约、模型调用的文字进了出口、上游有取数 → 同样换成报告撰写
- R3 input → agent → output(问数据的典型图)→ 中间插一个报告撰写,出口改取它的正文
- R4 配了 output_schema 的 agent → 打开 cite_fields
- R5 沙箱代码的产出喂口径卡 → 不改,只在 notes 里建议标成 source
禁止规则和发布前修复同一套,只多两个例外:模型调用换成报告撰写、R3 改接的那条连线。
升级接口只给预览,不改库。
"""
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 Artifact, Run, RunEvent, Setting, Workflow, WorkflowVersion
from app.engine.governance import publish_issues
from app.engine.schema import GraphSpec, validate_graph
from app.engine.upgrade import forbidden, upgrade_candidates, upgrade_for_evidence
from app.main import app
UPGRADE = "evidence.upgrade_available"
@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, x=0, **config):
return {"id": nid, "type": ntype, "position": {"x": x, "y": 80}, "data": {"label": label or nid, "config": config}}
def edge(i, a, b, handle=None):
out = {"id": f"e{i}", "source": a, "target": b}
if handle:
out["sourceHandle"] = handle
return out
def edges_of(*pairs):
return [edge(i, a, b) for i, (a, b) in enumerate(pairs)]
def narrative_graph() -> dict:
"""旧的周报:口径卡算数、模型调用写叙述、契约按数值回指叙述(R1)。"""
return {
"nodes": [
node("start", "input", "周期", x=0, fields=[{"name": "week", "default": "2026-W37"}]),
node("fetch", "code", "取数", x=280, language="python", evidence_role="source", assign_to="agg",
code="import json\nprint(json.dumps({'orders': 12, 'amount': 345.5}))"),
node("caliber", "metrics", "周报口径卡", x=560, caliber="周报口径", caliber_version="v1", metrics=[
{"id": "orders", "name": "订单数", "unit": "单", "expression": "vars.agg.orders"},
{"id": "amount", "name": "销售额", "unit": "元", "expression": "vars.agg.amount"}]),
node("narrate", "llm", "叙述", x=840, system="你是周报撰写人。", provider="mock", model="mock-writer",
temperature=0.2, prompt="{{ nodes.caliber.text }}\n\n请为 {{ input.week }} 写周报正文。",
assign_to="report", skills=["结构化分析"]),
node("done", "output", "出具", x=1120, fields=[
{"name": "周报", "value": "{{ vars.report }}"},
{"name": "指标清单", "value": "{{ nodes.caliber.text }}"}],
contract={"metrics_from": ["caliber"], "narrative": "{{ vars.report }}", "required": ["amount"],
"expected": ["orders"], "allow_numbers": ["37"], "strict": False}),
],
"edges": edges_of(("start", "fetch"), ("fetch", "caliber"), ("caliber", "narrate"), ("narrate", "done")),
"viewport": {"x": 1, "y": 2, "zoom": 0.9},
}
def summary_graph() -> dict:
"""没有契约:调用工具查库,模型调用写总结,出口直接取它的文字(R2)。"""
return {
"nodes": [
node("start", "input", "输入"),
node("fetch", "tool", "查库", tool="db_query__shop", args={"sql": "SELECT COUNT(*) AS n FROM orders"}),
node("sum", "llm", "总结", prompt="根据 {{ nodes.fetch }} 写一句总结"),
node("out", "output", "成果", fields=[{"name": "总结", "value": "{{ nodes.sum.text }}"}]),
],
"edges": edges_of(("start", "fetch"), ("fetch", "sum"), ("sum", "out")),
}
def ask_graph(**agent) -> dict:
"""问数据的典型图:input → agent(绑查库工具)→ output(R3)。"""
cfg = {"tools": ["db_query__shop"], "prompt": "查库回答:{{ input.question }}", "assign_to": "answer", **agent}
return {
"nodes": [
node("start", "input", "问题", x=0),
node("ask", "agent", "查数作答", x=280, **cfg),
node("out", "output", "答案", x=560, fields=[{"name": "answer", "value": "{{ vars.answer }}"}]),
],
"edges": edges_of(("start", "ask"), ("ask", "out")),
}
def schema_graph() -> dict:
"""agent 配了 output_schema 却没开 cite_fields(R4),另有一个节点把它的 assign_to 当文字用。"""
return {
"nodes": [
node("start", "input", "输入"),
node("fetch", "agent", "取数", tools=["db_query__shop"], prompt="查本周销售额", assign_to="kpi",
output_schema={"type": "object", "properties": {"gmv": {"type": "number"}}}),
node("card", "metrics", "口径卡", metrics=[{"id": "gmv", "name": "销售额", "expression": "vars.kpi.gmv"}]),
node("write", "report", "报告撰写", instructions="写一句话"),
node("peek", "output", "调试", fields=[{"name": "原样", "value": "{{ vars.kpi }}"}]),
node("out", "output", "成果", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}]),
],
"edges": edges_of(("start", "fetch"), ("fetch", "card"), ("card", "write"), ("write", "out"),
("fetch", "peek")),
}
def compute_graph() -> dict:
"""沙箱代码在算数、口径卡读它(R5:只建议,不改)。"""
return {
"nodes": [
node("start", "input", "输入"),
node("fetch", "tool", "查库", tool="db_query__shop", args={"sql": "SELECT 1 AS a, 2 AS b"}),
node("calc", "code", "计算", language="python", assign_to="calc",
code="import json\nprint(json.dumps({'ratio': 1 / 2}))"),
node("card", "metrics", "口径卡", metrics=[{"id": "ratio", "name": "比率", "expression": "vars.calc.ratio"}]),
node("write", "report", "报告撰写", instructions="写一句话"),
node("out", "output", "成果", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}]),
],
"edges": edges_of(("start", "fetch"), ("fetch", "calc"), ("calc", "card"), ("card", "write"),
("write", "out")),
}
def nodes_by_id(graph: dict) -> dict:
return {n["id"]: n for n in graph["nodes"]}
def cfg(graph: dict, nid: str) -> dict:
return nodes_by_id(graph)[nid]["data"]["config"]
def pairs(graph: dict) -> set:
return {(e["source"], e["target"]) for e in graph["edges"]}
def errors(graph: dict, level: str = "published") -> list:
return [i for i in publish_issues(GraphSpec.model_validate(graph), level=level) if i.level == "error"]
def infos(graph: dict) -> list:
return [i for i in validate_graph(GraphSpec.model_validate(graph)).issues if i.code == UPGRADE]
# --------------------------------------------------------------------------
# R1–R5
# --------------------------------------------------------------------------
def test_r1_turns_the_narrating_llm_into_a_report_and_the_contract_into_report_from():
before = narrative_graph()
out = upgrade_for_evidence(before)
g = out["graph"]
write = nodes_by_id(g)["narrate"]
assert write["type"] == "report" and write["data"]["label"] == "叙述"
c = write["data"]["config"]
# system 和 prompt 合成 instructions,模型配置照搬,assign_to 留着(出口还在读 vars.report)
assert c["instructions"] == "你是周报撰写人。\n\n{{ nodes.caliber.text }}\n\n请为 {{ input.week }} 写周报正文。"
assert (c["provider"], c["model"], c["temperature"], c["assign_to"]) == ("mock", "mock-writer", 0.2, "report")
assert not {"prompt", "system", "skills"} & set(c)
contract = cfg(g, "done")["contract"]
assert contract == {"metrics_from": ["caliber"], "report_from": "narrate", "required": ["amount"],
"expected": ["orders"], "allow_numbers": ["37"], "strict": False}
assert out["applied"] == ["R1:narrate"] and out["rejected"] == []
type_change = next(ch for ch in out["changes"] if ch["field"] == "type")
assert (type_change["node_id"], type_change["before"], type_change["after"], type_change["rule"]) == (
"narrate", "llm", "report", "R1")
assert any(ch["node_id"] == "done" and ch["field"] == "contract" for ch in out["changes"])
assert g["viewport"] == before["viewport"] and [e["id"] for e in g["edges"]] == [e["id"] for e in before["edges"]]
assert errors(g) == [] and infos(g) == []
texts = " ".join(n["text"] for n in out["notes"])
assert "结构化分析" in texts # 技能不再挂载要说出来
assert "{{ nodes.caliber.text }}" in texts # 指令里照搬了口径卡的文字,提醒可以删
def test_r2_replaces_an_llm_whose_text_reaches_an_exit_without_a_contract():
out = upgrade_for_evidence(summary_graph())
g = out["graph"]
assert nodes_by_id(g)["sum"]["type"] == "report"
assert cfg(g, "sum")["instructions"] == "根据 {{ nodes.fetch }} 写一句总结"
assert out["applied"] == ["R2:sum"] and errors(g) == [] and infos(g) == []
assert "contract" not in cfg(g, "out") # R2 不替人加契约
def test_r2_leaves_an_llm_with_nothing_fetched_upstream_alone():
graph = summary_graph()
graph["nodes"] = [n for n in graph["nodes"] if n["id"] != "fetch"]
graph["edges"] = edges_of(("start", "sum"), ("sum", "out"))
out = upgrade_for_evidence(graph)
assert out["changes"] == [] and out["graph"] == graph and infos(graph) == []
def test_r2_does_not_count_knowledge_retrieval_as_fetching_data():
# 知识库问答的答案换成报告撰写,文档里的数只能当引文、不能当值引用,全会被判成裸数字:不算取数
graph = summary_graph()
graph["nodes"][1] = node("fetch", "retrieve", "检索", query="{{ input.question }}", collection="docs")
out = upgrade_for_evidence(graph)
assert out["changes"] == [] and infos(graph) == []
def test_r3_inserts_a_report_between_the_agent_and_the_exit():
before = ask_graph()
out = upgrade_for_evidence(before)
g = out["graph"]
[rid] = [n["id"] for n in g["nodes"] if n["type"] == "report"]
assert pairs(g) == {("start", "ask"), ("ask", rid), (rid, "out")}
assert cfg(g, "out")["fields"] == [{"name": "answer", "value": f"{{{{ nodes.{rid}.text }}}}"}]
instructions = cfg(g, rid)["instructions"]
assert "{{ input.question }}" in instructions and "{{ nodes.ask.text }}" in instructions
assert cfg(g, "ask") == cfg(before, "ask") # agent 本身不动
assert out["applied"] == [f"R3:ask"] and errors(g) == [] and infos(g) == []
fields = {ch["field"] for ch in out["changes"]}
assert {"node", "edge", "fields"} <= fields
rewire = next(ch for ch in out["changes"] if ch["field"] == "edge" and ch["before"])
assert rewire["before"] == {"source": "ask", "target": "out"} and rewire["after"] == {"source": rid, "target": "out"}
assert "contract" not in cfg(g, "out") # 契约(和 cells)要人拿主意,不替人加
assert any("cells" in n["text"] for n in out["notes"])
def test_r3_skips_agents_that_only_read_table_structures():
graph = ask_graph(tools=["db_schema__shop"])
assert upgrade_for_evidence(graph)["changes"] == [] and infos(graph) == []
def test_r4_turns_on_cite_fields_and_warns_where_the_variable_was_read_as_text():
out = upgrade_for_evidence(schema_graph())
g = out["graph"]
assert cfg(g, "fetch")["cite_fields"] is True
assert out["applied"] == ["R4:fetch"] and errors(g) == [] and infos(g) == []
[note] = [n for n in out["notes"] if n["rule"] == "R4"]
assert note["node_id"] == "peek" and "{{ vars.kpi }}" in note["text"]
def test_r5_only_suggests_marking_the_code_node_as_a_source():
before = compute_graph()
out = upgrade_for_evidence(before)
assert out["graph"] == before and out["changes"] == []
[note] = [n for n in out["notes"] if n["rule"] == "R5"]
assert note["node_id"] == "calc" and "source" in note["text"]
assert infos(before) == [] # 只有 R5 的不算「旧结构」
@pytest.mark.parametrize("make", [narrative_graph, summary_graph, ask_graph, schema_graph, compute_graph])
def test_upgrading_twice_changes_nothing_more(make):
first = upgrade_for_evidence(make())
second = upgrade_for_evidence(first["graph"])
assert second["graph"] == first["graph"] and second["changes"] == [] and second["applied"] == []
@pytest.mark.parametrize("make", [narrative_graph, summary_graph, ask_graph, schema_graph])
def test_upgrade_is_pure(make):
graph = make()
kept = copy.deepcopy(graph)
upgrade_for_evidence(graph)
upgrade_for_evidence(GraphSpec.model_validate(graph))
assert graph == kept
def test_graph_spec_input_gives_the_same_result():
assert upgrade_for_evidence(GraphSpec.model_validate(ask_graph()))["applied"] == ["R3:ask"]
def test_governed_upgrades_write_the_strict_report_settings():
out = upgrade_for_evidence(ask_graph(), level="governed")
[rid] = [n["id"] for n in out["graph"]["nodes"] if n["type"] == "report"]
c = cfg(out["graph"], rid)
assert (c["numbers"], c["on_violation"], c["claims"]) == ("strict", "fail", "require_citation")
assert out["rejected"] == []
# published 下不替探索运行拧紧:修不掉的数字违规照常标注
loose = cfg(upgrade_for_evidence(ask_graph())["graph"], rid)
assert not {"numbers", "on_violation", "claims"} & set(loose)
def test_a_rewrite_that_would_add_an_error_is_dropped_with_a_reason():
graph = narrative_graph()
# 叙述节点没连到出口:换成报告撰写以后 report_from 指向不在上游的节点,会冒出新的 error
graph["edges"] = edges_of(("start", "fetch"), ("fetch", "caliber"), ("caliber", "narrate"), ("caliber", "done"))
out = upgrade_for_evidence(graph)
assert out["graph"] == graph and out["changes"] == [] and out["applied"] == []
[rejected] = out["rejected"]
assert rejected["fix_id"] == "R1:narrate" and "新的问题" in rejected["reason"]
def test_a_narrative_that_mixes_two_writers_is_not_guessed():
graph = narrative_graph()
graph["nodes"].insert(4, node("extra", "llm", "补充", prompt="补一句", assign_to="more"))
graph["edges"].append(edge(9, "caliber", "extra"))
graph["edges"].append(edge(10, "extra", "done"))
cfg(graph, "done")["contract"]["narrative"] = "{{ vars.report }}{{ vars.more }}"
out = upgrade_for_evidence(graph)
assert not any(a.startswith("R1:") for a in out["applied"])
assert any(n["rule"] == "R1" and "补充" in n["text"] for n in out["notes"])
assert infos(graph) == [] # 说不准怎么改的不建议升级
def two_exit_narrative_graph(*, extra_first: bool, mixed_exit_first: bool) -> dict:
"""两个出口都读「叙述」:「出具」的 narrative 还拼了「补充」的文字(说不准),「出具2」只读「叙述」。"""
graph = narrative_graph()
extra = node("extra", "llm", "补充", prompt="补一句", assign_to="more")
graph["nodes"].insert(3 if extra_first else 4, extra)
graph["edges"] += [edge(9, "caliber", "extra"), edge(10, "extra", "done"), edge(11, "narrate", "done2")]
cfg(graph, "done")["contract"]["narrative"] = "{{ vars.report }}{{ vars.more }}"
other = node("done2", "output", "出具2", fields=[{"name": "周报", "value": "{{ vars.report }}"}],
contract={"metrics_from": ["caliber"], "narrative": "{{ vars.report }}", "required": ["amount"],
"strict": False})
done = next(i for i, n in enumerate(graph["nodes"]) if n["id"] == "done")
graph["nodes"].insert(done + 1 if mixed_exit_first else done, other)
return graph
@pytest.mark.parametrize("extra_first", [False, True])
@pytest.mark.parametrize("mixed_exit_first", [False, True])
def test_an_ambiguous_narrative_blocks_the_retype_whatever_the_node_order(extra_first, mixed_exit_first):
# 同一个模型调用从两个出口认出来:一个出口说不准就整个不换——不看哪个出口在画布上排前面
graph = two_exit_narrative_graph(extra_first=extra_first, mixed_exit_first=mixed_exit_first)
out = upgrade_for_evidence(graph)
assert out["graph"] == graph and out["changes"] == [] and out["applied"] == []
blocked = [n for n in out["notes"] if n["rule"] == "R1" and n["level"] == "warning"]
assert len(blocked) == 1 and "补充" in blocked[0]["text"] and "叙述" in blocked[0]["text"]
assert infos(graph) == []
def test_a_narrative_that_mixes_an_llm_and_an_agent_is_not_guessed():
graph = narrative_graph()
graph["nodes"].insert(4, node("ask", "agent", "查数", tools=["db_query__shop"], prompt="查一下", assign_to="ans"))
graph["edges"] += [edge(9, "caliber", "ask"), edge(10, "ask", "done")]
cfg(graph, "done")["contract"]["narrative"] = "{{ vars.report }}\n{{ nodes.ask.text }}"
out = upgrade_for_evidence(graph)
assert out["changes"] == [] and out["graph"] == graph
assert any(n["rule"] == "R1" and n["level"] == "warning" and "查数" in n["text"] for n in out["notes"])
assert any(n["rule"] == "R3" and n["level"] == "warning" and "出具" in n["text"] for n in out["notes"])
assert infos(graph) == []
def indirect_ask_graph(*, indirect_first: bool) -> dict:
"""问数据的 agent 直连「答案」,另一个出口「转述」经过整形节点才读到它的原话。"""
graph = ask_graph()
graph["nodes"].insert(2, node("shape", "transform", "整形", template="{{ vars.answer }}", assign_to="shaped"))
other = node("out2", "output", "转述", fields=[{"name": "原话", "value": "{{ nodes.ask.text }}"}])
graph["nodes"].insert(3 if indirect_first else 4, other)
graph["edges"] += [edge(7, "ask", "shape"), edge(8, "shape", "out2")]
return graph
@pytest.mark.parametrize("indirect_first", [False, True])
def test_r3_rewires_the_direct_exit_and_explains_the_indirect_one_whatever_the_node_order(indirect_first):
graph = indirect_ask_graph(indirect_first=indirect_first)
out = upgrade_for_evidence(graph)
g = out["graph"]
assert out["applied"] == ["R3:ask"] and out["rejected"] == []
[rid] = [n["id"] for n in g["nodes"] if n["type"] == "report"]
assert pairs(g) == {("start", "ask"), ("ask", rid), (rid, "out"), ("ask", "shape"), ("shape", "out2")}
assert cfg(g, "out")["fields"] == [{"name": "answer", "value": f"{{{{ nodes.{rid}.text }}}}"}]
assert cfg(g, "out2") == cfg(graph, "out2") # 经过别的节点的出口不替人改
[skipped] = [n for n in out["notes"] if n["rule"] == "R3" and n["level"] == "warning"]
assert "转述" in skipped["text"] and "经过别的节点" in skipped["text"]
assert infos(graph) and infos(g) == []
again = upgrade_for_evidence(g)
assert again["changes"] == [] and again["graph"] == g
def test_r3_with_only_indirect_exits_is_not_guessed():
graph = indirect_ask_graph(indirect_first=True)
graph["nodes"] = [n for n in graph["nodes"] if n["id"] != "out"]
graph["edges"] = [e for e in graph["edges"] if e["target"] != "out"]
out = upgrade_for_evidence(graph)
assert out["changes"] == [] and out["graph"] == graph
[blocked] = [n for n in out["notes"] if n["rule"] == "R3"]
assert blocked["level"] == "warning" and "转述" in blocked["text"]
assert infos(graph) == []
@pytest.mark.parametrize("unset", ["", None])
@pytest.mark.parametrize("after_narrative", [True, False])
def test_an_unset_report_from_does_not_swallow_the_new_one(unset, after_narrative):
graph = narrative_graph()
contract = cfg(graph, "done")["contract"]
rest = {k: v for k, v in contract.items() if k != "narrative"}
cfg(graph, "done")["contract"] = ({"narrative": contract["narrative"], "report_from": unset, **rest}
if after_narrative else
{"report_from": unset, "narrative": contract["narrative"], **rest})
out = upgrade_for_evidence(graph)
assert out["applied"] == ["R1:narrate"]
assert cfg(out["graph"], "done")["contract"] == {"report_from": "narrate", **rest}
def test_an_unset_report_from_is_dropped_when_r3_converts_the_narrative():
graph = ask_graph()
cfg(graph, "out")["contract"] = {"narrative": "{{ vars.answer }}", "report_from": "", "strict": True}
out = upgrade_for_evidence(graph)
[rid] = [n["id"] for n in out["graph"]["nodes"] if n["type"] == "report"]
assert cfg(out["graph"], "out")["contract"] == {"report_from": rid, "strict": True}
def cells_notes(out: dict) -> list:
return [n for n in out["notes"] if n["rule"] == "R3" and "cells: true" in n["text"]]
def test_r3_warns_that_governed_formal_runs_fail_without_cells():
out = upgrade_for_evidence(ask_graph(), level="governed")
[note] = cells_notes(out)
assert note["level"] == "warning" and "每次正式运行都会失败" in note["text"] and "口径卡" in note["text"]
def test_r3_warns_when_it_turns_a_contract_on_a_graph_without_cards_into_report_from():
graph = ask_graph()
cfg(graph, "out")["contract"] = {"narrative": "{{ vars.answer }}", "strict": True}
out = upgrade_for_evidence(graph)
[note] = cells_notes(out)
assert note["level"] == "warning" and "每次正式运行都会失败" in note["text"]
assert "cells" not in cfg(out["graph"], "out")["contract"] # 要人拿主意,不替人打开
def test_r3_only_informs_about_cells_at_published_level_without_a_contract():
[note] = cells_notes(upgrade_for_evidence(ask_graph()))
assert note["level"] == "info"
def test_r3_says_nothing_about_cells_when_the_contract_already_declares_them():
graph = ask_graph()
cfg(graph, "out")["contract"] = {"narrative": "{{ vars.answer }}", "strict": True, "cells": True}
out = upgrade_for_evidence(graph, level="governed")
assert out["applied"] == ["R3:ask"] and cells_notes(out) == []
def test_candidates_list_what_the_upgrade_would_touch():
assert [(m.rule, m.node_id) for m in upgrade_candidates(GraphSpec.model_validate(narrative_graph()))] == [
("R1", "narrate")]
assert [(m.rule, m.node_id) for m in upgrade_candidates(GraphSpec.model_validate(ask_graph()))] == [
("R3", "ask")]
# --------------------------------------------------------------------------
# 禁止规则
# --------------------------------------------------------------------------
def test_deleting_a_node_is_refused():
before = narrative_graph()
after = copy.deepcopy(before)
after["nodes"] = [n for n in after["nodes"] if n["id"] != "fetch"]
assert any("删了节点" in r for r in forbidden(before, after))
def test_llm_to_report_is_allowed_only_for_the_node_the_upgrade_names():
before = narrative_graph()
after = copy.deepcopy(before)
nodes_by_id(after)["narrate"]["type"] = "report"
assert forbidden(before, after, retyped={("narrate", "llm", "report")}) == []
assert any("换成了" in r for r in forbidden(before, after))
# 别的换类型(比如把沙箱代码换成口径卡)不在例外里
nodes_by_id(after)["fetch"]["type"] = "metrics"
assert any("取数" in r and "换成了" in r for r in forbidden(before, after, retyped={("narrate", "llm", "report")}))
def test_only_the_rewired_edge_may_go():
before = ask_graph()
after = copy.deepcopy(before)
after["edges"] = [e for e in after["edges"] if (e["source"], e["target"]) != ("ask", "out")]
assert forbidden(before, after, rewired={("ask", "out")}) == []
assert any("删了连线" in r for r in forbidden(before, after))
after["edges"] = []
assert any("start → ask" in r for r in forbidden(before, after, rewired={("ask", "out")}))
@pytest.mark.parametrize("damage, word", [
(lambda g: cfg(g, "done").pop("contract"), "契约"),
(lambda g: cfg(g, "done")["contract"].update(required=[]), "required"),
(lambda g: cfg(g, "done")["contract"].update(strict=True), None),
])
def test_contract_rules_are_the_publish_fix_rules(damage, word):
before = narrative_graph()
after = copy.deepcopy(before)
damage(after)
reasons = forbidden(before, after)
if word:
assert any(word in r for r in reasons)
else:
assert reasons == [] # 收紧不算
def test_strict_off_and_looser_approval_are_refused():
before = ask_graph(approval="dangerous")
before["nodes"].append(node("fin", "output", "出具", contract={"report_from": "w", "strict": True}))
after = copy.deepcopy(before)
cfg(after, "ask")["approval"] = "never"
cfg(after, "fin")["contract"]["strict"] = False
reasons = forbidden(before, after)
assert any("全部自动放行" in r for r in reasons) and any("strict" in r for r in reasons)
# --------------------------------------------------------------------------
# validate 的建议
# --------------------------------------------------------------------------
def test_old_structures_get_one_info_and_nothing_else_changes():
spec = GraphSpec.model_validate(narrative_graph())
result = validate_graph(spec)
[hint] = [i for i in result.issues if i.code == UPGRADE]
assert hint.level == "info" and hint.message.startswith("可以升级为可追溯结构") and hint.node_id is None
assert "「叙述」" in hint.message # 说得出要改哪里
assert result.ok is (not any(i.level == "error" for i in result.issues))
assert len([i for i in validate_graph(GraphSpec.model_validate(ask_graph())).issues if i.code == UPGRADE]) == 1
def test_already_traceable_graphs_get_no_info():
for graph in (upgrade_for_evidence(narrative_graph())["graph"], compute_graph()):
assert infos(graph) == []
def test_the_info_is_not_a_blocker_for_runs_or_publishing():
graph = ask_graph()
assert errors(graph) == [] and validate_graph(GraphSpec.model_validate(graph)).ok is True
# --------------------------------------------------------------------------
# 升级接口:只给预览,不改库
# --------------------------------------------------------------------------
async def _snapshot() -> dict:
async with SessionLocal() as session:
out = {}
for model in (Workflow, WorkflowVersion, Run, RunEvent, Artifact, Setting):
rows = (await session.execute(select(model))).scalars().all()
out[model.__tablename__] = sorted(
(repr({c.name: getattr(r, c.name) for c in model.__table__.columns}) for r in rows))
return out
async def test_the_endpoint_previews_without_touching_the_database(client):
created = await client.post("/api/workflows", json={"name": "升级-只读", "graph": narrative_graph()})
assert created.status_code == 201, created.text
before = await _snapshot()
r = await client.post("/api/copilot/upgrade-evidence", json={"graph": narrative_graph()})
assert r.status_code == 200, r.text
body = r.json()
assert set(body) >= {"graph", "changes", "ops", "notes", "issues", "rejected", "assist"}
assert body["assist"] is None
assert nodes_by_id(body["graph"])["narrate"]["type"] == "report"
assert any(op["op"] == "replace_node" and op["id"] == "narrate" for op in body["ops"])
assert not [i for i in body["issues"] if i["level"] == "error"]
assert not [i for i in body["issues"] if i.get("code") == UPGRADE]
assert await _snapshot() == before # 一行都没改
stored = (await client.get(f"/api/workflows/{created.json()['id']}")).json()["graph"]
assert nodes_by_id(stored)["narrate"]["type"] == "llm"
async def test_the_endpoint_places_the_inserted_report_without_moving_the_others(client):
from app.engine.layout import NODE_W
graph = ask_graph()
# 一个不相干的节点正好摆在 agent 和出口之间偏下的空处:新插进来的报告撰写不能压在它身上
graph["nodes"].append(node("memo", "memory", "记一笔", x=420, action="write", content="{{ input.question }}"))
graph["nodes"][-1]["position"]["y"] = 200
graph["edges"].append(edge(9, "start", "memo"))
body = (await client.post("/api/copilot/upgrade-evidence", json={"graph": graph})).json()
placed = nodes_by_id(body["graph"])
for nid, n in nodes_by_id(graph).items():
assert placed[nid]["position"] == n["position"]
[rid] = [nid for nid in placed if nid not in nodes_by_id(graph)]
mine = placed[rid]["position"]
for nid, n in placed.items():
if nid != rid:
p = n["position"]
assert abs(p["x"] - mine["x"]) >= NODE_W or abs(p["y"] - mine["y"]) >= 60, (nid, p, mine)
async def test_the_endpoint_explains_an_unreadable_graph(client):
r = await client.post("/api/copilot/upgrade-evidence", json={"graph": {"nodes": [{"id": "bad id!", "type": "llm"}]}})
assert r.status_code == 200
body = r.json()
assert body["changes"] == [] and body["issues"][0]["level"] == "error" and "读不懂" in body["issues"][0]["message"]
async def test_the_endpoint_rejects_an_unknown_level(client):
r = await client.post("/api/copilot/upgrade-evidence", json={"graph": ask_graph(), "level": "everything"})
assert r.status_code == 422