Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ import {
resolveCodexComputerUse,
type CodexComputerUseAvailability,
} from "../../provider/CodexComputerUse.ts";
import { configureProviderComputerUse } from "../../provider/CodexComputerUseSetup.ts";
import {
buildCodexAdditionalContext,
buildCodexComputerUseInstructions,
Expand Down Expand Up @@ -2329,6 +2330,16 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
});
if (refused) return yield* refused;
yield* client.notify("initialized", undefined);
if (adapterOptions.enableComputerUse) {
yield* configureProviderComputerUse(
(resolvedRuntime?.config ?? adapterOptions.settings).homePath ||
environment.CODEX_HOME,
).pipe(
Effect.provideService(CodexClient.CodexAppServerClient, client),
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, pathService),
);
}
yield* Ref.set(initialized, true);
}),
);
Expand Down
138 changes: 130 additions & 8 deletions apps/server/src/provider/CodexComputerUse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Stream from "effect/Stream";
import { TestClock } from "effect/testing";
import { stringify } from "smol-toml";

import { resolveCodexComputerUse } from "./CodexComputerUse.ts";
import { configureProviderComputerUse, prepareComputerUseEdits } from "./CodexComputerUseSetup.ts";

const resources = "/custom/Acme.app/Contents/Resources";
const command = `${resources}/cua_node/bin/node_repl`;
Expand Down Expand Up @@ -41,6 +43,56 @@ const rpcError = new CodexErrors.CodexAppServerTransportError({
cause: "fixture",
});

it("prepares only the native Mac tool for a separate Codex home without replacing its settings", () => {
const source = configured();
const node = source.mcp_servers.node_repl;
const config = {
...source,
mcp_servers: {
node_repl: {
...node,
args: [],
environment_id: "local",
tool_timeout_sec: null,
env: {
...node.env,
CODEX_HOME: "/personal",
TOKEN: "private",
NODE_REPL_TRUSTED_SERVICES: JSON.stringify({ sky: "@oai/sky/service", browser: "other" }),
},
},
},
};
const target = { model_provider: "custom", mcp_servers: { unrelated: {} } };
const edits = prepareComputerUseEdits(config, target);
assert.deepEqual(
edits.map((edit) => edit.keyPath),
["plugins.computer-use@openai-bundled.enabled", "mcp_servers.node_repl"],
);
assert.deepEqual(edits[1]?.value, {
command,
enabled_tools: ["js"],
env: {
...node.env,
NODE_REPL_NODE_PATH: `${resources}/cua_node/bin/node`,
NODE_REPL_TRUSTED_CODE_PATHS: modules,
},
});
for (const conflict of [
{ features: { plugins: false } },
{ plugins: { "computer-use@openai-bundled": { enabled: false } } },
{ mcp_servers: { node_repl: {} } },
]) {
assert.throws(() => prepareComputerUseEdits(config, conflict));
}
assert.throws(() =>
prepareComputerUseEdits(
{ ...config, mcp_servers: { node_repl: { ...node, disabled_tools: ["js"] } } },
target,
),
);
});

