Skip to content

fix(worker): 支持 Codex 单 bot 独立鉴权 - #906

Open
xiongz-c wants to merge 2 commits into
masterfrom
codex/codex-auth-sync
Open

fix(worker): 支持 Codex 单 bot 独立鉴权#906
xiongz-c wants to merge 2 commits into
masterfrom
codex/codex-auth-sync

Conversation

@xiongz-c

@xiongz-c xiongz-c commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

背景与根因

Codex bot 使用 per-bot CODEX_HOME 时,worker 此前会在每次冷启动无条件把全局 ~/.codex/auth.json 刷新到该目录,覆盖 bot 自己通过 codex login --with-api-key 写入的 API-key auth。

对 requires_openai_auth = true 的 OpenAI-compatible provider,Codex 会使用登录凭证并忽略 provider env_key,因此即使 OPENAI_API_KEY 已通过 per-bot env 正确传入,请求仍可能拿到错误的全局 ChatGPT 凭证并返回 401 INVALID_API_KEY。

改动

  • 新增 per-bot 配置 codexAuthSync: shared | isolated。
  • 缺省 shared 保持旧行为:非沙箱直接使用全局 ~/.codex;沙箱把 CODEX_HOME 重定向到 per-bot 目录,并在每次冷启动同步全局 auth。
  • isolated 无论是否启用沙箱都使用 <BOTMUX_HOME>/bots//codex,且完全不读取或复制全局 auth;已有 auth 保持不变,缺失时输出明确登录指引。
  • 将“per-bot home 重定向”与“OS 文件读写隔离”拆开:isolated 只改变 Codex 状态/凭证归属,sandbox 决定是否额外限制宿主文件访问。
  • wrapperCli 或不支持 home 重定向的 runtime 无法兑现 isolated 语义时拒绝启动,不静默回退到全局登录态。
  • 凭证目录与文件分别收紧为 0700/0600;拒绝 bot home、codex home、auth.json 符号链接及非普通文件,shared 刷新使用原子写入,日志不输出凭证内容。
  • 配置贯通 bots.json 解析、通用 /config 持久化、daemon → worker IPC、Dashboard GET/PUT 与聚合代理。Dashboard 将 Codex 登录凭证作为独立安全配置展示,不再从属于沙箱开关。
  • 核查 Linux bwrap 链路:per-bot env 由 PTY 子进程环境或 tmux pane 的 /usr/bin/env 前缀传给外层 bwrap,bwrap 不清空继承环境;更新过时注释并补 sibling bot 不串 key 的回归测试。

默认值与迁移

无需迁移即可升级:未配置 codexAuthSync 时按 shared 处理。

需要单 bot key 时设置 isolated,然后执行:

CODEX_HOME=<BOTMUX_HOME>/bots/<larkAppId>/codex codex login --with-api-key

sandbox: false 不再意味着必须共享登录态:shared 仍使用全局登录态;isolated 仍使用独立 CODEX_HOME,但不会提供 OS 文件访问隔离。

Dashboard

Codex 登录凭证

测试覆盖

  • shared 缺省兼容及全局 auth 冷启动刷新
  • isolated 在首次启动、daemon restart、suspend/resume 中保持 auth 字节不变
  • isolated 缺失凭证不复制全局 auth,并输出登录指引
  • sandbox=true / false 均覆盖 per-bot home 选择;非沙箱 isolated 明确重定向
  • 两个 bot 的 key/env 不交叉
  • secret 日志脱敏、0600 权限、符号链接与路径逃逸拒绝
  • bots.json、/config、IPC、Dashboard 读写链路
  • PTY/tmux 与 Linux bwrap per-bot env 传递边界

验证

  • pnpm build
  • pnpm exec vitest run --reporter=dot test/codex-auth-sync.test.ts test/codex-auth-sync-worker-wiring.test.ts test/read-isolation.test.ts test/slash-inject.test.ts test/bot-config-store.test.ts test/dashboard-bot-payload.test.ts test/dashboard-bot-defaults-layout.test.ts test/tmux-backend-env.test.ts
    • 8 files passed;194 tests passed;5 skipped
  • git diff --check

本地 daemon restart 因当前环境不存在 botmux 配置而在启动前退出,未创建配置或改动进程。未连接真实自定义 Responses endpoint,因此真实 provider 请求仍需在部署环境完成 isolated 登录后手动验收。

影响范围

  • Codex shared 用户行为不变。
  • Codex isolated 在 sandbox on/off、PTY/tmux、冷启动与恢复路径上使用同一 per-bot CODEX_HOME。
  • sandbox 仍单独控制文件访问;isolated 不是安全沙箱。
  • 其它 CLI 与公共 sandbox 包装逻辑不变。

@xiongz-c
xiongz-c requested a review from deepcoldy as a code owner August 17, 2026 13:32
@xiongz-c
xiongz-c force-pushed the codex/codex-auth-sync branch from b172d82 to 65353fe Compare August 18, 2026 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant