@jacks001314/codex-go 1.0.6 → 1.0.8

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.
package/README.md CHANGED
@@ -1,11 +1,11 @@
1
- # Codex Go
1
+ # Codex Go
2
2
 
3
- The latest audited alignment target is Rust `main` at `8c076942cb70fba8e5c4732c23f1faabc4aed484`; see [the 2026-10-18 alignment record](./update/plan_2026_10_18_round142.md).
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
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
5
 
6
6
  `codex_go` 是 Codex CLI 的 Go 实现,目标是逐步对齐 Rust 版 Codex CLI 的命令行行为、配置语义、认证、模型请求、TUI、app-server、MCP、插件、沙箱和会话能力。
7
7
 
8
- 当前仓库是迁移中的工程版本。开发时以 Rust 版本为行为基线,进度和待办以 [plan.md](./plan.md) 为准。
8
+ 当前仓库是迁移中的工程版本。开发时以 Rust 版本为行为基线,进度和待办以 [update/plan_2026_11_08_ledger.md](./update/plan_2026_11_08_ledger.md) 与 [parity/README.md](./parity/README.md) 为准。
9
9
 
10
10
  ## 环境要求
11
11
 
@@ -220,13 +220,13 @@ docs 设计和技术选型文档
220
220
  - 新功能优先放入已有功能包,避免无必要地新增目录。
221
221
  - 结构体参数和结构体返回值默认优先使用指针,简单标量按 Go 惯例传值。
222
222
  - 代码变更后至少运行相关包测试;模块收口时运行 `go list -buildvcs=false ./...` 和 `go test ./...`。
223
- - 迁移进度、验收项和工作日志维护在 [plan.md](./plan.md)。
223
+ - 迁移进度、验收项和工作日志维护在 [update/](./update/)(最新一轮见 [plan_2026_11_08_ledger.md](./update/plan_2026_11_08_ledger.md))。
224
224
 
225
225
  ## 参考文档
226
226
 
227
- - [plan.md](./plan.md):迁移进度和待办
228
- - [desgn.md](./desgn.md):迁移设计草案
229
- - [docs/tui_tech_selection.md](./docs/tui_tech_selection.md):TUI 技术选型
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
230
 
231
231
  ## 许可证
232
232
 
package/bin/codex.js CHANGED
@@ -1,15 +1,36 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { spawn } from "node:child_process";
4
- import { chmodSync, existsSync, realpathSync } from "node:fs";
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";
5
17
  import { createRequire } from "node:module";
6
18
  import { constants } from "node:os";
19
+ import os from "node:os";
7
20
  import path from "node:path";
8
21
  import { fileURLToPath } from "node:url";
9
22
 
23
+ import {
24
+ detectPackageManager,
25
+ managedByEnvVarFor,
26
+ updateCommandFor,
27
+ } from "./package-manager.js";
28
+
10
29
  const require = createRequire(import.meta.url);
11
30
  const scriptDir = path.dirname(fileURLToPath(import.meta.url));
12
31
  const codexPackageRoot = realpathSync(path.resolve(scriptDir, ".."));
