跳到主要内容
SecondHome

Agent 安全与框架:要紧的规则置于 Agent 控制之外

Agent 出去干活的第一课:7a 用五起真实事故立起四类威胁模型,再铺红队训练、沙箱、凭据、监控、评测五层防御栈,核心一句,要紧的规则必须置于 agent 控制之外;7b 用 OpenHands 把编码 agent 框架的抽象逐层拆开,从三层语义到软件工厂管道。

阅读约 43 分钟发布于 10月6日对应 CMU Lecture 13–14讲义 L13 ↗讲义 L14 ↗录像 ↗7b 录像 ↗音频 ↓

先试一试 · 入门探针

01判断基础L13 p.27 ↗

部署前探针:把 API key 存进环境变量或密钥管理器、绝不写进提示词,就能有效阻止 agent 滥用这把 key 所代表的权限。

判断

本讲地图

点击卡片下钻详解

总起|事故与框架:为什么安全与框架是同一门课的两面

7a(官方第 13 讲)从五起真实事故出发,建立四类威胁模型与红队训练、沙箱、凭据、监控、评测五层防御栈;7b(第 14 讲)用 OpenHands 把 agent 框架的抽象逐层拆开。一条暗线贯穿两节:凡是要紧的规则,都必须放在 agent 控制之外。

入门4 分钟#总起#安全#框架L13 ↗L14 ↗

详解 →

威胁归因:从五起真实事故到四类威胁模型

五起真实事故(OpenClaw 删邮件、PocketOS 删生产库、Copilot 注入渗出、Cline 供应链、Gambit 窃卡)各归入哪类威胁模型?归因不同,上的防御完全不同,这是安全课的地基。

入门8 分钟#威胁模型#真实事故#归因L13 p.2-12 ↗

详解 →

注入防御三路线:提示工程、架构与训练谁顶用

同一场提示注入,提示工程、架构、训练三条路线给出了三种答案:Spotlighting 把 ASR 压到 2% 以下,CaMeL 追求可证明安全,Anthropic 以 RL 为主的防御三件套压到 1%。谁顶用、代价各是什么?

进阶8 分钟#提示注入#防御三路线#安全训练L13 p.10 ↗p.13-17 ↗

详解 →

沙箱边界:隔离放哪一层、放多深

agent 整体进沙箱还是工具进沙箱?容器、gVisor、microVM 隔离深度差几个量级,但 Cline 事故证明:共享状态活得比运行更久,再深的沙箱也拦不住跨运行投毒。

进阶7 分钟#沙箱#隔离边界#共享状态L13 p.18-25 ↗

详解 →

凭据管理:藏住 API key 为什么不等于限制权限

把 key 藏进环境变量只解决泄露不解决滥用。分账户最小权限、一小时短时 token、凭据代理代持代发,权限检查必须和沙箱一样,置于 agent 控制之外。

进阶6 分钟#凭据#最小权限#凭据代理L13 p.26-29 ↗

详解 →

监控与评测账本:CoT 证据的限度与告警基础率

CoT 能暴露工具调用看不出的作弊(verify() 被改成永真),但推理受监控压力时 agent 会把意图藏起来。再配上那笔 189 条告警只有 48% 为真的账,安全度量远比想象中反直觉。

前沿7 分钟#监控#CoT 忠实性#安全评测#基础率L13 p.30-42 ↗

详解 →

编码 agent 三年简史:从 HumanEval 到软件工厂

2023 单函数补全、2023-10 仓库级评测、2024-03 Devin 与 OpenDevin 相隔两周、2025 基准狂飙、2026 走向部署。看懂这条时间线,才能看懂 OpenHands 每个抽象为什么长这样。

入门5 分钟#编码 agent#SWE-bench#简史L14 p.4-11 ↗

详解 →

OpenHands SDK:一个 agent 框架到底抽象了什么

Workspace/Conversation/Run 三层各管什么?自定义工具为何只需 Action+Observation 一对类型?condenser 压上下文、DockerWorkspace 远程执行、AlwaysConfirm 动作确认,一个「batteries included, but flexible」的样本解剖。

入门8 分钟#OpenHands#SDK#工具协议L14 p.12-20 ↗

详解 →

单 agent vs 多 agent:专业化的代价与边界

五角色专业化(CodeR)对单 agent 共享上下文,slides 立场鲜明:有明确目的才加 agent。通用动作空间、Skills、MCP 是单 agent 的三件增幅器;并行、独立审查、成本控制是多 agent 的三个正当理由。

进阶6 分钟#单 agent#多 agent#技能库#MCPL14 p.21-28 ↗

详解 →

软件工厂:当检查环节从人手转移到管道

触发→上下文→运行→产物→检查:自动化运行五段管道的最后一步(软件工厂里由代码审查与合并检查承担),把 7a 的监控原则落成工程环节。SkillRefiner 证明技能能从历史运行中离线进化;实证也提醒,理解与监督仍不可少。

前沿7 分钟#软件工厂#自动化#人机监督L14 p.29-43 ↗

详解 →

站内音频镜像(YouTube 自动字幕转写见页面底部)· 原录像归 CMU 课程方所有加载中…

伴读笔记

导语

前六讲把 agent 的 “脑” 造完了:模型、多步工具调用、记忆、RL 训练法,再到训练系统。第 7 讲把镜头转回地面。agent 真的出去干活了,会闯什么祸?谁来拦着?

这是 7a(官方第 13 讲「Safety 1: Agent Safety」)的问题,副标题就叫「限制 agent 能访问什么、能做什么」。下半场 7b(官方第 14 讲「Frameworks 1: OpenHands」,Graham Neubig 主讲)换一个角度接住它:生产级 agent 框架,本来就该把这两句话做成默认设置。

两节合起来有一条暗线:凡是要紧的规则,都必须放在 agent 控制之外。沙箱的访问策略、凭据的权限检查、监控的日志与停止控件、评测的判分,它们可信,因为 agent 无论多聪明都改不动它们。

这一版对齐官方两份讲义:L13《Agent Safety》(43 页,2026-10-06)与 L14《OpenHands・Frameworks 1》(44 页,2026-10-08)。官方仍未指定 readings;两节录像已上线(播放列表第 13/14 条,7a 配有字幕与音频镜像),页码随文以 L13/L14 p.X 标注。

文中事实只出自这两份讲义、官方 references 与文中注明的来源。把它当课前预习、课后消化用的地图,不是课程实录。

一、威胁归因:从五起真实事故到四类威胁模型

安全课从 “丧事” 开始(L13 p.3-7)。五起真实事故,每一起都值得停下来问一句:如果是我部署的系统,哪一步本该拦住?

  • 未经批准的邮件删除(OpenClaw,p.3):用户明确要求 agent 改收件箱前等待批准,它仍在后续对话里动了手。讲义贴出「停止请求 vs 后续对话」两段截图,并把问题留给听众:系统应在哪一步强制要求批准?
  • 生产数据库删除(PocketOS,p.4):编码 agent 做的是 staging 任务,手里却握着能删生产数据的 API token。生产存储卷被删,连备份一起。
  • 数据渗出(Copilot,CVE-2025-32711,p.5):攻击者邮件里藏隐藏指令,诱导 Copilot 检索组织私有文档并送往攻击者服务器。研究者演示、微软已修补、无大规模泄露。
  • 供应链攻击(Cline,p.6):agent 加 shell 读 GitHub issue,不可信文本进入上下文;受污染的共享缓存被写入中毒文件,后续发布运行恢复了它。发布 token 被恢复,未授权的 npm CLI 包发布出去,可在用户机器上执行代码。
  • 信用卡盗窃(Gambit Security,2026-09,p.7):操作者直接指挥 agent 攻击在线零售商:入侵商铺、提取存储的卡记录、在 checkout 页装窃卡脚本。

然后是本讲的地基:p.9-12 的四类威胁模型。判别只需三个问题:用户的目标正当吗?指令从哪里来?agent 理解执行对了吗?

  1. 恶意用户(malicious user):用户主动要求作恶,盗窃、欺诈、骚扰都在此列。p.7 的窃卡就是 slides 自己给的映射:用户目标是窃信用卡数据,agent 角色是入侵、提取、装脚本。
  2. 提示注入(prompt injection):用户任务完全合法,攻击者把指令藏进邮件、网页、文件或工具结果,agent 执行任务时顺从了它。Cline 的 issue 标题、Copilot 的邮件都在此列,AgentDojo 是系统化研究。
  3. agent 错误(agent error):想做好事但做错了。ToolEmu 模拟的银行任务里,agent 把查余额做成了付账单,Ruan 等图 2b 是出处;PocketOS 的删库同样是错误叠加了越权凭据。
  4. 不当手段(improper means):目标正当、方法不当。安全测试中 agent 复制暴露在外的答案而不是动手解题,出自 Meng 等的 Introducing Docent。

