用 CI 检查企业 Agent 的上线条件
文章用 YAML 清单和 Python 脚本把身份、工具网关、模型回退、出站白名单、状态持久化和运行预算等要求变成自动检查。Agent 缺少必要配置时,CI 可以阻止发布。
AI 深度解读
把 AI Agent 从演示搬到生产,先要回答几个具体问题。它用谁的身份调用工具?模型挂了怎么办?一次任务最多花多少钱?暂停后从哪里继续?作者把这些问题归成七项上线要求,用一份 YAML 清单和一个 Python 脚本,让 CI 在合并前检查配置。
这套做法适合已经能跑通业务流程、准备把 Agent 接进真实系统的团队。最值得拿走的是把上线约束放进同一个 PR 审查。脚本能发现漏配和明显冲突,权限、网络和预算能否真正生效,还得看运行环境。
七项要求怎么落到一份 YAML 里?
作者把生产要求分成七项:独立身份、工具网关、模型网关、网络出口白名单、持久化状态、费用预算,以及追踪与回滚。前四项规定 Agent 能用什么身份、经过哪里、访问哪些服务。后三项决定任务中断后能不能续跑、费用失控时怎么处理,以及出了问题能查到什么、退回哪个版本。

原文用一个假想的发票分流 Agent 展示如何把这些约束写进仓库。审查者可以直接看到负责团队、允许调用的工具,以及读发票和改状态分别需要什么权限。
agent: invoice-triage
owner: finance-platform-team
environment: production
identity:
principal: spiffe://corp.example/agents/invoice-triage
credential_ttl_minutes: 15
shared_credentials: false
tools:
gateway: https://tools.corp.example/mcp
token_audience: https://tools.corp.example/mcp
allowed:
- name: erp.read_invoice
scopes: [invoices:read]
- name: erp.update_status
scopes: [invoices:write]
models:
gateway: https://models.corp.example
primary: model-a
fallback: model-b
network:
egress_allowlist:
- tools.corp.example
- models.corp.example
- otel.corp.example
state:
store: postgres
checkpoint_every_step: true
budget:
max_usd_per_run: 2.50
max_usd_per_day: 150
on_exceed: pause
observability:
trace_endpoint: https://otel.corp.example:4318
rollback:
previous_release: invoice-triage@1.4.2
看 erp.update_status 下的 scopes: [invoices:write]。一旦业务从读取发票变成修改状态,权限变化就会出现在代码审查里。网络出口也一样。清单列出三个允许访问的主机,实际连接能否被拦住,取决于部署环境有没有应用这份规则。
CI 能拦下哪些配置问题?
作者把预检设成主分支的必过检查。新增工具、扩大权限或更换模型时,代码和清单一起提交。预检会逐条输出配置问题,所以开发者可以一次修完多项错误。

