Skip to content

Commit 8c861aa

Browse files
fix(codex): honor managed binary owner root
1 parent 160e368 commit 8c861aa

3 files changed

Lines changed: 66 additions & 32 deletions

File tree

‎extensions/codex/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { createCodexAppServerAgentHarness } from "./harness.js";
1616
import { buildCodexMediaUnderstandingProvider } from "./media-understanding-provider.js";
1717
import { readCodexPluginConfig } from "./src/app-server/config.js";
1818
import { createCodexAppServerConnectionHealthService } from "./src/app-server/connection-health.js";
19+
import { setManagedCodexPluginRoot } from "./src/app-server/managed-binary.js";
1920
import {
2021
CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
2122
CODEX_APP_SERVER_BINDING_NAMESPACE,
@@ -60,6 +61,9 @@ export default definePluginEntry({
6061
name: "Codex",
6162
description: "Codex app-server harness and native session supervision.",
6263
register(api) {
64+
// Bundled modules may execute from a shared dist chunk, so import.meta.url
65+
// cannot identify the owning plugin package or its pinned dependencies.
66+
setManagedCodexPluginRoot(api.rootDir);
6367
const resolveCurrentConfig = () =>
6468
api.runtime.config?.current ? (api.runtime.config.current() as OpenClawConfig) : undefined;
6569
const resolvePluginConfig = (resolveConfig: () => OpenClawConfig | undefined) => {

‎extensions/codex/src/app-server/managed-binary.test.ts‎

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import { mkdir, mkdtemp, realpath, writeFile } from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
5-
import { describe, expect, it, vi } from "vitest";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
66
import type { CodexAppServerStartOptions } from "./config.js";
77
import {
88
resolveManagedCodexAppServerStartOptions,
99
resolveManagedCodexNativeCommand,
10+
setManagedCodexPluginRoot,
1011
} from "./managed-binary.js";
1112

1213
function startOptions(
@@ -33,6 +34,8 @@ const MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND =
3334
"/Applications/ChatGPT.app/Contents/Resources/codex";
3435

3536
describe("managed Codex app-server binary", () => {
37+
afterEach(() => setManagedCodexPluginRoot(undefined));
38+
3639
it("resolves the platform-native artifact behind the managed npm launcher", () => {
3740
const packageJsonPath =
3841
"/repo/extensions/codex/node_modules/@openai/codex-darwin-arm64/package.json";
@@ -62,7 +65,7 @@ describe("managed Codex app-server binary", () => {
6265
).toBe(MACOS_DESKTOP_CHATGPT_APP_SERVER_COMMAND);
6366
});
6467

65-
it("leaves explicit command overrides unchanged", async () => {
68+
it("leaves explicit command overrides unchanged without probing managed paths", async () => {
6669
const explicitOptions = startOptions("config");
6770
const pathExists = vi.fn(async () => false);
6871

@@ -146,7 +149,7 @@ describe("managed Codex app-server binary", () => {
146149
});
147150
});
148151

149-
it("falls back to the plugin-local binary when neither desktop bundle exists", async () => {
152+
it("falls back to the source plugin-local binary when neither desktop bundle exists", async () => {
150153
const pluginRoot = path.join("/tmp", "openclaw", "extensions", "codex");
151154
const pluginLocalCommand = managedCommandPath(pluginRoot, "darwin");
152155
const pathExists = vi.fn(async (filePath: string) => filePath === pluginLocalCommand);
@@ -185,6 +188,50 @@ describe("managed Codex app-server binary", () => {
185188
});
186189
});
187190

191+
it("prefers the bundled plugin binary over a stale hoisted package binary", async () => {
192+
const installRoot = path.join("/tmp", "openclaw-package");
193+
const packageRoot = path.join(installRoot, "node_modules", "openclaw");
194+
const bundledPluginRoot = path.join(packageRoot, "dist", "extensions", "codex");
195+
const bundledCommand = managedCommandPath(bundledPluginRoot, "linux");
196+
const hoistedCommand = managedCommandPath(installRoot, "linux");
197+
const pathExists = vi.fn(
198+
async (filePath: string) => filePath === bundledCommand || filePath === hoistedCommand,
199+
);
200+
setManagedCodexPluginRoot(bundledPluginRoot);
201+
202+
await expect(
203+
resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
204+
platform: "linux",
205+
pathExists,
206+
}),
207+
).resolves.toEqual({
208+
...startOptions("managed"),
209+
command: bundledCommand,
210+
commandSource: "resolved-managed",
211+
managedFallbackCommandPaths: [hoistedCommand],
212+
});
213+
});
214+
215+
it("falls back to the hoisted package when the bundled plugin binary is absent", async () => {
216+
const installRoot = path.join("/tmp", "openclaw-package");
217+
const packageRoot = path.join(installRoot, "node_modules", "openclaw");
218+
const bundledPluginRoot = path.join(packageRoot, "dist", "extensions", "codex");
219+
const hoistedCommand = managedCommandPath(installRoot, "linux");
220+
const pathExists = vi.fn(async (filePath: string) => filePath === hoistedCommand);
221+
setManagedCodexPluginRoot(bundledPluginRoot);
222+
223+
await expect(
224+
resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
225+
platform: "linux",
226+
pathExists,
227+
}),
228+
).resolves.toEqual({
229+
...startOptions("managed"),
230+
command: hoistedCommand,
231+
commandSource: "resolved-managed",
232+
});
233+
});
234+
188235
it("finds Codex bins hoisted into an isolated npm project root", async () => {
189236
const projectRoot = path.join("/tmp", "state", "npm", "projects", "openclaw-codex-hash");
190237
const pluginRoot = path.join(projectRoot, "node_modules", "@openclaw", "codex");
@@ -204,7 +251,7 @@ describe("managed Codex app-server binary", () => {
204251
});
205252
});
206253

207-
it("finds Windows Codex shims hoisted into an isolated npm project root", async () => {
254+
it("finds a Windows codex.cmd shim in an isolated npm root using win32 paths", async () => {
208255
const projectRoot = path.win32.join(
209256
"C:\\",
210257
"Users",

‎extensions/codex/src/app-server/managed-binary.ts‎

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ import { MANAGED_CODEX_APP_SERVER_PACKAGE } from "./version.js";
1313

1414
const CODEX_APP_SERVER_MODULE_DIR = path.dirname(fileURLToPath(import.meta.url));
1515
const CODEX_PLUGIN_ROOT = resolveDefaultCodexPluginRoot(CODEX_APP_SERVER_MODULE_DIR);
16+
let registeredCodexPluginRoot: string | undefined;
1617
// ChatGPT.app is the current desktop owner; keep Codex.app as the legacy fallback.
1718
const MACOS_DESKTOP_CODEX_APP_SERVER_COMMANDS = [
1819
"/Applications/ChatGPT.app/Contents/Resources/codex",
1920
"/Applications/Codex.app/Contents/Resources/codex",
2021
] as const;
2122

22-
type ManagedCodexAppServerPaths = {
23-
commandPath: string;
24-
candidateCommandPaths: string[];
25-
};
26-
2723
type ResolveManagedCodexAppServerOptions = {
2824
platform?: NodeJS.Platform;
2925
pluginRoot?: string;
@@ -37,6 +33,11 @@ type ResolveManagedCodexNativeCommandOptions = {
3733
resolvePackageJson?: (packageName: string, root: string) => string | undefined;
3834
};
3935

36+
/** Records the process-stable plugin root prepared by OpenClaw's plugin loader. */
37+
export function setManagedCodexPluginRoot(pluginRoot: string | undefined): void {
38+
registeredCodexPluginRoot = pluginRoot;
39+
}
40+
4041
/** Rewrites managed stdio start options to point at an executable Codex binary path. */
4142
export async function resolveManagedCodexAppServerStartOptions(
4243
startOptions: CodexAppServerStartOptions,
@@ -47,14 +48,14 @@ export async function resolveManagedCodexAppServerStartOptions(
4748
}
4849

4950
const platform = options.platform ?? process.platform;
50-
const paths = resolveManagedCodexAppServerPaths({
51+
const candidateCommandPaths = resolveManagedCodexAppServerCommandCandidates(
52+
options.pluginRoot ?? registeredCodexPluginRoot ?? CODEX_PLUGIN_ROOT,
5153
platform,
52-
pluginRoot: options.pluginRoot,
53-
managedCommandOrder: startOptions.managedCommandOrder,
54-
});
54+
startOptions.managedCommandOrder ?? "package-first",
55+
);
5556
const pathExists = options.pathExists ?? commandPathExists;
5657
const commandPaths = await findManagedCodexAppServerCommandPaths({
57-
candidateCommandPaths: paths.candidateCommandPaths,
58+
candidateCommandPaths,
5859
pathExists,
5960
platform,
6061
});
@@ -178,24 +179,6 @@ function resolvePackageJsonFromRoot(packageName: string, root: string): string |
178179
}
179180
}
180181

181-
/** Returns the preferred and fallback managed Codex binary paths for a plugin root. */
182-
function resolveManagedCodexAppServerPaths(params: {
183-
platform?: NodeJS.Platform;
184-
pluginRoot?: string;
185-
managedCommandOrder?: CodexManagedCommandOrder;
186-
}): ManagedCodexAppServerPaths {
187-
const platform = params.platform ?? process.platform;
188-
const candidateCommandPaths = resolveManagedCodexAppServerCommandCandidates(
189-
params.pluginRoot ?? CODEX_PLUGIN_ROOT,
190-
platform,
191-
params.managedCommandOrder ?? "package-first",
192-
);
193-
return {
194-
commandPath: candidateCommandPaths[0] ?? "",
195-
candidateCommandPaths,
196-
};
197-
}
198-
199182
function resolveManagedCodexAppServerCommandCandidates(
200183
pluginRoot: string,
201184
platform: NodeJS.Platform,

0 commit comments

Comments
 (0)