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
87 changes: 80 additions & 7 deletions apps/server/src/provider/CodexProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ import {
mapCodexModelCapabilities,
} from "./CodexProvider.ts";

const REMOTE_PLUGIN_SKILL = "remote-plugin:skill";
const OLD_CLI_HOME = "/home-old-cli";
const OFFLINE_ONCE_HOME = "/home-offline-once";

function makeFakeCodexAppServers() {
const helpers: Array<{
readonly codexHome: unknown;
readonly crash: Effect.Effect<void>;
readonly requests: Array<unknown>;
closed: boolean;
remotePluginsLoaded: boolean;
reconcileFailuresLeft: number;
}> = [];
const spawner = ChildProcessSpawner.make((command) =>
Effect.gen(function* () {
Expand All @@ -34,16 +40,22 @@ function makeFakeCodexAppServers() {
Effect.andThen(Queue.end(output)),
Effect.asVoid,
);
const codexHome =
command._tag === "StandardCommand" ? command.options.env?.CODEX_HOME : undefined;
const helper = {
codexHome: command._tag === "StandardCommand" ? command.options.env?.CODEX_HOME : undefined,
codexHome,
crash,
requests: [] as Array<unknown>,
closed: false,
remotePluginsLoaded: false,
reconcileFailuresLeft: codexHome === OFFLINE_ONCE_HOME ? 1 : 0,
};
helpers.push(helper);
yield* Effect.addFinalizer(() => Effect.sync(() => (helper.closed = true)));
const respond = (id: unknown, result: unknown) =>
Queue.offer(output, new TextEncoder().encode(`${JSON.stringify({ id, result })}\n`));
const respondError = (id: unknown, error: unknown) =>
Queue.offer(output, new TextEncoder().encode(`${JSON.stringify({ id, error })}\n`));
return ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(900_000_000 + helpers.length),
exitCode: Deferred.await(exited),
Expand Down Expand Up @@ -71,6 +83,24 @@ function makeFakeCodexAppServers() {
});
}
helper.requests.push(frame.params);
if (frame.method === "plugin/reconcile") {
if (helper.codexHome === OLD_CLI_HOME) {
return respondError(frame.id, {
code: -32600,
message: "Invalid request: unknown variant `plugin/reconcile`",
});
}
if (helper.reconcileFailuresLeft > 0) {
helper.reconcileFailuresLeft -= 1;
return respondError(frame.id, { code: -32603, message: "backend unreachable" });
}
helper.remotePluginsLoaded = true;
return respond(frame.id, {
changedPlugins: [],
failedMaterializationRemotePluginIds: [],
failedRemotePluginIds: [],
});
}
return respond(frame.id, {
data: (frame.params?.cwds ?? []).map((cwd) => ({
cwd,
Expand All @@ -83,6 +113,18 @@ function makeFakeCodexAppServers() {
description: "",
scope: "repo",
},
// Real Codex lists remote plugin skills only once that inventory is published.
...(helper.remotePluginsLoaded
? [
{
name: REMOTE_PLUGIN_SKILL,
path: "/codex-home/plugins/remote/SKILL.md",
enabled: true,
description: "",
scope: "user",
},
]
: []),
],
})),
});
Expand Down Expand Up @@ -114,34 +156,65 @@ it.effect("reuses one Codex skills helper per launch until it idles out", () =>
Effect.map((skills) => skills.map((skill) => skill.name)),
);

assert.deepStrictEqual(yield* read("/alpha"), ["skill-in-/alpha"]);
assert.deepStrictEqual(yield* read("/beta"), ["skill-in-/beta"]);
assert.deepStrictEqual(yield* read("/alpha"), ["skill-in-/alpha", REMOTE_PLUGIN_SKILL]);
assert.deepStrictEqual(yield* read("/beta"), ["skill-in-/beta", REMOTE_PLUGIN_SKILL]);
assert.strictEqual(helpers.length, 1);
assert.deepStrictEqual(helpers[0]!.requests, [
{},
{ cwds: ["/alpha"], forceReload: true },
{ cwds: ["/beta"], forceReload: true },
]);

// Another home is another launch identity; the previous helper retires at once.
assert.deepStrictEqual(yield* read("/alpha", "/home-b"), ["skill-in-/alpha"]);
assert.deepStrictEqual(yield* read("/alpha", "/home-b"), [
"skill-in-/alpha",
REMOTE_PLUGIN_SKILL,
]);
assert.strictEqual(helpers.length, 2);
assert.strictEqual(helpers[0]!.closed, true);
assert.strictEqual(helpers[1]!.codexHome, "/home-b");

// A helper that exits is replaced within the same read.
yield* helpers[1]!.crash;
assert.deepStrictEqual(yield* read("/beta", "/home-b"), ["skill-in-/beta"]);
assert.deepStrictEqual(yield* read("/beta", "/home-b"), [
"skill-in-/beta",
REMOTE_PLUGIN_SKILL,
]);
assert.strictEqual(helpers.length, 3);

yield* TestClock.adjust("29 seconds");
assert.strictEqual(helpers[2]!.closed, false);
yield* TestClock.adjust("1 second");
assert.strictEqual(helpers[2]!.closed, true);
assert.deepStrictEqual(yield* read("/alpha", "/home-b"), ["skill-in-/alpha"]);
assert.deepStrictEqual(yield* read("/alpha", "/home-b"), [
"skill-in-/alpha",
REMOTE_PLUGIN_SKILL,
]);
assert.strictEqual(helpers.length, 4);

// A CLI that rejects plugin/reconcile as unknown lists its skills and is not asked again.
assert.deepStrictEqual(yield* read("/alpha", OLD_CLI_HOME), ["skill-in-/alpha"]);
yield* TestClock.adjust("29 seconds");
assert.deepStrictEqual(yield* read("/alpha", OLD_CLI_HOME), ["skill-in-/alpha"]);
yield* TestClock.adjust("29 seconds");
assert.deepStrictEqual(yield* read("/alpha", OLD_CLI_HOME), ["skill-in-/alpha"]);
assert.strictEqual(helpers.length, 5);
const alphaRead = { cwds: ["/alpha"], forceReload: true };
assert.deepStrictEqual(helpers[4]!.requests, [{}, alphaRead, alphaRead, alphaRead]);

// A failed reconcile is tried again on a helper that reads keep alive.
assert.deepStrictEqual(yield* read("/alpha", OFFLINE_ONCE_HOME), ["skill-in-/alpha"]);
yield* TestClock.adjust("29 seconds");
assert.deepStrictEqual(yield* read("/alpha", OFFLINE_ONCE_HOME), ["skill-in-/alpha"]);
yield* TestClock.adjust("2 seconds");
assert.deepStrictEqual(yield* read("/alpha", OFFLINE_ONCE_HOME), [
"skill-in-/alpha",
REMOTE_PLUGIN_SKILL,
]);
assert.strictEqual(helpers.length, 6);

yield* Scope.close(providerScope, Exit.void);
assert.strictEqual(helpers[3]!.closed, true);
assert.strictEqual(helpers[5]!.closed, true);
}).pipe(Effect.provideService(HostProcess.Platform, "linux")),
);

