补上四道形同虚设的门禁

## 沙箱关不掉

get() 先看 prefer,而 _resolve_named 完全不看 settings.sandbox_backend。
配置里 off 只挡得住默认路径,图里任何 isolation=strict 的代码节点照样能跑。
另外 alias.get(prefer, prefer) 对未知档位原样透传,而 isolation 是图 JSON 里的
自由字符串,写 isolation="local" 就能点名那个明说了"没有任何访问控制"的兜底
后端——档位是用来提高隔离的,不该能降级。现在 off 优先判定,档位限白名单。

## formal 可以跑从未过闸的版本

"不可变"和"过过闸"是两件事:PATCH 存出来的草稿同样有不可变快照。原来只查
version 存不存在,于是工作流只要发布过任意一版,之后所有草稿都能以 formal
身份启动,发布期的 validate_graph + lint_for_publish 全部绕过。这和代码里
"正式运行的全部语义就这一条规则:必须引用一个不可变的已发布版本"直接矛盾。
现在要求 version == published_version,否则 409。

放大这个问题的是 update_workflow 改图不重置 status:发布过 governed 之后把
画布改成含 supervisor 的图,界面上仍显示 governed。status 说的是当前画布的
状态,改图就退回 draft;已发布的那版由 published_version 指着,formal 不受影响
(所以 formal 的前置检查也从看 status 改成只看 published_version)。

## governed lint 只查 contract 这个 key 在不在

契约内容一律不校验,于是 contract={"metrics_from": "随便一个字符串"} 就满足了
"受管模板必须声明出具契约"这条硬门禁——一个结构上保证每次都出 formal 的空壳
模板能合法发布成受管模板。门禁写成这样还不如没有:它给的是"这张图过过闸"的
错觉。现在实质校验:metrics_from 必须指向图里真实存在的口径卡节点、narrative
必须非空、governed 下 required 必须非空、非 strict 给警告。

端到端验证:空壳契约发布被拒(4 条 issue)、改图后 status 退回 draft 且
published_version 保留、formal 跑 v2 被 409 拒、跑已发布的 v1 正常。
This commit is contained in:
ThearchyHelios
2026-09-14 15:30:57 +08:00
parent 2207269e07
commit 0afffd4b9a
4 changed files with 77 additions and 3 deletions
+13 -1
View File
@@ -75,9 +75,21 @@ async def start_run(
workflow = await session.get(Workflow, payload.workflow_id)
if not workflow:
raise HTTPException(404, "工作流不存在")
if workflow.status not in ("published", "governed") or not workflow.published_version:
# 只看 published_version:status 反映的是当前画布(改过图就退回 draft),
# 而 formal 跑的是已发布的那一版,两者本来就可以不一致
if not workflow.published_version:
raise HTTPException(409, f"「{workflow.name}」还没有发布版本,先在编排页发布")
version = payload.version or workflow.published_version
# "不可变"和"过过闸"是两件事。快照确实不可变,但 PATCH 保存出来的
# 草稿版本同样有快照——只认 version 存不存在的话,工作流只要发布过
# 任意一个版本,之后所有草稿都能以 formal 身份启动,发布期的
# validate_graph + lint_for_publish 全部绕过。
if version != workflow.published_version:
raise HTTPException(
409,
f"v{version} 不是当前发布版本(v{workflow.published_version})。"
"正式运行只能引用发布过的版本——要跑这一版就先发布它。",
)
snapshot = (
await session.execute(
_select(WorkflowVersion).where(
+5
View File
@@ -122,6 +122,11 @@ async def update_workflow(
note=payload.note,
)
)
# status 说的是**当前画布**的状态,不是"这个工作流曾经发布过"。
# 改完图还挂着 governed,界面上就会显示一张从未过闸的图是受管模板——
# 已发布的那一版仍由 published_version 指着,formal 运行不受影响。
if workflow.status in ("published", "governed"):
workflow.status = "draft"
await session.commit()
await session.refresh(workflow)
return workflow
+47 -1
View File
@@ -61,8 +61,10 @@ def lint_for_publish(spec: GraphSpec, *, level: str) -> ValidationResult:
node_id=node.id,
)
elif node.type == NodeType.OUTPUT:
if cfg.get("contract"):
contract = cfg.get("contract")
if contract:
has_contract_output = True
_lint_contract(contract, node_id=node.id, spec=spec, flag=flag, strict=strict)
elif node.type == NodeType.CODE:
if cfg.get("network"):
flag("代码节点开了网络,受管模板建议把取数收敛到工具节点(有快照)", node_id=node.id)
@@ -75,6 +77,50 @@ def lint_for_publish(spec: GraphSpec, *, level: str) -> ValidationResult:
return result
def _lint_contract(
contract: Any, *, node_id: str, spec: GraphSpec, flag: Any, strict: bool
) -> None:
"""契约要查内容,不能只查这个 key 在不在。
只看 key 存不存在的话,contract={"metrics_from": "随便一个字符串"} 就满足
"受管模板必须声明出具契约"这条硬门禁了 —— 一个结构上保证每次都出 formal 的
空壳模板,能合法发布成受管模板。门禁写成这样,还不如没有:它给的是
"这张图过过闸"的错觉。
"""
if not isinstance(contract, dict):
flag("contract 必须是对象", node_id=node_id, hard=True)
return
metric_nodes = {n.id for n in spec.nodes if n.type == NodeType.METRICS}
sources = contract.get("metrics_from") or []
if isinstance(sources, str):
sources = [sources]
if not sources:
flag("契约没有声明 metrics_from,叙述里的数字无从回指", node_id=node_id, hard=True)
for src in sources:
if src not in metric_nodes:
flag(
f"契约的 metrics_from 指向 {src!r},但图里没有这个口径卡节点"
"(写错了,或者那个节点还没建)",
node_id=node_id, hard=True,
)
if not str(contract.get("narrative") or "").strip():
flag("契约没有声明 narrative,数字回指校验不会执行", node_id=node_id, hard=True)
if strict and not (contract.get("required") or []):
flag(
"受管模板的契约必须声明 required 指标,否则'必需指标缺失'这一档永远触发不了",
node_id=node_id, hard=True,
)
if strict and not contract.get("strict"):
flag(
"受管模板建议把契约设为 strict:非 strict 下未回指的数字只降档不拦截",
node_id=node_id,
)
async def unresolved_caliber_upgrades(
session: AsyncSession, spec: GraphSpec
) -> tuple[list[str], list[dict[str, Any]]]:
+12 -1
View File
@@ -78,8 +78,19 @@ class SandboxManager:
def _resolve_named(self, prefer: str) -> Sandbox | None:
"""按档位名取一个**已就绪**的后端;不可用时返回 None 让调用方退回默认。"""
# 关掉就是关掉。这条必须在最前面:否则配置里 off 只挡得住默认路径,
# 图里任何一个 isolation=strict 的代码节点照样能跑代码,
# DisabledSandbox 那句"沙箱已在设置中关闭"根本到不了。
if (settings.sandbox_backend or "auto").lower() == "off":
return None
# 只认这两个档位。以前是 alias.get(prefer, prefer) 原样透传,而 isolation
# 是图 JSON 里的自由字符串,于是图里写 isolation="local" 就能点名那个
# 明说了"没有任何访问控制"的兜底后端——档位是用来提高隔离的,不该能降级。
alias = {"strict": "microvm", "fast": "seatbelt" if sys.platform == "darwin" else "bubblewrap"}
name = alias.get(prefer, prefer)
name = alias.get(prefer)
if name is None:
return None
if name in self._named:
return self._named[name]
impl = dict(_BACKENDS).get(name)