mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 21:05:51 +00:00
* docs: 修复文档巡检发现的 4 处错误 - daemon.md: 反映实际实现状态(supervisor/worker 已实现而非 stub) - bridge-mode.md: API 操作数量从 7 修正为 9 - web-search-tool.md: 文件路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - remote-control-self-hosting.md: 补充缺失的 RCS_WS_IDLE_TIMEOUT 和 RCS_WS_KEEPALIVE_INTERVAL 配置项 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety 和 Context 文档中的代码引用和类型错误 - permission-model: 修正规则来源从"五层"到八层,优先级顺序对齐代码 - permission-model: PermissionUpdate 类型改为实际的 addRules/replaceRules 等 - permission-model: 补充 acceptEdits 和 dontAsk 两种权限模式 - permission-model: DENIAL_LIMITS 字段名对齐实际代码 - plan-mode: 工具路径从 src/tools/ 改为 packages/builtin-tools/src/tools/ - compaction: 修正 COMPACTABLE_TOOLS 和 POST_COMPACT_* 的行号 - project-memory: 修正 ENTRYPOINT_NAME 常量的行号 - system-prompt: 修正 SystemPrompt 类型定义文件路径和多个行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 introduction 文档中的错误路径和行号引用 - why-this-whitepaper.mdx: BashTool 路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - what-is-claude-code.mdx: 移除不存在的 Azure provider,改为实际的 7 种 provider - architecture-overview.mdx: State 类型行号从 204 修正为 207 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 conversation/features 文档中的错误 - streaming.mdx: queryStreamRaw → queryModelWithStreaming 函数名修正 - streaming.mdx: Azure 提供商不存在,替换为实际 7 个提供商 - debug-mode.mdx: --inspect-wait 描述错误,实际使用 BUN_INSPECT 环境变量 - buddy.mdx: 补充缺失的 companionReact.ts、CompanionCard.tsx、index.ts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复文档巡检中的源码引用错误 - feature-flags.mdx: 修正 feature() 兜底描述,实际从 bun:bundle 导入而非 cli.tsx:3 内联 - feature-flags.mdx: 修正工具 require 路径为 @claude-code-best/builtin-tools 包路径 - ant-only-world.mdx: 修正 tools.ts 中 require 路径为包路径 - ant-only-world.mdx: 修正 INTERNAL_ONLY_COMMANDS 行号 (267-295) 和数量 (24+) - skills.mdx: 修正 COMMANDS memoize 行号 258 → 299 - mcp-protocol.mdx: 修正 fetchToolsForClient LRU 缓存上限 20 → 100 - streaming.mdx: 修正流式事件引用 - file-operations.mdx: 修正工具路径引用 - search-and-navigation.mdx: 修正搜索工具引用 - shell-execution.mdx: 修正 shell 工具引用 - buddy.mdx: 补充缺失的 frontmatter 字段 - debug-mode.mdx: 修正调试模式描述 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 tools/agent 文档中的文件路径和行号引用 - 修正 TodoWriteTool、AgentTool、ToolSearchTool 等工具路径 src/tools/ → packages/builtin-tools/src/tools/ - 更新 Tool.ts、tools.ts、BashTool.tsx 中过时的行号引用 - 修正 WebSearchTool/WebFetchTool/EnterWorktreeTool/ExitWorktreeTool 路径 - 修正 AgentTool.tsx 中多行行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的文件路径和行号引用 - ultraplan.md: 更新文件行数(525/349/127) - fork-subagent.md: 路径迁移 src/tools/ → packages/builtin-tools/ - mcp-skills.md: 修正 getMcpSkillCommands 行号 547→604,client.ts 行号 117→129 - kairos.md: 修正 getBriefSection/getProactiveSection 行号 - proactive.md: 修正 getProactiveSection 行号 860→864 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正顶层文档中的路径迁移和行号引用 - auto-updater.md: config.ts 行号 1735→1737,标注未接入启动流程的函数 - external-dependencies.md: WebSearchTool/WebFetchTool 路径迁移到 builtin-tools 包,Vertex 行号修正 - lsp-integration.md: LSPTool 路径从 src/tools/ 迁移到 packages/builtin-tools/ - stub-recovery-design-1-4.md: 修正 Windows 绝对路径链接为标准代码引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 task 文档中的文件扩展名和路径引用 - task-004: AssistantSessionChooser.ts → .tsx, assistant.ts → .tsx - task-003: cli.tsx 行号 249→272, markdownConfigLoader.ts 行号 29→35 - lan-pipes: SendMessageTool 路径迁移到 packages/builtin-tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 computer-use-tools-reference 缺失的 Windows 工具 添加遗漏的 open_terminal 和 activate_window 两个 Windows 专属工具, 修正工具总数 37→39,Windows 工具数 10→12。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 audit/bash-classifier/token-budget/tree-sitter 文档 - feature-flags-audit: ScheduleCronTool 路径迁移、DAEMON 状态更新为 COMPLETE、assistant 文件标记已补全、UDS 标记已实现 - bash-classifier: BashPermissionRequest 文件路径修正、withRetry 行号移除 - token-budget: attachments.ts 行号范围修正 - tree-sitter-bash: bashPermissions.ts 路径迁移到 packages/builtin-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 langfuse-monitoring AgentTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 bridgeApi 行号和 Tool.ts 行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety/Extensibility 文档中的工具路径迁移和行号引用 - sandbox.mdx: shouldUseSandbox.ts 和 bashPermissions.ts 路径迁移至 packages/builtin-tools - why-safety-matters.mdx: bashPermissions.ts 路径迁移(3 处) - plan-mode.mdx: EnterPlanModeTool/prompt.ts 路径迁移 - auto-mode.mdx: Auto mode 指令行号 3464→3481 - hooks.mdx: AgentTool/runAgent.ts 路径迁移 - skills.mdx: SkillTool.ts 路径迁移 - custom-agents.mdx: Agent built-in 目录和 exploreAgent.ts 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 internals 文档引用计数和路径 - ant-only-world: USER_TYPE 引用计数 465→410+,工具路径迁移到 builtin-tools - growthbook-ab-testing: growthbook.ts 行数 1156→1258 - hidden-features: 语音模式状态更新(audio-napi 已恢复) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正工具文档中的行号引用 - sub-agents: AgentTool.call 入口行号 340→387 - shell-execution: ShellCommand onTimeout 行号 129→144 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的状态、路径和计数 - all-features-guide: 修正 feature flag 启用范围(dev only vs dev+build) - tier3-stubs: 大量状态修正(stub→已实现),缩减过时条目 - workflow-scripts: 路径迁移到 builtin-tools,状态更新 - web-browser-tool: 工具状态缺失→已实现,路径迁移 - context-collapse: CtxInspectTool 状态缺失→已实现 - computer-use: 行号引用更新,平台分发描述修正 - computer-use-tools-reference: 工具数 39→38 - voice-mode: voiceModeEnabled 行数 55→54 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 the-loop 查询循环行号引用 query.ts 代码变更后终止原因行号整体偏移约 40 行 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 feature-flags-audit 完整 build 默认 feature 列表 添加 ULTRATHINK/LODESTONE/ACP/DAEMON 等 19 个缺失的 build 默认 feature, 修正 dev-only 特征标注(UDS_INBOX/LAN_PIPES/BG_SESSIONS/TEMPLATES) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit ConfigTool 路径迁移 ConfigTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit BashTool 路径迁移 BashTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit SkillTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 feature-flags-audit WorkflowTool 状态为已实现 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
264 lines
11 KiB
Plaintext
264 lines
11 KiB
Plaintext
---
|
||
title: "Auto Mode - AI 分类器驱动的自主执行模式"
|
||
description: "详解 Claude Code 的 auto mode:基于 transcript classifier 的自动权限决策、两阶段分类流水线、危险权限剥离机制、模式切换状态管理、以及与 plan mode 的协作方式。"
|
||
keywords: ["auto mode", "yoloClassifier", "transcript classifier", "权限分类", "自动执行", "两阶段分类"]
|
||
---
|
||
|
||
## 概述
|
||
|
||
Auto mode 是 Claude Code 的一种权限模式,让 AI 进入**连续自主执行**状态。与传统模式(每个敏感操作都弹出权限对话框等待用户审批)不同,auto mode 使用 AI 分类器(transcript classifier)自动判断每个工具调用是否安全,从而实现无中断的执行体验。
|
||
|
||
```
|
||
权限模式层级:
|
||
|
||
default → auto → bypassPermissions
|
||
(逐项确认) (AI 分类器审批) (全部放行)
|
||
```
|
||
|
||
## 核心架构
|
||
|
||
### 1. AI 分类器(yoloClassifier)
|
||
|
||
分类器是 auto mode 的核心,位于 `src/utils/permissions/yoloClassifier.ts`。
|
||
|
||
每个工具调用经过分类器评估,返回三种裁决之一:
|
||
|
||
| 裁决 | 含义 | 处理方式 |
|
||
|------|------|---------|
|
||
| **allow** | 操作安全 | 直接执行,用户无感知 |
|
||
| **deny** | 操作危险 | 拒绝执行,向 AI 返回拒绝消息 |
|
||
| **ask** | 无法确定 | 回退到传统权限对话框 |
|
||
|
||
分类器的判断基于完整的对话上下文(transcript),而不仅仅看单条命令——它能理解操作的意图和上下文。
|
||
|
||
### 2. 两阶段分类流水线
|
||
|
||
分类器支持两阶段 XML 分类(`classifyYoloActionXml`):
|
||
|
||
```
|
||
Stage 1 — "fast"(快速判断)
|
||
├── max_tokens=64,stop_sequences 触发即时 yes/no
|
||
├── 如果 allow → 直接放行(最快路径)
|
||
└── 如果 block → 进入 Stage 2
|
||
|
||
Stage 2 — "thinking"(深度思考)
|
||
├── chain-of-thought 推理
|
||
├── 减少误报(false positives)
|
||
└── 最终决定 allow / deny / ask
|
||
```
|
||
|
||
两个阶段共享相同的 system prompt 和 user content,利用 API 的 prompt caching(1 小时 TTL)优化性能。
|
||
|
||
可通过配置选择模式:
|
||
- `'both'`(默认)— 两阶段都跑
|
||
- `'fast'` — 只跑 Stage 1
|
||
- `'thinking'` — 只跑 Stage 2
|
||
|
||
### 3. 分类器结果类型
|
||
|
||
```typescript
|
||
// src/types/permissions.ts
|
||
type YoloClassifierResult = {
|
||
thinking?: string // 分类器的推理过程
|
||
shouldBlock: boolean // 是否阻止
|
||
reason: string // 决策原因
|
||
unavailable?: boolean // 分类器是否不可用
|
||
transcriptTooLong?: boolean // 对话是否超出上下文窗口
|
||
model: string // 使用的分类器模型
|
||
stage?: 'fast' | 'thinking' // 哪个阶段做出的决定
|
||
// ... token 使用量、耗时等监控字段
|
||
}
|
||
```
|
||
|
||
## 安全机制
|
||
|
||
### 危险权限剥离
|
||
|
||
进入 auto mode 时,系统调用 `stripDangerousPermissionsForAutoMode()`(`permissionSetup.ts:510`),移除所有可能绕过分类器的 allow 规则。
|
||
|
||
被剥离的规则类型(`dangerousPatterns.ts`):
|
||
|
||
| 规则类型 | 示例 | 剥离原因 |
|
||
|---------|------|---------|
|
||
| **Bash 代码执行** | `Bash(python:*)`, `Bash(node:*)` | 解释器可执行任意代码,绕过分类器审查 |
|
||
| **Shell 入口** | `Bash(bash:*)`, `Bash(sh:*)` | 直接 shell 访问等同无限制 |
|
||
| **Agent 规则** | `Agent(*)` | 任何 Agent allow 规则会绕过分类器审批子代理 |
|
||
| **PowerShell 代码执行** | `PowerShell(node:*)` | 同 Bash 逻辑 |
|
||
| **权限提升** | `Bash(sudo:*)`, `Bash(eval:*)` | 可执行任意命令 |
|
||
|
||
剥离的规则被暂存在 `strippedDangerousRules` 中,退出 auto mode 时通过 `restoreDangerousPermissions()` 恢复。
|
||
|
||
### 模型支持检测
|
||
|
||
不是所有模型都支持 auto mode。`modelSupportsAutoMode()`(`src/utils/betas.ts`)检查当前模型是否具备安全分类能力。不支持的模型无法进入 auto mode。
|
||
|
||
### Circuit Breaker 机制
|
||
|
||
`autoModeState.ts` 维护一个 circuit breaker 标志:
|
||
|
||
```typescript
|
||
let autoModeCircuitBroken = false // 由远程配置控制
|
||
```
|
||
|
||
当远程配置(GrowthBook `tengu_auto_mode_config.enabled`)设为 `'disabled'` 时,circuit breaker 触发,阻止 auto mode 的进入和继续使用。这为 Anthropic 提供了远程紧急关停能力。
|
||
|
||
## 模式切换状态管理
|
||
|
||
### 进入 Auto Mode
|
||
|
||
`transitionPermissionMode()`(`permissionSetup.ts:597`)处理所有模式切换:
|
||
|
||
```
|
||
1. 检查 auto mode gate 是否开启(isAutoModeGateEnabled)
|
||
2. 设置 autoModeActive = true
|
||
3. 调用 stripDangerousPermissionsForAutoMode() 剥离危险规则
|
||
4. 向对话注入 Auto Mode 系统提示
|
||
```
|
||
|
||
### 退出 Auto Mode
|
||
|
||
```
|
||
1. 设置 autoModeActive = false
|
||
2. 设置 needsAutoModeExitAttachment = true(触发退出通知)
|
||
3. 调用 restoreDangerousPermissions() 恢复被剥离的规则
|
||
4. 向对话注入 "Exited Auto Mode" 提示
|
||
```
|
||
|
||
### 触发路径
|
||
|
||
Auto mode 可通过以下方式激活:
|
||
- CLI 参数 `--enable-auto-mode`
|
||
- settings.json 中的 `autoMode` 配置
|
||
- Plan mode 默认使用 auto mode 语义(`useAutoModeDuringPlan`,默认 true)
|
||
- SDK 控制消息
|
||
- REPL 中 Shift+Tab 切换
|
||
|
||
## 系统提示词
|
||
|
||
### 进入时(Full Instructions)
|
||
|
||
注入到对话中的指令(`messages.ts:3481`):
|
||
|
||
> Auto mode is active. The user chose continuous, autonomous execution. You should:
|
||
>
|
||
> 1. **Execute immediately** — 直接实现,做合理假设
|
||
> 2. **Minimize interruptions** — 常规决策自行判断,减少提问
|
||
> 3. **Prefer action over planning** — 默认直接编码,不进 plan mode
|
||
> 4. **Expect course corrections** — 用户可随时纠正
|
||
> 5. **Do not take overly destructive actions** — 删除数据/修改生产系统仍需确认
|
||
> 6. **Avoid data exfiltration** — 不主动分享密钥/内部文档
|
||
|
||
### 持续运行时(Sparse Instructions)
|
||
|
||
后续轮次注入简短提醒:
|
||
|
||
> Auto mode still active. Execute autonomously, minimize interruptions, prefer action over planning.
|
||
|
||
### 退出时(Exit Instructions)
|
||
|
||
> You have exited auto mode. Ask clarifying questions when the approach is ambiguous rather than making assumptions.
|
||
|
||
## 与 Plan Mode 的协作
|
||
|
||
Plan mode 默认使用 auto mode 语义(`getUseAutoModeDuringPlan()`,默认 true)。这意味着:
|
||
|
||
- Plan mode 进入时,如果 auto mode 可用,也会激活分类器
|
||
- `isAutoModeActive()` 是权威信号(`prePlanMode`/`strippedDangerousRules` 不可靠)
|
||
- 退出 plan mode 时会同时退出 auto mode
|
||
|
||
## 分类器不可用的降级策略
|
||
|
||
当分类器 API 不可用时(`unavailable: true` 或 `transcriptTooLong: true`):
|
||
|
||
- 不会直接 allow — 回退到传统的权限对话框(ask)
|
||
- 向 AI 发送消息:"{model} is temporarily unavailable, so auto mode cannot determine the safety of {toolName} right now."
|
||
- 确定性错误(如对话过长)不重试,直接降级
|
||
|
||
## 分类器 Prompt 模板
|
||
|
||
分类器的行为由三个 prompt 文件控制,位于 `src/utils/permissions/yolo-classifier-prompts/`。这些文件在构建时通过 `require()` 内联为字符串常量,运行时不可修改。
|
||
|
||
### auto_mode_system_prompt.txt
|
||
|
||
主系统提示词,定义分类器的角色、分类流程和决策类别。包含:
|
||
|
||
- **分类流程**:理解操作 → 检查用户意图 → 评估风险
|
||
- **BLOCK 始终阻止**:外部代码执行、不可逆删除、未授权持久化、安全削弱、提权、网络服务
|
||
- **BLOCK 除非明确意图**:CWD 外写入、系统包管理、git push、大规模变更
|
||
- **ALLOW 安全操作**:读文件、搜索、git 只读命令、测试/lint/构建、CWD 内编辑
|
||
- `<permissions_template>` 占位符 — 运行时替换为具体权限模板(external 或 anthropic)
|
||
- 末尾 `Use the classify_result tool to report your classification.` — XML 模式下被替换为 `<block>` 标签格式说明
|
||
|
||
### permissions_external.txt
|
||
|
||
外部用户版本的权限模板。三个 `<user_*_to_replace>` 标签内包裹默认规则(bullet 格式),用户自定义规则**整体替换**默认值:
|
||
|
||
```
|
||
<user_allow_rules_to_replace>
|
||
- 默认 allow 规则 1
|
||
- 默认 allow 规则 2
|
||
</user_allow_rules_to_replace>
|
||
```
|
||
|
||
- **allow**:9 条默认规则(只读命令、版本检查、git 只读、测试/lint/构建、包安装、CWD 内文件操作等)
|
||
- **soft_deny**:10 条默认规则(外部代码执行、递归删除、shell 配置修改、提权、网络服务等)
|
||
- **environment**:4 条环境描述(终端环境、auto mode 上下文、开发工具可用、语言/框架不限)
|
||
|
||
`getDefaultExternalAutoModeRules()` 从此文件提取 bullet 项,供 `claude auto-mode defaults` 命令输出。
|
||
|
||
### permissions_anthropic.txt
|
||
|
||
Anthropic 内部版本的权限模板。默认规则在标签**外部**,标签内为空,用户自定义规则以**追加**方式叠加:
|
||
|
||
```
|
||
- 默认规则(在标签外,始终生效)
|
||
<user_allow_rules_to_replace>
|
||
</user_allow_rules_to_replace>
|
||
```
|
||
|
||
相比 external 版本,额外包含:
|
||
- 云 CLI 只读命令(aws describe, gcloud describe, kubectl get 等)
|
||
- 基础设施即代码 plan 命令(terraform plan, pulumi preview 等)
|
||
- 对应的 deny 规则(云资源创建/修改/删除、IaC apply、生产环境访问等)
|
||
|
||
### 模板替换流程
|
||
|
||
```
|
||
buildYoloSystemPrompt()
|
||
├── BASE_PROMPT.replace('<permissions_template>', EXTERNAL/ANTHROPIC_TEMPLATE)
|
||
├── .replace(<user_allow_rules_to_replace>, userAllow ?? defaults)
|
||
├── .replace(<user_deny_rules_to_replace>, userDeny ?? defaults)
|
||
└── .replace(<user_environment_to_replace>, userEnvironment ?? defaults)
|
||
```
|
||
|
||
- 外部模板:用户设置非空时**替换**对应标签内容,否则保留默认值
|
||
- 内部模板:用户设置**追加**到默认值之后(标签在末尾为空)
|
||
|
||
## 当前状态说明
|
||
|
||
> **注意**:auto mode 的完整代码逻辑已存在于代码库中,但依赖 `feature('TRANSCRIPT_CLASSIFIER')` feature flag。
|
||
> 在当前反编译版本中,`feature()` 始终返回 `false`,因此 auto mode 不可用。
|
||
> 要启用需将 `feature('TRANSCRIPT_CLASSIFIER')` 改为 `true`,并确保 GrowthBook 配置源有合理的 fallback 默认值。
|
||
|
||
Prompt 模板文件为**重建产物**——原始文件在反编译过程中丢失,已根据代码逻辑和 `yoloClassifier.ts` 中的替换模式重新编写。
|
||
|
||
## 相关源码索引
|
||
|
||
| 文件 | 职责 |
|
||
|------|------|
|
||
| `src/utils/permissions/yoloClassifier.ts` | 分类器核心实现 |
|
||
| `src/utils/permissions/autoModeState.ts` | Auto mode 状态管理 |
|
||
| `src/utils/permissions/permissionSetup.ts` | 模式切换、危险权限剥离 |
|
||
| `src/utils/permissions/dangerousPatterns.ts` | 危险命令模式列表 |
|
||
| `src/utils/permissions/classifierDecision.ts` | 分类器决策处理 |
|
||
| `src/utils/permissions/classifierShared.ts` | 分类器共享逻辑 |
|
||
| `src/utils/permissions/bashClassifier.ts` | Bash 命令分类规则 |
|
||
| `src/utils/permissions/bypassPermissionsKillswitch.ts` | bypass 权限熔断器 |
|
||
| `src/utils/permissions/yolo-classifier-prompts/auto_mode_system_prompt.txt` | 分类器主系统提示词 |
|
||
| `src/utils/permissions/yolo-classifier-prompts/permissions_external.txt` | 外部权限模板 |
|
||
| `src/utils/permissions/yolo-classifier-prompts/permissions_anthropic.txt` | 内部权限模板 |
|
||
| `src/cli/handlers/autoMode.ts` | CLI `auto-mode` 子命令处理 |
|
||
| `src/utils/messages.ts` | Auto mode 系统提示词注入 |
|
||
| `src/types/permissions.ts` | 权限类型定义 |
|
||
| `src/utils/betas.ts` | 模型 auto mode 支持检测 |
|