diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index a70ea15a73570..4e8f3f49e34f2 100644 --- a/apps/server/src/provider/CodexProvider.test.ts +++ b/apps/server/src/provider/CodexProvider.test.ts @@ -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; readonly requests: Array; closed: boolean; + remotePluginsLoaded: boolean; + reconcileFailuresLeft: number; }> = []; const spawner = ChildProcessSpawner.make((command) => Effect.gen(function* () { @@ -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, 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), @@ -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, @@ -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", + }, + ] + : []), ], })), }); @@ -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")), ); diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 63154f4e99499..6fc6e5a2f2086 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -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 = 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. @@ -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, @@ -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, @@ -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); }); @@ -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, }); @@ -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)), diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index dcbadc9d49aaa..e9dfb9c93b914 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -349,6 +349,51 @@ function mockHandle(result: { stdout: string; stderr: string; code: number }) { }); } +/** A Codex app-server whose replies come from `answer`; a request it ignores stays unanswered. */ +function scriptedCodexAppServer( + answer: ( + request: { readonly id: number; readonly method: string }, + reply: (result: unknown) => Effect.Effect, + ) => Effect.Effect, +) { + return ChildProcessSpawner.make(() => + Effect.gen(function* () { + const scope = yield* Effect.scope; + const output = yield* Queue.unbounded(); + const decoder = new TextDecoder(); + let buffered = ""; + const handle = (line: string) => { + const { id, method } = JSON.parse(line) as { id?: number; method?: string }; + if (id === undefined || method === undefined) return Effect.void; + return answer({ id, method }, (result) => + Queue.offer(output, encoder.encode(`${JSON.stringify({ id, result })}\n`)).pipe( + Effect.asVoid, + ), + ).pipe(Scope.provide(scope)); + }; + return ChildProcessSpawner.makeHandle({ + ...mockHandle({ stdout: "", stderr: "", code: 0 }), + exitCode: Effect.never, + isRunning: Effect.succeed(true), + stdin: Sink.forEach((chunk: Uint8Array) => { + buffered += decoder.decode(chunk, { stream: true }); + const lines = buffered.split("\n"); + buffered = lines.pop() ?? ""; + return Effect.forEach(lines.filter(Boolean), handle, { discard: true }); + }), + stdout: Stream.fromQueue(output), + }); + }), + ); +} + +const codexInitializeResult = { + userAgent: "codex_cli_rs/0.167.0", + codexHome: "/nonexistent-codex-home", + platformFamily: "unix", + platformOs: "linux", +}; + function layerMockSpawner( handler: (args: ReadonlyArray) => { stdout: string; @@ -922,68 +967,35 @@ it.layer( it.effect("shows Codex usage that answers after three seconds inside the probe budget", () => Effect.gen(function* () { const usageRequested = yield* Deferred.make(); - const spawner = ChildProcessSpawner.make(() => - Effect.gen(function* () { - const scope = yield* Effect.scope; - const output = yield* Queue.unbounded(); - const send = (message: unknown) => - Queue.offer(output, encoder.encode(`${JSON.stringify(message)}\n`)); - const results: Record = { - initialize: { - userAgent: "codex_cli_rs/0.167.0", - codexHome: "/nonexistent-codex-home", - platformFamily: "unix", - platformOs: "linux", - }, - "account/read": { - account: { type: "chatgpt", email: "test@example.com", planType: "pro" }, - requiresOpenaiAuth: true, - }, - "config/read": { config: {}, origins: {} }, - "skills/list": { data: [] }, - "model/list": { data: [] }, - }; - const answer = (line: string) => { - const { id, method } = JSON.parse(line) as { id?: number; method?: string }; - if (id === undefined || method === undefined) return Effect.void; - if (method !== "account/rateLimits/read") { - return send({ id, result: results[method] ?? {} }); - } - return Deferred.succeed(usageRequested, undefined).pipe( + const results: Record = { + initialize: codexInitializeResult, + "account/read": { + account: { type: "chatgpt", email: "test@example.com", planType: "pro" }, + requiresOpenaiAuth: true, + }, + "config/read": { config: {}, origins: {} }, + "skills/list": { data: [] }, + "model/list": { data: [] }, + }; + const spawner = scriptedCodexAppServer(({ method }, reply) => + method !== "account/rateLimits/read" + ? reply(results[method] ?? {}) + : Deferred.succeed(usageRequested, undefined).pipe( Effect.andThen(Effect.sleep("4 seconds")), Effect.andThen( - send({ - id, - result: { - rateLimits: { - limitId: "codex", - planType: "pro", - primary: { usedPercent: 42, windowDurationMins: 300, resetsAt: 2e9 }, - }, - rateLimitsByLimitId: null, - rateLimitResetCredits: null, - accountId: null, + reply({ + rateLimits: { + limitId: "codex", + planType: "pro", + primary: { usedPercent: 42, windowDurationMins: 300, resetsAt: 2e9 }, }, + rateLimitsByLimitId: null, + rateLimitResetCredits: null, + accountId: null, }), ), - Effect.forkIn(scope), - ); - }; - const decoder = new TextDecoder(); - let buffered = ""; - return ChildProcessSpawner.makeHandle({ - ...mockHandle({ stdout: "", stderr: "", code: 0 }), - exitCode: Effect.never, - isRunning: Effect.succeed(true), - stdin: Sink.forEach((chunk: Uint8Array) => { - buffered += decoder.decode(chunk, { stream: true }); - const lines = buffered.split("\n"); - buffered = lines.pop() ?? ""; - return Effect.forEach(lines.filter(Boolean), answer, { discard: true }); - }), - stdout: Stream.fromQueue(output), - }); - }), + Effect.forkScoped, + ), ); const fiber = yield* checkCodexProviderStatus(defaultCodexSettings).pipe( Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), @@ -999,6 +1011,60 @@ it.layer( }), ); + it.effect("keeps Codex skills when plugin reconcile outlasts the discovery budget", () => + Effect.gen(function* () { + const accountRequested = yield* Deferred.make(); + const reconcileRequested = yield* Deferred.make(); + const results: Record = { + initialize: codexInitializeResult, + "account/read": { account: { type: "apiKey" }, requiresOpenaiAuth: false }, + "config/read": { config: {}, origins: {} }, + "skills/list": { + data: [ + { + cwd: "/", + errors: [], + skills: [ + { + name: "local", + path: "/local/SKILL.md", + enabled: true, + description: "", + scope: "user", + }, + ], + }, + ], + }, + "model/list": { data: [] }, + }; + const spawner = scriptedCodexAppServer(({ method }, reply) => { + if (method === "plugin/reconcile") return Deferred.succeed(reconcileRequested, undefined); + if (method !== "account/read") return reply(results[method] ?? {}); + // A slow account read leaves discovery less time than a reconcile may take. + return Deferred.succeed(accountRequested, undefined).pipe( + Effect.andThen(Effect.sleep("16 seconds")), + Effect.andThen(reply(results[method])), + Effect.forkScoped, + ); + }); + const fiber = yield* checkCodexProviderStatus(defaultCodexSettings).pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.forkChild, + ); + yield* Deferred.await(accountRequested); + yield* TestClock.adjust("16 seconds"); + yield* TestClock.adjust("4 seconds"); + const status = yield* Fiber.join(fiber); + + assert.isTrue(yield* Deferred.isDone(reconcileRequested)); + assert.deepStrictEqual( + status.skills.map((skill) => skill.name), + ["local"], + ); + }), + ); + it.effect("does not run --version when the Codex executable cannot start", () => Effect.gen(function* () { if ((yield* HostProcess.Platform) === "win32") return;