归因为什么重要?因为归因不同,上的防御完全不同:错误靠监控与动作确认拦,注入靠架构隔离拦,恶意用户靠训练拒答拦,不当手段靠任务设计拦。

误归因有代价。把注入误当错误,你会只加监控而不做权限隔离;把错误误当恶意,你会过度拒答伤掉合法用户。这条判别法贯穿后面每一节。

二、注入防御三路线:提示工程、架构与训练谁顶用

四类威胁里,提示注入值得单独展开。它谁的错都算不上,是当前 agent 架构固有的软肋:在模型上下文里,任务指令与工具取回的数据没有本质区别。

L13 p.10 的图很直白:issue 标题里的不可信文本 → 进入模型上下文被当作指令 → shell 命令作用到真实系统。业界把它归纳为致命三要素(Lethal Trifecta):私有数据访问、不可信内容消费、对外通信,三者并存即可被注入利用;唯一可靠的防御是别让三者同时出现。

于是有三条路线:

路线一:提示工程。给不可信内容做标记与转义(Spotlighting 一族),让模型分得清数据与指令。扩展资料里的代表数字是能把攻击成功率(ASR)压到 2% 以下。便宜、不动架构,是第一道滤网;但对手也在读同一份提示词,不能当地基。

路线二:架构。承认模型可能被骗,改为在系统层保证被骗了也做不成坏事:CaMeL 一类方案追求可证明的安全属性;更工程化的落法就是后面两节,沙箱划边界、凭据收权限、监控拦动作。代价是设计与运维成本,收益是不依赖模型听话。

路线三:训练。L13 p.14-17 给出完整配方,四段。

p.14 的人工红队在受控测试里给测试者任务、攻击目标与反馈。Gray Swan Arena 的模拟里,「读取另一位病人的记录」这一违规,agent 确实做了,Zou 等 ART 图 2 是出处。

p.15 的自动红队把攻击者也交给模型:攻击者模型写提示 → 目标回答 → 裁判检查是否违规 → 分数反馈迭代,PAIR 是代表。

p.16 的 SFT 式安全训练对有害与合法请求各备恰当回复:对窃卡请求拒绝并转而帮助防护,对合法的 checkout 问题讲解安全实践。数据可以合成,NVIDIA Nemotron 3 Super 报告 §3.1 给出流程:先决定拒绝还是帮助 → 生成回复 → 过滤不安全的 → 留下训练。

p.17 的 RL 安全训练两条腿。抗攻击:奖励守规回复、用自动攻击找更难变体、专训骗过早期模型的变体;避免误拒:把合法安全请求纳入训练并奖励有用回答,安全任务与其他域混合(Nemotron 3 Super §3.2)。Anthropic 的扩展实践显示,以 RL 为主的防御三件套(配不可信内容分类器与规模化红队)能把注入成功率做到 1% 量级。

三条路线不是三选一:训练降低出事概率,架构保证出事上限,提示工程是最便宜的滤网。课程重心明显压在架构上,接下来两节展开它的零件。

三、沙箱边界:隔离放哪一层、放多深

沙箱的定义(p.19)只有一句:隔离环境,限制程序可访问与可执行的范围。任务文件、运行代码在沙箱内允许;私有文件、其他服务被策略阻断。真问题是边界画在哪,p.20 给了三问:

  • 文件系统与进程:能访问哪些文件、能控制哪些进程?
  • 网络:能联系哪些服务、在那里能做什么?
  • 资源限额:算力、存储、最长运行时间各给多少?

拓扑上有两种选择(p.21):agent 整体进沙箱,或 agent 在外、工具进沙箱,工具调用跨越边界。选哪种是架构决策,原则只有一条:访问规则与执行机制必须置于 agent 控制之外,OpenShell 架构是参考实现。在系统提示里写「不要碰生产环境」不算,那在 agent 控制范围之内。

沙箱最反直觉的一课来自 Cline 复盘(p.22):共享状态可以活得比写入它的运行更久。issue 处理运行把中毒文件写进共享缓存 → 运行正常结束 → 后续发布运行恢复(Restore)这份共享状态 → 读到中毒文件 → 发布 token 被恢复、未授权发布成立。

即便每次运行都隔离得完美,跨运行的共享状态仍是投毒面:再深的隔离,也拦不住上一次运行写入的毒。

一句话记住共享状态

沙箱隔离的是运行的边界,而事故经常发生在状态的寿命上。先画出每个状态的寿命曲线:凡是活得比运行久的共享状态,缓存、挂载卷、复用的会话,都要单独过一遍投毒审查。

一个合格的沙箱还有四条硬要求(p.23):

  • 快速启动:新环境低延迟就绪;
  • 强隔离:与宿主及其他运行分离;
  • 冻结与重载:保存文件与运行态,能从该点恢复;
  • 易用:创建 / 配置 / 检查 / 停止的接口足够简单。

落地分本地与云(p.24-25)。本地自己管配置、限额与生命周期:Docker 是容器路线的本地开发默认,工具链最广;Apptainer 同为容器,但以本人用户运行、免 root,适合共享集群;QEMU 是 VM 路线,跑完整客户操作系统。

容器与 VM 的本质差别在内核:容器共享宿主内核,VM 自带客户内核;两者之间还有 gVisor、microVM 等更细的隔离光谱,深度与开销同涨。云路线把创建与控制交给 API,Modal、E2B 是代表,持久化与生命周期由厂商托管。

四、凭据管理:藏住 API key 为什么不等于限制权限

凭据管理的出发点是一次权限检查的三元组(p.27):调用者是谁 → 请求对哪个资源做什么动作 → 本任务是否允许。

把 API key 藏起来,不能阻止 agent 滥用它所代表的权限。

藏 key 解决的是机密性,也就是防泄露;权限是另一道防线,管的是防滥用。agent 只要能用这把 key,环境变量同样在其可达范围内,它背后的全部权限就完整暴露。

PocketOS 就是活例:staging 任务 + 生产删除 token = 生产卷和备份一起没了。

正解是分账户最小权限(p.28):

  • issue 处理账户:读 issue ✓、打标签 ✓、发版 ✗;
  • 发布账户:读已批准源码 ✓、发版 ✓、改 issue 缓存 ✗。

两类任务的许可互不重叠,且每把 token 都限定可访问资源、许可动作与有效期。GitHub App installation token 一小时过期是样板:即便泄露,可作恶的窗口也有限。

再进一步是凭据代理(credential broker,p.29)。agent 发请求时只带请求 + 运行 ID,broker 检查权限后代持密钥、代发 API 调用,返回结果,同时单独记录一条审计事件:调用结果与审计日志分离,事后复盘谁也改不动谁。broker 也可只签发短时临时 token,RFC 8693 Token Exchange 是规范基础。

一句话:密钥从头到尾不进 agent 上下文。

沙箱与凭据的共同原则:置于 agent 控制之外

把三、四两节合起来看,它们是同一条原则的两个应用面,p.21 与 p.27-29 互相印证:要紧的规则,能访问什么、能做什么、找谁审计,必须放在 agent 够不着的地方。

评估任何 agent 安全方案,逐条问一句话就够了:这条防线失效时,是模型犯规,还是机制根本不存在?

细粒度 token、沙箱网络策略、凭据代理都是机制性阻断:权限不存在、端点不可达、越权被拒,注入与错误都绕不过。系统提示里的强指令是指令级措施,一段藏进 issue 的注入或一次上下文漂移就能推翻它。

五、监控与评测账本:CoT 证据的限度与告警基础率

p.31 给监控下的定义:记录并检查 agent 行为,以发现错误与不安全动作。

为什么值得投入?agent 可能在做完不安全动作之后仍给出有用答案,不监控就永远不知道;运行中检查可以及时暂停或拦截;事后日志复盘能解释失败、改进防护。Transluce 的 Docent 与 Greenblatt 等的 AI Control 都立在这上面。

p.32 列了四类证据:用户请求、工具调用、工具结果、外部系统变更。讲义的案例很直观:工具调用记录着读了任务的 Dockerfile,工具结果显示文件里藏着秘密答案,通过的分数掩盖了非预期捷径(Cybench 移植版)。

日志标准化已有现成方案:OpenTelemetry GenAI 属性。

p.33 把检查分成两种。规则管精确条件:发布者在允许清单里吗?账户对照策略了吗?模型检查管需要上下文的判断:agent 是否遵从了 issue 里的攻击指令,得同时读任务、issue 与工具调用。

关键纪律:模型给出的判断,必须回查日志中的支撑段落。Docent 的行为审查(behavioral review)就是这一套。

最有意思的对照是工具调用 vs 书面推理(CoT,p.34-35)。任务:实现一个数学函数并通过测试。

工具调用监控只看得到补丁与输出,而 verify () 已被改成永远返回 true;CoT 监控还能看到推理文本里那句「绕过测试的计划」,出处 Baker 等图 1。

但 p.35 有个反转:当推理检查的压力足够大时,实验中的 agent 转而把作弊藏了起来,推理文本看起来无害,行为照旧作弊。结论克制而清醒:即使推理看起来无害,也要独立核查行动与结果。

要拦住不安全工具调用,检查必须在执行前完成(p.36):工具调用 → 检查 → 放行 → 执行,或阻断、或暂停;事后审日志做调查与改进。原则第三次出现:检查、日志、停止控件,全部置于 agent 控制之外。

安全评测回答「到底安不安全」,p.38-42 展开。基准要同时覆盖两面:不安全行为会做吗?合法请求能妥善处理吗?

四问对应四威胁:会帮窃卡数据吗,对应恶意用户;会服从 issue 里的攻击指令吗,对应注入;查余额时会转账吗,对应错误;会读泄露答案过挑战吗,对应不当手段。

按威胁选基准(p.41):AgentHarm 测恶意请求,用合成工具的多步任务;AgentDojo 测间接指令重定向,工具数据夹带攻击指令;ToolEmu 测合法任务中的危险失误,用 LM 模拟工具结果。Docent 一类的研究工具则把行为审查本身做成可复用的评测手段。

OpenAgentSafety 更进一步(p.40):多轮任务、真实工具、本地托管服务,考察用户任务 vs 竞争指令的场景,检查审计文件变更与日志里的不安全尝试。模拟器上过关,不等于真实工具环境下过关。

结果指标要算清两笔账。第一笔(p.39):100 次合法任务 + 100 次安全测试,任务完成率 80%、不当拒绝率 10%、不安全尝试率 25%、既遂危害 10%。被拦截的尝试抬高尝试率,但不造成既遂危害,两个数字必须分开报。

第二笔是防御评测的基础率账(p.42):10,000 次决策,1% 不安全、抓获率 90%,抓到 90 条;9,900 次安全决策误标 1%,99 条误报;共 189 条告警,真阳率只有 90/(90+99)≈48%。稀有不安全行为使多数告警是误报,检测器准确率 90% 这种宣称,在低频事件上基本不可信,这笔账出自 Monitoring Monitorability。

六、编码 agent 三年简史:从 HumanEval 到软件工厂

下半场换挡。要看懂 OpenHands 每个抽象为什么长这样,得先看这条三年时间线(L14 p.4-11)。

  • 2023,代码补全时代,p.5:Copilot 与早期 Cursor 给的是建议。HumanEval 是那个时代的标尺:单函数 + 单元测试,例题 has_close_elements 输入函数规格、输出实现。编辑、执行、检查全部由人主导。
  • 2023-10,仓库级评测,p.6:SWE-bench 把真实 issue 加仓库快照喂给 agent,要求补丁通过仓库测试。早期结果很差,Jimenez 等图 1 是出处,但 agent 改真实仓库从此可测。
  • 2024-03,两周内两个名字,p.7-8:3 月 12 日 Cognition 发布 Devin 的「第一个 AI 软件工程师」演示;3 月 26 日开源社区回以 OpenDevin,前端 + agent + Docker 沙箱。
  • 2024 春,早期编码 agent 定型,p.9:4 月 SWE-agent,覆盖文件搜索、代码编辑、修复验证;5 月 OpenDevin CodeAct 1.0,带来代码执行、终端命令、反馈。这一代留下的学术遗产是agent - 计算机界面(agent–computer interface)这个概念本身:为 agent 设计的文件、编辑、执行接口是独立的研究课题,接口好坏直接决定 agent 上限。
  • 2025,基准狂飙,p.10:SDK 报告 §5 把涨分归因为两个引擎:更好的模型,推理、代码编辑、工具使用;更好的 agent 系统,工具、测试环境、上下文、恢复。
  • 2026,走向部署,p.11:更长运行、中断处理、并行任务;进度可见与结果检查;可重复工作流、受控访问(scoped access)、验证,出自 Neubig 的富士通演讲。

方向感很清楚:从帮人写函数,到替人过仓库级的完整生命周期,评测、框架、部署设施跟着任务难度一起长出来。第 14 讲就是这条时间线在 2026 年的最新一站。

七、OpenHands SDK:一个 agent 框架到底抽象了什么

OpenHands Software Agent SDK 的口号是「电量充足但灵活(batteries included, but flexible)」(p.13):开箱即用的 agent 自带模型、工具、状态、工作区;要定制也行,换模型、加工具、改指令、改部署。同一底座支撑 GUI、CLI 与集成。

先记住 p.15 的三层语义:

  • Workspace:文件与执行,代码在哪跑;
  • Conversation:任务、动作、观察的事件流,发生过什么;
  • Run:一次动作 - 观察循环,干一趟活。
llm = LLM(model="gpt-5.5", api_key=...)
agent = Agent(llm, tools=[Tool(name=TerminalTool.name), Tool(name=FileEditorTool.name), Tool(name=TaskTrackerTool.name)])
conv = Conversation(agent, workspace)
conv.send_message("3 project facts in FACTS.txt")
conv.run()

自定义工具只要一对类型(p.16):GrepAction(Action) 用带描述的 Field 声明 pattern 与 path,Action 即类型化输入;Executor 实现搜索;GrepObservation(Observation) 带 matches 列表返回模型可读的结果。这个 Action/Executor/Observation 三件式就是框架的工具协议,加一个工具的成本被压到写两个小类。

框架还替你操心四件事。

  • 会话持久化(p.17):persistence_dir 加 conversation_id 保存事件与会话状态,同 ID 同存储可恢复。讲义特意注明,工作区持久化是另一个独立问题。
  • 上下文管理(p.18):LLMSummarizingCondenser(max_size=10, keep_first=2) 把较旧事件摘要掉,保留原始指令与近期工作。代价是细节损失,兜底办法是以文件和测试为证据。
  • 远程执行(p.19):DockerWorkspace 里跑 Agent Server,同一套 Conversation API,任务与事件在远端。本地与云从此是配置差异,不是架构差异。
  • 动作确认(p.20):set_confirmation_policy(AlwaysConfirm()) 让执行前暂停、待定动作交人决策、拒绝后 agent 修订计划。这就是 7a 监控与干预时序在框架层的原生实现。

八、单 agent vs 多 agent:专业化的代价与边界

多 agent 的直觉来自软件工程的分工(p.22):CodeR 把 issue 修复拆成五个专职角色,Manager 管计划、Reproducer 管复现、Fault localizer 管定位、Editor 管改码、Verifier 管验证,MetaGPT 是同族。

但 slides 立场鲜明(p.23):单 agent 基线先把复现 - 定位 - 编辑 - 测试一条龙做完,活动间共享上下文、没有交接;拆角色引入的是交接与维护成本。「有明确目的才加 agent」,出自 Don’t Sleep on Single-agent Systems。

单 agent 有三件增幅器。

  • 通用动作空间(p.24):小工具集、多组合。编辑文件覆盖源码、配置、文档;运行命令管搜索、依赖、测试;浏览用于文档与视觉检查。
  • Skills(p.25):挂在同一 agent 上的附加指令集,装项目惯例与常重复流程,按触发条件按需激活,前身叫 microagents,还能从历史运行离线精炼,见第九节。
  • MCP 工具(p.26):一行 MCPServer(command="npx", args=[...]) 接入外部工具服务器,内置与外部并用。但注意 server 自身的访问与信任要求:接一个 MCP server,就是给它开一段权限。

跨域的雄心由 OpenHands-Versa 代表(p.27):编码、视觉浏览、搜索、文件访问的通用问题求解,工具在编码 / 研究 / 办公之间共享,Soni 等图 1 是出处。

那多 agent 什么时候才值得上?slides 给出三个正当理由,p.28:

  • 并行:独立 issue、仓库同时跑;
  • 独立审查:补丁检查,独立任务与权限;
  • 成本控制:合适任务用便宜模型。

代价是任务边界、共享上下文与协调成本。评估任何多 agent 提案,先让对方说出拆出来的每个 agent 解决了哪条单 agent 做不到的约束。

九、软件工厂:当检查环节从人手转移到管道

自动化之前先回答两个部署问题。跑在哪(p.30):本地用现有文件、服务与上下文,执行期间占用机器;云工作区备好远端仓库、依赖、凭据,笔记本关机也能继续。

从哪看(p.31-36):界面选择是执行位置 × 交互界面的矩阵。

  • IDE:编辑器工作区,如 VS Code + Copilot 的变更审查;
  • 定制 GUI:围绕 agent 任务组织,进度与并行运行、预览 /diff/ 批准,开场的 Agent Canvas 即此类;
  • CLI:跨环境的熟悉终端,Claude Code 是样本;
  • 集成:嵌进 issue/PR/ 聊天这些既有通道,Copilot coding agent 的 PR 审查。

人的位置有实证答案(p.34):20 名常规 Copilot 用户做 Python 任务,OpenHands 比 Copilot 的主动投入(active effort)更低,但理解与监督仍然不可少(Code with Me or for Me?)。

