@jacks001314/codex-go 1.0.9 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +239 -238
  2. package/bin/codex.js +238 -233
  3. package/package.json +7 -7
package/README.md CHANGED
@@ -1,238 +1,239 @@
1
- # Codex Go
2
-
3
- The latest audited alignment target is Rust `main` at `9018f3a3b021082ef186ddd6503c0b4fd7a26260`; see [the 2026-11-11 sync record](./update/plan_2026_11_11.md).
4
- [parity.json](./parity.json) records the current SDK and implementation evidence. Candidate baseline, commit, domain, and contract state live under [parity/](./parity/); only `parity/baseline.json` with `certificationReady: true` denotes a fully certified release.
5
-
6
- `codex_go` 是 Codex CLI 的 Go 实现,目标是逐步对齐 Rust 版 Codex CLI 的命令行行为、配置语义、认证、模型请求、TUI、app-server、MCP、插件、沙箱和会话能力。
7
-
8
- 当前仓库是迁移中的工程版本。开发时以 Rust 版本为行为基线,进度和待办以 [update/plan_2026_11_08_ledger.md](./update/plan_2026_11_08_ledger.md) 与 [parity/README.md](./parity/README.md) 为准。
9
-
10
- ## 环境要求
11
-
12
- - Go 1.26.2+
13
- - 推荐使用 Go toolchain 1.26.2
14
- - Windows、Linux 或 macOS
15
-
16
- 模块信息见 [go.mod](./go.mod)。
17
-
18
- ## 快速开始
19
-
20
- 构建 CLI:
21
-
22
- ```powershell
23
- go build -o .\bin\codex.exe .\cmd\codex
24
- ```
25
-
26
- 直接运行:
27
-
28
- ```powershell
29
- go run .\cmd\codex -- "解释当前目录的代码结构"
30
- ```
31
-
32
- 非交互执行:
33
-
34
- ```powershell
35
- go run .\cmd\codex exec "用一句话总结这个项目"
36
- ```
37
-
38
- 登录状态:
39
-
40
- ```powershell
41
- go run .\cmd\codex login status
42
- ```
43
-
44
- 使用 API key 登录时从 stdin 读取密钥:
45
-
46
- ```powershell
47
- $env:OPENAI_API_KEY | go run .\cmd\codex login --with-api-key
48
- ```
49
-
50
- ## 常用命令
51
-
52
- ```text
53
- codex [OPTIONS] [PROMPT] 启动交互式 TUI,或用 PROMPT 开始会话
54
- codex exec [OPTIONS] [PROMPT] 非交互执行
55
- codex review [OPTIONS] 代码审查
56
- codex login 登录 ChatGPT
57
- codex login --with-api-key 从 stdin 保存 API key
58
- codex login status 查看登录状态
59
- codex logout 退出登录
60
- codex features list 查看功能开关
61
- codex mcp ... 管理 MCP server 配置和登录
62
- codex plugin ... 管理插件和 marketplace
63
- codex app-server ... 启动或管理 app-server
64
- codex sandbox -- COMMAND 在权限配置下运行命令
65
- codex doctor 环境诊断
66
- codex completion [SHELL] 生成 shell completion
67
- ```
68
-
69
- 根级常用选项:
70
-
71
- ```text
72
- -c, --config KEY=VALUE 覆盖配置
73
- --enable FEATURE 启用功能开关
74
- --disable FEATURE 禁用功能开关
75
- -m, --model MODEL 指定模型
76
- -p, --profile PROFILE 使用配置 profile
77
- -C, --cd DIR 指定工作目录
78
- --add-dir DIR 增加可访问目录
79
- --sandbox PROFILE 指定沙箱策略
80
- --ask-for-approval POLICY 指定审批策略
81
- ```
82
-
83
- ## 测试与检查
84
-
85
- 包加载检查:
86
-
87
- ```powershell
88
- go list -buildvcs=false ./...
89
- ```
90
-
91
- 运行全部测试:
92
-
93
- ```powershell
94
- go test ./...
95
- ```
96
-
97
- 如果本机 Go 缓存目录受限,可以临时指定缓存:
98
-
99
- ```powershell
100
- $env:GOCACHE = Join-Path $env:TEMP 'codex_go_gocache'
101
- go test ./...
102
- ```
103
-
104
- 格式化:
105
-
106
- ```powershell
107
- go fmt ./...
108
- ```
109
-
110
- ## 编译、安装与发布
111
-
112
- ### npm 安装
113
-
114
- 发布后的全局安装命令:
115
-
116
- ```powershell
117
- npm install -g @jacks001314/codex-go
118
- gcode --version
119
- ```
120
-
121
- npm 对外暴露的命令名为 `gcode`,避免与官方 `codex` 命令冲突;底层原生二进制仍名为 `codex`。主包会根据当前操作系统和 CPU 架构自动安装对应的原生二进制包。发布新版本时先构建七个 tarball,再按“六个平台包、主包”的顺序发布:
122
-
123
- ```powershell
124
- node .\npm\scripts\build-packages.mjs --version 0.1.0
125
- node .\npm\scripts\publish-packages.mjs --directory .\dist\v0.1.0\npm
126
- ```
127
-
128
- 如果 npm 账号要求发布时进行双因素认证,请传入认证器当前生成的 OTP:
129
-
130
- ```powershell
131
- node .\npm\scripts\publish-packages.mjs --directory .\dist\v0.1.0\npm --otp 123456
132
- ```
133
-
134
- 发布需要登录 npm 账号 `jacks001314`;预发布可追加 `--tag next`。CI 发布应使用启用了 `Bypass 2FA` 的 granular access token,避免交互式 OTP 过期。
135
-
136
- 编译当前平台的开发版本:
137
-
138
- ```powershell
139
- .\scripts\build.ps1
140
- ```
141
-
142
- Windows builds also write `codex-command-runner.exe` and
143
- `codex-windows-sandbox-setup.exe` under `bin\codex-resources`. NPM platform
144
- packages use the official `vendor/<target-triple>/bin` layout with a sibling
145
- `codex-resources` directory.
146
-
147
- 编译指定版本和目标平台:
148
-
149
- ```powershell
150
- .\scripts\build.ps1 -Version 0.1.0 -GOOS linux -GOARCH amd64 -CGO off
151
- ```
152
-
153
- 从当前源码安装。Windows 默认安装到 `%LOCALAPPDATA%\Programs\CodexGo\bin`,
154
- 其他系统默认安装到 `~/.local/bin`:
155
-
156
- ```powershell
157
- .\scripts\install.ps1 -Version 0.1.0
158
- ```
159
-
160
- 也可以指定安装目录:
161
-
162
- ```powershell
163
- .\scripts\install.ps1 -InstallDir C:\Tools\CodexGo -Force
164
- ```
165
-
166
- 生成 Windows、Linux 和 macOS 的 amd64/arm64 发布包及 SHA-256 校验清单:
167
-
168
- ```powershell
169
- .\scripts\release.ps1 -Version 0.1.0
170
- ```
171
-
172
- 发布产物默认写入 `dist/v<版本号>`。可用 `-Targets windows/amd64,linux/amd64`
173
- 限制目标平台,或用 `-SkipTests` 跳过发布前的全包编译检查。
174
-
175
- Linux、macOS、WSL 或其他 POSIX Shell 环境可以使用对应的 Shell 脚本:
176
-
177
- ```sh
178
- ./scripts/build.sh --version 0.1.0
179
- ./scripts/install.sh --version 0.1.0
180
- ./scripts/release.sh --version 0.1.0
181
- ```
182
-
183
- 指定发布目标:
184
-
185
- ```sh
186
- ./scripts/release.sh --version 0.1.0 \
187
- --targets 'linux/amd64 linux/arm64 darwin/arm64'
188
- ```
189
-
190
- ## 目录结构
191
-
192
- ```text
193
- cmd/codex 可执行入口
194
- app 顶层命令分发和运行时拼装
195
- cli 命令行解析、参数校验、dispatch alias
196
- config 配置加载、profile、feature override、权限配置
197
- auth 登录凭据、auth.json、OAuth/API key/access token
198
- model 模型 provider、Responses 请求/流式事件、模型 catalog
199
- codexapi OpenAI/Codex API 客户端辅助
200
- turn agent loop、tool dispatch、turn runtime
201
- tool shell/apply_patch/MCP/tool_search/agent 工具运行时
202
- exec codex exec/review 的非交互运行
203
- appserver JSON-RPC app-server、thread/turn/runtime 服务
204
- mcp MCP client/server、OAuth、resource、tool runtime
205
- tui 终端 UI 状态、渲染、组件和 Bubble Tea adapter
206
- session 会话存储、resume/fork/archive/delete
207
- rollout rollout JSONL 记录和恢复
208
- sandbox 权限 profile、Linux/Windows 沙箱、执行计划
209
- review review 目标解析和 git diff 采集
210
- doctor 环境诊断
211
- plugin 插件 manifest、marketplace、安装流程
212
- agent 多 agent graph、身份、registry、工具
213
- utils 路径、JSON、ANSI、LRU、截断等共享工具
214
- docs 设计和技术选型文档
215
- ```
216
-
217
- ## 开发约定
218
-
219
- - 以 Rust 版 Codex 的行为和协议形状为基线。
220
- - 新功能优先放入已有功能包,避免无必要地新增目录。
221
- - 结构体参数和结构体返回值默认优先使用指针,简单标量按 Go 惯例传值。
222
- - 代码变更后至少运行相关包测试;模块收口时运行 `go list -buildvcs=false ./...` 和 `go test ./...`。
223
- - 迁移进度、验收项和工作日志维护在 [update/](./update/)(最新一轮见 [plan_2026_11_08_ledger.md](./update/plan_2026_11_08_ledger.md))。
224
-
225
- ## 参考文档
226
-
227
- - [update/plan_2026_11_08_ledger.md](./update/plan_2026_11_08_ledger.md):迁移进度和待办(当前对齐轮次)
228
- - [parity/README.md](./parity/README.md):对齐控制面(基线、提交账本、领域与契约)
229
- - [tui_alignment.md](./tui_alignment.md):TUI 对齐与选型记录
230
-
231
- ## 许可证
232
-
233
- 本项目基于上游 [OpenAI Codex](https://github.com/openai/codex) 的 Apache-2.0 许可实现,同样以 Apache License 2.0 发布。
234
-
235
- - [LICENSE](./LICENSE):Apache License, Version 2.0 全文
236
- - [NOTICE](./NOTICE):上游来源、修改声明,以及发布包内附带的 ripgrep、bubblewrap、Opus 等第三方组件许可信息
237
-
238
- 分发二进制或 npm 包时必须一并携带 `LICENSE` 与 `NOTICE`(`scripts/release.ps1`、`scripts/release.sh`、`npm/scripts/build-packages.mjs` 已自动打包)。
1
+ # Codex Go
2
+
3
+ The latest audited alignment target is Rust `main` at `a394ff9cf66e9c17258077e8f8295178ca0c2025`; see [the
4
+ 2026-11-16 sync record](./update/plan_2026_11_16.md).
5
+ [parity.json](./parity.json) records the current SDK and implementation evidence. Candidate baseline, commit, domain, and contract state live under [parity/](./parity/); only `parity/baseline.json` with `certificationReady: true` denotes a fully certified release.
6
+
7
+ `codex_go` 是 Codex CLI 的 Go 实现,目标是逐步对齐 Rust 版 Codex CLI 的命令行行为、配置语义、认证、模型请求、TUI、app-server、MCP、插件、沙箱和会话能力。
8
+
9
+ 当前仓库是迁移中的工程版本。开发时以 Rust 版本为行为基线,进度和待办以 [update/plan_2026_11_08_ledger.md](./update/plan_2026_11_08_ledger.md) 与 [parity/README.md](./parity/README.md) 为准。
10
+
11
+ ## 环境要求
12
+
13
+ - Go 1.26.2+
14
+ - 推荐使用 Go toolchain 1.26.2
15
+ - Windows、Linux 或 macOS
16
+
17
+ 模块信息见 [go.mod](./go.mod)。
18
+
19
+ ## 快速开始
20
+
21
+ 构建 CLI:
22
+
23
+ ```powershell
24
+ go build -o .\bin\codex.exe .\cmd\codex
25
+ ```
26
+
27
+ 直接运行:
28
+
29
+ ```powershell
30
+ go run .\cmd\codex -- "解释当前目录的代码结构"
31
+ ```
32
+
33
+ 非交互执行:
34
+
35
+ ```powershell
36
+ go run .\cmd\codex exec "用一句话总结这个项目"
37
+ ```
38
+
39
+ 登录状态:
40
+
41
+ ```powershell
42
+ go run .\cmd\codex login status
43
+ ```
44
+
45
+ 使用 API key 登录时从 stdin 读取密钥:
46
+
47
+ ```powershell
48
+ $env:OPENAI_API_KEY | go run .\cmd\codex login --with-api-key
49
+ ```
50
+
51
+ ## 常用命令
52
+
53
+ ```text
54
+ codex [OPTIONS] [PROMPT] 启动交互式 TUI,或用 PROMPT 开始会话
55
+ codex exec [OPTIONS] [PROMPT] 非交互执行
56
+ codex review [OPTIONS] 代码审查
57
+ codex login 登录 ChatGPT
58
+ codex login --with-api-key 从 stdin 保存 API key
59
+ codex login status 查看登录状态
60
+ codex logout 退出登录
61
+ codex features list 查看功能开关
62
+ codex mcp ... 管理 MCP server 配置和登录
63
+ codex plugin ... 管理插件和 marketplace
64
+ codex app-server ... 启动或管理 app-server
65
+ codex sandbox -- COMMAND 在权限配置下运行命令
66
+ codex doctor 环境诊断
67
+ codex completion [SHELL] 生成 shell completion
68
+ ```
69
+
70
+ 根级常用选项:
71
+
72
+ ```text
73
+ -c, --config KEY=VALUE 覆盖配置
74
+ --enable FEATURE 启用功能开关
75
+ --disable FEATURE 禁用功能开关
76
+ -m, --model MODEL 指定模型
77
+ -p, --profile PROFILE 使用配置 profile
78
+ -C, --cd DIR 指定工作目录
79
+ --add-dir DIR 增加可访问目录
80
+ --sandbox PROFILE 指定沙箱策略
81
+ --ask-for-approval POLICY 指定审批策略
82
+ ```
83
+
84
+ ## 测试与检查
85
+
86
+ 包加载检查:
87
+
88
+ ```powershell
89
+ go list -buildvcs=false ./...
90
+ ```
91
+
92
+ 运行全部测试:
93
+
94
+ ```powershell
95
+ go test ./...
96
+ ```
97
+
98
+ 如果本机 Go 缓存目录受限,可以临时指定缓存:
99
+
100
+ ```powershell
101
+ $env:GOCACHE = Join-Path $env:TEMP 'codex_go_gocache'
102
+ go test ./...
103
+ ```
104
+
105
+ 格式化:
106
+
107
+ ```powershell
108
+ go fmt ./...
109
+ ```
110
+
111
+ ## 编译、安装与发布
112
+
113
+ ### npm 安装
114
+
115
+ 发布后的全局安装命令:
116
+
117
+ ```powershell
118
+ npm install -g @jacks001314/codex-go
119
+ gcode --version
120
+ ```
121
+
122
+ npm 对外暴露的命令名为 `gcode`,避免与官方 `codex` 命令冲突;底层原生二进制仍名为 `codex`。主包会根据当前操作系统和 CPU 架构自动安装对应的原生二进制包。发布新版本时先构建七个 tarball,再按“六个平台包、主包”的顺序发布:
123
+
124
+ ```powershell
125
+ node .\npm\scripts\build-packages.mjs --version 0.1.0
126
+ node .\npm\scripts\publish-packages.mjs --directory .\dist\v0.1.0\npm
127
+ ```
128
+
129
+ 如果 npm 账号要求发布时进行双因素认证,请传入认证器当前生成的 OTP:
130
+
131
+ ```powershell
132
+ node .\npm\scripts\publish-packages.mjs --directory .\dist\v0.1.0\npm --otp 123456
133
+ ```
134
+
135
+ 发布需要登录 npm 账号 `jacks001314`;预发布可追加 `--tag next`。CI 发布应使用启用了 `Bypass 2FA` 的 granular access token,避免交互式 OTP 过期。
136
+
137
+ 编译当前平台的开发版本:
138
+
139
+ ```powershell
140
+ .\scripts\build.ps1
141
+ ```
142
+
143
+ Windows builds also write `codex-command-runner.exe` and
144
+ `codex-windows-sandbox-setup.exe` under `bin\codex-resources`. NPM platform
145
+ packages use the official `vendor/<target-triple>/bin` layout with a sibling
146
+ `codex-resources` directory.
147
+
148
+ 编译指定版本和目标平台:
149
+
150
+ ```powershell
151
+ .\scripts\build.ps1 -Version 0.1.0 -GOOS linux -GOARCH amd64 -CGO off
152
+ ```
153
+
154
+ 从当前源码安装。Windows 默认安装到 `%LOCALAPPDATA%\Programs\CodexGo\bin`,
155
+ 其他系统默认安装到 `~/.local/bin`:
156
+
157
+ ```powershell
158
+ .\scripts\install.ps1 -Version 0.1.0
159
+ ```
160
+
161
+ 也可以指定安装目录:
162
+
163
+ ```powershell
164
+ .\scripts\install.ps1 -InstallDir C:\Tools\CodexGo -Force
165
+ ```
166
+
167
+ 生成 Windows、Linux 和 macOS 的 amd64/arm64 发布包及 SHA-256 校验清单:
168
+
169
+ ```powershell
170
+ .\scripts\release.ps1 -Version 0.1.0
171
+ ```
172
+
173
+ 发布产物默认写入 `dist/v<版本号>`。可用 `-Targets windows/amd64,linux/amd64`
174
+ 限制目标平台,或用 `-SkipTests` 跳过发布前的全包编译检查。
175
+
176
+ Linux、macOS、WSL 或其他 POSIX Shell 环境可以使用对应的 Shell 脚本:
177
+
178
+ ```sh
179
+ ./scripts/build.sh --version 0.1.0
180
+ ./scripts/install.sh --version 0.1.0
181
+ ./scripts/release.sh --version 0.1.0
182
+ ```
183
+
184
+ 指定发布目标:
185
+
186
+ ```sh
187
+ ./scripts/release.sh --version 0.1.0 \
188
+ --targets 'linux/amd64 linux/arm64 darwin/arm64'
189
+ ```
190
+
191
+ ## 目录结构
192
+
193
+ ```text
194
+ cmd/codex 可执行入口
195
+ app 顶层命令分发和运行时拼装
196
+ cli 命令行解析、参数校验、dispatch alias
197
+ config 配置加载、profile、feature override、权限配置
198
+ auth 登录凭据、auth.json、OAuth/API key/access token
199
+ model 模型 provider、Responses 请求/流式事件、模型 catalog
200
+ codexapi OpenAI/Codex API 客户端辅助
201
+ turn agent loop、tool dispatch、turn runtime
202
+ tool shell/apply_patch/MCP/tool_search/agent 工具运行时
203
+ exec codex exec/review 的非交互运行
204
+ appserver JSON-RPC app-server、thread/turn/runtime 服务
205
+ mcp MCP client/server、OAuth、resource、tool runtime
206
+ tui 终端 UI 状态、渲染、组件和 Bubble Tea adapter
207
+ session 会话存储、resume/fork/archive/delete
208
+ rollout rollout JSONL 记录和恢复
209
+ sandbox 权限 profile、Linux/Windows 沙箱、执行计划
210
+ review review 目标解析和 git diff 采集
211
+ doctor 环境诊断
212
+ plugin 插件 manifest、marketplace、安装流程
213
+ agent 多 agent graph、身份、registry、工具
214
+ utils 路径、JSON、ANSI、LRU、截断等共享工具
215
+ docs 设计和技术选型文档
216
+ ```
217
+
218
+ ## 开发约定
219
+
220
+ - 以 Rust 版 Codex 的行为和协议形状为基线。
221
+ - 新功能优先放入已有功能包,避免无必要地新增目录。
222
+ - 结构体参数和结构体返回值默认优先使用指针,简单标量按 Go 惯例传值。
223
+ - 代码变更后至少运行相关包测试;模块收口时运行 `go list -buildvcs=false ./...` 和 `go test ./...`。
224
+ - 迁移进度、验收项和工作日志维护在 [update/](./update/)(最新一轮见 [plan_2026_11_08_ledger.md](./update/plan_2026_11_08_ledger.md))。
225
+
226
+ ## 参考文档
227
+
228
+ - [update/plan_2026_11_08_ledger.md](./update/plan_2026_11_08_ledger.md):迁移进度和待办(当前对齐轮次)
229
+ - [parity/README.md](./parity/README.md):对齐控制面(基线、提交账本、领域与契约)
230
+ - [tui_alignment.md](./tui_alignment.md):TUI 对齐与选型记录
231
+
232
+ ## 许可证
233
+
234
+ 本项目基于上游 [OpenAI Codex](https://github.com/openai/codex) 的 Apache-2.0 许可实现,同样以 Apache License 2.0 发布。
235
+
236
+ - [LICENSE](./LICENSE):Apache License, Version 2.0 全文
237
+ - [NOTICE](./NOTICE):上游来源、修改声明,以及发布包内附带的 ripgrep、bubblewrap、Opus 等第三方组件许可信息
238
+
239
+ 分发二进制或 npm 包时必须一并携带 `LICENSE` 与 `NOTICE`(`scripts/release.ps1`、`scripts/release.sh`、`npm/scripts/build-packages.mjs` 已自动打包)。
package/bin/codex.js CHANGED
@@ -1,233 +1,238 @@
1
- #!/usr/bin/env node
2
-
3
- import { spawn } from "node:child_process";
4
- import { createHash } from "node:crypto";
5
- import {
6
- accessSync,
7
- chmodSync,
8
- constants as fsConstants,
9
- cpSync,
10
- existsSync,
11
- mkdirSync,
12
- realpathSync,
13
- renameSync,
14
- rmSync,
15
- statSync,
16
- } from "node:fs";
17
- import { createRequire } from "node:module";
18
- import { constants } from "node:os";
19
- import os from "node:os";
20
- import path from "node:path";
21
- import { fileURLToPath } from "node:url";
22
-
23
- import {
24
- detectPackageManager,
25
- managedByEnvVarFor,
26
- updateCommandFor,
27
- } from "./package-manager.js";
28
-
29
- const require = createRequire(import.meta.url);
30
- const scriptDir = path.dirname(fileURLToPath(import.meta.url));
31
- const codexPackageRoot = realpathSync(path.resolve(scriptDir, ".."));
32
- const entrypointDir = path.dirname(path.resolve(process.argv[1]));
33
- const packageManager = detectPackageManager({ codexPackageRoot, entrypointDir, scriptDir });
34
- const platformPackages = {
35
- "linux-x64": "@jacks001314/codex-go-linux-x64",
36
- "linux-arm64": "@jacks001314/codex-go-linux-arm64",
37
- "darwin-x64": "@jacks001314/codex-go-darwin-x64",
38
- "darwin-arm64": "@jacks001314/codex-go-darwin-arm64",
39
- "win32-x64": "@jacks001314/codex-go-win32-x64",
40
- "win32-arm64": "@jacks001314/codex-go-win32-arm64",
41
- };
42
- const targetTriples = {
43
- "linux-x64": "x86_64-unknown-linux-musl",
44
- "linux-arm64": "aarch64-unknown-linux-musl",
45
- "darwin-x64": "x86_64-apple-darwin",
46
- "darwin-arm64": "aarch64-apple-darwin",
47
- "win32-x64": "x86_64-pc-windows-msvc",
48
- "win32-arm64": "aarch64-pc-windows-msvc",
49
- };
50
-
51
- const target = `${process.platform}-${process.arch}`;
52
- const platformPackage = platformPackages[target];
53
- const targetTriple = targetTriples[target];
54
- if (!platformPackage || !targetTriple) {
55
- console.error(`Unsupported platform: ${process.platform} (${process.arch})`);
56
- process.exit(1);
57
- }
58
-
59
- let packageJsonPath;
60
- try {
61
- packageJsonPath = require.resolve(`${platformPackage}/package.json`);
62
- } catch {
63
- console.error(
64
- `Missing optional dependency ${platformPackage}. ` +
65
- `Reinstall with: ${updateCommandFor(packageManager)}`,
66
- );
67
- process.exit(1);
68
- }
69
-
70
- const executable = path.join(
71
- path.dirname(packageJsonPath),
72
- "vendor",
73
- targetTriple,
74
- "bin",
75
- process.platform === "win32" ? "codex.exe" : "codex",
76
- );
77
- if (!existsSync(executable)) {
78
- console.error(`Codex executable is missing from ${platformPackage}: ${executable}`);
79
- process.exit(1);
80
- }
81
-
82
- const executableMode = 0o755;
83
- const executableNameFor = (platform) => (platform === "win32" ? "codex.exe" : "codex");
84
- const packageRoot = path.dirname(packageJsonPath);
85
-
86
- function platformExecutables(root, triple, platform) {
87
- const base = path.join(root, "vendor", triple);
88
- return [
89
- path.join(base, "bin", executableNameFor(platform)),
90
- path.join(base, "bin", platform === "win32" ? "codex-code-mode-host.exe" : "codex-code-mode-host"),
91
- path.join(base, "bin", platform === "win32" ? "codex-exec-server.exe" : "codex-exec-server"),
92
- path.join(base, "codex-path", platform === "win32" ? "rg.exe" : "rg"),
93
- path.join(base, "codex-resources", "bwrap"),
94
- ];
95
- }
96
-
97
- // chmodExecutable reports whether file is executable after applying the mode.
98
- // Windows ignores Unix mode bits, so it is always considered ready there.
99
- function chmodExecutable(file) {
100
- if (process.platform === "win32") return true;
101
- try {
102
- chmodSync(file, executableMode);
103
- } catch {
104
- return false;
105
- }
106
- try {
107
- accessSync(file, fsConstants.X_OK);
108
- return true;
109
- } catch {
110
- return false;
111
- }
112
- }
113
-
114
- // privatePackageCopy mirrors the platform package into the user cache when the
115
- // installed copy cannot be made executable: a root-owned global install, a
116
- // read-only prefix, or a store that dropped the mode bits. The whole vendored
117
- // target is copied so the bundled code-mode host, rg, bwrap, and
118
- // codex-package.json still resolve next to the running binary.
119
- function privatePackageCopy(root, triple, binary) {
120
- const cacheRoot = process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache");
121
- const source = path.join(root, "vendor", triple);
122
- let stamp;
123
- try {
124
- const info = statSync(binary);
125
- stamp = `${realpathSync(source)}|${info.size}|${info.mtimeMs}`;
126
- } catch {
127
- return null;
128
- }
129
- const key = createHash("sha256").update(stamp).digest("hex").slice(0, 16);
130
- const destination = path.join(cacheRoot, "codex-go", key);
131
- const copiedRoot = path.join(destination, "vendor", triple);
132
- const copiedBinary = path.join(copiedRoot, "bin", executableNameFor(process.platform));
133
- if (existsSync(copiedBinary)) return copiedBinary;
134
-
135
- const staging = `${destination}.tmp-${process.pid}`;
136
- try {
137
- rmSync(staging, { recursive: true, force: true });
138
- mkdirSync(staging, { recursive: true });
139
- cpSync(source, path.join(staging, "vendor", triple), {
140
- recursive: true,
141
- preserveTimestamps: true,
142
- });
143
- for (const file of platformExecutables(staging, triple, process.platform)) {
144
- if (existsSync(file)) chmodExecutable(file);
145
- }
146
- try {
147
- renameSync(staging, destination);
148
- } catch {
149
- rmSync(staging, { recursive: true, force: true });
150
- }
151
- } catch {
152
- rmSync(staging, { recursive: true, force: true });
153
- return null;
154
- }
155
- return existsSync(copiedBinary) ? copiedBinary : null;
156
- }
157
-
158
- // The native binary must be executable. Platform tarballs packed on a host that
159
- // does not preserve Unix mode bits (Windows) ship it as 0644, and a root-owned
160
- // global install cannot be chmodded by the user. Repair in place when possible
161
- // and otherwise run an executable private copy with the same package layout.
162
- let launchExecutable = executable;
163
- let executableReady = true;
164
- if (process.platform !== "win32") {
165
- executableReady = chmodExecutable(launchExecutable);
166
- if (!executableReady) {
167
- const repaired = privatePackageCopy(packageRoot, targetTriple, executable);
168
- if (repaired) {
169
- launchExecutable = repaired;
170
- executableReady = true;
171
- }
172
- }
173
- }
174
-
175
- const packageManagerEnvVar = managedByEnvVarFor(packageManager);
176
- const env = { ...process.env, CODEX_MANAGED_PACKAGE_ROOT: codexPackageRoot };
177
- delete env.CODEX_MANAGED_BY_NPM;
178
- delete env.CODEX_MANAGED_BY_BUN;
179
- delete env.CODEX_MANAGED_BY_PNPM;
180
- delete env.CODEX_MANAGED_BY_VITE_PLUS;
181
- env[packageManagerEnvVar] = "1";
182
-
183
- // Replace Node so the native binary receives signals and exits directly.
184
- // Node exposes execve on Windows too, but calling it there is unsupported.
185
- if (executableReady && process.platform !== "win32" && typeof process.execve === "function") {
186
- // Node ignores SIGXFSZ, and ignored signals survive execve. Installing a
187
- // handler lets execve restore the default disposition in the native binary.
188
- process.on("SIGXFSZ", () => {});
189
- try {
190
- process.execve(launchExecutable, [launchExecutable, ...process.argv.slice(2)], env);
191
- } catch (error) {
192
- // execve does not return on success. A failure (for example a noexec mount)
193
- // falls through to spawn so the user sees a normal error instead of a
194
- // native abort.
195
- console.error(`Failed to exec Codex: ${error?.message ?? error}`);
196
- }
197
- }
198
-
199
- // Older Node versions and Windows need a child process. Spawn asynchronously
200
- // so Node can forward signals while the native binary is executing.
201
- const child = spawn(launchExecutable, process.argv.slice(2), {
202
- stdio: "inherit",
203
- env,
204
- });
205
-
206
- child.on("error", (error) => {
207
- console.error(`Failed to start Codex: ${error.message}`);
208
- if (error?.code === "EACCES") {
209
- console.error(`The Codex binary is not executable: ${launchExecutable}`);
210
- console.error(
211
- `Reinstall the package as the user who runs it, or fix the mode with: chmod +x ${launchExecutable}`,
212
- );
213
- }
214
- process.exit(1);
215
- });
216
-
217
- for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
218
- process.on(signal, () => {
219
- if (!child.killed) child.kill(signal);
220
- });
221
- }
222
-
223
- child.on("exit", (code, signal) => {
224
- if (signal) {
225
- // Rust #57352: preserve a failure exit code even if Node ignores the
226
- // re-emitted signal.
227
- process.exitCode = 128 + constants.signals[signal];
228
- process.removeAllListeners(signal);
229
- process.kill(process.pid, signal);
230
- } else {
231
- process.exit(code ?? 1);
232
- }
233
- });
1
+ #!/usr/bin/env node
2
+
3
+ import { spawn } from "node:child_process";
4
+ import { createHash } from "node:crypto";
5
+ import {
6
+ accessSync,
7
+ chmodSync,
8
+ constants as fsConstants,
9
+ cpSync,
10
+ existsSync,
11
+ mkdirSync,
12
+ realpathSync,
13
+ renameSync,
14
+ rmSync,
15
+ statSync,
16
+ } from "node:fs";
17
+ import { createRequire } from "node:module";
18
+ import { constants } from "node:os";
19
+ import os from "node:os";
20
+ import path from "node:path";
21
+ import { fileURLToPath } from "node:url";
22
+
23
+ import {
24
+ detectPackageManager,
25
+ managedByEnvVarFor,
26
+ updateCommandFor,
27
+ } from "./package-manager.js";
28
+
29
+ const require = createRequire(import.meta.url);
30
+ const scriptDir = path.dirname(fileURLToPath(import.meta.url));
31
+ const codexPackageRoot = realpathSync(path.resolve(scriptDir, ".."));
32
+ const entrypointDir = path.dirname(path.resolve(process.argv[1]));
33
+ const packageManager = detectPackageManager({ codexPackageRoot, entrypointDir, scriptDir });
34
+ const platformPackages = {
35
+ "linux-x64": "@jacks001314/codex-go-linux-x64",
36
+ "linux-arm64": "@jacks001314/codex-go-linux-arm64",
37
+ "darwin-x64": "@jacks001314/codex-go-darwin-x64",
38
+ "darwin-arm64": "@jacks001314/codex-go-darwin-arm64",
39
+ "win32-x64": "@jacks001314/codex-go-win32-x64",
40
+ "win32-arm64": "@jacks001314/codex-go-win32-arm64",
41
+ };
42
+ const targetTriples = {
43
+ "linux-x64": "x86_64-unknown-linux-musl",
44
+ "linux-arm64": "aarch64-unknown-linux-musl",
45
+ "darwin-x64": "x86_64-apple-darwin",
46
+ "darwin-arm64": "aarch64-apple-darwin",
47
+ "win32-x64": "x86_64-pc-windows-msvc",
48
+ "win32-arm64": "aarch64-pc-windows-msvc",
49
+ };
50
+
51
+ const target = `${process.platform}-${process.arch}`;
52
+ const platformPackage = platformPackages[target];
53
+ const targetTriple = targetTriples[target];
54
+ if (!platformPackage || !targetTriple) {
55
+ console.error(`Unsupported platform: ${process.platform} (${process.arch})`);
56
+ process.exit(1);
57
+ }
58
+
59
+ let packageJsonPath;
60
+ try {
61
+ packageJsonPath = require.resolve(`${platformPackage}/package.json`);
62
+ } catch {
63
+ console.error(
64
+ `Missing optional dependency ${platformPackage}. ` +
65
+ `Reinstall with: ${updateCommandFor(packageManager)}`,
66
+ );
67
+ process.exit(1);
68
+ }
69
+
70
+ const executable = path.join(
71
+ path.dirname(packageJsonPath),
72
+ "vendor",
73
+ targetTriple,
74
+ "bin",
75
+ process.platform === "win32" ? "codex.exe" : "codex",
76
+ );
77
+ if (!existsSync(executable)) {
78
+ console.error(`Codex executable is missing from ${platformPackage}: ${executable}`);
79
+ process.exit(1);
80
+ }
81
+
82
+ const executableMode = 0o755;
83
+ const executableNameFor = (platform) => (platform === "win32" ? "codex.exe" : "codex");
84
+ const packageRoot = path.dirname(packageJsonPath);
85
+
86
+ function platformExecutables(root, triple, platform) {
87
+ const base = path.join(root, "vendor", triple);
88
+ return [
89
+ path.join(base, "bin", executableNameFor(platform)),
90
+ path.join(base, "bin", platform === "win32" ? "codex-code-mode-host.exe" : "codex-code-mode-host"),
91
+ path.join(base, "bin", platform === "win32" ? "codex-exec-server.exe" : "codex-exec-server"),
92
+ path.join(base, "codex-path", platform === "win32" ? "rg.exe" : "rg"),
93
+ path.join(base, "codex-resources", "bwrap"),
94
+ ];
95
+ }
96
+
97
+ // chmodExecutable reports whether file is executable after applying the mode.
98
+ // Windows ignores Unix mode bits, so it is always considered ready there.
99
+ function chmodExecutable(file) {
100
+ if (process.platform === "win32") return true;
101
+ try {
102
+ chmodSync(file, executableMode);
103
+ } catch {
104
+ return false;
105
+ }
106
+ try {
107
+ accessSync(file, fsConstants.X_OK);
108
+ return true;
109
+ } catch {
110
+ return false;
111
+ }
112
+ }
113
+
114
+ // privatePackageCopy mirrors the platform package into the user cache when the
115
+ // installed copy cannot be made executable: a root-owned global install, a
116
+ // read-only prefix, or a store that dropped the mode bits. The whole vendored
117
+ // target is copied so the bundled code-mode host, rg, bwrap, and
118
+ // codex-package.json still resolve next to the running binary.
119
+ function privatePackageCopy(root, triple, binary) {
120
+ const cacheRoot = process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache");
121
+ const source = path.join(root, "vendor", triple);
122
+ let stamp;
123
+ try {
124
+ const info = statSync(binary);
125
+ stamp = `${realpathSync(source)}|${info.size}|${info.mtimeMs}`;
126
+ } catch {
127
+ return null;
128
+ }
129
+ const key = createHash("sha256").update(stamp).digest("hex").slice(0, 16);
130
+ const destination = path.join(cacheRoot, "codex-go", key);
131
+ const copiedRoot = path.join(destination, "vendor", triple);
132
+ const copiedBinary = path.join(copiedRoot, "bin", executableNameFor(process.platform));
133
+ if (existsSync(copiedBinary)) return copiedBinary;
134
+
135
+ const staging = `${destination}.tmp-${process.pid}`;
136
+ try {
137
+ rmSync(staging, { recursive: true, force: true });
138
+ mkdirSync(staging, { recursive: true });
139
+ cpSync(source, path.join(staging, "vendor", triple), {
140
+ recursive: true,
141
+ preserveTimestamps: true,
142
+ });
143
+ for (const file of platformExecutables(staging, triple, process.platform)) {
144
+ if (existsSync(file)) chmodExecutable(file);
145
+ }
146
+ try {
147
+ renameSync(staging, destination);
148
+ } catch {
149
+ rmSync(staging, { recursive: true, force: true });
150
+ }
151
+ } catch {
152
+ rmSync(staging, { recursive: true, force: true });
153
+ return null;
154
+ }
155
+ return existsSync(copiedBinary) ? copiedBinary : null;
156
+ }
157
+
158
+ // The native binary must be executable. Platform tarballs packed on a host that
159
+ // does not preserve Unix mode bits (Windows) ship it as 0644, and a root-owned
160
+ // global install cannot be chmodded by the user. Repair in place when possible
161
+ // and otherwise run an executable private copy with the same package layout.
162
+ let launchExecutable = executable;
163
+ let executableReady = true;
164
+ if (process.platform !== "win32") {
165
+ executableReady = chmodExecutable(launchExecutable);
166
+ if (!executableReady) {
167
+ const repaired = privatePackageCopy(packageRoot, targetTriple, executable);
168
+ if (repaired) {
169
+ launchExecutable = repaired;
170
+ executableReady = true;
171
+ }
172
+ }
173
+ }
174
+
175
+ const packageManagerEnvVar = managedByEnvVarFor(packageManager);
176
+ const env = { ...process.env, CODEX_MANAGED_PACKAGE_ROOT: codexPackageRoot };
177
+ delete env.CODEX_MANAGED_BY_NPM;
178
+ delete env.CODEX_MANAGED_BY_BUN;
179
+ delete env.CODEX_MANAGED_BY_PNPM;
180
+ delete env.CODEX_MANAGED_BY_VITE_PLUS;
181
+ env[packageManagerEnvVar] = "1";
182
+
183
+ // Replace Node so the native binary receives signals and exits directly.
184
+ // Node exposes execve on Windows too, but calling it there is unsupported.
185
+ if (executableReady && process.platform !== "win32" && typeof process.execve === "function") {
186
+ // Node ignores SIGXFSZ, and ignored signals survive execve. Installing a
187
+ // handler lets execve restore the default disposition in the native binary.
188
+ process.on("SIGXFSZ", () => {});
189
+ try {
190
+ process.execve(launchExecutable, [launchExecutable, ...process.argv.slice(2)], env);
191
+ } catch (error) {
192
+ // execve does not return on success. A failure (for example a noexec mount)
193
+ // falls through to spawn so the user sees a normal error instead of a
194
+ // native abort.
195
+ console.error(`Failed to exec Codex: ${error?.message ?? error}`);
196
+ }
197
+ }
198
+
199
+ // Older Node versions and Windows need a child process. Spawn asynchronously
200
+ // so Node can forward signals while the native binary is executing.
201
+ const child = spawn(launchExecutable, process.argv.slice(2), {
202
+ stdio: "inherit",
203
+ env,
204
+ });
205
+
206
+ child.on("error", (error) => {
207
+ console.error(`Failed to start Codex: ${error.message}`);
208
+ if (error?.code === "EACCES") {
209
+ console.error(`The Codex binary is not executable: ${launchExecutable}`);
210
+ console.error(
211
+ `Reinstall the package as the user who runs it, or fix the mode with: chmod +x ${launchExecutable}`,
212
+ );
213
+ }
214
+ process.exit(1);
215
+ });
216
+
217
+ for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
218
+ process.on(signal, () => {
219
+ // A successful kill only means the signal was sent, not that the child exited.
220
+ try {
221
+ child.kill(signal);
222
+ } catch {
223
+ // The child may have exited between receiving and forwarding the signal.
224
+ }
225
+ });
226
+ }
227
+
228
+ child.on("exit", (code, signal) => {
229
+ if (signal) {
230
+ // Rust #57352: preserve a failure exit code even if Node ignores the
231
+ // re-emitted signal.
232
+ process.exitCode = 128 + constants.signals[signal];
233
+ process.removeAllListeners(signal);
234
+ process.kill(process.pid, signal);
235
+ } else {
236
+ process.exit(code ?? 1);
237
+ }
238
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacks001314/codex-go",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "Codex CLI implemented in Go",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -19,11 +19,11 @@
19
19
  "url": "git+https://github.com/jacks001314/codex_go.git"
20
20
  },
21
21
  "optionalDependencies": {
22
- "@jacks001314/codex-go-linux-x64": "1.0.9",
23
- "@jacks001314/codex-go-linux-arm64": "1.0.9",
24
- "@jacks001314/codex-go-darwin-x64": "1.0.9",
25
- "@jacks001314/codex-go-darwin-arm64": "1.0.9",
26
- "@jacks001314/codex-go-win32-x64": "1.0.9",
27
- "@jacks001314/codex-go-win32-arm64": "1.0.9"
22
+ "@jacks001314/codex-go-linux-x64": "1.1.0",
23
+ "@jacks001314/codex-go-linux-arm64": "1.1.0",
24
+ "@jacks001314/codex-go-darwin-x64": "1.1.0",
25
+ "@jacks001314/codex-go-darwin-arm64": "1.1.0",
26
+ "@jacks001314/codex-go-win32-x64": "1.1.0",
27
+ "@jacks001314/codex-go-win32-arm64": "1.1.0"
28
28
  }
29
29
  }