mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-25 11:55:47 -06:00
f94a7dc183
* feat(codex): add native session supervision * fix(codex): harden supervision integration * fix(codex): preserve locked harness ownership * fix(codex): fence native session archive * fix(codex): revalidate archive binding ownership * feat(codex): integrate supervision runtime * feat(sessions): preserve harness-owned execution * feat(sessions): persist harness ownership invariants * feat(gateway): enforce harness-owned sessions * feat(setup): enable detected Codex supervision * feat(mac): expose supervised Codex sessions * feat(ui): make Codex sessions actionable * docs(codex): document session supervision * test(codex): cover integration ownership * chore(i18n): refresh supervision inventories * fix(setup): finalize Codex activation atomically * test(codex): narrow binding store update * fix(sessions): preserve legacy model locks * test(macos): serialize Codex catalog fixtures * fix(sessions): preserve legacy lock admission * chore(i18n): reconcile supervision metadata * test(sessions): mark legacy lock fixture * fix(macos): drain final Codex catalog frame * docs: leave supervision note to release * style(macos): satisfy Codex catalog type length * chore: record session accessor seam owners * fix(macos): honor configured Codex supervision * fix(codex): preserve harness-owned model locks * fix(codex): satisfy supervision lint gates * chore(i18n): refresh native supervision inventory * fix(codex): align supervision validation contracts * fix(codex): close supervision boundary gaps * fix(codex): preserve supervision activation contracts * fix(codex): dispose standalone supervision runtime * fix(codex): pin supervised source connection * fix(plugins): bind delegated runs to exact session target * fix(codex): scope supervised sessions to configured agents * fix(codex): fingerprint effective supervision home * fix(codex): normalize supervision plugin policy * fix(codex): keep supervised bindings stable across upgrades * fix(codex): guard all supervised binding connections * fix(codex): preserve catalog filters and pending CAS identity * fix(codex): preserve supervision identity for diagnostics * fix(codex): bind uncertain commits to supervision connection * fix(codex): satisfy supervision type boundaries * fix(macos): reconcile current main validation * fix(codex): handle absent runtime config in supervision * fix(doctor): own local audio acceleration check * fix(codex): satisfy integration lint gates * fix(codex): satisfy lifecycle safety guards
229 lines
6.4 KiB
TypeScript
229 lines
6.4 KiB
TypeScript
// Codex tests cover transport stdio plugin behavior.
|
|
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { CodexAppServerStartOptions } from "./config.js";
|
|
import {
|
|
createStdioTransport,
|
|
resolveCodexAppServerDetachedMode,
|
|
resolveCodexAppServerSpawnEnv,
|
|
resolveCodexAppServerSpawnInvocation,
|
|
} from "./transport-stdio.js";
|
|
|
|
const spawnMock = vi.hoisted(() => vi.fn(() => ({ pid: 1234 })));
|
|
|
|
vi.mock("node:child_process", () => ({ spawn: spawnMock }));
|
|
|
|
const tempDirs: string[] = [];
|
|
|
|
async function createTempDir(): Promise<string> {
|
|
const dir = await mkdtemp(path.join(os.tmpdir(), "openclaw-codex-spawn-"));
|
|
tempDirs.push(dir);
|
|
return dir;
|
|
}
|
|
|
|
afterEach(async () => {
|
|
for (const dir of tempDirs.splice(0)) {
|
|
await rm(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
beforeEach(() => {
|
|
spawnMock.mockClear();
|
|
});
|
|
|
|
function startOptions(command: string): CodexAppServerStartOptions {
|
|
return {
|
|
transport: "stdio",
|
|
command,
|
|
args: ["app-server", "--listen", "stdio://"],
|
|
headers: {},
|
|
};
|
|
}
|
|
|
|
describe("resolveCodexAppServerSpawnInvocation", () => {
|
|
it("keeps non-Windows Codex app-server invocation unchanged", () => {
|
|
const resolved = resolveCodexAppServerSpawnInvocation(startOptions("codex"), {
|
|
platform: "darwin",
|
|
env: {},
|
|
execPath: "/usr/local/bin/node",
|
|
});
|
|
|
|
expect(resolved).toEqual({
|
|
command: "codex",
|
|
args: ["app-server", "--listen", "stdio://"],
|
|
shell: undefined,
|
|
windowsHide: undefined,
|
|
});
|
|
});
|
|
|
|
it("requires managed Codex commands to be resolved before spawn", () => {
|
|
expect(() =>
|
|
resolveCodexAppServerSpawnInvocation(
|
|
{
|
|
...startOptions("codex"),
|
|
commandSource: "managed",
|
|
},
|
|
{
|
|
platform: "darwin",
|
|
env: {},
|
|
execPath: "/usr/local/bin/node",
|
|
},
|
|
),
|
|
).toThrow("must be resolved before spawn");
|
|
});
|
|
|
|
it("resolves Windows npm .cmd Codex shims through Node instead of raw spawn", async () => {
|
|
const binDir = await createTempDir();
|
|
const entryPath = path.join(binDir, "node_modules", "@openai", "codex", "bin", "codex.js");
|
|
const shimPath = path.join(binDir, "codex.cmd");
|
|
await mkdir(path.dirname(entryPath), { recursive: true });
|
|
await writeFile(entryPath, "console.log('codex')\n", "utf8");
|
|
await writeFile(
|
|
shimPath,
|
|
'@ECHO off\r\n"%~dp0\\node_modules\\@openai\\codex\\bin\\codex.js" %*\r\n',
|
|
"utf8",
|
|
);
|
|
|
|
const resolved = resolveCodexAppServerSpawnInvocation(startOptions("codex"), {
|
|
platform: "win32",
|
|
env: { PATH: binDir, PATHEXT: ".CMD;.EXE;.BAT" },
|
|
execPath: "C:\\node\\node.exe",
|
|
});
|
|
|
|
expect(resolved).toEqual({
|
|
command: "C:\\node\\node.exe",
|
|
args: [entryPath, "app-server", "--listen", "stdio://"],
|
|
shell: undefined,
|
|
windowsHide: true,
|
|
});
|
|
});
|
|
|
|
it("rejects Windows Codex app-server commands that include inline script arguments", () => {
|
|
expect(() =>
|
|
resolveCodexAppServerSpawnInvocation(
|
|
startOptions(
|
|
"node C:\\Users\\me\\.openclaw\\npm\\node_modules\\@openai\\codex\\bin\\codex.js",
|
|
),
|
|
{
|
|
platform: "win32",
|
|
env: {},
|
|
execPath: "C:\\node\\node.exe",
|
|
},
|
|
),
|
|
).toThrow("Windows spawn command must be an executable path only");
|
|
});
|
|
});
|
|
|
|
describe("createStdioTransport", () => {
|
|
it("spawns a compatibility endpoint in its configured working directory", () => {
|
|
createStdioTransport({
|
|
...startOptions("codex"),
|
|
cwd: "/srv/codex-project",
|
|
});
|
|
|
|
expect(spawnMock).toHaveBeenCalledWith(
|
|
"codex",
|
|
["app-server", "--listen", "stdio://"],
|
|
expect.objectContaining({ cwd: "/srv/codex-project" }),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("resolveCodexAppServerSpawnEnv", () => {
|
|
it("applies configured env overrides before clearing denied env vars", () => {
|
|
expect({
|
|
...resolveCodexAppServerSpawnEnv(
|
|
{
|
|
env: {
|
|
OPENAI_API_KEY: "configured-openai-key",
|
|
KEEP: "override",
|
|
},
|
|
clearEnv: ["OPENAI_API_KEY", "CODEX_API_KEY", "MISSING"],
|
|
},
|
|
{
|
|
OPENAI_API_KEY: "parent-openai-key",
|
|
CODEX_API_KEY: "parent-codex-key",
|
|
KEEP: "parent",
|
|
},
|
|
),
|
|
}).toEqual({
|
|
KEEP: "override",
|
|
});
|
|
});
|
|
|
|
it("clears denied env vars case-insensitively on Windows", () => {
|
|
expect({
|
|
...resolveCodexAppServerSpawnEnv(
|
|
{
|
|
env: {
|
|
OpenAI_Api_Key: "configured-openai-key",
|
|
Other: "configured",
|
|
},
|
|
clearEnv: ["OPENAI_API_KEY", " CODEX_API_KEY ", ""],
|
|
},
|
|
{
|
|
Codex_Api_Key: "parent-codex-key",
|
|
KEEP: "parent",
|
|
},
|
|
"win32",
|
|
),
|
|
}).toEqual({
|
|
KEEP: "parent",
|
|
Other: "configured",
|
|
});
|
|
});
|
|
|
|
it("uses a null-prototype env map and ignores prototype-polluting keys", () => {
|
|
const overrides = Object.create(null) as Record<string, string | undefined>;
|
|
Object.defineProperty(overrides, "__proto__", {
|
|
value: "polluted",
|
|
enumerable: true,
|
|
});
|
|
Object.defineProperty(overrides, "constructor", {
|
|
value: "polluted",
|
|
enumerable: true,
|
|
});
|
|
Object.defineProperty(overrides, "prototype", {
|
|
value: "polluted",
|
|
enumerable: true,
|
|
});
|
|
overrides.SAFE = "1";
|
|
|
|
const env = resolveCodexAppServerSpawnEnv(
|
|
{
|
|
env: overrides as Record<string, string>,
|
|
},
|
|
{
|
|
BASE: "1",
|
|
},
|
|
);
|
|
|
|
expect(Object.getPrototypeOf(env)).toBeNull();
|
|
expect({ ...env }).toEqual({
|
|
BASE: "1",
|
|
SAFE: "1",
|
|
});
|
|
expect(Object.hasOwn(env, "__proto__")).toBe(false);
|
|
expect(Object.hasOwn(env, "constructor")).toBe(false);
|
|
expect(Object.hasOwn(env, "prototype")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("resolveCodexAppServerDetachedMode", () => {
|
|
it("detaches normal POSIX app-server processes", () => {
|
|
expect(resolveCodexAppServerDetachedMode({}, "darwin")).toBe(true);
|
|
});
|
|
|
|
it("keeps QA app-server processes in the gateway process group", () => {
|
|
expect(resolveCodexAppServerDetachedMode({ OPENCLAW_QA_PARENT_PID: "12345" }, "linux")).toBe(
|
|
false,
|
|
);
|
|
});
|
|
|
|
it("does not detach Windows app-server processes", () => {
|
|
expect(resolveCodexAppServerDetachedMode({}, "win32")).toBe(false);
|
|
});
|
|
});
|