diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index 30f63b21f318e..1665c9f2232a5 100644 --- a/apps/server/src/provider/CodexProvider.test.ts +++ b/apps/server/src/provider/CodexProvider.test.ts @@ -1,6 +1,68 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Path from "effect/Path"; -import { applyPreferredCodexDefaultModel, mapCodexModelCapabilities } from "./CodexProvider.ts"; +import { + applyPreferredCodexDefaultModel, + mapCodexModelCapabilities, + probeCodexSkillsForCwd, +} from "./CodexProvider.ts"; + +// The mock app-server is a shell script Windows cannot execute. +const windowsHost = HostProcess.Platform.defaultValue() === "win32"; + +const makeRemotePluginCodexFixture = Effect.fn("CodexProvider.test.makeRemotePluginCodexFixture")( + function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const fixtureDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-codex-skills-" }); + const sourceDir = path.join(import.meta.dirname, "testFixtures"); + const binaryPath = path.join(fixtureDir, "codex"); + const scriptPath = path.join(fixtureDir, "codex-script.json"); + yield* fileSystem.copyFile(path.join(sourceDir, "codexCollabMockPeer.sh"), binaryPath); + yield* fileSystem.chmod(binaryPath, 0o755); + for (const file of ["codexCollabMockPeer.mjs", "codexMultiAgentWire.json"]) { + yield* fileSystem.copyFile(path.join(sourceDir, file), path.join(fixtureDir, file)); + } + yield* fileSystem.writeFileString( + scriptPath, + JSON.stringify({ + rootThreadId: "probe-thread", + notifications: [], + remoteInstalledPluginSkills: [ + { + name: "impeccable:impeccable", + description: "Design review skill", + path: path.join(fixtureDir, "impeccable", "SKILL.md"), + enabled: true, + scope: "user", + pluginId: "impeccable@openai-curated-remote", + }, + ], + }), + ); + return { + binaryPath, + cwd: fixtureDir, + environment: { ...process.env, T3_CODEX_COLLAB_SCRIPT: scriptPath }, + }; + }, +); + +it.live.skipIf(windowsHost)("lists remote marketplace plugin skills for a workspace", () => + Effect.gen(function* () { + const fixture = yield* makeRemotePluginCodexFixture(); + const skills = yield* probeCodexSkillsForCwd(fixture); + + assert.deepStrictEqual( + skills.map((skill) => skill.name), + ["impeccable:impeccable"], + ); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); it("maps current Codex model capability fields", () => { const capabilities = mapCodexModelCapabilities({ diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 2e56eede84cd6..04de24215faee 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -73,6 +73,7 @@ type CodexRateLimitsProbe = 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_INSTALLED_PLUGINS_TIMEOUT_MS = 5_000; 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. @@ -449,6 +450,29 @@ const requestCodexChatGptModels = Effect.fn("requestCodexChatGptModels")(functio ); }); +/** + * A fresh app-server fills its remote plugin inventory in the background, and + * skills/list omits those plugins' skills until it lands. plugin/installed + * fetches and caches it when empty, so ask for it first, best-effort. + */ +const requestCodexSkills = Effect.fn("requestCodexSkills")(function* (input: { + readonly client: CodexClient.CodexAppServerClient["Service"]; + readonly cwd: string; + readonly deadlineMs: number; +}) { + yield* withCodexDiscoveryDeadline( + input.client.request("plugin/installed", { cwds: [input.cwd] }), + input.deadlineMs, + CODEX_INSTALLED_PLUGINS_TIMEOUT_MS, + ).pipe( + Effect.catch((cause) => Effect.logDebug("Codex installed plugin read failed.", { cause })), + ); + return yield* withCodexDiscoveryDeadline( + input.client.request("skills/list", { cwds: [input.cwd] }), + input.deadlineMs, + ); +}); + /** Set originator at spawn; the pre-initialize refresh otherwise overwrites shared cache with plain-CLI models. */ export const codexAppServerEnvironment = ( environment: NodeJS.ProcessEnv | undefined, @@ -554,7 +578,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun const deadlineMs = spawnedAtMs + CODEX_AUTH_PROBE_TIMEOUT_MS - CODEX_DISCOVERY_MARGIN_MS; const [skillsResponse, models, serviceTier, rateLimits] = yield* Effect.all( [ - withCodexDiscoveryDeadline(client.request("skills/list", { cwds: [input.cwd] }), deadlineMs), + requestCodexSkills({ client, cwd: input.cwd, deadlineMs }), accountResponse.account?.type === "chatgpt" ? requestCodexChatGptModels({ client, @@ -655,8 +679,12 @@ export const probeCodexSkillsForCwd = Effect.fn("probeCodexSkillsForCwd")(functi ...input, environment: providerProbeEnvironment(input.environment ?? process.env), }); - const skillsResponse = yield* client.request("skills/list", { cwds: [input.cwd] }); - return parseCodexSkillsListResponse(skillsResponse, input.cwd); + const skillsResponse = yield* requestCodexSkills({ + client, + cwd: input.cwd, + deadlineMs: Infinity, + }); + return skillsResponse ? parseCodexSkillsListResponse(skillsResponse, input.cwd) : []; }); const emptyCodexModelsFromSettings = (codexSettings: CodexSettings): ServerProvider["models"] => diff --git a/apps/server/src/provider/testFixtures/codexCollabMockPeer.mjs b/apps/server/src/provider/testFixtures/codexCollabMockPeer.mjs index cae8ea2c9380d..bed8a93aa319b 100644 --- a/apps/server/src/provider/testFixtures/codexCollabMockPeer.mjs +++ b/apps/server/src/provider/testFixtures/codexCollabMockPeer.mjs @@ -12,6 +12,7 @@ const script = JSON.parse(NodeFS.readFileSync(process.env.T3_CODEX_COLLAB_SCRIPT const write = (message) => process.stdout.write(`${JSON.stringify(message)}\n`); let turnStartCount = 0; let activeTurn; +let remotePluginCacheLoaded = false; const openServerRequests = new Map(); const rl = NodeReadline.createInterface({ input: process.stdin }); rl.on("line", (line) => { @@ -84,6 +85,25 @@ rl.on("line", (line) => { write({ id, result: { outcome: script.resetCreditOutcome } }); return; } + if (method === "plugin/installed") { + remotePluginCacheLoaded = true; + write({ id, result: { marketplaces: [] } }); + return; + } + if (method === "skills/list" && script.remoteInstalledPluginSkills) { + // Like real Codex, remote marketplace plugin skills appear only once plugin/installed has filled its cache. + write({ + id, + result: { + data: message.params.cwds.map((cwd) => ({ + cwd, + errors: [], + skills: remotePluginCacheLoaded ? script.remoteInstalledPluginSkills : [], + })), + }, + }); + return; + } if (method === "skills/list" || method === "model/list") { write({ id, result: { data: [] } }); return;