Expand Down
66 changes: 63 additions & 3 deletions apps/server/src/provider/CodexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export const codexRoutesSpendNativeQuota = (
const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const;
const CODEX_CATALOG_REFRESH_TIMEOUT_MS = 3_000;
const CODEX_CONFIG_PROBE_TIMEOUT_MS = 3_000;
const CODEX_PLUGIN_RECONCILE_TIMEOUT_MS = 5_000;
// Codex answers a method it does not know with invalid request, not method not found.
const CODEX_UNSUPPORTED_REQUEST_CODES: ReadonlySet<number> = new Set([-32600, -32601]);
const CODEX_CATALOG_REFRESH_POLL = "100 millis" as const;
const CODEX_CLIENT_NAME = "T3 Code";
// Handshake plus account/models/skills requests can exceed the shared 10s probe budget.
Expand Down Expand Up @@ -603,6 +606,50 @@ const requestCodexChatGptModels = Effect.fn("requestCodexChatGptModels")(functio
);
});

/**
* A fresh app-server lists skills before it has loaded the account's remote plugins and
* sends no notification once it has. plugin/reconcile answers after publishing them.
* Best-effort: the local skills are still worth listing. False means a later try may help.
*/
const loadCodexRemotePlugins = Effect.fn("loadCodexRemotePlugins")(function* (
client: CodexClient.CodexAppServerClient["Service"],
deadlineMs = Infinity,
) {
return yield* withCodexDiscoveryDeadline(
client.request("plugin/reconcile", {}),
deadlineMs,
CODEX_PLUGIN_RECONCILE_TIMEOUT_MS,
).pipe(
Effect.map((result) => result?.failedMaterializationRemotePluginIds.length === 0),
Effect.catch((cause) => {
const unsupported =
cause._tag === "CodexAppServerRequestError" &&
CODEX_UNSUPPORTED_REQUEST_CODES.has(cause.code);
return Effect.logDebug("Codex plugin reconcile failed.", {
errorTag: cause._tag,
unsupported,
}).pipe(Effect.as(unsupported));
}),
);
});