下面的脚本只依赖 PyYAML,用 pip install pyyaml 安装后,执行 python preflight.py agent-infra.yaml 检查清单。
#!/usr/bin/env python3
"""Fail the merge when an agent manifest misses an infrastructure requirement.
Usage: python preflight.py agent-infra.yaml
Needs: pip install pyyaml
"""
import sys
from urllib.parse import urlparse
import yaml
def host(url):
return urlparse(url).hostname or ""
def check(m):
errors = []
def need(ok, msg):
if not ok:
errors.append(msg)
ident = m.get("identity", {})
need(m.get("owner"), "identity: owner team is missing")
need(ident.get("principal"), "identity: no per-agent principal")
need(ident.get("shared_credentials") is False, "identity: shared credentials are not allowed")
need(0 < ident.get("credential_ttl_minutes", 0) <= 60, "identity: credential TTL must be 1-60 minutes")
tools = m.get("tools", {})
gateway = tools.get("gateway", "")
need(gateway.startswith("https://"), "tools: gateway must be an https URL")
need(tools.get("token_audience") == gateway, "tools: token audience must equal the gateway URL")
allowed = tools.get("allowed", [])
need(allowed, "tools: allow-list is empty")
for t in allowed:
scopes = t.get("scopes", [])
need(scopes and "*" not in scopes, f"tools: {t.get('name')} needs explicit, non-wildcard scopes")
models = m.get("models", {})
need(models.get("gateway"), "models: no model gateway")
need(models.get("fallback") and models.get("fallback") != models.get("primary"),
"models: fallback must exist and differ from primary")
allow = m.get("network", {}).get("egress_allowlist", [])
need(allow and "*" not in allow, "network: egress allow-list missing or wildcard")
for url in (gateway, models.get("gateway", ""), m.get("observability", {}).get("trace_endpoint", "")):
need(host(url) in allow, f"network: {host(url) or 'endpoint'} is not on the egress allow-list")
state = m.get("state", {})
need(state.get("store"), "state: no durable store")
need(state.get("checkpoint_every_step") is True, "state: checkpoint every step")
budget = m.get("budget", {})
need(budget.get("max_usd_per_run", 0) > 0, "budget: per-run cap missing")
need(budget.get("on_exceed") in ("pause", "stop"), "budget: on_exceed must be pause or stop")
need(m.get("observability", {}).get("trace_endpoint"), "observability: no trace endpoint")
need(m.get("rollback", {}).get("previous_release"), "rollback: previous release not pinned")
return errors
if __name__ == "__main__":
with open(sys.argv[1]) as f:
problems = check(yaml.safe_load(f))
for p in problems:
print("FAIL", p)
print("preflight:", "blocked" if problems else "passed")
sys.exit(1 if problems else 0)
最后一行把检查结果交给 CI。有问题时退出码为 1,检查通过时为 0。作者展示了正常清单输出 preflight: passed,而 python preflight.py bad.yaml 一次报出五项问题,包括共享凭据、令牌受众不匹配、备用模型无效、模型网关主机漏配和超支动作无效,最后输出 preflight: blocked。这两组输出演示的是脚本对样例配置的检查结果。
令牌、模型故障和超支要在运行时怎么处理?
工具调用统一经过网关以后,网关还得确认收到的令牌是发给自己的。令牌受众(audience)标识它适用于哪个服务。按原文引用的 2026-07-28 版 MCP 授权规范,客户端要在授权请求和令牌请求中都发送 RFC 8707 定义的 resource 参数,服务端要拒绝发给其他服务的令牌。清单用 token_audience 等于网关 URL 表达这个约束,实际的令牌校验由服务端完成。

模型网关解决的是切换成本。Agent 始终调用同一个入口,由网关决定请求发给 model-a 还是 model-b。作者引用了 VentureBeat 在 2026 年 10 月做的 VB Pulse 调查,共有 100 名符合条件的受访者。在曾经为生产应用换过模型的受访者中,69% 表示花了数周工程时间。原文没给出这个子组的人数,也没提供接入网关前后的耗时对照。备用模型能否接手,仍要用实际任务验证,比如同一份工具调用参数能否正确生成和解析。
预算则要接进执行循环。作者建议每次模型调用后累计费用,单次超过 $2.50 就暂停并保存检查点(checkpoint),由人工决定加预算还是结束任务。这个示例按调用结束后的账单判断,单次调用可能把总额推过阈值。每次运行再按 OpenTelemetry GenAI 约定导出一条带费用的 trace,排查时就能把开支和调用链对应起来。
我的判断
这篇最实用的部分是让基础设施变更进入代码审查。只靠 Wiki 或人工上线清单,业务代码加了一个写工具,审查者还得去别处查授权。把清单跟代码放在一个 PR 里,同一次审查就能看见调用行为和权限怎么一起变。至于所有团队是否都需要同样的七项规则,以及网关能省多少迁移时间,原文没有对照数据,属于作者的经验判断。
样例脚本适合当起点。它限制单次预算必须大于零,却没读取清单里的 max_usd_per_day: 150。它检查回滚版本字段是否有值,却没验证 invoice-triage@1.4.2 对应的发布物是否可用。脚本通过以后,部署系统还得拿同一份清单配置网关、出口和执行器,否则仓库里的约束很容易跟生产脱节。
逐步保存状态也有一个容易漏掉的前提。假设 erp.update_status 已经成功,进程却在保存检查点前退出,恢复时就可能再执行一次。对只读分流 Agent,重试多查一次通常还能接受。对会写数据的 Agent,要给工具调用加幂等键,或者在恢复前核对操作是否已经生效。
能拿来做什么
- 给现有的 Node Agent 先挑一个以读取和分类为主的流程,把负责团队、允许调用的工具和权限写进同仓库的
agent-infra.yaml,再把预检设为主分支的必过检查。 - 给备用模型跑一遍主模型使用的任务样例,验证工具调用和结构化输出能被现有代码处理,再模拟主模型故障,确认请求会按配置切过去。
- 在执行循环里同时接入费用累计和检查点保存,模拟一次达到预算后的暂停与恢复,检查已完成的写操作有没有被重复执行。