Termaxa 场景:给 AI 编码 Agent 装上命令安全门控
AI 编码 Agent(Claude Code、Cursor、Codex)极大提升了开发效率,但它们操作的是真实的生产环境——你的代码仓库、数据库、Docker 容器和云资源。当 Agent 执行 git push --force、DROP TABLE users 或 rm -rf / 时,信任与风险的天平如何平衡?
今天的选择只有两种:全程监督每条命令(违背 Agent 的自动化初衷),或盲目信任(后果自负)。Termaxa 提供了一个第三种方案——一个协作式的命令安全门控层。
场景:Agent 要删表,怎么办?
想象一下这个场景:Claude Code 正在为你的电商应用重构数据库架构,它决定先删除旧的 users 表。
如果没有 Termaxa,Agent 直接执行 psql -d shop -c 'DROP TABLE users'——3 个外键依赖的表在级联删除中被连带清除,50,000 条用户记录瞬间消失。
有了 Termaxa,同一个命令会变成:
$ termaxa check "psql -d shop -c 'DROP TABLE users'"
decision: deny
reason : DROP TABLE is blocked. Archive or rename instead.
preview : postgres impact
DROP TABLE users
rows (estimate) : 50,000
referenced by : audit_log, orders, sessions (3 tables)
without CASCADE : this DROP will FAIL (dependents exist)
insurance : pg_dump users before execution (automatic on run/hook)
Termaxa 不仅阻止了危险操作,还提供了影响预览——多少行数据、哪些表受影响、CASCADE 依赖关系。Agent 收到「deny」和原因后,会自动提出替代方案(如 ALTER TABLE users RENAME TO users_archive),而不是盲目重试。
Termaxa 是什么
Termaxa 是一个 Rust 编写的 CLI 工具,作为 AI Agent 和 Shell 之间的协作式门控层。它不是沙箱(sandbox),而是风挡玻璃(windshield)——沙箱将 Agent 困在隔离环境中,Termaxa 让 Agent 安全地操作真实资源。
它的核心工作流:
Claude Code → TERMAXA → git . postgres . docker . terraform . 你的 Shell
|
+-- decide 按策略 allow / ask / deny
+-- preview 展示影响范围
+-- insure 破坏性操作前自动备份
+-- escalate 重复危险操作自动阻断
+-- record 完整审计日志
架构解析
Termaxa 由六个引擎组成,打包在一个二进制文件中:
- Shell 分割引擎:将复合命令(
&&,||,;,|)拆分为独立片段,逐段判断 - 策略引擎:基于 YAML 策略文件(
.termaxa/policy.yaml),首个匹配规则生效 - 上下文引擎:检测当前分支、环境标记(prod/dev)、SQL 上下文
- 预览引擎:Git 预览(即将丢失的提交)、PostgreSQL 影响评估、Terraform plan
- 保险引擎:自动备份(Git ref / pg_dump / 文件快照)
- 执行与审计引擎:JSONL 格式审计日志,支持 Webhook 通知
5 分钟快速上手
安装
Termaxa 可通过两种方式安装:
- 预编译二进制:从 Releases 页面 下载
- Rust 工具链编译:
cargo install --git https://github.com/termaxa/termaxa
初始化项目
cd your-project termaxa init --claude-code
这会创建 .termaxa/policy.yaml 并安装 Claude Code 的 PreToolUse 钩子。运行时状态(日志、备份)存储在 ~/.termaxa/——在仓库之外,不会被 git 操作意外触及。
测试门控
$ termaxa check "git push --force origin main" ┌ push preview (main -> origin) │ ⚠ remote will LOSE 1 commit(s): │ ✗ 44510f1 important work └
四个关键场景
场景一:安全前缀掩不住危险操作
Agent 经常用 “安全” 前缀隐藏真实意图:
$ termaxa check "git status && rm -rf /" decision: deny reason : segment 2/2 `rm -rf /` — Recursive delete from root is blocked.
git status && 什么都没”买到”——Termaxa 拆分复合命令后逐段判断。
场景二:跨 shell 意图识别与断路器
Agent 被阻止后,往往会换一种写法重试:
$ rm -rf . → ask (file-delete #1)
$ Remove-Item -Recurse -Force . → ask (file-delete #2, 不同 shell)
$ del /s /q . → DENY circuit breaker: 2 prior
file-delete attempts this session
三个 shell,同一个意图,第三次自动阻断。find -exec rm、xargs rm、unlink 同样计入。断路器默认启用,阈值 2 次。
场景三:先破坏,再恢复
当 Agent 确实需要执行破坏性操作时,Termaxa 在执行前自动备份:
$ termaxa run -- git push --force origin main ┌ push preview (main -> origin) │ ⚠ remote will LOSE 1 commit(s): │ ✗ 44510f1 important work └ Proceed? [y/N] y 🛟 backup b-1783006590625 — origin/main @ 44510f1 pinned to termaxa/backup/b-1783006590625 $ termaxa rollback b-1783006590625 ✓ origin/main restored to 44510f1
Force push 不只展示远程将新增什么,还展示将丢失什么——然后将丢失的提交钉在备份分支上。
场景四:会话结束后的事后审计
$ termaxa report ┌─ Termaxa Execution Report ───────────────────────── │ commands : 4 ✓ 1 allow · ? 2 ask · ✗ 1 deny │ blocked : ✗ psql -d shop -c "DROP TABLE users" │ impact : • DROP users ~50,000 rows, 3 dependent(s) │ • plan: +3 ~0 -1 │ backups : 1 — rollback available │ risk : Medium (deny×3 + escalation×2 + ask×1 = 5) └──────────────────────────────────────────────────
每行都是审计日志中确凿的事实,没有猜测。
策略配置
策略文件 .termaxa/policy.yaml 采用 YAML 格式,首个匹配规则生效:
version: 1
default: ask # 未匹配的命令需要审批
rules:
- match: "git status*"
action: allow
- match: "git push*--force*"
action: ask
reason: "Force push — remote history will be overwritten."
- match: "*drop table*"
action: deny
reason: "DROP TABLE is blocked. Archive or rename instead."
circuit_breaker:
enabled: true
threshold: 2 # 第 3 次重复危险操作时触发
notify:
webhook: https://hooks.slack.com/services/...
on: [deny, ask]
策略文件在仓库中版本管理,可通过 PR 审查——团队协作友好。
完整 CLI 命令
| 命令 | 用途 |
|---|---|
termaxa init [--claude-code] | 初始化项目,安装钩子 |
termaxa check " | 干运行:给出判断结果和预览 |
termaxa run -- | 门控执行:预览 → 审批 → 备份 → 运行 |
termaxa hook | Claude Code PreToolUse 模式 |
termaxa log [--decision D] [--json] | 审计日志查询 |
termaxa stats | 统计概览 |
termaxa backups / termaxa rollback | 管理备份 |
termaxa report [--session ID] [--md] | 会话总结报告 |
termaxa notify --test | 验证 Webhook |
termaxa paths | 查看策略和状态文件路径 |
与同类工具的对比
| 维度 | Termaxa | 沙箱/Docker | OPA 策略引擎 |
|---|---|---|---|
| 定位 | 协作式风挡 | 隔离墙 | 策略决策器 |
| 执行预览 | ✅ 自带的 Git/PG/Terraform 预览 | ❌ | ❌ |
| 自动备份 | ✅ 自动执行前备份 | ❌ | ❌ |
| 回滚能力 | ✅ termaxa rollback | ❌ | ❌ |
| Shell 意图识别 | ✅ 跨 shell 断路器 | ❌ | ❌ |
| Agent 原生钩子 | ✅ Claude Code / Cursor | ❌ | ❌ |
| 限制 | 协作式(非强制) | 完全隔离 | 纯策略 |
诚实说明
Termaxa 目前是 v0.x,它在真实场景下经过测试,但不是魔法:
- 钩子是协作式集成点,不是强制执行边界。Claude Code 和 Cursor 会尊重
deny并提出替代方案,但一个完全自主模式的 Agent 可能通过不同路径绕过。真正的强制执行需要 OS 级沙箱配合。 - 原生 Agent 工具跳过 Shell 门控。文件编辑、代码生成等内置工具不走 Shell——在实测中,Cursor Agent 切换到原生文件删除工具后,Termaxa 完全看不到。这需要 OS 级隔离来兜底。
- Shell 解析不是完美的。对
&&、||、;、|的分割是可靠的,但深层嵌套的引号、变量展开的命令会被保守判断。 - 预览是最佳努力。PostgreSQL 行数估计来自计划器(
pg_class.reltuples),不会扫描全表。Terraform 预览通过terraform plan子进程完成。 - Claude Code 和 Cursor 已端到端验证。Codex 和 Copilot 的钩子格式已解析但未端到端验证。
总结
Termaxa 解决了一个微妙但重要的问题:如何让 AI Agent 安全地操作真实资源。它的设计哲学——风挡而非围墙(windshield, not sandbox)——意味着它与现有沙箱方案互补而非竞争。对于正在将 Claude Code 或 Cursor 引入工作流的开发团队,Termaxa 提供了一个低成本、高回报的安全层:一个 cargo install、一个 termaxa init,就能获得命令预览、自动备份、策略门控和完整审计。
相关链接
- Termaxa GitHub 仓库(MIT / Apache-2.0 双许可)
- Termaxa 发布页