/** Lists before and after reconcile, so one that outlasts the budget keeps the first answer. */
const requestCodexCatalogSkills = Effect.fn("requestCodexCatalogSkills")(function* (input: {
readonly client: CodexClient.CodexAppServerClient["Service"];
readonly cwd: string;
readonly deadlineMs: number;
}) {
const listSkills = withCodexDiscoveryDeadline(
input.client.request("skills/list", { cwds: [input.cwd] }),
input.deadlineMs,
);
const listed = yield* listSkills;
if (listed === undefined) return listed;
yield* loadCodexRemotePlugins(input.client, input.deadlineMs);
const reconciled = yield* listSkills.pipe(Effect.orElseSucceed(() => undefined));
return reconciled ?? listed;
});

/** Set originator at spawn; the pre-initialize refresh otherwise overwrites shared cache with plain-CLI models. */
export const codexAppServerEnvironment = (
environment: NodeJS.ProcessEnv | undefined,
Expand Down Expand Up @@ -770,7 +817,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
const deadlineMs = spawnedAtMs + input.probeTimeoutMs - CODEX_DISCOVERY_MARGIN_MS;
const [skillsResponse, models, { config, nativeUsage, rateLimits }] = yield* Effect.all(
[
withCodexDiscoveryDeadline(client.request("skills/list", { cwds: [input.cwd] }), deadlineMs),
requestCodexCatalogSkills({ client, cwd: input.cwd, deadlineMs }),
accountResponse.account?.type === "chatgpt"
? requestCodexChatGptModels({
client,
Expand Down Expand Up @@ -875,6 +922,7 @@ export const probeCodexSkillsForCwd = Effect.fn("probeCodexSkillsForCwd")(functi
...input,
environment: providerProbeEnvironment(input.environment ?? process.env),
});
yield* loadCodexRemotePlugins(client);
const skillsResponse = yield* client.request("skills/list", { cwds: [input.cwd] });
return parseCodexSkillsListResponse(skillsResponse, input.cwd);
});
Expand All @@ -893,7 +941,15 @@ export const makeCodexSkillsProbe = Effect.fn("makeCodexSkillsProbe")(function*
readonly environment: NodeJS.ProcessEnv;
}) =>
withCodexAppServerClient({ ...launch, cwd: process.cwd() }).pipe(
Effect.map(({ client }) => client),
Effect.flatMap(({ client }) =>
// Remembered once it works; a failed attempt may run again on a later read.
loadCodexRemotePlugins(client).pipe(
Effect.cachedWithTTL((exit) =>
Exit.isSuccess(exit) && exit.value ? Duration.infinity : CODEX_SKILLS_HELPER_IDLE_TTL,
),
Effect.map((loadRemotePlugins) => ({ client, loadRemotePlugins })),
),
),
),
idleTimeToLive: CODEX_SKILLS_HELPER_IDLE_TTL,
});
Expand All @@ -915,7 +971,11 @@ export const makeCodexSkillsProbe = Effect.fn("makeCodexSkillsProbe")(function*
);
yield* Effect.forEach(stale, (key) => RcMap.invalidate(helpers, key), { discard: true });
const skillsResponse = yield* RcMap.get(helpers, launch).pipe(
Effect.flatMap((client) => client.request("skills/list", { cwds: [cwd], forceReload: true })),
Effect.flatMap(({ client, loadRemotePlugins }) =>
loadRemotePlugins.pipe(
Effect.andThen(client.request("skills/list", { cwds: [cwd], forceReload: true })),
),
),
Effect.scoped,
// RcMap keeps a failed spawn, and a dead helper keeps failing; drop it so the retry respawns.
Effect.onError(() => RcMap.invalidate(helpers, launch)),
Expand Down
Loading
Loading