# Claude Code Auto mode 策略参考

> Claude Code Auto mode 的启用条件、classifier 配置、trusted infrastructure、allow/soft_deny/hard_deny、classifyAllShell、denial 复盘和缓存影响。

URL: https://docs.passion8.cc/docs/claude-code/auto-mode-policy
Language: zh-CN
Publisher: Passion8

Auto mode 用 classifier 减少日常工具审批,但它不是“完全自动且安全”的许可。它运行在普通 permissions 之后:显式 ask/deny 规则先判断,再由 classifier 判断剩余动作是否超出用户意图、是否触达未知基础设施、是否像被 hostile content 引导。




必须永远禁止的动作放进 `permissions.deny`,不要只放进 `autoMode.hard_deny`。`permissions.deny` 是 classifier 之前的硬门。




## 可用性速查

| 条件 | 要求 |
| --- | --- |
| Claude Code 版本 | Auto mode 需要较新的 Claude Code,企业 rollout 前固定最低版本 |
| Anthropic API | 默认可用,仍受账号、模型和组织策略影响 |
| Bedrock / Vertex / Foundry / Claude apps gateway | 需要设置 `CLAUDE_CODE_ENABLE_AUTO_MODE=1` 后才进入 `Shift+Tab` 模式循环 |
| LLM Gateway / Passion8 | 如果通过 `ANTHROPIC_BASE_URL` 走 Anthropic API 兼容路径,可能不需要 enable 变量,但要按实际 upstream 验证 |
| 默认启动 | `defaultMode: "auto"` 应放在用户或 managed settings,项目 settings 中的 auto 会被忽略 |
| 禁用 | managed settings 可用 `disableAutoMode: "disable"` 阻止用户启用 |

启用云 provider 路径:

```json title="~/.claude/settings.json"
{
  "env": {
    "CLAUDE_CODE_ENABLE_AUTO_MODE": "1"
  },
  "permissions": {
    "defaultMode": "auto"
  }
}
```

## Classifier 读哪些配置

| 来源 | 适合放什么 |
| --- | --- |
| `CLAUDE.md` | 项目约定、禁止 force push、发布流程、生产环境说明 |
| `~/.claude/settings.json` | 个人可信服务、个人常用 bucket、默认模式 |
| `.claude/settings.local.json` | 个人在某项目的本地例外 |
| Managed settings | 组织级 source control、internal domains、sensitive targets、hard policy |
| `--settings` 或 Agent SDK inline settings | 一次性自动化任务的边界 |

Auto mode 不从共享项目 `.claude/settings.json` 读取 `autoMode`,避免仓库自己给自己放宽规则。

## Trusted infrastructure

`autoMode.environment` 是最重要的字段。它告诉 classifier 什么是公司内部和可信目标,也告诉它哪些目标是敏感目标。

```json
{
  "autoMode": {
    "environment": [
      "$defaults",
      "Organization: acme-corp. Primary use: software development and internal automation",
      "Source control: github.example.com/acme-corp and all repos under it",
      "Cloud provider(s): AWS and GCP",
      "Trusted cloud buckets: s3://acme-build-artifacts, gs://acme-ml-datasets",
      "Trusted internal domains: *.corp.example.com, api.internal.example.com",
      "Key internal services: Jenkins at ci.example.com, Artifactory at artifacts.example.com",
      "Internal package registry: npm.corp.example.com and pypi.corp.example.com",
      "Sensitive remote targets: prod Kubernetes namespaces, production databases, prod Redis",
      "Protected IaC scopes: terraform/prod and pulumi production stacks"
    ]
  }
}
```

这些条目是自然语言,不是正则。写法应该像给新同事解释基础设施边界。

## allow、soft_deny、hard_deny

| 字段 | 作用 | 例子 |
| --- | --- | --- |
| `allow` | 对 soft block 的例外 | 允许写 staging namespace、允许写临时 scratch bucket |
| `soft_deny` | 需要用户明确意图才能放行 | 安装依赖、访问外网、创建工单、修改 staging 配置 |
| `hard_deny` | classifier 内无条件阻断 | 外发源码到第三方 review API、直接修改 production |
| `permissions.deny` | classifier 前硬阻断 | 读取 `.env`、删除目录、读取私钥、生产数据库写操作 |

保留官方默认规则时,每个数组都要包含 `"$defaults"`:

