Skip to content

fix: 让 codex 自动配置遵循 $CODEX_HOME(本地 & ssh)#129

Merged
wxtsky merged 1 commit into
wxtsky:mainfrom
MatrixA:MatrixA/codex-home-env
Apr 25, 2026
Merged

fix: 让 codex 自动配置遵循 $CODEX_HOME(本地 & ssh)#129
wxtsky merged 1 commit into
wxtsky:mainfrom
MatrixA:MatrixA/codex-home-env

Conversation

@MatrixA
Copy link
Copy Markdown
Contributor

@MatrixA MatrixA commented Apr 25, 2026

背景

codex 自身解析配置目录的顺序是:先看 $CODEX_HOME,没有再退回 ~/.codex。但当前我们做 codex 自动配置时,无论本地还是 ssh,都直接拼了 ~/.codex/

  • 本地:ConfigInstallerhooks.json 写到 ~/.codex/hooks.json,并在 ~/.codex/config.toml 里改 codex_hooks = true
  • 远端:RemoteInstaller 里嵌入的 Python 脚本同样硬编码 home / ".codex"

对于把 CODEX_HOME 指到别处的用户,钩子写入的目录 codex 根本不会读 —— 安装看起来成功,运行时一个事件都不会触发。

改动

ConfigInstaller.swift

  • 新增 codexHome() 解析器:读 $CODEX_HOME,处理 ~ / ~/x 展开,空串和纯空白都视为未设置,回退到 ~/.codex
  • 新增 displayCodexPath(filename:):UI/日志里显示 \$CODEX_HOME/hooks.json~/.codex/hooks.json,跟实际写入位置保持一致。
  • CLIConfig 加了两个可选闭包 rootOverride / displayPathOverride,特例本地化在 codex 一条记录里,其它 CLI 完全不受影响。
  • installAll / verifyAndRepair / enableCodexHooksConfig 里原本拼 NSHomeDirectory() + "/.codex" 的三处都改成走 codexHome()

RemoteInstaller.swift

  • 远端 Python 脚本里加 _codex_home():同样的 $CODEX_HOME~/.codex 回退逻辑,install_codex 走它。
  • ssh 调用改走登录 shell:\"\${SHELL:-/bin/bash}\" -lc \"...\",让远端 ~/.zprofile / ~/.bash_profile 里 export 的 CODEX_HOME 能进到非交互会话。Python 脚本用 base64 编码再喂给 stdin,避开嵌套 heredoc 的引号问题。

测试

  • Tests/CodeIslandTests/CodexHomeTests.swift 覆盖 8 种情况:未设置 / 绝对路径 / ~/foo / 裸 ~ / 空串 / 纯空白 / display 名走 env / display 名回退。
  • 全量 swift test:197/197 通过,没有回归。

验证步骤

  1. 不设 CODEX_HOME,安装 hooks → ~/.codex/hooks.jsonconfig.toml 正常更新。
  2. launchctl setenv CODEX_HOME ~/codex-custom 后重启 app,再次安装 → ~/codex-custom/ 下生成两个文件,~/.codex/ 没被碰过。
  3. 远端机器在 ~/.zprofileexport CODEX_HOME=$HOME/codex-custom,触发远端安装 → ~/codex-custom/hooks.json 写入成功。

之前 codex 的 hooks 安装与 config.toml 改写都直接拼 ~/.codex/,
对自定义了 CODEX_HOME 的用户来说,钩子被写到了 codex 根本不会读的目录里。

- ConfigInstaller: 新增 codexHome() 解析器,按 $CODEX_HOME → ~/.codex
  顺序回退,并扩展 CLIConfig 让 codex 走自定义根目录,其他 CLI 不受影响。
- RemoteInstaller: 远端 Python 脚本同样读取 $CODEX_HOME;ssh 调用改走
  登录 shell("${SHELL:-/bin/bash}" -lc),让远端 ~/.zprofile /
  ~/.bash_profile 里导出的环境变量能进到非交互会话。
- 补 8 个解析器单测:未设置 / 绝对路径 / ~ 展开 / 空串 / 纯空白 等。
@wxtsky wxtsky merged commit 9346ff3 into wxtsky:main Apr 25, 2026
senshinya added a commit to senshinya/CodeIsland that referenced this pull request Apr 26, 2026
Two narrow fixes from upstream's v1.0.23 day, applied without the broader
HookServer / Buddy work:

- ConfigInstaller: honor $CODEX_HOME for hooks.json and config.toml writes
  instead of hardcoding ~/.codex/. Falls back to ~/.codex when the env var
  is unset, with `~`/`~/...` expansion. Codex CLIConfig now resolves
  configPath through a new optional rootOverride closure on CLIConfig
  (keeps the home-relative semantics for every other CLI). RemoteInstaller
  parts of the upstream commit are skipped — this fork has no SSH session
  feature. (upstream 9346ff3, wxtsky#129)

- TerminalActivator: append a System Events Accessibility fallback to
  Ghostty and Terminal.app activation so a window minimized to the dock
  is restored even when the terminal's own AppleScript dictionary fails
  (Ghostty issue wxtsky#84) or its `windows` collection doesn't include the
  minimized window (Terminal.app issue wxtsky#124, some macOS 14 cases). The
  upstream `tty` variable-rename (`set tty to ...` → `set targetTty`) is
  not applicable here — this fork uses a string-literal interpolation so
  the AppleScript-shadowing bug never manifests. (upstream ed7cb7e)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

2 participants