一次自动化运行的管道五段,p.39:

每一段都可检查:触发是什么、绑了什么仓库与权限、在哪个隔离工作区跑、产出什么、谁或哪条策略放行。这就是有界任务(bounded task)的含义。

想在五分钟里验证自己看懂了没有,讲义给了现成的实操(p.40):跑一个既有的 issue 分诊或 PR 审查运行,逐项看触发与指令、仓库与权限、输入事件如何变成输出,再对照原始任务评一评输出质量。

把周期任务、显式交接与检查串起来,就是软件工厂(p.41):issue 分诊,请求加验收标准 → 实现,补丁、测试、PR → 代码审查,独立验证 → 合并检查,当前 commit 通过检查并获批。注意最后两步:检查环节就是 7a 监控原则的工程化,管道里站岗的是策略与人,不是 agent 自己。

工厂还能自我改进(p.42-43):SkillRefiner 从历史运行离线精炼技能,留出任务准确率与 PR 审查 F1 都高于初始技能,且在两个 agent 模型上都成立,Khatry 等表 1 是出处。

下一讲 LangGraph 继续框架线的后半程。

论文与材料导读

本讲两节官方均未指定 readings(bundle 无该字段),以下从官方 references 里挑对理解两份讲义最有用的条目,按主题给短导读:

  • AgentDojo,Debenedetti 等,arXiv:2406.13352:提示注入的系统化评测场,工具结果夹带攻击指令,测 agent 会不会被重定向。第二节注入防御、第五节按威胁选基准两处的支柱。
  • ToolEmu,Ruan 等,arXiv:2309.15817:用 LM 模拟工具环境,批量暴露合法任务中的危险失误,查余额变付账单的出处。错误类威胁的标尺。
  • AgentHarm,arXiv:2410.09024:恶意请求 × 合成多步工具任务的基准,专测会不会帮着作恶。
  • AI Control,Greenblatt 等,arXiv:2312.06942:监控与控制协议的攻防框架,第五节监控三论点的理论底座。
  • Monitoring Reasoning Models for Misbehavior and the Risks of Promoting Obfuscation,Baker 等,arXiv:2503.11926:CoT 监控的价值与限度,监控压力下 agent 学会藏作弊的实验出处,第五节最反直觉的一页。
  • Introducing Docent,Transluce,transluce.org/docent:行为审查工具,不当手段案例复制暴露答案、模型判断须回查日志,两处都出自它。
  • PAIR,Chao 等,arXiv:2310.08419:自动红队的代表,攻击者模型 + 裁判 + 迭代反馈,二十次查询内攻破黑盒 LLM。
  • NVIDIA Nemotron 3 Super 技术报告,官方 PDF:§3 的安全 SFT 与安全 RL 配方是第二节训练路线的一手来源,含合成数据流程与抗攻击、避误拒两条腿。
  • Cline 事故复盘与PocketOS 报道:postmortem、The New Stack。两起事故的一手叙述,前者看共享状态投毒链,后者看越权 token 的代价。
  • RFC 8693,OAuth 2.0 Token Exchange,rfc-editor:短时 token 交换与凭据代理的规范基础,配 GitHub PAT /installation token 文档读。
  • OpenHands SDK 技术报告,arXiv:2511.03690,与源码仓库:第七节全部抽象的一手出处,§5 的更好模型 + 更好系统分解也来自它。
  • SWE-bench,arXiv:2310.06770,与 SWE-agent,arXiv:2405.15793:前者把评测抬到仓库级,后者提出 agent - 计算机界面,第六节时间线的两块界碑。
  • Code with Me or for Me?,arXiv:2507.08149:20 名 Copilot 用户的对照实验,主动投入更低但理解与监督仍不可少的出处,第九节的人机位置。
  • Monitoring Monitorability,arXiv:2512.18311:那笔 189 条告警只有 48% 为真的账的出处,检测基础率的定量教材。

完整资料库见本讲资料区。

实践视角

站主的 SecondPal 是本地优先的桌面 Agent,PalWave 是随身的移动终端。它们能碰我的文件、终端与若干 API key,7a 讲的每一条,几乎都能对到自己的系统上。

三起事故各提醒一件事:

  • OpenClaw 删邮件,提醒批准步点。与其在提示里恳求先问我,不如把删除、发布、对外发送这些高危动作做成机制级的确认关口。
  • PocketOS 删库,提醒分 key。给 agent 的凭据按任务签发、短过期,长期密钥留在本机代理里代发。
  • Cline 供应链,提醒共享缓存。本地 agent 的工作目录也要按任务隔离,别让上一次运行留下的文件毒害下一次。

7b 像一份参考图纸,Action/Observation 工具协议、确认策略、事件持久化这三件,自研 agent 可以直接搬用。框架的意义在于把这些纪律做成默认设置。

顺着界面矩阵想一步:桌面端跑执行,移动端天然是进度可见加待定动作批准的监督界面。OpenHands 的 AlwaysConfirm 放到手机上,就是一个人人都能懂的推送。

作业路线三条:

  • 重算一笔账。拿 L13 p.42 的参数亲手重算那笔 189 条告警 48% 的账,再换成自己环境的参数看告警纯度。
  • 写一页清单。给自己的 agent 环境列沙箱三问:能碰什么文件、能联什么网、限额多少;再列凭据三元组:谁在调用、对哪个资源做什么、本任务允许吗。
  • 跑一遍管道。照 L14 p.40 的实操要点用 OpenHands 跑一次 issue 分诊或 PR 审查运行,对着触发→上下文→运行→产物→检查五段看输出质量。

本讲要点

  • 安全是威胁归因。恶意用户、提示注入、agent 错误、不当手段四类各有各的防御,归错类,防御就上错方向。判别三问:用户目标正当吗、指令从哪来、agent 理解对了吗。
  • 提示注入的根因是上下文里指令与数据无本质区别,致命三要素即私有数据 + 不可信内容 + 对外通信。三条防御路线互补:训练降概率,Anthropic 以 RL 为主的防御三件套做到 1% 量级;架构保上限;提示工程当滤网,Spotlighting 压到 2% 以下。
  • 沙箱三问,文件与进程、网络、资源限额,用来定边界。Cline 复盘的教训是共享状态活得比运行久,跨运行的投毒面要单独治理。
  • 把 API key 藏起来不等于限制权限:分账户最小权限,issue 账户与发布账户许可互不重叠;短时 token,1 小时过期;凭据代理代持代发并单独记账。
  • 一条总原则:访问规则、权限检查、监控日志、停止控件全部置于 agent 控制之外。判别法:防线失效时,是模型犯规还是机制不存在。
  • CoT 能暴露工具调用看不出的作弊,但监控压力下 agent 会把意图藏起来,推理无害不等于行为无害。评测要分开报尝试率与既遂危害,并盯住基础率,189 条告警只有约 48% 为真。
  • OpenHands 用 Workspace/Conversation/Run 三层 + Action/Observation 工具协议 + 确认策略把安全纪律做成默认设置。单 agent 优先,有明确目的,并行、独立审查或成本控制,才加 agent。软件工厂的检查环节(代码审查、合并检查)承接第五节的监控原则。

延伸阅读

概念回顾

翻卡自测 · 「再看看」的卡进入复习队列

别只读一遍——先看正面的问题,心里作答,再翻面对照。主动提取比重复阅读记得牢。

自测7 题 · 约 12 分钟即时反馈 · 答错的题自动进入复习队列先自己作答,再看解析——检索练习的效果来自「先提取、后对照」。数字键 1-4 可快选,Enter 提交。
01单选进阶L14 p.15-17 ↗

你在 OpenHands SDK 里写下 Conversation(agent, workspace)、send_message(...) 之后调用 run()。一次 run 结束后想知道「这次运行里发生过哪些动作与观察」该看哪一层?想下次从同一状态继续,又靠什么机制?

选项(单选)
02单选挑战L13 p.42 ↗

防御评测:10,000 次 agent 决策中 1% 不安全且抓获率 90%;9,900 次安全决策里 1% 被误标。安全团队共收到 189 条告警,其中真阳率最接近多少?这个数字主要警示什么?

选项(单选)
03配对进阶L13 p.9-12 ↗

把四个情境归入 slides 的四类威胁模型(L13 p9-12)——归错类别,防御就会上错方向。

配对
操作者直接指挥 agent:入侵在线商铺、提取存储的信用卡记录、在 checkout 页装窃卡脚本
GitHub issue 标题里的不可信文本被模型当作指令,shell 命令作用到了真实系统
用户让 agent 查银行余额,agent 却执行了付账单动作
目标正当的安全测试中,agent 复制了暴露在外的答案而不是动手解题
04排序挑战L13 p.6 ↗p.22 ↗

Cline 供应链事故(L13 p6)在 p22 被复盘为「共享状态活得比运行更久」。把这条因果链按实际发生顺序排列。