32
+ const entrypointDir = path.dirname(path.resolve(process.argv[1]));
33
+ const packageManager = detectPackageManager({ codexPackageRoot, entrypointDir, scriptDir });
13
34
  const platformPackages = {
14
35
  "linux-x64": "@jacks001314/codex-go-linux-x64",
15
36
  "linux-arm64": "@jacks001314/codex-go-linux-arm64",
@@ -39,16 +60,9 @@ let packageJsonPath;
39
60
  try {
40
61
  packageJsonPath = require.resolve(`${platformPackage}/package.json`);
41
62
  } catch {
42
- const packageManager = detectPackageManager();
43
- const updateCommand =
44
- packageManager === "bun"
45
- ? "bun install -g @jacks001314/codex-go@latest"
46
- : packageManager === "pnpm"
47
- ? "pnpm add -g @jacks001314/codex-go@latest"
48
- : "npm install -g @jacks001314/codex-go@latest";
49
63
  console.error(
50
64
  `Missing optional dependency ${platformPackage}. ` +
51
- `Reinstall with: ${updateCommand}`,
65
+ `Reinstall with: ${updateCommandFor(packageManager)}`,
52
66
  );
53
67
  process.exit(1);
54
68
  }
@@ -65,76 +79,138 @@ if (!existsSync(executable)) {
65
79
  process.exit(1);
66
80
  }
67
81
 
68
- // The native binary must be executable. When a platform package was built or
69
- // packed on a host that does not preserve Unix mode bits (e.g. Windows), the
70
- // tarball ships the binary without the exec bit, causing EACCES on Linux/macOS.
71
- // Ensure it is executable before spawn (no-op on Windows, harmless elsewhere).
72
- try {
73
- chmodSync(executable, 0o755);
74
- } catch {}
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
+ }
75
96
 
76
- function isPnpmOwnedCodexInstall(nodeModulesDir) {
77
- if (!existsSync(path.join(nodeModulesDir, ".modules.yaml"))) return false;
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;
78
101
  try {
79
- return realpathSync(path.join(nodeModulesDir, "@jacks001314", "codex-go")) === codexPackageRoot;
102
+ chmodSync(file, executableMode);
103
+ } catch {
104
+ return false;
105
+ }
106
+ try {
107
+ accessSync(file, fsConstants.X_OK);
108
+ return true;
80
109
  } catch {
81
110
  return false;
82
111
  }
83
112
  }
84
113
 
85
- function detectPackageManager() {
86
- const entrypointDir = path.dirname(path.resolve(process.argv[1]));
87
- for (const startDir of new Set([codexPackageRoot, entrypointDir])) {
88
- const filesystemRoot = path.parse(startDir).root;
89
- for (let currentDir = startDir; currentDir !== filesystemRoot; currentDir = path.dirname(currentDir)) {
90
- if (isPnpmOwnedCodexInstall(path.join(currentDir, "node_modules"))) return "pnpm";
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 });
91
150
  }
92
- if (isPnpmOwnedCodexInstall(path.join(filesystemRoot, "node_modules"))) return "pnpm";
151
+ } catch {
152
+ rmSync(staging, { recursive: true, force: true });
153
+ return null;
93
154
  }
94
- const userAgent = process.env.npm_config_user_agent || "";
95
- const execPath = process.env.npm_execpath || "";
96
- if (
97
- /\bbun\//.test(userAgent) ||
98
- execPath.includes("bun") ||
99
- scriptDir.includes(".bun/install/global") ||
100
- scriptDir.includes(".bun\\install\\global")
101
- ) {
102
- return "bun";
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
+ }
103
172
  }
104
- return userAgent ? "npm" : null;
105
173
  }
106
174
 
107
- const packageManager = detectPackageManager();
108
- const packageManagerEnvVar =
109
- packageManager === "bun"
110
- ? "CODEX_MANAGED_BY_BUN"
111
- : packageManager === "pnpm"
112
- ? "CODEX_MANAGED_BY_PNPM"
113
- : "CODEX_MANAGED_BY_NPM";
175
+ const packageManagerEnvVar = managedByEnvVarFor(packageManager);
114
176
  const env = { ...process.env, CODEX_MANAGED_PACKAGE_ROOT: codexPackageRoot };
115
177
  delete env.CODEX_MANAGED_BY_NPM;
116
178
  delete env.CODEX_MANAGED_BY_BUN;
117
179
  delete env.CODEX_MANAGED_BY_PNPM;
180
+ delete env.CODEX_MANAGED_BY_VITE_PLUS;
118
181
  env[packageManagerEnvVar] = "1";
119
182
 
120
183
  // Replace Node so the native binary receives signals and exits directly.
