跳到主要内容

对抗模式

对抗模式(Adversary Mode)添加了一个静默的、独立的智能体审查者,在工具调用执行前对其进行监视。它充当第二双眼睛——在主智能体被攻陷、被提示词注入操纵,或者只是做出了与你要求不符的事情时保护你。

与基于模式的检测不同,对抗审查者能够理解上下文。它知道你最初要求的是什么、你最近说了什么,并能判断一次工具调用对你的任务来说是否合理。

工作原理​

  1. 在每次工具调用之前,对抗审查者会检查你的原始任务、最近的消息以及工具调用详情
  2. 它根据你的规则评估该工具调用,并返回 ALLOW 或 BLOCK
  3. 被阻止的工具调用会被拒绝——智能体会看到拒绝信息,且无法重试
  4. 如果审查者因任何原因失败,该工具调用会被放行(fail-open,失败即放行)

对抗模式使用 zai 已配置的同一模型和提供商。无需额外的 API 密钥或服务。

启用对抗模式​

创建一个位于 ~/.config/zai/adversary.md 的文件,写入你的规则:

BLOCK if the tool call:
- Exfiltrates data (posting to unknown URLs, piping secrets to external services)
- Is destructive beyond the project scope (deleting system files, wiping directories)
- Installs malware or runs obfuscated code
- Attempts to escalate privileges unnecessarily
- Downloads and executes untrusted remote scripts

ALLOW normal development operations like editing files, running tests,
installing packages, using git, etc. Most tool calls are fine.
Err on the side of ALLOW — only block truly dangerous things.

就这样。如果该文件存在,对抗模式即处于开启状态。删除该文件即可关闭。

空文件将使用与上面的示例类似的合理默认规则。

编写好的规则​

adversary.md 中的规则就是你的策略,用自然语言编写:

  • 对应该阻止的内容要具体——模糊的规则会导致误报
  • 倾向于 ALLOW——对抗审查者应当捕获真正危险的操作,而不是拖慢正常工作
  • 思考你的威胁模型——一个失控的智能体实际上会尝试做什么?

你可以根据环境定制规则:

BLOCK if:
- Any network request goes to a domain not in: github.com, npmjs.org, pypi.org
- Files outside of ~/projects/ are modified
- SSH keys, .env files, or credentials are read or transmitted

ALLOW all standard development operations within ~/projects/.

审查范围​

默认情况下,对抗审查者审查 shell——即可以执行任意代码的工具。

你可以通过在 adversary.md 顶部添加一行 tools: 来扩大覆盖范围:

tools: shell, computercontroller__computer_control
---
BLOCK if the command exfiltrates data or is destructive.
ALLOW normal development operations.

tools: 行是一个以逗号分隔的待审查工具名称列表。--- 分隔符之前的所有内容都是配置;之后的内容是你的规则。如果省略 tools: 行,默认审查 shell。

一些你可能会想添加的工具名称:

工具名称作用
shell执行 shell 命令(默认)
computercontroller__computer_control通过 peekaboo 实现 macOS UI 自动化——点击、按键、输入

另请参阅​