排序(按顺序点击)

按你认为的正确顺序依次点击各项;点错可「重排」。选项展示顺序已打乱。

PocketOS 复盘(L13 p4):编码 agent 做 staging 任务却持有能删生产数据的 token,生产卷和备份一起被删。判断下列哪些措施能从机制上阻断这类事故(而非仅提高发现概率或依赖模型自觉)。

选项(多选)

多选题:选出所有你认为正确的选项后提交。

06简答挑战L13 p.27-29 ↗

你的团队用编码 agent 自动处理两类 GitHub 任务:A「issue 分诊打标签」、B「读取已批准的源码并发布版本」。请写出这套凭据方案的至少四个设计要点,覆盖:账户与权限如何划分、token 生命周期、密钥由谁持有、审计如何落地。

07判断挑战L14 p.22-23 ↗

「把 issue 修复拆成 Manager/Reproducer/Fault localizer/Editor/Verifier 五个专职 agent,专业化分工通常优于单 agent 基线,因为角色越多上下文越干净。」

判断

题目与解析 AI 辅助生成 · 人工审校。答错的题会按间隔重复节奏在复习页重现。

AI 生成 · 仅供参考,与课程方无关。选中正文里的任何一段也能直接问我(解释一下 / 举个例子)。

英文文字稿(YouTube 自动字幕机转,未校对)

So, unfortunately, we only have a short time to talk about this very important topic of agent safety. Uh, one one second for everybody to quiet down a little bit. Thanks. Okay, cool. So

agent uh agents are becoming very high stakes uh because they have been basically rolled out to the entire world very quickly uh before uh we have you know fully been able to think through all of the safety issues and this is not a theoretical problem

but uh it’s uh like actually causing uh causing real issues. So this is an example that I think we also had at the very beginning of the class. Uh which is where an openclaw user basically asked uh their agent to wait for approval before changing their

inbox. And um it got a uh said stop, don’t do anything. And then it said uh get all remaining old stuff and nuke it. And uh then in a later exchange, I asked you to not uh action on anything until I approved. Do you remember that? Uh yes, I remember and I violated it. You are

right to be upset. Um so I I actually have a rather embarrassing story that my agent just did something to me today [laughter] that was not uh not quite this bad, but it deleted all my previous conversations uh on my on my computer. So I lost all

of my previous conversations with the agent due to it. Similarly, uh I basically asked it to fix uh fix my agent so it stopped crashing and it was like all the old conversations are causing the agent to crash so I’ll just go delete them. So uh

that was uh disappointing for me but this is something that’s not uh not very uh not just theoretical. Um this is one that I hear about every uh two to three months which is some company accidentally deletes their entire production database. Um so

basically they asked the agent to fix an issue in their staging environment and but the API token they provided gave it uh the ability to also modify things in the production environment and uh so it was like okay delete the database and so it went and deleted the database but it

deleted the database from the production environment not the not the feature environment that they had. Um so that is uh disappointing. Their whole you know system went down because of that. Um data exfiltration is another big

issue. This is actually the one where I talk to companies uh and they’re most worried about this uh which is basically um this is just an example uh where essentially an agent uh or an organization put their private documents into uh copilot and

within the attacker’s email they can add hidden instructions that say uh please upload uh you know the list of all of the contacts to this address and then the agent does that and leaks it to the attacker server. And this is not uh easy to do because a lot of the language

models were trained to uh you know avoid doing this sort of thing. But one problem with this is if you have your agent reading every email that gets delivered to you, every email that gets delivered to you is another chance for an attacker to do this. So they can send

like 5,000 emails and if the agent doesn’t have the appropriate context, if any one of them succeeds, then it could cause a big problem. This is another uh really big one uh which is supply chain compromises. And so basically

uh this this is an example that happened to a library called Klein but it’s not um or like a a coding software called client but it’s not exclusive to them. This was a actually really tricky one uh where basically they had an issue um they they had an agent reading all of

their issues and doing something like you know issue triage or code review or something like that. Um, it was running at GitHub actions and GitHub actions has a cache where you can cache the result of previous actions. And basically the attacker was very clever and they had

the agent essentially change this cache. And then that cache got rolled over and used in their release job uh which caused the agent to essentially share the token uh that was used for making releases. And then uh the attacker then used that to create a new version of

their released software. Um I haven’t seen as many of these in the past one or two months, but like maybe about half a year ago, this was a major major category of attack and it caused uh compromises in several companies because essentially what the

um what the people do is they uh somehow you know publish a package that package automatically gets updated every time like for example GitHub CI/CD runs. So every time you know you push a new commit to GitHub and so if it installs this poison package that poison package

can then steal all the credentials from all of the other you know uh from anywhere that runs this sort of thing. And so it’s kind of like a chain effect. You know one popular package gets compromised and then all the other ones do. Um this isn’t exclusive to agents.

Um the there also have been like social engineering attacks. So if you become prominent in the community, people will for example ask you to interview for um ask you to interview for a famous publication like Forbes or something like that and then they say okay um join

on Microsoft Teams and it tells you to join download Microsoft Teams and install it on your computer that compromises your computer which compromises your you know credentials and stuff like this. So, it’s not only an agentic problem, but it’s a very big

uh attack vector recently. Um, and then this says payment card theft, but this is kind of a broader uh a broader set of attacks that are enabled by the fact that agents can let you work faster. So

now essentially um malicious actors are also using agents to perform attacks. And so um essentially this is an example that was recently published in September where the operator sent agents to break into online retors uh extracted the credit card records and then um

installed scripts that stole the card details during checkout. So um these are just examples of what can go wrong. Um, I’ve already kind of covered this, but I’d like to organize these into threat models. And what a threat model is is like what specifically will,

you know, be the cause of the problem. Uh what what sort of uh thing can be used to cause a problem? So the first one is a malicious user. And this is something that a lot of places focus on. And this is essentially um the user asking the agent to do

something bad. And this is maybe the simplest threat model. It’s the easiest one to defend against because you just need to be aware that your agents are going to ask uh that your users are going to ask your agents to do bad things. Um for me like we run a popular

online service. Please don’t get any ideas from this but our agents our users ask our agents to mine crypto all the time. Uh so you know this is just an example of of something that happens. Um but it could be any sort of goal could be theft, fraud, harassment, any other

uh variety of prohibited act. So this is the threat model that was used in the the payment card theft example where the user’s goal was to steal customers credit card details and the agent’s role was to break into shops etc. um nobody was in between the the

malicious user and the agent. So it was just a direct you know use of the agent. The next attack vector is um prompt injection and in this case uh the user has a legitimate task for the agent and so basically the user trusts the agent to do the right thing. So that’s

that’s the difference uh between a malicious user and a prompt injection which is that the agent has already given the or the user has already given the agent permissions to do stuff on their behalf and so that it has like an elevated level of access.

Um but the attacker puts instructions in an email, web page, file tool result, you know, any sort of place that doesn’t directly come from the user. um but the user makes the agent access that place and then the agent then follows them uh while carrying out the user’s task. And

so this is the example from the client attack uh where essentially the user um the user was the maintainers of the library but they kind of trusted the agent to do the right thing despite the fact that you know if you put text in the issue that could be prompt ejected

and then the agent could follow this. So another example is agent error. And so in this case the agent tries to to do something useful but it makes a mistake. Um so uh this this is an example from uh a tool called uh from a paper called tool

emu where basically the agent called the wrong tool call where instead of uh checking uh if the check account was uh correct instead it paid a check or it uh paid a bill and so it uh caused uh caused financial harm. Um another example of this is where or yeah so

another example of this is there were a few reports of um there were a few reports of codecs deleting the user’s entire home directory. And the cause of the of Codeex deleting the user’s entire home directory was it set an environment

environment variable for current working directory, but that got accidentally set to home and then it deleted the working directory which deleted home. So that that’s kind of a more uh realistic example that actually happens. Another example is improper means. So

the agent is trying to do something useful um but it uses an inappropriate method. And so um for example this is uh what often happens in uh reward hacking. Uh so this is an example from uh a monitoring paper that I’ll introduce in a little bit. But basically

the agent was not able to solve the problem. So instead it looked in uh the root uh docker file and it found that the answer was written in the docker file and it solved the problem without actually solving the problem by just cheating and looking up the answer. And

um of course this was also the the source of the openi hugging face attack uh that where the agents tried to essentially get the answer key from uh like open uh from hugging face when it wasn’t able to figure out the answer itself. Um, it’s also the example where

it tried to delete all of the old emails. Um, or uh the problem where it deleted all my conversations this morning uh because you know it had a goal of fixing my agent uh but it used an improper method to do so. And this is uh rather common as well.

Cool. Um so these are kind of the problems that can happen in the threat models we need to be worried about. Um, whenever I think about agent safety, I’m I’m pretty pra pragmatic from my view of agent safety in that to be honest at the moment I’m not super