121
184
  // Node exposes execve on Windows too, but calling it there is unsupported.
122
- if (process.platform !== "win32" && typeof process.execve === "function") {
185
+ if (executableReady && process.platform !== "win32" && typeof process.execve === "function") {
123
186
  // Node ignores SIGXFSZ, and ignored signals survive execve. Installing a
124
187
  // handler lets execve restore the default disposition in the native binary.
125
188
  process.on("SIGXFSZ", () => {});
126
- process.execve(executable, [executable, ...process.argv.slice(2)], env);
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
+ }
127
197
  }
128
198
 
129
199
  // Older Node versions and Windows need a child process. Spawn asynchronously
130
200
  // so Node can forward signals while the native binary is executing.
131
- const child = spawn(executable, process.argv.slice(2), {
201
+ const child = spawn(launchExecutable, process.argv.slice(2), {
132
202
  stdio: "inherit",
133
203
  env,
134
204
  });
135
205
 
136
206
  child.on("error", (error) => {
137
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
+ }
138
214
  process.exit(1);
139
215
  });
140
216
 
@@ -0,0 +1,115 @@
1
+ import { existsSync, readFileSync, realpathSync } from "node:fs";
2
+ import path from "node:path";
3
+
4
+ // CODEX_PACKAGE_NAME is the package this shim is published as. Keeping it in
5
+ // this module lets the shim and its tests use the same ownership path.
6
+ export const CODEX_PACKAGE_NAME = "@jacks001314/codex-go";
7
+
8
+ function sameRealPath(left, right) {
9
+ try {
10
+ return realpathSync(left) === realpathSync(right);
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
15
+
16
+ export function isPnpmOwnedCodexInstall(nodeModulesDir, codexPackageRoot, packageName = CODEX_PACKAGE_NAME) {
17
+ if (!existsSync(path.join(nodeModulesDir, ".modules.yaml"))) return false;
18
+ return sameRealPath(path.join(nodeModulesDir, ...packageName.split("/")), codexPackageRoot);
19
+ }
20
+
21
+ // Vite+ records the active global installation in
22
+ // packages/<package-name>.json. Older installs append a #-prefixed ID to the
23
+ // package directory; newer installs put the ID in a subdirectory.
24
+ export function isVitePlusOwnedCodexInstall(packagesDir, codexPackageRoot, packageName = CODEX_PACKAGE_NAME) {
25
+ if (path.basename(packagesDir) !== "packages") return false;
26
+ try {
27
+ const metadata = JSON.parse(
28
+ readFileSync(path.join(packagesDir, `${packageName}.json`), "utf8"),
29
+ );
30
+ if (metadata.name !== packageName) return false;
31
+ const installId = typeof metadata.installId === "string" ? metadata.installId : "";
32
+ const packageDir = path.join(packagesDir, ...packageName.split("/"));
33
+ const installDir = installId.startsWith("#")
34
+ ? `${packageDir}${installId}`
35
+ : path.join(packageDir, installId);
36
+ for (const nodeModulesDir of [
37
+ path.join(installDir, "lib", "node_modules"),
38
+ path.join(installDir, "node_modules"),
39
+ ]) {
40
+ if (sameRealPath(path.join(nodeModulesDir, ...packageName.split("/")), codexPackageRoot)) {
41
+ return true;
42
+ }
43
+ }
44
+ } catch {
45
+ // Missing or unreadable ownership metadata must not prevent Codex starting.
46
+ }
47
+ return false;
48
+ }
49
+
50
+ // detectPackageManager uses owning-directory metadata from the canonical
51
+ // package root and lexical entrypoint. Vite+ is checked before pnpm because
52
+ // both can expose the same package through ancestor node_modules layouts.
53
+ export function detectPackageManager({
54
+ codexPackageRoot,
55
+ entrypointDir,
56
+ packageName = CODEX_PACKAGE_NAME,
57
+ scriptDir,
58
+ env = process.env,
59
+ }) {
60
+ for (const startDir of new Set([codexPackageRoot, entrypointDir])) {
61
+ const filesystemRoot = path.parse(startDir).root;
62
+ for (
63
+ let currentDir = startDir;
64
+ currentDir !== filesystemRoot;
65
+ currentDir = path.dirname(currentDir)
66
+ ) {
67
+ if (isVitePlusOwnedCodexInstall(currentDir, codexPackageRoot, packageName)) {
68
+ return "vite-plus";
69
+ }
70
+ if (isPnpmOwnedCodexInstall(path.join(currentDir, "node_modules"), codexPackageRoot, packageName)) {
71
+ return "pnpm";
72
+ }
73
+ }
74
+ if (isPnpmOwnedCodexInstall(path.join(filesystemRoot, "node_modules"), codexPackageRoot, packageName)) {
75
+ return "pnpm";
76
+ }
77
+ }
78
+ const userAgent = env.npm_config_user_agent || "";
79
+ const execPath = env.npm_execpath || "";
80
+ if (
81
+ /\bbun\//.test(userAgent) ||
82
+ execPath.includes("bun") ||
83
+ scriptDir.includes(".bun/install/global") ||
84
+ scriptDir.includes(".bun\\install\\global")
85
+ ) {
86
+ return "bun";
87
+ }
88
+ return userAgent ? "npm" : null;
89
+ }
90
+
91
+ export function updateCommandFor(packageManager, packageName = CODEX_PACKAGE_NAME) {
92
+ switch (packageManager) {
93
+ case "bun":
94
+ return `bun install -g ${packageName}@latest`;
95
+ case "pnpm":
96
+ return `pnpm add -g ${packageName}@latest`;
97
+ case "vite-plus":
98
+ return `vp install -g ${packageName}@latest`;
99
+ default:
100
+ return `npm install -g ${packageName}@latest`;
101
+ }
102
+ }
103
+
104
+ export function managedByEnvVarFor(packageManager) {
105
+ switch (packageManager) {
106
+ case "bun":
107
+ return "CODEX_MANAGED_BY_BUN";
108
+ case "pnpm":
109
+ return "CODEX_MANAGED_BY_PNPM";
110
+ case "vite-plus":
111
+ return "CODEX_MANAGED_BY_VITE_PLUS";
112
+ default:
113
+ return "CODEX_MANAGED_BY_NPM";
114
+ }
115
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacks001314/codex-go",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "Codex CLI implemented in Go",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -11,18 +11,19 @@
11
11
  "node": ">=16"
12
12
  },
13
13
  "files": [
14
- "bin/codex.js"
14
+ "bin/codex.js",
15
+ "bin/package-manager.js"
15
16
  ],
16
17
  "repository": {
17
18
  "type": "git",
18
19
  "url": "git+https://github.com/jacks001314/codex_go.git"
19
20
  },
20
21
  "optionalDependencies": {
21
- "@jacks001314/codex-go-linux-x64": "1.0.6",
22
- "@jacks001314/codex-go-linux-arm64": "1.0.6",
23
- "@jacks001314/codex-go-darwin-x64": "1.0.6",
24
- "@jacks001314/codex-go-darwin-arm64": "1.0.6",
25
- "@jacks001314/codex-go-win32-x64": "1.0.6",
26
- "@jacks001314/codex-go-win32-arm64": "1.0.6"
22
+ "@jacks001314/codex-go-linux-x64": "1.0.8",
23
+ "@jacks001314/codex-go-linux-arm64": "1.0.8",
24
+ "@jacks001314/codex-go-darwin-x64": "1.0.8",
25
+ "@jacks001314/codex-go-darwin-arm64": "1.0.8",
26
+ "@jacks001314/codex-go-win32-x64": "1.0.8",
27
+ "@jacks001314/codex-go-win32-arm64": "1.0.8"
27
28
  }
28
29
  }