【Bug已解决】codex: --full-auto still asks for approval / Full auto mode not skipping prompts --- CodeX 全自动模式仍提示审批解决方案
1. 问题描述
CodeX CLI 使用 --full-auto 后仍然提示审批权限:
bash
# --full-auto 仍提示
$ codex --full-auto "修改 src/index.js" --max-turns 10
# 仍然弹出: Approve write to src/index.js? [y/n]
# 或 --auto-approve 不生效
$ codex --auto-approve "task" --max-turns 10
# 仍然等待审批输入
# 或 CI/CD 中卡住
$ codex --print --full-auto "task" --max-turns 10
# CI 管道超时,卡在审批步骤
# 或 Docker 中不工作
$ docker run codex-env codex --full-auto "task"
# Error: Cannot prompt in non-interactive mode
这个问题在以下场景中特别常见:
- 配置覆盖命令行参数
- 无 TTY 环境
- 版本不支持 --full-auto
- 沙箱配置严格
- 环境变量冲突
- 参数顺序错误
2. 原因分析
原因分类表
| 原因分类 | 具体表现 | 占比 |
|---|---|---|
| 配置覆盖 | requireApproval | 约 35% |
| 无 TTY | CI/Docker | 约 25% |
| 版本不支持 | 旧版本 | 约 15% |
| 沙箱严格 | strict | 约 10% |
| 环境变量 | env 覆盖 | 约 10% |
| 参数冲突 | 多参数 | 约 5% |
3. 解决方案
方案一:检查配置覆盖(最推荐)
bash
# 步骤 1:检查配置
cat ~/.codex/config.json | grep -i "approve\|permission\|require"
# 步骤 2:删除冲突配置
python3 -c "
import json
with open('/root/.codex/config.json') as f:
config = json.load(f)
config.pop('requireApproval', None)
config.pop('interactiveApproval', None)
config['autoApprove'] = True
with open('/root/.codex/config.json', 'w') as f:
json.dump(config, f, indent=2)
"
# 步骤 3:验证
codex --full-auto "task" --max-turns 5
# 步骤 4:或检查项目级配置
cat .codex/config.json | grep -i "approve\|permission"
方案二:使用 --print 模式
bash
# 步骤 1:使用 --print 避免 TTY 依赖
codex --print --full-auto "task" --max-turns 10
# 步骤 2:CI/CD 中
codex --print --full-auto --no-stream "task" --max-turns 10 2>&1
# 步骤 3:或组合 --auto-approve
codex --print --auto-approve "task" --max-turns 10
# 步骤 4:验证
codex --print --full-auto "echo hello" --max-turns 1
方案三:配置沙箱自动审批
bash
# 步骤 1:在配置中设置
cat > .codex/config.json << 'EOF'
{
"model": "o1",
"sandbox": {
"enabled": true,
"autoApprove": true,
"allowedDirectories": ["./src", "./tests"]
}
}
EOF
# 步骤 2:验证 JSON
python3 -m json.tool .codex/config.json
# 步骤 3:测试
codex --print "修改 src/index.js" --max-turns 5
# 步骤 4:验证
codex --print "echo hello" --max-turns 1
方案四:使用环境变量
bash
# 步骤 1:设置环境变量
export CODEX_FULL_AUTO=true
export CODEX_AUTO_APPROVE=true
codex --print "task" --max-turns 10
# 步骤 2:CI/CD 中
# GitHub Actions: env: CODEX_FULL_AUTO=true
codex --print "task" --max-turns 10
# 步骤 3:永久设置
echo 'export CODEX_FULL_AUTO=true' >> ~/.bashrc
source ~/.bashrc
# 步骤 4:验证
codex --print "echo hello" --max-turns 1
方案五:检查 TTY
bash
# 步骤 1:检查 TTY
tty # 如果报 "not a tty"
# 步骤 2:CI/CD 中使用 --print
codex --print --full-auto "task" --max-turns 10
# 步骤 3:Docker 中使用 -it
docker run -it codex-env codex --full-auto "task"
# 步骤 4:或使用 script 命令
script -q /dev/null codex --full-auto "task"
方案六:更新版本
bash
# 步骤 1:检查版本
codex --version
# 步骤 2:更新
npm update -g @openai/codex
# 步骤 3:验证 --full-auto 支持
codex --help | grep -i "full-auto\|auto-approve"
# 步骤 4:测试
codex --print --full-auto "echo hello" --max-turns 1
4. 各方案对比总结
| 方案 | 适用场景 | 推荐指数 | 难度 |
|---|---|---|---|
| 方案一:检查配置 | 配置覆盖 | ⭐⭐⭐⭐⭐ | 低 |
| 方案二:--print | CI/CD | ⭐⭐⭐⭐⭐ | 低 |
| 方案三:配置沙箱 | 长期 | ⭐⭐⭐⭐⭐ | 低 |
| 方案四:环境变量 | CI/CD | ⭐⭐⭐⭐ | 低 |
| 方案五:TTY | 无 TTY | ⭐⭐⭐⭐⭐ | 低 |
| 方案六:更新 | 版本 | ⭐⭐⭐ | 低 |
5. 常见问题 FAQ
5.1 --full-auto 和 --auto-approve 的区别
--full-auto: 全自动模式(沙箱+自动审批)--auto-approve: 仅自动审批工具调用
5.2 CI/CD 中为什么卡住
无 TTY 环境无法显示审批提示,导致 CodeX 等待输入。
5.3 如何在 CI/CD 中使用
bash
codex --print --full-auto "task" --max-turns 10 2>&1
5.4 配置覆盖命令行参数
config.json 中的 requireApproval: true 会覆盖 --full-auto。
5.5 如何检查 TTY
bash
tty # 如果 "not a tty",说明无 TTY
5.6 CODEX_FULL_AUTO 是什么
环境变量,启用全自动模式。
5.7 sandbox.autoApprove 是什么
在配置中设置 "autoApprove": true,沙箱自动审批。
5.8 Docker 中如何使用
bash
docker run -it codex-env codex --full-auto "task"
# 或
docker run codex-env codex --print --full-auto "task"
5.9 如何验证自动审批
bash
codex --print --full-auto "echo hello" --max-turns 1
5.10 排查清单速查表
□ 1. cat config.json | grep approve 检查
□ 2. 删除 requireApproval 配置
□ 3. codex --print --full-auto "task"
□ 4. --no-stream CI/CD 稳定
□ 5. sandbox.autoApprove: true
□ 6. CODEX_FULL_AUTO=true 环境变量
□ 7. tty 检查 TTY 环境
□ 8. Docker: -it 或 --print
□ 9. script -q /dev/null 模拟 TTY
□ 10. npm update 更新版本
6. 总结
- 根本原因:--full-auto 不生效最常见原因是配置覆盖(35%)和无 TTY 环境(25%)
- 最佳实践 :使用
codex --print --full-auto "task" --max-turns 10组合 - 配置沙箱 :在 config.json 中设置
"autoApprove": true - 环境变量 :
export CODEX_FULL_AUTO=true或CODEX_AUTO_APPROVE=true - 最佳实践建议 :CI/CD 中使用
codex --print --full-auto --no-stream "task" --max-turns 10 2>&1
故障排查流程图
flowchart TD
A[--full-auto 不生效] --> B[cat config.json | grep approve]
B --> C{有 requireApproval?}
C -->|是| D[删除 requireApproval]
C -->|否| E[使用 --print 模式]
D --> F[设置 autoApprove: true]
F --> G[codex --print --full-auto "task"]
E --> G
G --> H{成功?}
H -->|是| I[✅ 问题解决]
H -->|否| j[检查 TTY]
j --> K[tty 命令]
K --> L{有 TTY?}
L -->|否| M[使用 --print]
L -->|是| N[检查环境变量]
M --> G
N --> O[env | grep CODEX]
O --> P{env 覆盖?}
P -->|是| Q[unset 冲突变量]
P -->|否| R[使用环境变量]
Q --> G
R --> S[CODEX_FULL_AUTO=true]
S --> G
G --> T{成功?}
T -->|是| I
T -->|否| U[配置沙箱]
U --> V[sandbox.autoApprove: true]
V --> G
G --> W{成功?}
W -->|是| I
W -->|否| X[更新版本]
X --> Y[npm update -g @openai/codex]
Y --> G
I --> Z[CI/CD: --print + --full-auto + --no-stream]
Z --> AA[✅ 长期方案]