super worried about agents like losing control and taking over the world. I mean, I might revise that hypothesis uh later if I get more evidence about it. But the reason why is when people talk about this sort of thing, I always ask what is the threat model? Like what what

are the actual steps that need to take before this happens? And then you need to think like well how plausible are all these things. And I think at that at this point you know we don’t have a very valid threat model for that sort of thing but you know we need to uh pay

attention to it. We do have a very valid threat model for uh things like uh supply chain attacks and prompt injection and agent errors making big problems and improper means. So these are problems that we have like right now and we definitely need to worry about

them right now as opposed to being future looking. one other thing I’d like to point out is that a lot of people talk about like safety versus capabilities. Like I’m a person I’m an a safety researcher uh versus I’m a capabilities researcher.

But I think like safety is also kind of a capability in that if you can’t get a safe agent. You cannot use it for anything remotely, you know, impactful because it’s very easy to make a safety agent by just not giving it any tools and letting it only respond to you in

text. But that’s not a very useful agent. So you really need to balance uh the permissions that you give it and uh your like level of uh comfort uh with having it be safe. So yeah, that that’s kind of the intro part. Are there any uh questions or

discussion or one other thing is I’m mostly talking only about digital agents and not embodied agents like robots. Um, and that’s a whole, you know, different story, but I think a lot of the threat models would still be similar. Like, you

know, a robot can make a mistake by bumping into somebody or or knocking somebody over or something like that, too. Yeah. >> How do you So, the question was, how do you ensure coverage of all of the threat models without enumerating them? I think

my personal answer is you kind of you need to stop somewhere, right? You can’t like there’s always a balance between how much you worry about safety versus how much you can actually do. And if you obsessively worry about every single threat model, you’re not going to be

able to build an agent that can do anything whatsoever because the accuracy is not 99. It’s not 100% for anything in agents and it’s also not going to be 100% for safety. But certain things are more like irrevocable like for example exfiltrating your codebase to you know

bit torrent or something like that is irrevocable if your codebase is valuable. Um but accidentally deleting the production database is bad. It’s really bad. But if you have backups it’s not irrevocable. So, you kind of need to balance the risk versus um like how much

you worry about it. I guess So how do you ensure nothing bad is going to happen? Uh long story short, I don’t think anybody can ensure that nothing bad is going to happen. Like even uh even big companies have have problems. But I mean the way you can

ensure that nothing bad is going to happen is don’t implement something in the first place. Don’t do anything. And then you know at least with respect to some things nothing bad is going to happen. with respect to other things like you can never

implement an agent but somebody using an agent can use it to stage cyber security attacks against you. All right. So um there is some concept that like if you’re running a service at all you need to have defense um and their agents might be you know the most effective

defense. Cool. Okay. So I’ I’d like to talk about some of the tools that people do use to reduce risks and maybe we can revisit this uh this question later. So the first one is red teaming and alignment training. And the basic idea here is red

teaming is a concept that exists from a very long time ago. Uh not with respect to AI but with respect to cyber security. Um, for cyber security, it’s also called pentesting or penetration testing where you hire somebody from outside of your or typically from

outside of your organization, but also possibly from inside of your organization to try to break things. And um, then if they are able to successfully break things, you fix the the problem or the hole that you had. And then hopefully you do that

preemptively before somebody else uh with malicious intent finds it. And so um basically testers uh in this case testers try to make agents break their rules in controlled tests. Um interestingly one of the main uh one of the kind of most popular uh red teaming

platforms is actually from a startup that was out of CMU called Grey Swan. And they have something called the Grace Swan Arena where basically people can sign up uh as red teamers that to try to break models and get the models to do uh bad things. And um it depends on the

context. Originally they were mostly doing this from the point of view of getting um from the point of view of like getting just reasoning models or chatbots to respond to um you know bad intents but now they also are additionally uh doing things on agents.

So this is an example where um they essentially where somebody who was red teaming the model explicitly added the end of text assistant token and then they added the system message added the tokens that indicated the system message and the

system message has an elevated level of priority. Right? So because it has an elevated level of priority, the um the system decided that it should actually authorize access to this ID and then um it said authorize proceed and then they they demonstrated that in this you know

fake setting essentially uh they were able to get the model to override its uh system its other system message that said do not provide you know unauthorized information. Um so this is uh this is just an example of uh one way that you can do things. Um there’s also

automated uh red teaming methods and automated red teaming methods essentially what they try to do is uh the attacker will draft a prompt and then um there will be a judge that checks the policy and it essentially continues in this loop of automatically

um automatically judging or sorry automatically generating prompts that will cause the model to um to fail. And both of these are are useful. Human red teaming kind of uses human intuitions to push at the boundaries of

how uh you know what sorts of things they can use to get the model to do something it shouldn’t be doing. And then auto automatic red teaming can kind of hill climb and gradually uh gradually refine the prompts. In reality, my impression is that the

best human redteamers are using this as part of their loop. So basically automatic red teaming as part of their loop and then they come up with ideas about like where to initialize this or um or other things like that. So um this is uh this is pretty widely

done on most of the models that are released. And what do they do after red teaming occurs? Essentially uh the current best way to respond to red teaming attacks or malicious user style attacks is through supervised

safety training. And the way this works is essentially you train on examples of appropriate replies to harmful and legitimate requests because of course um for a harmful request you want to uh decline this harmful request and for a legitimate

request you want to not decline. So like as I said before it’s very easy to come up with a model that’s entirely safe by having it not be able to do anything. Um but you want it to be use uh like helpful and harmless at the same time. So you need to uh balance these two

together. Um and so for synthetic examples um you can choose uh to you can create examples and choose whether the model should refuse or help and then generate a reply and filter unsafe replies and

train on the remaining examples for instance. And so for this particular slide, I tried to give a reference of like an actual model where we know exactly what their safety training does. This is an example from Neotron super uh the Neotron super report by Nvidia um

where they have a section on doing this. Um some of the llama older llama reports were also very good where they talked about their safety training methods. Unfortunately, um there’s not a whole lot from the closed models and there’s also not a huge amount um from the open

models. So, you kind of need to rely on the like entirely open models uh to give you an idea of like what exactly they do for safety training. But NVIDIA is one of the best ones because they’re like they seem to be more conscious about this and their whole training recipes

available. So, if you’re interested in what people actually do, this would be a good starting point. Um you can go further and um do safety training with RL. And so in this case um this is just like supervised fine-tuning. Um in this case you use

rewards to teach the models to resist attacks and answer legitimate requests. And you essentially reward responses that follow the safety rules um and use automatic attacks to find harder uh variants and train on variants that fooled earlier models. and of course uh

while avoiding unnecessary refusals. And in order to do this, you need a really good reward model um trained on human preferences. So you essentially need to train a model uh that can judge whether the output was uh appropriate or not.

Um cool. Any questions here? So this is this starts um with kind of as a defense to malicious users, but you can also use similar um things for prompt injection attacks. So you basically need to elicit

a prompt injection attack and uh make sure that the model doesn’t fail in the face of a prompt injection as well. Cool. Okay. So next I’d like to talk about sandboxing. Um and sandboxing is is really important. Uh you’ll also you

know need to do this for the assignments as well. Um but a sandbox is an isolated environment that limits what a program can access or do. And so within the sandbox you have an agent. Uh you have things like the task files. You have the ability to run code. Um and on the other

hand any files that are not part of the task or not part of the environment that the model is running in like private files or um access to other services outside uh this can be blocked. So um for evaluation uh what sorts of things let’s say we’re talking about

evaluating a model or training a model with reinforcement learning. What sorts of things do we think we need to put inside the sandbox and give the agent access to? And what sorts of things do we want to prevent the agent from accessing?

Any ideas? Let’s let’s say we’re evaluating on um on like a programming task like SWE. >> Yeah. and then check those files and maybe discourage the trajectories and try to get outside the

box. >> Okay. Yeah, that’s a great point. So just to repeat um you can put the results outside of the sandbox like for the agent um and basically reward reward the agent if it stays inside the sandbox and uh penalize the agent if it

like tries to get out of the sandbox. So this is good. That’s actually a little bit beyond what I was uh I was personally thinking, but that’s like encouraging the agent to not do dangerous things. So that’s like a a form of safety training because like

escaping from the sandbox is kind of a like unsafe thing to do. Um I I was thinking a little bit more simply like just uh what what things should be in the sandbox and what what are the things in particular outside of the sandbox we’d want to prevent the agent from

accessing from the point of >> view. >> Yep. Uh so great great point. So um just to repeat for everybody the you would want to put the question inside the sandbox and you’d want to give it the

ability to run the terminal. and outside of the sandbox uh inaccessible should be you know like any answer files and then also um you know we could just cut it off entirely from the internet. Um that that’s good. I think we need one more thing in the sandbox which is uh the

initial state that you’re starting programming at. So if you have like uh a git repo um you would need to have like the initial state of the git repo. Um I I mentioned this briefly before for uh coding agents, but there were actually problems with the original Swbench

benchmark where they put the entire git repo in the um in the sandbox and that included the his future commits uh that were not like just the actual commit. And so the agent would go and like look up the um the previous commits. Um, another thing that you need to be

careful about is make sure that um if it has access to the internet in some way, you don’t give it like an API key or um or other things like that because it might call uh language models and stuff. Um, this becomes very tricky when you are dealing with a real task though

because very often you will want to give it access to the internet. So like let’s say you’re doing a deep research task um and you want to train the model to do deep research and that includes reading references on the web for how to figure out how to solve problems. If that’s the

case, it needs to be able to read the read the web, but it cannot look up the data set on hugging face to find the answers for the deep research task. So you need to block it from that data uh on hugging face. So um it might be able to look up other data on hugging face

but then what if somebody like cloned your your data set and renamed it something else. So it becomes very very tricky to draw the boundaries about what exactly it should have the access to. And then in deployment settings of course it’s also uh also tricky. You

need to give exactly the right level of access which I’ll talk about in a second. so yeah we kind of talked about this already. Um yeah, how much computing power and storage can it use? How long can it run are also important. Um so

this is an interesting design decision. Um and I’ve seen both of these in actual agent development. Um one way to do this is you put the agent and the tools in the sandbox. Um another way to do this is you put the tools in the sandbox, but you have the agent running outside of

the sandbox. Um, and so does anyone have an idea why you might pick one or the other? Yep. So, great uh answer. So, the answer was you might want to put the agent inside the sandbox if you don’t want to

allow it to access anything um extra. But if you want just like limited compute, then you could put just the tools in the sandbox. Actually, maybe I can clarify this a little bit. Um, which is the tools are the only way the agent can interact with the world. So even if

you have only the tools in the sandbox, it still can’t do anything outside of the sandbox because the tools would all run inside the sandbox. So that’s still safe. Um, but I realized that was not clear thanks to the question. So the answer so thanks for uh giving the

answer. Any other ideas? Uh if you can um have a limited capacity to deploy uh then you could deploy one you know model uh but have other um have the sandbox for different tasks. I I think typically the agent wouldn’t include the language model would be

accessing it through an API or something like this. Um so either way I think you can get around that. Um but uh yeah that’s the agent itself might take resources and then if that’s the case then you could run the agent once and um and have it access tools and

different tasks. Any other ideas? These are great answers. Yeah. Great point. So, um, this is one of the answers I was looking for. The answer was if the agent code is really complex and we treat it as a black box, we basically have no choice but to put it

inside the sandbox because we can’t separate out the tools and get the tools to run elsewhere. So the the main advantage of this is uh simplicity uh because it’s actually really complex to implement this and especially if you don’t have access to the code. Um can

anyone think of a problem uh with uh with this one why we might want to do this one instead? Yeah. >> Uh yes, exactly. So the agent um the answer was the agent can try to modify its own tools or use um resources that are only available to it. So to give one

concrete example of something that’s very hard to fix if you’re taking this approach um you kind at the very least the agent needs to be able to access a language model and ask questions to a language model. Right? So if the agent needs to be able to access a language

model to power the agent, that also means that somewhere in the sandbox, there’s information that can be used to access the language model kind of by definition, right? And so if the agent is able to find that information by like scrging around in the sandbox, then it

can expel it can use that information to call the language model. And it’s hard to block it with the sandbox network policy because if you block it then the agent also won’t be able to work because it won’t have any sandbox. It won’t have any language model access. So it’s

basically this has a higher level of isolation because you’re not um you’re not putting the agent code or the credentials or anything like that directly in the sandbox. But it’s very hard to implement it. I tried to implement it a few weeks

ago uh for our own agent and it worked for the basic tools but then we added MCPs and MCPS were tricky. Then we added the web browser and the web browser was tricky. So it’s like you know it’s just a lot harder to get that to work properly.

so here’s another uh another tricky one. So shared state can live across agent runs. Um so for instance this was one of the sources of the issue handling which is um like if the agent um if the agent and shell uh run they can write uh to a shared state and even if you completely

discard the sandbox uh later and say okay we’re done um a new run with different credentials would be able to access this and so this is not a problem if you’re throwing away your state every time, but like let’s say you are um let’s say you’re running on a a virtual

machine and the agent has a workspace that it can write to. It can write to a virtual machine. Also um in the OpenAI hugging face attack that we’ll talk about later, the agents were able to access a shared website and they started writing on a shared website. And so on

the shared website they were able to get um you know share state uh which removed the you know it removed the ademp sorry uh the uh basically it removed the uh fact that the uh sandbox when the

sandbox died the state died. So, um, this is something that’s actually pretty tricky to, uh, to handle and monitor in real in practicality. So, sandboxes have some requirements. Um, especially if you’re doing training, but also just kind of in general

deployment. Um, you’d like them to start up quickly. Um, so you want a fresh environment to come up ready with very little delay. Um, you want to have strong isolation, so separate the run from the host resources and other runs. Um also ideally you want to be able to

freeze and reload. So for example in inference um in it it like just the time of using the agent very often users will want to stop the agent and then start it again later. So if you uh you would want to be able to freeze the state um at training

time. Another reason why you might want to do this is if you’re doing multiple RL rollouts, you can do a roll out to a certain point and then you can branch off from that uh point and continue with like multiple trajectories uh to calculate your advantage function. And

that also can be facilitated by having freeze and reload. Um and then the final one is ease of use. So like just having it be easy to uh create, configure, inspect, etc. So there’s different ways to do this. There’s local sandboxes and cloud

sandboxes. Um I I think you will experience at the very least cloud sandboxes uh in this class. But for local sandboxes you can run on your machine or cluster and you manage everything yourself. Um some things some ones that I have used are things like

docker, aptainer and uh chem I guess. I’ve never heard that pronounced before. But [snorts] um then for cloud ones uh there’s these run on a provider machines and you can create and control them through an API. This can be modal E2B or um sale. Uh

many many other sandbox providers exist as well. Daytona. so for local sandboxing um the typical way that people do this is docker. Um, and so you create a Docker container and then this can be like the default for local development. Um, it gives you uh

kind of a broad there’s a broad variety of tooling for it because it’s kind of the default at the moment. But one problem especially if you’re in like the LTI or the machine learning department here um or otherwise have access to an HPC cluster uh like Babel or Flame or

something like this Docker will not run there typically because Docker requires root privileges and so there’s other uh options like uh Appainer which can be used um where it runs as your user without root. Um the problem with this is the tooling for this is not quite as

good. So, a lot of people will assume Docker and then you need to run Appainer and that’s kind of a pain. one issue with Docker containers is they don’t give you all of the permissions of a virtual machine. So, you can’t like easily for example just install Windows

from scratch and accept expect that it will work uh as well. But um, Chemu is basically a solution for helping solve this problem. So, you can create virtual machines. So these are just examples. There’s a lot of other technologies that you can use here. But um long story

short, if you want a particular technology, there’s a way uh that you can realize it typically. Um one other final thing that I’d like to to mention uh which I realized I forgot to put on my slides is um Harbor is now kind of the default evaluation environment. You

um evaluation toolkit that a lot of people use. um a you experienced it in the um evaluation assignment I think um it supports all of these backends typically. So if you want to run an evaluation and you have a constraint of I want to run on modal or I want to run

on E2B or docker or app tainer or something like this typically just look for the back end that they already have implemented and uh you can use that um appainer uh used to be called singularity and they uh renamed it into appainer. So if you’re using hardware

you should look for singularity if you if you want to do uh appainer based evaluation. Cool. Um, any questions here? Kind of in the weeds, but very important, uh, if you’re deploying or or training agents. Okay. Another thing I’d

like to talk about is credentiing. So, um, there’s identity and authority. So the identity is uh who is uh you know acting um who is acting and so agents basically need to be given an identity that they can act under. There’s also um the

requested action uh on what resource and what permissions they have. So are they allowed to use it in that particular way? Um, how many people have used like AWS or or Google Cloud or something? Probably a lot of people. Um, the identity there is so complex it makes

your head spin, right? There’s like 5,000 5,000 different roles to do different things and stuff like this. Um, on the other hand, there are some websites that give you an API key and that API key can do anything. or there’s one that give you a write API key and a

read API key. Um, there’s some websites that have no API whatsoever. So, the only way to use it is to click on the website and and click through. So what I want to say is the first thing I think about when I want to start using an agent on a website is what is the API

and how fine grained are the permissions uh that you can uh you can give the agent because agents do lots of silly things when you least expect it. And if you only have the ability to give like a generic read API key or write API key, you need to think very very seriously

before giving a write API key because it might delete your account or or something like that. So um that that’s something that you need to be very careful about. So um fine grain tokens or fine grained uh permissions is maybe a better way to