const fixture = (
options: {
config?: unknown;
Expand All @@ -51,6 +103,8 @@ const fixture = (
missing?: string;
fail?: string;
platform?: NodeJS.Platform;
homePath?: string;
nativeConfig?: string;
request?: CodexClient.CodexAppServerClient["Service"]["raw"]["request"];
} = {},
) => {
Expand All @@ -63,6 +117,7 @@ const fixture = (
calls.push({ method, params });
if (options.request) return options.request(method, params);
if (options.fail === method) return Effect.fail(rpcError);
if (method === "config/batchWrite") return Effect.succeed({ status: "ok" });
if (method === "config/read")
return Effect.succeed({
config: options.config ?? configured(),
Expand All @@ -76,7 +131,11 @@ const fixture = (
if (method === "skills/list")
return Effect.succeed({
data: [
{ cwd: "/project", skills: options.skills ?? [], errors: options.skillErrors ?? [] },
{
cwd: options.homePath ? "/" : "/project",
skills: options.skills ?? [],
errors: options.skillErrors ?? [],
},
],
});
return Effect.die(`Unexpected RPC ${method}`);
Expand All @@ -100,13 +159,15 @@ const fixture = (
});
const absent = FileSystem.makeNoop({});
const fileSystem = FileSystem.makeNoop({
realPath: (path) => Effect.succeed(path),
access: (path) =>
Effect.sync(() => {
reads.push(path);
}).pipe(Effect.andThen(path === options.missing ? absent.access(path) : Effect.void)),
readFileString: (path) => {
reads.push(path);
if (path === options.missing) return absent.readFileString(path);
if (path === "/mac/.codex/config.toml") return Effect.succeed(options.nativeConfig ?? "");
if (path.endsWith("package.json"))
return Effect.succeed(
JSON.stringify({ name: "@oai/sky", exports: { "./service": "./service.js" } }),
Expand All @@ -116,15 +177,76 @@ const fixture = (
);
},
});
const resolve = resolveCodexComputerUse({ cwd: "/project" }).pipe(
Effect.provideService(CodexClient.CodexAppServerClient, client),
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provide(Path.layer),
Effect.provideService(HostProcess.Platform, options.platform ?? "darwin"),
);
return { resolve, calls, reads };
const provide = <A, E>(
effect: Effect.Effect<
A,
E,
CodexClient.CodexAppServerClient | FileSystem.FileSystem | Path.Path
>,
) =>
effect.pipe(
Effect.provideService(CodexClient.CodexAppServerClient, client),
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provide(Path.layer),
Effect.provideService(HostProcess.HomeDirectory, "/mac"),
Effect.provideService(HostProcess.Platform, options.platform ?? "darwin"),
);
return {
resolve: provide(resolveCodexComputerUse({ cwd: "/project" })),
configure: provide(configureProviderComputerUse(options.homePath)),
calls,
reads,
};
};

it.effect(
"provisions an isolated provider home globally without replacing overrides or writing from a project layer",
() =>
Effect.gen(function* () {
const file = "/providers/custom/config.toml";
const user = { name: { type: "user", file }, config: {}, version: "original" };
const custom = {
homePath: "/providers/custom",
config: {},
layers: [user],
nativeConfig: stringify(configured()),
};
const fresh = fixture(custom);
yield* fresh.configure;
assert.deepEqual(
fresh.calls
.filter((call) => call.method === "config/batchWrite")
.map((call) => call.params),
[
{
filePath: file,
expectedVersion: "original",
reloadUserConfig: true,
edits: prepareComputerUseEdits(configured(), {}),
},
],
);
for (const overrides of [
{ config: configured() },
{ config: { features: { plugins: false } } },
{ config: { mcp_servers: { node_repl: { enabled: false } } } },
{ requirements: { allowBrowserAndComputerUse: false } },
{
layers: [
user,
{ name: { type: "project", dotCodexFolder: "/.codex" }, config: {}, version: "other" },
],
},
{ nativeConfig: "invalid TOML" },
{ platform: "linux" as const },
]) {
const blocked = fixture({ ...custom, ...overrides });
yield* blocked.configure;
assert.isFalse(blocked.calls.some((call) => call.method === "config/batchWrite"));
}
}),
);

it.effect("locates configured native files from the selected effective home and cwd", () =>
Effect.gen(function* () {
const f = fixture();
Expand Down
24 changes: 15 additions & 9 deletions apps/server/src/provider/CodexComputerUse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,17 @@ const relevantSkill = (value: { name?: unknown; path?: unknown }) =>
(typeof value.name === "string" && skillNames.has(value.name)) ||
(typeof value.path === "string" && /(?:^|[/\\])computer-use[/\\]SKILL\.md$/.test(value.path));

export function computerUseSkillDisabled(config: Record<string, unknown>) {
const rules = record(config.skills).config;
return (
Array.isArray(rules) &&
rules.some((rule) => record(rule).enabled === false && relevantSkill(record(rule)))
);
}

export const resolveCodexComputerUse = Effect.fn("CodexComputerUse.resolve")(function* (input: {
readonly cwd: string | null;
readonly candidateConfig?: Readonly<Record<string, unknown>>;
}): Effect.fn.Return<
CodexComputerUseAvailability,
never,
Expand All @@ -61,9 +70,11 @@ export const resolveCodexComputerUse = Effect.fn("CodexComputerUse.resolve")(fun
const cwd = input.cwd ?? (yield* HostProcess.WorkingDirectory);

return yield* Effect.gen(function* () {
const { config } = yield* client.raw
.request("config/read", { cwd, includeLayers: false })
.pipe(Effect.flatMap(decodeConfig));
const config =
input.candidateConfig ??
(yield* client.raw
.request("config/read", { cwd, includeLayers: false })
.pipe(Effect.flatMap(decodeConfig))).config;
if (
record(config.features).plugins === false ||
record(record(config.plugins)[pluginId]).enabled !== true
Expand Down Expand Up @@ -109,12 +120,7 @@ export const resolveCodexComputerUse = Effect.fn("CodexComputerUse.resolve")(fun
!env.SKY_CUA_SERVICE_PATH.endsWith(".app")
)
return unavailable;
const rules = record(config.skills).config;
if (
Array.isArray(rules) &&
rules.some((rule) => record(rule).enabled === false && relevantSkill(record(rule)))
)
return unavailable;
if (computerUseSkillDisabled(config)) return unavailable;

const catalog = yield* client.raw
.request("skills/list", { cwds: [cwd] })
Expand Down
Loading
Loading