```json
{
  "autoMode": {
    "allow": [
      "$defaults",
      "Writing to s3://acme-scratch/ is allowed. It is an ephemeral bucket with a 7-day lifecycle policy"
    ],
    "soft_deny": [
      "$defaults",
      "Do not run database migrations outside the migrations CLI, even against dev databases"
    ],
    "hard_deny": [
      "$defaults",
      "Never send repository contents to third-party code review APIs"
    ]
  }
}
```

省略 `"$defaults"` 表示替换整组默认规则。只有你完整接管默认列表时才这么做。

## 所有 shell 都进 classifier

默认情况下,窄的 Bash allow 规则,例如 `Bash(npm test)`,可能先于 classifier 生效。要让每条 Bash/PowerShell 都经过 classifier,打开:

```json
{
  "autoMode": {
    "classifyAllShell": true
  }
}
```

这会增加 classifier 调用和延迟,但适合企业强管控环境。低风险个人项目可以不打开。

## 复查 effective config

| 命令 | 用途 | 缓存影响 |
| --- | --- | --- |
| `claude auto-mode defaults` | 打印官方默认 environment、allow、soft_deny、hard_deny | 本地配置检查,不应调用主模型 |
| `claude auto-mode config` | 打印合并后的 effective config | 本地配置检查,不改变 prompt cache |
| `claude auto-mode critique` | 让模型审查规则是否模糊、重复或容易误伤 | 会调用模型,按当前 provider 的 5m/1h TTL 观察 |
| `/permissions` Recently denied | 查看最近被 classifier 或权限系统拒绝的动作 | UI/会话内操作,重试后工具结果会进入上下文 |

反复被拒的目标通常缺 `autoMode.environment`。先补清楚目标,再跑 `claude auto-mode config` 看是否进入 effective config。

## 企业 rollout 模板

```json
{
  "env": {
    "CLAUDE_CODE_ENABLE_AUTO_MODE": "1"
  },
  "permissions": {
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)",
      "Read(./**/*private_key*)",
      "Bash(curl * | sh)",
      "Bash(rm -rf *)",
      "Bash(git push *)",
      "mcp__prod_database__write_*"
    ],
    "disableBypassPermissionsMode": "disable"
  },
  "autoMode": {
    "environment": [
      "$defaults",
      "Source control: github.example.com/acme-corp and all repos under it",
      "Trusted internal domains: *.corp.example.com",
      "Sensitive remote targets: prod Kubernetes namespaces and production databases"
    ],
    "classifyAllShell": true,
    "soft_deny": [
      "$defaults",
      "Installing dependencies from public registries requires explicit user intent"
    ],
    "hard_deny": [
      "$defaults",
      "Never send source code, logs, or customer data to unapproved external services"
    ]
  }
}
```

## 5m / 1h 缓存影响

| 动作 | 影响 |
| --- | --- |
| 切到 Auto mode | 模式变化本身不等于 cache miss,但后续工具执行更少停顿,上下文增长更快 |
| 修改 `autoMode.environment` | 后续 classifier 决策会变化,主会话 prompt cache 仍按内容和 provider 判断 |
| `claude auto-mode critique` | 调用模型,短间隔可复用当前 provider 的缓存窗口 |
| `classifyAllShell` | 每条 shell 都可能多一次 classifier 判定,但不等于主模型 response 缓存 |
| denial 后重试 | 批准后的工具调用和结果会进入会话,可能刷新后续缓存前缀 |
| Passion8/custom gateway | 以 gateway 实际透传的 usage/cache 字段为准,不要假设与 claude.ai 完全一致 |

## 官方参考

- [Configure auto mode](https://code.claude.com/docs/en/auto-mode-config.md)
- [Permission modes](https://code.claude.com/docs/en/permission-modes.md)
- [Settings](https://code.claude.com/docs/en/settings.md)
- [Environment variables](https://code.claude.com/docs/en/env-vars.md)
- [Permissions](https://code.claude.com/docs/en/permissions.md)
- [Server-managed settings](https://code.claude.com/docs/en/server-managed-settings.md)

## 相关页面



- [权限与模式](https://docs.passion8.cc/docs/claude-code/permissions): default、plan、auto、dontAsk、bypassPermissions 和 allow/ask/deny。
- [企业控制面与受管配置](https://docs.passion8.cc/docs/claude-code/enterprise-controls): Managed settings、MCP、Auto mode、analytics 和合规清单。
- [企业 rollout](https://docs.passion8.cc/docs/claude-code/enterprise-rollout): 把 Auto mode 放进 30/60/90 天推广节奏。