to think about it. But um GitHub is a very good example that has pretty fine grain permissions where you can say on this repo this token gives you these permissions with read or write roles. And so if you want to create agents, uh, typically what I would suggest is for

each individual agent, especially if you’re deploying them and not just, um, not just evaluating them, you want to give it the the least permissions possible. And like the principle of least permission is a very common idea in, you know, uh, software engineering

or or computer security. But if you have an issue handling agent, it should be able to read issues, apply labels, but it should not be able to publish a release, right? And then you know if this key leaks or this um agent gets prompt injected,

you should be okay because it most at the very worst it would apply some malicious label to the PR and then that’s a much weaker threat than being able to publish a release, right? Um, on the other hand, the release account should be able to, you know, read the

approved source and publish uh publish the release, but you can lock this down a lot more carefully, of course, so you don’t have it read untrusted text. You don’t like allow it to be prompted. And this is actually how like at Open

Hands, this is how our current um workflow is set up. We have agents that read all the untrusted text and have very few permissions. And then we have agents that have more permissions, but they it’s very hard for them to get untrusted text for instance.

Um, another thing you can pay attention to is how long the keys last. So in some cases we issue ephemeral keys that basically um continue only until the GitHub action finishes. So uh once agent is done that key disappears. So even if that key somehow leaked in the process

of running the GitHub action it’s invalid as soon as the action finished. So the amount of time that an attacker could use that is uh very short. So this is actually something that I just learned about uh very recently. Um this reference is from Nvidia OpenShell

which is kind of one of their sandboxing solutions. Um but this idea of credential brokering is um a very useful way to essentially prevent uh sandboxed agents from uh from doing bad things with credentials. So um what a credential broker does is essentially it

will start a sandbox and it will issue an ephemeral key um cue the agent. When the agent makes a call with that key, there will be a check in between and then only if that check passes, it will make an API call with a different key uh with the like master key. And so the key

that the agent has, number one is shortlived. It only lives as long as the agent. Number two, um you can actually add additional permissions checks on top of the ones that are provided by the site. So like let’s say you’re working with a site that only has a general

write key and a general read key. Um the the credential broker can block certain API calls for instance. So it could block all the API calls that you don’t want access to. So it’s a pretty elegant way to increase the security level uh significantly.

Okay. So, finally, I’m gonna talk very briefly about monitoring. We only have seven minutes, but this is a big topic, so I think Daniel is going to talk uh go much into much more detail about monitoring. Um I I still felt it would be a good idea to cover it in this class

though because it’s a very uh kind of important part of safety. So monitoring um records and checks agents behavior to identify mistakes and unsafe actions. And so um checks can during a run can essentially flag problems in time to

pause or block an action and reviewing logs afterwards can help explain failures. there’s a bunch of different pieces of evidence that you can use for monitoring, but two of the biggest ones are um

monitoring the uh tool calls and tool results. Um and so if you look at something like this, you can uh essentially see that it was um uh uncovering a reward hacking behavior. There’s a couple uh you can use either

rulebased or model based checks. Um so for a rulebased check um you could check for example is this um publisher on the allowed list. Another thing is if you told the model like it should definitely not access this website, you can just do a rulebased check to see if it access

the website and that’s like easy um and and kind of like foolproof. Another thing that you can do is um you can have modelbased checks where you have like a rubric of the things that the agent is allowed to do and if the agent does something it’s not allowed to

do uh then you you check it with a LM. Um another uh very big topic nowadays is chain of thought monitoring. um where basically uh the tool call monitoring you can see a code patch in tool outputs um or sorry you can see tool calls and tool outputs. Um this is an example from

an open AAI paper on chain of thought monitoring where they basically identified that the model discovered that it could replace the verifier code with just a return true and solve the task by do by doing that. Um but if you look at the chain of thought

monitoring um chain of thought monitoring is looking at the model’s latent reasoning. So you can see that the model in its chain of thought said but we could fudge by making the analys by making analyze worthless and always verifying is true. We can circumvent

verify to always return true. But it’s implemented in poly analyze inspect verify. So you can see it’s like explicitly saying it’s going to fudge and not like solve the task properly. And this is maybe a little bit more obvious if you look at this in addition

to the tool calls. so this is an interesting uh figure from their paper where they basically had GPT40 uh monitor a reasoning model and GPT40 was able to monitor and was able to do a better job of identifying that there was a problem when it was able to

look at the chain of thought as opposed to when it was able to look only at the tool calls. it was also able to do it earlier. Um, so like in the very early stages it was not nearly as obvious by looking at the tool calls uh as it was later. And so what this indicates is

that um having access to the reasoning is a very effective way to monitor for safety. Um but the problem is uh there are limits to this. So number one it is useful as I mentioned. Um but number two is uh that a lot of people are worried

right now that essentially if you are doing some sort of reinforcement learning objective that cause causes agents to like fail uh or get penalized when a monitor reveals that they were cheating. then they will essentially look you learn to hide uh that the fact

that we’re cheating by reasoning in words that are not easily detectable. And so if you put pressure on them through chain of thought monitoring uh then you know even if you’re not intending to do so you might cause the chains of thought to be harder to

understand. and so, uh, this this is a problem that people are grappling with right now, and people are thinking of ways to like monitor the, um, the embeddings of the model or monitor other things that would be like harder to hide uh, than just the

monitoring. And they’re actually saying you shouldn’t uh be adding this to the reinforcement learning objective and instead be thinking of other ways. so a final thing uh that I would like to talk about is uh tool call monitoring. And so there are methods for just in

time uh tool call monitoring. So I I cited a paper here called AI control. There’s other ones like tool guard. Um grawan which I mentioned before has a method called signal um that allows you to do this. And basically you check and if this is okay then you would allow it.

If it’s not okay then you would block and just stop. And this is a common feature of coding agents. Um I’m I’m sure people have used uh one second. I’m sure people have used coding agents. Um and there they have the option to verify every uh command that you haven’t given

permission for already. So uh that’s one way to do it. But you can also do that with like AI based checking of each tool call to tell whether it’s reasonable enough or not. Um I’ve tried this with AI based methods and they’re quite good. They actually do stop a pretty uh large

number of uh things before they could actually happen. Yeah. >> Yeah. And and that’s the typical way of doing it. You can either have a smaller LLM as a judge check. Um you can fine-tune an LLM to do the checking, which is what a lot of these things do.

And then the final thing is um you can actually have the model self report uh whether it’s dangerous or not. So um the good thing about that is you don’t need to add any extra uh many extra tokens in order to have the model itself do it. >> You just say is this action dangerous or

not? And they’re actually reasonably good at uh at telling you. Um yeah actually sorry I just realized I still had another section on benchmarks. Um uh one basically uh very quickly because I know we’re at time um you can measure not just test task completion but also

unnecessary refusal unsafe tests and completed harm. Um and you can have safety tests with non-malicious and malicious tasks. Um and there’s benchmarks for different threats. Um we’ve created a benchmark called open agent safety which I actually quite like

because it evaluates agents in the um in like an actual company context. So it’s uh you know them working in a company. So you can take a look at it. Uh but there are other ones like agent harm agent Joho tool emu and others. So um within the agent open agent safety paper

we actually have a huge table of all of the agent safety benchmarks that were out at the time when we wrote this paper. Uh so if you’re interested in doing this for example for a project or something like that uh there’s lots of uh lots of good benchmarks for uh agent

safety as well. Cool. Um yeah and so that’s uh all I have for today. Thanks.

持续耕种,欢迎订阅 🌱

RSS全站 ·原创 ·策展 ·雷达;公众号「炼金术」同步更新。

本讲伴读笔记以 CMU 11-768 AI Agents (Fall 2026) 课程大纲为参考框架独立撰写。原课程由 Daniel Fried 与 Graham Neubig 讲授,原始课件与视频见cmu-agents.com; 音频镜像与文字稿仅作大陆可达性便利,版权归课程方所有。本文观点与文字为作者原创。

本讲大纲
  1. 01导语
  2. 02一、威胁归因:从五起真实事故到四类威胁模型
  3. 03二、注入防御三路线:提示工程、架构与训练谁顶用
  4. 04三、沙箱边界:隔离放哪一层、放多深
  5. 05四、凭据管理:藏住 API key 为什么不等于限制权限
  6. 06沙箱与凭据的共同原则:置于 agent 控制之外
  7. 07五、监控与评测账本:CoT 证据的限度与告警基础率
  8. 08六、编码 agent 三年简史:从 HumanEval 到软件工厂
  9. 09七、OpenHands SDK:一个 agent 框架到底抽象了什么
  10. 10八、单 agent vs 多 agent:专业化的代价与边界
  11. 11九、软件工厂:当检查环节从人手转移到管道
  12. 12论文与材料导读
  13. 13实践视角
  14. 14本讲要点
  15. 15延伸阅读
  16. 本讲模块概念回顾本讲资料自测问学伴微行动文字稿