From 17705028e1c47f19d5d622b47fe600f27947a075 Mon Sep 17 00:00:00 2001 From: Couiz <11710548+Couiz@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:50:10 +0100 Subject: [PATCH 1/2] fix(server): load Codex remote plugin skills on the first read A freshly spawned Codex app-server answers skills/list before it has published the account's remote plugin inventory, so the skill picker missed skills from marketplace plugins. Await plugin/reconcile before the first skills/list on each fresh app-server: the reusable skills helper, the one-shot workspace probe, and the provider catalog read. The call is best-effort and capped. The catalog read keeps part of the discovery budget for skills/list, and the reusable helper retries a failed reconcile on a later read. Co-Authored-By: Claude Fable 5.2 --- .../server/src/provider/CodexProvider.test.ts | 85 ++++++++++++++++-- apps/server/src/provider/CodexProvider.ts | 55 +++++++++++- .../src/provider/ProviderRegistry.test.ts | 87 +++++++++++++++++++ 3 files changed, 217 insertions(+), 10 deletions(-) diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index a70ea15a73570..780889bdfe59a 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,21 @@ function makeFakeCodexAppServers() { }); } helper.requests.push(frame.params); + if (frame.method === "plugin/reconcile") { + if (helper.codexHome === OLD_CLI_HOME) { + return respondError(frame.id, { code: -32601, message: "Method not found" }); + } + 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 +110,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 +153,66 @@ 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"]); + // The first read on a fresh helper already includes remote plugin skills. + 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 without plugin/reconcile still lists the skills it has, 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..e8ced925f5896 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -139,6 +139,10 @@ 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; +// A slow reconcile must leave the skills read enough of the discovery budget to answer. +const CODEX_SKILLS_LIST_RESERVE_MS = 1_000; +const JSON_RPC_METHOD_NOT_FOUND = -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 +607,31 @@ 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) => + Effect.logDebug("Codex plugin reconcile failed.", { cause }).pipe( + Effect.as( + cause._tag === "CodexAppServerRequestError" && cause.code === JSON_RPC_METHOD_NOT_FOUND, + ), + ), + ), + ); +}); + /** 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 +799,14 @@ 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), + loadCodexRemotePlugins(client, deadlineMs - CODEX_SKILLS_LIST_RESERVE_MS).pipe( + Effect.andThen( + withCodexDiscoveryDeadline( + client.request("skills/list", { cwds: [input.cwd] }), + deadlineMs, + ), + ), + ), accountResponse.account?.type === "chatgpt" ? requestCodexChatGptModels({ client, @@ -875,6 +911,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 +930,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 +960,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 021c3ddb57039..94cb2ef738cc2 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -999,6 +999,93 @@ it.layer( }), ); + it.effect("lists Codex skills when plugin reconcile outlasts the discovery budget", () => + Effect.gen(function* () { + const accountRequested = yield* Deferred.make(); + const reconcileRequested = 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.170.0", + codexHome: "/nonexistent-codex-home", + platformFamily: "unix", + platformOs: "linux", + }, + "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 answer = (line: string) => { + const { id, method } = JSON.parse(line) as { id?: number; method?: string }; + if (id === undefined || method === undefined) return Effect.void; + // Never answered: the read after it must still get part of the budget. + if (method === "plugin/reconcile") { + return Deferred.succeed(reconcileRequested, undefined); + } + if (method !== "account/read") { + return send({ id, result: results[method] ?? {} }); + } + return Deferred.succeed(accountRequested, undefined).pipe( + Effect.andThen(Effect.sleep("16 seconds")), + Effect.andThen(send({ id, result: results[method] })), + 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), + }); + }), + ); + const fiber = yield* checkCodexProviderStatus(defaultCodexSettings).pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.forkChild, + ); + yield* Deferred.await(accountRequested); + yield* TestClock.adjust("16 seconds"); + yield* Deferred.await(reconcileRequested); + yield* TestClock.adjust("3 seconds"); + const status = yield* Fiber.join(fiber); + + 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; From 502056a375ed2e644d12aa65880dd985be9539b3 Mon Sep 17 00:00:00 2001 From: Couiz <11710548+Couiz@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:28:39 +0100 Subject: [PATCH 2/2] fix(server): keep listed Codex skills when plugin reconcile is slow The catalog read now lists skills before and after plugin/reconcile, so a reconcile that outlasts the discovery budget keeps the first answer instead of depending on a reserved second. Codex rejects a method it does not know as an invalid request, so that answer is remembered as unsupported too. The reconcile failure log carries the error tag only. The catalog deadline test asserts on the reconcile request and the listed skill instead of waiting on a milestone, and shares its scripted app-server with the usage test beside it. Co-Authored-By: Claude Fable 5.2 --- .../server/src/provider/CodexProvider.test.ts | 8 +- apps/server/src/provider/CodexProvider.ts | 47 ++-- .../src/provider/ProviderRegistry.test.ts | 223 ++++++++---------- 3 files changed, 135 insertions(+), 143 deletions(-) diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index 780889bdfe59a..4e8f3f49e34f2 100644 --- a/apps/server/src/provider/CodexProvider.test.ts +++ b/apps/server/src/provider/CodexProvider.test.ts @@ -85,7 +85,10 @@ function makeFakeCodexAppServers() { helper.requests.push(frame.params); if (frame.method === "plugin/reconcile") { if (helper.codexHome === OLD_CLI_HOME) { - return respondError(frame.id, { code: -32601, message: "Method not found" }); + return respondError(frame.id, { + code: -32600, + message: "Invalid request: unknown variant `plugin/reconcile`", + }); } if (helper.reconcileFailuresLeft > 0) { helper.reconcileFailuresLeft -= 1; @@ -153,7 +156,6 @@ it.effect("reuses one Codex skills helper per launch until it idles out", () => Effect.map((skills) => skills.map((skill) => skill.name)), ); - // The first read on a fresh helper already includes remote plugin skills. 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); @@ -190,7 +192,7 @@ it.effect("reuses one Codex skills helper per launch until it idles out", () => ]); assert.strictEqual(helpers.length, 4); - // A CLI without plugin/reconcile still lists the skills it has, and is not asked again. + // 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"]); diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index e8ced925f5896..6fc6e5a2f2086 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -140,9 +140,8 @@ 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; -// A slow reconcile must leave the skills read enough of the discovery budget to answer. -const CODEX_SKILLS_LIST_RESERVE_MS = 1_000; -const JSON_RPC_METHOD_NOT_FOUND = -32601; +// 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. @@ -622,14 +621,33 @@ const loadCodexRemotePlugins = Effect.fn("loadCodexRemotePlugins")(function* ( CODEX_PLUGIN_RECONCILE_TIMEOUT_MS, ).pipe( Effect.map((result) => result?.failedMaterializationRemotePluginIds.length === 0), - Effect.catch((cause) => - Effect.logDebug("Codex plugin reconcile failed.", { cause }).pipe( - Effect.as( - cause._tag === "CodexAppServerRequestError" && cause.code === JSON_RPC_METHOD_NOT_FOUND, - ), - ), - ), + 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. */ @@ -799,14 +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( [ - loadCodexRemotePlugins(client, deadlineMs - CODEX_SKILLS_LIST_RESERVE_MS).pipe( - Effect.andThen( - withCodexDiscoveryDeadline( - client.request("skills/list", { cwds: [input.cwd] }), - deadlineMs, - ), - ), - ), + requestCodexCatalogSkills({ client, cwd: input.cwd, deadlineMs }), accountResponse.account?.type === "chatgpt" ? requestCodexChatGptModels({ client, diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index 94cb2ef738cc2..89fb1e98fb5b2 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,86 +1011,53 @@ it.layer( }), ); - it.effect("lists Codex skills when plugin reconcile outlasts the discovery budget", () => + 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 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.170.0", - codexHome: "/nonexistent-codex-home", - platformFamily: "unix", - platformOs: "linux", - }, - "account/read": { account: { type: "apiKey" }, requiresOpenaiAuth: false }, - "config/read": { config: {}, origins: {} }, - "skills/list": { - data: [ + const results: Record = { + initialize: codexInitializeResult, + "account/read": { account: { type: "apiKey" }, requiresOpenaiAuth: false }, + "config/read": { config: {}, origins: {} }, + "skills/list": { + data: [ + { + cwd: "/", + errors: [], + skills: [ { - cwd: "/", - errors: [], - skills: [ - { - name: "local", - path: "/local/SKILL.md", - enabled: true, - description: "", - scope: "user", - }, - ], + name: "local", + path: "/local/SKILL.md", + enabled: true, + description: "", + scope: "user", }, ], }, - "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; - // Never answered: the read after it must still get part of the budget. - if (method === "plugin/reconcile") { - return Deferred.succeed(reconcileRequested, undefined); - } - if (method !== "account/read") { - return send({ id, result: results[method] ?? {} }); - } - return Deferred.succeed(accountRequested, undefined).pipe( - Effect.andThen(Effect.sleep("16 seconds")), - Effect.andThen(send({ id, result: results[method] })), - 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), - }); - }), - ); + ], + }, + "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* Deferred.await(reconcileRequested); - yield* TestClock.adjust("3 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"],