From 30d9bd806eaed50240b2b503392c9896731fc1fe Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sat, 21 Nov 2026 23:29:19 +0200 Subject: [PATCH 01/30] scient: load installed Codex plugins' skills and MCP servers Scient Agent reads the plugins Codex has installed and enabled ($CODEX_HOME/config.toml, the version Codex runs from plugins/cache) and loads their skills and MCP servers through the existing skill reader and Codex MCP mapping. The source is `codex-plugins`, opt-in through SCIENT_AGENT_ENABLE_SOURCES or enabledProviders, and --runtime-info lists it under enableSources so a host can check before enabling it. Plugins that need OpenAI's host (browser, chrome, computer-use, unified-computer-use, codex-app-tools), servers launched by computer-use-client-launcher or with auth "chatgpt", app references and hooks are left out. Scient-Divergence: SA-CODEX-PLUGINS --- SCIENT.md | 3 +- docs/environment-variables.md | 2 +- packages/coding-agent/CHANGELOG.md | 6 + packages/coding-agent/src/capability/index.ts | 3 + packages/coding-agent/src/discovery/codex.ts | 3 + packages/coding-agent/src/discovery/index.ts | 3 + .../coding-agent/src/scient/codex-plugins.ts | 261 ++++++++++++++++++ .../coding-agent/src/scient/runtime-info.ts | 6 + .../test/scient/codex-plugins.test.ts | 169 ++++++++++++ .../test/scient/runtime-info.test.ts | 1 + scripts/scient/divergences.json | 19 +- 11 files changed, 473 insertions(+), 3 deletions(-) create mode 100644 packages/coding-agent/src/scient/codex-plugins.ts create mode 100644 packages/coding-agent/test/scient/codex-plugins.test.ts diff --git a/SCIENT.md b/SCIENT.md index ebaeeac3cf..07299dd176 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -14,7 +14,8 @@ Everything else is upstream's code and behavior. | Wording | The agent calls itself Scient Agent, and its command `scient-agent`, wherever a person or a model reads its words: messages, help, sign-in instructions, prompts, the built-in documentation, window and notification titles. The prompts also address researchers: the model's role is a research and coding assistant, and each personality matches its explanations to the reader's expertise instead of assuming a technical reader. `scripts/scient/rename-wording.ts` generates this. | | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | -| Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. | +| Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`), from the version Codex runs in its plugin cache. MCP fields map as the Codex `config.toml` reader maps them, with relative commands and working folders inside the package. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/docs/environment-variables.md b/docs/environment-variables.md index cf89a88a4f..1cb7d747c8 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -11,7 +11,7 @@ It documents only active behavior. ## Host discovery sources -`SCIENT_AGENT_ENABLE_SOURCES` adds foreign user-level discovery sources to the effective `enabledProviders` set without replacing or persisting that setting. Set it in the launch environment, for example `SCIENT_AGENT_ENABLE_SOURCES=claude-plugins` to load Claude Code's installed plugins under `~/.claude/plugins`. Separate multiple provider ids with commas or whitespace; empty and malformed entries are ignored. Ids use lowercase letters, digits and hyphens, starting with a letter; `*` and `all` are also accepted. The normal opt-in semantics apply, including `claude` enabling `claude-plugins`. `disabledProviders` takes precedence. +`SCIENT_AGENT_ENABLE_SOURCES` adds foreign user-level discovery sources to the effective `enabledProviders` set without replacing or persisting that setting. Set it in the launch environment, for example `SCIENT_AGENT_ENABLE_SOURCES=claude-plugins` to load Claude Code's installed plugins under `~/.claude/plugins`, or `codex-plugins` to load the skills and MCP servers of the Codex plugins installed and enabled in `$CODEX_HOME/config.toml` (default `~/.codex`) from Codex's plugin cache. Separate multiple provider ids with commas or whitespace; empty and malformed entries are ignored. Ids use lowercase letters, digits and hyphens, starting with a letter; `*` and `all` are also accepted. The normal opt-in semantics apply, including `claude` enabling `claude-plugins`. `disabledProviders` takes precedence. `--runtime-info` lists the plugin sources the executable can load in `enableSources`. This uses the same environment loading rules as other `SCIENT_AGENT_*` variables: when a host sets `SCIENT_AGENT_ROOT`, project and home `.env` files cannot set it (a `.env` inside the host root can). Standalone use retains the normal `.env` loading rules below. The additions affect discovery only; `getEnabledProviders()`, settings UI values, and source enable/disable writes remain based on settings alone. diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 7420decb64..6a9c7f0a6d 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -2,6 +2,12 @@ ## [Unreleased] + +### Added + +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`). Plugins that only work inside OpenAI's apps are left out. + + ## [19.0.1] - 2026-11-19 ### Added diff --git a/packages/coding-agent/src/capability/index.ts b/packages/coding-agent/src/capability/index.ts index 69c14e3f1c..00a621230f 100644 --- a/packages/coding-agent/src/capability/index.ts +++ b/packages/coding-agent/src/capability/index.ts @@ -56,6 +56,9 @@ const FOREIGN_USER_PROVIDERS: Record = { codex: true, claude: true, "claude-plugins": true, + // scient(SA-CODEX-PLUGINS): begin - Installed Codex plugins are opt-in like Claude's. + "codex-plugins": true, + // scient(SA-CODEX-PLUGINS): end gemini: true, opencode: true, windsurf: true, diff --git a/packages/coding-agent/src/discovery/codex.ts b/packages/coding-agent/src/discovery/codex.ts index 646c5863b2..0ff1cf51ef 100644 --- a/packages/coding-agent/src/discovery/codex.ts +++ b/packages/coding-agent/src/discovery/codex.ts @@ -256,6 +256,9 @@ function extractMCPServersFromToml( return result; } +// scient(SA-CODEX-PLUGINS): begin - Installed Codex plugins map their MCP servers as Codex's config.toml does. +export { extractMCPServersFromToml }; +// scient(SA-CODEX-PLUGINS): end // ============================================================================= // Skills (skills/) diff --git a/packages/coding-agent/src/discovery/index.ts b/packages/coding-agent/src/discovery/index.ts index ff58c99216..46b3d033b8 100644 --- a/packages/coding-agent/src/discovery/index.ts +++ b/packages/coding-agent/src/discovery/index.ts @@ -42,6 +42,9 @@ import "./ssh"; import "./ssh-config"; import "./vscode"; import "./windsurf"; +// scient(SA-CODEX-PLUGINS): begin - Installed, enabled Codex plugins. +import "../scient/codex-plugins"; +// scient(SA-CODEX-PLUGINS): end // Re-export the main API from capability registry export { diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts new file mode 100644 index 0000000000..e60e46f748 --- /dev/null +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -0,0 +1,261 @@ +/** + * Installed Codex plugins (SA-CODEX-PLUGINS). + * + * Codex lists plugins in `$CODEX_HOME/config.toml` (default `~/.codex`) as + * `[plugins."@"]`, enabled unless the table says `enabled = false`, and runs + * each from the active version of `$CODEX_HOME/plugins/cache////`, + * described by `.codex-plugin/plugin.json`. Their skills and MCP servers load here, MCP fields + * mapped as the Codex config.toml reader maps them. App references (`.app.json`) do not: a + * person's ChatGPT apps reach the agent through the ChatGPT sign-in (SA-CHATGPT-APPS). Plugins + * and servers that run only inside OpenAI's own apps are left out. + */ +import * as path from "node:path"; +import { $env, isRecord, logger } from "@oh-my-pi/pi-utils"; +import { isUserSourceEnabled, registerProvider } from "../capability"; +import { readDirEntries, readFile } from "../capability/fs"; +import { type MCPServer, mcpCapability } from "../capability/mcp"; +import { type Skill, skillCapability } from "../capability/skill"; +import type { LoadContext, LoadResult } from "../capability/types"; +import { extractMCPServersFromToml } from "../discovery/codex"; +import { createSourceMeta, scanSkillsFromDir } from "../discovery/helpers"; + +const PROVIDER_ID = "codex-plugins"; +const DISPLAY_NAME = "Codex Plugins"; +/** Below the `codex` provider (70), so a person's own `~/.codex` skills and servers win. */ +const PRIORITY = 65; + +/** The folder of a plugin Codex links from its source while it is developed; it wins over versions. */ +const LOCAL_VERSION = "local"; + +/** + * Bundled plugins that drive OpenAI's apps (the in-app browser, the Chrome extension host, + * computer use, the Codex app's own threads) through services only those apps provide. Their + * skills drive those services too. + */ +const HOST_PLUGINS: Record = { + "browser@openai-bundled": true, + "chrome@openai-bundled": true, + "codex-app-tools@openai-bundled": true, + "computer-use@openai-bundled": true, + "unified-computer-use@openai-bundled": true, +}; + +/** OpenAI's launcher for its signed Computer Use client, which serves only Codex (the Messages plugin). */ +const HOST_LAUNCHER = "computer-use-client-launcher"; + +const SEMVER = + /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/; + +interface CodexPlugin { + /** `@`, the plugin's key in config.toml. */ + id: string; + /** Plugin name: skill namespace and MCP server prefix. */ + name: string; + /** The active version's folder; package-relative paths resolve against it. */ + root: string; + manifestPath: string; + manifest: Record; + /** The plugin's `[plugins.""]` table. */ + config: Record; +} + +/** Codex's rule for a cache path segment (plugin, marketplace or version name). */ +function isPathSegment(value: string): boolean { + return value !== "." && value !== ".." && /^[A-Za-z0-9._+-]+$/.test(value); +} + +/** Codex's version order: semantic versions by precedence, anything else by its characters. */ +function compareVersions(a: string, b: string): number { + if (SEMVER.test(a) && SEMVER.test(b)) { + const order = Bun.semver.order(a, b); + if (order !== 0) return order; + } + return a < b ? -1 : a > b ? 1 : 0; +} + +/** The version folder Codex runs: `local` while present, else the highest version; null when not installed. */ +async function activeVersion(base: string): Promise { + const versions = (await readDirEntries(base)) + .filter(entry => entry.isDirectory() && isPathSegment(entry.name)) + .map(entry => entry.name); + if (versions.includes(LOCAL_VERSION)) return LOCAL_VERSION; + return versions.sort(compareVersions).at(-1) ?? null; +} + +async function readPlugin( + home: string, + id: string, + config: Record, + warnings: string[], +): Promise { + const at = id.lastIndexOf("@"); + const name = id.slice(0, at); + const marketplace = id.slice(at + 1); + if (at <= 0 || !isPathSegment(name) || !isPathSegment(marketplace)) { + warnings.push(`[codex-plugins] Ignoring invalid plugin id: ${id}`); + return null; + } + const base = path.join(home, "plugins", "cache", marketplace, name); + const version = await activeVersion(base); + // Listed but not installed (or removed since): Codex skips it as well. + if (version === null) return null; + const root = path.join(base, version); + const manifestPath = path.join(root, ".codex-plugin", "plugin.json"); + const raw = await readFile(manifestPath); + let manifest: unknown = null; + try { + manifest = raw === null ? null : JSON.parse(raw); + } catch {} + if (!isRecord(manifest)) { + warnings.push(`[codex-plugins] Missing or invalid plugin.json for ${id}: ${manifestPath}`); + return null; + } + return { id, name, root, manifestPath, manifest, config }; +} + +/** Installed, enabled Codex plugins outside {@link HOST_PLUGINS}. */ +async function listCodexPlugins(ctx: LoadContext): Promise<{ plugins: CodexPlugin[]; warnings: string[] }> { + const warnings: string[] = []; + // Codex's home: `CODEX_HOME`, else `~/.codex`. + const home = $env.CODEX_HOME?.trim() || path.join(ctx.home, ".codex"); + const configPath = path.join(home, "config.toml"); + const raw = await readFile(configPath); + if (raw === null) return { plugins: [], warnings }; + let toml: unknown; + try { + toml = Bun.TOML.parse(raw); + } catch (error) { + warnings.push(`[codex-plugins] Invalid TOML in ${configPath}: ${String(error)}`); + return { plugins: [], warnings }; + } + const table = isRecord(toml) && isRecord(toml.plugins) ? toml.plugins : {}; + const plugins = await Promise.all( + Object.entries(table).map(([id, config]) => { + if (!isRecord(config) || config.enabled === false || HOST_PLUGINS[id] === true) return null; + return readPlugin(home, id, config, warnings); + }), + ); + return { plugins: plugins.filter(plugin => plugin !== null), warnings }; +} + +/** A manifest path inside the package, or null (with a warning when it points outside). */ +function packagePath(plugin: CodexPlugin, value: unknown, key: string, warnings: string[]): string | null { + if (typeof value !== "string" || !value.trim()) return null; + const resolved = path.resolve(plugin.root, value.trim()); + const relative = path.relative(plugin.root, resolved); + if (relative.startsWith("..") || path.isAbsolute(relative)) { + warnings.push(`[codex-plugins] Ignoring ${key} path outside ${plugin.id}: ${value}`); + return null; + } + return resolved; +} + +async function loadSkills(ctx: LoadContext): Promise> { + if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; + const { plugins, warnings } = await listCodexPlugins(ctx); + const scans = plugins.flatMap(plugin => { + const declared = plugin.manifest.skills; + const entries = declared == null ? ["skills"] : Array.isArray(declared) ? declared : [declared]; + const dirs = new Set(); + for (const entry of entries) { + const dir = packagePath(plugin, entry, "skills", warnings); + if (dir !== null) dirs.add(dir); + } + return [...dirs].map(dir => + scanSkillsFromDir(ctx, { dir, providerId: PROVIDER_ID, level: "user", pluginName: plugin.name }), + ); + }); + const items: Skill[] = []; + for (const result of await Promise.all(scans)) { + items.push(...result.items); + if (result.warnings) warnings.push(...result.warnings); + } + return { items, warnings }; +} + +/** Why an MCP server runs only inside OpenAI's apps, or undefined when it runs anywhere. */ +function hostOnlyReason(server: Record): string | undefined { + if (typeof server.command === "string" && path.basename(server.command) === HOST_LAUNCHER) { + return "it launches OpenAI's Computer Use client"; + } + if (server.auth === "chatgpt") return "it signs in with Codex's own ChatGPT session"; + return undefined; +} + +/** The plugin's server map: the manifest's inline map, its named file, or the package's `.mcp.json`. */ +async function readServerMap( + plugin: CodexPlugin, + warnings: string[], +): Promise<{ servers: Record; sourcePath: string } | null> { + const declared = plugin.manifest.mcpServers; + let parsed: unknown = declared; + let sourcePath = plugin.manifestPath; + if (!isRecord(declared)) { + const file = + declared == null ? path.join(plugin.root, ".mcp.json") : packagePath(plugin, declared, "mcpServers", warnings); + if (file === null) return null; + const raw = await readFile(file); + if (raw === null) { + if (declared != null) + warnings.push(`[codex-plugins] Missing mcpServers file declared by ${plugin.id}: ${file}`); + return null; + } + try { + parsed = JSON.parse(raw); + } catch { + warnings.push(`[codex-plugins] Invalid JSON in ${file}`); + return null; + } + sourcePath = file; + } + if (!isRecord(parsed)) return null; + // Codex accepts `{ "mcpServers": { ... } }` and the bare server map. + const servers = "mcpServers" in parsed ? parsed.mcpServers : parsed; + return isRecord(servers) ? { servers, sourcePath } : null; +} + +async function loadMCPServers(ctx: LoadContext): Promise> { + if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; + const { plugins, warnings } = await listCodexPlugins(ctx); + const items: MCPServer[] = []; + for (const plugin of plugins) { + const map = await readServerMap(plugin, warnings); + if (map === null) continue; + const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; + for (const [serverName, server] of Object.entries(map.servers)) { + if (!isRecord(server)) continue; + const reason = hostOnlyReason(server); + if (reason !== undefined) { + logger.debug(`[codex-plugins] Skipping MCP server "${serverName}" of ${plugin.id}: ${reason}`); + continue; + } + // Codex resolves a relative cwd, and a relative command from that cwd, against the package. + const mapped = extractMCPServersFromToml({ mcp_servers: { [serverName]: server } }, plugin.root)[serverName]; + const policy = policies[serverName]; + items.push({ + name: `${plugin.name}:${serverName}`, + ...mapped, + // `[plugins."".mcp_servers.] enabled = false` turns the server off in Codex. + ...(isRecord(policy) && policy.enabled === false && { enabled: false }), + _source: createSourceMeta(PROVIDER_ID, map.sourcePath, "user", undefined, plugin.name), + }); + } + } + return { items, warnings }; +} + +registerProvider(skillCapability.id, { + id: PROVIDER_ID, + displayName: DISPLAY_NAME, + description: "Load skills from installed, enabled Codex plugins ($CODEX_HOME/plugins/cache)", + priority: PRIORITY, + load: loadSkills, +}); + +registerProvider(mcpCapability.id, { + id: PROVIDER_ID, + displayName: DISPLAY_NAME, + description: "Load MCP servers from installed, enabled Codex plugins ($CODEX_HOME/plugins/cache)", + priority: PRIORITY, + load: loadMCPServers, +}); diff --git a/packages/coding-agent/src/scient/runtime-info.ts b/packages/coding-agent/src/scient/runtime-info.ts index aa50b84928..efd99f8bb6 100644 --- a/packages/coding-agent/src/scient/runtime-info.ts +++ b/packages/coding-agent/src/scient/runtime-info.ts @@ -24,6 +24,11 @@ export interface RuntimeInfo { * prompt, so a host checks this list instead of guessing from the version. */ hostCommands: string[]; + /** + * Plugin discovery sources a host may name in `SCIENT_AGENT_ENABLE_SOURCES`. An executable + * that does not list a source has no reader for it, so a host checks this list before it offers it. + */ + enableSources: string[]; } export function formatRuntimeInfo(): string { @@ -34,6 +39,7 @@ export function formatRuntimeInfo(): string { rpcProtocolVersions: [1, 2], buildId: BUILD_ID, hostCommands: ["apps", "dictate"], + enableSources: ["claude-plugins", "codex-plugins"], }; return `${JSON.stringify(info)}\n`; } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts new file mode 100644 index 0000000000..b557013e17 --- /dev/null +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -0,0 +1,169 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test"; +import * as fs from "node:fs/promises"; +import * as os from "node:os"; +import * as path from "node:path"; +import { initializeWithSettings, loadCapability } from "@oh-my-pi/pi-coding-agent/capability"; +import { clearCache as clearFsCache } from "@oh-my-pi/pi-coding-agent/capability/fs"; +import type { MCPServer } from "@oh-my-pi/pi-coding-agent/capability/mcp"; +import type { Skill } from "@oh-my-pi/pi-coding-agent/capability/skill"; +import { cfgEnabledProviders } from "@oh-my-pi/pi-coding-agent/config/model-settings"; +import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; +import { TempDir } from "@oh-my-pi/pi-utils"; +import { restoreEnvValue } from "../helpers/settings-test-state"; +import "@oh-my-pi/pi-coding-agent/discovery"; + +const ENV_KEYS = ["SCIENT_AGENT_ENABLE_SOURCES", "CODEX_HOME", "CODEX_PLUGIN_TEST_TOKEN", "CODEX_PLUGIN_TEST_PAT"]; + +async function writeSkill(root: string, name: string): Promise { + const skillPath = path.join(root, "skills", name, "SKILL.md"); + await Bun.write(skillPath, `---\nname: ${name}\ndescription: ${name} skill\n---\nUse ${name}.\n`); + return skillPath; +} + +async function writeManifest(root: string, manifest: Record): Promise { + await Bun.write(path.join(root, ".codex-plugin", "plugin.json"), JSON.stringify(manifest)); +} + +describe("installed Codex plugins", () => { + let temp: TempDir; + let settings: Settings; + let release: () => void; + let saved: Record; + let cwd: string; + let agentDir: string; + let researchRoot: string; + let researchSkill: string; + + beforeEach(async () => { + saved = Object.fromEntries(ENV_KEYS.map(key => [key, process.env[key]])); + for (const key of ENV_KEYS) restoreEnvValue(key, undefined); + settings = Settings.isolated(); + release = initializeWithSettings(settings); + temp = TempDir.createSync("@scient-codex-plugins-"); + const home = temp.join("home"); + const codexHome = temp.join("codex-home"); + cwd = temp.join("project"); + agentDir = temp.join("agent"); + await fs.mkdir(cwd, { recursive: true }); + vi.spyOn(os, "homedir").mockReturnValue(home); + restoreEnvValue("CODEX_HOME", codexHome); + restoreEnvValue("CODEX_PLUGIN_TEST_TOKEN", "forwarded-value"); + restoreEnvValue("CODEX_PLUGIN_TEST_PAT", "pat-value"); + + await Bun.write( + path.join(codexHome, "config.toml"), + [ + '[plugins."research@market"]', + "enabled = true", + '[plugins."research@market".mcp_servers.quiet]', + "enabled = false", + '[plugins."paused@market"]', + "enabled = false", + '[plugins."browser@openai-bundled"]', + "enabled = true", + "", + ].join("\n"), + ); + const cache = path.join(codexHome, "plugins", "cache"); + const research = path.join(cache, "market", "research"); + researchRoot = path.join(research, "1.10.0"); + const olderRoot = path.join(research, "1.2.0"); + for (const root of [researchRoot, olderRoot]) { + await writeManifest(root, { + name: "research", + skills: "./skills/", + mcpServers: "./.mcp.json", + apps: "./.app.json", + interface: { displayName: "Research" }, + }); + } + researchSkill = await writeSkill(researchRoot, "lit-review"); + await writeSkill(olderRoot, "stale-review"); + // Codex ignores links in the cache (bundled plugins ship a `latest` link to a version). + await fs.symlink(olderRoot, path.join(research, "latest"), "dir"); + await Bun.write( + path.join(researchRoot, ".mcp.json"), + JSON.stringify({ + mcpServers: { + papers: { + command: "./bin/papers", + args: ["--stdio"], + cwd: ".", + env_vars: ["CODEX_PLUGIN_TEST_TOKEN"], + tool_timeout_sec: 30, + }, + tracker: { + type: "http", + url: "https://mcp.example.test/mcp", + bearer_token_env_var: "CODEX_PLUGIN_TEST_PAT", + }, + quiet: { command: "node", args: ["./quiet.mjs"] }, + messages: { command: "./bin/computer-use-client-launcher", args: ["messages", "mcp"], cwd: "." }, + }, + }), + ); + await Bun.write( + path.join(researchRoot, ".app.json"), + JSON.stringify({ apps: { research: { id: "asdk_app_x" } } }), + ); + + const paused = path.join(cache, "market", "paused", "1.0.0"); + await writeManifest(paused, { name: "paused", skills: "./skills/" }); + await writeSkill(paused, "paused-skill"); + + const browser = path.join(cache, "openai-bundled", "browser", "26.1117.41037"); + await writeManifest(browser, { name: "browser", skills: "./skills/" }); + await writeSkill(browser, "control-in-app-browser"); + clearFsCache(); + }); + + afterEach(() => { + release(); + for (const key of ENV_KEYS) restoreEnvValue(key, saved[key]); + vi.restoreAllMocks(); + clearFsCache(); + temp.removeSync(); + }); + + const skills = async () => + (await loadCapability("skills", { cwd, agentDir })).items.filter( + skill => skill._source.provider === "codex-plugins", + ); + const servers = async () => + (await loadCapability("mcps", { cwd, agentDir })).items.filter( + server => server._source.provider === "codex-plugins", + ); + + it("loads nothing until the host or settings enable codex-plugins", async () => { + cfgEnabledProviders.set(settings, ["codex", "claude"]); + expect(await skills()).toEqual([]); + expect(await servers()).toEqual([]); + }); + + it("loads the active version's skills and MCP servers with package-relative paths", async () => { + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + + // Only the enabled, installed plugin's highest version: not the disabled plugin, the + // host-only browser plugin, the older version or the `latest` link to it. + expect((await skills()).map(skill => [skill.name, skill.path])).toEqual([["lit-review", researchSkill]]); + + const byName = new Map((await servers()).map(server => [server.name, server])); + // The Computer Use client launcher runs only inside Codex. + expect([...byName.keys()].sort()).toEqual(["research:papers", "research:quiet", "research:tracker"]); + expect(byName.get("research:papers")).toMatchObject({ + command: path.join(researchRoot, "bin", "papers"), + args: ["--stdio"], + cwd: researchRoot, + env: { CODEX_PLUGIN_TEST_TOKEN: "forwarded-value" }, + timeout: 30_000, + transport: "stdio", + }); + expect(byName.get("research:tracker")).toMatchObject({ + url: "https://mcp.example.test/mcp", + headers: { Authorization: "Bearer pat-value" }, + transport: "http", + }); + // The plugin's config table turns this server off, as Codex does. + expect(byName.get("research:quiet")?.enabled).toBe(false); + }); +}); diff --git a/packages/coding-agent/test/scient/runtime-info.test.ts b/packages/coding-agent/test/scient/runtime-info.test.ts index 8a1c6eed9f..112bf4fabc 100644 --- a/packages/coding-agent/test/scient/runtime-info.test.ts +++ b/packages/coding-agent/test/scient/runtime-info.test.ts @@ -16,6 +16,7 @@ describe("--runtime-info", () => { rpcProtocolVersions: [1, 2], buildId: BUILD_ID, hostCommands: ["apps", "dictate"], + enableSources: ["claude-plugins", "codex-plugins"], }); }); }); diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 49188463ad..1aa35282b7 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -69,7 +69,7 @@ }, "SA-PRODUCT": { "status": "active", - "contract": "Scient Agent reports its own release: scient.json names the product, its version and the exact Oh My Pi release; --version, the startup screen, completions and the User-Agent report the version, and --runtime-info reports all of it with the build id Scient's workflow stamps and the Scient commands a host may run (hostCommands).", + "contract": "Scient Agent reports its own release: scient.json names the product, its version and the exact Oh My Pi release; --version, the startup screen, completions and the User-Agent report the version, and --runtime-info reports all of it with the build id Scient's workflow stamps, the Scient commands a host may run (hostCommands) and the plugin sources a host may add with SCIENT_AGENT_ENABLE_SOURCES (enableSources).", "owned": [ "scient.json", "scripts/scient/stamp-build-id.ts", @@ -517,6 +517,23 @@ "verification": [ "bun test packages/coding-agent/test/scient/xdev-catalog.test.ts packages/coding-agent/test/scient/eval-docs.test.ts" ] + }, + "SA-CODEX-PLUGINS": { + "status": "active", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), from the version Codex runs in $CODEX_HOME/plugins/cache/// and its .codex-plugin/plugin.json. MCP fields map as the codex provider maps config.toml (env_vars, bearer_token_env_var, tool_timeout_sec), relative command and cwd resolve inside the package, servers are named :, and a plugin's mcp_servers..enabled = false disables one. OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded.", + "owned": [ + "packages/coding-agent/src/scient/codex-plugins.ts", + "packages/coding-agent/test/scient/codex-plugins.test.ts" + ], + "upstream": [ + "packages/coding-agent/src/capability/index.ts", + "packages/coding-agent/src/discovery/codex.ts", + "packages/coding-agent/src/discovery/index.ts", + "packages/coding-agent/CHANGELOG.md" + ], + "verification": [ + "bun --cwd=packages/coding-agent test test/scient/codex-plugins.test.ts" + ] } } } From 0af9eab8099399b0589b7bf1b487f12020f058a4 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 02:01:13 +0200 Subject: [PATCH 02/30] fix(codex-plugins): never widen a plugin server's tools; one bad server skips alone - A server whose plugin entry or config.toml policy sets enabled_tools (or a non-empty disabled_tools) is skipped with a warning: Scient Agent cannot enforce Codex's tool filters. - Fields the mapper iterates are validated per server; a malformed server is skipped with a warning and its siblings still load. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- .../coding-agent/src/scient/codex-plugins.ts | 55 ++++++++++++++-- .../test/scient/codex-plugins.test.ts | 66 +++++++++++++++++++ scripts/scient/divergences.json | 2 +- 5 files changed, 119 insertions(+), 8 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index 07299dd176..fdfb5817b5 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`), from the version Codex runs in its plugin cache. MCP fields map as the Codex `config.toml` reader maps them, with relative commands and working folders inside the package. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`), from the version Codex runs in its plugin cache. MCP fields map as the Codex `config.toml` reader maps them, with relative commands and working folders inside the package. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`, in the plugin or in its `[plugins."".mcp_servers.]` table) is skipped with a warning, because Scient Agent cannot enforce the filter; so is a server with malformed fields, while its siblings load. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 6a9c7f0a6d..0853a714be 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`). Plugins that only work inside OpenAI's apps are left out. +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`). Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`) and servers with malformed fields. ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index e60e46f748..8a612cf4d1 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -182,6 +182,33 @@ function hostOnlyReason(server: Record): string | undefined { return undefined; } +/** + * The Codex key that narrows a server's tools, or undefined when it exposes them all. Scient + * Agent's MCP servers carry no tool filter, so such a server would run with more tools than Codex + * allows. An empty denylist narrows nothing. + */ +function toolFilterKey(table: Record): string | undefined { + if (table.enabled_tools != null) return "enabled_tools"; + const denied = table.disabled_tools; + if (denied != null && !(Array.isArray(denied) && denied.length === 0)) return "disabled_tools"; + return undefined; +} + +/** The first field the config.toml mapper would misread, or undefined when its shapes are sound. */ +function malformedField(server: Record): string | undefined { + for (const key of ["args", "env_vars"]) { + const value = server[key]; + if (value != null && !(Array.isArray(value) && value.every(item => typeof item === "string"))) return key; + } + for (const key of ["env", "http_headers", "env_http_headers"]) { + const value = server[key]; + if (value != null && !(isRecord(value) && Object.values(value).every(item => typeof item === "string"))) { + return key; + } + } + return undefined; +} + /** The plugin's server map: the manifest's inline map, its named file, or the package's `.mcp.json`. */ async function readServerMap( plugin: CodexPlugin, @@ -224,19 +251,37 @@ async function loadMCPServers(ctx: LoadContext): Promise> const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; for (const [serverName, server] of Object.entries(map.servers)) { if (!isRecord(server)) continue; + const skip = `[codex-plugins] Skipping MCP server "${serverName}" of ${plugin.id}`; const reason = hostOnlyReason(server); if (reason !== undefined) { - logger.debug(`[codex-plugins] Skipping MCP server "${serverName}" of ${plugin.id}: ${reason}`); + logger.debug(`${skip}: ${reason}`); + continue; + } + const declaredPolicy = policies[serverName]; + const policy: Record = isRecord(declaredPolicy) ? declaredPolicy : {}; + const filter = toolFilterKey(server) ?? toolFilterKey(policy); + if (filter !== undefined) { + warnings.push(`${skip}: Codex restricts its tools (${filter}), which Scient Agent cannot enforce`); + continue; + } + const malformed = malformedField(server); + if (malformed !== undefined) { + warnings.push(`${skip}: invalid ${malformed} in ${map.sourcePath}`); + continue; + } + let mapped: Partial | undefined; + try { + // Codex resolves a relative cwd, and a relative command from that cwd, against the package. + mapped = extractMCPServersFromToml({ mcp_servers: { [serverName]: server } }, plugin.root)[serverName]; + } catch (error) { + warnings.push(`${skip}: ${String(error)}`); continue; } - // Codex resolves a relative cwd, and a relative command from that cwd, against the package. - const mapped = extractMCPServersFromToml({ mcp_servers: { [serverName]: server } }, plugin.root)[serverName]; - const policy = policies[serverName]; items.push({ name: `${plugin.name}:${serverName}`, ...mapped, // `[plugins."".mcp_servers.] enabled = false` turns the server off in Codex. - ...(isRecord(policy) && policy.enabled === false && { enabled: false }), + ...(policy.enabled === false && { enabled: false }), _source: createSourceMeta(PROVIDER_ID, map.sourcePath, "user", undefined, plugin.name), }); } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index b557013e17..629f2d164d 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -166,4 +166,70 @@ describe("installed Codex plugins", () => { // The plugin's config table turns this server off, as Codex does. expect(byName.get("research:quiet")?.enabled).toBe(false); }); + + /** + * Installs and enables `limits@market` with these servers and config.toml lines after its table; + * returns its `.mcp.json`. + */ + async function installLimits(mcpServers: Record, policy: string[] = []): Promise { + const codexHome = temp.join("codex-home"); + const configPath = path.join(codexHome, "config.toml"); + const config = await Bun.file(configPath).text(); + await Bun.write(configPath, [config, '[plugins."limits@market"]', "enabled = true", ...policy, ""].join("\n")); + const root = path.join(codexHome, "plugins", "cache", "market", "limits", "1.0.0"); + await writeManifest(root, { name: "limits" }); + const mcpPath = path.join(root, ".mcp.json"); + await Bun.write(mcpPath, JSON.stringify({ mcpServers })); + clearFsCache(); + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + return mcpPath; + } + const limits = async () => { + const result = await loadCapability("mcps", { cwd, agentDir }); + return { + names: result.items.filter(server => server.name.startsWith("limits:")).map(server => server.name), + warnings: result.warnings.filter(warning => warning.includes("of limits@market")), + }; + }; + + it("skips a plugin's server whose tools Codex narrows with enabled_tools", async () => { + await installLimits({ + narrow: { command: "node", args: ["narrow.mjs"], enabled_tools: ["search"] }, + open: { command: "node", args: ["open.mjs"], disabled_tools: [] }, + }); + expect(await limits()).toEqual({ + names: ["limits:open"], + warnings: [ + '[Codex Plugins] [codex-plugins] Skipping MCP server "narrow" of limits@market: Codex restricts its tools (enabled_tools), which Scient Agent cannot enforce', + ], + }); + }); + + it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { + await installLimits( + { denied: { command: "node", args: ["denied.mjs"] }, open: { command: "node", args: ["open.mjs"] } }, + ['[plugins."limits@market".mcp_servers.denied]', 'disabled_tools = ["delete"]'], + ); + expect(await limits()).toEqual({ + names: ["limits:open"], + warnings: [ + '[Codex Plugins] [codex-plugins] Skipping MCP server "denied" of limits@market: Codex restricts its tools (disabled_tools), which Scient Agent cannot enforce', + ], + }); + }); + + it("skips a malformed server and keeps its siblings", async () => { + const mcpPath = await installLimits({ + bad: { command: "node", env_vars: "CODEX_PLUGIN_TEST_TOKEN" }, + headers: { url: "https://mcp.example.test/mcp", http_headers: ["X-Bad"] }, + open: { command: "node", args: ["open.mjs"] }, + }); + expect(await limits()).toEqual({ + names: ["limits:open"], + warnings: [ + `[Codex Plugins] [codex-plugins] Skipping MCP server "bad" of limits@market: invalid env_vars in ${mcpPath}`, + `[Codex Plugins] [codex-plugins] Skipping MCP server "headers" of limits@market: invalid http_headers in ${mcpPath}`, + ], + }); + }); }); diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 1aa35282b7..c03e17c514 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), from the version Codex runs in $CODEX_HOME/plugins/cache/// and its .codex-plugin/plugin.json. MCP fields map as the codex provider maps config.toml (env_vars, bearer_token_env_var, tool_timeout_sec), relative command and cwd resolve inside the package, servers are named :, and a plugin's mcp_servers..enabled = false disables one. OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), from the version Codex runs in $CODEX_HOME/plugins/cache/// and its .codex-plugin/plugin.json. MCP fields map as the codex provider maps config.toml (env_vars, bearer_token_env_var, tool_timeout_sec), relative command and cwd resolve inside the package, servers are named :, and a plugin's mcp_servers..enabled = false disables one. A server whose plugin definition or mcp_servers. policy sets enabled_tools or a non-empty disabled_tools is skipped with a warning, since Scient Agent's MCP servers carry no tool filter, and a server with a malformed args, env_vars, env, http_headers or env_http_headers is skipped with a warning while its siblings load. OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts" From 4d90a4861420a2fbe7d723b765a5059b799d114e Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 09:52:27 +0200 Subject: [PATCH 03/30] fix(codex-plugins): load Codex plugins under Codex's own trust rules Codex review of 0af9eab809: - CODEX_HOME and header credentials come from the launch environment, never a project's .env. - Stdio servers inherit only Codex's variable list plus their env_vars (MCPServer.envInherit), not the agent's whole environment; env values are literal, headers literal and origin-locked. - [plugins._default] and /etc/codex/config.toml apply; nothing loads under an organization policy. - Package files, cwd and relative commands must stay inside the symlink-resolved package. - One transport per server with only its Codex fields; OAuth client settings, scopes and oauth_resource map; http_headers_helper and auth other than oauth are skipped. - Same-named plugins from two marketplaces are named .. - Codex's manifest precedence: root Agent Plugins plugin.json, then .codex-, .claude-, .cursor-plugin. - The plugin mapper no longer borrows the codex provider's config.toml mapper. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- packages/coding-agent/src/capability/mcp.ts | 7 + packages/coding-agent/src/discovery/codex.ts | 3 - packages/coding-agent/src/mcp/config.ts | 3 + .../coding-agent/src/mcp/transports/stdio.ts | 7 +- packages/coding-agent/src/mcp/types.ts | 4 + .../coding-agent/src/scient/codex-plugins.ts | 513 ++++++++++++++---- .../test/scient/codex-plugins.test.ts | 237 +++++++- scripts/scient/divergences.json | 7 +- 10 files changed, 679 insertions(+), 106 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index fdfb5817b5..f19dd7a614 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`), from the version Codex runs in its plugin cache. MCP fields map as the Codex `config.toml` reader maps them, with relative commands and working folders inside the package. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`, in the plugin or in its `[plugins."".mcp_servers.]` table) is skipped with a warning, because Scient Agent cannot enforce the filter; so is a server with malformed fields, while its siblings load. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), one transport per server with only that transport's fields, package files and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME` and header credentials come from the launch environment, never a project's `.env`. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`) is skipped with a warning, because Scient Agent cannot enforce the filter; so is a server with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 0853a714be..a621a19345 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`). Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`) and servers with malformed fields. +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]` and its manifest formats included), servers that inherit only the variables Codex would pass them, plugin values taken literally, and package paths kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`) and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex. ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/capability/mcp.ts b/packages/coding-agent/src/capability/mcp.ts index 4b12e9293a..2a0528860a 100644 --- a/packages/coding-agent/src/capability/mcp.ts +++ b/packages/coding-agent/src/capability/mcp.ts @@ -36,6 +36,10 @@ export interface MCPServer { envPolicy?: "literal"; /** Env keys whose values are final package data (expanded by the provider). */ envLiteralKeys?: string[]; + // scient(SA-CODEX-PLUGINS): begin - A Codex plugin's server inherits only what Codex passes it. + /** When set, the stdio server inherits only these variables from the agent's environment, plus its own `env`. */ + envInherit?: string[]; + // scient(SA-CODEX-PLUGINS): end /** Working directory for stdio transport */ cwd?: string; /** URL (for HTTP/SSE transport) */ @@ -98,6 +102,9 @@ export function isSameMCPConnection(left: MCPServer, right: MCPServer): boolean Bun.deepEquals(left.args, right.args) && Bun.deepEquals(left.env, right.env) && Bun.deepEquals(literalKeysOf(left), literalKeysOf(right)) && + // scient(SA-CODEX-PLUGINS): begin - Inheritance decides what the process sees. + Bun.deepEquals(left.envInherit, right.envInherit) && + // scient(SA-CODEX-PLUGINS): end left.cwd === right.cwd ); } diff --git a/packages/coding-agent/src/discovery/codex.ts b/packages/coding-agent/src/discovery/codex.ts index 0ff1cf51ef..646c5863b2 100644 --- a/packages/coding-agent/src/discovery/codex.ts +++ b/packages/coding-agent/src/discovery/codex.ts @@ -256,9 +256,6 @@ function extractMCPServersFromToml( return result; } -// scient(SA-CODEX-PLUGINS): begin - Installed Codex plugins map their MCP servers as Codex's config.toml does. -export { extractMCPServersFromToml }; -// scient(SA-CODEX-PLUGINS): end // ============================================================================= // Skills (skills/) diff --git a/packages/coding-agent/src/mcp/config.ts b/packages/coding-agent/src/mcp/config.ts index f8224fe4c7..fe82b2d84d 100644 --- a/packages/coding-agent/src/mcp/config.ts +++ b/packages/coding-agent/src/mcp/config.ts @@ -67,6 +67,9 @@ function convertToLegacyConfig(server: MCPServer): MCPServerConfig { if (server.env) config.env = server.env; if (server.envPolicy) config.envPolicy = server.envPolicy; if (server.envLiteralKeys) config.envLiteralKeys = server.envLiteralKeys; + // scient(SA-CODEX-PLUGINS): begin - A Codex plugin's server inherits only what Codex passes it. + if (server.envInherit) config.envInherit = server.envInherit; + // scient(SA-CODEX-PLUGINS): end if (server.cwd) config.cwd = server.cwd; return config; } diff --git a/packages/coding-agent/src/mcp/transports/stdio.ts b/packages/coding-agent/src/mcp/transports/stdio.ts index b96a905696..7c06fe7686 100644 --- a/packages/coding-agent/src/mcp/transports/stdio.ts +++ b/packages/coding-agent/src/mcp/transports/stdio.ts @@ -592,10 +592,15 @@ export class StdioTransport implements MCPTransport { signal?.throwIfAborted(); if (this.#connected) return; + // scient(SA-CODEX-PLUGINS): begin - A Codex plugin's server inherits only what Codex passes it. + const inherited: Record = this.config.envInherit + ? Object.fromEntries(this.config.envInherit.map(name => [name, Bun.env[name]]).filter(([, value]) => value)) + : Bun.env; const env = { - ...Bun.env, + ...inherited, ...this.config.env, }; + // scient(SA-CODEX-PLUGINS): end const cwd = this.config.cwd ?? getProjectDir(); const spawnCommand = await resolveStdioSpawnCommand(this.config, { cwd, diff --git a/packages/coding-agent/src/mcp/types.ts b/packages/coding-agent/src/mcp/types.ts index 8252c3a9f3..ab2825fae3 100644 --- a/packages/coding-agent/src/mcp/types.ts +++ b/packages/coding-agent/src/mcp/types.ts @@ -124,6 +124,10 @@ export interface MCPStdioServerConfig extends MCPServerConfigBase { envPolicy?: "literal"; /** Env keys whose values are final package data; auth resolution keeps them verbatim. */ envLiteralKeys?: string[]; + // scient(SA-CODEX-PLUGINS): begin - A Codex plugin's server inherits only what Codex passes it. + /** When set, only these variables are inherited from the agent's environment; `env` still applies on top. */ + envInherit?: string[]; + // scient(SA-CODEX-PLUGINS): end cwd?: string; } diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 8a612cf4d1..3cb6e2fa74 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -2,21 +2,27 @@ * Installed Codex plugins (SA-CODEX-PLUGINS). * * Codex lists plugins in `$CODEX_HOME/config.toml` (default `~/.codex`) as - * `[plugins."@"]`, enabled unless the table says `enabled = false`, and runs - * each from the active version of `$CODEX_HOME/plugins/cache////`, - * described by `.codex-plugin/plugin.json`. Their skills and MCP servers load here, MCP fields - * mapped as the Codex config.toml reader maps them. App references (`.app.json`) do not: a - * person's ChatGPT apps reach the agent through the ChatGPT sign-in (SA-CHATGPT-APPS). Plugins - * and servers that run only inside OpenAI's own apps are left out. + * `[plugins."@"]`, enabled unless the table, or else `[plugins._default]`, + * says `enabled = false`, and runs each from the active version of + * `$CODEX_HOME/plugins/cache////`. Their skills and MCP servers load + * here under Codex's own rules: its manifest precedence, its MCP field validation, and the + * environment and header handling it gives a plugin's servers. App references (`.app.json`) do + * not: a person's ChatGPT apps reach the agent through the ChatGPT sign-in (SA-CHATGPT-APPS). + * Plugins and servers that run only inside OpenAI's own apps are left out, and so is everything + * when an organization manages Codex, since its policy cannot be read here. */ +import type { Dirent, Stats } from "node:fs"; +import * as fs from "node:fs/promises"; +import * as os from "node:os"; import * as path from "node:path"; -import { $env, isRecord, logger } from "@oh-my-pi/pi-utils"; +import { filterChildShellEnv, getPluginsDir, isRecord, logger } from "@oh-my-pi/pi-utils"; import { isUserSourceEnabled, registerProvider } from "../capability"; import { readDirEntries, readFile } from "../capability/fs"; import { type MCPServer, mcpCapability } from "../capability/mcp"; import { type Skill, skillCapability } from "../capability/skill"; import type { LoadContext, LoadResult } from "../capability/types"; -import { extractMCPServersFromToml } from "../discovery/codex"; +import { classifyAgentPluginRoot, parseAgentPluginMcp } from "../discovery/agent-plugin-format"; +import { isContainedResolved, realpathIfExists, resolveContainedPath } from "../discovery/contained-path"; import { createSourceMeta, scanSkillsFromDir } from "../discovery/helpers"; const PROVIDER_ID = "codex-plugins"; @@ -27,6 +33,12 @@ const PRIORITY = 65; /** The folder of a plugin Codex links from its source while it is developed; it wins over versions. */ const LOCAL_VERSION = "local"; +/** + * Codex's legacy manifests, in its order of precedence after a root Agent Plugins `plugin.json`. + * The first one present decides; a link or non-file there stops the search, as in Codex. + */ +const LEGACY_MANIFESTS = [".codex-plugin/plugin.json", ".claude-plugin/plugin.json", ".cursor-plugin/plugin.json"]; + /** * Bundled plugins that drive OpenAI's apps (the in-app browser, the Chrome extension host, * computer use, the Codex app's own threads) through services only those apps provide. Their @@ -43,22 +55,107 @@ const HOST_PLUGINS: Record = { /** OpenAI's launcher for its signed Computer Use client, which serves only Codex (the Messages plugin). */ const HOST_LAUNCHER = "computer-use-client-launcher"; +/** + * The variables Codex passes a plugin's local stdio server from its own environment, besides + * those the server names in `env_vars` (codex-rs/rmcp-client `DEFAULT_ENV_VARS`, and + * `WINDOWS_CORE_ENV_VARS` on Windows), plus the certificate-bundle variables it always keeps + * (`CUSTOM_CA_ENV_KEYS`). Nothing else, API keys included, reaches the server. + */ +const INHERITED_ENV = + process.platform === "win32" + ? [ + "PATH", + "PATHEXT", + "SHELL", + "COMSPEC", + "SYSTEMROOT", + "WINDIR", + "SYSTEMDRIVE", + "USERNAME", + "USERDOMAIN", + "USERPROFILE", + "HOMEDRIVE", + "HOMEPATH", + "PROGRAMFILES", + "PROGRAMFILES(X86)", + "PROGRAMW6432", + "PROGRAMDATA", + "LOCALAPPDATA", + "APPDATA", + "TEMP", + "TMP", + "TMPDIR", + "POWERSHELL", + "PWSH", + ] + : [ + "HOME", + "LOGNAME", + "PATH", + "SHELL", + "USER", + "__CF_USER_TEXT_ENCODING", + "LANG", + "LC_ALL", + "TERM", + "TMPDIR", + "TZ", + ]; +const CA_BUNDLE_ENV = [ + "CODEX_CA_CERTIFICATE", + "SSL_CERT_FILE", + "REQUESTS_CA_BUNDLE", + "CURL_CA_BUNDLE", + "NODE_EXTRA_CA_CERTS", + "GIT_SSL_CAINFO", + "CARGO_HTTP_CAINFO", + "PIP_CERT", + "BUNDLE_SSL_CA_CERT", + "npm_config_cafile", + "NPM_CONFIG_CAFILE", +]; + +/** Fields Codex rejects on a stdio server, and on a streamable HTTP server. */ +const NOT_STDIO = [ + "url", + "bearer_token_env_var", + "bearer_token", + "http_headers_helper", + "http_headers", + "env_http_headers", + "oauth", + "oauth_resource", + "auth", +]; +const NOT_HTTP = ["args", "env", "env_vars", "cwd", "bearer_token"]; + const SEMVER = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/; interface CodexPlugin { /** `@`, the plugin's key in config.toml. */ id: string; - /** Plugin name: skill namespace and MCP server prefix. */ name: string; - /** The active version's folder; package-relative paths resolve against it. */ + marketplace: string; + /** Skill namespace and MCP server prefix: the name, or `.` when two enabled plugins share it. */ + label: string; + /** The active version's folder, symlinks resolved; every package path stays inside it. */ root: string; + /** `standard`: a root Agent Plugins `plugin.json`, read under that standard; `legacy`: a Codex-style manifest. */ + format: "standard" | "legacy"; manifestPath: string; manifest: Record; /** The plugin's `[plugins.""]` table. */ config: Record; } +interface PluginList { + plugins: CodexPlugin[]; + warnings: string[]; + /** Scient Agent's launch environment, without a project's `.env` values. */ + env: Record; +} + /** Codex's rule for a cache path segment (plugin, marketplace or version name). */ function isPathSegment(value: string): boolean { return value !== "." && value !== ".." && /^[A-Za-z0-9._+-]+$/.test(value); @@ -76,18 +173,54 @@ function compareVersions(a: string, b: string): number { /** The version folder Codex runs: `local` while present, else the highest version; null when not installed. */ async function activeVersion(base: string): Promise { const versions = (await readDirEntries(base)) - .filter(entry => entry.isDirectory() && isPathSegment(entry.name)) + .filter((entry: Dirent) => entry.isDirectory() && isPathSegment(entry.name)) .map(entry => entry.name); if (versions.includes(LOCAL_VERSION)) return LOCAL_VERSION; return versions.sort(compareVersions).at(-1) ?? null; } +async function lstatOrNull(file: string): Promise { + try { + return await fs.lstat(file); + } catch { + return null; + } +} + +/** The first legacy manifest present, read; null (with a warning) when none is usable. */ +async function readLegacyManifest( + root: string, + id: string, + warnings: string[], +): Promise<{ manifestPath: string; manifest: Record } | null> { + for (const relative of LEGACY_MANIFESTS) { + const manifestPath = path.join(root, relative); + const stat = await lstatOrNull(manifestPath); + if (stat === null) continue; + if (!stat.isFile()) { + warnings.push(`[codex-plugins] Ignoring ${id}: ${relative} is not a regular file`); + return null; + } + let manifest: unknown = null; + try { + manifest = JSON.parse((await readFile(manifestPath)) ?? ""); + } catch {} + if (!isRecord(manifest)) { + warnings.push(`[codex-plugins] Invalid plugin.json for ${id}: ${manifestPath}`); + return null; + } + return { manifestPath, manifest }; + } + warnings.push(`[codex-plugins] No plugin.json for ${id} in ${root}`); + return null; +} + async function readPlugin( home: string, id: string, config: Record, warnings: string[], -): Promise { +): Promise | null> { const at = id.lastIndexOf("@"); const name = id.slice(0, at); const marketplace = id.slice(at + 1); @@ -99,72 +232,145 @@ async function readPlugin( const version = await activeVersion(base); // Listed but not installed (or removed since): Codex skips it as well. if (version === null) return null; - const root = path.join(base, version); - const manifestPath = path.join(root, ".codex-plugin", "plugin.json"); - const raw = await readFile(manifestPath); - let manifest: unknown = null; + const root = await realpathIfExists(path.join(base, version)); + if (root === null) return null; + // A root `plugin.json` that targets Agent Plugins wins; one that does not leaves the legacy manifests. + const rootManifest = path.join(root, "plugin.json"); + const rootStat = await lstatOrNull(rootManifest); + if (rootStat !== null && !rootStat.isFile()) { + warnings.push(`[codex-plugins] Ignoring ${id}: plugin.json is not a regular file`); + return null; + } + const standard = await classifyAgentPluginRoot(root); + if (standard.kind === "invalid") { + warnings.push(`[codex-plugins] Ignoring ${id}: ${standard.reason}`); + return null; + } + if (standard.kind === "standard") { + warnings.push(...standard.warnings.map(warning => `[codex-plugins] ${id}: ${warning}`)); + return { + id, + name, + marketplace, + root, + format: "standard", + manifestPath: rootManifest, + manifest: { ...standard.manifest }, + config, + }; + } + const legacy = await readLegacyManifest(root, id, warnings); + if (legacy === null) return null; + return { id, name, marketplace, root, format: "legacy", ...legacy, config }; +} + +async function readToml(file: string, warnings: string[]): Promise | null> { + const raw = await readFile(file); + if (raw === null) return null; try { - manifest = raw === null ? null : JSON.parse(raw); - } catch {} - if (!isRecord(manifest)) { - warnings.push(`[codex-plugins] Missing or invalid plugin.json for ${id}: ${manifestPath}`); + const parsed = Bun.TOML.parse(raw); + return isRecord(parsed) ? parsed : null; + } catch (error) { + warnings.push(`[codex-plugins] Invalid TOML in ${file}: ${String(error)}`); return null; } - return { id, name, root, manifestPath, manifest, config }; +} + +/** + * Where an organization's Codex policy lives (requirements, managed config, macOS managed + * preferences). Codex enforces them over the user's config; Scient Agent cannot, so it loads no + * plugins while one is present. + */ +function managedPolicyFiles(): string[] { + if (process.platform === "win32") { + const programData = process.env.ProgramData || "C:\\ProgramData"; + return [path.join(programData, "OpenAI", "Codex", "requirements.toml")]; + } + const files = ["/etc/codex/requirements.toml", "/etc/codex/managed_config.toml"]; + if (process.platform === "darwin") { + const managed = "/Library/Managed Preferences"; + files.push( + path.join(managed, "com.openai.codex.plist"), + path.join(managed, os.userInfo().username, "com.openai.codex.plist"), + ); + } + return files; } /** Installed, enabled Codex plugins outside {@link HOST_PLUGINS}. */ -async function listCodexPlugins(ctx: LoadContext): Promise<{ plugins: CodexPlugin[]; warnings: string[] }> { +async function listCodexPlugins(ctx: LoadContext): Promise { const warnings: string[] = []; - // Codex's home: `CODEX_HOME`, else `~/.codex`. - const home = $env.CODEX_HOME?.trim() || path.join(ctx.home, ".codex"); - const configPath = path.join(home, "config.toml"); - const raw = await readFile(configPath); - if (raw === null) return { plugins: [], warnings }; - let toml: unknown; - try { - toml = Bun.TOML.parse(raw); - } catch (error) { - warnings.push(`[codex-plugins] Invalid TOML in ${configPath}: ${String(error)}`); - return { plugins: [], warnings }; + // The environment Scient Agent was launched with: a project's `.env` never chooses Codex's + // home or the credentials sent to a plugin's server. + const env = filterChildShellEnv(Bun.env); + for (const file of managedPolicyFiles()) { + if ((await lstatOrNull(file)) === null) continue; + warnings.push( + `[codex-plugins] Codex is managed by an organization policy (${file}), which Scient Agent cannot apply; no Codex plugins load`, + ); + return { plugins: [], warnings, env }; } - const table = isRecord(toml) && isRecord(toml.plugins) ? toml.plugins : {}; - const plugins = await Promise.all( + // Codex's home: `CODEX_HOME`, else `~/.codex`. + const home = env.CODEX_HOME?.trim() || path.join(ctx.home, ".codex"); + const user = await readToml(path.join(home, "config.toml"), warnings); + if (user === null) return { plugins: [], warnings, env }; + // The system layer sits under the user's; per-plugin tables and `_default` merge by key. + const system = process.platform === "win32" ? null : await readToml("/etc/codex/config.toml", warnings); + const table: Record = { + ...(isRecord(system?.plugins) ? system.plugins : {}), + ...(isRecord(user.plugins) ? user.plugins : {}), + }; + const defaults = isRecord(table._default) ? table._default : {}; + const enabledByDefault = defaults.enabled !== false; + const read = await Promise.all( Object.entries(table).map(([id, config]) => { - if (!isRecord(config) || config.enabled === false || HOST_PLUGINS[id] === true) return null; - return readPlugin(home, id, config, warnings); + if (id === "_default" || !isRecord(config) || HOST_PLUGINS[id] === true) return null; + const enabled = typeof config.enabled === "boolean" ? config.enabled : enabledByDefault; + return enabled ? readPlugin(home, id, config, warnings) : null; }), ); - return { plugins: plugins.filter(plugin => plugin !== null), warnings }; + const found = read.filter(plugin => plugin !== null); + const sharedNames = new Set( + found.filter((plugin, index) => found.findIndex(other => other.name === plugin.name) !== index).map(p => p.name), + ); + const plugins = found.map(plugin => ({ + ...plugin, + label: sharedNames.has(plugin.name) ? `${plugin.name}.${plugin.marketplace}` : plugin.name, + })); + return { plugins, warnings, env }; } -/** A manifest path inside the package, or null (with a warning when it points outside). */ -function packagePath(plugin: CodexPlugin, value: unknown, key: string, warnings: string[]): string | null { +/** A manifest path inside the package, symlinks resolved; null (with a warning when it points outside). */ +async function packagePath(plugin: CodexPlugin, value: unknown, key: string, warnings: string[]) { if (typeof value !== "string" || !value.trim()) return null; - const resolved = path.resolve(plugin.root, value.trim()); - const relative = path.relative(plugin.root, resolved); - if (relative.startsWith("..") || path.isAbsolute(relative)) { + const resolved = await resolveContainedPath(plugin.root, path.resolve(plugin.root, value.trim())); + if (resolved.status === "outside") { warnings.push(`[codex-plugins] Ignoring ${key} path outside ${plugin.id}: ${value}`); return null; } - return resolved; + return resolved.status === "ok" ? resolved.realPath : null; } async function loadSkills(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; const { plugins, warnings } = await listCodexPlugins(ctx); - const scans = plugins.flatMap(plugin => { - const declared = plugin.manifest.skills; - const entries = declared == null ? ["skills"] : Array.isArray(declared) ? declared : [declared]; - const dirs = new Set(); - for (const entry of entries) { - const dir = packagePath(plugin, entry, "skills", warnings); - if (dir !== null) dirs.add(dir); - } - return [...dirs].map(dir => - scanSkillsFromDir(ctx, { dir, providerId: PROVIDER_ID, level: "user", pluginName: plugin.name }), - ); - }); + const dirsByPlugin = await Promise.all( + plugins.map(async plugin => { + const declared = plugin.format === "standard" ? undefined : plugin.manifest.skills; + const entries = declared == null ? ["skills"] : Array.isArray(declared) ? declared : [declared]; + const dirs = new Set(); + for (const entry of entries) { + const dir = await packagePath(plugin, entry, "skills", warnings); + if (dir !== null) dirs.add(dir); + } + return { plugin, dirs: [...dirs] }; + }), + ); + const scans = dirsByPlugin.flatMap(({ plugin, dirs }) => + dirs.map(dir => + scanSkillsFromDir(ctx, { dir, providerId: PROVIDER_ID, level: "user", pluginName: plugin.label }), + ), + ); const items: Skill[] = []; for (const result of await Promise.all(scans)) { items.push(...result.items); @@ -194,23 +400,119 @@ function toolFilterKey(table: Record): string | undefined { return undefined; } -/** The first field the config.toml mapper would misread, or undefined when its shapes are sound. */ -function malformedField(server: Record): string | undefined { - for (const key of ["args", "env_vars"]) { - const value = server[key]; - if (value != null && !(Array.isArray(value) && value.every(item => typeof item === "string"))) return key; - } - for (const key of ["env", "http_headers", "env_http_headers"]) { - const value = server[key]; - if (value != null && !(isRecord(value) && Object.values(value).every(item => typeof item === "string"))) { - return key; +const isStringList = (value: unknown): value is string[] => + Array.isArray(value) && value.every(item => typeof item === "string"); +const isStringMap = (value: unknown): value is Record => + isRecord(value) && Object.values(value).every(item => typeof item === "string"); + +/** Codex's `env_vars` entry: a name, or `{ name, source = "local" }`. */ +function envVarName(entry: unknown): string | undefined { + if (typeof entry === "string") return entry; + if (isRecord(entry) && typeof entry.name === "string" && (entry.source ?? "local") === "local") return entry.name; + return undefined; +} + +type Mapped = { server: Partial } | { error: string }; + +/** + * A legacy plugin's server as Codex reads it: exactly one transport with its own fields, package + * paths kept inside the package, values taken literally, and only Codex's variables inherited. + */ +async function mapLegacyServer( + plugin: CodexPlugin, + server: Record, + env: Record, +): Promise { + const isStdio = typeof server.command === "string"; + if (!isStdio && typeof server.url !== "string") return { error: "it declares neither command nor url" }; + const wrong = (isStdio ? NOT_STDIO : NOT_HTTP).find(key => server[key] != null); + if (wrong !== undefined) return { error: `${wrong} is not supported for ${isStdio ? "stdio" : "streamable HTTP"}` }; + const timeout = + typeof server.tool_timeout_sec === "number" && server.tool_timeout_sec > 0 + ? { timeout: server.tool_timeout_sec * 1000 } + : {}; + + if (isStdio) { + const command = (server.command as string).trim(); + if (!command) return { error: "command is empty" }; + if (server.args != null && !isStringList(server.args)) return { error: "invalid args" }; + if (server.env != null && !isStringMap(server.env)) return { error: "invalid env" }; + if (server.env_vars != null && !Array.isArray(server.env_vars)) return { error: "invalid env_vars" }; + const forwarded = ((server.env_vars as unknown[] | undefined) ?? []).map(envVarName); + if (forwarded.some(name => name === undefined)) return { error: "invalid env_vars" }; + // Codex runs a server from the package unless it names a folder inside it. + const cwd = typeof server.cwd === "string" ? path.resolve(plugin.root, server.cwd) : plugin.root; + if (!(await isContainedResolved(plugin.root, cwd))) return { error: `cwd ${server.cwd} leaves the package` }; + // A bare name runs from PATH and an absolute path as given; a relative path runs from the package. + const relative = !path.isAbsolute(command) && /[\\/]/.test(command); + const resolvedCommand = relative ? path.resolve(cwd, command) : command; + if (relative && !(await isContainedResolved(plugin.root, resolvedCommand))) { + return { error: `command ${command} leaves the package` }; } + return { + server: { + transport: "stdio", + command: resolvedCommand, + ...(server.args != null && { args: server.args as string[] }), + ...(server.env != null && { env: server.env as Record }), + envPolicy: "literal", + envInherit: [...new Set([...INHERITED_ENV, ...CA_BUNDLE_ENV, ...(forwarded as string[])])], + cwd, + ...timeout, + }, + }; } - return undefined; + + if (server.http_headers_helper != null) return { error: "it computes its headers with a helper command" }; + if (server.auth != null && server.auth !== "oauth") return { error: `auth ${String(server.auth)} is not supported` }; + if (server.http_headers != null && !isStringMap(server.http_headers)) return { error: "invalid http_headers" }; + if (server.env_http_headers != null && !isStringMap(server.env_http_headers)) { + return { error: "invalid env_http_headers" }; + } + if (server.bearer_token_env_var != null && typeof server.bearer_token_env_var !== "string") { + return { error: "invalid bearer_token_env_var" }; + } + if (server.oauth != null && !isRecord(server.oauth)) return { error: "invalid oauth" }; + const url = server.url as string; + if (!URL.canParse(url) || !/^https?:$/.test(new URL(url).protocol)) return { error: `invalid url ${url}` }; + // Static headers are data; the others name variables of the launch environment, as in Codex. + const headers: Record = { ...(server.http_headers as Record | undefined) }; + for (const [header, name] of Object.entries((server.env_http_headers ?? {}) as Record)) { + const value = env[name]?.trim(); + if (value) headers[header] = value; + } + const bearer = server.bearer_token_env_var ? env[server.bearer_token_env_var as string]?.trim() : undefined; + if (bearer) headers.Authorization = `Bearer ${bearer}`; + // Codex's OAuth client settings, snake_case or the camelCase plugins may use. + const oauthTable = (server.oauth ?? {}) as Record; + const oauth: NonNullable = {}; + for (const [snake, camel, target] of [ + ["client_id", "clientId", "clientId"], + ["client_secret", "clientSecret", "clientSecret"], + ["callback_url", "callbackUrl", "redirectUri"], + ] as const) { + const value = oauthTable[snake] ?? oauthTable[camel]; + if (typeof value === "string" && value) oauth[target] = value; + } + const port = oauthTable.callback_port ?? oauthTable.callbackPort; + if (Number.isInteger(port) && Number(port) > 0) oauth.callbackPort = Number(port); + if (isStringList(server.scopes) && server.scopes.length > 0) oauth.scope = server.scopes.join(" "); + const resource = typeof server.oauth_resource === "string" && server.oauth_resource ? server.oauth_resource : null; + return { + server: { + transport: "http", + url, + ...(Object.keys(headers).length > 0 && { headers }), + headerPolicy: "origin-locked", + ...(Object.keys(oauth).length > 0 && { oauth }), + ...(resource !== null && { auth: { type: "oauth" as const, resource } }), + ...timeout, + }, + }; } /** The plugin's server map: the manifest's inline map, its named file, or the package's `.mcp.json`. */ -async function readServerMap( +async function readLegacyServerMap( plugin: CodexPlugin, warnings: string[], ): Promise<{ servers: Record; sourcePath: string } | null> { @@ -218,17 +520,13 @@ async function readServerMap( let parsed: unknown = declared; let sourcePath = plugin.manifestPath; if (!isRecord(declared)) { - const file = - declared == null ? path.join(plugin.root, ".mcp.json") : packagePath(plugin, declared, "mcpServers", warnings); - if (file === null) return null; - const raw = await readFile(file); - if (raw === null) { - if (declared != null) - warnings.push(`[codex-plugins] Missing mcpServers file declared by ${plugin.id}: ${file}`); + const file = await packagePath(plugin, declared ?? ".mcp.json", "mcpServers", warnings); + if (file === null) { + if (declared != null) warnings.push(`[codex-plugins] Missing mcpServers file declared by ${plugin.id}`); return null; } try { - parsed = JSON.parse(raw); + parsed = JSON.parse((await readFile(file)) ?? ""); } catch { warnings.push(`[codex-plugins] Invalid JSON in ${file}`); return null; @@ -241,15 +539,49 @@ async function readServerMap( return isRecord(servers) ? { servers, sourcePath } : null; } +/** An Agent Plugins package's `mcp.json`, read under that standard, keyed by server name. */ +async function readStandardServers( + ctx: LoadContext, + plugin: CodexPlugin, + warnings: string[], +): Promise<{ servers: Record>; sourcePath: string } | null> { + const resolved = await resolveContainedPath(plugin.root, path.join(plugin.root, "mcp.json")); + if (resolved.status !== "ok") { + if (resolved.status === "outside") warnings.push(`[codex-plugins] ${plugin.id}: mcp.json leaves the package`); + return null; + } + const raw = await readFile(resolved.realPath); + if (raw === null) return null; + const digest = new Bun.CryptoHasher("sha256").update(`codex:${plugin.id}`).digest("hex").slice(0, 16); + const pluginData = path.join(getPluginsDir(ctx.home), "data", `codex-${plugin.name}-${digest}`); + const result = await parseAgentPluginMcp(raw, { pluginRoot: plugin.root, pluginData }); + if (result.status === "disabled") { + warnings.push(`[codex-plugins] ${plugin.id}: MCP disabled: ${result.reason}`); + return null; + } + warnings.push(...result.warnings.map(warning => `[codex-plugins] ${plugin.id}: ${warning}`)); + if (result.servers.some(server => server.transport === "stdio")) await fs.mkdir(pluginData, { recursive: true }); + const servers: Record> = {}; + for (const { name, ...server } of result.servers) { + servers[name] = + server.transport === "stdio" + ? { ...server, envPolicy: "literal", envInherit: [...INHERITED_ENV, ...CA_BUNDLE_ENV] } + : { ...server, headerPolicy: "origin-locked" }; + } + return { servers, sourcePath: resolved.realPath }; +} + async function loadMCPServers(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; - const { plugins, warnings } = await listCodexPlugins(ctx); + const { plugins, warnings, env } = await listCodexPlugins(ctx); const items: MCPServer[] = []; for (const plugin of plugins) { - const map = await readServerMap(plugin, warnings); - if (map === null) continue; const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; - for (const [serverName, server] of Object.entries(map.servers)) { + const standard = plugin.format === "standard" ? await readStandardServers(ctx, plugin, warnings) : null; + const legacy = plugin.format === "legacy" ? await readLegacyServerMap(plugin, warnings) : null; + const declared: Record = standard?.servers ?? legacy?.servers ?? {}; + const sourcePath = standard?.sourcePath ?? legacy?.sourcePath ?? plugin.manifestPath; + for (const [serverName, server] of Object.entries(declared)) { if (!isRecord(server)) continue; const skip = `[codex-plugins] Skipping MCP server "${serverName}" of ${plugin.id}`; const reason = hostOnlyReason(server); @@ -264,25 +596,22 @@ async function loadMCPServers(ctx: LoadContext): Promise> warnings.push(`${skip}: Codex restricts its tools (${filter}), which Scient Agent cannot enforce`); continue; } - const malformed = malformedField(server); - if (malformed !== undefined) { - warnings.push(`${skip}: invalid ${malformed} in ${map.sourcePath}`); - continue; - } - let mapped: Partial | undefined; + let mapped: Mapped; try { - // Codex resolves a relative cwd, and a relative command from that cwd, against the package. - mapped = extractMCPServersFromToml({ mcp_servers: { [serverName]: server } }, plugin.root)[serverName]; + mapped = standard !== null ? { server } : await mapLegacyServer(plugin, server, env); } catch (error) { - warnings.push(`${skip}: ${String(error)}`); + mapped = { error: String(error) }; + } + if ("error" in mapped) { + warnings.push(`${skip}: ${mapped.error} (${sourcePath})`); continue; } items.push({ - name: `${plugin.name}:${serverName}`, - ...mapped, + name: `${plugin.label}:${serverName}`, + ...mapped.server, // `[plugins."".mcp_servers.] enabled = false` turns the server off in Codex. ...(policy.enabled === false && { enabled: false }), - _source: createSourceMeta(PROVIDER_ID, map.sourcePath, "user", undefined, plugin.name), + _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), }); } } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 629f2d164d..880d9e0b9d 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -8,11 +8,18 @@ import type { MCPServer } from "@oh-my-pi/pi-coding-agent/capability/mcp"; import type { Skill } from "@oh-my-pi/pi-coding-agent/capability/skill"; import { cfgEnabledProviders } from "@oh-my-pi/pi-coding-agent/config/model-settings"; import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; -import { TempDir } from "@oh-my-pi/pi-utils"; +import { StdioTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/stdio"; +import { getProjectDir, setProjectDir, TempDir } from "@oh-my-pi/pi-utils"; import { restoreEnvValue } from "../helpers/settings-test-state"; import "@oh-my-pi/pi-coding-agent/discovery"; -const ENV_KEYS = ["SCIENT_AGENT_ENABLE_SOURCES", "CODEX_HOME", "CODEX_PLUGIN_TEST_TOKEN", "CODEX_PLUGIN_TEST_PAT"]; +const ENV_KEYS = [ + "SCIENT_AGENT_ENABLE_SOURCES", + "CODEX_HOME", + "CODEX_PLUGIN_TEST_TOKEN", + "CODEX_PLUGIN_TEST_PAT", + "CODEX_PLUGIN_TEST_SECRET", +]; async function writeSkill(root: string, name: string): Promise { const skillPath = path.join(root, "skills", name, "SKILL.md"); @@ -114,6 +121,9 @@ describe("installed Codex plugins", () => { const browser = path.join(cache, "openai-bundled", "browser", "26.1117.41037"); await writeManifest(browser, { name: "browser", skills: "./skills/" }); await writeSkill(browser, "control-in-app-browser"); + // The loader reports symlink-resolved paths (macOS temp folders live under /private). + researchRoot = await fs.realpath(researchRoot); + researchSkill = await fs.realpath(researchSkill); clearFsCache(); }); @@ -154,13 +164,19 @@ describe("installed Codex plugins", () => { command: path.join(researchRoot, "bin", "papers"), args: ["--stdio"], cwd: researchRoot, - env: { CODEX_PLUGIN_TEST_TOKEN: "forwarded-value" }, + envPolicy: "literal", timeout: 30_000, transport: "stdio", }); + // Codex passes its own short list plus the server's env_vars, never the whole environment. + expect(byName.get("research:papers")?.env).toBeUndefined(); + expect(byName.get("research:papers")?.envInherit).toEqual( + expect.arrayContaining(["PATH", "HOME", "CODEX_PLUGIN_TEST_TOKEN"]), + ); expect(byName.get("research:tracker")).toMatchObject({ url: "https://mcp.example.test/mcp", headers: { Authorization: "Bearer pat-value" }, + headerPolicy: "origin-locked", transport: "http", }); // The plugin's config table turns this server off, as Codex does. @@ -182,7 +198,7 @@ describe("installed Codex plugins", () => { await Bun.write(mcpPath, JSON.stringify({ mcpServers })); clearFsCache(); restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); - return mcpPath; + return fs.realpath(mcpPath); } const limits = async () => { const result = await loadCapability("mcps", { cwd, agentDir }); @@ -227,9 +243,218 @@ describe("installed Codex plugins", () => { expect(await limits()).toEqual({ names: ["limits:open"], warnings: [ - `[Codex Plugins] [codex-plugins] Skipping MCP server "bad" of limits@market: invalid env_vars in ${mcpPath}`, - `[Codex Plugins] [codex-plugins] Skipping MCP server "headers" of limits@market: invalid http_headers in ${mcpPath}`, + `[Codex Plugins] [codex-plugins] Skipping MCP server "bad" of limits@market: invalid env_vars (${mcpPath})`, + `[Codex Plugins] [codex-plugins] Skipping MCP server "headers" of limits@market: invalid http_headers (${mcpPath})`, ], }); }); + + /** Installs `@` with this manifest file and extra files; returns its real root. */ + async function install( + id: string, + files: Record, + configLines: string[] = ["enabled = true"], + ): Promise { + const [name, marketplace] = id.split("@"); + const codexHome = temp.join("codex-home"); + const configPath = path.join(codexHome, "config.toml"); + const config = await Bun.file(configPath).text(); + await Bun.write(configPath, [config, `[plugins."${id}"]`, ...configLines, ""].join("\n")); + const root = path.join(codexHome, "plugins", "cache", marketplace!, name!, "1.0.0"); + for (const [file, content] of Object.entries(files)) { + await Bun.write(path.join(root, file), typeof content === "string" ? content : JSON.stringify(content)); + } + clearFsCache(); + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + return fs.realpath(root); + } + const load = async () => { + const result = await loadCapability("mcps", { cwd, agentDir }); + return { + byName: new Map( + result.items.filter(server => server._source.provider === "codex-plugins").map(s => [s.name, s]), + ), + warnings: result.warnings, + }; + }; + + it("follows [plugins._default]: a table without enabled inherits it", async () => { + const server = { mcpServers: { one: { command: "node" } } }; + await install("inherits@market", { ".codex-plugin/plugin.json": { name: "inherits" }, ".mcp.json": server }); + await install("opted@market", { ".codex-plugin/plugin.json": { name: "opted" }, ".mcp.json": server }); + await Bun.write( + path.join(temp.join("codex-home"), "config.toml"), + [ + "[plugins._default]", + "enabled = false", + // A table with only a server policy and no `enabled` takes the default. + '[plugins."inherits@market".mcp_servers.one]', + "enabled = true", + '[plugins."opted@market"]', + "enabled = true", + "", + ].join("\n"), + ); + clearFsCache(); + expect([...(await load()).byName.keys()]).toEqual(["opted:one"]); + }); + + it("rejects servers Codex would reject and maps OAuth client settings", async () => { + await install("strict@market", { + ".codex-plugin/plugin.json": { name: "strict" }, + ".mcp.json": { + mcpServers: { + both: { command: "node", url: "https://mcp.example.test/mcp" }, + argsOnHttp: { url: "https://mcp.example.test/mcp", args: ["x"] }, + helper: { url: "https://mcp.example.test/mcp", http_headers_helper: "./headers.sh" }, + ema: { url: "https://mcp.example.test/mcp", auth: "ema_auth" }, + signin: { + type: "http", + url: "https://mcp.example.test/mcp", + oauth_resource: "https://mcp.example.test/mcp", + scopes: ["read", "write"], + oauth: { clientId: "client-1", callback_port: 4567 }, + note: "unknown fields are ignored, as in Codex", + }, + }, + }, + }); + const { byName, warnings } = await load(); + expect([...byName.keys()].filter(name => name.startsWith("strict:"))).toEqual(["strict:signin"]); + expect(byName.get("strict:signin")).toMatchObject({ + oauth: { clientId: "client-1", callbackPort: 4567, scope: "read write" }, + auth: { type: "oauth", resource: "https://mcp.example.test/mcp" }, + }); + expect(warnings.filter(w => w.includes("of strict@market"))).toEqual([ + expect.stringContaining('"both" of strict@market: url is not supported for stdio'), + expect.stringContaining('"argsOnHttp" of strict@market: args is not supported for streamable HTTP'), + expect.stringContaining('"helper" of strict@market: it computes its headers with a helper command'), + expect.stringContaining('"ema" of strict@market: auth ema_auth is not supported'), + ]); + }); + + it("takes plugin values literally and keeps launch paths inside the package", async () => { + const root = await install("paths@market", { + ".codex-plugin/plugin.json": { name: "paths" }, + ".mcp.json": { + mcpServers: { + literal: { url: "https://mcp.example.test/mcp", http_headers: { "X-Run": "!touch /tmp/pwned" } }, + upCwd: { command: "node", cwd: "../.." }, + upCommand: { command: "../../../other/bin/server" }, + fromPath: { command: "node", env: { MODE: "CODEX_PLUGIN_TEST_PAT" } }, + }, + }, + }); + const { byName, warnings } = await load(); + expect([...byName.keys()].filter(name => name.startsWith("paths:")).sort()).toEqual([ + "paths:fromPath", + "paths:literal", + ]); + // Origin-locked headers and literal env are never resolved as variables or `!commands`. + expect(byName.get("paths:literal")).toMatchObject({ + headers: { "X-Run": "!touch /tmp/pwned" }, + headerPolicy: "origin-locked", + }); + expect(byName.get("paths:fromPath")).toMatchObject({ + command: "node", + cwd: root, + env: { MODE: "CODEX_PLUGIN_TEST_PAT" }, + envPolicy: "literal", + }); + const reasons = warnings.filter(w => w.includes("of paths@market")); + expect(reasons).toEqual([ + expect.stringContaining('"upCwd" of paths@market: cwd ../.. leaves the package'), + expect.stringContaining('"upCommand" of paths@market: command ../../../other/bin/server leaves the package'), + ]); + }); + + it("does not follow a skills or MCP file link out of the package", async () => { + const outside = temp.join("outside"); + await writeSkill(outside, "leaked"); + await Bun.write(path.join(outside, "servers.json"), JSON.stringify({ mcpServers: { x: { command: "node" } } })); + const root = await install("linked@market", { + ".codex-plugin/plugin.json": { name: "linked", mcpServers: "./servers.json" }, + }); + await fs.symlink(path.join(outside, "skills"), path.join(root, "skills"), "dir"); + await fs.symlink(path.join(outside, "servers.json"), path.join(root, "servers.json")); + clearFsCache(); + expect((await skills()).map(skill => skill.name)).toEqual(["lit-review"]); + const { byName, warnings } = await load(); + expect([...byName.keys()].some(name => name.startsWith("linked:"))).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers path outside linked@market")); + }); + + it("keeps two marketplaces' same-named plugins apart", async () => { + const server = { mcpServers: { api: { command: "node" } } }; + await install("twin@alpha", { ".codex-plugin/plugin.json": { name: "twin" }, ".mcp.json": server }); + await install("twin@beta", { ".codex-plugin/plugin.json": { name: "twin" }, ".mcp.json": server }); + expect([...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort()).toEqual([ + "twin.alpha:api", + "twin.beta:api", + ]); + }); + + it("reads Codex's manifest precedence: a root Agent Plugins manifest, then the legacy folders", async () => { + await install("portable@market", { + "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name: "portable" }, + ".codex-plugin/plugin.json": { name: "portable", mcpServers: { legacy: { command: "node" } } }, + "mcp.json": { + $schema: "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json", + mcpServers: { remote: { type: "streamable-http", url: "https://mcp.example.test/mcp" } }, + }, + }); + await install("clauded@market", { + ".claude-plugin/plugin.json": { name: "clauded" }, + ".mcp.json": { mcpServers: { tool: { command: "node" } } }, + }); + const { byName } = await load(); + expect(byName.get("portable:remote")).toMatchObject({ transport: "http", headerPolicy: "origin-locked" }); + expect(byName.has("portable:legacy")).toBe(false); + expect(byName.has("clauded:tool")).toBe(true); + }); + + it("ignores a CODEX_HOME that only a project's .env sets", async () => { + const previous = getProjectDir(); + const decoy = temp.join("decoy-home"); + await Bun.write(path.join(cwd, ".env"), `CODEX_HOME=${decoy}\n`); + await install("decoy@market", { ".codex-plugin/plugin.json": { name: "decoy" } }); + await fs.cp(temp.join("codex-home"), decoy, { recursive: true }); + // As if the agent had loaded the project's .env into its environment. + restoreEnvValue("CODEX_HOME", decoy); + setProjectDir(cwd); + try { + clearFsCache(); + const decoyServers = (await load()).warnings.filter(w => w.includes(decoy)); + expect(decoyServers).toEqual([]); + expect((await skills()).map(skill => skill.path).some(p => p.includes("decoy-home"))).toBe(false); + } finally { + setProjectDir(previous); + } + }); + + it("starts a stdio server with only the variables Codex would pass", async () => { + restoreEnvValue("CODEX_PLUGIN_TEST_SECRET", "must-not-leak"); + const out = temp.join("env.json"); + const transport = new StdioTransport({ + type: "stdio", + command: process.execPath, + args: ["-e", `require("fs").writeFileSync(${JSON.stringify(out)}, JSON.stringify(process.env))`], + env: { MODE: "set" }, + envInherit: ["PATH", "CODEX_PLUGIN_TEST_TOKEN"], + }); + // The script writes its environment and exits; the transport reports the exit. + const exited = Promise.withResolvers(); + transport.onClose = () => exited.resolve(); + try { + await transport.connect(); + await exited.promise; + } finally { + await transport.close(); + } + const seen = JSON.parse(await Bun.file(out).text()) as Record; + expect(seen.CODEX_PLUGIN_TEST_TOKEN).toBe("forwarded-value"); + expect(seen.MODE).toBe("set"); + expect(seen.CODEX_PLUGIN_TEST_SECRET).toBeUndefined(); + expect(seen.CODEX_PLUGIN_TEST_PAT).toBeUndefined(); + }); }); diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index c03e17c514..3d21582963 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,15 +520,18 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), from the version Codex runs in $CODEX_HOME/plugins/cache/// and its .codex-plugin/plugin.json. MCP fields map as the codex provider maps config.toml (env_vars, bearer_token_env_var, tool_timeout_sec), relative command and cwd resolve inside the package, servers are named :, and a plugin's mcp_servers..enabled = false disables one. A server whose plugin definition or mcp_servers. policy sets enabled_tools or a non-empty disabled_tools is skipped with a warning, since Scient Agent's MCP servers carry no tool filter, and a server with a malformed args, env_vars, env, http_headers or env_http_headers is skipped with a warning while its siblings load. OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex; CODEX_HOME from the launch environment, never a project's .env), layered over /etc/codex/config.toml, a table without enabled taking [plugins._default]. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and mcp.json, read under that standard), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there rejecting the plugin. A server must declare exactly one transport with only that transport's Codex fields; package files, cwd and relative commands must stay inside the symlink-resolved package. Stdio servers inherit only Codex's variable list (HOME, PATH and the like, certificate-bundle variables, and the server's env_vars: MCPServer.envInherit), with literal env values; HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers read from the launch environment, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or .: when two enabled plugins share a name; a plugin's mcp_servers..enabled = false disables one. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex (/etc/codex/requirements.toml or managed_config.toml, macOS managed preferences for com.openai.codex, %ProgramData%\\OpenAI\\Codex\\requirements.toml). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts" ], "upstream": [ "packages/coding-agent/src/capability/index.ts", - "packages/coding-agent/src/discovery/codex.ts", + "packages/coding-agent/src/capability/mcp.ts", "packages/coding-agent/src/discovery/index.ts", + "packages/coding-agent/src/mcp/config.ts", + "packages/coding-agent/src/mcp/transports/stdio.ts", + "packages/coding-agent/src/mcp/types.ts", "packages/coding-agent/CHANGELOG.md" ], "verification": [ From e53bde7b83596e3ff6910d6f4acee7994de849d0 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 10:38:12 +0200 Subject: [PATCH 04/30] fix(codex-plugins): match Codex on launch env, policy layers, containment and server rules Second Codex review of 4d90a48614: - getLaunchEnv() (utils env.ts) is the exec environment when known (procfs, --no-env-file), else the environment without the values of the launch folder's Bun-autoloaded dotenv files and the agent's own .env files, whichever folder the agent works in now. CODEX_HOME, header credentials and servers' variables come from it. - A stdio server's env is built as Codex builds it (variable list, absolute CA bundles, env_vars, literal env, minus Codex's non-inheritable names) and it inherits nothing else. - System config merges at every depth; Windows system config and Group Policy RequirementsPath. - Each skill and each legacy manifest's folder must stay in the package; a linked folder rejects. - A declaration's enabled = false and a policy's auth = "ema_auth" disable a server. - Mistyped command/url rejected; Agent Plugins mcp.json must be a regular file, sse is left out, and the legacy declaration's env_vars overlay applies. - Same-named plugins are qualified :, which no plugin name can collide with. --- .../coding-agent/src/scient/codex-plugins.ts | 359 ++++++++++++------ .../test/scient/codex-plugins.test.ts | 128 ++++++- packages/utils/src/env.ts | 24 ++ scripts/scient/divergences.json | 5 +- 4 files changed, 389 insertions(+), 127 deletions(-) diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 3cb6e2fa74..8241154ad0 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -15,7 +15,7 @@ import type { Dirent, Stats } from "node:fs"; import * as fs from "node:fs/promises"; import * as os from "node:os"; import * as path from "node:path"; -import { filterChildShellEnv, getPluginsDir, isRecord, logger } from "@oh-my-pi/pi-utils"; +import { filterChildShellEnv, getLaunchEnv, getPluginsDir, isRecord, logger } from "@oh-my-pi/pi-utils"; import { isUserSourceEnabled, registerProvider } from "../capability"; import { readDirEntries, readFile } from "../capability/fs"; import { type MCPServer, mcpCapability } from "../capability/mcp"; @@ -35,7 +35,8 @@ const LOCAL_VERSION = "local"; /** * Codex's legacy manifests, in its order of precedence after a root Agent Plugins `plugin.json`. - * The first one present decides; a link or non-file there stops the search, as in Codex. + * The first one present decides; a link or non-file there, or a linked folder holding it, stops + * the search, as in Codex. */ const LEGACY_MANIFESTS = [".codex-plugin/plugin.json", ".claude-plugin/plugin.json", ".cursor-plugin/plugin.json"]; @@ -55,52 +56,41 @@ const HOST_PLUGINS: Record = { /** OpenAI's launcher for its signed Computer Use client, which serves only Codex (the Messages plugin). */ const HOST_LAUNCHER = "computer-use-client-launcher"; +const IS_WINDOWS = process.platform === "win32"; + /** * The variables Codex passes a plugin's local stdio server from its own environment, besides * those the server names in `env_vars` (codex-rs/rmcp-client `DEFAULT_ENV_VARS`, and - * `WINDOWS_CORE_ENV_VARS` on Windows), plus the certificate-bundle variables it always keeps - * (`CUSTOM_CA_ENV_KEYS`). Nothing else, API keys included, reaches the server. + * `WINDOWS_CORE_ENV_VARS` on Windows). Nothing else, API keys included, reaches the server. */ -const INHERITED_ENV = - process.platform === "win32" - ? [ - "PATH", - "PATHEXT", - "SHELL", - "COMSPEC", - "SYSTEMROOT", - "WINDIR", - "SYSTEMDRIVE", - "USERNAME", - "USERDOMAIN", - "USERPROFILE", - "HOMEDRIVE", - "HOMEPATH", - "PROGRAMFILES", - "PROGRAMFILES(X86)", - "PROGRAMW6432", - "PROGRAMDATA", - "LOCALAPPDATA", - "APPDATA", - "TEMP", - "TMP", - "TMPDIR", - "POWERSHELL", - "PWSH", - ] - : [ - "HOME", - "LOGNAME", - "PATH", - "SHELL", - "USER", - "__CF_USER_TEXT_ENCODING", - "LANG", - "LC_ALL", - "TERM", - "TMPDIR", - "TZ", - ]; +const INHERITED_ENV = IS_WINDOWS + ? [ + "PATH", + "PATHEXT", + "SHELL", + "COMSPEC", + "SYSTEMROOT", + "WINDIR", + "SYSTEMDRIVE", + "USERNAME", + "USERDOMAIN", + "USERPROFILE", + "HOMEDRIVE", + "HOMEPATH", + "PROGRAMFILES", + "PROGRAMFILES(X86)", + "PROGRAMW6432", + "PROGRAMDATA", + "LOCALAPPDATA", + "APPDATA", + "TEMP", + "TMP", + "TMPDIR", + "POWERSHELL", + "PWSH", + ] + : ["HOME", "LOGNAME", "PATH", "SHELL", "USER", "__CF_USER_TEXT_ENCODING", "LANG", "LC_ALL", "TERM", "TMPDIR", "TZ"]; +/** Certificate bundles Codex always passes on, as absolute paths (`CUSTOM_CA_ENV_KEYS`). */ const CA_BUNDLE_ENV = [ "CODEX_CA_CERTIFICATE", "SSL_CERT_FILE", @@ -114,6 +104,18 @@ const CA_BUNDLE_ENV = [ "npm_config_cafile", "NPM_CONFIG_CAFILE", ]; +/** Names Codex never lets a model-reachable process have (`NON_INHERITABLE_ENV_VARS`), even from `env`. */ +const NON_INHERITABLE_ENV = [ + "CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN", + "NODE_REPL_AUTH_TOKEN", + "CODEX_GUARDIAN_DECISIONS_API_KEY", + "OPENAI_FEDERATION_RULE_ID", + "OPENAI_IDENTITY_TOKEN_FILE", + "OPENAI_IDENTITY_TOKEN_SOURCE", + "OPENAI_SPIFFE_AUDIENCE", + "OPENAI_SPIFFE_ID", + "OPENAI_WORKLOAD_IDENTITY_CONTEXT", +]; /** Fields Codex rejects on a stdio server, and on a streamable HTTP server. */ const NOT_STDIO = [ @@ -137,7 +139,10 @@ interface CodexPlugin { id: string; name: string; marketplace: string; - /** Skill namespace and MCP server prefix: the name, or `.` when two enabled plugins share it. */ + /** + * Skill namespace and MCP server prefix: the name, or `:` when two enabled + * plugins share it. Neither part may contain `:`, so the qualified form never meets another. + */ label: string; /** The active version's folder, symlinks resolved; every package path stays inside it. */ root: string; @@ -152,7 +157,7 @@ interface CodexPlugin { interface PluginList { plugins: CodexPlugin[]; warnings: string[]; - /** Scient Agent's launch environment, without a project's `.env` values. */ + /** Scient Agent's launch environment, without any `.env` file's values. */ env: Record; } @@ -187,6 +192,47 @@ async function lstatOrNull(file: string): Promise { } } +/** Environment names compare case-insensitively on Windows, as Codex compares them there. */ +function sameEnvName(left: string, right: string): boolean { + return IS_WINDOWS ? left.toUpperCase() === right.toUpperCase() : left === right; +} + +function envValue(env: Record, name: string): string | undefined { + if (!IS_WINDOWS) return env[name]; + const key = Object.keys(env).find(candidate => sameEnvName(candidate, name)); + return key === undefined ? undefined : env[key]; +} + +/** + * The environment Codex gives a plugin's local stdio server: its variable list and the server's + * `env_vars` from the launch environment, certificate bundles as absolute paths, then the + * server's own `env`, never the names Codex withholds. The server inherits nothing else. + */ +function stdioEnv( + env: Record, + forwarded: readonly string[], + declared: Record | undefined, +): Record { + const result: Record = {}; + const put = (name: string, value: string) => { + for (const key of Object.keys(result)) if (sameEnvName(key, name)) delete result[key]; + result[name] = value; + }; + for (const name of [...INHERITED_ENV, ...forwarded]) { + const value = envValue(env, name); + if (value !== undefined) put(name, value); + } + for (const name of CA_BUNDLE_ENV) { + const value = envValue(env, name); + if (value) put(name, path.resolve(value)); + } + for (const [name, value] of Object.entries(declared ?? {})) put(name, value); + for (const key of Object.keys(result)) { + if (NON_INHERITABLE_ENV.some(name => sameEnvName(name, key))) delete result[key]; + } + return result; +} + /** The first legacy manifest present, read; null (with a warning) when none is usable. */ async function readLegacyManifest( root: string, @@ -195,6 +241,12 @@ async function readLegacyManifest( ): Promise<{ manifestPath: string; manifest: Record } | null> { for (const relative of LEGACY_MANIFESTS) { const manifestPath = path.join(root, relative); + const folder = await lstatOrNull(path.dirname(manifestPath)); + if (folder === null) continue; + if (!folder.isDirectory()) { + warnings.push(`[codex-plugins] Ignoring ${id}: ${path.dirname(relative)} is not a folder`); + return null; + } const stat = await lstatOrNull(manifestPath); if (stat === null) continue; if (!stat.isFile()) { @@ -276,17 +328,37 @@ async function readToml(file: string, warnings: string[]): Promise, higher: Record): Record { + const merged: Record = { ...lower }; + for (const [key, value] of Object.entries(higher)) { + const below = merged[key]; + merged[key] = isRecord(below) && isRecord(value) ? mergeLayers(below, value) : value; + } + return merged; +} + +/** Codex's system folder: `/etc/codex`, or `OpenAI\Codex` in ProgramData on Windows. */ +function systemDir(): string { + return IS_WINDOWS ? path.join(process.env.ProgramData || "C:\\ProgramData", "OpenAI", "Codex") : "/etc/codex"; +} + /** - * Where an organization's Codex policy lives (requirements, managed config, macOS managed - * preferences). Codex enforces them over the user's config; Scient Agent cannot, so it loads no - * plugins while one is present. + * The first organization policy Codex would enforce over the user's config (requirements, a + * legacy managed config, macOS managed preferences, or a Windows Group Policy requirements path), + * or undefined. Scient Agent cannot apply one, so it loads no plugins while one is present. */ -function managedPolicyFiles(): string[] { - if (process.platform === "win32") { - const programData = process.env.ProgramData || "C:\\ProgramData"; - return [path.join(programData, "OpenAI", "Codex", "requirements.toml")]; +async function managedPolicy(): Promise { + if (IS_WINDOWS) { + const key = "HKLM\\SOFTWARE\\Policies\\OpenAI\\Codex"; + const query = Bun.spawnSync(["reg", "query", key, "/v", "RequirementsPath", "/reg:64"], { + stdout: "ignore", + stderr: "ignore", + }); + if (query.exitCode === 0) return `${key}\\RequirementsPath`; } - const files = ["/etc/codex/requirements.toml", "/etc/codex/managed_config.toml"]; + const files = [path.join(systemDir(), "requirements.toml")]; + if (!IS_WINDOWS) files.push("/etc/codex/managed_config.toml"); if (process.platform === "darwin") { const managed = "/Library/Managed Preferences"; files.push( @@ -294,19 +366,20 @@ function managedPolicyFiles(): string[] { path.join(managed, os.userInfo().username, "com.openai.codex.plist"), ); } - return files; + for (const file of files) if ((await lstatOrNull(file)) !== null) return file; + return undefined; } /** Installed, enabled Codex plugins outside {@link HOST_PLUGINS}. */ async function listCodexPlugins(ctx: LoadContext): Promise { const warnings: string[] = []; - // The environment Scient Agent was launched with: a project's `.env` never chooses Codex's - // home or the credentials sent to a plugin's server. - const env = filterChildShellEnv(Bun.env); - for (const file of managedPolicyFiles()) { - if ((await lstatOrNull(file)) === null) continue; + // The environment Scient Agent was launched with: no `.env` file, from this project or one the + // agent worked in before, chooses Codex's home or what reaches a plugin's server. + const env = filterChildShellEnv(getLaunchEnv()); + const policy = await managedPolicy(); + if (policy !== undefined) { warnings.push( - `[codex-plugins] Codex is managed by an organization policy (${file}), which Scient Agent cannot apply; no Codex plugins load`, + `[codex-plugins] Codex is managed by an organization policy (${policy}), which Scient Agent cannot apply; no Codex plugins load`, ); return { plugins: [], warnings, env }; } @@ -314,12 +387,9 @@ async function listCodexPlugins(ctx: LoadContext): Promise { const home = env.CODEX_HOME?.trim() || path.join(ctx.home, ".codex"); const user = await readToml(path.join(home, "config.toml"), warnings); if (user === null) return { plugins: [], warnings, env }; - // The system layer sits under the user's; per-plugin tables and `_default` merge by key. - const system = process.platform === "win32" ? null : await readToml("/etc/codex/config.toml", warnings); - const table: Record = { - ...(isRecord(system?.plugins) ? system.plugins : {}), - ...(isRecord(user.plugins) ? user.plugins : {}), - }; + const system = await readToml(path.join(systemDir(), "config.toml"), warnings); + const merged = mergeLayers(system ?? {}, user); + const table = isRecord(merged.plugins) ? merged.plugins : {}; const defaults = isRecord(table._default) ? table._default : {}; const enabledByDefault = defaults.enabled !== false; const read = await Promise.all( @@ -330,12 +400,12 @@ async function listCodexPlugins(ctx: LoadContext): Promise { }), ); const found = read.filter(plugin => plugin !== null); - const sharedNames = new Set( + const shared = new Set( found.filter((plugin, index) => found.findIndex(other => other.name === plugin.name) !== index).map(p => p.name), ); const plugins = found.map(plugin => ({ ...plugin, - label: sharedNames.has(plugin.name) ? `${plugin.name}.${plugin.marketplace}` : plugin.name, + label: shared.has(plugin.name) ? `${plugin.name}:${plugin.marketplace}` : plugin.name, })); return { plugins, warnings, env }; } @@ -354,7 +424,8 @@ async function packagePath(plugin: CodexPlugin, value: unknown, key: string, war async function loadSkills(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; const { plugins, warnings } = await listCodexPlugins(ctx); - const dirsByPlugin = await Promise.all( + const items: Skill[] = []; + await Promise.all( plugins.map(async plugin => { const declared = plugin.format === "standard" ? undefined : plugin.manifest.skills; const entries = declared == null ? ["skills"] : Array.isArray(declared) ? declared : [declared]; @@ -363,19 +434,22 @@ async function loadSkills(ctx: LoadContext): Promise> { const dir = await packagePath(plugin, entry, "skills", warnings); if (dir !== null) dirs.add(dir); } - return { plugin, dirs: [...dirs] }; + for (const dir of dirs) { + const result = await scanSkillsFromDir(ctx, { + dir, + providerId: PROVIDER_ID, + level: "user", + pluginName: plugin.label, + }); + if (result.warnings) warnings.push(...result.warnings); + // A skill folder or SKILL.md linked out of the package does not load, as in Codex. + for (const skill of result.items) { + if ((await resolveContainedPath(plugin.root, skill.path)).status === "ok") items.push(skill); + else warnings.push(`[codex-plugins] Ignoring skill outside ${plugin.id}: ${skill.path}`); + } + } }), ); - const scans = dirsByPlugin.flatMap(({ plugin, dirs }) => - dirs.map(dir => - scanSkillsFromDir(ctx, { dir, providerId: PROVIDER_ID, level: "user", pluginName: plugin.label }), - ), - ); - const items: Skill[] = []; - for (const result of await Promise.all(scans)) { - items.push(...result.items); - if (result.warnings) warnings.push(...result.warnings); - } return { items, warnings }; } @@ -405,11 +479,18 @@ const isStringList = (value: unknown): value is string[] => const isStringMap = (value: unknown): value is Record => isRecord(value) && Object.values(value).every(item => typeof item === "string"); -/** Codex's `env_vars` entry: a name, or `{ name, source = "local" }`. */ -function envVarName(entry: unknown): string | undefined { - if (typeof entry === "string") return entry; - if (isRecord(entry) && typeof entry.name === "string" && (entry.source ?? "local") === "local") return entry.name; - return undefined; +/** The names of Codex's `env_vars` entries (a name, or `{ name, source = "local" }`); null when one is malformed. */ +function envVarNames(value: unknown): string[] | null { + if (value == null) return []; + if (!Array.isArray(value)) return null; + const names: string[] = []; + for (const entry of value) { + if (typeof entry === "string") names.push(entry); + else if (isRecord(entry) && typeof entry.name === "string" && (entry.source ?? "local") === "local") { + names.push(entry.name); + } else return null; + } + return names; } type Mapped = { server: Partial } | { error: string }; @@ -423,23 +504,27 @@ async function mapLegacyServer( server: Record, env: Record, ): Promise { - const isStdio = typeof server.command === "string"; - if (!isStdio && typeof server.url !== "string") return { error: "it declares neither command nor url" }; + if (server.command != null && typeof server.command !== "string") return { error: "invalid command" }; + if (server.url != null && typeof server.url !== "string") return { error: "invalid url" }; + const isStdio = server.command != null; + if (!isStdio && server.url == null) return { error: "it declares neither command nor url" }; const wrong = (isStdio ? NOT_STDIO : NOT_HTTP).find(key => server[key] != null); if (wrong !== undefined) return { error: `${wrong} is not supported for ${isStdio ? "stdio" : "streamable HTTP"}` }; - const timeout = - typeof server.tool_timeout_sec === "number" && server.tool_timeout_sec > 0 - ? { timeout: server.tool_timeout_sec * 1000 } - : {}; + const common = { + // `enabled = false` in the declaration turns the server off in Codex. + ...(server.enabled === false && { enabled: false }), + ...(typeof server.tool_timeout_sec === "number" && + server.tool_timeout_sec > 0 && { timeout: server.tool_timeout_sec * 1000 }), + }; if (isStdio) { const command = (server.command as string).trim(); if (!command) return { error: "command is empty" }; if (server.args != null && !isStringList(server.args)) return { error: "invalid args" }; if (server.env != null && !isStringMap(server.env)) return { error: "invalid env" }; - if (server.env_vars != null && !Array.isArray(server.env_vars)) return { error: "invalid env_vars" }; - const forwarded = ((server.env_vars as unknown[] | undefined) ?? []).map(envVarName); - if (forwarded.some(name => name === undefined)) return { error: "invalid env_vars" }; + const forwarded = envVarNames(server.env_vars); + if (forwarded === null) return { error: "invalid env_vars" }; + if (server.cwd != null && typeof server.cwd !== "string") return { error: "invalid cwd" }; // Codex runs a server from the package unless it names a folder inside it. const cwd = typeof server.cwd === "string" ? path.resolve(plugin.root, server.cwd) : plugin.root; if (!(await isContainedResolved(plugin.root, cwd))) return { error: `cwd ${server.cwd} leaves the package` }; @@ -454,11 +539,11 @@ async function mapLegacyServer( transport: "stdio", command: resolvedCommand, ...(server.args != null && { args: server.args as string[] }), - ...(server.env != null && { env: server.env as Record }), + env: stdioEnv(env, forwarded, server.env as Record | undefined), envPolicy: "literal", - envInherit: [...new Set([...INHERITED_ENV, ...CA_BUNDLE_ENV, ...(forwarded as string[])])], + envInherit: [], cwd, - ...timeout, + ...common, }, }; } @@ -478,10 +563,10 @@ async function mapLegacyServer( // Static headers are data; the others name variables of the launch environment, as in Codex. const headers: Record = { ...(server.http_headers as Record | undefined) }; for (const [header, name] of Object.entries((server.env_http_headers ?? {}) as Record)) { - const value = env[name]?.trim(); + const value = envValue(env, name)?.trim(); if (value) headers[header] = value; } - const bearer = server.bearer_token_env_var ? env[server.bearer_token_env_var as string]?.trim() : undefined; + const bearer = server.bearer_token_env_var ? envValue(env, server.bearer_token_env_var as string)?.trim() : ""; if (bearer) headers.Authorization = `Bearer ${bearer}`; // Codex's OAuth client settings, snake_case or the camelCase plugins may use. const oauthTable = (server.oauth ?? {}) as Record; @@ -506,19 +591,21 @@ async function mapLegacyServer( headerPolicy: "origin-locked", ...(Object.keys(oauth).length > 0 && { oauth }), ...(resource !== null && { auth: { type: "oauth" as const, resource } }), - ...timeout, + ...common, }, }; } -/** The plugin's server map: the manifest's inline map, its named file, or the package's `.mcp.json`. */ +/** A legacy server map: the manifest's inline map, its named file, or the package's `.mcp.json`. */ async function readLegacyServerMap( plugin: CodexPlugin, + manifest: Record, + manifestPath: string, warnings: string[], ): Promise<{ servers: Record; sourcePath: string } | null> { - const declared = plugin.manifest.mcpServers; + const declared = manifest.mcpServers; let parsed: unknown = declared; - let sourcePath = plugin.manifestPath; + let sourcePath = manifestPath; if (!isRecord(declared)) { const file = await packagePath(plugin, declared ?? ".mcp.json", "mcpServers", warnings); if (file === null) { @@ -539,18 +626,26 @@ async function readLegacyServerMap( return isRecord(servers) ? { servers, sourcePath } : null; } -/** An Agent Plugins package's `mcp.json`, read under that standard, keyed by server name. */ +/** + * An Agent Plugins package's `mcp.json`, read under that standard and Codex's rules for it: the + * file must be a regular file at the root, `sse` servers do not run, and a stdio server takes the + * `env_vars` its package's legacy declaration of the same server adds (dropping the `${NAME}` + * placeholders those variables replace). + */ async function readStandardServers( ctx: LoadContext, plugin: CodexPlugin, + env: Record, warnings: string[], ): Promise<{ servers: Record>; sourcePath: string } | null> { - const resolved = await resolveContainedPath(plugin.root, path.join(plugin.root, "mcp.json")); - if (resolved.status !== "ok") { - if (resolved.status === "outside") warnings.push(`[codex-plugins] ${plugin.id}: mcp.json leaves the package`); + const file = path.join(plugin.root, "mcp.json"); + const stat = await lstatOrNull(file); + if (stat === null) return null; + if (!stat.isFile()) { + warnings.push(`[codex-plugins] ${plugin.id}: mcp.json is not a regular file`); return null; } - const raw = await readFile(resolved.realPath); + const raw = await readFile(file); if (raw === null) return null; const digest = new Bun.CryptoHasher("sha256").update(`codex:${plugin.id}`).digest("hex").slice(0, 16); const pluginData = path.join(getPluginsDir(ctx.home), "data", `codex-${plugin.name}-${digest}`); @@ -560,15 +655,37 @@ async function readStandardServers( return null; } warnings.push(...result.warnings.map(warning => `[codex-plugins] ${plugin.id}: ${warning}`)); - if (result.servers.some(server => server.transport === "stdio")) await fs.mkdir(pluginData, { recursive: true }); + const quiet: string[] = []; + const legacy = await readLegacyManifest(plugin.root, plugin.id, quiet); + const overlay = (await readLegacyServerMap(plugin, legacy?.manifest ?? {}, legacy?.manifestPath ?? file, quiet)) + ?.servers; const servers: Record> = {}; for (const { name, ...server } of result.servers) { - servers[name] = - server.transport === "stdio" - ? { ...server, envPolicy: "literal", envInherit: [...INHERITED_ENV, ...CA_BUNDLE_ENV] } - : { ...server, headerPolicy: "origin-locked" }; + if (server.transport === "sse") { + warnings.push(`[codex-plugins] Skipping MCP server "${name}" of ${plugin.id}: Codex does not run sse servers`); + continue; + } + if (server.transport !== "stdio") { + servers[name] = { ...server, headerPolicy: "origin-locked" }; + continue; + } + const declared = overlay?.[name]; + const forwarded = isRecord(declared) && declared.command != null ? (envVarNames(declared.env_vars) ?? []) : []; + // A `${NAME}` placeholder for a forwarded variable gives way to that variable's real value. + const kept = Object.entries(server.env ?? {}).filter( + ([key, value]) => + !forwarded.some(variable => sameEnvName(key, variable) && sameEnvName(value, `\${${variable}}`)), + ); + servers[name] = { + ...server, + env: stdioEnv(env, forwarded, Object.fromEntries(kept)), + envPolicy: "literal", + envInherit: [], + }; } - return { servers, sourcePath: resolved.realPath }; + if (Object.values(servers).some(server => server.transport === "stdio")) + await fs.mkdir(pluginData, { recursive: true }); + return { servers, sourcePath: file }; } async function loadMCPServers(ctx: LoadContext): Promise> { @@ -577,8 +694,11 @@ async function loadMCPServers(ctx: LoadContext): Promise> const items: MCPServer[] = []; for (const plugin of plugins) { const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; - const standard = plugin.format === "standard" ? await readStandardServers(ctx, plugin, warnings) : null; - const legacy = plugin.format === "legacy" ? await readLegacyServerMap(plugin, warnings) : null; + const standard = plugin.format === "standard" ? await readStandardServers(ctx, plugin, env, warnings) : null; + const legacy = + plugin.format === "legacy" + ? await readLegacyServerMap(plugin, plugin.manifest, plugin.manifestPath, warnings) + : null; const declared: Record = standard?.servers ?? legacy?.servers ?? {}; const sourcePath = standard?.sourcePath ?? legacy?.sourcePath ?? plugin.manifestPath; for (const [serverName, server] of Object.entries(declared)) { @@ -609,8 +729,9 @@ async function loadMCPServers(ctx: LoadContext): Promise> items.push({ name: `${plugin.label}:${serverName}`, ...mapped.server, - // `[plugins."".mcp_servers.] enabled = false` turns the server off in Codex. - ...(policy.enabled === false && { enabled: false }), + // The plugin's `mcp_servers.` table turns the server off with `enabled = false`, and + // with the enterprise sign-in Codex no longer supports there. + ...((policy.enabled === false || policy.auth === "ema_auth") && { enabled: false }), _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), }); } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 880d9e0b9d..e8b247b94d 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -169,10 +169,10 @@ describe("installed Codex plugins", () => { transport: "stdio", }); // Codex passes its own short list plus the server's env_vars, never the whole environment. - expect(byName.get("research:papers")?.env).toBeUndefined(); - expect(byName.get("research:papers")?.envInherit).toEqual( - expect.arrayContaining(["PATH", "HOME", "CODEX_PLUGIN_TEST_TOKEN"]), - ); + const papers = byName.get("research:papers"); + expect(papers?.envInherit).toEqual([]); + expect(papers?.env).toMatchObject({ PATH: process.env.PATH, CODEX_PLUGIN_TEST_TOKEN: "forwarded-value" }); + expect(papers?.env?.CODEX_PLUGIN_TEST_PAT).toBeUndefined(); expect(byName.get("research:tracker")).toMatchObject({ url: "https://mcp.example.test/mcp", headers: { Authorization: "Bearer pat-value" }, @@ -384,16 +384,132 @@ describe("installed Codex plugins", () => { expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers path outside linked@market")); }); - it("keeps two marketplaces' same-named plugins apart", async () => { + it("keeps same-named plugins from different marketplaces apart, whatever their names", async () => { const server = { mcpServers: { api: { command: "node" } } }; await install("twin@alpha", { ".codex-plugin/plugin.json": { name: "twin" }, ".mcp.json": server }); await install("twin@beta", { ".codex-plugin/plugin.json": { name: "twin" }, ".mcp.json": server }); + // Its name is what `twin` qualified with `alpha` would look like if dots qualified names. + await install("twin.alpha@gamma", { ".codex-plugin/plugin.json": { name: "twin.alpha" }, ".mcp.json": server }); expect([...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort()).toEqual([ "twin.alpha:api", - "twin.beta:api", + "twin:alpha:api", + "twin:beta:api", ]); }); + it("keeps startup dotenv values out of the launch environment after the agent changes folders", async () => { + const first = temp.join("first-project"); + const second = temp.join("second-project"); + await Bun.write(path.join(first, ".env"), "CODEX_HOME=/from/dotenv\nCODEX_PLUGIN_TEST_SECRET=dotenv-secret\n"); + await Bun.write(path.join(first, ".env.local"), "CODEX_PLUGIN_TEST_LOCAL=local-secret\n"); + await fs.mkdir(second, { recursive: true }); + // A fresh process, so the dotenv files load at its own startup in `first`. + const utils = path.join(import.meta.dir, "../../../utils/src/index.ts"); + const script = [ + `const { getLaunchEnv, setProjectDir } = await import(${JSON.stringify(utils)});`, + `setProjectDir(${JSON.stringify(second)});`, + "const env = getLaunchEnv();", + "console.log(JSON.stringify([Bun.env.CODEX_HOME, env.CODEX_HOME, env.CODEX_PLUGIN_TEST_SECRET, env.CODEX_PLUGIN_TEST_LOCAL, env.CODEX_PLUGIN_TEST_PAT]));", + ].join("\n"); + // With --no-env-file only the agent's own loader reads .env; without it Bun autoloads + // .env and .env.local too, as when the agent runs from source. + for (const flags of [["--no-env-file"], []]) { + const run = Bun.spawnSync([process.execPath, ...flags, "-e", script], { + cwd: first, + env: { PATH: process.env.PATH, HOME: temp.join("home"), CODEX_PLUGIN_TEST_PAT: "exported" }, + }); + // JSON writes a missing value as null. + expect(JSON.parse(run.stdout.toString().trim().split("\n").at(-1) ?? "null")).toEqual([ + "/from/dotenv", + null, + null, + null, + "exported", + ]); + } + }); + + it("follows Codex on disabled declarations, withheld variables and linked package folders", async () => { + await install( + "rules@market", + { + ".codex-plugin/plugin.json": { name: "rules" }, + ".mcp.json": { + mcpServers: { + off: { command: "node", enabled: false }, + retired: { command: "node", args: ["retired.mjs"] }, + numeric: { command: 7, url: "https://mcp.example.test/mcp" }, + withheld: { command: "node", env: { NODE_REPL_AUTH_TOKEN: "x", KEEP: "y" } }, + }, + }, + }, + ["enabled = true", '[plugins."rules@market".mcp_servers.retired]', 'auth = "ema_auth"'], + ); + const outside = temp.join("outside-manifest"); + await Bun.write(path.join(outside, "plugin.json"), JSON.stringify({ name: "linkedfolder" })); + const linkedRoot = await install("linkedfolder@market", { + ".mcp.json": { mcpServers: { x: { command: "node" } } }, + }); + await fs.symlink(outside, path.join(linkedRoot, ".codex-plugin"), "dir"); + clearFsCache(); + const { byName, warnings } = await load(); + expect(byName.get("rules:off")?.enabled).toBe(false); + expect(byName.get("rules:retired")?.enabled).toBe(false); + expect(byName.has("rules:numeric")).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining('"numeric" of rules@market: invalid command')); + expect(byName.get("rules:withheld")?.env).toMatchObject({ KEEP: "y" }); + expect(byName.get("rules:withheld")?.env?.NODE_REPL_AUTH_TOKEN).toBeUndefined(); + expect(byName.has("linkedfolder:x")).toBe(false); + expect(warnings).toContainEqual( + expect.stringContaining("Ignoring linkedfolder@market: .codex-plugin is not a folder"), + ); + }); + + it("drops a skill linked out of a real skills folder", async () => { + const outside = temp.join("outside-skill"); + await writeSkill(outside, "escaped"); + const root = await install("mixed@market", { ".codex-plugin/plugin.json": { name: "mixed" } }); + await writeSkill(root, "inside"); + await fs.symlink(path.join(outside, "skills", "escaped"), path.join(root, "skills", "escaped"), "dir"); + clearFsCache(); + const names = (await skills()).map(skill => skill.name); + expect(names).toContain("inside"); + expect(names).not.toContain("escaped"); + }); + + it("applies Codex's rules to an Agent Plugins package's mcp.json", async () => { + restoreEnvValue("CODEX_PLUGIN_TEST_PAT", "pat-value"); + const schema = "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json"; + await install("portable2@market", { + "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name: "portable2" }, + "mcp.json": { + $schema: schema, + mcpServers: { + events: { type: "sse", url: "https://mcp.example.test/sse" }, + local: { + type: "stdio", + command: "node", + env: { CODEX_PLUGIN_TEST_PAT: "$" + "{CODEX_PLUGIN_TEST_PAT}", MODE: "x" }, + }, + }, + }, + // The package's legacy declaration forwards the variable the placeholder stands for. + ".codex-plugin/plugin.json": { name: "portable2" }, + ".mcp.json": { mcpServers: { local: { command: "node", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } } }, + }); + const linked = await install("linkedmcp@market", { + "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name: "linkedmcp" }, + "real-mcp.json": { $schema: schema, mcpServers: { y: { type: "stdio", command: "node" } } }, + }); + await fs.symlink(path.join(linked, "real-mcp.json"), path.join(linked, "mcp.json")); + clearFsCache(); + const { byName, warnings } = await load(); + expect(byName.has("portable2:events")).toBe(false); + expect(byName.get("portable2:local")?.env).toMatchObject({ CODEX_PLUGIN_TEST_PAT: "pat-value", MODE: "x" }); + expect(byName.has("linkedmcp:y")).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining("linkedmcp@market: mcp.json is not a regular file")); + }); + it("reads Codex's manifest precedence: a root Agent Plugins manifest, then the legacy folders", async () => { await install("portable@market", { "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name: "portable" }, diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts index a6d5b777e5..6e9654538f 100644 --- a/packages/utils/src/env.ts +++ b/packages/utils/src/env.ts @@ -383,6 +383,30 @@ export function withOmpOtelEnv(fn: () => T): T { } } +// scient(SA-CODEX-PLUGINS): begin - Codex plugins read only the environment the agent was launched with. +const launchProjectDir = getProjectDir(); +let launchDotenvValues: ReadonlySet | undefined; + +/** + * The environment the agent was launched with, whichever folder it works in now: the exec + * environment when it is known (procfs, `--no-env-file`), else the environment without any value + * the launch folder's dotenv files (all those Bun autoloads) or the agent's own `.env` files + * (agent, config root, home) define. Codex reads its home, its plugins' credentials and their + * servers' variables from its launch environment alone; so does the agent when it runs them. + */ +export function getLaunchEnv(): Record { + if (launchEnvValues) return Object.fromEntries(launchEnvValues); + launchDotenvValues ??= new Set(getDotenvEnvValues(launchProjectDir, Bun.env)); + const result: Record = {}; + for (const key in Bun.env) { + const value = Bun.env[key]; + if (value === undefined || launchDotenvValues.has(value)) continue; + if ([projectEnv, agentEnv, piEnv, homeEnv].some(file => file[key] === value)) continue; + result[key] = value; + } + return result; +} +// scient(SA-CODEX-PLUGINS): end for (const file of [projectEnv, agentEnv, piEnv, homeEnv]) { for (const key in file) { // scient(SA-ROOT): begin - A host-assigned root owns the agent's own variables. diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 3d21582963..eaf8da666b 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex; CODEX_HOME from the launch environment, never a project's .env), layered over /etc/codex/config.toml, a table without enabled taking [plugins._default]. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and mcp.json, read under that standard), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there rejecting the plugin. A server must declare exactly one transport with only that transport's Codex fields; package files, cwd and relative commands must stay inside the symlink-resolved package. Stdio servers inherit only Codex's variable list (HOME, PATH and the like, certificate-bundle variables, and the server's env_vars: MCPServer.envInherit), with literal env values; HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers read from the launch environment, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or .: when two enabled plugins share a name; a plugin's mcp_servers..enabled = false disables one. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex (/etc/codex/requirements.toml or managed_config.toml, macOS managed preferences for com.openai.codex, %ProgramData%\\OpenAI\\Codex\\requirements.toml). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in ProgramData), a table without enabled taking [plugins._default]. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment when known, else the environment without any value the launch folder's Bun-autoloaded dotenv files or the agent's own .env files define, whichever folder the agent works in now), never a .env file. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars its package's legacy declaration of it adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there or a linked folder holding it rejecting the plugin. A legacy server must declare exactly one transport with correctly typed fields and only that transport's Codex fields; package files, each skill, cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name. enabled = false in a declaration or in the plugin's mcp_servers. table, or auth = \"ema_auth\" there, disables a server. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex (requirements.toml in the system folder or at the Windows Group Policy RequirementsPath, /etc/codex/managed_config.toml, macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts" @@ -532,7 +532,8 @@ "packages/coding-agent/src/mcp/config.ts", "packages/coding-agent/src/mcp/transports/stdio.ts", "packages/coding-agent/src/mcp/types.ts", - "packages/coding-agent/CHANGELOG.md" + "packages/coding-agent/CHANGELOG.md", + "packages/utils/src/env.ts" ], "verification": [ "bun --cwd=packages/coding-agent test test/scient/codex-plugins.test.ts" From 65ffa2e5667304627352b9576dda9f6a540faf1f Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 10:57:08 +0200 Subject: [PATCH 05/30] fix(codex-plugins): authoritative launch env, Codex approvals and EMA, overlay, skill containment, fail-closed policy Third Codex review of e53bde7b83: - getLaunchEnv() returns only an authoritative snapshot (procfs, --no-env-file, or the compiled binary's environment before the agent's .env files, told by its bundled module URL); otherwise nothing loads, with a warning. The loader takes it through filterProcessEnv. - An ema_auth key in a plugin's mcp_servers. table disables the server; a server whose tools Codex asks about (prompt, writes or an unknown approval mode) is skipped. - Agent Plugins stdio servers take env_vars only from a valid .codex-plugin declaration of the same server as a valid stdio server; Codex's non-inheritable names match in any case. - Skills are scanned as Codex scans them (recursive for legacy plugins), each folder and SKILL.md contained before it is read, and carry containRoot for skill://. - A policy location that cannot be checked counts as managed; Windows uses the ProgramData known folder and managed_config.toml in CODEX_HOME. - Tests stand in for the launch environment instead of relying on mutations after Bun starts. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- .../coding-agent/src/scient/codex-plugins.ts | 241 ++++++++++++++---- .../test/scient/codex-plugins.test.ts | 129 +++++++--- packages/utils/src/env.ts | 36 +-- scripts/scient/divergences.json | 2 +- 6 files changed, 304 insertions(+), 108 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index f19dd7a614..55411cba48 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), one transport per server with only that transport's fields, package files and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME` and header credentials come from the launch environment, never a project's `.env`. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`) is skipped with a warning, because Scient Agent cannot enforce the filter; so is a server with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its skill scan, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file`, outside Linux) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`) or asks about (`prompt` or `writes` approval) is skipped with a warning, because Scient Agent cannot enforce either; so is a server with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index a621a19345..6910b1ffae 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]` and its manifest formats included), servers that inherit only the variables Codex would pass them, plugin values taken literally, and package paths kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`) and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex. +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]` and its manifest formats included), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`) or asks about before running (`prompt` or `writes` approval) and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, or when Scient Agent runs from source without `--no-env-file` outside Linux. ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 8241154ad0..992ba290cf 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -8,22 +8,23 @@ * here under Codex's own rules: its manifest precedence, its MCP field validation, and the * environment and header handling it gives a plugin's servers. App references (`.app.json`) do * not: a person's ChatGPT apps reach the agent through the ChatGPT sign-in (SA-CHATGPT-APPS). - * Plugins and servers that run only inside OpenAI's own apps are left out, and so is everything - * when an organization manages Codex, since its policy cannot be read here. + * Plugins and servers that run only inside OpenAI's own apps are left out, and so are servers + * whose tools Codex narrows or asks about. Nothing loads when an organization manages Codex, since + * its policy cannot be applied here, or when this run cannot tell its launch environment apart. */ import type { Dirent, Stats } from "node:fs"; import * as fs from "node:fs/promises"; import * as os from "node:os"; import * as path from "node:path"; -import { filterChildShellEnv, getLaunchEnv, getPluginsDir, isRecord, logger } from "@oh-my-pi/pi-utils"; +import { filterProcessEnv, getLaunchEnv, getPluginsDir, isRecord, logger, parseFrontmatter } from "@oh-my-pi/pi-utils"; import { isUserSourceEnabled, registerProvider } from "../capability"; import { readDirEntries, readFile } from "../capability/fs"; import { type MCPServer, mcpCapability } from "../capability/mcp"; -import { type Skill, skillCapability } from "../capability/skill"; +import { type Skill, type SkillFrontmatter, skillCapability } from "../capability/skill"; import type { LoadContext, LoadResult } from "../capability/types"; import { classifyAgentPluginRoot, parseAgentPluginMcp } from "../discovery/agent-plugin-format"; import { isContainedResolved, realpathIfExists, resolveContainedPath } from "../discovery/contained-path"; -import { createSourceMeta, scanSkillsFromDir } from "../discovery/helpers"; +import { compareSkillOrder, createSourceMeta } from "../discovery/helpers"; const PROVIDER_ID = "codex-plugins"; const DISPLAY_NAME = "Codex Plugins"; @@ -104,7 +105,10 @@ const CA_BUNDLE_ENV = [ "npm_config_cafile", "NPM_CONFIG_CAFILE", ]; -/** Names Codex never lets a model-reachable process have (`NON_INHERITABLE_ENV_VARS`), even from `env`. */ +/** + * Names Codex never lets a model-reachable process have (`NON_INHERITABLE_ENV_VARS`), even from + * `env`, compared without case on every platform, as Codex compares them. + */ const NON_INHERITABLE_ENV = [ "CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN", "NODE_REPL_AUTH_TOKEN", @@ -131,6 +135,10 @@ const NOT_STDIO = [ ]; const NOT_HTTP = ["args", "env", "env_vars", "cwd", "bearer_token"]; +/** Codex's skill scan limits for a plugin's skill folder (codex-rs/ext/skills loader). */ +const MAX_SKILL_SCAN_DEPTH = 6; +const MAX_SKILL_DIRS = 2000; + const SEMVER = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/; @@ -228,7 +236,7 @@ function stdioEnv( } for (const [name, value] of Object.entries(declared ?? {})) put(name, value); for (const key of Object.keys(result)) { - if (NON_INHERITABLE_ENV.some(name => sameEnvName(name, key))) delete result[key]; + if (NON_INHERITABLE_ENV.some(name => name.toUpperCase() === key.toUpperCase())) delete result[key]; } return result; } @@ -238,8 +246,9 @@ async function readLegacyManifest( root: string, id: string, warnings: string[], + manifests: readonly string[] = LEGACY_MANIFESTS, ): Promise<{ manifestPath: string; manifest: Record } | null> { - for (const relative of LEGACY_MANIFESTS) { + for (const relative of manifests) { const manifestPath = path.join(root, relative); const folder = await lstatOrNull(path.dirname(manifestPath)); if (folder === null) continue; @@ -338,27 +347,54 @@ function mergeLayers(lower: Record, higher: Record { +async function managedPolicy(home: string): Promise { if (IS_WINDOWS) { const key = "HKLM\\SOFTWARE\\Policies\\OpenAI\\Codex"; const query = Bun.spawnSync(["reg", "query", key, "/v", "RequirementsPath", "/reg:64"], { stdout: "ignore", stderr: "ignore", }); - if (query.exitCode === 0) return `${key}\\RequirementsPath`; + if (query.exitCode === 0) return `Codex is managed by an organization policy (${key}\\RequirementsPath)`; } - const files = [path.join(systemDir(), "requirements.toml")]; - if (!IS_WINDOWS) files.push("/etc/codex/managed_config.toml"); + const files = [ + path.join(systemDir(), "requirements.toml"), + IS_WINDOWS ? path.join(home, "managed_config.toml") : "/etc/codex/managed_config.toml", + ]; if (process.platform === "darwin") { const managed = "/Library/Managed Preferences"; files.push( @@ -366,7 +402,15 @@ async function managedPolicy(): Promise { path.join(managed, os.userInfo().username, "com.openai.codex.plist"), ); } - for (const file of files) if ((await lstatOrNull(file)) !== null) return file; + for (const file of files) { + try { + await fs.stat(file); + return `Codex is managed by an organization policy (${file})`; + } catch (error) { + const code = (error as NodeJS.ErrnoException).code; + if (code !== "ENOENT") return `Codex's organization policy at ${file} cannot be checked (${code})`; + } + } return undefined; } @@ -375,16 +419,21 @@ async function listCodexPlugins(ctx: LoadContext): Promise { const warnings: string[] = []; // The environment Scient Agent was launched with: no `.env` file, from this project or one the // agent worked in before, chooses Codex's home or what reaches a plugin's server. - const env = filterChildShellEnv(getLaunchEnv()); - const policy = await managedPolicy(); - if (policy !== undefined) { + const launch = getLaunchEnv(); + if (launch === undefined) { warnings.push( - `[codex-plugins] Codex is managed by an organization policy (${policy}), which Scient Agent cannot apply; no Codex plugins load`, + "[codex-plugins] This run cannot tell Scient Agent's launch environment from .env files (start it with --no-env-file); no Codex plugins load", ); - return { plugins: [], warnings, env }; + return { plugins: [], warnings, env: {} }; } + const env = filterProcessEnv(launch); // Codex's home: `CODEX_HOME`, else `~/.codex`. const home = env.CODEX_HOME?.trim() || path.join(ctx.home, ".codex"); + const policy = await managedPolicy(home); + if (policy !== undefined) { + warnings.push(`[codex-plugins] ${policy}, which Scient Agent cannot apply; no Codex plugins load`); + return { plugins: [], warnings, env }; + } const user = await readToml(path.join(home, "config.toml"), warnings); if (user === null) return { plugins: [], warnings, env }; const system = await readToml(path.join(systemDir(), "config.toml"), warnings); @@ -421,6 +470,89 @@ async function packagePath(plugin: CodexPlugin, value: unknown, key: string, war return resolved.status === "ok" ? resolved.realPath : null; } +/** A plugin's skill, read once its `SKILL.md` is known to stay inside the package; null when it is not a skill. */ +async function readPluginSkill( + plugin: CodexPlugin, + skillPath: string, + realPath: string, + warnings: string[], +): Promise { + const stat = await fs.stat(realPath).catch(() => null); + if (!stat?.isFile()) return null; + const content = await readFile(realPath); + if (!content) return null; + try { + const { frontmatter, body } = parseFrontmatter(content, { source: skillPath }); + if (frontmatter.enabled === false) return null; + const folder = path.basename(path.dirname(skillPath)); + const name = typeof frontmatter.name === "string" ? frontmatter.name.trim() || folder : folder; + // `/` is reserved for skill namespaces and skill:// paths. + if (/[\\/]/.test(name)) { + warnings.push(`[codex-plugins] Skill name "${name}" contains a path separator, skipping: ${skillPath}`); + return null; + } + return { + name, + path: skillPath, + content: body, + frontmatter: frontmatter as SkillFrontmatter, + level: "user", + // skill:// resolves every file of the skill inside the package too. + containRoot: plugin.root, + _source: createSourceMeta(PROVIDER_ID, skillPath, "user", undefined, plugin.label), + }; + } catch { + warnings.push(`[codex-plugins] Failed to read skill file: ${skillPath}`); + return null; + } +} + +/** + * Codex's scan of a plugin's skill folder: every `SKILL.md` down to its depth limit for a legacy + * plugin, each direct child's for an Agent Plugins package, hidden folders skipped. Each folder and + * `SKILL.md` must resolve inside the package before it is read. + */ +async function scanPluginSkills(plugin: CodexPlugin, dir: string, warnings: string[]): Promise { + const recursive = plugin.format === "legacy"; + // A SKILL.md sits one level below its folder, so folders go one level less deep than files. + const maxFolderDepth = recursive ? MAX_SKILL_SCAN_DEPTH - 1 : 1; + const skills: Skill[] = []; + const seen = new Set(); + const visit = async (folder: string, depth: number): Promise => { + if (seen.has(folder) || seen.size >= MAX_SKILL_DIRS) return; + seen.add(folder); + if (recursive || depth === 1) { + const skillPath = path.join(folder, "SKILL.md"); + const file = await resolveContainedPath(plugin.root, skillPath); + if (file.status === "outside") + warnings.push(`[codex-plugins] Ignoring skill outside ${plugin.id}: ${skillPath}`); + if (file.status === "ok") { + const skill = await readPluginSkill(plugin, skillPath, file.realPath, warnings); + if (skill !== null) skills.push(skill); + } + } + if (depth >= maxFolderDepth) return; + const children = (await readDirEntries(folder)).filter( + entry => !entry.name.startsWith(".") && (entry.isDirectory() || entry.isSymbolicLink()), + ); + await Promise.all( + children.map(async entry => { + const child = await resolveContainedPath(plugin.root, path.join(folder, entry.name)); + if (child.status === "outside") { + warnings.push( + `[codex-plugins] Ignoring skill folder outside ${plugin.id}: ${path.join(folder, entry.name)}`, + ); + } + if (child.status !== "ok") return; + if ((await fs.stat(child.realPath).catch(() => null))?.isDirectory()) + await visit(child.realPath, depth + 1); + }), + ); + }; + await visit(dir, 0); + return skills; +} + async function loadSkills(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; const { plugins, warnings } = await listCodexPlugins(ctx); @@ -434,22 +566,11 @@ async function loadSkills(ctx: LoadContext): Promise> { const dir = await packagePath(plugin, entry, "skills", warnings); if (dir !== null) dirs.add(dir); } - for (const dir of dirs) { - const result = await scanSkillsFromDir(ctx, { - dir, - providerId: PROVIDER_ID, - level: "user", - pluginName: plugin.label, - }); - if (result.warnings) warnings.push(...result.warnings); - // A skill folder or SKILL.md linked out of the package does not load, as in Codex. - for (const skill of result.items) { - if ((await resolveContainedPath(plugin.root, skill.path)).status === "ok") items.push(skill); - else warnings.push(`[codex-plugins] Ignoring skill outside ${plugin.id}: ${skill.path}`); - } - } + for (const dir of dirs) items.push(...(await scanPluginSkills(plugin, dir, warnings))); }), ); + // Reads finish in any order; the list does not. + items.sort((a, b) => compareSkillOrder(a.name, a.path, b.name, b.path)); return { items, warnings }; } @@ -474,6 +595,22 @@ function toolFilterKey(table: Record): string | undefined { return undefined; } +/** + * The Codex key that makes some of a server's tools ask before they run (`prompt`, `writes`, or a + * mode Scient Agent does not know), or undefined when Codex runs them all without asking. Scient + * Agent cannot carry Codex's approval modes over, so such a server would run its tools unasked. + */ +function approvalKey(table: Record): string | undefined { + const unasked = (mode: unknown) => mode == null || mode === "auto" || mode === "approve"; + if (!unasked(table.default_tools_approval_mode)) return "default_tools_approval_mode"; + if (table.tools == null) return undefined; + if (!isRecord(table.tools)) return "tools"; + for (const [tool, config] of Object.entries(table.tools)) { + if (!isRecord(config) || !unasked(config.approval_mode)) return `tools.${tool}.approval_mode`; + } + return undefined; +} + const isStringList = (value: unknown): value is string[] => Array.isArray(value) && value.every(item => typeof item === "string"); const isStringMap = (value: unknown): value is Record => @@ -629,8 +766,8 @@ async function readLegacyServerMap( /** * An Agent Plugins package's `mcp.json`, read under that standard and Codex's rules for it: the * file must be a regular file at the root, `sse` servers do not run, and a stdio server takes the - * `env_vars` its package's legacy declaration of the same server adds (dropping the `${NAME}` - * placeholders those variables replace). + * `env_vars` a valid `.codex-plugin` declaration of the same stdio server adds (dropping the + * `${NAME}` placeholders those variables replace). */ async function readStandardServers( ctx: LoadContext, @@ -655,10 +792,11 @@ async function readStandardServers( return null; } warnings.push(...result.warnings.map(warning => `[codex-plugins] ${plugin.id}: ${warning}`)); - const quiet: string[] = []; - const legacy = await readLegacyManifest(plugin.root, plugin.id, quiet); - const overlay = (await readLegacyServerMap(plugin, legacy?.manifest ?? {}, legacy?.manifestPath ?? file, quiet)) - ?.servers; + // Codex overlays only a valid `.codex-plugin` declaration of a valid stdio server. + const codexManifest = await readLegacyManifest(plugin.root, plugin.id, [], LEGACY_MANIFESTS.slice(0, 1)); + const overlay = + codexManifest && + (await readLegacyServerMap(plugin, codexManifest.manifest, codexManifest.manifestPath, []))?.servers; const servers: Record> = {}; for (const { name, ...server } of result.servers) { if (server.transport === "sse") { @@ -670,7 +808,13 @@ async function readStandardServers( continue; } const declared = overlay?.[name]; - const forwarded = isRecord(declared) && declared.command != null ? (envVarNames(declared.env_vars) ?? []) : []; + let forwarded: string[] = []; + if (isRecord(declared)) { + const mapped = await mapLegacyServer(plugin, declared, env).catch(() => null); + if (mapped !== null && "server" in mapped && mapped.server.transport === "stdio") { + forwarded = envVarNames(declared.env_vars) ?? []; + } + } // A `${NAME}` placeholder for a forwarded variable gives way to that variable's real value. const kept = Object.entries(server.env ?? {}).filter( ([key, value]) => @@ -711,9 +855,10 @@ async function loadMCPServers(ctx: LoadContext): Promise> } const declaredPolicy = policies[serverName]; const policy: Record = isRecord(declaredPolicy) ? declaredPolicy : {}; - const filter = toolFilterKey(server) ?? toolFilterKey(policy); - if (filter !== undefined) { - warnings.push(`${skip}: Codex restricts its tools (${filter}), which Scient Agent cannot enforce`); + const restricted = + toolFilterKey(server) ?? toolFilterKey(policy) ?? approvalKey(server) ?? approvalKey(policy); + if (restricted !== undefined) { + warnings.push(`${skip}: Codex restricts its tools (${restricted}), which Scient Agent cannot enforce`); continue; } let mapped: Mapped; @@ -730,8 +875,8 @@ async function loadMCPServers(ctx: LoadContext): Promise> name: `${plugin.label}:${serverName}`, ...mapped.server, // The plugin's `mcp_servers.` table turns the server off with `enabled = false`, and - // with the enterprise sign-in Codex no longer supports there. - ...((policy.enabled === false || policy.auth === "ema_auth") && { enabled: false }), + // with an `ema_auth` entry, the enterprise sign-in Codex no longer supports there. + ...((policy.enabled === false || "ema_auth" in policy) && { enabled: false }), _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), }); } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index e8b247b94d..a17ff56e36 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -9,7 +9,7 @@ import type { Skill } from "@oh-my-pi/pi-coding-agent/capability/skill"; import { cfgEnabledProviders } from "@oh-my-pi/pi-coding-agent/config/model-settings"; import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; import { StdioTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/stdio"; -import { getProjectDir, setProjectDir, TempDir } from "@oh-my-pi/pi-utils"; +import * as piUtils from "@oh-my-pi/pi-utils"; import { restoreEnvValue } from "../helpers/settings-test-state"; import "@oh-my-pi/pi-coding-agent/discovery"; @@ -32,7 +32,7 @@ async function writeManifest(root: string, manifest: Record): P } describe("installed Codex plugins", () => { - let temp: TempDir; + let temp: piUtils.TempDir; let settings: Settings; let release: () => void; let saved: Record; @@ -46,7 +46,7 @@ describe("installed Codex plugins", () => { for (const key of ENV_KEYS) restoreEnvValue(key, undefined); settings = Settings.isolated(); release = initializeWithSettings(settings); - temp = TempDir.createSync("@scient-codex-plugins-"); + temp = piUtils.TempDir.createSync("@scient-codex-plugins-"); const home = temp.join("home"); const codexHome = temp.join("codex-home"); cwd = temp.join("project"); @@ -56,6 +56,9 @@ describe("installed Codex plugins", () => { restoreEnvValue("CODEX_HOME", codexHome); restoreEnvValue("CODEX_PLUGIN_TEST_TOKEN", "forwarded-value"); restoreEnvValue("CODEX_PLUGIN_TEST_PAT", "pat-value"); + // The loader reads the environment Bun started with, which a test cannot change afterwards; + // the test's live environment stands in for it (the real one is tested in a fresh process). + vi.spyOn(piUtils, "getLaunchEnv").mockImplementation(() => ({ ...process.env }) as Record); await Bun.write( path.join(codexHome, "config.toml"), @@ -221,6 +224,21 @@ describe("installed Codex plugins", () => { }); }); + it("skips a server whose tools Codex asks about before running them", async () => { + await installLimits( + { + writes: { command: "node", args: ["writes.mjs"], tools: { save: { approval_mode: "writes" } } }, + prompted: { command: "node", args: ["prompted.mjs"] }, + approved: { command: "node", args: ["approved.mjs"], default_tools_approval_mode: "approve" }, + }, + ['[plugins."limits@market".mcp_servers.prompted]', 'default_tools_approval_mode = "prompt"'], + ); + const { names, warnings } = await limits(); + expect(names).toEqual(["limits:approved"]); + expect(warnings).toContainEqual(expect.stringContaining("tools.save.approval_mode")); + expect(warnings).toContainEqual(expect.stringContaining("default_tools_approval_mode")); + }); + it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { await installLimits( { denied: { command: "node", args: ["denied.mjs"] }, open: { command: "node", args: ["open.mjs"] } }, @@ -397,7 +415,7 @@ describe("installed Codex plugins", () => { ]); }); - it("keeps startup dotenv values out of the launch environment after the agent changes folders", async () => { + it("reads the launch environment only where the process can know it", async () => { const first = temp.join("first-project"); const second = temp.join("second-project"); await Bun.write(path.join(first, ".env"), "CODEX_HOME=/from/dotenv\nCODEX_PLUGIN_TEST_SECRET=dotenv-secret\n"); @@ -409,26 +427,37 @@ describe("installed Codex plugins", () => { `const { getLaunchEnv, setProjectDir } = await import(${JSON.stringify(utils)});`, `setProjectDir(${JSON.stringify(second)});`, "const env = getLaunchEnv();", - "console.log(JSON.stringify([Bun.env.CODEX_HOME, env.CODEX_HOME, env.CODEX_PLUGIN_TEST_SECRET, env.CODEX_PLUGIN_TEST_LOCAL, env.CODEX_PLUGIN_TEST_PAT]));", + "console.log(JSON.stringify([Bun.env.CODEX_HOME, env && [env.CODEX_HOME, env.CODEX_PLUGIN_TEST_SECRET, env.CODEX_PLUGIN_TEST_LOCAL, env.CODEX_PLUGIN_TEST_PAT]]));", ].join("\n"); - // With --no-env-file only the agent's own loader reads .env; without it Bun autoloads - // .env and .env.local too, as when the agent runs from source. - for (const flags of [["--no-env-file"], []]) { + // JSON writes a missing value as null. + const launch = [null, null, null, "exported"]; + // With --no-env-file only the agent's own loader reads .env, after the snapshot. Without it + // Bun autoloads .env and .env.local first, as when the agent runs from source; only Linux's + // procfs still holds the launch environment then. + for (const [flags, expected] of [ + [["--no-env-file"], launch], + [[], process.platform === "linux" ? launch : null], + ] as const) { const run = Bun.spawnSync([process.execPath, ...flags, "-e", script], { cwd: first, env: { PATH: process.env.PATH, HOME: temp.join("home"), CODEX_PLUGIN_TEST_PAT: "exported" }, }); - // JSON writes a missing value as null. expect(JSON.parse(run.stdout.toString().trim().split("\n").at(-1) ?? "null")).toEqual([ "/from/dotenv", - null, - null, - null, - "exported", + expected, ]); } }); + it("loads nothing while the launch environment is unknown", async () => { + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + vi.spyOn(piUtils, "getLaunchEnv").mockReturnValue(undefined); + const { byName, warnings } = await load(); + expect(byName.size).toBe(0); + expect(await skills()).toEqual([]); + expect(warnings).toContainEqual(expect.stringContaining("start it with --no-env-file")); + }); + it("follows Codex on disabled declarations, withheld variables and linked package folders", async () => { await install( "rules@market", @@ -439,11 +468,14 @@ describe("installed Codex plugins", () => { off: { command: "node", enabled: false }, retired: { command: "node", args: ["retired.mjs"] }, numeric: { command: 7, url: "https://mcp.example.test/mcp" }, - withheld: { command: "node", env: { NODE_REPL_AUTH_TOKEN: "x", KEEP: "y" } }, + withheld: { + command: "node", + env: { NODE_REPL_AUTH_TOKEN: "x", openai_spiffe_id: "lower", KEEP: "y" }, + }, }, }, }, - ["enabled = true", '[plugins."rules@market".mcp_servers.retired]', 'auth = "ema_auth"'], + ["enabled = true", '[plugins."rules@market".mcp_servers.retired]', "ema_auth = true"], ); const outside = temp.join("outside-manifest"); await Bun.write(path.join(outside, "plugin.json"), JSON.stringify({ name: "linkedfolder" })); @@ -459,22 +491,30 @@ describe("installed Codex plugins", () => { expect(warnings).toContainEqual(expect.stringContaining('"numeric" of rules@market: invalid command')); expect(byName.get("rules:withheld")?.env).toMatchObject({ KEEP: "y" }); expect(byName.get("rules:withheld")?.env?.NODE_REPL_AUTH_TOKEN).toBeUndefined(); + expect(byName.get("rules:withheld")?.env?.openai_spiffe_id).toBeUndefined(); expect(byName.has("linkedfolder:x")).toBe(false); expect(warnings).toContainEqual( expect.stringContaining("Ignoring linkedfolder@market: .codex-plugin is not a folder"), ); }); - it("drops a skill linked out of a real skills folder", async () => { + it("contains every skill in its package and finds nested ones", async () => { const outside = temp.join("outside-skill"); await writeSkill(outside, "escaped"); - const root = await install("mixed@market", { ".codex-plugin/plugin.json": { name: "mixed" } }); + const root = await fs.realpath(await install("mixed@market", { ".codex-plugin/plugin.json": { name: "mixed" } })); await writeSkill(root, "inside"); + await writeSkill(path.join(root, "skills", "group"), "nested"); await fs.symlink(path.join(outside, "skills", "escaped"), path.join(root, "skills", "escaped"), "dir"); + await fs.mkdir(path.join(root, "skills", "filelink")); + await fs.symlink( + path.join(outside, "skills", "escaped", "SKILL.md"), + path.join(root, "skills", "filelink", "SKILL.md"), + ); clearFsCache(); - const names = (await skills()).map(skill => skill.name); - expect(names).toContain("inside"); - expect(names).not.toContain("escaped"); + const found = (await skills()).filter(skill => skill.path.startsWith(root)); + expect(found.map(skill => skill.name).sort()).toEqual(["inside", "nested"]); + // skill:// resolves the skill's other files inside the package too. + expect(found.every(skill => skill.containRoot === root)).toBe(true); }); it("applies Codex's rules to an Agent Plugins package's mcp.json", async () => { @@ -510,6 +550,36 @@ describe("installed Codex plugins", () => { expect(warnings).toContainEqual(expect.stringContaining("linkedmcp@market: mcp.json is not a regular file")); }); + it("forwards a variable only from a valid .codex-plugin declaration of the same stdio server", async () => { + const placeholder = "$" + "{CODEX_PLUGIN_TEST_PAT}"; + const standard = (name: string) => ({ + "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name }, + "mcp.json": { + $schema: "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json", + mcpServers: { + local: { type: "stdio", command: "node", args: [name], env: { CODEX_PLUGIN_TEST_PAT: placeholder } }, + }, + }, + }); + await install("viaclaude@market", { + ...standard("viaclaude"), + ".claude-plugin/plugin.json": { name: "viaclaude" }, + ".mcp.json": { mcpServers: { local: { command: "node", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } } }, + }); + await install("invalidoverlay@market", { + ...standard("invalidoverlay"), + ".codex-plugin/plugin.json": { name: "invalidoverlay" }, + ".mcp.json": { + mcpServers: { + local: { command: "node", url: "https://mcp.example.test/mcp", env_vars: ["CODEX_PLUGIN_TEST_PAT"] }, + }, + }, + }); + const { byName } = await load(); + expect(byName.get("viaclaude:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("invalidoverlay:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + }); + it("reads Codex's manifest precedence: a root Agent Plugins manifest, then the legacy folders", async () => { await install("portable@market", { "plugin.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", name: "portable" }, @@ -529,25 +599,6 @@ describe("installed Codex plugins", () => { expect(byName.has("clauded:tool")).toBe(true); }); - it("ignores a CODEX_HOME that only a project's .env sets", async () => { - const previous = getProjectDir(); - const decoy = temp.join("decoy-home"); - await Bun.write(path.join(cwd, ".env"), `CODEX_HOME=${decoy}\n`); - await install("decoy@market", { ".codex-plugin/plugin.json": { name: "decoy" } }); - await fs.cp(temp.join("codex-home"), decoy, { recursive: true }); - // As if the agent had loaded the project's .env into its environment. - restoreEnvValue("CODEX_HOME", decoy); - setProjectDir(cwd); - try { - clearFsCache(); - const decoyServers = (await load()).warnings.filter(w => w.includes(decoy)); - expect(decoyServers).toEqual([]); - expect((await skills()).map(skill => skill.path).some(p => p.includes("decoy-home"))).toBe(false); - } finally { - setProjectDir(previous); - } - }); - it("starts a stdio server with only the variables Codex would pass", async () => { restoreEnvValue("CODEX_PLUGIN_TEST_SECRET", "must-not-leak"); const out = temp.join("env.json"); diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts index 6e9654538f..80d37385a4 100644 --- a/packages/utils/src/env.ts +++ b/packages/utils/src/env.ts @@ -384,27 +384,27 @@ export function withOmpOtelEnv(fn: () => T): T { } // scient(SA-CODEX-PLUGINS): begin - Codex plugins read only the environment the agent was launched with. -const launchProjectDir = getProjectDir(); -let launchDotenvValues: ReadonlySet | undefined; +/** + * The exec environment, when this process can know it: procfs or `--no-env-file` (see + * {@link readLaunchEnv}), or, in the compiled binary, which Bun builds with dotenv autoloading off + * (coding-agent `scripts/compile-binary.ts`), `Bun.env` here, before the agent's own `.env` + * files load below. The binary is told by its bundled module URL, which no variable can fake. + */ +const launchEnvSnapshot: ReadonlyMap | undefined = + launchEnvValues ?? + (/\$bunfs|~BUN|%7EBUN/.test(import.meta.url) + ? new Map(Object.entries(Bun.env).filter((entry): entry is [string, string] => entry[1] !== undefined)) + : undefined); /** - * The environment the agent was launched with, whichever folder it works in now: the exec - * environment when it is known (procfs, `--no-env-file`), else the environment without any value - * the launch folder's dotenv files (all those Bun autoloads) or the agent's own `.env` files - * (agent, config root, home) define. Codex reads its home, its plugins' credentials and their - * servers' variables from its launch environment alone; so does the agent when it runs them. + * The environment the agent was launched with, whichever folder it works in now, or undefined + * when this run cannot tell it apart from what dotenv files added (Bun autoloaded them, as when + * the agent runs from source without `--no-env-file`, and there is no procfs). Codex reads its + * home, its plugins' credentials and their servers' variables from its launch environment alone; + * so does the agent when it runs Codex's plugins. */ -export function getLaunchEnv(): Record { - if (launchEnvValues) return Object.fromEntries(launchEnvValues); - launchDotenvValues ??= new Set(getDotenvEnvValues(launchProjectDir, Bun.env)); - const result: Record = {}; - for (const key in Bun.env) { - const value = Bun.env[key]; - if (value === undefined || launchDotenvValues.has(value)) continue; - if ([projectEnv, agentEnv, piEnv, homeEnv].some(file => file[key] === value)) continue; - result[key] = value; - } - return result; +export function getLaunchEnv(): Record | undefined { + return launchEnvSnapshot && Object.fromEntries(launchEnvSnapshot); } // scient(SA-CODEX-PLUGINS): end for (const file of [projectEnv, agentEnv, piEnv, homeEnv]) { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index eaf8da666b..2150b5b5d4 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in ProgramData), a table without enabled taking [plugins._default]. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment when known, else the environment without any value the launch folder's Bun-autoloaded dotenv files or the agent's own .env files define, whichever folder the agent works in now), never a .env file. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars its package's legacy declaration of it adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there or a linked folder holding it rejecting the plugin. A legacy server must declare exactly one transport with correctly typed fields and only that transport's Codex fields; package files, each skill, cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name. enabled = false in a declaration or in the plugin's mcp_servers. table, or auth = \"ema_auth\" there, disables a server. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex (requirements.toml in the system folder or at the Windows Group Policy RequirementsPath, /etc/codex/managed_config.toml, macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load), never a .env file; when it is unknown (from source with Bun's dotenv autoload, outside Linux) nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there or a linked folder holding it rejecting the plugin. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, and carry containRoot so skill:// stays in the package. A legacy server must declare exactly one transport with correctly typed fields and only that transport's Codex fields; package files, cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name. enabled = false in a declaration or in the plugin's mcp_servers. table, or an ema_auth key there, disables a server. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools) or asks about (default_tools_approval_mode or tools..approval_mode other than auto or approve), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the Windows Group Policy RequirementsPath, managed_config.toml in /etc/codex or, on Windows, in CODEX_HOME, macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts" From a50758743f173fa6d7a213c7aac050deddada7b5 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 11:13:03 +0200 Subject: [PATCH 06/30] fix(codex-plugins): build-marked launch env, ema_auth left out, Codex field types and paths, fail-closed Windows probe Fourth Codex review of 65ffa2e566: - getLaunchEnv() trusts the compiled binary only through __SCIENT_AGENT_NO_DOTENV__, which only compile-binary.ts defines (beside autoloadDotenv false), and no run given an explicit --env-file. - A server with an ema_auth policy entry is left out with a warning, so enabledServers cannot start it. - Legacy manifests and servers must have the field types Codex accepts (a .codex-plugin overlay too), manifest paths must be ./-relative without .., and a server bound to another Codex environment is skipped. - Windows reads the ProgramData known folder and the 64-bit RequirementsPath in one PowerShell query; a failed query counts as managed. A leftover CODEX_HOME/managed_config.toml no longer does, as in Codex. - Not changed: auto approval defers to the session's approval policy in Codex (mcp_permission_prompt_is_auto_approved), as it does to Scient Agent's approval mode here. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- .../coding-agent/scripts/compile-binary.ts | 3 + .../coding-agent/src/scient/codex-plugins.ts | 242 +++++++++++++----- .../test/scient/codex-plugins.test.ts | 38 ++- packages/utils/src/env.ts | 24 +- scripts/scient/divergences.json | 3 +- 7 files changed, 243 insertions(+), 71 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index 55411cba48..92e1f70c70 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its skill scan, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file`, outside Linux) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`) or asks about (`prompt` or `writes` approval) is skipped with a warning, because Scient Agent cannot enforce either; so is a server with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths, its skill scan, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off for good (`ema_auth`) and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 6910b1ffae..ad56116d03 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]` and its manifest formats included), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`) or asks about before running (`prompt` or `writes` approval) and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, or when Scient Agent runs from source without `--no-env-file` outside Linux. +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, its manifest formats and field types included), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`) or always asks about (`prompt` or `writes` approval), servers Codex turns off for good (`ema_auth`), and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/scripts/compile-binary.ts b/packages/coding-agent/scripts/compile-binary.ts index 31ad55d397..a49b58ed29 100644 --- a/packages/coding-agent/scripts/compile-binary.ts +++ b/packages/coding-agent/scripts/compile-binary.ts @@ -65,6 +65,9 @@ export async function compileCodingAgent(options: CodingAgentCompileOptions): Pr // scient(SA-EXPORT): begin - exports from a binary carry Scient's viewer. "process.env.SCIENT_EXPORT_VIEWER": JSON.stringify(await exportViewerSource(options.repoRoot)), // scient(SA-EXPORT): end + // scient(SA-CODEX-PLUGINS): begin - tells utils env.ts that Bun loaded no dotenv file (autoloadDotenv below). + __SCIENT_AGENT_NO_DOTENV__: "true", + // scient(SA-CODEX-PLUGINS): end }, // Precompiled bytecode skips parsing the ~20 MB bundle at boot: // `omp --version` 256 ms -> 30 ms on M4 Max (+52 MB binary). diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 992ba290cf..cbf5313cdd 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -266,8 +266,9 @@ async function readLegacyManifest( try { manifest = JSON.parse((await readFile(manifestPath)) ?? ""); } catch {} - if (!isRecord(manifest)) { - warnings.push(`[codex-plugins] Invalid plugin.json for ${id}: ${manifestPath}`); + const mistyped = isRecord(manifest) ? manifestTypeError(manifest) : "the file"; + if (mistyped !== undefined || !isRecord(manifest)) { + warnings.push(`[codex-plugins] Invalid plugin.json for ${id} (${mistyped}): ${manifestPath}`); return null; } return { manifestPath, manifest }; @@ -347,33 +348,49 @@ function mergeLayers(lower: Record, higher: Record { +async function managedPolicy(): Promise { + const files = [path.join(systemDir(), "requirements.toml")]; if (IS_WINDOWS) { - const key = "HKLM\\SOFTWARE\\Policies\\OpenAI\\Codex"; - const query = Bun.spawnSync(["reg", "query", key, "/v", "RequirementsPath", "/reg:64"], { - stdout: "ignore", - stderr: "ignore", - }); - if (query.exitCode === 0) return `Codex is managed by an organization policy (${key}\\RequirementsPath)`; - } - const files = [ - path.join(systemDir(), "requirements.toml"), - IS_WINDOWS ? path.join(home, "managed_config.toml") : "/etc/codex/managed_config.toml", - ]; + const probe = probeWindowsPolicy(); + if (probe === null) return "Windows cannot say where Codex's organization policy is"; + if (probe.requirementsPath !== null) { + return `Codex is managed by an organization policy (Group Policy RequirementsPath ${probe.requirementsPath})`; + } + } else { + // Codex ignores a leftover managed_config.toml on Windows. + files.push("/etc/codex/managed_config.toml"); + } if (process.platform === "darwin") { const managed = "/Library/Managed Preferences"; files.push( @@ -429,7 +445,7 @@ async function listCodexPlugins(ctx: LoadContext): Promise { const env = filterProcessEnv(launch); // Codex's home: `CODEX_HOME`, else `~/.codex`. const home = env.CODEX_HOME?.trim() || path.join(ctx.home, ".codex"); - const policy = await managedPolicy(home); + const policy = await managedPolicy(); if (policy !== undefined) { warnings.push(`[codex-plugins] ${policy}, which Scient Agent cannot apply; no Codex plugins load`); return { plugins: [], warnings, env }; @@ -459,10 +475,18 @@ async function listCodexPlugins(ctx: LoadContext): Promise { return { plugins, warnings, env }; } -/** A manifest path inside the package, symlinks resolved; null (with a warning when it points outside). */ +/** + * A manifest path inside the package, symlinks resolved; null (with a warning) when Codex would + * ignore it: not `./`-relative, holding `..`, or resolving outside the package. + */ async function packagePath(plugin: CodexPlugin, value: unknown, key: string, warnings: string[]) { - if (typeof value !== "string" || !value.trim()) return null; - const resolved = await resolveContainedPath(plugin.root, path.resolve(plugin.root, value.trim())); + if (typeof value !== "string" || !value) return null; + const relative = value.startsWith("./") ? value.slice(2) : ""; + if (!relative || relative.split(/[\\/]/).includes("..")) { + warnings.push(`[codex-plugins] Ignoring ${key} path of ${plugin.id}: ${value} (Codex takes ./paths without ..)`); + return null; + } + const resolved = await resolveContainedPath(plugin.root, path.resolve(plugin.root, relative)); if (resolved.status === "outside") { warnings.push(`[codex-plugins] Ignoring ${key} path outside ${plugin.id}: ${value}`); return null; @@ -560,7 +584,7 @@ async function loadSkills(ctx: LoadContext): Promise> { await Promise.all( plugins.map(async plugin => { const declared = plugin.format === "standard" ? undefined : plugin.manifest.skills; - const entries = declared == null ? ["skills"] : Array.isArray(declared) ? declared : [declared]; + const entries = declared == null ? ["./skills"] : Array.isArray(declared) ? declared : [declared]; const dirs = new Set(); for (const entry of entries) { const dir = await packagePath(plugin, entry, "skills", warnings); @@ -615,15 +639,115 @@ const isStringList = (value: unknown): value is string[] => Array.isArray(value) && value.every(item => typeof item === "string"); const isStringMap = (value: unknown): value is Record => isRecord(value) && Object.values(value).every(item => typeof item === "string"); +const isString = (value: unknown) => typeof value === "string"; +const isBoolean = (value: unknown) => typeof value === "boolean"; +const isPositiveInteger = (value: unknown) => Number.isInteger(value) && (value as number) > 0; +const isApprovalMode = (value: unknown) => ["auto", "prompt", "writes", "approve"].includes(value as string); + +/** Codex's string fields of a legacy manifest's `interface` (camelCase, with its URL aliases). */ +const INTERFACE_STRINGS = [ + "displayName", + "shortDescription", + "longDescription", + "developerName", + "category", + "websiteUrl", + "websiteURL", + "privacyPolicyUrl", + "privacyPolicyURL", + "termsOfServiceUrl", + "termsOfServiceURL", + "brandColor", + "composerIcon", + "logo", + "logoDark", +]; + +/** The first legacy manifest field whose type Codex rejects, which makes it skip the plugin, or undefined. */ +function manifestTypeError(manifest: Record): string | undefined { + // `name` and `keywords` cannot be null; the other fields may be. + if ("name" in manifest && !isString(manifest.name)) return "name"; + if ("keywords" in manifest && !isStringList(manifest.keywords)) return "keywords"; + for (const key of ["version", "description", "apps"]) { + if (manifest[key] != null && !isString(manifest[key])) return key; + } + const ui = manifest.interface; + if (ui == null) return undefined; + if (!isRecord(ui)) return "interface"; + for (const key of ["capabilities", "screenshots"]) { + if (key in ui && !isStringList(ui[key])) return `interface.${key}`; + } + for (const key of INTERFACE_STRINGS) if (ui[key] != null && !isString(ui[key])) return `interface.${key}`; + return undefined; +} + +/** + * The types Codex accepts for a legacy server's fields (null counts as absent); it rejects a server + * with any other, so its siblings load without it. + */ +const SERVER_FIELD_TYPES: Record boolean> = { + command: isString, + args: isStringList, + env: isStringMap, + cwd: isString, + http_headers: isStringMap, + env_http_headers: isStringMap, + url: isString, + bearer_token: isString, + bearer_token_env_var: isString, + http_headers_helper: isString, + environment_id: isString, + auth: value => value === "oauth" || value === "chatgpt" || value === "ema_auth", + startup_timeout_sec: value => typeof value === "number" && Number.isFinite(value), + startup_timeout_ms: value => Number.isInteger(value) && (value as number) >= 0, + tool_timeout_sec: value => typeof value === "number" && Number.isFinite(value) && value >= 0, + enabled: isBoolean, + required: isBoolean, + supports_parallel_tool_calls: isBoolean, + startup_readiness: value => value === "connection" || value === "catalog", + tool_input_schema_max_bytes: isPositiveInteger, + omit_tools_from: isStringList, + default_tools_approval_mode: isApprovalMode, + enabled_tools: isStringList, + disabled_tools: isStringList, + scopes: isStringList, + oauth: value => + isRecord(value) && + ["client_id", "client_secret", "callback_url", "authorization_server_issuer", "token_endpoint_auth_method"].every( + key => value[key] == null || isString(value[key]), + ) && + (value.callback_port == null || + (Number.isInteger(value.callback_port) && + (value.callback_port as number) >= 0 && + (value.callback_port as number) <= 65535)), + oauth_resource: isString, + name: isString, + tools: value => + isRecord(value) && + Object.values(value).every( + tool => + isRecord(tool) && + (tool.approval_mode == null || isApprovalMode(tool.approval_mode)) && + (tool.output_token_limit == null || isPositiveInteger(tool.output_token_limit)), + ), +}; -/** The names of Codex's `env_vars` entries (a name, or `{ name, source = "local" }`); null when one is malformed. */ +/** + * The names of Codex's `env_vars` entries (a name, or `{ name, source = "local" }` with no other + * keys); null when one is malformed. + */ function envVarNames(value: unknown): string[] | null { if (value == null) return []; if (!Array.isArray(value)) return null; const names: string[] = []; for (const entry of value) { if (typeof entry === "string") names.push(entry); - else if (isRecord(entry) && typeof entry.name === "string" && (entry.source ?? "local") === "local") { + else if ( + isRecord(entry) && + typeof entry.name === "string" && + (entry.source ?? "local") === "local" && + Object.keys(entry).every(key => key === "name" || key === "source") + ) { names.push(entry.name); } else return null; } @@ -641,8 +765,14 @@ async function mapLegacyServer( server: Record, env: Record, ): Promise { - if (server.command != null && typeof server.command !== "string") return { error: "invalid command" }; - if (server.url != null && typeof server.url !== "string") return { error: "invalid url" }; + const mistyped = Object.keys(SERVER_FIELD_TYPES).find( + key => server[key] != null && !SERVER_FIELD_TYPES[key](server[key]), + ); + if (mistyped !== undefined) return { error: `invalid ${mistyped}` }; + // Another environment runs the server for Codex; here it would run on this machine. + if (server.environment_id != null && server.environment_id !== "local") { + return { error: `it runs in Codex environment ${server.environment_id}` }; + } const isStdio = server.command != null; if (!isStdio && server.url == null) return { error: "it declares neither command nor url" }; const wrong = (isStdio ? NOT_STDIO : NOT_HTTP).find(key => server[key] != null); @@ -657,11 +787,8 @@ async function mapLegacyServer( if (isStdio) { const command = (server.command as string).trim(); if (!command) return { error: "command is empty" }; - if (server.args != null && !isStringList(server.args)) return { error: "invalid args" }; - if (server.env != null && !isStringMap(server.env)) return { error: "invalid env" }; const forwarded = envVarNames(server.env_vars); if (forwarded === null) return { error: "invalid env_vars" }; - if (server.cwd != null && typeof server.cwd !== "string") return { error: "invalid cwd" }; // Codex runs a server from the package unless it names a folder inside it. const cwd = typeof server.cwd === "string" ? path.resolve(plugin.root, server.cwd) : plugin.root; if (!(await isContainedResolved(plugin.root, cwd))) return { error: `cwd ${server.cwd} leaves the package` }; @@ -687,14 +814,6 @@ async function mapLegacyServer( if (server.http_headers_helper != null) return { error: "it computes its headers with a helper command" }; if (server.auth != null && server.auth !== "oauth") return { error: `auth ${String(server.auth)} is not supported` }; - if (server.http_headers != null && !isStringMap(server.http_headers)) return { error: "invalid http_headers" }; - if (server.env_http_headers != null && !isStringMap(server.env_http_headers)) { - return { error: "invalid env_http_headers" }; - } - if (server.bearer_token_env_var != null && typeof server.bearer_token_env_var !== "string") { - return { error: "invalid bearer_token_env_var" }; - } - if (server.oauth != null && !isRecord(server.oauth)) return { error: "invalid oauth" }; const url = server.url as string; if (!URL.canParse(url) || !/^https?:$/.test(new URL(url).protocol)) return { error: `invalid url ${url}` }; // Static headers are data; the others name variables of the launch environment, as in Codex. @@ -744,7 +863,7 @@ async function readLegacyServerMap( let parsed: unknown = declared; let sourcePath = manifestPath; if (!isRecord(declared)) { - const file = await packagePath(plugin, declared ?? ".mcp.json", "mcpServers", warnings); + const file = await packagePath(plugin, declared ?? "./.mcp.json", "mcpServers", warnings); if (file === null) { if (declared != null) warnings.push(`[codex-plugins] Missing mcpServers file declared by ${plugin.id}`); return null; @@ -861,6 +980,12 @@ async function loadMCPServers(ctx: LoadContext): Promise> warnings.push(`${skip}: Codex restricts its tools (${restricted}), which Scient Agent cannot enforce`); continue; } + // An `ema_auth` entry stands for the enterprise sign-in Codex no longer supports there; Codex + // turns the server off for good, so no setting here may turn it back on. + if ("ema_auth" in policy) { + warnings.push(`${skip}: Codex turns it off (ema_auth)`); + continue; + } let mapped: Mapped; try { mapped = standard !== null ? { server } : await mapLegacyServer(plugin, server, env); @@ -874,9 +999,8 @@ async function loadMCPServers(ctx: LoadContext): Promise> items.push({ name: `${plugin.label}:${serverName}`, ...mapped.server, - // The plugin's `mcp_servers.` table turns the server off with `enabled = false`, and - // with an `ema_auth` entry, the enterprise sign-in Codex no longer supports there. - ...((policy.enabled === false || "ema_auth" in policy) && { enabled: false }), + // The plugin's `mcp_servers.` table turns the server off with `enabled = false`. + ...(policy.enabled === false && { enabled: false }), _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), }); } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index a17ff56e36..650e2d2823 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -437,6 +437,8 @@ describe("installed Codex plugins", () => { for (const [flags, expected] of [ [["--no-env-file"], launch], [[], process.platform === "linux" ? launch : null], + // Bun loads a file it is told to even with --no-env-file. + [["--no-env-file", "--env-file=.env"], null], ] as const) { const run = Bun.spawnSync([process.execPath, ...flags, "-e", script], { cwd: first, @@ -486,7 +488,11 @@ describe("installed Codex plugins", () => { clearFsCache(); const { byName, warnings } = await load(); expect(byName.get("rules:off")?.enabled).toBe(false); - expect(byName.get("rules:retired")?.enabled).toBe(false); + // Codex turns an ema_auth server off for good, so it is not listed for a setting to turn on. + expect(byName.has("rules:retired")).toBe(false); + expect(warnings).toContainEqual( + expect.stringContaining('"retired" of rules@market: Codex turns it off (ema_auth)'), + ); expect(byName.has("rules:numeric")).toBe(false); expect(warnings).toContainEqual(expect.stringContaining('"numeric" of rules@market: invalid command')); expect(byName.get("rules:withheld")?.env).toMatchObject({ KEEP: "y" }); @@ -575,9 +581,39 @@ describe("installed Codex plugins", () => { }, }, }); + await install("textflag@market", { + ...standard("textflag"), + ".codex-plugin/plugin.json": { name: "textflag" }, + ".mcp.json": { + mcpServers: { local: { command: "node", enabled: "false", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } }, + }, + }); const { byName } = await load(); expect(byName.get("viaclaude:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); expect(byName.get("invalidoverlay:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("textflag:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + }); + + it("skips a manifest Codex cannot read and paths it would not take", async () => { + const root = await install("typed@market", { + ".codex-plugin/plugin.json": { name: 5 }, + ".mcp.json": { mcpServers: { x: { command: "node" } } }, + }); + await writeSkill(root, "typed-skill"); + const loose = await install("loose@market", { + ".codex-plugin/plugin.json": { name: "loose", skills: "skills/", mcpServers: "./sub/../.mcp.json" }, + ".mcp.json": { mcpServers: { y: { command: "node" } } }, + }); + await writeSkill(loose, "loose-skill"); + clearFsCache(); + const { byName, warnings } = await load(); + expect(byName.has("typed:x")).toBe(false); + expect(byName.has("loose:y")).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining("Invalid plugin.json for typed@market (name)")); + expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers path of loose@market")); + const names = (await skills()).map(skill => skill.name); + expect(names).not.toContain("typed-skill"); + expect(names).not.toContain("loose-skill"); }); it("reads Codex's manifest precedence: a root Agent Plugins manifest, then the legacy folders", async () => { diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts index 80d37385a4..4a8651252e 100644 --- a/packages/utils/src/env.ts +++ b/packages/utils/src/env.ts @@ -384,17 +384,25 @@ export function withOmpOtelEnv(fn: () => T): T { } // scient(SA-CODEX-PLUGINS): begin - Codex plugins read only the environment the agent was launched with. +/** Defined `true` only by the compiled binary's build (coding-agent `scripts/compile-binary.ts`), which turns Bun's dotenv loading off. */ +declare const __SCIENT_AGENT_NO_DOTENV__: boolean | undefined; + +/** Bun loads a dotenv file it is told to (`--env-file`, directly or through `BUN_OPTIONS`) even with `--no-env-file`. */ +const explicitEnvFile = [...process.execArgv, ...(Bun.env.BUN_OPTIONS?.split(/\s+/) ?? [])].some( + arg => arg === "--env-file" || arg.startsWith("--env-file="), +); + /** * The exec environment, when this process can know it: procfs or `--no-env-file` (see - * {@link readLaunchEnv}), or, in the compiled binary, which Bun builds with dotenv autoloading off - * (coding-agent `scripts/compile-binary.ts`), `Bun.env` here, before the agent's own `.env` - * files load below. The binary is told by its bundled module URL, which no variable can fake. + * {@link readLaunchEnv}) without an explicit env file, or, in the compiled binary, `Bun.env` + * here, before the agent's own `.env` files load below. */ -const launchEnvSnapshot: ReadonlyMap | undefined = - launchEnvValues ?? - (/\$bunfs|~BUN|%7EBUN/.test(import.meta.url) - ? new Map(Object.entries(Bun.env).filter((entry): entry is [string, string] => entry[1] !== undefined)) - : undefined); +const launchEnvSnapshot: ReadonlyMap | undefined = explicitEnvFile + ? undefined + : (launchEnvValues ?? + (typeof __SCIENT_AGENT_NO_DOTENV__ !== "undefined" && __SCIENT_AGENT_NO_DOTENV__ === true + ? new Map(Object.entries(Bun.env).filter((entry): entry is [string, string] => entry[1] !== undefined)) + : undefined)); /** * The environment the agent was launched with, whichever folder it works in now, or undefined diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 2150b5b5d4..ab00e56da1 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,12 +520,13 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load), never a .env file; when it is unknown (from source with Bun's dotenv autoload, outside Linux) nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there or a linked folder holding it rejecting the plugin. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, and carry containRoot so skill:// stays in the package. A legacy server must declare exactly one transport with correctly typed fields and only that transport's Codex fields; package files, cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name. enabled = false in a declaration or in the plugin's mcp_servers. table, or an ema_auth key there, disables a server. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools) or asks about (default_tools_approval_mode or tools..approval_mode other than auto or approve), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the Windows Group Policy RequirementsPath, managed_config.toml in /etc/codex or, on Windows, in CODEX_HOME, macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths count only as Codex takes them (./-relative, without ..) and must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, and carry containRoot so skill:// stays in the package. A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts, and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name. enabled = false in a declaration or in the plugin's mcp_servers. table disables a server; an ema_auth key there leaves it out with a warning, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query whose failure counts as managed; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts" ], "upstream": [ + "packages/coding-agent/scripts/compile-binary.ts", "packages/coding-agent/src/capability/index.ts", "packages/coding-agent/src/capability/mcp.ts", "packages/coding-agent/src/discovery/index.ts", From 24ee20a228ad87506a48715d091115f919b5f5aa Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 11:24:11 +0200 Subject: [PATCH 07/30] fix(codex-plugins): skill rules, omit_tools_from, durations, path fallbacks, tool-name suffixes, strict Windows probe Fifth Codex review of a50758743f: - The Windows probe stops on any error and counts only an explicit success as no policy; a spawn failure counts as managed. - A server whose tools Codex hides (non-empty omit_tools_from, values checked) is skipped. - The user's [[skills.config]] rules apply to plugin skills in Codex's order (canonical path or name). - Durations must be ones Rust's Duration::try_from_secs_f64 takes, so a negative startup_timeout_sec rejects the declaration and its env_vars. - Manifest paths Codex drops fall back to skills/ and .mcp.json, as in Codex. - Servers whose names would give their tools the same names take a stable -<6 hex> suffix. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- .../coding-agent/src/scient/codex-plugins.ts | 186 ++++++++++++++---- .../test/scient/codex-plugins.test.ts | 57 +++++- scripts/scient/divergences.json | 2 +- 5 files changed, 201 insertions(+), 48 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index 92e1f70c70..d3931a44e6 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths, its skill scan, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off for good (`ema_auth`) and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off for good (`ema_auth`) and one with malformed or unsupported fields, while its siblings load. Servers whose names would give their tools the same names take a short stable suffix. Nothing loads while an organization policy manages Codex, or may and cannot be checked. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index ad56116d03..b549f7febf 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, its manifest formats and field types included), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`) or always asks about (`prompt` or `writes` approval), servers Codex turns off for good (`ema_auth`), and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`) or always asks about (`prompt` or `writes` approval), servers Codex turns off for good (`ema_auth`), and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index cbf5313cdd..8a830d324c 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -162,11 +162,19 @@ interface CodexPlugin { config: Record; } +/** A user `[[skills.config]]` entry Codex applies to loaded skills, its path canonical. */ +interface SkillRule { + selector: { path: string } | { name: string }; + enabled: boolean; +} + interface PluginList { plugins: CodexPlugin[]; warnings: string[]; /** Scient Agent's launch environment, without any `.env` file's values. */ env: Record; + /** The user config's skill rules, in Codex's order. */ + skillRules: SkillRule[]; } /** Codex's rule for a cache path segment (plugin, marketplace or version name). */ @@ -364,21 +372,25 @@ let windowsProbe: WindowsPolicyProbe | null | undefined; */ function probeWindowsPolicy(): WindowsPolicyProbe | null { if (windowsProbe !== undefined) return windowsProbe; + // Any error stops the script with no output, so only a lookup that succeeded reports no policy. const script = [ - "$key = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Registry64').OpenSubKey('SOFTWARE\\Policies\\OpenAI\\Codex')", + "$ErrorActionPreference = 'Stop'", + "try { $key = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Registry64').OpenSubKey('SOFTWARE\\Policies\\OpenAI\\Codex')", "$path = if ($key) { $key.GetValue('RequirementsPath') } else { $null }", - "[Console]::Out.Write((@{ programData = [Environment]::GetFolderPath('CommonApplicationData'); requirementsPath = $path } | ConvertTo-Json -Compress))", + "$data = [Environment]::GetFolderPath('CommonApplicationData')", + "[Console]::Out.Write((@{ ok = $true; programData = $data; requirementsPath = $path } | ConvertTo-Json -Compress)) } catch { exit 1 }", ].join("; "); - const query = Bun.spawnSync(["powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script], { - stdout: "pipe", - stderr: "ignore", - }); let probe: unknown = null; try { + const query = Bun.spawnSync(["powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script], { + stdout: "pipe", + stderr: "ignore", + }); probe = query.exitCode === 0 ? JSON.parse(query.stdout.toString()) : null; } catch {} windowsProbe = isRecord(probe) && + probe.ok === true && typeof probe.programData === "string" && path.win32.isAbsolute(probe.programData) && (probe.requirementsPath == null || typeof probe.requirementsPath === "string") @@ -430,6 +442,48 @@ async function managedPolicy(): Promise { return undefined; } +/** + * The user config's `[[skills.config]]` rules as Codex reads them: an entry selects by an absolute + * `path` (canonicalized) or a `name`, never both; a later entry for the same selector replaces an + * earlier one. A `skills` table Codex cannot read yields no rules, as in Codex. + */ +async function readSkillRules(user: Record, warnings: string[]): Promise { + const skills = user.skills; + if (skills == null) return []; + const entries = isRecord(skills) ? (skills.config ?? []) : null; + const valid = + isRecord(skills) && + Array.isArray(entries) && + entries.every( + entry => + isRecord(entry) && + isBoolean(entry.enabled) && + (entry.path == null || (isString(entry.path) && path.isAbsolute(entry.path))) && + (entry.name == null || isString(entry.name)), + ) && + (skills.bundled == null || + (isRecord(skills.bundled) && (skills.bundled.enabled == null || isBoolean(skills.bundled.enabled)))) && + (skills.include_instructions == null || isBoolean(skills.include_instructions)) && + (skills.max_context_tokens == null || isPositiveInteger(skills.max_context_tokens)); + if (!valid) { + warnings.push("[codex-plugins] Ignoring Codex's [skills] settings, which Codex cannot read either"); + return []; + } + const rules: SkillRule[] = []; + for (const entry of entries as Record[]) { + const name = typeof entry.name === "string" ? entry.name.trim() : undefined; + if ((entry.path == null) === (entry.name == null) || name === "") continue; + const selector = + name !== undefined + ? { name } + : { path: await fs.realpath(entry.path as string).catch(() => entry.path as string) }; + const replaced = rules.findIndex(rule => JSON.stringify(rule.selector) === JSON.stringify(selector)); + if (replaced !== -1) rules.splice(replaced, 1); + rules.push({ selector, enabled: entry.enabled as boolean }); + } + return rules; +} + /** Installed, enabled Codex plugins outside {@link HOST_PLUGINS}. */ async function listCodexPlugins(ctx: LoadContext): Promise { const warnings: string[] = []; @@ -440,7 +494,7 @@ async function listCodexPlugins(ctx: LoadContext): Promise { warnings.push( "[codex-plugins] This run cannot tell Scient Agent's launch environment from .env files (start it with --no-env-file); no Codex plugins load", ); - return { plugins: [], warnings, env: {} }; + return { plugins: [], warnings, env: {}, skillRules: [] }; } const env = filterProcessEnv(launch); // Codex's home: `CODEX_HOME`, else `~/.codex`. @@ -448,10 +502,10 @@ async function listCodexPlugins(ctx: LoadContext): Promise { const policy = await managedPolicy(); if (policy !== undefined) { warnings.push(`[codex-plugins] ${policy}, which Scient Agent cannot apply; no Codex plugins load`); - return { plugins: [], warnings, env }; + return { plugins: [], warnings, env, skillRules: [] }; } const user = await readToml(path.join(home, "config.toml"), warnings); - if (user === null) return { plugins: [], warnings, env }; + if (user === null) return { plugins: [], warnings, env, skillRules: [] }; const system = await readToml(path.join(systemDir(), "config.toml"), warnings); const merged = mergeLayers(system ?? {}, user); const table = isRecord(merged.plugins) ? merged.plugins : {}; @@ -472,23 +526,21 @@ async function listCodexPlugins(ctx: LoadContext): Promise { ...plugin, label: shared.has(plugin.name) ? `${plugin.name}:${plugin.marketplace}` : plugin.name, })); - return { plugins, warnings, env }; + return { plugins, warnings, env, skillRules: await readSkillRules(user, warnings) }; } -/** - * A manifest path inside the package, symlinks resolved; null (with a warning) when Codex would - * ignore it: not `./`-relative, holding `..`, or resolving outside the package. - */ -async function packagePath(plugin: CodexPlugin, value: unknown, key: string, warnings: string[]) { - if (typeof value !== "string" || !value) return null; - const relative = value.startsWith("./") ? value.slice(2) : ""; - if (!relative || relative.split(/[\\/]/).includes("..")) { - warnings.push(`[codex-plugins] Ignoring ${key} path of ${plugin.id}: ${value} (Codex takes ./paths without ..)`); - return null; - } +/** A manifest path's part below `./`, or null when Codex ignores the path: not `./`-relative, or holding `..`. */ +function manifestRelative(value: unknown): string | null { + if (typeof value !== "string" || !value.startsWith("./")) return null; + const relative = value.slice(2); + return relative && !relative.split(/[\\/]/).includes("..") ? relative : null; +} + +/** A manifest path Codex takes, symlinks resolved; null (with a warning when it resolves outside the package). */ +async function packagePath(plugin: CodexPlugin, relative: string, key: string, warnings: string[]) { const resolved = await resolveContainedPath(plugin.root, path.resolve(plugin.root, relative)); if (resolved.status === "outside") { - warnings.push(`[codex-plugins] Ignoring ${key} path outside ${plugin.id}: ${value}`); + warnings.push(`[codex-plugins] Ignoring ${key} path outside ${plugin.id}: ./${relative}`); return null; } return resolved.status === "ok" ? resolved.realPath : null; @@ -579,20 +631,44 @@ async function scanPluginSkills(plugin: CodexPlugin, dir: string, warnings: stri async function loadSkills(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; - const { plugins, warnings } = await listCodexPlugins(ctx); - const items: Skill[] = []; + const { plugins, warnings, skillRules } = await listCodexPlugins(ctx); + const found: Skill[] = []; await Promise.all( plugins.map(async plugin => { const declared = plugin.format === "standard" ? undefined : plugin.manifest.skills; - const entries = declared == null ? ["./skills"] : Array.isArray(declared) ? declared : [declared]; + const listed = declared == null ? [] : Array.isArray(declared) ? declared : [declared]; + // Codex drops the paths it does not take, and falls back to `skills/` when none is left. + const taken = listed.map(manifestRelative).filter(relative => relative !== null); + if (taken.length < listed.length) { + warnings.push(`[codex-plugins] Ignoring skills paths of ${plugin.id} that are not ./paths without ..`); + } const dirs = new Set(); - for (const entry of entries) { - const dir = await packagePath(plugin, entry, "skills", warnings); + for (const relative of taken.length > 0 ? taken : ["skills"]) { + const dir = await packagePath(plugin, relative, "skills", warnings); if (dir !== null) dirs.add(dir); } - for (const dir of dirs) items.push(...(await scanPluginSkills(plugin, dir, warnings))); + for (const dir of dirs) { + for (const skill of await scanPluginSkills(plugin, dir, warnings)) found.push(skill); + } }), ); + // The user's [[skills.config]] rules, in order: a path selects one SKILL.md, a name every skill + // so named (bare, or as Codex names plugin skills, `:`). + const realPaths = await Promise.all(found.map(skill => fs.realpath(skill.path).catch(() => skill.path))); + const disabled = new Set(); + for (const rule of skillRules) { + found.forEach((skill, index) => { + const plugin = skill._source.pluginName; + const selected = + "path" in rule.selector + ? rule.selector.path === realPaths[index] + : rule.selector.name === skill.name || rule.selector.name === `${plugin}:${skill.name}`; + if (!selected) return; + if (rule.enabled) disabled.delete(realPaths[index]); + else disabled.add(realPaths[index]); + }); + } + const items = found.filter((_, index) => !disabled.has(realPaths[index])); // Reads finish in any order; the list does not. items.sort((a, b) => compareSkillOrder(a.name, a.path, b.name, b.path)); return { items, warnings }; @@ -616,6 +692,9 @@ function toolFilterKey(table: Record): string | undefined { if (table.enabled_tools != null) return "enabled_tools"; const denied = table.disabled_tools; if (denied != null && !(Array.isArray(denied) && denied.length === 0)) return "disabled_tools"; + // Codex hides the server's tools from the model where this lists. + const omitted = table.omit_tools_from; + if (omitted != null && !(Array.isArray(omitted) && omitted.length === 0)) return "omit_tools_from"; return undefined; } @@ -642,6 +721,9 @@ const isStringMap = (value: unknown): value is Record => const isString = (value: unknown) => typeof value === "string"; const isBoolean = (value: unknown) => typeof value === "boolean"; const isPositiveInteger = (value: unknown) => Number.isInteger(value) && (value as number) > 0; +/** Seconds Rust's `Duration::try_from_secs_f64` takes, as Codex converts them: finite, not negative, under 2^64. */ +const isDurationSeconds = (value: unknown) => + typeof value === "number" && Number.isFinite(value) && value >= 0 && value < 2 ** 64; const isApprovalMode = (value: unknown) => ["auto", "prompt", "writes", "approve"].includes(value as string); /** Codex's string fields of a legacy manifest's `interface` (camelCase, with its URL aliases). */ @@ -698,15 +780,16 @@ const SERVER_FIELD_TYPES: Record boolean> = { http_headers_helper: isString, environment_id: isString, auth: value => value === "oauth" || value === "chatgpt" || value === "ema_auth", - startup_timeout_sec: value => typeof value === "number" && Number.isFinite(value), + startup_timeout_sec: isDurationSeconds, startup_timeout_ms: value => Number.isInteger(value) && (value as number) >= 0, - tool_timeout_sec: value => typeof value === "number" && Number.isFinite(value) && value >= 0, + tool_timeout_sec: isDurationSeconds, enabled: isBoolean, required: isBoolean, supports_parallel_tool_calls: isBoolean, startup_readiness: value => value === "connection" || value === "catalog", tool_input_schema_max_bytes: isPositiveInteger, - omit_tools_from: isStringList, + omit_tools_from: value => + isStringList(value) && value.every(surface => ["code_mode", "deferred", "direct"].includes(surface)), default_tools_approval_mode: isApprovalMode, enabled_tools: isStringList, disabled_tools: isStringList, @@ -863,9 +946,14 @@ async function readLegacyServerMap( let parsed: unknown = declared; let sourcePath = manifestPath; if (!isRecord(declared)) { - const file = await packagePath(plugin, declared ?? "./.mcp.json", "mcpServers", warnings); + // Codex drops a path it does not take, or a value of another type, and reads `.mcp.json` instead. + const relative = manifestRelative(declared); + if (declared != null && relative === null) { + warnings.push(`[codex-plugins] Ignoring mcpServers of ${plugin.id}, which is not a ./path without ..`); + } + const file = await packagePath(plugin, relative ?? ".mcp.json", "mcpServers", warnings); if (file === null) { - if (declared != null) warnings.push(`[codex-plugins] Missing mcpServers file declared by ${plugin.id}`); + if (relative !== null) warnings.push(`[codex-plugins] Missing mcpServers file declared by ${plugin.id}`); return null; } try { @@ -954,7 +1042,7 @@ async function readStandardServers( async function loadMCPServers(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; const { plugins, warnings, env } = await listCodexPlugins(ctx); - const items: MCPServer[] = []; + const found: { identity: string; server: MCPServer }[] = []; for (const plugin of plugins) { const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; const standard = plugin.format === "standard" ? await readStandardServers(ctx, plugin, env, warnings) : null; @@ -996,15 +1084,33 @@ async function loadMCPServers(ctx: LoadContext): Promise> warnings.push(`${skip}: ${mapped.error} (${sourcePath})`); continue; } - items.push({ - name: `${plugin.label}:${serverName}`, - ...mapped.server, - // The plugin's `mcp_servers.` table turns the server off with `enabled = false`. - ...(policy.enabled === false && { enabled: false }), - _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), + found.push({ + identity: `${plugin.id}/${serverName}`, + server: { + name: `${plugin.label}:${serverName}`, + ...mapped.server, + // The plugin's `mcp_servers.` table turns the server off with `enabled = false`. + ...(policy.enabled === false && { enabled: false }), + _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), + }, }); } } + // Tool names keep only lowercase letters, digits and `_` of a server's name (mcp/tool-bridge.ts), + // so `twin:alpha:api` and `twin-alpha:api` would share their tools' names; such servers take a + // suffix from their plugin and server instead. + const toolPrefix = (name: string) => + name + .toLowerCase() + .replace(/[^a-z0-9_]+/g, "_") + .replace(/_+/g, "_") + .replace(/^_+|_+$/g, ""); + const items = found.map(({ identity, server }) => { + const prefix = toolPrefix(server.name); + if (found.filter(other => toolPrefix(other.server.name) === prefix).length < 2) return server; + const digest = new Bun.CryptoHasher("sha256").update(identity).digest("hex").slice(0, 6); + return { ...server, name: `${server.name}-${digest}` }; + }); return { items, warnings }; } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 650e2d2823..8df4694bd3 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -239,6 +239,22 @@ describe("installed Codex plugins", () => { expect(warnings).toContainEqual(expect.stringContaining("default_tools_approval_mode")); }); + it("skips a server Codex hides from the model or would not start", async () => { + await installLimits({ + hidden: { command: "node", args: ["hidden.mjs"], omit_tools_from: ["direct", "deferred", "code_mode"] }, + negative: { command: "node", args: ["negative.mjs"], startup_timeout_sec: -1 }, + shown: { command: "node", args: ["shown.mjs"], omit_tools_from: [] }, + }); + const { names, warnings } = await limits(); + expect(names).toEqual(["limits:shown"]); + expect(warnings).toContainEqual( + expect.stringContaining('"hidden" of limits@market: Codex restricts its tools (omit_tools_from)'), + ); + expect(warnings).toContainEqual( + expect.stringContaining('"negative" of limits@market: invalid startup_timeout_sec'), + ); + }); + it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { await installLimits( { denied: { command: "node", args: ["denied.mjs"] }, open: { command: "node", args: ["open.mjs"] } }, @@ -408,11 +424,41 @@ describe("installed Codex plugins", () => { await install("twin@beta", { ".codex-plugin/plugin.json": { name: "twin" }, ".mcp.json": server }); // Its name is what `twin` qualified with `alpha` would look like if dots qualified names. await install("twin.alpha@gamma", { ".codex-plugin/plugin.json": { name: "twin.alpha" }, ".mcp.json": server }); - expect([...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort()).toEqual([ + const names = [...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort(); + // `twin.alpha` and `twin:alpha` would give their tools the same names (mcp__twin_alpha_…), so + // both take a stable suffix; `twin:beta` keeps its name. + expect(names.map(name => name.replace(/-[0-9a-f]{6}$/, ""))).toEqual([ "twin.alpha:api", "twin:alpha:api", "twin:beta:api", ]); + expect(names).toContain("twin:beta:api"); + expect(new Set(names.map(name => name.toLowerCase().replace(/[^a-z0-9_]+/g, "_"))).size).toBe(3); + }); + + it("leaves out plugin skills the user's Codex config disables", async () => { + const root = await fs.realpath(await install("ruled@market", { ".codex-plugin/plugin.json": { name: "ruled" } })); + for (const name of ["off-by-path", "off-by-name", "back-on", "kept"]) await writeSkill(root, name); + const configPath = temp.join("codex-home", "config.toml"); + const rules = [ + "[[skills.config]]", + `path = ${JSON.stringify(path.join(root, "skills", "off-by-path", "SKILL.md"))}`, + "enabled = false", + "[[skills.config]]", + 'name = "ruled:off-by-name"', + "enabled = false", + // A later entry for the same selector replaces the earlier one. + "[[skills.config]]", + 'name = "back-on"', + "enabled = false", + "[[skills.config]]", + 'name = "back-on"', + "enabled = true", + ]; + await Bun.write(configPath, [await Bun.file(configPath).text(), ...rules, ""].join("\n")); + clearFsCache(); + const names = (await skills()).filter(skill => skill.path.startsWith(root)).map(skill => skill.name); + expect(names.sort()).toEqual(["back-on", "kept"]); }); it("reads the launch environment only where the process can know it", async () => { @@ -594,7 +640,7 @@ describe("installed Codex plugins", () => { expect(byName.get("textflag:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); }); - it("skips a manifest Codex cannot read and paths it would not take", async () => { + it("skips a manifest Codex cannot read and falls back as Codex does from paths it would not take", async () => { const root = await install("typed@market", { ".codex-plugin/plugin.json": { name: 5 }, ".mcp.json": { mcpServers: { x: { command: "node" } } }, @@ -608,12 +654,13 @@ describe("installed Codex plugins", () => { clearFsCache(); const { byName, warnings } = await load(); expect(byName.has("typed:x")).toBe(false); - expect(byName.has("loose:y")).toBe(false); expect(warnings).toContainEqual(expect.stringContaining("Invalid plugin.json for typed@market (name)")); - expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers path of loose@market")); + // Codex drops both paths and reads the package's skills/ and .mcp.json instead. + expect(byName.has("loose:y")).toBe(true); + expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers of loose@market")); const names = (await skills()).map(skill => skill.name); expect(names).not.toContain("typed-skill"); - expect(names).not.toContain("loose-skill"); + expect(names).toContain("loose-skill"); }); it("reads Codex's manifest precedence: a root Agent Plugins manifest, then the legacy folders", async () => { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index ab00e56da1..bb7de4ef3b 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths count only as Codex takes them (./-relative, without ..) and must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, and carry containRoot so skill:// stays in the package. A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts, and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name. enabled = false in a declaration or in the plugin's mcp_servers. table disables a server; an ema_auth key there leaves it out with a warning, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query whose failure counts as managed; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, or holding ..) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name, bare or :; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration takes them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name; servers whose names would give their tools the same names (tool-bridge keeps only [a-z0-9_]) take a -<6 hex> suffix from /. enabled = false in a declaration or in the plugin's mcp_servers. table disables a server; an ema_auth key there leaves it out with a warning, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts" From e6abb5a2e9349800cdbbc86c42d467b576ea6998 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 11:32:50 +0200 Subject: [PATCH 08/30] fix(codex-plugins): stable server names, manifest-named skill rules, strict [plugins], u64 timeouts, POSIX paths Sixth Codex review of 24ee20a228: - Server names no longer take a suffix that depends on other plugins, so disabledServers and enabledServers keep matching; the normalized tool-name collision stays a documented gap. - [[skills.config]] names match :, as Codex namespaces plugin skills. - A merged [plugins] table with a field of a type Codex rejects loads nothing, as in Codex. - startup_timeout_ms must fit a u64. - A backslash separates manifest path parts only on Windows, and rooted paths are dropped. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- .../coding-agent/src/scient/codex-plugins.ts | 97 ++++++++++++------- .../test/scient/codex-plugins.test.ts | 44 +++++++-- scripts/scient/divergences.json | 2 +- 5 files changed, 102 insertions(+), 45 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index d3931a44e6..e9bb926295 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off for good (`ema_auth`) and one with malformed or unsupported fields, while its siblings load. Servers whose names would give their tools the same names take a short stable suffix. Nothing loads while an organization policy manages Codex, or may and cannot be checked. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off for good (`ema_auth`) and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index b549f7febf..202f38c85c 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`) or always asks about (`prompt` or `writes` approval), servers Codex turns off for good (`ema_auth`), and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`) or always asks about (`prompt` or `writes` approval), servers Codex turns off for good (`ema_auth`), and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 8a830d324c..f786cf2d9a 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -484,6 +484,36 @@ async function readSkillRules(user: Record, warnings: string[]) return rules; } +/** + * The first field of the merged `[plugins]` table whose type Codex rejects (`PluginsConfigToml`), + * or undefined. Codex then reads no plugin settings at all. + */ +function pluginsTypeError(table: Record): string | undefined { + for (const [id, config] of Object.entries(table)) { + if (!isRecord(config)) return id; + if (config.enabled != null && !isBoolean(config.enabled)) return `${id}.enabled`; + for (const key of ["remote_plugin_id", "release_id"]) { + if (config[key] != null && !isString(config[key])) return `${id}.${key}`; + } + if (config.apps != null && !isRecord(config.apps)) return `${id}.apps`; + const servers = config.mcp_servers; + if (servers == null) continue; + if (!isRecord(servers)) return `${id}.mcp_servers`; + for (const [name, policy] of Object.entries(servers)) { + const at = `${id}.mcp_servers.${name}`; + if (!isRecord(policy)) return at; + if (policy.enabled != null && !isBoolean(policy.enabled)) return `${at}.enabled`; + const mode = policy.default_tools_approval_mode; + if (mode != null && !isApprovalMode(mode)) return `${at}.default_tools_approval_mode`; + for (const key of ["enabled_tools", "disabled_tools"]) { + if (policy[key] != null && !isStringList(policy[key])) return `${at}.${key}`; + } + if (policy.tools != null && !SERVER_FIELD_TYPES.tools(policy.tools)) return `${at}.tools`; + } + } + return undefined; +} + /** Installed, enabled Codex plugins outside {@link HOST_PLUGINS}. */ async function listCodexPlugins(ctx: LoadContext): Promise { const warnings: string[] = []; @@ -509,6 +539,12 @@ async function listCodexPlugins(ctx: LoadContext): Promise { const system = await readToml(path.join(systemDir(), "config.toml"), warnings); const merged = mergeLayers(system ?? {}, user); const table = isRecord(merged.plugins) ? merged.plugins : {}; + // Codex reads no plugin at all from a [plugins] table it cannot read. + const mistyped = pluginsTypeError(table); + if (mistyped !== undefined) { + warnings.push(`[codex-plugins] Codex cannot read its [plugins] settings (${mistyped}); no Codex plugins load`); + return { plugins: [], warnings, env, skillRules: [] }; + } const defaults = isRecord(table._default) ? table._default : {}; const enabledByDefault = defaults.enabled !== false; const read = await Promise.all( @@ -529,11 +565,16 @@ async function listCodexPlugins(ctx: LoadContext): Promise { return { plugins, warnings, env, skillRules: await readSkillRules(user, warnings) }; } -/** A manifest path's part below `./`, or null when Codex ignores the path: not `./`-relative, or holding `..`. */ +/** + * A manifest path's part below `./`, or null when Codex ignores the path: not `./`-relative, + * rooted, or holding `..`. A backslash separates path parts only on Windows, as in Codex. + */ function manifestRelative(value: unknown): string | null { if (typeof value !== "string" || !value.startsWith("./")) return null; const relative = value.slice(2); - return relative && !relative.split(/[\\/]/).includes("..") ? relative : null; + if (!relative || relative.startsWith("/")) return null; + if (IS_WINDOWS && (relative.startsWith("\\") || /^[A-Za-z]:/.test(relative))) return null; + return relative.split(IS_WINDOWS ? /[\\/]/ : "/").includes("..") ? null : relative; } /** A manifest path Codex takes, symlinks resolved; null (with a warning when it resolves outside the package). */ @@ -632,7 +673,8 @@ async function scanPluginSkills(plugin: CodexPlugin, dir: string, warnings: stri async function loadSkills(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; const { plugins, warnings, skillRules } = await listCodexPlugins(ctx); - const found: Skill[] = []; + // Each skill with the namespace Codex gives it: its plugin manifest's name. + const found: { skill: Skill; namespace: string }[] = []; await Promise.all( plugins.map(async plugin => { const declared = plugin.format === "standard" ? undefined : plugin.manifest.skills; @@ -647,28 +689,29 @@ async function loadSkills(ctx: LoadContext): Promise> { const dir = await packagePath(plugin, relative, "skills", warnings); if (dir !== null) dirs.add(dir); } + const namespace = + typeof plugin.manifest.name === "string" && plugin.manifest.name ? plugin.manifest.name : plugin.name; for (const dir of dirs) { - for (const skill of await scanPluginSkills(plugin, dir, warnings)) found.push(skill); + for (const skill of await scanPluginSkills(plugin, dir, warnings)) found.push({ skill, namespace }); } }), ); // The user's [[skills.config]] rules, in order: a path selects one SKILL.md, a name every skill - // so named (bare, or as Codex names plugin skills, `:`). - const realPaths = await Promise.all(found.map(skill => fs.realpath(skill.path).catch(() => skill.path))); + // so named (bare, or as Codex names plugin skills, `:`). + const realPaths = await Promise.all(found.map(({ skill }) => fs.realpath(skill.path).catch(() => skill.path))); const disabled = new Set(); for (const rule of skillRules) { - found.forEach((skill, index) => { - const plugin = skill._source.pluginName; + found.forEach(({ skill, namespace }, index) => { const selected = "path" in rule.selector ? rule.selector.path === realPaths[index] - : rule.selector.name === skill.name || rule.selector.name === `${plugin}:${skill.name}`; + : rule.selector.name === skill.name || rule.selector.name === `${namespace}:${skill.name}`; if (!selected) return; if (rule.enabled) disabled.delete(realPaths[index]); else disabled.add(realPaths[index]); }); } - const items = found.filter((_, index) => !disabled.has(realPaths[index])); + const items = found.filter((_, index) => !disabled.has(realPaths[index])).map(({ skill }) => skill); // Reads finish in any order; the list does not. items.sort((a, b) => compareSkillOrder(a.name, a.path, b.name, b.path)); return { items, warnings }; @@ -781,7 +824,7 @@ const SERVER_FIELD_TYPES: Record boolean> = { environment_id: isString, auth: value => value === "oauth" || value === "chatgpt" || value === "ema_auth", startup_timeout_sec: isDurationSeconds, - startup_timeout_ms: value => Number.isInteger(value) && (value as number) >= 0, + startup_timeout_ms: value => Number.isInteger(value) && (value as number) >= 0 && (value as number) < 2 ** 64, tool_timeout_sec: isDurationSeconds, enabled: isBoolean, required: isBoolean, @@ -1042,7 +1085,7 @@ async function readStandardServers( async function loadMCPServers(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; const { plugins, warnings, env } = await listCodexPlugins(ctx); - const found: { identity: string; server: MCPServer }[] = []; + const items: MCPServer[] = []; for (const plugin of plugins) { const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; const standard = plugin.format === "standard" ? await readStandardServers(ctx, plugin, env, warnings) : null; @@ -1084,33 +1127,15 @@ async function loadMCPServers(ctx: LoadContext): Promise> warnings.push(`${skip}: ${mapped.error} (${sourcePath})`); continue; } - found.push({ - identity: `${plugin.id}/${serverName}`, - server: { - name: `${plugin.label}:${serverName}`, - ...mapped.server, - // The plugin's `mcp_servers.` table turns the server off with `enabled = false`. - ...(policy.enabled === false && { enabled: false }), - _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), - }, + items.push({ + name: `${plugin.label}:${serverName}`, + ...mapped.server, + // The plugin's `mcp_servers.` table turns the server off with `enabled = false`. + ...(policy.enabled === false && { enabled: false }), + _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), }); } } - // Tool names keep only lowercase letters, digits and `_` of a server's name (mcp/tool-bridge.ts), - // so `twin:alpha:api` and `twin-alpha:api` would share their tools' names; such servers take a - // suffix from their plugin and server instead. - const toolPrefix = (name: string) => - name - .toLowerCase() - .replace(/[^a-z0-9_]+/g, "_") - .replace(/_+/g, "_") - .replace(/^_+|_+$/g, ""); - const items = found.map(({ identity, server }) => { - const prefix = toolPrefix(server.name); - if (found.filter(other => toolPrefix(other.server.name) === prefix).length < 2) return server; - const digest = new Bun.CryptoHasher("sha256").update(identity).digest("hex").slice(0, 6); - return { ...server, name: `${server.name}-${digest}` }; - }); return { items, warnings }; } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 8df4694bd3..2c4c42be4e 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -243,6 +243,8 @@ describe("installed Codex plugins", () => { await installLimits({ hidden: { command: "node", args: ["hidden.mjs"], omit_tools_from: ["direct", "deferred", "code_mode"] }, negative: { command: "node", args: ["negative.mjs"], startup_timeout_sec: -1 }, + // One past u64::MAX, which Codex's startup_timeout_ms cannot hold. + huge: { command: "node", args: ["huge.mjs"], startup_timeout_ms: 2 ** 64 }, shown: { command: "node", args: ["shown.mjs"], omit_tools_from: [] }, }); const { names, warnings } = await limits(); @@ -424,18 +426,48 @@ describe("installed Codex plugins", () => { await install("twin@beta", { ".codex-plugin/plugin.json": { name: "twin" }, ".mcp.json": server }); // Its name is what `twin` qualified with `alpha` would look like if dots qualified names. await install("twin.alpha@gamma", { ".codex-plugin/plugin.json": { name: "twin.alpha" }, ".mcp.json": server }); - const names = [...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort(); - // `twin.alpha` and `twin:alpha` would give their tools the same names (mcp__twin_alpha_…), so - // both take a stable suffix; `twin:beta` keeps its name. - expect(names.map(name => name.replace(/-[0-9a-f]{6}$/, ""))).toEqual([ + expect([...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort()).toEqual([ "twin.alpha:api", "twin:alpha:api", "twin:beta:api", ]); - expect(names).toContain("twin:beta:api"); - expect(new Set(names.map(name => name.toLowerCase().replace(/[^a-z0-9_]+/g, "_"))).size).toBe(3); }); + it("matches a skill rule to the name Codex gives a plugin's skills, whatever the marketplace", async () => { + for (const id of ["twin@alpha", "twin@beta"]) { + const root = await install(id, { ".codex-plugin/plugin.json": { name: "twin" } }); + await writeSkill(root, "search"); + } + const configPath = temp.join("codex-home", "config.toml"); + const rule = ["[[skills.config]]", 'name = "twin:search"', "enabled = false", ""]; + await Bun.write(configPath, [await Bun.file(configPath).text(), ...rule].join("\n")); + clearFsCache(); + expect((await skills()).map(skill => skill.name)).not.toContain("search"); + }); + + it("loads no plugin from a [plugins] table Codex cannot read", async () => { + await install("strict@market", { ".codex-plugin/plugin.json": { name: "strict" } }, ['enabled = "false"']); + const { byName, warnings } = await load(); + expect(byName.size).toBe(0); + expect(warnings).toContainEqual( + expect.stringContaining("cannot read its [plugins] settings (strict@market.enabled)"), + ); + }); + + it.skipIf(process.platform === "win32")( + "reads a backslash as part of a file name outside Windows, as Codex does", + async () => { + await install("slashed@market", { + ".codex-plugin/plugin.json": { name: "slashed", mcpServers: "./sub\\..\\.mcp.json" }, + ".mcp.json": { mcpServers: { fallback: { command: "node" } } }, + }); + const { byName, warnings } = await load(); + // Codex takes the path, finds no such file, and does not fall back to .mcp.json. + expect(byName.has("slashed:fallback")).toBe(false); + expect(warnings).toContainEqual(expect.stringContaining("Missing mcpServers file declared by slashed@market")); + }, + ); + it("leaves out plugin skills the user's Codex config disables", async () => { const root = await fs.realpath(await install("ruled@market", { ".codex-plugin/plugin.json": { name: "ruled" } })); for (const name of ["off-by-path", "off-by-name", "back-on", "kept"]) await writeSkill(root, name); diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index bb7de4ef3b..2e8806d133 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, or holding ..) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name, bare or :; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration takes them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name; servers whose names would give their tools the same names (tool-bridge keeps only [a-z0-9_]) take a -<6 hex> suffix from /. enabled = false in a declaration or in the plugin's mcp_servers. table disables a server; an ema_auth key there leaves it out with a warning, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]; a merged [plugins] table with a field of a type Codex rejects loads nothing, as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name, bare or :; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name; the name never depends on other plugins' names, so settings keyed by it stay put. enabled = false in a declaration or in the plugin's mcp_servers. table disables a server; an ema_auth key there leaves it out with a warning, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gap: two servers whose names differ only in characters tool names drop (twin:alpha:api, twin-alpha:api) mint the same tool names, and the registry keeps one.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts" From c3a232e9ed5ddd9416192a74580d99111799877c Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 12:20:51 +0200 Subject: [PATCH 09/30] fix(codex-plugins): exact CODEX_HOME, plugins switch, top-level servers, stable ids, Codex skill names, app and limit checks Seventh Codex review of e6abb5a2e9: - CODEX_HOME is used exactly as set; it must be a folder and is canonicalized, as in Codex. - [features] plugins anything but true turns every plugin off. - A plugin server the config's own [mcp_servers] names is left out: Codex runs that entry instead. - [[skills.config]] names match only :, the package folder standing in for a blank manifest name. - auto_provision and apps (AppConfig, its tools and links) are type-checked with the rest of [plugins]. - A server with a per-tool output_token_limit is skipped. - Servers are always named @: and skills carry the plugin ID, so no other plugin's arrival renames them and saved settings keep matching. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- .../coding-agent/src/scient/codex-plugins.ts | 134 ++++++++++---- .../test/scient/codex-plugins.test.ts | 171 ++++++++++++------ scripts/scient/divergences.json | 2 +- 5 files changed, 224 insertions(+), 87 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index e9bb926295..d6fdf9a87a 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off for good (`ema_auth`) and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. Servers are named `@:`. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off for good (`ema_auth`), one the user's own `[mcp_servers]` replaces, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 202f38c85c..40bff8259a 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`) or always asks about (`prompt` or `writes` approval), servers Codex turns off for good (`ema_auth`), and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off for good (`ema_auth`) or replaces with your own `[mcp_servers]` entry, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index f786cf2d9a..495d6874b2 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -147,11 +147,6 @@ interface CodexPlugin { id: string; name: string; marketplace: string; - /** - * Skill namespace and MCP server prefix: the name, or `:` when two enabled - * plugins share it. Neither part may contain `:`, so the qualified form never meets another. - */ - label: string; /** The active version's folder, symlinks resolved; every package path stays inside it. */ root: string; /** `standard`: a root Agent Plugins `plugin.json`, read under that standard; `legacy`: a Codex-style manifest. */ @@ -175,6 +170,8 @@ interface PluginList { env: Record; /** The user config's skill rules, in Codex's order. */ skillRules: SkillRule[]; + /** Servers the config names in `[mcp_servers]`, which Codex runs instead of a plugin's same-named one. */ + configuredServers: ReadonlySet; } /** Codex's rule for a cache path segment (plugin, marketplace or version name). */ @@ -290,7 +287,7 @@ async function readPlugin( id: string, config: Record, warnings: string[], -): Promise | null> { +): Promise { const at = id.lastIndexOf("@"); const name = id.slice(0, at); const marketplace = id.slice(at + 1); @@ -484,6 +481,41 @@ async function readSkillRules(user: Record, warnings: string[]) return rules; } +/** + * Where an app's settings in a plugin's `apps` table have a type Codex rejects (`AppConfig`), as + * a key path suffix (`""` for the table itself), or undefined. + */ +function appTypeError(settings: unknown): string | undefined { + const isReviewer = (value: unknown) => value === "user" || value === "auto_review" || value === "guardian_subagent"; + if (!isRecord(settings)) return ""; + for (const key of ["enabled", "destructive_enabled", "open_world_enabled", "default_tools_enabled"]) { + if (settings[key] != null && !isBoolean(settings[key])) return `.${key}`; + } + if (settings.omit_tools_from != null && !SERVER_FIELD_TYPES.omit_tools_from(settings.omit_tools_from)) { + return ".omit_tools_from"; + } + if (settings.approvals_reviewer != null && !isReviewer(settings.approvals_reviewer)) return ".approvals_reviewer"; + const mode = settings.default_tools_approval_mode; + if (mode != null && !isApprovalMode(mode)) return ".default_tools_approval_mode"; + for (const [group, fields] of [ + ["tools", { enabled: isBoolean, approval_mode: isApprovalMode, approvals_reviewer: isReviewer }], + ["links", { approvals_reviewer: isReviewer, default_tools_approval_mode: isApprovalMode }], + ] as const) { + const entries = settings[group]; + if (entries == null) continue; + if (!isRecord(entries)) return `.${group}`; + for (const [name, entry] of Object.entries(entries)) { + if (!isRecord(entry)) return `.${group}.${name}`; + for (const [field, valid] of Object.entries(fields)) { + if (entry[field] != null && !(valid as (value: unknown) => boolean)(entry[field])) { + return `.${group}.${name}.${field}`; + } + } + } + } + return undefined; +} + /** * The first field of the merged `[plugins]` table whose type Codex rejects (`PluginsConfigToml`), * or undefined. Codex then reads no plugin settings at all. @@ -495,7 +527,13 @@ function pluginsTypeError(table: Record): string | undefined { for (const key of ["remote_plugin_id", "release_id"]) { if (config[key] != null && !isString(config[key])) return `${id}.${key}`; } - if (config.apps != null && !isRecord(config.apps)) return `${id}.apps`; + if (config.auto_provision != null && !isBoolean(config.auto_provision)) return `${id}.auto_provision`; + const apps = config.apps; + if (apps != null && !isRecord(apps)) return `${id}.apps`; + for (const [app, settings] of Object.entries(apps ?? {})) { + const mistyped = appTypeError(settings); + if (mistyped !== undefined) return `${id}.apps.${app}${mistyped}`; + } const servers = config.mcp_servers; if (servers == null) continue; if (!isRecord(servers)) return `${id}.mcp_servers`; @@ -517,6 +555,13 @@ function pluginsTypeError(table: Record): string | undefined { /** Installed, enabled Codex plugins outside {@link HOST_PLUGINS}. */ async function listCodexPlugins(ctx: LoadContext): Promise { const warnings: string[] = []; + const none = (env: Record): PluginList => ({ + plugins: [], + warnings, + env, + skillRules: [], + configuredServers: new Set(), + }); // The environment Scient Agent was launched with: no `.env` file, from this project or one the // agent worked in before, chooses Codex's home or what reaches a plugin's server. const launch = getLaunchEnv(); @@ -524,26 +569,40 @@ async function listCodexPlugins(ctx: LoadContext): Promise { warnings.push( "[codex-plugins] This run cannot tell Scient Agent's launch environment from .env files (start it with --no-env-file); no Codex plugins load", ); - return { plugins: [], warnings, env: {}, skillRules: [] }; + return none({}); } const env = filterProcessEnv(launch); - // Codex's home: `CODEX_HOME`, else `~/.codex`. - const home = env.CODEX_HOME?.trim() || path.join(ctx.home, ".codex"); + // Codex's home: `CODEX_HOME` exactly as set, which must be a folder, canonicalized; else `~/.codex`. + let home = path.join(ctx.home, ".codex"); + if (env.CODEX_HOME) { + if (!(await fs.stat(env.CODEX_HOME).catch(() => null))?.isDirectory()) { + warnings.push( + `[codex-plugins] CODEX_HOME points to ${JSON.stringify(env.CODEX_HOME)}, which is not a folder, so Codex does not start; no Codex plugins load`, + ); + return none(env); + } + home = await fs.realpath(env.CODEX_HOME); + } const policy = await managedPolicy(); if (policy !== undefined) { warnings.push(`[codex-plugins] ${policy}, which Scient Agent cannot apply; no Codex plugins load`); - return { plugins: [], warnings, env, skillRules: [] }; + return none(env); } const user = await readToml(path.join(home, "config.toml"), warnings); - if (user === null) return { plugins: [], warnings, env, skillRules: [] }; + if (user === null) return none(env); const system = await readToml(path.join(systemDir(), "config.toml"), warnings); const merged = mergeLayers(system ?? {}, user); + // `[features] plugins = false` turns every plugin off in Codex. + if (isRecord(merged.features) && "plugins" in merged.features && merged.features.plugins !== true) { + warnings.push("[codex-plugins] Codex's plugins are turned off ([features] plugins); no Codex plugins load"); + return none(env); + } const table = isRecord(merged.plugins) ? merged.plugins : {}; // Codex reads no plugin at all from a [plugins] table it cannot read. const mistyped = pluginsTypeError(table); if (mistyped !== undefined) { warnings.push(`[codex-plugins] Codex cannot read its [plugins] settings (${mistyped}); no Codex plugins load`); - return { plugins: [], warnings, env, skillRules: [] }; + return none(env); } const defaults = isRecord(table._default) ? table._default : {}; const enabledByDefault = defaults.enabled !== false; @@ -554,15 +613,13 @@ async function listCodexPlugins(ctx: LoadContext): Promise { return enabled ? readPlugin(home, id, config, warnings) : null; }), ); - const found = read.filter(plugin => plugin !== null); - const shared = new Set( - found.filter((plugin, index) => found.findIndex(other => other.name === plugin.name) !== index).map(p => p.name), - ); - const plugins = found.map(plugin => ({ - ...plugin, - label: shared.has(plugin.name) ? `${plugin.name}:${plugin.marketplace}` : plugin.name, - })); - return { plugins, warnings, env, skillRules: await readSkillRules(user, warnings) }; + return { + plugins: read.filter(plugin => plugin !== null), + warnings, + env, + skillRules: await readSkillRules(user, warnings), + configuredServers: new Set(isRecord(merged.mcp_servers) ? Object.keys(merged.mcp_servers) : []), + }; } /** @@ -616,7 +673,7 @@ async function readPluginSkill( level: "user", // skill:// resolves every file of the skill inside the package too. containRoot: plugin.root, - _source: createSourceMeta(PROVIDER_ID, skillPath, "user", undefined, plugin.label), + _source: createSourceMeta(PROVIDER_ID, skillPath, "user", undefined, plugin.id), }; } catch { warnings.push(`[codex-plugins] Failed to read skill file: ${skillPath}`); @@ -689,15 +746,17 @@ async function loadSkills(ctx: LoadContext): Promise> { const dir = await packagePath(plugin, relative, "skills", warnings); if (dir !== null) dirs.add(dir); } - const namespace = - typeof plugin.manifest.name === "string" && plugin.manifest.name ? plugin.manifest.name : plugin.name; + // Codex names a plugin's skills `:`, the package folder's name standing + // in for a blank manifest name. + const raw = typeof plugin.manifest.name === "string" ? plugin.manifest.name : ""; + const namespace = raw.trim() ? raw : path.basename(plugin.root); for (const dir of dirs) { for (const skill of await scanPluginSkills(plugin, dir, warnings)) found.push({ skill, namespace }); } }), ); - // The user's [[skills.config]] rules, in order: a path selects one SKILL.md, a name every skill - // so named (bare, or as Codex names plugin skills, `:`). + // The user's [[skills.config]] rules, in order: a path selects one SKILL.md, a name the skill + // Codex names so. const realPaths = await Promise.all(found.map(({ skill }) => fs.realpath(skill.path).catch(() => skill.path))); const disabled = new Set(); for (const rule of skillRules) { @@ -705,7 +764,7 @@ async function loadSkills(ctx: LoadContext): Promise> { const selected = "path" in rule.selector ? rule.selector.path === realPaths[index] - : rule.selector.name === skill.name || rule.selector.name === `${namespace}:${skill.name}`; + : rule.selector.name === `${namespace}:${skill.name}`; if (!selected) return; if (rule.enabled) disabled.delete(realPaths[index]); else disabled.add(realPaths[index]); @@ -743,8 +802,9 @@ function toolFilterKey(table: Record): string | undefined { /** * The Codex key that makes some of a server's tools ask before they run (`prompt`, `writes`, or a - * mode Scient Agent does not know), or undefined when Codex runs them all without asking. Scient - * Agent cannot carry Codex's approval modes over, so such a server would run its tools unasked. + * mode Scient Agent does not know) or caps their output (`output_token_limit`), or undefined when + * neither applies. Scient Agent cannot carry either over, so such a server would run its tools + * unasked or return more than Codex allows. */ function approvalKey(table: Record): string | undefined { const unasked = (mode: unknown) => mode == null || mode === "auto" || mode === "approve"; @@ -753,6 +813,7 @@ function approvalKey(table: Record): string | undefined { if (!isRecord(table.tools)) return "tools"; for (const [tool, config] of Object.entries(table.tools)) { if (!isRecord(config) || !unasked(config.approval_mode)) return `tools.${tool}.approval_mode`; + if (config.output_token_limit != null) return `tools.${tool}.output_token_limit`; } return undefined; } @@ -1084,7 +1145,7 @@ async function readStandardServers( async function loadMCPServers(ctx: LoadContext): Promise> { if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; - const { plugins, warnings, env } = await listCodexPlugins(ctx); + const { plugins, warnings, env, configuredServers } = await listCodexPlugins(ctx); const items: MCPServer[] = []; for (const plugin of plugins) { const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; @@ -1103,6 +1164,12 @@ async function loadMCPServers(ctx: LoadContext): Promise> logger.debug(`${skip}: ${reason}`); continue; } + // Codex registers the config's own `[mcp_servers.]` over a plugin's server of that name, + // so its settings, `enabled = false` included, are what Codex runs. + if (configuredServers.has(serverName)) { + warnings.push(`${skip}: Codex runs your own [mcp_servers.${serverName}] instead`); + continue; + } const declaredPolicy = policies[serverName]; const policy: Record = isRecord(declaredPolicy) ? declaredPolicy : {}; const restricted = @@ -1128,11 +1195,12 @@ async function loadMCPServers(ctx: LoadContext): Promise> continue; } items.push({ - name: `${plugin.label}:${serverName}`, + // The plugin's ID names the server, so no other plugin's arrival renames it. + name: `${plugin.id}:${serverName}`, ...mapped.server, // The plugin's `mcp_servers.` table turns the server off with `enabled = false`. ...(policy.enabled === false && { enabled: false }), - _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.label), + _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.id), }); } } diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 2c4c42be4e..2306798a47 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -162,8 +162,12 @@ describe("installed Codex plugins", () => { const byName = new Map((await servers()).map(server => [server.name, server])); // The Computer Use client launcher runs only inside Codex. - expect([...byName.keys()].sort()).toEqual(["research:papers", "research:quiet", "research:tracker"]); - expect(byName.get("research:papers")).toMatchObject({ + expect([...byName.keys()].sort()).toEqual([ + "research@market:papers", + "research@market:quiet", + "research@market:tracker", + ]); + expect(byName.get("research@market:papers")).toMatchObject({ command: path.join(researchRoot, "bin", "papers"), args: ["--stdio"], cwd: researchRoot, @@ -172,18 +176,18 @@ describe("installed Codex plugins", () => { transport: "stdio", }); // Codex passes its own short list plus the server's env_vars, never the whole environment. - const papers = byName.get("research:papers"); + const papers = byName.get("research@market:papers"); expect(papers?.envInherit).toEqual([]); expect(papers?.env).toMatchObject({ PATH: process.env.PATH, CODEX_PLUGIN_TEST_TOKEN: "forwarded-value" }); expect(papers?.env?.CODEX_PLUGIN_TEST_PAT).toBeUndefined(); - expect(byName.get("research:tracker")).toMatchObject({ + expect(byName.get("research@market:tracker")).toMatchObject({ url: "https://mcp.example.test/mcp", headers: { Authorization: "Bearer pat-value" }, headerPolicy: "origin-locked", transport: "http", }); // The plugin's config table turns this server off, as Codex does. - expect(byName.get("research:quiet")?.enabled).toBe(false); + expect(byName.get("research@market:quiet")?.enabled).toBe(false); }); /** @@ -206,7 +210,7 @@ describe("installed Codex plugins", () => { const limits = async () => { const result = await loadCapability("mcps", { cwd, agentDir }); return { - names: result.items.filter(server => server.name.startsWith("limits:")).map(server => server.name), + names: result.items.filter(server => server.name.startsWith("limits@market:")).map(server => server.name), warnings: result.warnings.filter(warning => warning.includes("of limits@market")), }; }; @@ -217,7 +221,7 @@ describe("installed Codex plugins", () => { open: { command: "node", args: ["open.mjs"], disabled_tools: [] }, }); expect(await limits()).toEqual({ - names: ["limits:open"], + names: ["limits@market:open"], warnings: [ '[Codex Plugins] [codex-plugins] Skipping MCP server "narrow" of limits@market: Codex restricts its tools (enabled_tools), which Scient Agent cannot enforce', ], @@ -234,7 +238,7 @@ describe("installed Codex plugins", () => { ['[plugins."limits@market".mcp_servers.prompted]', 'default_tools_approval_mode = "prompt"'], ); const { names, warnings } = await limits(); - expect(names).toEqual(["limits:approved"]); + expect(names).toEqual(["limits@market:approved"]); expect(warnings).toContainEqual(expect.stringContaining("tools.save.approval_mode")); expect(warnings).toContainEqual(expect.stringContaining("default_tools_approval_mode")); }); @@ -245,16 +249,22 @@ describe("installed Codex plugins", () => { negative: { command: "node", args: ["negative.mjs"], startup_timeout_sec: -1 }, // One past u64::MAX, which Codex's startup_timeout_ms cannot hold. huge: { command: "node", args: ["huge.mjs"], startup_timeout_ms: 2 ** 64 }, + capped: { command: "node", args: ["capped.mjs"], tools: { read: { output_token_limit: 100 } } }, shown: { command: "node", args: ["shown.mjs"], omit_tools_from: [] }, }); const { names, warnings } = await limits(); - expect(names).toEqual(["limits:shown"]); + expect(names).toEqual(["limits@market:shown"]); expect(warnings).toContainEqual( expect.stringContaining('"hidden" of limits@market: Codex restricts its tools (omit_tools_from)'), ); expect(warnings).toContainEqual( expect.stringContaining('"negative" of limits@market: invalid startup_timeout_sec'), ); + expect(warnings).toContainEqual( + expect.stringContaining( + '"capped" of limits@market: Codex restricts its tools (tools.read.output_token_limit)', + ), + ); }); it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { @@ -263,7 +273,7 @@ describe("installed Codex plugins", () => { ['[plugins."limits@market".mcp_servers.denied]', 'disabled_tools = ["delete"]'], ); expect(await limits()).toEqual({ - names: ["limits:open"], + names: ["limits@market:open"], warnings: [ '[Codex Plugins] [codex-plugins] Skipping MCP server "denied" of limits@market: Codex restricts its tools (disabled_tools), which Scient Agent cannot enforce', ], @@ -277,7 +287,7 @@ describe("installed Codex plugins", () => { open: { command: "node", args: ["open.mjs"] }, }); expect(await limits()).toEqual({ - names: ["limits:open"], + names: ["limits@market:open"], warnings: [ `[Codex Plugins] [codex-plugins] Skipping MCP server "bad" of limits@market: invalid env_vars (${mcpPath})`, `[Codex Plugins] [codex-plugins] Skipping MCP server "headers" of limits@market: invalid http_headers (${mcpPath})`, @@ -332,7 +342,7 @@ describe("installed Codex plugins", () => { ].join("\n"), ); clearFsCache(); - expect([...(await load()).byName.keys()]).toEqual(["opted:one"]); + expect([...(await load()).byName.keys()]).toEqual(["opted@market:one"]); }); it("rejects servers Codex would reject and maps OAuth client settings", async () => { @@ -356,8 +366,8 @@ describe("installed Codex plugins", () => { }, }); const { byName, warnings } = await load(); - expect([...byName.keys()].filter(name => name.startsWith("strict:"))).toEqual(["strict:signin"]); - expect(byName.get("strict:signin")).toMatchObject({ + expect([...byName.keys()].filter(name => name.startsWith("strict@market:"))).toEqual(["strict@market:signin"]); + expect(byName.get("strict@market:signin")).toMatchObject({ oauth: { clientId: "client-1", callbackPort: 4567, scope: "read write" }, auth: { type: "oauth", resource: "https://mcp.example.test/mcp" }, }); @@ -382,16 +392,16 @@ describe("installed Codex plugins", () => { }, }); const { byName, warnings } = await load(); - expect([...byName.keys()].filter(name => name.startsWith("paths:")).sort()).toEqual([ - "paths:fromPath", - "paths:literal", + expect([...byName.keys()].filter(name => name.startsWith("paths@market:")).sort()).toEqual([ + "paths@market:fromPath", + "paths@market:literal", ]); // Origin-locked headers and literal env are never resolved as variables or `!commands`. - expect(byName.get("paths:literal")).toMatchObject({ + expect(byName.get("paths@market:literal")).toMatchObject({ headers: { "X-Run": "!touch /tmp/pwned" }, headerPolicy: "origin-locked", }); - expect(byName.get("paths:fromPath")).toMatchObject({ + expect(byName.get("paths@market:fromPath")).toMatchObject({ command: "node", cwd: root, env: { MODE: "CODEX_PLUGIN_TEST_PAT" }, @@ -416,7 +426,7 @@ describe("installed Codex plugins", () => { clearFsCache(); expect((await skills()).map(skill => skill.name)).toEqual(["lit-review"]); const { byName, warnings } = await load(); - expect([...byName.keys()].some(name => name.startsWith("linked:"))).toBe(false); + expect([...byName.keys()].some(name => name.startsWith("linked@market:"))).toBe(false); expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers path outside linked@market")); }); @@ -427,30 +437,86 @@ describe("installed Codex plugins", () => { // Its name is what `twin` qualified with `alpha` would look like if dots qualified names. await install("twin.alpha@gamma", { ".codex-plugin/plugin.json": { name: "twin.alpha" }, ".mcp.json": server }); expect([...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort()).toEqual([ - "twin.alpha:api", - "twin:alpha:api", - "twin:beta:api", + "twin.alpha@gamma:api", + "twin@alpha:api", + "twin@beta:api", ]); }); - it("matches a skill rule to the name Codex gives a plugin's skills, whatever the marketplace", async () => { + it("matches skill rules to the name Codex gives a plugin's skills, and only that name", async () => { for (const id of ["twin@alpha", "twin@beta"]) { const root = await install(id, { ".codex-plugin/plugin.json": { name: "twin" } }); await writeSkill(root, "search"); } + // A blank manifest name leaves Codex the package folder's name, the version. + const blank = await install("blank@market", { ".codex-plugin/plugin.json": { name: " " } }); + await writeSkill(blank, "find"); const configPath = temp.join("codex-home", "config.toml"); - const rule = ["[[skills.config]]", 'name = "twin:search"', "enabled = false", ""]; - await Bun.write(configPath, [await Bun.file(configPath).text(), ...rule].join("\n")); + const rules = [ + "[[skills.config]]", + 'name = "twin:search"', + "enabled = false", + // Codex names no plugin skill bare `search`, so this enables nothing. + "[[skills.config]]", + 'name = "search"', + "enabled = true", + "[[skills.config]]", + 'name = "1.0.0:find"', + "enabled = false", + "", + ]; + await Bun.write(configPath, [await Bun.file(configPath).text(), ...rules].join("\n")); clearFsCache(); - expect((await skills()).map(skill => skill.name)).not.toContain("search"); + const names = (await skills()).map(skill => skill.name); + expect(names).not.toContain("search"); + expect(names).not.toContain("find"); }); it("loads no plugin from a [plugins] table Codex cannot read", async () => { - await install("strict@market", { ".codex-plugin/plugin.json": { name: "strict" } }, ['enabled = "false"']); - const { byName, warnings } = await load(); - expect(byName.size).toBe(0); - expect(warnings).toContainEqual( - expect.stringContaining("cannot read its [plugins] settings (strict@market.enabled)"), + const configPath = temp.join("codex-home", "config.toml"); + const original = await Bun.file(configPath).text(); + for (const [line, field] of [ + ['enabled = "false"', "strict@market.enabled"], + ['auto_provision = "yes"', "strict@market.auto_provision"], + ["apps.connector = false", "strict@market.apps.connector"], + [ + 'apps.connector.tools.list.approval_mode = "sometimes"', + "strict@market.apps.connector.tools.list.approval_mode", + ], + ]) { + await Bun.write(configPath, original); + await install("strict@market", { ".codex-plugin/plugin.json": { name: "strict" } }, [line]); + const { byName, warnings } = await load(); + expect(byName.size).toBe(0); + expect(warnings).toContainEqual(expect.stringContaining(`cannot read its [plugins] settings (${field})`)); + } + }); + + it("follows Codex's home, plugins switch and own server settings", async () => { + const codexHome = temp.join("codex-home"); + const configPath = path.join(codexHome, "config.toml"); + const original = await Bun.file(configPath).text(); + restoreEnvValue("SCIENT_AGENT_ENABLE_SOURCES", "codex-plugins"); + // Codex opens CODEX_HOME exactly as set: with a trailing space it names another folder. + restoreEnvValue("CODEX_HOME", `${codexHome} `); + let result = await load(); + expect(result.byName.size).toBe(0); + expect(result.warnings).toContainEqual(expect.stringContaining("which is not a folder")); + restoreEnvValue("CODEX_HOME", codexHome); + // `[features] plugins = false` turns every plugin off. + await Bun.write(configPath, `${original}\n[features]\nplugins = false\n`); + clearFsCache(); + result = await load(); + expect(result.byName.size).toBe(0); + expect(result.warnings).toContainEqual(expect.stringContaining("plugins are turned off")); + // The config's own [mcp_servers.papers] is what Codex runs, not the plugin's server. + await Bun.write(configPath, `${original}\n[mcp_servers.papers]\ncommand = "node"\nenabled = false\n`); + clearFsCache(); + result = await load(); + expect(result.byName.has("research@market:papers")).toBe(false); + expect(result.byName.has("research@market:tracker")).toBe(true); + expect(result.warnings).toContainEqual( + expect.stringContaining("Codex runs your own [mcp_servers.papers] instead"), ); }); @@ -463,7 +529,7 @@ describe("installed Codex plugins", () => { }); const { byName, warnings } = await load(); // Codex takes the path, finds no such file, and does not fall back to .mcp.json. - expect(byName.has("slashed:fallback")).toBe(false); + expect(byName.has("slashed@market:fallback")).toBe(false); expect(warnings).toContainEqual(expect.stringContaining("Missing mcpServers file declared by slashed@market")); }, ); @@ -565,18 +631,18 @@ describe("installed Codex plugins", () => { await fs.symlink(outside, path.join(linkedRoot, ".codex-plugin"), "dir"); clearFsCache(); const { byName, warnings } = await load(); - expect(byName.get("rules:off")?.enabled).toBe(false); + expect(byName.get("rules@market:off")?.enabled).toBe(false); // Codex turns an ema_auth server off for good, so it is not listed for a setting to turn on. - expect(byName.has("rules:retired")).toBe(false); + expect(byName.has("rules@market:retired")).toBe(false); expect(warnings).toContainEqual( expect.stringContaining('"retired" of rules@market: Codex turns it off (ema_auth)'), ); - expect(byName.has("rules:numeric")).toBe(false); + expect(byName.has("rules@market:numeric")).toBe(false); expect(warnings).toContainEqual(expect.stringContaining('"numeric" of rules@market: invalid command')); - expect(byName.get("rules:withheld")?.env).toMatchObject({ KEEP: "y" }); - expect(byName.get("rules:withheld")?.env?.NODE_REPL_AUTH_TOKEN).toBeUndefined(); - expect(byName.get("rules:withheld")?.env?.openai_spiffe_id).toBeUndefined(); - expect(byName.has("linkedfolder:x")).toBe(false); + expect(byName.get("rules@market:withheld")?.env).toMatchObject({ KEEP: "y" }); + expect(byName.get("rules@market:withheld")?.env?.NODE_REPL_AUTH_TOKEN).toBeUndefined(); + expect(byName.get("rules@market:withheld")?.env?.openai_spiffe_id).toBeUndefined(); + expect(byName.has("linkedfolder@market:x")).toBe(false); expect(warnings).toContainEqual( expect.stringContaining("Ignoring linkedfolder@market: .codex-plugin is not a folder"), ); @@ -628,9 +694,12 @@ describe("installed Codex plugins", () => { await fs.symlink(path.join(linked, "real-mcp.json"), path.join(linked, "mcp.json")); clearFsCache(); const { byName, warnings } = await load(); - expect(byName.has("portable2:events")).toBe(false); - expect(byName.get("portable2:local")?.env).toMatchObject({ CODEX_PLUGIN_TEST_PAT: "pat-value", MODE: "x" }); - expect(byName.has("linkedmcp:y")).toBe(false); + expect(byName.has("portable2@market:events")).toBe(false); + expect(byName.get("portable2@market:local")?.env).toMatchObject({ + CODEX_PLUGIN_TEST_PAT: "pat-value", + MODE: "x", + }); + expect(byName.has("linkedmcp@market:y")).toBe(false); expect(warnings).toContainEqual(expect.stringContaining("linkedmcp@market: mcp.json is not a regular file")); }); @@ -667,9 +736,9 @@ describe("installed Codex plugins", () => { }, }); const { byName } = await load(); - expect(byName.get("viaclaude:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); - expect(byName.get("invalidoverlay:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); - expect(byName.get("textflag:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("viaclaude@market:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("invalidoverlay@market:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("textflag@market:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); }); it("skips a manifest Codex cannot read and falls back as Codex does from paths it would not take", async () => { @@ -685,10 +754,10 @@ describe("installed Codex plugins", () => { await writeSkill(loose, "loose-skill"); clearFsCache(); const { byName, warnings } = await load(); - expect(byName.has("typed:x")).toBe(false); + expect(byName.has("typed@market:x")).toBe(false); expect(warnings).toContainEqual(expect.stringContaining("Invalid plugin.json for typed@market (name)")); // Codex drops both paths and reads the package's skills/ and .mcp.json instead. - expect(byName.has("loose:y")).toBe(true); + expect(byName.has("loose@market:y")).toBe(true); expect(warnings).toContainEqual(expect.stringContaining("Ignoring mcpServers of loose@market")); const names = (await skills()).map(skill => skill.name); expect(names).not.toContain("typed-skill"); @@ -709,9 +778,9 @@ describe("installed Codex plugins", () => { ".mcp.json": { mcpServers: { tool: { command: "node" } } }, }); const { byName } = await load(); - expect(byName.get("portable:remote")).toMatchObject({ transport: "http", headerPolicy: "origin-locked" }); - expect(byName.has("portable:legacy")).toBe(false); - expect(byName.has("clauded:tool")).toBe(true); + expect(byName.get("portable@market:remote")).toMatchObject({ transport: "http", headerPolicy: "origin-locked" }); + expect(byName.has("portable@market:legacy")).toBe(false); + expect(byName.has("clauded@market:tool")).toBe(true); }); it("starts a stdio server with only the variables Codex would pass", async () => { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 2e8806d133..673df5b5ea 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (default ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]; a merged [plugins] table with a field of a type Codex rejects loads nothing, as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name, bare or :; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named :, or :: when two enabled plugins share a name; the name never depends on other plugins' names, so settings keyed by it stay put. enabled = false in a declaration or in the plugin's mcp_servers. table disables a server; an ema_auth key there leaves it out with a warning, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gap: two servers whose names differ only in characters tool names drop (twin:alpha:api, twin-alpha:api) mint the same tool names, and the registry keeps one.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or the name Codex gives the skill, :, the package folder's name standing in for a blank manifest name; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. A plugin server the config's own [mcp_servers] names is left out with a warning, as Codex runs that entry instead. enabled = false in a declaration or in the plugin's mcp_servers. table disables a server; an ema_auth key there leaves it out with a warning, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts" From c9eaa439531cd20be45d7e6b607766c3b577f5e1 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 12:45:43 +0200 Subject: [PATCH 10/30] fix(codex-plugins): own sign-in per imported server, Codex disables and duplicates, Codex skill names Eighth Codex review of c3a232e9ed: - Each imported HTTP server signs in under its own credential (mcpOAuthServerCredentialId: mcp_oauth:server:::), never another server's at the same URL; the profile and URL parsers read it. Reauthorization takes its values literally, asks for its configured scopes (empty meaning none, discovery's scope dropped) and oauth_resource first, stores the credential under that id and writes nothing back. - A server Codex turns off (enabled = false in its declaration or policy) is left out, so enabledServers cannot start it. - Servers two enabled plugins declare under one name are left out: Codex runs only one. - Skill names collapse their whitespace as Codex's parser does before rules match them. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- .../coding-agent/src/mcp/oauth-credentials.ts | 6 ++ packages/coding-agent/src/mcp/oauth-flow.ts | 39 +++++++- packages/coding-agent/src/mcp/oauth-reauth.ts | 63 ++++++++++--- .../coding-agent/src/scient/codex-plugins.ts | 61 ++++++++++--- .../test/scient/codex-plugins-oauth.test.ts | 91 +++++++++++++++++++ .../test/scient/codex-plugins.test.ts | 73 ++++++++++----- scripts/scient/divergences.json | 10 +- 9 files changed, 292 insertions(+), 55 deletions(-) create mode 100644 packages/coding-agent/test/scient/codex-plugins-oauth.test.ts diff --git a/SCIENT.md b/SCIENT.md index d6fdf9a87a..9b1bd7c62c 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. Servers are named `@:`. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off for good (`ema_auth`), one the user's own `[mcp_servers]` replaces, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. Servers are named `@:`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), never another server's at the same URL, and re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 40bff8259a..e89d3bca5c 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off for good (`ema_auth`) or replaces with your own `[mcp_servers]` entry, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index d2db7c7b0a..0e8270ff72 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -6,6 +6,9 @@ import { expandEnvVarsDeep } from "../discovery/helpers"; import type { AuthStorage } from "../session/auth-storage"; import { isManagedMCPOAuthCredentialId, + // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. + isMcpOAuthServerCredentialId, + // scient(SA-CODEX-PLUGINS): end type MCPStoredOAuthCredential, mcpOAuthCredentialId, mcpOAuthCredentialProfile, @@ -54,6 +57,9 @@ export function lookupMcpOAuthCredentialForServer( return { credentialId: auth.credentialId, credential }; } } + // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in never takes another server's by URL. + if (isMcpOAuthServerCredentialId(auth?.credentialId)) return undefined; + // scient(SA-CODEX-PLUGINS): end if (options.allowUrlKeyedFallback === false) return undefined; for (const credentialId of urlKeyedCredentialIds) { const credential = authStorage.credentials.get(credentialId); diff --git a/packages/coding-agent/src/mcp/oauth-flow.ts b/packages/coding-agent/src/mcp/oauth-flow.ts index 0c5492f114..2e2b15475b 100644 --- a/packages/coding-agent/src/mcp/oauth-flow.ts +++ b/packages/coding-agent/src/mcp/oauth-flow.ts @@ -33,6 +33,27 @@ export function mcpOAuthCredentialId(serverUrl: string, profile: string | undefi return `${MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX}${profile ?? "default"}:${serverUrl}`; } +// scient(SA-CODEX-PLUGINS): begin - a sign-in bound to one server, as Codex keys MCP sign-ins by server name and URL. +/** Credential-id prefix for one server's own sign-in (`mcp_oauth:server:::`). */ +const MCP_OAUTH_SERVER_CREDENTIAL_PREFIX = `${MCP_OAUTH_URL_CREDENTIAL_PREFIX}server:`; + +/** + * Credential id for one server's own sign-in, under its profile. Lookups find it only through the + * server's `auth.credentialId`, never by URL, so no other server at that URL shares it. + */ +export function mcpOAuthServerCredentialId( + serverName: string, + serverUrl: string, + profile: string | undefined = getActiveProfile(), +): string { + return `${MCP_OAUTH_SERVER_CREDENTIAL_PREFIX}${profile ?? "default"}:${encodeURIComponent(serverName)}:${serverUrl}`; +} + +/** Whether a credential id is one server's own sign-in ({@link mcpOAuthServerCredentialId}). */ +export function isMcpOAuthServerCredentialId(credentialId: string | undefined): boolean { + return credentialId?.startsWith(MCP_OAUTH_SERVER_CREDENTIAL_PREFIX) === true; +} +// scient(SA-CODEX-PLUGINS): end /** Whether a credential id was minted by OMP's MCP OAuth flows (either era). */ export function isManagedMCPOAuthCredentialId(credentialId: string | undefined): credentialId is string { return ( @@ -49,9 +70,14 @@ export function isManagedMCPOAuthCredentialId(credentialId: string | undefined): * is the profile; everything after it is the URL. */ export function mcpOAuthCredentialProfile(credentialId: string): string | undefined { - if (!credentialId.startsWith(MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX)) return undefined; - const separator = credentialId.indexOf(":", MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX.length); - return separator === -1 ? undefined : credentialId.slice(MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX.length, separator); + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in carries its profile in the same place. + const prefix = [MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX, MCP_OAUTH_SERVER_CREDENTIAL_PREFIX].find(candidate => + credentialId.startsWith(candidate), + ); + if (prefix === undefined) return undefined; + const separator = credentialId.indexOf(":", prefix.length); + return separator === -1 ? undefined : credentialId.slice(prefix.length, separator); + // scient(SA-CODEX-PLUGINS): end } /** @@ -65,6 +91,13 @@ export function mcpOAuthCredentialProfile(credentialId: string): string | undefi * config — recover the server URL for the RFC 8707 fallback resource on refresh. */ export function mcpOAuthServerUrlFromCredentialId(credentialId: string): string | undefined { + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in: the URL follows its profile and server name. + if (credentialId.startsWith(MCP_OAUTH_SERVER_CREDENTIAL_PREFIX)) { + const profileEnd = credentialId.indexOf(":", MCP_OAUTH_SERVER_CREDENTIAL_PREFIX.length); + const nameEnd = profileEnd === -1 ? -1 : credentialId.indexOf(":", profileEnd + 1); + return nameEnd === -1 ? undefined : credentialId.slice(nameEnd + 1) || undefined; + } + // scient(SA-CODEX-PLUGINS): end if (credentialId.startsWith(MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX)) { const separator = credentialId.indexOf(":", MCP_OAUTH_PROFILE_CREDENTIAL_PREFIX.length); return separator === -1 ? undefined : credentialId.slice(separator + 1) || undefined; diff --git a/packages/coding-agent/src/mcp/oauth-reauth.ts b/packages/coding-agent/src/mcp/oauth-reauth.ts index f27d74535e..593bcf43a5 100644 --- a/packages/coding-agent/src/mcp/oauth-reauth.ts +++ b/packages/coding-agent/src/mcp/oauth-reauth.ts @@ -36,6 +36,9 @@ import { MCPOAuthFlow, type MCPStoredOAuthCredential, mcpOAuthCredentialId, + // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. + isMcpOAuthServerCredentialId, + // scient(SA-CODEX-PLUGINS): end } from "./oauth-flow"; import type { AuthStorage } from "../session/auth-storage"; import type { MCPAuthChallenge, MCPAuthConfig, MCPServerConfig } from "./types"; @@ -476,6 +479,10 @@ export interface McpOAuthFlowRunOptions extends McpOAuthFlowPresentation { registrationUrl?: string; issuerUrl?: string; resource?: string; + // scient(SA-CODEX-PLUGINS): begin - store the sign-in under a server-bound id. + /** Where to store the credential, instead of the server URL's own row. */ + credentialId?: string; + // scient(SA-CODEX-PLUGINS): end stripSameOriginResource?: boolean; callbackPort?: number; callbackPath?: string; @@ -596,9 +603,13 @@ export async function runMcpOAuthFlow(opts: McpOAuthFlowRunOptions): Promise server[key] != null); if (wrong !== undefined) return { error: `${wrong} is not supported for ${isStdio ? "stdio" : "streamable HTTP"}` }; const common = { - // `enabled = false` in the declaration turns the server off in Codex. - ...(server.enabled === false && { enabled: false }), ...(typeof server.tool_timeout_sec === "number" && server.tool_timeout_sec > 0 && { timeout: server.tool_timeout_sec * 1000 }), }; @@ -1024,7 +1025,8 @@ async function mapLegacyServer( } const port = oauthTable.callback_port ?? oauthTable.callbackPort; if (Number.isInteger(port) && Number(port) > 0) oauth.callbackPort = Number(port); - if (isStringList(server.scopes) && server.scopes.length > 0) oauth.scope = server.scopes.join(" "); + // Codex asks for the configured scopes as they are, an empty list meaning none. + if (isStringList(server.scopes)) oauth.scope = server.scopes.join(" "); const resource = typeof server.oauth_resource === "string" && server.oauth_resource ? server.oauth_resource : null; return { server: { @@ -1147,15 +1149,28 @@ async function loadMCPServers(ctx: LoadContext): Promise> if (!isUserSourceEnabled(PROVIDER_ID, ctx)) return { items: [] }; const { plugins, warnings, env, configuredServers } = await listCodexPlugins(ctx); const items: MCPServer[] = []; + const read: { plugin: CodexPlugin; declared: Record; sourcePath: string; standard: boolean }[] = []; for (const plugin of plugins) { - const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; const standard = plugin.format === "standard" ? await readStandardServers(ctx, plugin, env, warnings) : null; const legacy = plugin.format === "legacy" ? await readLegacyServerMap(plugin, plugin.manifest, plugin.manifestPath, warnings) : null; - const declared: Record = standard?.servers ?? legacy?.servers ?? {}; - const sourcePath = standard?.sourcePath ?? legacy?.sourcePath ?? plugin.manifestPath; + read.push({ + plugin, + declared: standard?.servers ?? legacy?.servers ?? {}, + sourcePath: standard?.sourcePath ?? legacy?.sourcePath ?? plugin.manifestPath, + standard: standard !== null, + }); + } + // Codex runs one server per name: when enabled plugins declare the same one, which declaration + // wins (and so whose settings apply) is Codex's call, so none of them runs here. + const declarers = new Map(); + for (const { plugin, declared } of read) { + for (const name of Object.keys(declared)) declarers.set(name, [...(declarers.get(name) ?? []), plugin.id]); + } + for (const { plugin, declared, sourcePath, standard } of read) { + const policies = isRecord(plugin.config.mcp_servers) ? plugin.config.mcp_servers : {}; for (const [serverName, server] of Object.entries(declared)) { if (!isRecord(server)) continue; const skip = `[codex-plugins] Skipping MCP server "${serverName}" of ${plugin.id}`; @@ -1170,6 +1185,11 @@ async function loadMCPServers(ctx: LoadContext): Promise> warnings.push(`${skip}: Codex runs your own [mcp_servers.${serverName}] instead`); continue; } + const others = (declarers.get(serverName) ?? []).filter(id => id !== plugin.id); + if (others.length > 0) { + warnings.push(`${skip}: ${others.join(", ")} declares a server of that name too, and Codex runs only one`); + continue; + } const declaredPolicy = policies[serverName]; const policy: Record = isRecord(declaredPolicy) ? declaredPolicy : {}; const restricted = @@ -1184,9 +1204,15 @@ async function loadMCPServers(ctx: LoadContext): Promise> warnings.push(`${skip}: Codex turns it off (ema_auth)`); continue; } + // Codex turns a server off for good when its declaration or the plugin's mcp_servers. + // table says `enabled = false`: no setting here may start it. + if (server.enabled === false || policy.enabled === false) { + warnings.push(`${skip}: Codex turns it off (enabled = false)`); + continue; + } let mapped: Mapped; try { - mapped = standard !== null ? { server } : await mapLegacyServer(plugin, server, env); + mapped = standard ? { server } : await mapLegacyServer(plugin, server, env); } catch (error) { mapped = { error: String(error) }; } @@ -1194,12 +1220,21 @@ async function loadMCPServers(ctx: LoadContext): Promise> warnings.push(`${skip}: ${mapped.error} (${sourcePath})`); continue; } + // The plugin's ID names the server, so no other plugin's arrival renames it. + const name = `${plugin.id}:${serverName}`; + const { server: config } = mapped; items.push({ - // The plugin's ID names the server, so no other plugin's arrival renames it. - name: `${plugin.id}:${serverName}`, - ...mapped.server, - // The plugin's `mcp_servers.` table turns the server off with `enabled = false`. - ...(policy.enabled === false && { enabled: false }), + name, + ...config, + // Its own sign-in, never another server's at the same URL: Codex keys sign-ins by name and URL. + ...(config.transport === "http" && + config.url && { + auth: { + ...config.auth, + type: "oauth" as const, + credentialId: mcpOAuthServerCredentialId(name, config.url), + }, + }), _source: createSourceMeta(PROVIDER_ID, sourcePath, "user", undefined, plugin.id), }); } diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts new file mode 100644 index 0000000000..655bcc871e --- /dev/null +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -0,0 +1,91 @@ +import { Database } from "bun:sqlite"; +import { afterEach, describe, expect, it, vi } from "bun:test"; +import * as path from "node:path"; +import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-ai"; +import * as mcpClient from "@oh-my-pi/pi-coding-agent/mcp/client"; +import { lookupMcpOAuthCredentialForServer } from "@oh-my-pi/pi-coding-agent/mcp/oauth-credentials"; +import * as oauthFlow from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow"; +import { reauthorizeMcpServer } from "@oh-my-pi/pi-coding-agent/mcp/oauth-reauth"; +import type { MCPServerConfig } from "@oh-my-pi/pi-coding-agent/mcp/types"; +import { TempDir } from "@oh-my-pi/pi-utils"; +import { restoreEnvValue } from "../helpers/settings-test-state"; + +const SERVER_URL = "https://mcp.example.test/mcp"; +const NAME = "tools@market:api"; +const placeholder = "$" + "{CODEX_PLUGIN_OAUTH_TEST}"; + +describe("an imported Codex plugin server's own sign-in", () => { + const stores: AuthStorage[] = []; + const freshStorage = () => { + const storage = new AuthStorage(new SqliteAuthCredentialStore(new Database(":memory:"))); + stores.push(storage); + return storage; + }; + const saved = process.env.CODEX_PLUGIN_OAUTH_TEST; + + afterEach(() => { + while (stores.length > 0) stores.pop()?.close(); + vi.restoreAllMocks(); + restoreEnvValue("CODEX_PLUGIN_OAUTH_TEST", saved); + }); + + it("never takes another server's sign-in at the same URL", async () => { + const storage = freshStorage(); + const token = { type: "oauth" as const, access: "a", refresh: "r", expires: Date.now() + 60_000 }; + await storage.credentials.set(oauthFlow.mcpOAuthCredentialId(SERVER_URL), { ...token, access: "write-scope" }); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const auth = { type: "oauth" as const, credentialId }; + expect(lookupMcpOAuthCredentialForServer(storage, auth, SERVER_URL)).toBeUndefined(); + await storage.credentials.set(credentialId, { ...token, access: "own" }); + expect(lookupMcpOAuthCredentialForServer(storage, auth, SERVER_URL)?.credential.access).toBe("own"); + expect(oauthFlow.mcpOAuthServerUrlFromCredentialId(credentialId)).toBe(SERVER_URL); + }); + + it("signs in with the plugin's literal values, configured scope and resource, under its own id", async () => { + // A value an `.env` file could supply; Codex never resolves a plugin header from it. + restoreEnvValue("CODEX_PLUGIN_OAUTH_TEST", "dotenv-secret"); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const config: MCPServerConfig = { + type: "http", + url: SERVER_URL, + headers: { "X-Team": placeholder }, + headerPolicy: "origin-locked", + auth: { type: "oauth", credentialId, resource: "https://resource.example.test/" }, + // The plugin asks for no scopes. + oauth: { scope: "" }, + }; + const probed: MCPServerConfig[] = []; + vi.spyOn(mcpClient, "connectToServer").mockImplementation((async (_name: string, probe: MCPServerConfig) => { + probed.push(probe); + throw new Error( + 'HTTP 401: {"authorization_url":"https://auth.example.test/authorize?scope=admin","token_url":"https://auth.example.test/token","resource":"https://discovered.example.test/","scopes":"admin"}', + ); + }) as never); + const flows: { scopes?: string }[] = []; + vi.spyOn(oauthFlow.MCPOAuthFlow.prototype, "login").mockImplementation( + async function (this: { config: { scopes?: string } }) { + flows.push(this.config); + return { access: "fresh", refresh: "fresh-refresh", expires: Date.now() + 3_600_000 }; + }, + ); + const storage = freshStorage(); + using temp = TempDir.createSync("@scient-codex-plugins-oauth-"); + const outcome = await reauthorizeMcpServer( + NAME, + { filePath: path.join(temp.path(), "mcp.json"), scope: "user", config, discovered: true }, + { authStorage: storage, silent: true }, + ); + expect(probed[0]).toMatchObject({ url: SERVER_URL, headers: { "X-Team": placeholder } }); + expect(flows[0]?.scopes).toBeUndefined(); + // Discovery's `scope=admin` is dropped from the authorization URL. + expect(storage.credentials.get(credentialId)).toMatchObject({ + access: "fresh", + resource: "https://resource.example.test/", + authorizationUrl: "https://auth.example.test/authorize", + }); + expect(storage.credentials.get(oauthFlow.mcpOAuthCredentialId(SERVER_URL))).toBeUndefined(); + // Found by its fixed id: nothing is written to a config file, and the auth block stays. + expect(outcome).toMatchObject({ persisted: false, credentialId, config: { auth: { credentialId } } }); + expect(await Bun.file(path.join(temp.path(), "mcp.json")).exists()).toBe(false); + }); +}); diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 2306798a47..9347e7a333 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -162,11 +162,7 @@ describe("installed Codex plugins", () => { const byName = new Map((await servers()).map(server => [server.name, server])); // The Computer Use client launcher runs only inside Codex. - expect([...byName.keys()].sort()).toEqual([ - "research@market:papers", - "research@market:quiet", - "research@market:tracker", - ]); + expect([...byName.keys()].sort()).toEqual(["research@market:papers", "research@market:tracker"]); expect(byName.get("research@market:papers")).toMatchObject({ command: path.join(researchRoot, "bin", "papers"), args: ["--stdio"], @@ -186,8 +182,8 @@ describe("installed Codex plugins", () => { headerPolicy: "origin-locked", transport: "http", }); - // The plugin's config table turns this server off, as Codex does. - expect(byName.get("research@market:quiet")?.enabled).toBe(false); + // The plugin's config table turns this server off in Codex for good, so it is not listed. + expect(byName.has("research@market:quiet")).toBe(false); }); /** @@ -431,18 +427,38 @@ describe("installed Codex plugins", () => { }); it("keeps same-named plugins from different marketplaces apart, whatever their names", async () => { - const server = { mcpServers: { api: { command: "node" } } }; - await install("twin@alpha", { ".codex-plugin/plugin.json": { name: "twin" }, ".mcp.json": server }); - await install("twin@beta", { ".codex-plugin/plugin.json": { name: "twin" }, ".mcp.json": server }); + await install("twin@alpha", { + ".codex-plugin/plugin.json": { name: "twin" }, + ".mcp.json": { mcpServers: { a: { command: "node" } } }, + }); + await install("twin@beta", { + ".codex-plugin/plugin.json": { name: "twin" }, + ".mcp.json": { mcpServers: { b: { command: "node" } } }, + }); // Its name is what `twin` qualified with `alpha` would look like if dots qualified names. - await install("twin.alpha@gamma", { ".codex-plugin/plugin.json": { name: "twin.alpha" }, ".mcp.json": server }); + await install("twin.alpha@gamma", { + ".codex-plugin/plugin.json": { name: "twin.alpha" }, + ".mcp.json": { mcpServers: { c: { command: "node" } } }, + }); expect([...(await load()).byName.keys()].filter(name => name.startsWith("twin")).sort()).toEqual([ - "twin.alpha@gamma:api", - "twin@alpha:api", - "twin@beta:api", + "twin.alpha@gamma:c", + "twin@alpha:a", + "twin@beta:b", ]); }); + it("runs none of the servers two enabled plugins declare under one name", async () => { + const server = { mcpServers: { api: { command: "node" } } }; + await install("first@market", { ".codex-plugin/plugin.json": { name: "first" }, ".mcp.json": server }); + await install("second@market", { ".codex-plugin/plugin.json": { name: "second" }, ".mcp.json": server }); + const { byName, warnings } = await load(); + expect(byName.has("first@market:api")).toBe(false); + expect(byName.has("second@market:api")).toBe(false); + expect(warnings).toContainEqual( + expect.stringContaining('"api" of first@market: second@market declares a server of that name too'), + ); + }); + it("matches skill rules to the name Codex gives a plugin's skills, and only that name", async () => { for (const id of ["twin@alpha", "twin@beta"]) { const root = await install(id, { ".codex-plugin/plugin.json": { name: "twin" } }); @@ -451,6 +467,8 @@ describe("installed Codex plugins", () => { // A blank manifest name leaves Codex the package folder's name, the version. const blank = await install("blank@market", { ".codex-plugin/plugin.json": { name: " " } }); await writeSkill(blank, "find"); + // Codex collapses the name's inner whitespace before a rule meets it. + await writeSkill(blank, "Paper Search"); const configPath = temp.join("codex-home", "config.toml"); const rules = [ "[[skills.config]]", @@ -463,6 +481,9 @@ describe("installed Codex plugins", () => { "[[skills.config]]", 'name = "1.0.0:find"', "enabled = false", + "[[skills.config]]", + 'name = "1.0.0:Paper Search"', + "enabled = false", "", ]; await Bun.write(configPath, [await Bun.file(configPath).text(), ...rules].join("\n")); @@ -470,6 +491,7 @@ describe("installed Codex plugins", () => { const names = (await skills()).map(skill => skill.name); expect(names).not.toContain("search"); expect(names).not.toContain("find"); + expect(names.some(name => name.startsWith("Paper"))).toBe(false); }); it("loads no plugin from a [plugins] table Codex cannot read", async () => { @@ -631,7 +653,10 @@ describe("installed Codex plugins", () => { await fs.symlink(outside, path.join(linkedRoot, ".codex-plugin"), "dir"); clearFsCache(); const { byName, warnings } = await load(); - expect(byName.get("rules@market:off")?.enabled).toBe(false); + expect(byName.has("rules@market:off")).toBe(false); + expect(warnings).toContainEqual( + expect.stringContaining('"off" of rules@market: Codex turns it off (enabled = false)'), + ); // Codex turns an ema_auth server off for good, so it is not listed for a setting to turn on. expect(byName.has("rules@market:retired")).toBe(false); expect(warnings).toContainEqual( @@ -710,21 +735,25 @@ describe("installed Codex plugins", () => { "mcp.json": { $schema: "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json", mcpServers: { - local: { type: "stdio", command: "node", args: [name], env: { CODEX_PLUGIN_TEST_PAT: placeholder } }, + [name]: { type: "stdio", command: "node", env: { CODEX_PLUGIN_TEST_PAT: placeholder } }, }, }, }); await install("viaclaude@market", { ...standard("viaclaude"), ".claude-plugin/plugin.json": { name: "viaclaude" }, - ".mcp.json": { mcpServers: { local: { command: "node", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } } }, + ".mcp.json": { mcpServers: { viaclaude: { command: "node", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } } }, }); await install("invalidoverlay@market", { ...standard("invalidoverlay"), ".codex-plugin/plugin.json": { name: "invalidoverlay" }, ".mcp.json": { mcpServers: { - local: { command: "node", url: "https://mcp.example.test/mcp", env_vars: ["CODEX_PLUGIN_TEST_PAT"] }, + invalidoverlay: { + command: "node", + url: "https://mcp.example.test/mcp", + env_vars: ["CODEX_PLUGIN_TEST_PAT"], + }, }, }, }); @@ -732,13 +761,13 @@ describe("installed Codex plugins", () => { ...standard("textflag"), ".codex-plugin/plugin.json": { name: "textflag" }, ".mcp.json": { - mcpServers: { local: { command: "node", enabled: "false", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } }, + mcpServers: { textflag: { command: "node", enabled: "false", env_vars: ["CODEX_PLUGIN_TEST_PAT"] } }, }, }); const { byName } = await load(); - expect(byName.get("viaclaude@market:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); - expect(byName.get("invalidoverlay@market:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); - expect(byName.get("textflag@market:local")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("viaclaude@market:viaclaude")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("invalidoverlay@market:invalidoverlay")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); + expect(byName.get("textflag@market:textflag")?.env?.CODEX_PLUGIN_TEST_PAT).toBe(placeholder); }); it("skips a manifest Codex cannot read and falls back as Codex does from paths it would not take", async () => { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 673df5b5ea..a172a98747 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,10 +520,11 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or the name Codex gives the skill, :, the package folder's name standing in for a blank manifest name; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes and oauth_resource. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. A plugin server the config's own [mcp_servers] names is left out with a warning, as Codex runs that entry instead. enabled = false in a declaration or in the plugin's mcp_servers. table disables a server; an ema_auth key there leaves it out with a warning, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take a frontmatter name with its whitespace collapsed as Codex's parser does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or the name Codex gives the skill, :, the package folder's name standing in for a blank manifest name; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read), mcp/oauth-credentials.ts never falls back from it to the URL-keyed row, and reauthorization (mcp/oauth-reauth.ts) takes the server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", - "packages/coding-agent/test/scient/codex-plugins.test.ts" + "packages/coding-agent/test/scient/codex-plugins.test.ts", + "packages/coding-agent/test/scient/codex-plugins-oauth.test.ts" ], "upstream": [ "packages/coding-agent/scripts/compile-binary.ts", @@ -531,13 +532,16 @@ "packages/coding-agent/src/capability/mcp.ts", "packages/coding-agent/src/discovery/index.ts", "packages/coding-agent/src/mcp/config.ts", + "packages/coding-agent/src/mcp/oauth-credentials.ts", + "packages/coding-agent/src/mcp/oauth-flow.ts", + "packages/coding-agent/src/mcp/oauth-reauth.ts", "packages/coding-agent/src/mcp/transports/stdio.ts", "packages/coding-agent/src/mcp/types.ts", "packages/coding-agent/CHANGELOG.md", "packages/utils/src/env.ts" ], "verification": [ - "bun --cwd=packages/coding-agent test test/scient/codex-plugins.test.ts" + "bun --cwd=packages/coding-agent test test/scient/codex-plugins.test.ts test/scient/codex-plugins-oauth.test.ts" ] } } From 084a5deef847f1715e7a74b6162c10bb4d896e4a Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 12:56:41 +0200 Subject: [PATCH 11/30] fix(codex-plugins): bound sign-ins through reconnect and unauth, bearer precedence, fallback skill names Ninth Codex review of c9eaa43953: - Reconnecting a server bound to its own sign-in keeps its auth block and headers, and matches only a manager entry with the same credential id. - TUI /mcp unauth removes only a bound server's own credential and rewrites no config. - A server declaring bearer_token_env_var is bound as an apikey: no OAuth sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login; a configured Authorization header wins over a bound sign-in. - A skill's folder-name fallback collapses Unicode whitespace too. - Native reauthorization keeps the flow's invalid-URL diagnostic. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- .../coding-agent/src/mcp/oauth-credentials.ts | 4 + packages/coding-agent/src/mcp/oauth-reauth.ts | 27 ++++- .../controllers/mcp-command-controller.ts | 12 +++ .../coding-agent/src/scient/codex-plugins.ts | 20 ++-- .../test/scient/codex-plugins-oauth.test.ts | 98 ++++++++++++++++++- .../test/scient/codex-plugins.test.ts | 8 ++ scripts/scient/divergences.json | 3 +- 9 files changed, 159 insertions(+), 17 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index 9b1bd7c62c..5981ed9e94 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. Servers are named `@:`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), never another server's at the same URL, and re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. Servers are named `@:`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), never another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, and `/mcp unauth` removes only it. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index e89d3bca5c..8a396b7a38 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures; one that names its own token keeps it and is never signed in with OAuth. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index 0e8270ff72..378cd09951 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -79,6 +79,10 @@ export function lookupMcpOAuthCredential( return lookupMcpOAuthCredentialForServer(authStorage, auth, undefined); } if (hasMcpAuthorizationHeader(config)) { + // scient(SA-CODEX-PLUGINS): begin - for a server bound to its own sign-in, the configured + // Authorization header (an imported Codex plugin's bearer) wins over a stored sign-in, as in Codex. + if (isMcpOAuthServerCredentialId(auth?.credentialId)) return undefined; + // scient(SA-CODEX-PLUGINS): end return lookupMcpOAuthCredentialForServer(authStorage, auth, config.url, { allowUrlKeyedFallback: false }); } return lookupMcpOAuthCredentialForServer(authStorage, auth, config.url); diff --git a/packages/coding-agent/src/mcp/oauth-reauth.ts b/packages/coding-agent/src/mcp/oauth-reauth.ts index 593bcf43a5..16b066d942 100644 --- a/packages/coding-agent/src/mcp/oauth-reauth.ts +++ b/packages/coding-agent/src/mcp/oauth-reauth.ts @@ -319,6 +319,10 @@ export function matchingManagerServerConfig( const managerConfig = manager?.getServerConfig(name); if (!managerConfig || managerConfig.type !== selected.type) return undefined; const urls = [expandEnvVarsDeep(selected.url), selected.url]; + // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in matches only the entry holding that sign-in. + const boundIds = [selected.auth?.credentialId, managerConfig.auth?.credentialId]; + if (boundIds.some(isMcpOAuthServerCredentialId) && boundIds[0] !== boundIds[1]) return undefined; + // scient(SA-CODEX-PLUGINS): end return urls.includes(managerConfig.url) ? managerConfig : undefined; } @@ -342,7 +346,11 @@ export async function reconnectMcpManagerServer( if (!manager) return undefined; const managerConfig = matchingManagerServerConfig(manager, name, selected); if (!managerConfig) return undefined; - const pending = manager.updateServerConfig(name, clearMcpClientAuthSurface(managerConfig)); + // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in keeps its auth block and headers: + // without them it would reconnect on another server's url-keyed sign-in. + const bound = isMcpOAuthServerCredentialId(managerConfig.auth?.credentialId); + const pending = manager.updateServerConfig(name, bound ? managerConfig : clearMcpClientAuthSurface(managerConfig)); + // scient(SA-CODEX-PLUGINS): end if (pending) await pending; await manager.reconnectServer(name, { manual: true }); return manager.getConnectionStatus(name); @@ -765,6 +773,12 @@ export async function reauthorizeMcpServer( // takes its values literally, as Codex does, and keeps its configured scope and resource below. const bound = isMcpOAuthServerCredentialId(currentAuth?.credentialId); const runtimeBaseConfig = bound ? baseConfig : expandEnvVarsDeep(baseConfig); + // Codex never signs in with OAuth to a server that names its own token (`bearer_token_env_var`). + if (bound && currentAuth?.type !== "oauth") { + throw new McpReauthRefusalError( + `Server "${name}" signs in with its own token, so Codex does not sign in to it with OAuth.`, + ); + } // scient(SA-CODEX-PLUGINS): end // Resolve endpoints first: this fails fast for stdio transports and // probes http/sse with { oauth: false }, so nothing destructive has @@ -836,13 +850,18 @@ export async function reauthorizeMcpServer( const oauthResource = preferredResource ?? ("url" in runtimeBaseConfig ? runtimeBaseConfig.url : undefined); const oauthResourceIsFallback = preferredResource === undefined; const configuredScope = bound ? runtimeBaseConfig.oauth?.scope : undefined; - const authUrl = new URL(oauth.authorizationUrl); - if (configuredScope !== undefined) authUrl.searchParams.delete("scope"); + // A URL that does not parse is left for the flow's own validation to report. + let authUrl = oauth.authorizationUrl; + if (configuredScope !== undefined && URL.canParse(authUrl)) { + const parsed = new URL(authUrl); + parsed.searchParams.delete("scope"); + authUrl = parsed.toString(); + } // scient(SA-CODEX-PLUGINS): end const oauthResult = await runMcpOAuthFlow({ // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in asks for its configured scopes. - authUrl: configuredScope !== undefined ? authUrl.toString() : oauth.authorizationUrl, + authUrl, tokenUrl: oauth.tokenUrl, clientId: flowClientId, clientSecret: flowClientSecret, diff --git a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts index 7f8e0e71b6..4d533ab3dc 100644 --- a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts +++ b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts @@ -48,6 +48,9 @@ import { removeManagedMcpOAuthCredentials, } from "../../mcp/oauth-credentials"; import type { MCPAuthChallenge, MCPConfigFile, MCPServerConfig, MCPServerConnection } from "../../mcp/types"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import { isMcpOAuthServerCredentialId } from "../../mcp/oauth-flow"; +// scient(SA-CODEX-PLUGINS): end import { shortenPath } from "@oh-my-pi/pi-tui/render/render-utils"; import { urlHyperlinkAlways } from "@oh-my-pi/pi-tui/render"; import { copyToClipboard } from "../../utils/clipboard"; @@ -1516,6 +1519,15 @@ export class MCPCommandController { const currentAuth = found.config.auth; const authStorage = this.ctx.session.modelRegistry.authStorage; + // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in signs out of that one alone: no + // other server's url-keyed sign-in goes with it, and its definition lives in no config file to rewrite. + if (isMcpOAuthServerCredentialId(currentAuth?.credentialId)) { + await removeManagedMcpOAuthCredential(authStorage, currentAuth?.credentialId); + await this.reloadServers(); + this.#showMessage(["", theme.fg("success", `- Cleared auth for "${name}"`), ""].join("\n")); + return; + } + // scient(SA-CODEX-PLUGINS): end if (currentAuth?.type === "oauth") { await removeManagedMcpOAuthCredential(authStorage, currentAuth.credentialId); } diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index ee7098828c..1c2084a1cc 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -659,10 +659,16 @@ async function readPluginSkill( try { const { frontmatter, body } = parseFrontmatter(content, { source: skillPath }); if (frontmatter.enabled === false) return null; - const folder = path.basename(path.dirname(skillPath)); - // Codex collapses a name's whitespace (`sanitize_single_line`), so rules match that name. - const raw = typeof frontmatter.name === "string" ? frontmatter.name.split(/\s+/).filter(Boolean).join(" ") : ""; - const name = raw || folder; + // Codex collapses the whitespace of a skill's name, or of the folder name standing in for it + // (`split_whitespace`, Unicode White_Space), so rules match that name. + const collapse = (value: string) => + value + .split(/\p{White_Space}+/u) + .filter(Boolean) + .join(" "); + const name = + (typeof frontmatter.name === "string" ? collapse(frontmatter.name) : "") || + collapse(path.basename(path.dirname(skillPath))); // `/` is reserved for skill namespaces and skill:// paths. if (/[\\/]/.test(name)) { warnings.push(`[codex-plugins] Skill name "${name}" contains a path separator, skipping: ${skillPath}`); @@ -1226,12 +1232,14 @@ async function loadMCPServers(ctx: LoadContext): Promise> items.push({ name, ...config, - // Its own sign-in, never another server's at the same URL: Codex keys sign-ins by name and URL. + // Its own sign-in, never another server's at the same URL: Codex keys sign-ins by name and URL. A + // server that names its own token (`bearer_token_env_var`) is never signed in with OAuth there, + // so it is bound as a key, which no stored sign-in answers. ...(config.transport === "http" && config.url && { auth: { ...config.auth, - type: "oauth" as const, + type: server.bearer_token_env_var != null ? ("apikey" as const) : ("oauth" as const), credentialId: mcpOAuthServerCredentialId(name, config.url), }, }), diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts index 655bcc871e..471465b910 100644 --- a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -1,18 +1,37 @@ import { Database } from "bun:sqlite"; -import { afterEach, describe, expect, it, vi } from "bun:test"; +import { afterEach, beforeAll, describe, expect, it, vi } from "bun:test"; import * as path from "node:path"; import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-ai"; import * as mcpClient from "@oh-my-pi/pi-coding-agent/mcp/client"; -import { lookupMcpOAuthCredentialForServer } from "@oh-my-pi/pi-coding-agent/mcp/oauth-credentials"; +import { + lookupMcpOAuthCredential, + lookupMcpOAuthCredentialForServer, +} from "@oh-my-pi/pi-coding-agent/mcp/oauth-credentials"; import * as oauthFlow from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow"; -import { reauthorizeMcpServer } from "@oh-my-pi/pi-coding-agent/mcp/oauth-reauth"; +import { + McpReauthRefusalError, + reauthorizeMcpServer, + reconnectMcpManagerServer, +} from "@oh-my-pi/pi-coding-agent/mcp/oauth-reauth"; import type { MCPServerConfig } from "@oh-my-pi/pi-coding-agent/mcp/types"; -import { TempDir } from "@oh-my-pi/pi-utils"; +import { MCPCommandController } from "@oh-my-pi/pi-coding-agent/modes/controllers/mcp-command-controller"; +import { initTheme } from "@oh-my-pi/pi-tui/theme"; +import { getProjectDir, setProjectDir, TempDir } from "@oh-my-pi/pi-utils"; +import { createInteractiveModeContext, createMcpManagerStub } from "../helpers/interactive-mode-context"; import { restoreEnvValue } from "../helpers/settings-test-state"; const SERVER_URL = "https://mcp.example.test/mcp"; const NAME = "tools@market:api"; const placeholder = "$" + "{CODEX_PLUGIN_OAUTH_TEST}"; +const token = { type: "oauth" as const, access: "a", refresh: "r", expires: Date.now() + 3_600_000 }; +const boundConfig = (): MCPServerConfig => ({ + type: "http", + url: SERVER_URL, + headerPolicy: "origin-locked", + auth: { type: "oauth", credentialId: oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL) }, +}); + +beforeAll(() => initTheme()); describe("an imported Codex plugin server's own sign-in", () => { const stores: AuthStorage[] = []; @@ -88,4 +107,75 @@ describe("an imported Codex plugin server's own sign-in", () => { expect(outcome).toMatchObject({ persisted: false, credentialId, config: { auth: { credentialId } } }); expect(await Bun.file(path.join(temp.path(), "mcp.json")).exists()).toBe(false); }); + + it("reconnects on its own sign-in, and never stands in for another entry at its URL", async () => { + const updateServerConfig = vi.fn(() => undefined); + const manager = createMcpManagerStub({ + getServerConfig: vi.fn(() => boundConfig()), + updateServerConfig, + reconnectServer: vi.fn(async () => null), + getConnectionStatus: vi.fn(() => "connected" as const), + }); + expect(await reconnectMcpManagerServer(manager, NAME, boundConfig())).toBe("connected"); + expect(updateServerConfig).toHaveBeenCalledWith(NAME, boundConfig()); + // A definition without that sign-in is another server, even at the same URL. + expect(await reconnectMcpManagerServer(manager, NAME, { type: "http", url: SERVER_URL })).toBeUndefined(); + }); + + it("signs out of its own sign-in alone", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + await storage.credentials.set(credentialId, token); + await storage.credentials.set(oauthFlow.mcpOAuthCredentialId(SERVER_URL), { ...token, access: "native" }); + using temp = TempDir.createSync("@scient-codex-plugins-unauth-"); + const previous = getProjectDir(); + setProjectDir(temp.path()); + try { + const ctx = createInteractiveModeContext({ + session: { modelRegistry: { authStorage: storage } }, + mcpManager: createMcpManagerStub({ + getServerConfig: vi.fn(() => boundConfig()), + getSource: vi.fn(() => ({ + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/codex/plugin/.mcp.json", + level: "user" as const, + })), + }), + }); + await new MCPCommandController(ctx).handle(`/mcp unauth ${NAME}`); + expect(ctx.showError).not.toHaveBeenCalled(); + } finally { + setProjectDir(previous); + } + expect(storage.credentials.get(credentialId)).toBeUndefined(); + // Another server's sign-in at the same URL stays. + expect(storage.credentials.get(oauthFlow.mcpOAuthCredentialId(SERVER_URL))).toMatchObject({ access: "native" }); + }); + + it("leaves a server's own token in charge, as Codex does", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + await storage.credentials.set(credentialId, token); + // A configured Authorization header wins over a stored sign-in. + expect( + lookupMcpOAuthCredential(storage, { + type: "http", + url: SERVER_URL, + headers: { Authorization: "Bearer service" }, + auth: { type: "oauth", credentialId }, + }), + ).toBeUndefined(); + // A server declaring bearer_token_env_var is bound as a key: no sign-in answers it, and none starts. + const keyed: MCPServerConfig = { ...boundConfig(), auth: { type: "apikey", credentialId } }; + expect(lookupMcpOAuthCredential(storage, keyed)).toBeUndefined(); + using temp = TempDir.createSync("@scient-codex-plugins-keyed-"); + await expect( + reauthorizeMcpServer( + NAME, + { filePath: path.join(temp.path(), "mcp.json"), scope: "user", config: keyed, discovered: true }, + { authStorage: storage, silent: true }, + ), + ).rejects.toBeInstanceOf(McpReauthRefusalError); + }); }); diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 9347e7a333..2d5fd2d4a5 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -181,6 +181,8 @@ describe("installed Codex plugins", () => { headers: { Authorization: "Bearer pat-value" }, headerPolicy: "origin-locked", transport: "http", + // It names its own token, so it is bound as a key: no OAuth sign-in replaces the bearer. + auth: { type: "apikey" }, }); // The plugin's config table turns this server off in Codex for good, so it is not listed. expect(byName.has("research@market:quiet")).toBe(false); @@ -469,6 +471,8 @@ describe("installed Codex plugins", () => { await writeSkill(blank, "find"); // Codex collapses the name's inner whitespace before a rule meets it. await writeSkill(blank, "Paper Search"); + // A skill without a frontmatter name takes its folder's, collapsed the same way. + await Bun.write(path.join(blank, "skills", "daily review", "SKILL.md"), "---\ndescription: d\n---\nRun.\n"); const configPath = temp.join("codex-home", "config.toml"); const rules = [ "[[skills.config]]", @@ -484,6 +488,9 @@ describe("installed Codex plugins", () => { "[[skills.config]]", 'name = "1.0.0:Paper Search"', "enabled = false", + "[[skills.config]]", + 'name = "1.0.0:daily review"', + "enabled = false", "", ]; await Bun.write(configPath, [await Bun.file(configPath).text(), ...rules].join("\n")); @@ -492,6 +499,7 @@ describe("installed Codex plugins", () => { expect(names).not.toContain("search"); expect(names).not.toContain("find"); expect(names.some(name => name.startsWith("Paper"))).toBe(false); + expect(names.some(name => name.startsWith("daily"))).toBe(false); }); it("loads no plugin from a [plugins] table Codex cannot read", async () => { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index a172a98747..3c57cc6654 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take a frontmatter name with its whitespace collapsed as Codex's parser does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or the name Codex gives the skill, :, the package folder's name standing in for a blank manifest name; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read), mcp/oauth-credentials.ts never falls back from it to the URL-keyed row, and reauthorization (mcp/oauth-reauth.ts) takes the server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or the name Codex gives the skill, :, the package folder's name standing in for a blank manifest name; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). mcp/oauth-credentials.ts never falls back from it to the URL-keyed row and lets a configured Authorization header win over it; a server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", @@ -537,6 +537,7 @@ "packages/coding-agent/src/mcp/oauth-reauth.ts", "packages/coding-agent/src/mcp/transports/stdio.ts", "packages/coding-agent/src/mcp/types.ts", + "packages/coding-agent/src/modes/controllers/mcp-command-controller.ts", "packages/coding-agent/CHANGELOG.md", "packages/utils/src/env.ts" ], From 97a0826fd185316cf5fa97dfc105c420467574fd Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 13:08:51 +0200 Subject: [PATCH 12/30] fix(codex-plugins): only the codex source binds a sign-in, required bearer, header order, Unicode selector trim, toggles Tenth Codex review of 084a5deef8: - mcp/config.ts drops a server-bound credential id from any definition but a codex-plugins one whose name and URL give it; lookup answers a bound id only for its own URL under the active profile; reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it. - A server whose bearer_token_env_var is unset or empty is skipped, as Codex refuses to start it. Headers are set in Codex's order (static, environment, bearer), each replacing one of the same name in any case. - [[skills.config]] name selectors are trimmed of Unicode whitespace. - /mcp enable and disable toggle an imported server through disabledServers. --- SCIENT.md | 2 +- packages/coding-agent/src/mcp/config.ts | 16 ++- .../coding-agent/src/mcp/oauth-credentials.ts | 9 ++ packages/coding-agent/src/mcp/oauth-flow.ts | 12 +++ packages/coding-agent/src/mcp/oauth-reauth.ts | 12 +++ .../controllers/mcp-command-controller.ts | 18 +++- .../coding-agent/src/scient/codex-plugins.ts | 26 +++-- .../test/scient/codex-plugins-oauth.test.ts | 101 +++++++++++++++++- .../test/scient/codex-plugins.test.ts | 17 ++- scripts/scient/divergences.json | 2 +- 10 files changed, 201 insertions(+), 14 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index 5981ed9e94..caec7f43ca 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. Servers are named `@:`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), never another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, and `/mcp unauth` removes only it. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which no other definition can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, and `/mcp unauth` removes only it. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/src/mcp/config.ts b/packages/coding-agent/src/mcp/config.ts index fe82b2d84d..7d803d0003 100644 --- a/packages/coding-agent/src/mcp/config.ts +++ b/packages/coding-agent/src/mcp/config.ts @@ -12,6 +12,9 @@ import { loadCapability } from "../discovery"; import { readMCPConfigFile } from "./config-writer"; import { builtinDropFor, type MCPDroppedServer, type MCPTransformContext } from "./transforms"; import type { MCPConfigFile, MCPServerConfig } from "./types"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import { isMcpOAuthServerCredentialId, mcpOAuthServerCredentialId } from "./oauth-flow"; +// scient(SA-CODEX-PLUGINS): end /** Options for loading MCP configs */ export interface LoadMCPConfigsOptions { @@ -53,7 +56,18 @@ function convertToLegacyConfig(server: MCPServer): MCPServerConfig { timeout: server.timeout, requestIdFormat: server.requestIdFormat, instructions: server.instructions, - auth: server.auth, + // scient(SA-CODEX-PLUGINS): begin - only the codex-plugins source binds a server to its own sign-in, + // and only under the id its name and URL give: any other definition naming one is not bound to it. + auth: + isMcpOAuthServerCredentialId(server.auth?.credentialId) && + !( + server._source.provider === "codex-plugins" && + server.url !== undefined && + server.auth?.credentialId === mcpOAuthServerCredentialId(server.name, server.url) + ) + ? { ...server.auth, type: server.auth?.type ?? "oauth", credentialId: undefined } + : server.auth, + // scient(SA-CODEX-PLUGINS): end oauth: server.oauth, }; diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index 378cd09951..09758d7ff4 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -8,6 +8,7 @@ import { isManagedMCPOAuthCredentialId, // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. isMcpOAuthServerCredentialId, + mcpOAuthServerCredentialIdMatchesUrl, // scient(SA-CODEX-PLUGINS): end type MCPStoredOAuthCredential, mcpOAuthCredentialId, @@ -48,6 +49,14 @@ export function lookupMcpOAuthCredentialForServer( if (!authStorage) return undefined; if (auth && auth.type !== "oauth") return undefined; const urlKeyedCredentialIds = mcpOAuthCredentialIdsForServerUrl(serverUrl); + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in answers only the URL it was made for. + if ( + isMcpOAuthServerCredentialId(auth?.credentialId) && + !mcpOAuthServerCredentialIdMatchesUrl(auth?.credentialId ?? "", serverUrl) + ) { + return undefined; + } + // scient(SA-CODEX-PLUGINS): end if ( auth?.credentialId && (!auth.credentialId.startsWith("mcp_oauth:profile:") || urlKeyedCredentialIds.includes(auth.credentialId)) diff --git a/packages/coding-agent/src/mcp/oauth-flow.ts b/packages/coding-agent/src/mcp/oauth-flow.ts index 2e2b15475b..79add9cc57 100644 --- a/packages/coding-agent/src/mcp/oauth-flow.ts +++ b/packages/coding-agent/src/mcp/oauth-flow.ts @@ -53,6 +53,18 @@ export function mcpOAuthServerCredentialId( export function isMcpOAuthServerCredentialId(credentialId: string | undefined): boolean { return credentialId?.startsWith(MCP_OAUTH_SERVER_CREDENTIAL_PREFIX) === true; } + +/** + * Whether a server's own sign-in id belongs to this server URL under the active profile: a + * definition naming another server's id, or one moved to another URL, gets nothing from it. + */ +export function mcpOAuthServerCredentialIdMatchesUrl(credentialId: string, serverUrl: string | undefined): boolean { + return ( + serverUrl !== undefined && + mcpOAuthCredentialProfile(credentialId) === (getActiveProfile() ?? "default") && + mcpOAuthServerUrlFromCredentialId(credentialId) === serverUrl + ); +} // scient(SA-CODEX-PLUGINS): end /** Whether a credential id was minted by OMP's MCP OAuth flows (either era). */ export function isManagedMCPOAuthCredentialId(credentialId: string | undefined): credentialId is string { diff --git a/packages/coding-agent/src/mcp/oauth-reauth.ts b/packages/coding-agent/src/mcp/oauth-reauth.ts index 16b066d942..4d8ea01665 100644 --- a/packages/coding-agent/src/mcp/oauth-reauth.ts +++ b/packages/coding-agent/src/mcp/oauth-reauth.ts @@ -38,6 +38,7 @@ import { mcpOAuthCredentialId, // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. isMcpOAuthServerCredentialId, + mcpOAuthServerCredentialId, // scient(SA-CODEX-PLUGINS): end } from "./oauth-flow"; import type { AuthStorage } from "../session/auth-storage"; @@ -772,6 +773,17 @@ export async function reauthorizeMcpServer( // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in (an imported Codex plugin server) // takes its values literally, as Codex does, and keeps its configured scope and resource below. const bound = isMcpOAuthServerCredentialId(currentAuth?.credentialId); + // Only a discovered server holds its own sign-in, under the id its name and URL give. + if ( + bound && + !( + found.discovered && + (found.config.type === "http" || found.config.type === "sse") && + currentAuth?.credentialId === mcpOAuthServerCredentialId(name, found.config.url) + ) + ) { + throw new McpReauthRefusalError(`Server "${name}" names a sign-in that belongs to another server.`); + } const runtimeBaseConfig = bound ? baseConfig : expandEnvVarsDeep(baseConfig); // Codex never signs in with OAuth to a server that names its own token (`bearer_token_env_var`). if (bound && currentAuth?.type !== "oauth") { diff --git a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts index 4d533ab3dc..2fdc6f65d3 100644 --- a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts +++ b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts @@ -49,7 +49,7 @@ import { } from "../../mcp/oauth-credentials"; import type { MCPAuthChallenge, MCPConfigFile, MCPServerConfig, MCPServerConnection } from "../../mcp/types"; // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. -import { isMcpOAuthServerCredentialId } from "../../mcp/oauth-flow"; +import { isMcpOAuthServerCredentialId, mcpOAuthServerCredentialId } from "../../mcp/oauth-flow"; // scient(SA-CODEX-PLUGINS): end import { shortenPath } from "@oh-my-pi/pi-tui/render/render-utils"; import { urlHyperlinkAlways } from "@oh-my-pi/pi-tui/render"; @@ -1408,7 +1408,12 @@ export class MCPCommandController { const found = await findMcpServerForAuth(name, getProjectDir(), this.ctx.mcpManager, { enableProjectConfig: cfgMcpEnableProjectConfig.get(this.ctx.settings), }); - if (!found) { + // scient(SA-CODEX-PLUGINS): begin - an imported Codex plugin server lives in no config file: its + // toggle is the disabledServers list, as for any third-party discovered server. + const imported = + found?.discovered === true && this.ctx.mcpManager?.getSource(name)?.provider === "codex-plugins"; + if (!found || imported) { + // scient(SA-CODEX-PLUGINS): end // Check if this is a discovered server from a third-party config const userConfigPath = getMCPConfigPath("user", getProjectDir()); const disabledServers = new Set(await readDisabledServers(userConfigPath)); @@ -1522,6 +1527,15 @@ export class MCPCommandController { // scient(SA-CODEX-PLUGINS): begin - a server bound to its own sign-in signs out of that one alone: no // other server's url-keyed sign-in goes with it, and its definition lives in no config file to rewrite. if (isMcpOAuthServerCredentialId(currentAuth?.credentialId)) { + // Only a discovered server holds its own sign-in, under the id its name and URL give. + const own = + found.discovered && + (found.config.type === "http" || found.config.type === "sse") && + currentAuth?.credentialId === mcpOAuthServerCredentialId(name, found.config.url); + if (!own) { + this.ctx.showError(`Server "${name}" names a sign-in that belongs to another server.`); + return; + } await removeManagedMcpOAuthCredential(authStorage, currentAuth?.credentialId); await this.reloadServers(); this.#showMessage(["", theme.fg("success", `- Cleared auth for "${name}"`), ""].join("\n")); diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 1c2084a1cc..7c36cefed6 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -469,7 +469,9 @@ async function readSkillRules(user: Record, warnings: string[]) } const rules: SkillRule[] = []; for (const entry of entries as Record[]) { - const name = typeof entry.name === "string" ? entry.name.trim() : undefined; + // Codex trims a name selector of Unicode whitespace (`str::trim`). + const name = + typeof entry.name === "string" ? entry.name.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "") : undefined; if ((entry.path == null) === (entry.name == null) || name === "") continue; const selector = name !== undefined @@ -1010,14 +1012,26 @@ async function mapLegacyServer( if (server.auth != null && server.auth !== "oauth") return { error: `auth ${String(server.auth)} is not supported` }; const url = server.url as string; if (!URL.canParse(url) || !/^https?:$/.test(new URL(url).protocol)) return { error: `invalid url ${url}` }; - // Static headers are data; the others name variables of the launch environment, as in Codex. - const headers: Record = { ...(server.http_headers as Record | undefined) }; + // Static headers are data; the others name variables of the launch environment, as in Codex, which + // sets them in that order, each replacing a header of the same name in any case. A named bearer + // variable that is unset or empty keeps Codex from starting the server. + const headers: Record = {}; + const put = (header: string, value: string) => { + for (const key of Object.keys(headers)) if (key.toLowerCase() === header.toLowerCase()) delete headers[key]; + headers[header] = value; + }; + for (const [header, value] of Object.entries((server.http_headers ?? {}) as Record)) + put(header, value); for (const [header, name] of Object.entries((server.env_http_headers ?? {}) as Record)) { const value = envValue(env, name)?.trim(); - if (value) headers[header] = value; + if (value) put(header, value); + } + if (server.bearer_token_env_var != null) { + const bearer = envValue(env, server.bearer_token_env_var as string); + if (!bearer) + return { error: `its bearer_token_env_var ${String(server.bearer_token_env_var)} is unset or empty` }; + put("Authorization", `Bearer ${bearer}`); } - const bearer = server.bearer_token_env_var ? envValue(env, server.bearer_token_env_var as string)?.trim() : ""; - if (bearer) headers.Authorization = `Bearer ${bearer}`; // Codex's OAuth client settings, snake_case or the camelCase plugins may use. const oauthTable = (server.oauth ?? {}) as Record; const oauth: NonNullable = {}; diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts index 471465b910..d2eb7484bc 100644 --- a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -2,7 +2,11 @@ import { Database } from "bun:sqlite"; import { afterEach, beforeAll, describe, expect, it, vi } from "bun:test"; import * as path from "node:path"; import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-ai"; +import { initializeWithSettings } from "@oh-my-pi/pi-coding-agent/capability"; +import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; import * as mcpClient from "@oh-my-pi/pi-coding-agent/mcp/client"; +import { loadAllMCPConfigs } from "@oh-my-pi/pi-coding-agent/mcp/config"; +import { readDisabledServers } from "@oh-my-pi/pi-coding-agent/mcp/config-writer"; import { lookupMcpOAuthCredential, lookupMcpOAuthCredentialForServer, @@ -16,7 +20,7 @@ import { import type { MCPServerConfig } from "@oh-my-pi/pi-coding-agent/mcp/types"; import { MCPCommandController } from "@oh-my-pi/pi-coding-agent/modes/controllers/mcp-command-controller"; import { initTheme } from "@oh-my-pi/pi-tui/theme"; -import { getProjectDir, setProjectDir, TempDir } from "@oh-my-pi/pi-utils"; +import { getAgentDir, getMCPConfigPath, getProjectDir, setAgentDir, setProjectDir, TempDir } from "@oh-my-pi/pi-utils"; import { createInteractiveModeContext, createMcpManagerStub } from "../helpers/interactive-mode-context"; import { restoreEnvValue } from "../helpers/settings-test-state"; @@ -178,4 +182,99 @@ describe("an imported Codex plugin server's own sign-in", () => { ), ).rejects.toBeInstanceOf(McpReauthRefusalError); }); + + it("gives a sign-in to no definition but its own server's", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + await storage.credentials.set(credentialId, token); + // Repointed to another URL, the id answers nothing. + expect( + lookupMcpOAuthCredentialForServer(storage, { type: "oauth", credentialId }, "https://other.example.test/mcp"), + ).toBeUndefined(); + using temp = TempDir.createSync("@scient-codex-plugins-claim-"); + const filePath = path.join(temp.path(), "mcp.json"); + // A config-file definition, or one under another name, cannot sign in under it. + for (const [name, discovered] of [ + [NAME, false], + ["copy", true], + ] as const) { + await expect( + reauthorizeMcpServer( + name, + { filePath, scope: "user", config: boundConfig(), discovered }, + { authStorage: storage, silent: true }, + ), + ).rejects.toBeInstanceOf(McpReauthRefusalError); + } + // Nor sign it out. + const previous = getProjectDir(); + setProjectDir(temp.path()); + try { + const ctx = createInteractiveModeContext({ + session: { modelRegistry: { authStorage: storage } }, + mcpManager: createMcpManagerStub({ + getServerConfig: vi.fn(() => boundConfig()), + getSource: vi.fn(() => ({ + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/codex/plugin/.mcp.json", + level: "user" as const, + })), + }), + }); + await new MCPCommandController(ctx).handle("/mcp unauth copy"); + expect(ctx.showError).toHaveBeenCalledWith(expect.stringContaining("belongs to another server")); + } finally { + setProjectDir(previous); + } + expect(storage.credentials.get(credentialId)).toMatchObject({ access: "a" }); + // Only the codex-plugins source binds a definition to a server's own sign-in, even under the id + // the definition's own name and URL give. + const ownId = oauthFlow.mcpOAuthServerCredentialId("imposter", SERVER_URL); + await Bun.write( + path.join(temp.path(), ".mcp.json"), + JSON.stringify({ + mcpServers: { + copy: { type: "http", url: SERVER_URL, auth: { type: "oauth", credentialId } }, + imposter: { type: "http", url: SERVER_URL, auth: { type: "oauth", credentialId: ownId } }, + }, + }), + ); + const release = initializeWithSettings(Settings.isolated()); + try { + const { configs } = await loadAllMCPConfigs(temp.path()); + expect(configs.copy?.auth?.credentialId).toBeUndefined(); + expect(configs.imposter?.auth?.credentialId).toBeUndefined(); + } finally { + release(); + } + }); + + it("toggles an imported server through the disabled-servers list", async () => { + using temp = TempDir.createSync("@scient-codex-plugins-toggle-"); + const previousProject = getProjectDir(); + const previousAgent = getAgentDir(); + setProjectDir(temp.path()); + setAgentDir(temp.join("agent")); + try { + const ctx = createInteractiveModeContext({ + session: { modelRegistry: { authStorage: freshStorage() } }, + mcpManager: createMcpManagerStub({ + getServerConfig: vi.fn(() => boundConfig()), + getSource: vi.fn(() => ({ + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/codex/plugin/.mcp.json", + level: "user" as const, + })), + }), + }); + await new MCPCommandController(ctx).handle(`/mcp disable ${NAME}`); + expect(ctx.showError).not.toHaveBeenCalled(); + expect(await readDisabledServers(getMCPConfigPath("user", temp.path()))).toEqual([NAME]); + } finally { + setProjectDir(previousProject); + setAgentDir(previousAgent); + } + }); }); diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 2d5fd2d4a5..369ec0f70b 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -249,9 +249,21 @@ describe("installed Codex plugins", () => { huge: { command: "node", args: ["huge.mjs"], startup_timeout_ms: 2 ** 64 }, capped: { command: "node", args: ["capped.mjs"], tools: { read: { output_token_limit: 100 } } }, shown: { command: "node", args: ["shown.mjs"], omit_tools_from: [] }, + // Codex refuses to start a server whose named bearer variable is unset. + unsetbearer: { url: "https://mcp.example.test/a", bearer_token_env_var: "CODEX_PLUGIN_TEST_UNSET" }, + // The bearer replaces a static header of the same name in any case. + cased: { + url: "https://mcp.example.test/b", + http_headers: { authorization: "Basic old" }, + bearer_token_env_var: "CODEX_PLUGIN_TEST_PAT", + }, }); const { names, warnings } = await limits(); - expect(names).toEqual(["limits@market:shown"]); + expect(names.sort()).toEqual(["limits@market:cased", "limits@market:shown"]); + expect((await load()).byName.get("limits@market:cased")?.headers).toEqual({ Authorization: "Bearer pat-value" }); + expect(warnings).toContainEqual( + expect.stringContaining('"unsetbearer" of limits@market: its bearer_token_env_var CODEX_PLUGIN_TEST_UNSET'), + ); expect(warnings).toContainEqual( expect.stringContaining('"hidden" of limits@market: Codex restricts its tools (omit_tools_from)'), ); @@ -476,7 +488,8 @@ describe("installed Codex plugins", () => { const configPath = temp.join("codex-home", "config.toml"); const rules = [ "[[skills.config]]", - 'name = "twin:search"', + // Codex trims a selector's Unicode whitespace, U+0085 included. + 'name = "\\u0085twin:search\\u0085"', "enabled = false", // Codex names no plugin skill bare `search`, so this enables nothing. "[[skills.config]]", diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 3c57cc6654..5cdff71e06 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or the name Codex gives the skill, :, the package folder's name standing in for a blank manifest name; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); HTTP servers carry literal, origin-locked headers, bearer_token_env_var and env_http_headers values, and their OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). mcp/oauth-credentials.ts never falls back from it to the URL-keyed row and lets a configured Authorization header win over it; a server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a blank manifest name; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case (http_headers, env_http_headers values, then the bearer_token_env_var value, which must be set and non-empty or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: mcp/config.ts drops a server-bound id from any other definition and from one whose name and URL do not give it, mcp/oauth-credentials.ts answers it only for its own URL under the active profile, never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it, and reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", From 92cf28ef24df104c612cc6bec0c2a7edadf3455a Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 13:20:47 +0200 Subject: [PATCH 13/30] fix(codex-plugins): trust server-bound sign-ins on every connection path, tolerate malformed ids, Codex header and namespace rules Eleventh Codex review of 97a0826fd1: - trustedMcpAuth / withTrustedMcpAuth (mcp/oauth-flow.ts) keep a server-bound credential id only on the server the codex-plugins source imported under it; applied in config conversion, at every MCPManager connection, and in /mcp test (TUI and ACP). - isMcpOAuthServerCredentialId tolerates non-string ids, so a malformed native credentialId no longer aborts MCP discovery. - Headers follow Codex: names must be HTTP tokens and values tab or visible ASCII, else skipped; an environment value is used as set unless only whitespace; an invalid bearer skips the server. - A manifest name of Unicode whitespace alone is blank, so the package folder names its skills. --- packages/coding-agent/src/mcp/config.ts | 12 +----- packages/coding-agent/src/mcp/manager.ts | 11 ++++- packages/coding-agent/src/mcp/oauth-flow.ts | 40 ++++++++++++++++++- .../controllers/mcp-command-controller.ts | 9 +++-- .../coding-agent/src/scient/codex-plugins.ts | 28 ++++++++----- .../src/slash-commands/helpers/mcp.ts | 9 ++++- .../test/scient/codex-plugins-oauth.test.ts | 7 ++++ .../test/scient/codex-plugins.test.ts | 21 +++++++++- scripts/scient/divergences.json | 4 +- 9 files changed, 112 insertions(+), 29 deletions(-) diff --git a/packages/coding-agent/src/mcp/config.ts b/packages/coding-agent/src/mcp/config.ts index 7d803d0003..8989df603c 100644 --- a/packages/coding-agent/src/mcp/config.ts +++ b/packages/coding-agent/src/mcp/config.ts @@ -13,7 +13,7 @@ import { readMCPConfigFile } from "./config-writer"; import { builtinDropFor, type MCPDroppedServer, type MCPTransformContext } from "./transforms"; import type { MCPConfigFile, MCPServerConfig } from "./types"; // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. -import { isMcpOAuthServerCredentialId, mcpOAuthServerCredentialId } from "./oauth-flow"; +import { trustedMcpAuth } from "./oauth-flow"; // scient(SA-CODEX-PLUGINS): end /** Options for loading MCP configs */ @@ -58,15 +58,7 @@ function convertToLegacyConfig(server: MCPServer): MCPServerConfig { instructions: server.instructions, // scient(SA-CODEX-PLUGINS): begin - only the codex-plugins source binds a server to its own sign-in, // and only under the id its name and URL give: any other definition naming one is not bound to it. - auth: - isMcpOAuthServerCredentialId(server.auth?.credentialId) && - !( - server._source.provider === "codex-plugins" && - server.url !== undefined && - server.auth?.credentialId === mcpOAuthServerCredentialId(server.name, server.url) - ) - ? { ...server.auth, type: server.auth?.type ?? "oauth", credentialId: undefined } - : server.auth, + auth: trustedMcpAuth(server.name, server.auth, server.url, server._source.provider), // scient(SA-CODEX-PLUGINS): end oauth: server.oauth, }; diff --git a/packages/coding-agent/src/mcp/manager.ts b/packages/coding-agent/src/mcp/manager.ts index ae1db767ec..80a513c7a2 100644 --- a/packages/coding-agent/src/mcp/manager.ts +++ b/packages/coding-agent/src/mcp/manager.ts @@ -40,6 +40,9 @@ import { refreshStoredManagedMcpOAuthCredential, } from "./oauth-credentials"; import type { MCPStoredOAuthCredential } from "./oauth-flow"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import { withTrustedMcpAuth } from "./oauth-flow"; +// scient(SA-CODEX-PLUGINS): end import type { McpConnectionStatusEvent } from "./startup-events"; import { resolveMCPStartupTimeoutMs } from "./timeout"; @@ -972,7 +975,10 @@ export class MCPManager implements MCPManagerApi { // Prepare connection tasks const connectionTasks: ConnectionTask[] = []; - for (const [name, config] of Object.entries(configs)) { + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it. + for (const [name, declared] of Object.entries(configs)) { + const config = withTrustedMcpAuth(name, declared, (sources[name] ?? this.#sources.get(name))?.provider); + // scient(SA-CODEX-PLUGINS): end this.#startupServers.add(name); // Connecting a server by name (`/mcp enable`) ends its built-in drop. this.#builtinDrops.delete(name); @@ -1976,6 +1982,9 @@ export class MCPManager implements MCPManagerApi { source: SourceMeta | undefined, reconnectEpoch: number, ): Promise { + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it. + config = withTrustedMcpAuth(name, config, source?.provider); + // scient(SA-CODEX-PLUGINS): end // Capture before resolving credentials: a disconnectAll during that wait must abort this handshake too. const connectSignal = this.#connectAbort.signal; const resolvedConfig = await this.#resolveAuthConfig(config, { signal: connectSignal }); diff --git a/packages/coding-agent/src/mcp/oauth-flow.ts b/packages/coding-agent/src/mcp/oauth-flow.ts index 79add9cc57..076ef1c363 100644 --- a/packages/coding-agent/src/mcp/oauth-flow.ts +++ b/packages/coding-agent/src/mcp/oauth-flow.ts @@ -12,6 +12,9 @@ import type { FetchImpl } from "@oh-my-pi/pi-ai/types"; import { getActiveProfile } from "@oh-my-pi/pi-utils/dirs"; import type { OAuthCredential } from "../session/auth-storage"; import { buildWellKnownUrls } from "./oauth-discovery"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import type { MCPAuthConfig, MCPServerConfig } from "./types"; +// scient(SA-CODEX-PLUGINS): end /** Credential-id prefix for OMP-managed MCP OAuth credentials keyed by profile and server URL. */ const MCP_OAUTH_URL_CREDENTIAL_PREFIX = "mcp_oauth:"; @@ -50,8 +53,41 @@ export function mcpOAuthServerCredentialId( } /** Whether a credential id is one server's own sign-in ({@link mcpOAuthServerCredentialId}). */ -export function isMcpOAuthServerCredentialId(credentialId: string | undefined): boolean { - return credentialId?.startsWith(MCP_OAUTH_SERVER_CREDENTIAL_PREFIX) === true; +export function isMcpOAuthServerCredentialId(credentialId: unknown): boolean { + return typeof credentialId === "string" && credentialId.startsWith(MCP_OAUTH_SERVER_CREDENTIAL_PREFIX); +} + +/** + * The auth block a server definition may use. A server-bound credential id stays only on the + * server the codex-plugins source imported under it (the id its name and URL give); any other + * definition drops it and finds its sign-in as a native server does. + */ +export function trustedMcpAuth( + name: string, + auth: MCPAuthConfig | undefined, + url: string | undefined, + provider: string | undefined, +): MCPAuthConfig | undefined { + if (!auth || !isMcpOAuthServerCredentialId(auth.credentialId)) return auth; + if ( + provider === "codex-plugins" && + url !== undefined && + auth.credentialId === mcpOAuthServerCredentialId(name, url) + ) { + return auth; + } + return { ...auth, credentialId: undefined }; +} + +/** {@link trustedMcpAuth} applied to a server definition from the source `provider`. */ +export function withTrustedMcpAuth( + name: string, + config: MCPServerConfig, + provider: string | undefined, +): MCPServerConfig { + const url = config.type === "http" || config.type === "sse" ? config.url : undefined; + const auth = trustedMcpAuth(name, config.auth, url, provider); + return auth === config.auth ? config : { ...config, auth }; } /** diff --git a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts index 2fdc6f65d3..29b9490e28 100644 --- a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts +++ b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts @@ -49,7 +49,7 @@ import { } from "../../mcp/oauth-credentials"; import type { MCPAuthChallenge, MCPConfigFile, MCPServerConfig, MCPServerConnection } from "../../mcp/types"; // scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. -import { isMcpOAuthServerCredentialId, mcpOAuthServerCredentialId } from "../../mcp/oauth-flow"; +import { isMcpOAuthServerCredentialId, mcpOAuthServerCredentialId, withTrustedMcpAuth } from "../../mcp/oauth-flow"; // scient(SA-CODEX-PLUGINS): end import { shortenPath } from "@oh-my-pi/pi-tui/render/render-utils"; import { urlHyperlinkAlways } from "@oh-my-pi/pi-tui/render"; @@ -1307,13 +1307,16 @@ export class MCPCommandController { // Resolve auth config if needed let resolvedConfig: MCPServerConfig; + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it. + const testConfig = withTrustedMcpAuth(name, config, this.ctx.mcpManager?.getSource(name)?.provider); if (this.ctx.mcpManager) { - resolvedConfig = await this.ctx.mcpManager.prepareConfig(config); + resolvedConfig = await this.ctx.mcpManager.prepareConfig(testConfig); } else { const tempManager = new MCPManager(getProjectDir()); tempManager.setAuthStorage(this.ctx.session.modelRegistry.authStorage); - resolvedConfig = await tempManager.prepareConfig(config); + resolvedConfig = await tempManager.prepareConfig(testConfig); } + // scient(SA-CODEX-PLUGINS): end // Create temporary connection connection = await connectToServer(name, resolvedConfig, { signal: abortController.signal }); diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 7c36cefed6..642e4f96cc 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -135,6 +135,9 @@ const NOT_STDIO = [ "auth", ]; const NOT_HTTP = ["args", "env", "env_vars", "cwd", "bearer_token"]; +/** Header names and values the `http` crate Codex uses accepts: an RFC 9110 token, and tab or visible ASCII. */ +const HTTP_TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; +const HTTP_HEADER_VALUE = /^[\t\x20-\x7e]*$/; /** Codex's skill scan limits for a plugin's skill folder (codex-rs/ext/skills loader). */ const MAX_SKILL_SCAN_DEPTH = 6; @@ -760,7 +763,7 @@ async function loadSkills(ctx: LoadContext): Promise> { // Codex names a plugin's skills `:`, the package folder's name standing // in for a blank manifest name. const raw = typeof plugin.manifest.name === "string" ? plugin.manifest.name : ""; - const namespace = raw.trim() ? raw : path.basename(plugin.root); + const namespace = raw.replace(/\p{White_Space}+/gu, "") ? raw : path.basename(plugin.root); for (const dir of dirs) { for (const skill of await scanPluginSkills(plugin, dir, warnings)) found.push({ skill, namespace }); } @@ -1013,24 +1016,31 @@ async function mapLegacyServer( const url = server.url as string; if (!URL.canParse(url) || !/^https?:$/.test(new URL(url).protocol)) return { error: `invalid url ${url}` }; // Static headers are data; the others name variables of the launch environment, as in Codex, which - // sets them in that order, each replacing a header of the same name in any case. A named bearer - // variable that is unset or empty keeps Codex from starting the server. + // sets them in that order, each replacing a header of the same name in any case, and skips a header + // whose name or value HTTP does not allow, or an environment value that is only whitespace (used + // as is otherwise). A named bearer variable that is unset or empty, or a bearer HTTP does not + // allow, keeps Codex from starting the server. const headers: Record = {}; const put = (header: string, value: string) => { + if (!HTTP_TOKEN.test(header) || !HTTP_HEADER_VALUE.test(value)) return false; for (const key of Object.keys(headers)) if (key.toLowerCase() === header.toLowerCase()) delete headers[key]; headers[header] = value; + return true; }; - for (const [header, value] of Object.entries((server.http_headers ?? {}) as Record)) + for (const [header, value] of Object.entries((server.http_headers ?? {}) as Record)) { put(header, value); + } for (const [header, name] of Object.entries((server.env_http_headers ?? {}) as Record)) { - const value = envValue(env, name)?.trim(); - if (value) put(header, value); + const value = envValue(env, name); + if (value?.replace(/\p{White_Space}+/gu, "")) put(header, value); } if (server.bearer_token_env_var != null) { const bearer = envValue(env, server.bearer_token_env_var as string); - if (!bearer) - return { error: `its bearer_token_env_var ${String(server.bearer_token_env_var)} is unset or empty` }; - put("Authorization", `Bearer ${bearer}`); + if (!bearer || !put("Authorization", `Bearer ${bearer}`)) { + return { + error: `its bearer_token_env_var ${String(server.bearer_token_env_var)} is unset, empty or not a header value`, + }; + } } // Codex's OAuth client settings, snake_case or the camelCase plugins may use. const oauthTable = (server.oauth ?? {}) as Record; diff --git a/packages/coding-agent/src/slash-commands/helpers/mcp.ts b/packages/coding-agent/src/slash-commands/helpers/mcp.ts index cfd978dcd6..7b633a03e7 100644 --- a/packages/coding-agent/src/slash-commands/helpers/mcp.ts +++ b/packages/coding-agent/src/slash-commands/helpers/mcp.ts @@ -17,6 +17,9 @@ import { reauthorizeMcpServer, } from "../../mcp/oauth-reauth"; import type { MCPServerConfig, MCPServerConnection } from "../../mcp/types"; +// scient(SA-CODEX-PLUGINS): begin - server-bound sign-ins. +import { withTrustedMcpAuth } from "../../mcp/oauth-flow"; +// scient(SA-CODEX-PLUGINS): end import { parseCommandArgs } from "../../utils/command-args"; import { openPath } from "../../utils/open"; import type { ParsedSlashCommand, SlashCommandResult, SlashCommandRuntime } from "../types"; @@ -150,7 +153,11 @@ async function withPreparedMcpConnection( // Without this, `/mcp test|resources|prompts` silently fails for any // server saved by the TUI/reauth path. manager.setAuthStorage(runtime.session.modelRegistry.authStorage); - const resolvedConfig = await manager.prepareConfig(config); + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it. + const resolvedConfig = await manager.prepareConfig( + withTrustedMcpAuth(name, config, runtime.mcpManager?.getSource(name)?.provider), + ); + // scient(SA-CODEX-PLUGINS): end connection = await connectToServer(name, resolvedConfig); return await fn(connection); } finally { diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts index d2eb7484bc..50f8bcd837 100644 --- a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -237,6 +237,8 @@ describe("an imported Codex plugin server's own sign-in", () => { mcpServers: { copy: { type: "http", url: SERVER_URL, auth: { type: "oauth", credentialId } }, imposter: { type: "http", url: SERVER_URL, auth: { type: "oauth", credentialId: ownId } }, + // A malformed id belongs to no one; it must not stop its siblings from loading. + numeric: { type: "http", url: SERVER_URL, auth: { type: "apikey", credentialId: 5 } }, }, }), ); @@ -245,9 +247,14 @@ describe("an imported Codex plugin server's own sign-in", () => { const { configs } = await loadAllMCPConfigs(temp.path()); expect(configs.copy?.auth?.credentialId).toBeUndefined(); expect(configs.imposter?.auth?.credentialId).toBeUndefined(); + expect(configs.numeric).toMatchObject({ type: "http", url: SERVER_URL }); } finally { release(); } + // Manual connections (/mcp test) apply the same rule to the definition they read. + expect(oauthFlow.withTrustedMcpAuth(NAME, boundConfig(), "codex-plugins").auth?.credentialId).toBe(credentialId); + expect(oauthFlow.withTrustedMcpAuth(NAME, boundConfig(), "mcp-json").auth?.credentialId).toBeUndefined(); + expect(oauthFlow.withTrustedMcpAuth("copy", boundConfig(), "codex-plugins").auth?.credentialId).toBeUndefined(); }); it("toggles an imported server through the disabled-servers list", async () => { diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 369ec0f70b..cc2f5396d4 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -19,6 +19,7 @@ const ENV_KEYS = [ "CODEX_PLUGIN_TEST_TOKEN", "CODEX_PLUGIN_TEST_PAT", "CODEX_PLUGIN_TEST_SECRET", + "CODEX_PLUGIN_TEST_NEWLINE", ]; async function writeSkill(root: string, name: string): Promise { @@ -242,6 +243,8 @@ describe("installed Codex plugins", () => { }); it("skips a server Codex hides from the model or would not start", async () => { + // A value HTTP does not allow is skipped, so it cannot displace the static header Codex keeps. + restoreEnvValue("CODEX_PLUGIN_TEST_NEWLINE", "account-b\n"); await installLimits({ hidden: { command: "node", args: ["hidden.mjs"], omit_tools_from: ["direct", "deferred", "code_mode"] }, negative: { command: "node", args: ["negative.mjs"], startup_timeout_sec: -1 }, @@ -257,10 +260,17 @@ describe("installed Codex plugins", () => { http_headers: { authorization: "Basic old" }, bearer_token_env_var: "CODEX_PLUGIN_TEST_PAT", }, + kept: { + url: "https://mcp.example.test/c", + http_headers: { "X-Account": "account-a", "Bad Name": "x" }, + env_http_headers: { "X-Account": "CODEX_PLUGIN_TEST_NEWLINE" }, + }, }); const { names, warnings } = await limits(); - expect(names.sort()).toEqual(["limits@market:cased", "limits@market:shown"]); - expect((await load()).byName.get("limits@market:cased")?.headers).toEqual({ Authorization: "Bearer pat-value" }); + expect(names.sort()).toEqual(["limits@market:cased", "limits@market:kept", "limits@market:shown"]); + const byName = (await load()).byName; + expect(byName.get("limits@market:cased")?.headers).toEqual({ Authorization: "Bearer pat-value" }); + expect(byName.get("limits@market:kept")?.headers).toEqual({ "X-Account": "account-a" }); expect(warnings).toContainEqual( expect.stringContaining('"unsetbearer" of limits@market: its bearer_token_env_var CODEX_PLUGIN_TEST_UNSET'), ); @@ -481,6 +491,9 @@ describe("installed Codex plugins", () => { // A blank manifest name leaves Codex the package folder's name, the version. const blank = await install("blank@market", { ".codex-plugin/plugin.json": { name: " " } }); await writeSkill(blank, "find"); + // A manifest name of Unicode whitespace alone is blank to Codex too. + const nel = await install("nel@market", { ".codex-plugin/plugin.json": { name: "\u0085" } }); + await writeSkill(nel, "nel-skill"); // Codex collapses the name's inner whitespace before a rule meets it. await writeSkill(blank, "Paper Search"); // A skill without a frontmatter name takes its folder's, collapsed the same way. @@ -504,6 +517,9 @@ describe("installed Codex plugins", () => { "[[skills.config]]", 'name = "1.0.0:daily review"', "enabled = false", + "[[skills.config]]", + 'name = "1.0.0:nel-skill"', + "enabled = false", "", ]; await Bun.write(configPath, [await Bun.file(configPath).text(), ...rules].join("\n")); @@ -513,6 +529,7 @@ describe("installed Codex plugins", () => { expect(names).not.toContain("find"); expect(names.some(name => name.startsWith("Paper"))).toBe(false); expect(names.some(name => name.startsWith("daily"))).toBe(false); + expect(names).not.toContain("nel-skill"); }); it("loads no plugin from a [plugins] table Codex cannot read", async () => { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 5cdff71e06..234fe93973 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a blank manifest name; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case (http_headers, env_http_headers values, then the bearer_token_env_var value, which must be set and non-empty or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: mcp/config.ts drops a server-bound id from any other definition and from one whose name and URL do not give it, mcp/oauth-credentials.ts answers it only for its own URL under the active profile, never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it, and reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts) and in /mcp test (the TUI controller and slash-commands/helpers/mcp.ts); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile, never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it, and reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", @@ -532,12 +532,14 @@ "packages/coding-agent/src/capability/mcp.ts", "packages/coding-agent/src/discovery/index.ts", "packages/coding-agent/src/mcp/config.ts", + "packages/coding-agent/src/mcp/manager.ts", "packages/coding-agent/src/mcp/oauth-credentials.ts", "packages/coding-agent/src/mcp/oauth-flow.ts", "packages/coding-agent/src/mcp/oauth-reauth.ts", "packages/coding-agent/src/mcp/transports/stdio.ts", "packages/coding-agent/src/mcp/types.ts", "packages/coding-agent/src/modes/controllers/mcp-command-controller.ts", + "packages/coding-agent/src/slash-commands/helpers/mcp.ts", "packages/coding-agent/CHANGELOG.md", "packages/utils/src/env.ts" ], From b069eaf70a641ff290c5488e385db81ab43c1408 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 13:34:07 +0200 Subject: [PATCH 14/30] fix(codex-plugins): selected-definition provenance, client-bound sign-ins, tool timeout cap - /mcp test binds only the selected definition's own source; config-file helpers never bind; a live imported server's sign-in challenge is answered for that live definition. - A server-bound sign-in made with another OAuth client than the configured one is not used. - tool_timeout_sec is a hard cap on tool calls (toolTimeoutMs) that call arguments never extend; tool_timeout_sec = 0 skips the server. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- packages/coding-agent/src/capability/mcp.ts | 4 ++ packages/coding-agent/src/mcp/config.ts | 1 + .../coding-agent/src/mcp/oauth-credentials.ts | 15 +++++- packages/coding-agent/src/mcp/timeout.ts | 12 +++++ .../coding-agent/src/mcp/transports/http.ts | 9 +++- .../coding-agent/src/mcp/transports/sse.ts | 9 +++- .../coding-agent/src/mcp/transports/stdio.ts | 9 +++- packages/coding-agent/src/mcp/types.ts | 7 +++ .../controllers/mcp-command-controller.ts | 29 ++++++++-- .../coding-agent/src/scient/codex-plugins.ts | 6 ++- .../src/slash-commands/helpers/mcp.ts | 7 ++- .../test/scient/codex-plugins-oauth.test.ts | 53 +++++++++++++++++++ .../test/scient/codex-plugins.test.ts | 19 ++++++- scripts/scient/divergences.json | 5 +- 16 files changed, 170 insertions(+), 19 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index caec7f43ca..e701969639 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), and plugin values taken literally. Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which no other definition can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, and `/mcp unauth` removes only it. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec` whatever the call asks to wait (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which no other definition can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made with another OAuth client than the one the plugin now configures is not used, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 8a396b7a38..2d5bd58d9e 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures; one that names its own token keeps it and is never signed in with OAuth. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, tool calls held to the plugin's `tool_timeout_sec`, and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures and signing in again when the plugin changes its OAuth client; one that names its own token keeps it and is never signed in with OAuth. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/capability/mcp.ts b/packages/coding-agent/src/capability/mcp.ts index 2a0528860a..f381618538 100644 --- a/packages/coding-agent/src/capability/mcp.ts +++ b/packages/coding-agent/src/capability/mcp.ts @@ -19,6 +19,10 @@ export interface MCPServer { enabled?: boolean; /** Connection timeout in milliseconds */ timeout?: number; + // scient(SA-CODEX-PLUGINS): begin - a hard cap on tool calls. + /** Hard limit on a tool call in milliseconds, never extended by its arguments (Codex's `tool_timeout_sec`). */ + toolTimeoutMs?: number; + // scient(SA-CODEX-PLUGINS): end /** Encoding for outgoing JSON-RPC request ids (default: `"number"`) */ requestIdFormat?: MCPRequestIdFormat; /** Include server-provided instructions in the system prompt (default: true) */ diff --git a/packages/coding-agent/src/mcp/config.ts b/packages/coding-agent/src/mcp/config.ts index 8989df603c..6f1885ca97 100644 --- a/packages/coding-agent/src/mcp/config.ts +++ b/packages/coding-agent/src/mcp/config.ts @@ -59,6 +59,7 @@ function convertToLegacyConfig(server: MCPServer): MCPServerConfig { // scient(SA-CODEX-PLUGINS): begin - only the codex-plugins source binds a server to its own sign-in, // and only under the id its name and URL give: any other definition naming one is not bound to it. auth: trustedMcpAuth(server.name, server.auth, server.url, server._source.provider), + toolTimeoutMs: server.toolTimeoutMs, // scient(SA-CODEX-PLUGINS): end oauth: server.oauth, }; diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index 09758d7ff4..f2836ae5e2 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -94,7 +94,20 @@ export function lookupMcpOAuthCredential( // scient(SA-CODEX-PLUGINS): end return lookupMcpOAuthCredentialForServer(authStorage, auth, config.url, { allowUrlKeyedFallback: false }); } - return lookupMcpOAuthCredentialForServer(authStorage, auth, config.url); + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in made with another OAuth client than the one + // it now configures is not used or refreshed: Codex asks for a fresh sign-in then. + const lookup = lookupMcpOAuthCredentialForServer(authStorage, auth, config.url); + const configuredClient = config.oauth?.clientId?.trim(); + if ( + lookup && + configuredClient && + isMcpOAuthServerCredentialId(lookup.credentialId) && + lookup.credential.clientId?.trim() !== configuredClient + ) { + return undefined; + } + return lookup; + // scient(SA-CODEX-PLUGINS): end } export function selectMcpOAuthRefreshMaterial( diff --git a/packages/coding-agent/src/mcp/timeout.ts b/packages/coding-agent/src/mcp/timeout.ts index 48a55a5f7d..9d266e3ee4 100644 --- a/packages/coding-agent/src/mcp/timeout.ts +++ b/packages/coding-agent/src/mcp/timeout.ts @@ -64,12 +64,24 @@ export function resolveMCPRequestDeadline( configTimeout: number | undefined, method: string, params?: Record, + // scient(SA-CODEX-PLUGINS): begin - a hard cap on tool calls (an imported Codex plugin's tool_timeout_sec). + toolTimeoutCapMs?: number, + // scient(SA-CODEX-PLUGINS): end ): MCPRequestDeadline { const configured = envTimeoutMs() ?? configTimeout; if (method !== "tools/call") { const timeoutMs = configured ?? DEFAULT_MCP_TIMEOUT_MS; return { timeoutMs, maxTotalMs: timeoutMs }; } + // scient(SA-CODEX-PLUGINS): begin - Codex keeps tool_timeout_sec as the limit, whatever the call asks for. + if (toolTimeoutCapMs !== undefined) { + const cap = + configured !== undefined && isMCPTimeoutEnabled(configured) + ? Math.min(toolTimeoutCapMs, configured) + : toolTimeoutCapMs; + return { timeoutMs: cap, maxTotalMs: cap }; + } + // scient(SA-CODEX-PLUGINS): end const base = configured ?? DEFAULT_MCP_TOOL_CALL_TIMEOUT_MS; if (!isMCPTimeoutEnabled(base)) return { timeoutMs: base, maxTotalMs: base }; const requested = requestedToolTimeoutMs(params?.arguments); diff --git a/packages/coding-agent/src/mcp/transports/http.ts b/packages/coding-agent/src/mcp/transports/http.ts index e64c4cbe16..87ecdcea54 100644 --- a/packages/coding-agent/src/mcp/transports/http.ts +++ b/packages/coding-agent/src/mcp/transports/http.ts @@ -478,7 +478,14 @@ export class HttpTransport implements MCPTransport { } callerSignal = controller.signal; } - const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline(this.config.timeout, method, params); + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap. + const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline( + this.config.timeout, + method, + params, + this.config.toolTimeoutMs, + ); + // scient(SA-CODEX-PLUGINS): end const operation = createMCPTimeout(timeout, this.#operationSignal(callerSignal), maxTotalMs); let stage: MCPFailureStage = "send"; let traceId: string | undefined; diff --git a/packages/coding-agent/src/mcp/transports/sse.ts b/packages/coding-agent/src/mcp/transports/sse.ts index cab598676f..ad26d2a4d5 100644 --- a/packages/coding-agent/src/mcp/transports/sse.ts +++ b/packages/coding-agent/src/mcp/transports/sse.ts @@ -260,7 +260,14 @@ export class LegacySseTransport implements MCPTransport { method, params: options?.onProgress ? withProgressToken(params, id) : (params ?? {}), }; - const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline(this.#config.timeout, method, params); + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap. + const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline( + this.#config.timeout, + method, + params, + this.#config.toolTimeoutMs, + ); + // scient(SA-CODEX-PLUGINS): end const operation = createMCPTimeout(timeout, this.#operationSignal(options?.signal), maxTotalMs); const progressSink = options?.onProgress; const deferred = Promise.withResolvers(); diff --git a/packages/coding-agent/src/mcp/transports/stdio.ts b/packages/coding-agent/src/mcp/transports/stdio.ts index 7c06fe7686..be1b1b6f94 100644 --- a/packages/coding-agent/src/mcp/transports/stdio.ts +++ b/packages/coding-agent/src/mcp/transports/stdio.ts @@ -824,7 +824,14 @@ export class StdioTransport implements MCPTransport { params: options?.onProgress ? withProgressToken(params, id) : (params ?? {}), }; - const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline(this.config.timeout, method, params); + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap. + const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline( + this.config.timeout, + method, + params, + this.config.toolTimeoutMs, + ); + // scient(SA-CODEX-PLUGINS): end const startedAt = Date.now(); const signal = options?.signal; diff --git a/packages/coding-agent/src/mcp/types.ts b/packages/coding-agent/src/mcp/types.ts index ab2825fae3..313faed673 100644 --- a/packages/coding-agent/src/mcp/types.ts +++ b/packages/coding-agent/src/mcp/types.ts @@ -76,6 +76,13 @@ interface MCPServerConfigBase { enabled?: boolean; /** MCP request timeout in milliseconds (default: 30000, 0 to disable) */ timeout?: number; + // scient(SA-CODEX-PLUGINS): begin - a hard cap on tool calls. + /** + * Hard limit on a `tools/call`, in milliseconds, which a call's own wait arguments never extend + * (an imported Codex plugin's `tool_timeout_sec`). + */ + toolTimeoutMs?: number; + // scient(SA-CODEX-PLUGINS): end /** * Encoding for outgoing JSON-RPC request ids (default: `"number"`). * diff --git a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts index 29b9490e28..7215c42a2c 100644 --- a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts +++ b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts @@ -1307,8 +1307,13 @@ export class MCPCommandController { // Resolve auth config if needed let resolvedConfig: MCPServerConfig; - // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it. - const testConfig = withTrustedMcpAuth(name, config, this.ctx.mcpManager?.getSource(name)?.provider); + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it: the + // source is the selected definition's own, and a config-file definition has none that binds. + const testConfig = withTrustedMcpAuth( + name, + config, + found.discovered ? this.ctx.mcpManager?.getSource(name)?.provider : undefined, + ); if (this.ctx.mcpManager) { resolvedConfig = await this.ctx.mcpManager.prepareConfig(testConfig); } else { @@ -1601,9 +1606,23 @@ export class MCPCommandController { } try { - const found = await findMcpServerForAuth(name, getProjectDir(), this.ctx.mcpManager, { - enableProjectConfig: cfgMcpEnableProjectConfig.get(this.ctx.settings), - }); + // scient(SA-CODEX-PLUGINS): begin - a challenge from a live imported server is answered for that live + // definition, never a same-named config-file entry. + const live = + options.authChallenge !== undefined && this.ctx.mcpManager?.getSource(name)?.provider === "codex-plugins" + ? this.ctx.mcpManager.getServerConfig(name) + : undefined; + const found = live + ? { + filePath: getMCPConfigPath("user", getProjectDir()), + scope: "user" as const, + config: live, + discovered: true, + } + : await findMcpServerForAuth(name, getProjectDir(), this.ctx.mcpManager, { + enableProjectConfig: cfgMcpEnableProjectConfig.get(this.ctx.settings), + }); + // scient(SA-CODEX-PLUGINS): end if (!found) { if (!options.silent) this.ctx.showError(`Server "${name}" not found.`); return; diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 642e4f96cc..29f4b71436 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -978,9 +978,11 @@ async function mapLegacyServer( if (!isStdio && server.url == null) return { error: "it declares neither command nor url" }; const wrong = (isStdio ? NOT_STDIO : NOT_HTTP).find(key => server[key] != null); if (wrong !== undefined) return { error: `${wrong} is not supported for ${isStdio ? "stdio" : "streamable HTTP"}` }; + // Codex holds every tool call to tool_timeout_sec, whatever the call's arguments ask; at zero every + // call times out, which no cap here can reproduce. + if (server.tool_timeout_sec === 0) return { error: "Codex times out each of its tool calls (tool_timeout_sec = 0)" }; const common = { - ...(typeof server.tool_timeout_sec === "number" && - server.tool_timeout_sec > 0 && { timeout: server.tool_timeout_sec * 1000 }), + ...(typeof server.tool_timeout_sec === "number" && { toolTimeoutMs: server.tool_timeout_sec * 1000 }), }; if (isStdio) { diff --git a/packages/coding-agent/src/slash-commands/helpers/mcp.ts b/packages/coding-agent/src/slash-commands/helpers/mcp.ts index 7b633a03e7..23229d3ad3 100644 --- a/packages/coding-agent/src/slash-commands/helpers/mcp.ts +++ b/packages/coding-agent/src/slash-commands/helpers/mcp.ts @@ -153,10 +153,9 @@ async function withPreparedMcpConnection( // Without this, `/mcp test|resources|prompts` silently fails for any // server saved by the TUI/reauth path. manager.setAuthStorage(runtime.session.modelRegistry.authStorage); - // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it. - const resolvedConfig = await manager.prepareConfig( - withTrustedMcpAuth(name, config, runtime.mcpManager?.getSource(name)?.provider), - ); + // scient(SA-CODEX-PLUGINS): begin - these definitions come from config files, which never hold an + // imported server's own sign-in: a server-bound id there is dropped. + const resolvedConfig = await manager.prepareConfig(withTrustedMcpAuth(name, config, undefined)); // scient(SA-CODEX-PLUGINS): end connection = await connectToServer(name, resolvedConfig); return await fn(connection); diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts index 50f8bcd837..2422665fa1 100644 --- a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -157,6 +157,20 @@ describe("an imported Codex plugin server's own sign-in", () => { expect(storage.credentials.get(oauthFlow.mcpOAuthCredentialId(SERVER_URL))).toMatchObject({ access: "native" }); }); + it("asks for a fresh sign-in once the plugin configures another OAuth client", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const stored: oauthFlow.MCPStoredOAuthCredential = { ...token, clientId: "client-a" }; + await storage.credentials.set(credentialId, stored); + const withClient = (clientId: string): MCPServerConfig => ({ + ...boundConfig(), + oauth: { clientId, clientSecret: "s" }, + }); + expect(lookupMcpOAuthCredential(storage, withClient("client-a"))?.credentialId).toBe(credentialId); + // Client A's token is neither sent nor refreshed for client B. + expect(lookupMcpOAuthCredential(storage, withClient("client-b"))).toBeUndefined(); + }); + it("leaves a server's own token in charge, as Codex does", async () => { const storage = freshStorage(); const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); @@ -284,4 +298,43 @@ describe("an imported Codex plugin server's own sign-in", () => { setAgentDir(previousAgent); } }); + + it("answers a live imported server's sign-in challenge for that server, not a same-named file entry", async () => { + using temp = TempDir.createSync("@scient-codex-plugins-challenge-"); + const previousProject = getProjectDir(); + const previousAgent = getAgentDir(); + setProjectDir(temp.path()); + setAgentDir(temp.join("agent")); + // A config-file entry of the same name, which a lookup by name alone would pick. + await Bun.write( + getMCPConfigPath("project", temp.path()), + JSON.stringify({ mcpServers: { [NAME]: { type: "http", url: SERVER_URL } } }), + ); + const connect = vi.spyOn(mcpClient, "connectToServer").mockRejectedValue(new Error("HTTP 401")); + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + try { + const ctx = createInteractiveModeContext({ + session: { modelRegistry: { authStorage: storage } }, + mcpManager: createMcpManagerStub({ + // The live server names its own token, so no OAuth sign-in may start for it. + getServerConfig: vi.fn(() => ({ ...boundConfig(), auth: { type: "apikey" as const, credentialId } })), + getSource: vi.fn(() => ({ + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/codex/plugin/.mcp.json", + level: "user" as const, + })), + }), + }); + const challenge = { wwwAuthenticate: ['Bearer scope="read"'] }; + expect(await new MCPCommandController(ctx).handleMCPAuthChallenge(NAME, challenge)).toBeUndefined(); + } finally { + setProjectDir(previousProject); + setAgentDir(previousAgent); + } + // Refused before any probe: the file entry was never signed in. + expect(connect).not.toHaveBeenCalled(); + expect(storage.credentials.get(oauthFlow.mcpOAuthCredentialId(SERVER_URL))).toBeUndefined(); + }); }); diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index cc2f5396d4..ac29f50d02 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -8,6 +8,7 @@ import type { MCPServer } from "@oh-my-pi/pi-coding-agent/capability/mcp"; import type { Skill } from "@oh-my-pi/pi-coding-agent/capability/skill"; import { cfgEnabledProviders } from "@oh-my-pi/pi-coding-agent/config/model-settings"; import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; +import { resolveMCPRequestDeadline } from "@oh-my-pi/pi-coding-agent/mcp/timeout"; import { StdioTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/stdio"; import * as piUtils from "@oh-my-pi/pi-utils"; import { restoreEnvValue } from "../helpers/settings-test-state"; @@ -169,12 +170,14 @@ describe("installed Codex plugins", () => { args: ["--stdio"], cwd: researchRoot, envPolicy: "literal", - timeout: 30_000, + // tool_timeout_sec is a hard cap on tool calls, not the stretchable request timeout. + toolTimeoutMs: 30_000, transport: "stdio", }); // Codex passes its own short list plus the server's env_vars, never the whole environment. const papers = byName.get("research@market:papers"); expect(papers?.envInherit).toEqual([]); + expect(papers?.timeout).toBeUndefined(); expect(papers?.env).toMatchObject({ PATH: process.env.PATH, CODEX_PLUGIN_TEST_TOKEN: "forwarded-value" }); expect(papers?.env?.CODEX_PLUGIN_TEST_PAT).toBeUndefined(); expect(byName.get("research@market:tracker")).toMatchObject({ @@ -250,6 +253,8 @@ describe("installed Codex plugins", () => { negative: { command: "node", args: ["negative.mjs"], startup_timeout_sec: -1 }, // One past u64::MAX, which Codex's startup_timeout_ms cannot hold. huge: { command: "node", args: ["huge.mjs"], startup_timeout_ms: 2 ** 64 }, + // Codex times out every tool call of this one. + instant: { command: "node", args: ["instant.mjs"], tool_timeout_sec: 0 }, capped: { command: "node", args: ["capped.mjs"], tools: { read: { output_token_limit: 100 } } }, shown: { command: "node", args: ["shown.mjs"], omit_tools_from: [] }, // Codex refuses to start a server whose named bearer variable is unset. @@ -285,6 +290,18 @@ describe("installed Codex plugins", () => { '"capped" of limits@market: Codex restricts its tools (tools.read.output_token_limit)', ), ); + expect(warnings).toContainEqual(expect.stringContaining('"instant" of limits@market: Codex times out each')); + }); + + it("holds a tool call to tool_timeout_sec whatever its arguments ask", () => { + // A call asking to wait 10 s still ends at the configured 2 s, as in Codex. + const args = { arguments: { wait_seconds: 10 } }; + expect(resolveMCPRequestDeadline(undefined, "tools/call", args, 2_000)).toEqual({ + timeoutMs: 2_000, + maxTotalMs: 2_000, + }); + // Other requests keep their own timeout. + expect(resolveMCPRequestDeadline(undefined, "tools/list", undefined, 2_000).timeoutMs).not.toBe(2_000); }); it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 234fe93973..7ab3071be1 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts) and in /mcp test (the TUI controller and slash-commands/helpers/mcp.ts); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile, never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it, and reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Its tool_timeout_sec is a hard cap on tool calls (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments), and a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection, slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures an OAuth client id, only for a credential made with that client (Codex asks for a fresh sign-in otherwise), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it; reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", @@ -536,6 +536,9 @@ "packages/coding-agent/src/mcp/oauth-credentials.ts", "packages/coding-agent/src/mcp/oauth-flow.ts", "packages/coding-agent/src/mcp/oauth-reauth.ts", + "packages/coding-agent/src/mcp/timeout.ts", + "packages/coding-agent/src/mcp/transports/http.ts", + "packages/coding-agent/src/mcp/transports/sse.ts", "packages/coding-agent/src/mcp/transports/stdio.ts", "packages/coding-agent/src/mcp/types.ts", "packages/coding-agent/src/modes/controllers/mcp-command-controller.ts", From 02a3d5505da566d275989b1660acd9d1ec4e987a Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 13:48:27 +0200 Subject: [PATCH 15/30] fix(codex-plugins): supplied-source provenance, exact confidential client IDs, Codex tool-timeout default - The manager judges a server-bound sign-in only by the source supplied with that definition; /mcp test syncs the tested definition with its own source. - The client-ID check compares exact IDs for confidential clients only and leaves non-bound lookups untouched; imports with a client secret but no ID, a blank secret, or a padded client ID are skipped. - Imported tool calls default to Codex's 300 s cap, and the HTTP transport carries the remaining cap through the auth refresh and retry. --- SCIENT.md | 2 +- packages/coding-agent/CHANGELOG.md | 2 +- packages/coding-agent/src/mcp/manager.ts | 5 +- .../coding-agent/src/mcp/oauth-credentials.ts | 14 ++--- .../coding-agent/src/mcp/transports/http.ts | 50 ++++++++++++++- .../controllers/mcp-command-controller.ts | 17 +++--- .../coding-agent/src/scient/codex-plugins.ts | 13 ++++ .../test/scient/codex-plugins-oauth.test.ts | 54 ++++++++++++++-- .../test/scient/codex-plugins.test.ts | 61 +++++++++++++++++++ scripts/scient/divergences.json | 2 +- 10 files changed, 191 insertions(+), 29 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index e701969639..af7c040f95 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec` whatever the call asks to wait (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which no other definition can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made with another OAuth client than the one the plugin now configures is not used, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec`, or Codex's five-minute default, whatever the call asks to wait and across an HTTP sign-in refresh and retry (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which only a definition the `codex-plugins` source supplies can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made for another confidential client ID than the one the plugin now configures (compared exactly, as Codex does) is not used, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. A client secret without a client ID or of only whitespace, or a client ID with surrounding whitespace, skips the server. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 2d5bd58d9e..2ec92412fd 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added -- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, tool calls held to the plugin's `tool_timeout_sec`, and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures and signing in again when the plugin changes its OAuth client; one that names its own token keeps it and is never signed in with OAuth. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). +- The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, tool calls held to the plugin's `tool_timeout_sec` (five minutes unless set, as in Codex), and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures and signing in again when the plugin changes its OAuth client; one that names its own token keeps it and is never signed in with OAuth. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/mcp/manager.ts b/packages/coding-agent/src/mcp/manager.ts index 80a513c7a2..f42932db0c 100644 --- a/packages/coding-agent/src/mcp/manager.ts +++ b/packages/coding-agent/src/mcp/manager.ts @@ -975,9 +975,10 @@ export class MCPManager implements MCPManagerApi { // Prepare connection tasks const connectionTasks: ConnectionTask[] = []; - // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it. + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it, as the + // source supplied with this definition says: a source left registered by an earlier definition never vouches. for (const [name, declared] of Object.entries(configs)) { - const config = withTrustedMcpAuth(name, declared, (sources[name] ?? this.#sources.get(name))?.provider); + const config = withTrustedMcpAuth(name, declared, sources[name]?.provider); // scient(SA-CODEX-PLUGINS): end this.#startupServers.add(name); // Connecting a server by name (`/mcp enable`) ends its built-in drop. diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index f2836ae5e2..3ff660eb4e 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -94,15 +94,15 @@ export function lookupMcpOAuthCredential( // scient(SA-CODEX-PLUGINS): end return lookupMcpOAuthCredentialForServer(authStorage, auth, config.url, { allowUrlKeyedFallback: false }); } - // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in made with another OAuth client than the one - // it now configures is not used or refreshed: Codex asks for a fresh sign-in then. + // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in made for another confidential client than the one + // it now configures is not used or refreshed: Codex compares the exact IDs and asks for a fresh sign-in. const lookup = lookupMcpOAuthCredentialForServer(authStorage, auth, config.url); - const configuredClient = config.oauth?.clientId?.trim(); + if (!lookup || !isMcpOAuthServerCredentialId(lookup.credentialId)) return lookup; + const clientId = config.oauth?.clientId; if ( - lookup && - configuredClient && - isMcpOAuthServerCredentialId(lookup.credentialId) && - lookup.credential.clientId?.trim() !== configuredClient + typeof config.oauth?.clientSecret === "string" && + typeof clientId === "string" && + lookup.credential.clientId !== clientId ) { return undefined; } diff --git a/packages/coding-agent/src/mcp/transports/http.ts b/packages/coding-agent/src/mcp/transports/http.ts index 87ecdcea54..8640c0f20b 100644 --- a/packages/coding-agent/src/mcp/transports/http.ts +++ b/packages/coding-agent/src/mcp/transports/http.ts @@ -52,6 +52,17 @@ interface SSEResumeState { */ class SSEResumeError extends Error {} +// scient(SA-CODEX-PLUGINS): begin - a tool call that ran out its hard cap. +function toolTimeoutError(capMs: number): MCPTransportError { + return new MCPTransportError({ + transport: "http", + stage: "send", + failure: "timeout", + message: `Request timeout after ${capMs}ms`, + retryable: false, + }); +} +// scient(SA-CODEX-PLUGINS): end /** Wait for the server-provided SSE retry interval while remaining abortable. */ async function waitForSSERetry(ms: number, signal: AbortSignal): Promise { if (signal.aborted) throw signal.reason; @@ -411,6 +422,11 @@ export class HttpTransport implements MCPTransport { params: Record | undefined, options: MCPRequestOptions | undefined, ): Promise { + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap spans the first attempt, the sign-in refresh + // after a 401 or 403 and the retry. + const capMs = method === "tools/call" ? this.config.toolTimeoutMs : undefined; + const startedAt = Date.now(); + // scient(SA-CODEX-PLUGINS): end try { return await this.#executeRequest(method, params, options); } catch (error) { @@ -420,21 +436,49 @@ export class HttpTransport implements MCPTransport { // state-changing tool twice. const status = error instanceof Error ? AIError.status(error) : undefined; if (!(error instanceof SSEResumeError) && this.onAuthError && (status === 401 || status === 403)) { - const newHeaders = await this.onAuthError(); + // scient(SA-CODEX-PLUGINS): begin - the refresh and the retry get only what is left of the cap. + const newHeaders = + capMs === undefined + ? await this.onAuthError() + : await this.#withinToolBudget(this.onAuthError(), capMs, startedAt); if (newHeaders) { // Persist refreshed headers so subsequent requests use them directly this.config = { ...this.config, headers: newHeaders }; - return this.#executeRequest(method, params, options); + const leftMs = capMs === undefined ? undefined : this.#toolBudgetLeft(capMs, startedAt); + return this.#executeRequest(method, params, options, leftMs); } + // scient(SA-CODEX-PLUGINS): end } throw error; } } + // scient(SA-CODEX-PLUGINS): begin - what is left of a tool call's hard cap, or its timeout once spent. + #toolBudgetLeft(capMs: number, startedAt: number): number { + const leftMs = capMs - (Date.now() - startedAt); + if (leftMs > 0) return leftMs; + throw toolTimeoutError(capMs); + } + + async #withinToolBudget(work: Promise, capMs: number, startedAt: number): Promise { + void work.catch(() => {}); + const operation = createMCPTimeout(this.#toolBudgetLeft(capMs, startedAt)); + try { + return await untilAborted(operation.signal, work); + } catch (error) { + throw operation.timedOut() ? toolTimeoutError(capMs) : error; + } finally { + operation.clear(); + } + } + // scient(SA-CODEX-PLUGINS): end async #executeRequest( method: string, params: Record | undefined, options: MCPRequestOptions | undefined, + // scient(SA-CODEX-PLUGINS): begin - what is left of a hard tool-call cap after an auth retry. + toolTimeoutCapMs?: number, + // scient(SA-CODEX-PLUGINS): end ): Promise { if (!this.#connected) { throw new MCPTransportError({ @@ -483,7 +527,7 @@ export class HttpTransport implements MCPTransport { this.config.timeout, method, params, - this.config.toolTimeoutMs, + toolTimeoutCapMs ?? this.config.toolTimeoutMs, ); // scient(SA-CODEX-PLUGINS): end const operation = createMCPTimeout(timeout, this.#operationSignal(callerSignal), maxTotalMs); diff --git a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts index 7215c42a2c..2aa5c48539 100644 --- a/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts +++ b/packages/coding-agent/src/modes/controllers/mcp-command-controller.ts @@ -905,10 +905,12 @@ export class MCPCommandController { } } - async #syncManagerConnection(name: string, config: MCPServerConfig): Promise { + // scient(SA-CODEX-PLUGINS): begin - the definition connects with its own source, if any. + async #syncManagerConnection(name: string, config: MCPServerConfig, source?: SourceMeta): Promise { if (!this.ctx.mcpManager) return; if (this.ctx.mcpManager.getConnectionStatus(name) !== "disconnected") return; - await this.ctx.mcpManager.connectServers({ [name]: config }, {}); + await this.ctx.mcpManager.connectServers({ [name]: config }, source ? { [name]: source } : {}); + // scient(SA-CODEX-PLUGINS): end if (this.ctx.mcpManager.getConnectionStatus(name) === "connected") { await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools()); } @@ -1309,11 +1311,8 @@ export class MCPCommandController { let resolvedConfig: MCPServerConfig; // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in only for the server imported under it: the // source is the selected definition's own, and a config-file definition has none that binds. - const testConfig = withTrustedMcpAuth( - name, - config, - found.discovered ? this.ctx.mcpManager?.getSource(name)?.provider : undefined, - ); + const testSource = found.discovered ? this.ctx.mcpManager?.getSource(name) : undefined; + const testConfig = withTrustedMcpAuth(name, config, testSource?.provider); if (this.ctx.mcpManager) { resolvedConfig = await this.ctx.mcpManager.prepareConfig(testConfig); } else { @@ -1347,7 +1346,9 @@ export class MCPCommandController { } lines.push(""); - await this.#syncManagerConnection(name, config); + // scient(SA-CODEX-PLUGINS): begin - the tested definition, with the source it was tested under. + await this.#syncManagerConnection(name, testConfig, testSource); + // scient(SA-CODEX-PLUGINS): end this.#showMessage(lines.join("\n")); } catch (error) { if (abortController.signal.aborted || (error instanceof Error && error.name === "AbortError")) { diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 29f4b71436..5db6204114 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -58,6 +58,9 @@ const HOST_PLUGINS: Record = { /** OpenAI's launcher for its signed Computer Use client, which serves only Codex (the Messages plugin). */ const HOST_LAUNCHER = "computer-use-client-launcher"; +/** Codex's tool-call limit when a server sets no tool_timeout_sec (codex-mcp DEFAULT_TOOL_TIMEOUT). */ +const CODEX_DEFAULT_TOOL_TIMEOUT_MS = 300_000; + const IS_WINDOWS = process.platform === "win32"; /** @@ -1055,6 +1058,14 @@ async function mapLegacyServer( const value = oauthTable[snake] ?? oauthTable[camel]; if (typeof value === "string" && value) oauth[target] = value; } + // Codex refuses a client secret without a client ID or of only whitespace, and holds a confidential client's + // sign-in to the client ID exactly as written, which Scient Agent's sign-in would trim. + if (oauth.clientSecret !== undefined && (oauth.clientId === undefined || !oauth.clientSecret.trim())) { + return { error: "its OAuth client_secret has no client_id or is blank, so Codex refuses to start it" }; + } + if (oauth.clientId !== undefined && oauth.clientId !== oauth.clientId.trim()) { + return { error: "its OAuth client_id has surrounding whitespace, which Scient Agent's sign-in would not keep" }; + } const port = oauthTable.callback_port ?? oauthTable.callbackPort; if (Number.isInteger(port) && Number(port) > 0) oauth.callbackPort = Number(port); // Codex asks for the configured scopes as they are, an empty list meaning none. @@ -1258,6 +1269,8 @@ async function loadMCPServers(ctx: LoadContext): Promise> items.push({ name, ...config, + // Codex holds each tool call to tool_timeout_sec, five minutes unless set, in both formats. + toolTimeoutMs: config.toolTimeoutMs ?? CODEX_DEFAULT_TOOL_TIMEOUT_MS, // Its own sign-in, never another server's at the same URL: Codex keys sign-ins by name and URL. A // server that names its own token (`bearer_token_env_var`) is never signed in with OAuth there, // so it is bound as a key, which no stored sign-in answers. diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts index 2422665fa1..16e08529df 100644 --- a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -7,6 +7,7 @@ import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; import * as mcpClient from "@oh-my-pi/pi-coding-agent/mcp/client"; import { loadAllMCPConfigs } from "@oh-my-pi/pi-coding-agent/mcp/config"; import { readDisabledServers } from "@oh-my-pi/pi-coding-agent/mcp/config-writer"; +import { MCPManager } from "@oh-my-pi/pi-coding-agent/mcp/manager"; import { lookupMcpOAuthCredential, lookupMcpOAuthCredentialForServer, @@ -157,18 +158,59 @@ describe("an imported Codex plugin server's own sign-in", () => { expect(storage.credentials.get(oauthFlow.mcpOAuthCredentialId(SERVER_URL))).toMatchObject({ access: "native" }); }); - it("asks for a fresh sign-in once the plugin configures another OAuth client", async () => { + it("asks for a fresh sign-in once the plugin configures another confidential OAuth client", async () => { const storage = freshStorage(); const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); const stored: oauthFlow.MCPStoredOAuthCredential = { ...token, clientId: "client-a" }; await storage.credentials.set(credentialId, stored); - const withClient = (clientId: string): MCPServerConfig => ({ + const withClient = (clientId: string, clientSecret?: string): MCPServerConfig => ({ ...boundConfig(), - oauth: { clientId, clientSecret: "s" }, + oauth: { clientId, ...(clientSecret !== undefined && { clientSecret }) }, }); - expect(lookupMcpOAuthCredential(storage, withClient("client-a"))?.credentialId).toBe(credentialId); - // Client A's token is neither sent nor refreshed for client B. - expect(lookupMcpOAuthCredential(storage, withClient("client-b"))).toBeUndefined(); + expect(lookupMcpOAuthCredential(storage, withClient("client-a", "s"))?.credentialId).toBe(credentialId); + // Client A's token is neither sent nor refreshed for client B, nor for an ID that differs only in spaces. + expect(lookupMcpOAuthCredential(storage, withClient("client-b", "s"))).toBeUndefined(); + expect(lookupMcpOAuthCredential(storage, withClient("client-a ", "s"))).toBeUndefined(); + // Codex holds only a confidential client (one with a secret) to its ID. + expect(lookupMcpOAuthCredential(storage, withClient("client-b"))?.credentialId).toBe(credentialId); + // A server with no sign-in of its own is looked up as before, whatever its client settings hold. + const urlKeyed = oauthFlow.mcpOAuthCredentialId(SERVER_URL); + await storage.credentials.set(urlKeyed, { + ...token, + clientId: "registered", + } as oauthFlow.MCPStoredOAuthCredential); + const ordinary = (clientId: unknown) => + ({ type: "http", url: SERVER_URL, oauth: { clientId, clientSecret: "s" } }) as never; + expect(lookupMcpOAuthCredential(storage, ordinary("configured"))?.credentialId).toBe(urlKeyed); + expect(lookupMcpOAuthCredential(storage, ordinary(7))?.credentialId).toBe(urlKeyed); + }); + + it("binds a definition only by the source supplied with it, never one left registered", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + await storage.credentials.set(credentialId, { ...token, access: "own" }); + const probed: MCPServerConfig[] = []; + vi.spyOn(mcpClient, "connectToServer").mockImplementation((async (_name: string, probe: MCPServerConfig) => { + probed.push(probe); + throw new Error("unreachable"); + }) as never); + using temp = TempDir.createSync("@scient-codex-plugins-provenance-"); + const manager = new MCPManager(temp.path()); + manager.setAuthStorage(storage); + const imported = { + provider: "codex-plugins", + providerName: "Codex Plugins", + path: "/p/.mcp.json", + level: "user" as const, + }; + // The imported server fails to connect, which leaves its source registered under the name. + await manager.connectServers({ [NAME]: boundConfig() }, { [NAME]: imported }); + // A definition arriving without a source (a config file's) names the same sign-in. + await manager.connectServers({ [NAME]: boundConfig() }, {}); + expect(probed).toHaveLength(2); + expect(probed[0]).toMatchObject({ headers: { Authorization: "Bearer own" } }); + expect(probed[1]).not.toMatchObject({ headers: { Authorization: expect.anything() } }); + await manager.disconnectAll(); }); it("leaves a server's own token in charge, as Codex does", async () => { diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index ac29f50d02..3ef0b9bd8a 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -9,6 +9,7 @@ import type { Skill } from "@oh-my-pi/pi-coding-agent/capability/skill"; import { cfgEnabledProviders } from "@oh-my-pi/pi-coding-agent/config/model-settings"; import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; import { resolveMCPRequestDeadline } from "@oh-my-pi/pi-coding-agent/mcp/timeout"; +import { HttpTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/http"; import { StdioTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/stdio"; import * as piUtils from "@oh-my-pi/pi-utils"; import { restoreEnvValue } from "../helpers/settings-test-state"; @@ -270,12 +271,22 @@ describe("installed Codex plugins", () => { http_headers: { "X-Account": "account-a", "Bad Name": "x" }, env_http_headers: { "X-Account": "CODEX_PLUGIN_TEST_NEWLINE" }, }, + // Codex compares a confidential client's stored ID with this one exactly, which Scient would trim. + spacedclient: { url: "https://mcp.example.test/d", oauth: { client_id: " id ", client_secret: "s" } }, + // Codex refuses a client secret without a client ID. + secretonly: { url: "https://mcp.example.test/e", oauth: { client_secret: "s" } }, }); const { names, warnings } = await limits(); expect(names.sort()).toEqual(["limits@market:cased", "limits@market:kept", "limits@market:shown"]); const byName = (await load()).byName; expect(byName.get("limits@market:cased")?.headers).toEqual({ Authorization: "Bearer pat-value" }); expect(byName.get("limits@market:kept")?.headers).toEqual({ "X-Account": "account-a" }); + // Codex's five-minute default holds a server that sets no tool_timeout_sec. + expect(byName.get("limits@market:shown")?.toolTimeoutMs).toBe(300_000); + expect(warnings).toContainEqual(expect.stringContaining('"spacedclient" of limits@market: its OAuth client_id')); + expect(warnings).toContainEqual( + expect.stringContaining('"secretonly" of limits@market: its OAuth client_secret has no client_id'), + ); expect(warnings).toContainEqual( expect.stringContaining('"unsetbearer" of limits@market: its bearer_token_env_var CODEX_PLUGIN_TEST_UNSET'), ); @@ -304,6 +315,54 @@ describe("installed Codex plugins", () => { expect(resolveMCPRequestDeadline(undefined, "tools/list", undefined, 2_000).timeoutMs).not.toBe(2_000); }); + // The cap is the transport's own timer on a live socket, which fake timers do not drive: the test + // waits on no fixed delay, only on that cap (300 ms at most) firing. + it("keeps a tool call's cap across an HTTP sign-in refresh and retry", async () => { + let posts = 0; + const server = Bun.serve({ + port: 0, + async fetch(req) { + await req.json(); + posts++; + // The first attempt is refused; the retry is never answered. + return posts === 1 ? new Response("expired", { status: 401 }) : new Promise(() => {}); + }, + }); + const call = async (refresh: () => Promise | null>) => { + posts = 0; + const transport = new HttpTransport({ + type: "http", + url: `http://127.0.0.1:${server.port}/mcp`, + toolTimeoutMs: 300, + }); + await transport.connect(); + transport.onAuthError = refresh; + try { + return await transport.request("tools/call", { name: "slow", arguments: {} }).then( + () => "resolved", + (error: Error) => error.message, + ); + } finally { + await transport.close(); + } + }; + try { + // A refresh that never ends still ends the call at the cap. + expect(await call(() => new Promise(() => {}))).toBe("Request timeout after 300ms"); + // A refresh that takes 200 ms of the cap leaves the retry the rest, not a fresh 300 ms. + const realNow = Date.now; + let skew = 0; + vi.spyOn(Date, "now").mockImplementation(() => realNow() + skew); + const message = await call(async () => { + skew = 200; + return { Authorization: "Bearer refreshed" }; + }); + expect(Number(/Request timeout after (\d+)ms/.exec(message)?.[1])).toBeLessThanOrEqual(100); + } finally { + server.stop(true); + } + }); + it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { await installLimits( { denied: { command: "node", args: ["denied.mjs"] }, open: { command: "node", args: ["open.mjs"] } }, @@ -780,6 +839,8 @@ describe("installed Codex plugins", () => { MODE: "x", }); expect(byName.has("linkedmcp@market:y")).toBe(false); + // mcp.json carries no tool_timeout_sec, so Codex's five-minute default holds its tool calls. + expect(byName.get("portable2@market:local")?.toolTimeoutMs).toBe(300_000); expect(warnings).toContainEqual(expect.stringContaining("linkedmcp@market: mcp.json is not a regular file")); }); diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 7ab3071be1..5d688ef163 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Its tool_timeout_sec is a hard cap on tool calls (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments), and a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection, slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures an OAuth client id, only for a credential made with that client (Codex asks for a fresh sign-in otherwise), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it; reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress, and in mcp/transports/http.ts spanning the sign-in refresh and the retry after a 401 or 403), and a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource; a client secret without a client ID or of only whitespace, which Codex refuses, or a client ID with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in otherwise; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it; reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", From bfa3436d6c9f27a8a296c8dd6c840f64c19c0707 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 14:02:53 +0200 Subject: [PATCH 16/30] fix(codex-plugins): Codex OAuth validation, secret-bound refresh, one deadline per tool call - An empty client_secret stays present and is rejected; authorization_server_issuer and token_endpoint_auth_method skip the server; a padded secret skips it like a padded client ID. - A bound confidential sign-in is used only when its stored client ID and secret both match the configured ones, since refresh sends the stored secret. - MCPRequestOptions.deadlineAt carries one deadline through the HTTP auth retry and the tool bridge's auth reconnect, 503/expired-session reconnect and replay; a reconnect pending at the deadline ends the call. --- SCIENT.md | 2 +- .../coding-agent/src/mcp/oauth-credentials.ts | 8 ++- packages/coding-agent/src/mcp/timeout.ts | 8 ++- packages/coding-agent/src/mcp/tool-bridge.ts | 59 ++++++++++++--- .../coding-agent/src/mcp/transports/http.ts | 41 ++++++----- .../coding-agent/src/mcp/transports/sse.ts | 1 + .../coding-agent/src/mcp/transports/stdio.ts | 1 + packages/coding-agent/src/mcp/types.ts | 7 ++ .../coding-agent/src/scient/codex-plugins.ts | 21 ++++-- .../test/scient/codex-plugins-oauth.test.ts | 4 +- .../test/scient/codex-plugins.test.ts | 72 +++++++++++++++++++ scripts/scient/divergences.json | 3 +- 12 files changed, 185 insertions(+), 42 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index af7c040f95..786a3f9293 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec`, or Codex's five-minute default, whatever the call asks to wait and across an HTTP sign-in refresh and retry (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which only a definition the `codex-plugins` source supplies can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made for another confidential client ID than the one the plugin now configures (compared exactly, as Codex does) is not used, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. A client secret without a client ID or of only whitespace, or a client ID with surrounding whitespace, skips the server. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec`, or Codex's five-minute default, whatever the call asks to wait and across a sign-in refresh, a reconnect and a replay of the same call (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which only a definition the `codex-plugins` source supplies can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made for another confidential client ID or secret than the plugin now configures (compared exactly) is not used, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. OAuth settings Codex refuses (a client secret without a client ID, or empty or blank; `authorization_server_issuer` or `token_endpoint_auth_method` without `ema_auth`) or a client ID or secret with surrounding whitespace skip the server. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index 3ff660eb4e..6ed3122cfb 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -96,13 +96,15 @@ export function lookupMcpOAuthCredential( } // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in made for another confidential client than the one // it now configures is not used or refreshed: Codex compares the exact IDs and asks for a fresh sign-in. + // Refresh reads its client from the stored sign-in, so one stored with another secret is not used either: + // it would refresh with a secret the plugin no longer configures. const lookup = lookupMcpOAuthCredentialForServer(authStorage, auth, config.url); if (!lookup || !isMcpOAuthServerCredentialId(lookup.credentialId)) return lookup; - const clientId = config.oauth?.clientId; + const { clientId, clientSecret } = config.oauth ?? {}; if ( - typeof config.oauth?.clientSecret === "string" && + typeof clientSecret === "string" && typeof clientId === "string" && - lookup.credential.clientId !== clientId + (lookup.credential.clientId !== clientId || lookup.credential.clientSecret !== clientSecret) ) { return undefined; } diff --git a/packages/coding-agent/src/mcp/timeout.ts b/packages/coding-agent/src/mcp/timeout.ts index 9d266e3ee4..98948cd315 100644 --- a/packages/coding-agent/src/mcp/timeout.ts +++ b/packages/coding-agent/src/mcp/timeout.ts @@ -64,8 +64,10 @@ export function resolveMCPRequestDeadline( configTimeout: number | undefined, method: string, params?: Record, - // scient(SA-CODEX-PLUGINS): begin - a hard cap on tool calls (an imported Codex plugin's tool_timeout_sec). + // scient(SA-CODEX-PLUGINS): begin - a hard cap on tool calls (an imported Codex plugin's tool_timeout_sec), + // never past the logical call's deadline. toolTimeoutCapMs?: number, + deadlineAt?: number, // scient(SA-CODEX-PLUGINS): end ): MCPRequestDeadline { const configured = envTimeoutMs() ?? configTimeout; @@ -75,10 +77,12 @@ export function resolveMCPRequestDeadline( } // scient(SA-CODEX-PLUGINS): begin - Codex keeps tool_timeout_sec as the limit, whatever the call asks for. if (toolTimeoutCapMs !== undefined) { - const cap = + let cap = configured !== undefined && isMCPTimeoutEnabled(configured) ? Math.min(toolTimeoutCapMs, configured) : toolTimeoutCapMs; + // A spent deadline still arms a timer (1 ms), as 0 would disable it. + if (deadlineAt !== undefined) cap = Math.max(1, Math.min(cap, deadlineAt - Date.now())); return { timeoutMs: cap, maxTotalMs: cap }; } // scient(SA-CODEX-PLUGINS): end diff --git a/packages/coding-agent/src/mcp/tool-bridge.ts b/packages/coding-agent/src/mcp/tool-bridge.ts index 4365e2d796..ff7e979056 100644 --- a/packages/coding-agent/src/mcp/tool-bridge.ts +++ b/packages/coding-agent/src/mcp/tool-bridge.ts @@ -448,6 +448,14 @@ function getMcpAuthChallenge(result: MCPToolCallResult): MCPAuthChallenge | unde return wwwAuthenticate.length > 0 ? { wwwAuthenticate } : undefined; } +// scient(SA-CODEX-PLUGINS): begin - one deadline for a logical call to a server with a hard tool-call cap +// (an imported Codex plugin's tool_timeout_sec), carried through reconnecting and replaying it. +/** When a logical tool call to this connection's server must end, if its server caps tool calls. */ +function toolCallDeadline(connection: MCPServerConnection): number | undefined { + const capMs = connection.config.toolTimeoutMs; + return capMs === undefined ? undefined : Date.now() + capMs; +} + async function callToolWithAuthRetry( connection: MCPServerConnection, toolName: string, @@ -455,14 +463,15 @@ async function callToolWithAuthRetry( reconnect: MCPReconnect | undefined, signal?: AbortSignal, onProgress?: MCPRequestOptions["onProgress"], + deadlineAt?: number, ): Promise { - const result = await callTool(connection, toolName, args, { signal, onProgress }); + const result = await callTool(connection, toolName, args, { signal, onProgress, deadlineAt }); const authChallenge = getMcpAuthChallenge(result); if (!authChallenge || !reconnect) return { connection, result }; let newConnection: MCPServerConnection | null; try { - newConnection = await reconnectWithAbort(reconnect, signal, { authChallenge }); + newConnection = await reconnectWithAbort(reconnect, signal, { authChallenge }, deadlineAt); } catch (error) { rethrowIfAborted(error, signal); return { connection, error }; @@ -472,13 +481,14 @@ async function callToolWithAuthRetry( try { return { connection: newConnection, - result: await callTool(newConnection, toolName, args, { signal, onProgress }), + result: await callTool(newConnection, toolName, args, { signal, onProgress, deadlineAt }), }; } catch (error) { rethrowIfAborted(error, signal); return { connection: newConnection, error }; } } +// scient(SA-CODEX-PLUGINS): end /** Re-throw abort-related errors so they bypass error-result handling. */ function rethrowIfAborted(error: unknown, signal?: AbortSignal): void { @@ -487,18 +497,31 @@ function rethrowIfAborted(error: unknown, signal?: AbortSignal): void { if (signal?.aborted) throw new ToolAbortError(); } +// scient(SA-CODEX-PLUGINS): begin - a reconnect is waited on no longer than the logical call's deadline; +// past it the call ends with its own error. async function reconnectWithAbort( reconnect: MCPReconnect, signal?: AbortSignal, options?: { authChallenge?: MCPAuthChallenge }, + deadlineAt?: number, ): Promise { try { - return await untilAborted(signal, () => reconnect(options)); + const pending = untilAborted(signal, () => reconnect(options)); + if (deadlineAt === undefined) return await pending; + void pending.catch(() => {}); + const { promise: expired, resolve } = Promise.withResolvers(); + const timer = setTimeout(() => resolve(null), Math.max(0, deadlineAt - Date.now())); + try { + return await Promise.race([pending, expired]); + } finally { + clearTimeout(timer); + } } catch (error) { rethrowIfAborted(error, signal); return null; } } +// scient(SA-CODEX-PLUGINS): end /** * Create a unique tool name for an MCP tool. @@ -861,6 +884,9 @@ export class MCPTool implements CustomTool { const progress = progressUpdater(onUpdate, this.connection.name, this.tool.name, provider, providerName); const onProgress = progress?.onProgress; + // scient(SA-CODEX-PLUGINS): begin - one deadline for this call, its auth retry, reconnect and replay. + const deadlineAt = toolCallDeadline(this.connection); + // scient(SA-CODEX-PLUGINS): end try { const attempt = await callToolWithAuthRetry( this.connection, @@ -869,6 +895,9 @@ export class MCPTool implements CustomTool { this.reconnect, signal, onProgress, + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + deadlineAt, + // scient(SA-CODEX-PLUGINS): end ); if (attempt.error !== undefined) { return buildErrorResult(attempt.error, this.connection.name, this.tool.name, provider, providerName); @@ -893,14 +922,18 @@ export class MCPTool implements CustomTool { } catch (error) { rethrowIfAborted(error, signal); if (this.reconnect && isRetriableConnectionError(error)) { - const newConn = await reconnectWithAbort(this.reconnect, signal); + // scient(SA-CODEX-PLUGINS): begin - the reconnect and the replay get only what is left. + const newConn = await reconnectWithAbort(this.reconnect, signal, undefined, deadlineAt); + // scient(SA-CODEX-PLUGINS): end if (newConn) { // Rebind so subsequent calls on this instance use the fresh connection this.connection = newConn; const retryProvider = newConn._source?.provider ?? provider; const retryProviderName = newConn._source?.providerName ?? providerName; try { - const result = await callTool(newConn, this.tool.name, args, { signal, onProgress }); + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + const result = await callTool(newConn, this.tool.name, args, { signal, onProgress, deadlineAt }); + // scient(SA-CODEX-PLUGINS): end return buildResult(result, newConn.name, this.tool.name, retryProvider, retryProviderName); } catch (retryError) { rethrowIfAborted(retryError, signal); @@ -1026,6 +1059,9 @@ export class DeferredMCPTool implements CustomTool { try { const connection = await untilAborted(signal, () => this.getConnection()); throwIfAborted(signal); + // scient(SA-CODEX-PLUGINS): begin - one deadline for this call, its auth retry, reconnect and replay. + const deadlineAt = toolCallDeadline(connection); + // scient(SA-CODEX-PLUGINS): end try { const attempt = await callToolWithAuthRetry( connection, @@ -1034,6 +1070,9 @@ export class DeferredMCPTool implements CustomTool { this.reconnect, signal, onProgress, + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + deadlineAt, + // scient(SA-CODEX-PLUGINS): end ); if (attempt.error !== undefined) { return buildErrorResult( @@ -1063,12 +1102,16 @@ export class DeferredMCPTool implements CustomTool { } catch (callError) { rethrowIfAborted(callError, signal); if (this.reconnect && isRetriableConnectionError(callError)) { - const newConn = await reconnectWithAbort(this.reconnect, signal); + // scient(SA-CODEX-PLUGINS): begin - the reconnect and the replay get only what is left. + const newConn = await reconnectWithAbort(this.reconnect, signal, undefined, deadlineAt); + // scient(SA-CODEX-PLUGINS): end if (newConn) { const retryProvider = newConn._source?.provider ?? provider; const retryProviderName = newConn._source?.providerName ?? providerName; try { - const result = await callTool(newConn, this.tool.name, args, { signal, onProgress }); + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + const result = await callTool(newConn, this.tool.name, args, { signal, onProgress, deadlineAt }); + // scient(SA-CODEX-PLUGINS): end return buildResult(result, this.serverName, this.tool.name, retryProvider, retryProviderName); } catch (retryError) { rethrowIfAborted(retryError, signal); diff --git a/packages/coding-agent/src/mcp/transports/http.ts b/packages/coding-agent/src/mcp/transports/http.ts index 8640c0f20b..6bb2285e25 100644 --- a/packages/coding-agent/src/mcp/transports/http.ts +++ b/packages/coding-agent/src/mcp/transports/http.ts @@ -423,12 +423,18 @@ export class HttpTransport implements MCPTransport { options: MCPRequestOptions | undefined, ): Promise { // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap spans the first attempt, the sign-in refresh - // after a 401 or 403 and the retry. + // after a 401 or 403 and the retry, within any deadline the caller already set. const capMs = method === "tools/call" ? this.config.toolTimeoutMs : undefined; - const startedAt = Date.now(); + const deadlineAt = + capMs === undefined + ? undefined + : Math.min(options?.deadlineAt ?? Number.POSITIVE_INFINITY, Date.now() + capMs); + const callOptions = deadlineAt === undefined ? options : { ...options, deadlineAt }; // scient(SA-CODEX-PLUGINS): end try { - return await this.#executeRequest(method, params, options); + // scient(SA-CODEX-PLUGINS): begin - the call's deadline. + return await this.#executeRequest(method, params, callOptions); + // scient(SA-CODEX-PLUGINS): end } catch (error) { // Retry once on auth failure if onAuthError is wired. Never replay // after an SSE resume failure: the server already accepted the @@ -438,14 +444,13 @@ export class HttpTransport implements MCPTransport { if (!(error instanceof SSEResumeError) && this.onAuthError && (status === 401 || status === 403)) { // scient(SA-CODEX-PLUGINS): begin - the refresh and the retry get only what is left of the cap. const newHeaders = - capMs === undefined + capMs === undefined || deadlineAt === undefined ? await this.onAuthError() - : await this.#withinToolBudget(this.onAuthError(), capMs, startedAt); + : await this.#beforeToolDeadline(this.onAuthError(), capMs, deadlineAt); if (newHeaders) { // Persist refreshed headers so subsequent requests use them directly this.config = { ...this.config, headers: newHeaders }; - const leftMs = capMs === undefined ? undefined : this.#toolBudgetLeft(capMs, startedAt); - return this.#executeRequest(method, params, options, leftMs); + return this.#executeRequest(method, params, callOptions); } // scient(SA-CODEX-PLUGINS): end } @@ -453,16 +458,12 @@ export class HttpTransport implements MCPTransport { } } - // scient(SA-CODEX-PLUGINS): begin - what is left of a tool call's hard cap, or its timeout once spent. - #toolBudgetLeft(capMs: number, startedAt: number): number { - const leftMs = capMs - (Date.now() - startedAt); - if (leftMs > 0) return leftMs; - throw toolTimeoutError(capMs); - } - - async #withinToolBudget(work: Promise, capMs: number, startedAt: number): Promise { + // scient(SA-CODEX-PLUGINS): begin - work a capped tool call waits on, ended by the call's deadline. + async #beforeToolDeadline(work: Promise, capMs: number, deadlineAt: number): Promise { void work.catch(() => {}); - const operation = createMCPTimeout(this.#toolBudgetLeft(capMs, startedAt)); + const leftMs = deadlineAt - Date.now(); + if (leftMs <= 0) throw toolTimeoutError(capMs); + const operation = createMCPTimeout(leftMs); try { return await untilAborted(operation.signal, work); } catch (error) { @@ -476,9 +477,6 @@ export class HttpTransport implements MCPTransport { method: string, params: Record | undefined, options: MCPRequestOptions | undefined, - // scient(SA-CODEX-PLUGINS): begin - what is left of a hard tool-call cap after an auth retry. - toolTimeoutCapMs?: number, - // scient(SA-CODEX-PLUGINS): end ): Promise { if (!this.#connected) { throw new MCPTransportError({ @@ -522,12 +520,13 @@ export class HttpTransport implements MCPTransport { } callerSignal = controller.signal; } - // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap. + // scient(SA-CODEX-PLUGINS): begin - a hard tool-call cap, never past the logical call's deadline. const { timeoutMs: timeout, maxTotalMs } = resolveMCPRequestDeadline( this.config.timeout, method, params, - toolTimeoutCapMs ?? this.config.toolTimeoutMs, + this.config.toolTimeoutMs, + options?.deadlineAt, ); // scient(SA-CODEX-PLUGINS): end const operation = createMCPTimeout(timeout, this.#operationSignal(callerSignal), maxTotalMs); diff --git a/packages/coding-agent/src/mcp/transports/sse.ts b/packages/coding-agent/src/mcp/transports/sse.ts index ad26d2a4d5..e28be1601b 100644 --- a/packages/coding-agent/src/mcp/transports/sse.ts +++ b/packages/coding-agent/src/mcp/transports/sse.ts @@ -266,6 +266,7 @@ export class LegacySseTransport implements MCPTransport { method, params, this.#config.toolTimeoutMs, + options?.deadlineAt, ); // scient(SA-CODEX-PLUGINS): end const operation = createMCPTimeout(timeout, this.#operationSignal(options?.signal), maxTotalMs); diff --git a/packages/coding-agent/src/mcp/transports/stdio.ts b/packages/coding-agent/src/mcp/transports/stdio.ts index be1b1b6f94..57981cb8e8 100644 --- a/packages/coding-agent/src/mcp/transports/stdio.ts +++ b/packages/coding-agent/src/mcp/transports/stdio.ts @@ -830,6 +830,7 @@ export class StdioTransport implements MCPTransport { method, params, this.config.toolTimeoutMs, + options?.deadlineAt, ); // scient(SA-CODEX-PLUGINS): end const startedAt = Date.now(); diff --git a/packages/coding-agent/src/mcp/types.ts b/packages/coding-agent/src/mcp/types.ts index 313faed673..102b61983f 100644 --- a/packages/coding-agent/src/mcp/types.ts +++ b/packages/coding-agent/src/mcp/types.ts @@ -316,6 +316,13 @@ export interface MCPRequestOptions { * `notifications/progress` while pending; each one restarts a default tool-call deadline (never a configured one). */ onProgress?: (progress: MCPProgress) => void; + // scient(SA-CODEX-PLUGINS): begin - one deadline for a logical tool call across reconnects and retries. + /** + * Epoch milliseconds a capped `tools/call` (a server's `toolTimeoutMs`) must end by, so a retry or a + * replay after reconnecting gets only what is left of the cap. + */ + deadlineAt?: number; + // scient(SA-CODEX-PLUGINS): end } /** Transport interface - abstracts stdio/http */ diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 5db6204114..c8e92c0866 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -1056,15 +1056,26 @@ async function mapLegacyServer( ["callback_url", "callbackUrl", "redirectUri"], ] as const) { const value = oauthTable[snake] ?? oauthTable[camel]; - if (typeof value === "string" && value) oauth[target] = value; + if (typeof value !== "string") continue; + // An empty client secret is kept for the check below, as Codex rejects it; an empty ID or URL means none. + if (value || target === "clientSecret") oauth[target] = value; } - // Codex refuses a client secret without a client ID or of only whitespace, and holds a confidential client's - // sign-in to the client ID exactly as written, which Scient Agent's sign-in would trim. + // Codex refuses a client secret without a client ID or of only whitespace, and the enterprise sign-in + // settings on any server but an ema_auth one, which is left out above. if (oauth.clientSecret !== undefined && (oauth.clientId === undefined || !oauth.clientSecret.trim())) { return { error: "its OAuth client_secret has no client_id or is blank, so Codex refuses to start it" }; } - if (oauth.clientId !== undefined && oauth.clientId !== oauth.clientId.trim()) { - return { error: "its OAuth client_id has surrounding whitespace, which Scient Agent's sign-in would not keep" }; + const enterprise = ["authorization_server_issuer", "token_endpoint_auth_method"].find( + key => oauthTable[key] != null, + ); + if (enterprise !== undefined) + return { error: `its OAuth ${enterprise} needs ema_auth, so Codex refuses to start it` }; + // Codex holds a confidential client's sign-in to its client ID and secret exactly as written, which + // Scient Agent's sign-in would trim. + if ([oauth.clientId, oauth.clientSecret].some(value => value !== undefined && value !== value.trim())) { + return { + error: "its OAuth client_id or client_secret has surrounding whitespace, which Scient Agent's sign-in would not keep", + }; } const port = oauthTable.callback_port ?? oauthTable.callbackPort; if (Number.isInteger(port) && Number(port) > 0) oauth.callbackPort = Number(port); diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts index 16e08529df..aed1fc0089 100644 --- a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -161,7 +161,7 @@ describe("an imported Codex plugin server's own sign-in", () => { it("asks for a fresh sign-in once the plugin configures another confidential OAuth client", async () => { const storage = freshStorage(); const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); - const stored: oauthFlow.MCPStoredOAuthCredential = { ...token, clientId: "client-a" }; + const stored: oauthFlow.MCPStoredOAuthCredential = { ...token, clientId: "client-a", clientSecret: "s" }; await storage.credentials.set(credentialId, stored); const withClient = (clientId: string, clientSecret?: string): MCPServerConfig => ({ ...boundConfig(), @@ -171,6 +171,8 @@ describe("an imported Codex plugin server's own sign-in", () => { // Client A's token is neither sent nor refreshed for client B, nor for an ID that differs only in spaces. expect(lookupMcpOAuthCredential(storage, withClient("client-b", "s"))).toBeUndefined(); expect(lookupMcpOAuthCredential(storage, withClient("client-a ", "s"))).toBeUndefined(); + // Refresh would send the stored secret, so a sign-in made with another secret is not used either. + expect(lookupMcpOAuthCredential(storage, withClient("client-a", "rotated"))).toBeUndefined(); // Codex holds only a confidential client (one with a secret) to its ID. expect(lookupMcpOAuthCredential(storage, withClient("client-b"))?.credentialId).toBe(credentialId); // A server with no sign-in of its own is looked up as before, whatever its client settings hold. diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 3ef0b9bd8a..6862d95b3b 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -8,9 +8,12 @@ import type { MCPServer } from "@oh-my-pi/pi-coding-agent/capability/mcp"; import type { Skill } from "@oh-my-pi/pi-coding-agent/capability/skill"; import { cfgEnabledProviders } from "@oh-my-pi/pi-coding-agent/config/model-settings"; import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; +import { MCPTransportError } from "@oh-my-pi/pi-coding-agent/mcp/errors"; import { resolveMCPRequestDeadline } from "@oh-my-pi/pi-coding-agent/mcp/timeout"; +import { MCPTool } from "@oh-my-pi/pi-coding-agent/mcp/tool-bridge"; import { HttpTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/http"; import { StdioTransport } from "@oh-my-pi/pi-coding-agent/mcp/transports/stdio"; +import type { MCPRequestOptions, MCPServerConnection, MCPTransport } from "@oh-my-pi/pi-coding-agent/mcp/types"; import * as piUtils from "@oh-my-pi/pi-utils"; import { restoreEnvValue } from "../helpers/settings-test-state"; import "@oh-my-pi/pi-coding-agent/discovery"; @@ -275,6 +278,11 @@ describe("installed Codex plugins", () => { spacedclient: { url: "https://mcp.example.test/d", oauth: { client_id: " id ", client_secret: "s" } }, // Codex refuses a client secret without a client ID. secretonly: { url: "https://mcp.example.test/e", oauth: { client_secret: "s" } }, + // Codex rejects an empty secret, which must not pass as no secret at all. + emptysecret: { url: "https://mcp.example.test/f", oauth: { client_id: "id", client_secret: "" } }, + spacedsecret: { url: "https://mcp.example.test/g", oauth: { client_id: "id", client_secret: " s" } }, + // Enterprise sign-in settings Codex allows only with ema_auth. + issuer: { url: "https://mcp.example.test/h", oauth: { authorization_server_issuer: "https://idp.test" } }, }); const { names, warnings } = await limits(); expect(names.sort()).toEqual(["limits@market:cased", "limits@market:kept", "limits@market:shown"]); @@ -287,6 +295,17 @@ describe("installed Codex plugins", () => { expect(warnings).toContainEqual( expect.stringContaining('"secretonly" of limits@market: its OAuth client_secret has no client_id'), ); + expect(warnings).toContainEqual( + expect.stringContaining('"emptysecret" of limits@market: its OAuth client_secret'), + ); + expect(warnings).toContainEqual( + expect.stringContaining( + '"spacedsecret" of limits@market: its OAuth client_id or client_secret has surrounding', + ), + ); + expect(warnings).toContainEqual( + expect.stringContaining('"issuer" of limits@market: its OAuth authorization_server_issuer needs ema_auth'), + ); expect(warnings).toContainEqual( expect.stringContaining('"unsetbearer" of limits@market: its bearer_token_env_var CODEX_PLUGIN_TEST_UNSET'), ); @@ -363,6 +382,59 @@ describe("installed Codex plugins", () => { } }); + it("replays a tool call after reconnecting only within the call's own deadline", async () => { + const deadlines: (number | undefined)[] = []; + const transport = (respond: () => Promise): MCPTransport => ({ + connected: true, + request: (async (_method: string, _params: unknown, options?: MCPRequestOptions) => { + deadlines.push(options?.deadlineAt); + return respond(); + }) as MCPTransport["request"], + async notify() {}, + async close() {}, + }); + const connection = (respond: () => Promise): MCPServerConnection => ({ + name: "tools@market:api", + config: { type: "http", url: "https://mcp.example.test/mcp", toolTimeoutMs: 300_000 }, + transport: transport(respond), + serverInfo: { name: "api", version: "1" }, + capabilities: { tools: {} }, + }); + const unavailable = new MCPTransportError({ + transport: "http", + stage: "receive", + failure: "http_status", + message: "HTTP 503: busy", + retryable: true, + code: 503, + }); + const definition = { name: "run", inputSchema: { type: "object" as const } }; + const ctx = {} as Parameters[3]; + // A 503 reconnects and replays the call: the replay keeps the first attempt's deadline. + const replayed = new MCPTool( + connection(async () => Promise.reject(unavailable)), + definition, + async () => connection(async () => ({ content: [{ type: "text", text: "ok" }] })), + ); + await replayed.execute("call", {}, () => {}, ctx); + expect(deadlines).toHaveLength(2); + expect(deadlines[0]).toBeNumber(); + expect(deadlines[1]).toBe(deadlines[0]); + // A reconnect still pending at the deadline ends the call with its own error, never a replay. + deadlines.length = 0; + const stalled = new MCPTool( + { + ...connection(async () => Promise.reject(unavailable)), + config: { type: "http", url: "https://x.test/", toolTimeoutMs: 1 }, + }, + definition, + () => new Promise(() => {}), + ); + const result = await stalled.execute("call", {}, () => {}, ctx); + expect(result.isError).toBe(true); + expect(deadlines).toHaveLength(1); + }); + it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { await installLimits( { denied: { command: "node", args: ["denied.mjs"] }, open: { command: "node", args: ["open.mjs"] } }, diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 5d688ef163..a961a5062d 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress, and in mcp/transports/http.ts spanning the sign-in refresh and the retry after a 401 or 403), and a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource; a client secret without a client ID or of only whitespace, which Codex refuses, or a client ID with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in otherwise; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it; reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one deadline (MCPRequestOptions.deadlineAt) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts), a reconnect still pending at the deadline ending the call with its own error; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or is empty or only whitespace, or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; and when its client ID or secret has surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID and that secret (Codex asks for a fresh sign-in on another client ID, and refresh sends the stored secret, so a sign-in made with another secret is not used either; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it; reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", @@ -537,6 +537,7 @@ "packages/coding-agent/src/mcp/oauth-flow.ts", "packages/coding-agent/src/mcp/oauth-reauth.ts", "packages/coding-agent/src/mcp/timeout.ts", + "packages/coding-agent/src/mcp/tool-bridge.ts", "packages/coding-agent/src/mcp/transports/http.ts", "packages/coding-agent/src/mcp/transports/sse.ts", "packages/coding-agent/src/mcp/transports/stdio.ts", From 6416142d506c3726ce22ecc9ef48555c717252eb Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 14:26:43 +0200 Subject: [PATCH 17/30] fix(codex-plugins): no tool-call deadline for a connection without config - Hosts and tests hand the tool bridge connections without config; toolCallDeadline read connection.config unconditionally and threw (CI: tool-bridge-structured-content). --- packages/coding-agent/src/mcp/tool-bridge.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/coding-agent/src/mcp/tool-bridge.ts b/packages/coding-agent/src/mcp/tool-bridge.ts index ff7e979056..61d76cd521 100644 --- a/packages/coding-agent/src/mcp/tool-bridge.ts +++ b/packages/coding-agent/src/mcp/tool-bridge.ts @@ -452,7 +452,8 @@ function getMcpAuthChallenge(result: MCPToolCallResult): MCPAuthChallenge | unde // (an imported Codex plugin's tool_timeout_sec), carried through reconnecting and replaying it. /** When a logical tool call to this connection's server must end, if its server caps tool calls. */ function toolCallDeadline(connection: MCPServerConnection): number | undefined { - const capMs = connection.config.toolTimeoutMs; + // Hosts and tests may hand the bridge a connection without its config: no cap then. + const capMs = connection.config?.toolTimeoutMs; return capMs === undefined ? undefined : Date.now() + capMs; } From e34e47e8f68d55dea04a7712253e849d80758222 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 14:36:49 +0200 Subject: [PATCH 18/30] fix(codex-plugins): Codex alias and blank rules, current-secret refresh, monotonic tool deadline - OAuth aliases fold by key presence before type checks; blank checks use Rust's Unicode White_Space. - Server-bound sign-ins store no client secret; refresh and re-sign-in send only the secret the current definition configures. - Tool-call deadlines and progress restarts run on performance.now(); no refresh, retry or replay starts once the deadline is spent. --- SCIENT.md | 2 +- packages/coding-agent/src/mcp/manager.ts | 3 + .../coding-agent/src/mcp/oauth-credentials.ts | 27 ++++--- packages/coding-agent/src/mcp/oauth-reauth.ts | 11 ++- packages/coding-agent/src/mcp/timeout.ts | 14 ++-- packages/coding-agent/src/mcp/tool-bridge.ts | 8 ++- .../coding-agent/src/mcp/transports/http.ts | 22 ++++-- .../coding-agent/src/mcp/transports/stdio.ts | 7 +- packages/coding-agent/src/mcp/types.ts | 4 +- .../coding-agent/src/scient/codex-plugins.ts | 58 +++++++++++---- .../test/scient/codex-plugins-oauth.test.ts | 72 ++++++++++++++++++- .../test/scient/codex-plugins.test.ts | 39 +++++++++- scripts/scient/divergences.json | 2 +- 13 files changed, 222 insertions(+), 47 deletions(-) diff --git a/SCIENT.md b/SCIENT.md index 786a3f9293..be1349a8b6 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -15,7 +15,7 @@ Everything else is upstream's code and behavior. | State | The config root is `~/.scient-agent` (project folder `.scient-agent/`), or the absolute directory a host sets in `SCIENT_AGENT_ROOT`. User config lookups, the native addon cache, browser storage state and crash logs follow it. A host's root is authoritative: `SCIENT_AGENT_DIR` is ignored under it, and a `.env` in the project or the home directory cannot set any `SCIENT_AGENT_*` variable (a `.env` inside the root can). The root is taken only from the launch environment, never from a `.env`. Standalone use keeps upstream's `.env` overrides. | | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | -| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec`, or Codex's five-minute default, whatever the call asks to wait and across a sign-in refresh, a reconnect and a replay of the same call (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which only a definition the `codex-plugins` source supplies can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made for another confidential client ID or secret than the plugin now configures (compared exactly) is not used, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. OAuth settings Codex refuses (a client secret without a client ID, or empty or blank; `authorization_server_issuer` or `token_endpoint_auth_method` without `ema_auth`) or a client ID or secret with surrounding whitespace skip the server. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec`, or Codex's five-minute default, on a clock that never runs backwards, whatever the call asks to wait and across a sign-in refresh, a reconnect and a replay of the same call (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which only a definition the `codex-plugins` source supplies can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made for another confidential client ID than the plugin now configures (compared exactly) is not used, the sign-in keeps no client secret, so refreshing and re-signing in send only the secret the plugin configures now, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. Its OAuth settings are read as Codex reads them (camelCase aliases giving way to snake_case keys); settings Codex refuses (a client secret without a client ID, or either of them blank; `authorization_server_issuer` or `token_endpoint_auth_method` without `ema_auth`) or a client ID or secret with surrounding whitespace skip the server. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/src/mcp/manager.ts b/packages/coding-agent/src/mcp/manager.ts index f42932db0c..a3372817cf 100644 --- a/packages/coding-agent/src/mcp/manager.ts +++ b/packages/coding-agent/src/mcp/manager.ts @@ -2379,6 +2379,9 @@ export class MCPManager implements MCPManagerApi { refreshStoredManagedMcpOAuthCredential(authStorage, credentialId, { serverUrl: config.type === "http" || config.type === "sse" ? config.url : undefined, auth, + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in refreshes with the secret configured now. + clientSecret: typeof config.oauth?.clientSecret === "string" ? config.oauth.clientSecret : undefined, + // scient(SA-CODEX-PLUGINS): end forceRefresh: opts?.forceRefresh, keepCredentialOnRefreshFailure: true, onRefreshFailure: refreshError => { diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index 6ed3122cfb..4a69f5d00d 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -96,16 +96,11 @@ export function lookupMcpOAuthCredential( } // scient(SA-CODEX-PLUGINS): begin - a server's own sign-in made for another confidential client than the one // it now configures is not used or refreshed: Codex compares the exact IDs and asks for a fresh sign-in. - // Refresh reads its client from the stored sign-in, so one stored with another secret is not used either: - // it would refresh with a secret the plugin no longer configures. + // Its secret is never stored: refresh sends the one the definition configures now. const lookup = lookupMcpOAuthCredentialForServer(authStorage, auth, config.url); if (!lookup || !isMcpOAuthServerCredentialId(lookup.credentialId)) return lookup; const { clientId, clientSecret } = config.oauth ?? {}; - if ( - typeof clientSecret === "string" && - typeof clientId === "string" && - (lookup.credential.clientId !== clientId || lookup.credential.clientSecret !== clientSecret) - ) { + if (typeof clientSecret === "string" && typeof clientId === "string" && lookup.credential.clientId !== clientId) { return undefined; } return lookup; @@ -201,6 +196,10 @@ export async function refreshStoredManagedMcpOAuthCredential( forceRefresh?: boolean; keepCredentialOnRefreshFailure?: boolean; onRefreshFailure?: (error: unknown) => void; + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in's secret comes from its current definition. + /** For a server-bound sign-in, the client secret its definition configures now; a stored one is never sent. */ + clientSecret?: string; + // scient(SA-CODEX-PLUGINS): end } = {}, ): Promise> { const row = authStorage.credentials @@ -212,7 +211,13 @@ export async function refreshStoredManagedMcpOAuthCredential( if (row?.credential.type !== "oauth") { return { credential: undefined, refreshed: false, removed: false }; } + // scient(SA-CODEX-PLUGINS): begin - like Codex, a server-bound sign-in refreshes with the stored client ID and + // the secret configured now, or none; it never sends or keeps a stored secret. + const bound = isMcpOAuthServerCredentialId(provider); + const withCurrentSecret = (credential: MCPStoredOAuthCredential): MCPStoredOAuthCredential => + bound ? { ...credential, clientSecret: opts.clientSecret } : credential; const observedCredential: MCPStoredOAuthCredential = row.credential; + // scient(SA-CODEX-PLUGINS): end const serverUrl = opts.serverUrl ?? (opts.recoverServerUrlFromCredentialId ? mcpOAuthServerUrlFromCredentialId(provider) : undefined); @@ -226,14 +231,16 @@ export async function refreshStoredManagedMcpOAuthCredential( const material = selectMcpOAuthRefreshMaterial(current, opts.auth); return Boolean(current.refresh && material?.tokenUrl); }, + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in refreshes with the secret configured now. refresh: (current, signal) => current.refresh === REMOTE_REFRESH_SENTINEL ? refreshBrokeredMcpOAuthCredential(authStorage, row.id, provider, signal) - : refreshManagedMcpOAuthCredential(current, { + : refreshManagedMcpOAuthCredential(withCurrentSecret(current), { serverUrl, auth: opts.auth, signal, }), + // scient(SA-CODEX-PLUGINS): end mergeRefreshedCredential: (current, refreshed) => { const material = selectMcpOAuthRefreshMaterial(current, opts.auth); const resourceIsFallback = !material?.resource && Boolean(serverUrl); @@ -242,7 +249,9 @@ export async function refreshStoredManagedMcpOAuthCredential( ...refreshed, tokenUrl: material?.tokenUrl, clientId: material?.clientId, - clientSecret: material?.clientSecret, + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in keeps no secret. + clientSecret: bound ? undefined : material?.clientSecret, + // scient(SA-CODEX-PLUGINS): end resource: resourceIsFallback ? undefined : material?.resource, authorizationUrl: material && "authorizationUrl" in material ? material.authorizationUrl : undefined, }; diff --git a/packages/coding-agent/src/mcp/oauth-reauth.ts b/packages/coding-agent/src/mcp/oauth-reauth.ts index 4d8ea01665..420e61e58d 100644 --- a/packages/coding-agent/src/mcp/oauth-reauth.ts +++ b/packages/coding-agent/src/mcp/oauth-reauth.ts @@ -627,7 +627,12 @@ export async function runMcpOAuthFlow(opts: McpOAuthFlowRunOptions): Promise { if (cleared || timerFired || callerAborted || timeoutId === undefined) return; - const delay = Math.min(timeoutMs, startedAt + maxTotalMs - Date.now()); + // scient(SA-CODEX-PLUGINS): begin - the monotonic clock. + const delay = Math.min(timeoutMs, startedAt + maxTotalMs - performance.now()); + // scient(SA-CODEX-PLUGINS): end if (delay <= 0) return; clearTimeout(timeoutId); timeoutId = setTimeout(onDeadline, delay); diff --git a/packages/coding-agent/src/mcp/tool-bridge.ts b/packages/coding-agent/src/mcp/tool-bridge.ts index 61d76cd521..0090cff26f 100644 --- a/packages/coding-agent/src/mcp/tool-bridge.ts +++ b/packages/coding-agent/src/mcp/tool-bridge.ts @@ -454,7 +454,7 @@ function getMcpAuthChallenge(result: MCPToolCallResult): MCPAuthChallenge | unde function toolCallDeadline(connection: MCPServerConnection): number | undefined { // Hosts and tests may hand the bridge a connection without its config: no cap then. const capMs = connection.config?.toolTimeoutMs; - return capMs === undefined ? undefined : Date.now() + capMs; + return capMs === undefined ? undefined : performance.now() + capMs; } async function callToolWithAuthRetry( @@ -511,9 +511,11 @@ async function reconnectWithAbort( if (deadlineAt === undefined) return await pending; void pending.catch(() => {}); const { promise: expired, resolve } = Promise.withResolvers(); - const timer = setTimeout(() => resolve(null), Math.max(0, deadlineAt - Date.now())); + const timer = setTimeout(() => resolve(null), Math.max(0, deadlineAt - performance.now())); try { - return await Promise.race([pending, expired]); + const connection = await Promise.race([pending, expired]); + // A reconnect that lands at or past the deadline gets no replay. + return performance.now() < deadlineAt ? connection : null; } finally { clearTimeout(timer); } diff --git a/packages/coding-agent/src/mcp/transports/http.ts b/packages/coding-agent/src/mcp/transports/http.ts index 6bb2285e25..62d748ca33 100644 --- a/packages/coding-agent/src/mcp/transports/http.ts +++ b/packages/coding-agent/src/mcp/transports/http.ts @@ -428,7 +428,7 @@ export class HttpTransport implements MCPTransport { const deadlineAt = capMs === undefined ? undefined - : Math.min(options?.deadlineAt ?? Number.POSITIVE_INFINITY, Date.now() + capMs); + : Math.min(options?.deadlineAt ?? Number.POSITIVE_INFINITY, performance.now() + capMs); const callOptions = deadlineAt === undefined ? options : { ...options, deadlineAt }; // scient(SA-CODEX-PLUGINS): end try { @@ -446,10 +446,18 @@ export class HttpTransport implements MCPTransport { const newHeaders = capMs === undefined || deadlineAt === undefined ? await this.onAuthError() - : await this.#beforeToolDeadline(this.onAuthError(), capMs, deadlineAt); + : await this.#beforeToolDeadline( + () => this.onAuthError?.() ?? Promise.resolve(null), + capMs, + deadlineAt, + ); if (newHeaders) { // Persist refreshed headers so subsequent requests use them directly this.config = { ...this.config, headers: newHeaders }; + // A deadline spent during the refresh sends no retry. + if (capMs !== undefined && deadlineAt !== undefined && performance.now() >= deadlineAt) { + throw toolTimeoutError(capMs); + } return this.#executeRequest(method, params, callOptions); } // scient(SA-CODEX-PLUGINS): end @@ -458,11 +466,13 @@ export class HttpTransport implements MCPTransport { } } - // scient(SA-CODEX-PLUGINS): begin - work a capped tool call waits on, ended by the call's deadline. - async #beforeToolDeadline(work: Promise, capMs: number, deadlineAt: number): Promise { - void work.catch(() => {}); - const leftMs = deadlineAt - Date.now(); + // scient(SA-CODEX-PLUGINS): begin - work a capped tool call waits on, started only while time is left and + // ended by the call's monotonic deadline. + async #beforeToolDeadline(start: () => Promise, capMs: number, deadlineAt: number): Promise { + const leftMs = deadlineAt - performance.now(); if (leftMs <= 0) throw toolTimeoutError(capMs); + const work = start(); + void work.catch(() => {}); const operation = createMCPTimeout(leftMs); try { return await untilAborted(operation.signal, work); diff --git a/packages/coding-agent/src/mcp/transports/stdio.ts b/packages/coding-agent/src/mcp/transports/stdio.ts index 57981cb8e8..3dbd860ea2 100644 --- a/packages/coding-agent/src/mcp/transports/stdio.ts +++ b/packages/coding-agent/src/mcp/transports/stdio.ts @@ -832,8 +832,9 @@ export class StdioTransport implements MCPTransport { this.config.toolTimeoutMs, options?.deadlineAt, ); + // The monotonic clock, so setting the wall clock back never stretches a hard cap. + const startedAt = performance.now(); // scient(SA-CODEX-PLUGINS): end - const startedAt = Date.now(); const signal = options?.signal; if (signal?.aborted) { @@ -900,7 +901,9 @@ export class StdioTransport implements MCPTransport { // Progress restarts the deadline, never past the request's hard cap. const armTimer = (): void => { - const delay = Math.min(timeout, startedAt + maxTotalMs - Date.now()); + // scient(SA-CODEX-PLUGINS): begin - the monotonic clock. + const delay = Math.min(timeout, startedAt + maxTotalMs - performance.now()); + // scient(SA-CODEX-PLUGINS): end if (delay <= 0) return; const limit = delay < timeout ? maxTotalMs : timeout; if (timer) clearTimeout(timer); diff --git a/packages/coding-agent/src/mcp/types.ts b/packages/coding-agent/src/mcp/types.ts index 102b61983f..40db436b62 100644 --- a/packages/coding-agent/src/mcp/types.ts +++ b/packages/coding-agent/src/mcp/types.ts @@ -318,8 +318,8 @@ export interface MCPRequestOptions { onProgress?: (progress: MCPProgress) => void; // scient(SA-CODEX-PLUGINS): begin - one deadline for a logical tool call across reconnects and retries. /** - * Epoch milliseconds a capped `tools/call` (a server's `toolTimeoutMs`) must end by, so a retry or a - * replay after reconnecting gets only what is left of the cap. + * Monotonic time (`performance.now()` milliseconds) a capped `tools/call` (a server's `toolTimeoutMs`) + * must end by, so a retry or a replay after reconnecting gets only what is left of the cap. */ deadlineAt?: number; // scient(SA-CODEX-PLUGINS): end diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index c8e92c0866..044c03906b 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -960,15 +960,40 @@ function envVarNames(value: unknown): string[] | null { type Mapped = { server: Partial } | { error: string }; +/** Codex's camelCase OAuth aliases, each giving way to its snake_case twin when that key is present, even null. */ +const OAUTH_ALIASES = [ + ["callbackUrl", "callback_url"], + ["callbackPort", "callback_port"], + ["clientId", "client_id"], + ["clientSecret", "client_secret"], +] as const; + +/** A plugin's OAuth table as Codex reads it: aliases folded by key presence before any type is checked. */ +function codexOAuthTable(oauth: Record): Record { + const table = { ...oauth }; + for (const [camel, snake] of OAUTH_ALIASES) { + if (!(camel in table)) continue; + if (!(snake in table)) table[snake] = table[camel]; + delete table[camel]; + } + return table; +} + +/** Blank as Rust's `str::trim` sees it (Unicode White_Space, U+0085 included and U+FEFF not). */ +function isCodexBlank(value: string): boolean { + return !value.replace(/\p{White_Space}+/gu, ""); +} + /** * A legacy plugin's server as Codex reads it: exactly one transport with its own fields, package * paths kept inside the package, values taken literally, and only Codex's variables inherited. */ async function mapLegacyServer( plugin: CodexPlugin, - server: Record, + declared: Record, env: Record, ): Promise { + const server = isRecord(declared.oauth) ? { ...declared, oauth: codexOAuthTable(declared.oauth) } : declared; const mistyped = Object.keys(SERVER_FIELD_TYPES).find( key => server[key] != null && !SERVER_FIELD_TYPES[key](server[key]), ); @@ -1047,37 +1072,44 @@ async function mapLegacyServer( }; } } - // Codex's OAuth client settings, snake_case or the camelCase plugins may use. + // Codex's OAuth client settings, their camelCase aliases already folded in. const oauthTable = (server.oauth ?? {}) as Record; const oauth: NonNullable = {}; - for (const [snake, camel, target] of [ - ["client_id", "clientId", "clientId"], - ["client_secret", "clientSecret", "clientSecret"], - ["callback_url", "callbackUrl", "redirectUri"], + for (const [key, target] of [ + ["client_id", "clientId"], + ["client_secret", "clientSecret"], + ["callback_url", "redirectUri"], ] as const) { - const value = oauthTable[snake] ?? oauthTable[camel]; + const value = oauthTable[key]; if (typeof value !== "string") continue; // An empty client secret is kept for the check below, as Codex rejects it; an empty ID or URL means none. if (value || target === "clientSecret") oauth[target] = value; } - // Codex refuses a client secret without a client ID or of only whitespace, and the enterprise sign-in + // Codex refuses a client secret without a client ID, or either of them blank, and the enterprise sign-in // settings on any server but an ema_auth one, which is left out above. - if (oauth.clientSecret !== undefined && (oauth.clientId === undefined || !oauth.clientSecret.trim())) { - return { error: "its OAuth client_secret has no client_id or is blank, so Codex refuses to start it" }; + if ( + oauth.clientSecret !== undefined && + (oauth.clientId === undefined || isCodexBlank(oauth.clientId) || isCodexBlank(oauth.clientSecret)) + ) { + return { + error: "its OAuth client_secret has no client_id or one of them is blank, so Codex refuses to start it", + }; } + // Without a secret, Codex takes a blank client ID for none. + if (oauth.clientId !== undefined && isCodexBlank(oauth.clientId)) delete oauth.clientId; const enterprise = ["authorization_server_issuer", "token_endpoint_auth_method"].find( key => oauthTable[key] != null, ); if (enterprise !== undefined) return { error: `its OAuth ${enterprise} needs ema_auth, so Codex refuses to start it` }; - // Codex holds a confidential client's sign-in to its client ID and secret exactly as written, which - // Scient Agent's sign-in would trim. + // Codex holds a confidential client's sign-in to its client ID exactly as written and sends its secret as + // written, both of which Scient Agent's sign-in would trim. if ([oauth.clientId, oauth.clientSecret].some(value => value !== undefined && value !== value.trim())) { return { error: "its OAuth client_id or client_secret has surrounding whitespace, which Scient Agent's sign-in would not keep", }; } - const port = oauthTable.callback_port ?? oauthTable.callbackPort; + const port = oauthTable.callback_port; if (Number.isInteger(port) && Number(port) > 0) oauth.callbackPort = Number(port); // Codex asks for the configured scopes as they are, an empty list meaning none. if (isStringList(server.scopes)) oauth.scope = server.scopes.join(" "); diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts index aed1fc0089..fbdeae5486 100644 --- a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -11,6 +11,7 @@ import { MCPManager } from "@oh-my-pi/pi-coding-agent/mcp/manager"; import { lookupMcpOAuthCredential, lookupMcpOAuthCredentialForServer, + refreshStoredManagedMcpOAuthCredential, } from "@oh-my-pi/pi-coding-agent/mcp/oauth-credentials"; import * as oauthFlow from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow"; import { @@ -113,6 +114,42 @@ describe("an imported Codex plugin server's own sign-in", () => { expect(await Bun.file(path.join(temp.path(), "mcp.json")).exists()).toBe(false); }); + it("signs in again with the secret configured now, never one stored with an earlier sign-in", async () => { + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const storage = freshStorage(); + // An earlier build's sign-in kept the secret the plugin has since removed. + await storage.credentials.set(credentialId, { + ...token, + tokenUrl: "https://auth.example.test/token", + clientId: "client-a", + clientSecret: "retired", + } as oauthFlow.MCPStoredOAuthCredential); + vi.spyOn(mcpClient, "connectToServer").mockRejectedValue( + new Error( + 'HTTP 401: {"authorization_url":"https://auth.example.test/authorize","token_url":"https://auth.example.test/token"}', + ), + ); + const flows: { clientSecret?: string }[] = []; + vi.spyOn(oauthFlow.MCPOAuthFlow.prototype, "login").mockImplementation( + async function (this: { config: { clientSecret?: string } }) { + flows.push(this.config); + return { access: "fresh", refresh: "fresh-refresh", expires: Date.now() + 3_600_000 }; + }, + ); + using temp = TempDir.createSync("@scient-codex-plugins-resign-"); + const config: MCPServerConfig = { ...boundConfig(), oauth: { clientId: "client-a" } }; + await reauthorizeMcpServer( + NAME, + { filePath: path.join(temp.path(), "mcp.json"), scope: "user", config, discovered: true }, + { authStorage: storage, silent: true }, + ); + expect(flows).toHaveLength(1); + expect(flows[0]?.clientSecret).toBeUndefined(); + // The new sign-in keeps no secret either. + expect(storage.credentials.get(credentialId)).toMatchObject({ access: "fresh", clientId: "client-a" }); + expect(storage.credentials.get(credentialId)).not.toHaveProperty("clientSecret", expect.anything()); + }); + it("reconnects on its own sign-in, and never stands in for another entry at its URL", async () => { const updateServerConfig = vi.fn(() => undefined); const manager = createMcpManagerStub({ @@ -171,8 +208,8 @@ describe("an imported Codex plugin server's own sign-in", () => { // Client A's token is neither sent nor refreshed for client B, nor for an ID that differs only in spaces. expect(lookupMcpOAuthCredential(storage, withClient("client-b", "s"))).toBeUndefined(); expect(lookupMcpOAuthCredential(storage, withClient("client-a ", "s"))).toBeUndefined(); - // Refresh would send the stored secret, so a sign-in made with another secret is not used either. - expect(lookupMcpOAuthCredential(storage, withClient("client-a", "rotated"))).toBeUndefined(); + // The secret is not part of the match: refresh sends the one configured now (tested below). + expect(lookupMcpOAuthCredential(storage, withClient("client-a", "rotated"))?.credentialId).toBe(credentialId); // Codex holds only a confidential client (one with a secret) to its ID. expect(lookupMcpOAuthCredential(storage, withClient("client-b"))?.credentialId).toBe(credentialId); // A server with no sign-in of its own is looked up as before, whatever its client settings hold. @@ -187,6 +224,37 @@ describe("an imported Codex plugin server's own sign-in", () => { expect(lookupMcpOAuthCredential(storage, ordinary(7))?.credentialId).toBe(urlKeyed); }); + it("refreshes with the secret the plugin configures now, never a stored or retired one", async () => { + const storage = freshStorage(); + const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); + const sent: (string | null)[] = []; + vi.spyOn(globalThis, "fetch").mockImplementation((async (_url: string, init?: RequestInit) => { + sent.push(new URLSearchParams(String(init?.body)).get("client_secret")); + return Response.json({ access_token: "fresh", refresh_token: "r2", expires_in: 3600 }); + }) as never); + const stored = (): oauthFlow.MCPStoredOAuthCredential => ({ + ...token, + tokenUrl: "https://auth.example.test/token", + clientId: "client-a", + // A row an earlier build kept with the secret the plugin has since retired. + clientSecret: "retired", + }); + await storage.credentials.set(credentialId, stored()); + await refreshStoredManagedMcpOAuthCredential(storage, credentialId, { + clientSecret: "current", + forceRefresh: true, + }); + // The plugin no longer configures a secret: none is sent, as in Codex. + await storage.credentials.set(credentialId, stored()); + const { credential } = await refreshStoredManagedMcpOAuthCredential(storage, credentialId, { + forceRefresh: true, + }); + expect(sent).toEqual(["current", null]); + // Nothing keeps a secret for the next refresh. + expect(credential?.clientSecret).toBeUndefined(); + expect(storage.credentials.get(credentialId)).not.toHaveProperty("clientSecret", expect.anything()); + }); + it("binds a definition only by the source supplied with it, never one left registered", async () => { const storage = freshStorage(); const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index 6862d95b3b..a6f5ddabe8 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -283,6 +283,15 @@ describe("installed Codex plugins", () => { spacedsecret: { url: "https://mcp.example.test/g", oauth: { client_id: "id", client_secret: " s" } }, // Enterprise sign-in settings Codex allows only with ema_auth. issuer: { url: "https://mcp.example.test/h", oauth: { authorization_server_issuer: "https://idp.test" } }, + // Codex folds camelCase aliases in by key presence: a null snake_case ID still wins over clientId, + // leaving the secret without an ID; a numeric clientSecret alias fails the type check. + nullsnake: { + url: "https://mcp.example.test/i", + oauth: { client_id: null, clientId: "id", client_secret: "s" }, + }, + numericalias: { url: "https://mcp.example.test/j", oauth: { client_id: "id", clientSecret: 7 } }, + // Rust trims U+0085 (NEL) as whitespace, so Codex finds this secret blank. + nelsecret: { url: "https://mcp.example.test/k", oauth: { client_id: "id", client_secret: "\u0085" } }, }); const { names, warnings } = await limits(); expect(names.sort()).toEqual(["limits@market:cased", "limits@market:kept", "limits@market:shown"]); @@ -303,6 +312,9 @@ describe("installed Codex plugins", () => { '"spacedsecret" of limits@market: its OAuth client_id or client_secret has surrounding', ), ); + expect(warnings).toContainEqual(expect.stringContaining('"nullsnake" of limits@market: its OAuth client_secret')); + expect(warnings).toContainEqual(expect.stringContaining('"numericalias" of limits@market: invalid oauth')); + expect(warnings).toContainEqual(expect.stringContaining('"nelsecret" of limits@market: its OAuth client_secret')); expect(warnings).toContainEqual( expect.stringContaining('"issuer" of limits@market: its OAuth authorization_server_issuer needs ema_auth'), ); @@ -369,14 +381,22 @@ describe("installed Codex plugins", () => { // A refresh that never ends still ends the call at the cap. expect(await call(() => new Promise(() => {}))).toBe("Request timeout after 300ms"); // A refresh that takes 200 ms of the cap leaves the retry the rest, not a fresh 300 ms. - const realNow = Date.now; + const realNow = performance.now.bind(performance); let skew = 0; - vi.spyOn(Date, "now").mockImplementation(() => realNow() + skew); + vi.spyOn(performance, "now").mockImplementation(() => realNow() + skew); const message = await call(async () => { skew = 200; return { Authorization: "Bearer refreshed" }; }); expect(Number(/Request timeout after (\d+)ms/.exec(message)?.[1])).toBeLessThanOrEqual(100); + // A refresh that ends past the deadline sends no retry at all. + skew = 0; + const late = await call(async () => { + skew = 400; + return { Authorization: "Bearer refreshed" }; + }); + expect(late).toBe("Request timeout after 300ms"); + expect(posts).toBe(1); } finally { server.stop(true); } @@ -433,6 +453,21 @@ describe("installed Codex plugins", () => { const result = await stalled.execute("call", {}, () => {}, ctx); expect(result.isError).toBe(true); expect(deadlines).toHaveLength(1); + // A reconnect that lands past the deadline, before its timer fires, gets no replay either. + deadlines.length = 0; + const realNow = performance.now.bind(performance); + let skew = 0; + vi.spyOn(performance, "now").mockImplementation(() => realNow() + skew); + const late = new MCPTool( + connection(async () => Promise.reject(unavailable)), + definition, + async () => { + skew = 400_000; + return connection(async () => ({ content: [{ type: "text", text: "ok" }] })); + }, + ); + expect((await late.execute("call", {}, () => {}, ctx)).isError).toBe(true); + expect(deadlines).toHaveLength(1); }); it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index a961a5062d..e2165e711b 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one deadline (MCPRequestOptions.deadlineAt) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts), a reconnect still pending at the deadline ending the call with its own error; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or is empty or only whitespace, or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; and when its client ID or secret has surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID and that secret (Codex asks for a fresh sign-in on another client ID, and refresh sends the stored secret, so a sign-in made with another secret is not used either; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it; reauthorization and TUI /mcp unauth act on it only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization (mcp/oauth-reauth.ts) takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one monotonic deadline (MCPRequestOptions.deadlineAt, on performance.now(), the clock createMCPTimeout and the stdio transport also measure progress restarts on) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts); no refresh, retry or replay starts once it is spent, and a reconnect still pending at it or landing past it ends the call with its own error; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. Its oauth table is read as Codex reads a plugin's: the camelCase aliases clientId, clientSecret, callbackUrl and callbackPort fold into their snake_case keys by key presence, a present snake_case key winning even when null, before any type is checked. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or either of them is blank by Rust's Unicode White_Space (an empty secret included), or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; a blank client ID without a secret counts as none; and a client ID or secret with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server too. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in on another one; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it. Like Codex, a server-bound sign-in stores no client secret: the sign-in flow (mcp/oauth-reauth.ts) keeps none, and refresh (refreshStoredManagedMcpOAuthCredential's clientSecret, which the manager fills from the current definition) and reauthorization send the secret the current definition configures, or none, never a stored or retired one. Reauthorization and TUI /mcp unauth act on a bound id only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", From c2452073f8d84095d496b2987b72593fcccc28af Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 14:51:30 +0200 Subject: [PATCH 19/30] fix(codex-plugins): no token-endpoint redirects, broker refresh without stored secrets, spent-deadline sends, Codex server names - Server-bound code exchange and refresh refuse redirects; refreshMCPOAuthToken reads trailing options even without a resource. - refreshManagedMcpOAuthCredential enforces the bound-row secret rule for every caller, the auth broker included. - stdio writes no capped call whose cap ran out before sending; the bridge starts no reconnect once the deadline is spent. - Declared server names Codex's validate_mcp_server_name rejects are skipped. --- .../coding-agent/src/cli/auth-broker-cli.ts | 3 ++ .../coding-agent/src/mcp/oauth-credentials.ts | 27 +++++++++-- packages/coding-agent/src/mcp/oauth-flow.ts | 19 +++++++- packages/coding-agent/src/mcp/oauth-reauth.ts | 3 ++ packages/coding-agent/src/mcp/tool-bridge.ts | 2 + .../coding-agent/src/mcp/transports/stdio.ts | 19 ++++++++ .../coding-agent/src/scient/codex-plugins.ts | 8 ++++ .../test/scient/codex-plugins-oauth.test.ts | 17 +++++-- .../test/scient/codex-plugins.test.ts | 46 +++++++++++++++++++ scripts/scient/divergences.json | 3 +- 10 files changed, 137 insertions(+), 10 deletions(-) diff --git a/packages/coding-agent/src/cli/auth-broker-cli.ts b/packages/coding-agent/src/cli/auth-broker-cli.ts index 0387ae2b09..0c635a5bcc 100644 --- a/packages/coding-agent/src/cli/auth-broker-cli.ts +++ b/packages/coding-agent/src/cli/auth-broker-cli.ts @@ -124,6 +124,9 @@ export function refreshBrokerOAuthCredential( return refreshManagedMcpOAuthCredential(credential, { serverUrl: mcpOAuthServerUrlFromCredentialId(provider), signal, + // scient(SA-CODEX-PLUGINS): begin - a server-bound row refreshes without a stored secret or redirects. + credentialId: provider, + // scient(SA-CODEX-PLUGINS): end }); } // Non-MCP rows: same per-provider path AuthStorage would take by default diff --git a/packages/coding-agent/src/mcp/oauth-credentials.ts b/packages/coding-agent/src/mcp/oauth-credentials.ts index 4a69f5d00d..b08499ec72 100644 --- a/packages/coding-agent/src/mcp/oauth-credentials.ts +++ b/packages/coding-agent/src/mcp/oauth-credentials.ts @@ -133,9 +133,23 @@ export function selectMcpOAuthRefreshMaterial( */ export function refreshManagedMcpOAuthCredential( credential: MCPStoredOAuthCredential, - opts: { serverUrl?: string; auth?: MCPAuthConfig; signal?: AbortSignal } = {}, + // scient(SA-CODEX-PLUGINS): begin - every refresh path (manager, broker, `omp token`) names the row, so a + // server-bound sign-in refreshes as Codex does: with the stored client ID and only the secret its current + // definition configures (none when the caller has no definition), and following no redirect. + opts: { + serverUrl?: string; + auth?: MCPAuthConfig; + signal?: AbortSignal; + credentialId?: string; + clientSecret?: string; + } = {}, ): Promise { - const material = selectMcpOAuthRefreshMaterial(credential, opts.auth); + const bound = isMcpOAuthServerCredentialId(opts.credentialId); + const material = selectMcpOAuthRefreshMaterial( + bound ? { ...credential, clientSecret: opts.clientSecret } : credential, + opts.auth, + ); + // scient(SA-CODEX-PLUGINS): end const tokenUrl = material?.tokenUrl; if (!credential.refresh || !tokenUrl) { throw new Error("MCP OAuth credential is missing refresh material"); @@ -147,6 +161,9 @@ export function refreshManagedMcpOAuthCredential( authorizationUrl, stripSameOriginResource: resourceIsFallback, signal: opts.signal, + // scient(SA-CODEX-PLUGINS): begin - a server-bound refresh follows no redirect; others are unchanged. + ...(bound && { rejectRedirects: true }), + // scient(SA-CODEX-PLUGINS): end }); } @@ -214,8 +231,6 @@ export async function refreshStoredManagedMcpOAuthCredential( // scient(SA-CODEX-PLUGINS): begin - like Codex, a server-bound sign-in refreshes with the stored client ID and // the secret configured now, or none; it never sends or keeps a stored secret. const bound = isMcpOAuthServerCredentialId(provider); - const withCurrentSecret = (credential: MCPStoredOAuthCredential): MCPStoredOAuthCredential => - bound ? { ...credential, clientSecret: opts.clientSecret } : credential; const observedCredential: MCPStoredOAuthCredential = row.credential; // scient(SA-CODEX-PLUGINS): end const serverUrl = @@ -235,10 +250,12 @@ export async function refreshStoredManagedMcpOAuthCredential( refresh: (current, signal) => current.refresh === REMOTE_REFRESH_SENTINEL ? refreshBrokeredMcpOAuthCredential(authStorage, row.id, provider, signal) - : refreshManagedMcpOAuthCredential(withCurrentSecret(current), { + : refreshManagedMcpOAuthCredential(current, { serverUrl, auth: opts.auth, signal, + credentialId: provider, + clientSecret: opts.clientSecret, }), // scient(SA-CODEX-PLUGINS): end mergeRefreshedCredential: (current, refreshed) => { diff --git a/packages/coding-agent/src/mcp/oauth-flow.ts b/packages/coding-agent/src/mcp/oauth-flow.ts index 076ef1c363..ed40d8453f 100644 --- a/packages/coding-agent/src/mcp/oauth-flow.ts +++ b/packages/coding-agent/src/mcp/oauth-flow.ts @@ -393,6 +393,10 @@ export interface MCPOAuthConfig { clientId?: string; /** Client secret (optional for PKCE flows) */ clientSecret?: string; + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in follows no token-endpoint redirect. + /** Fail the token exchange on a redirect instead of following it with the code, verifier and secret. */ + rejectRedirects?: boolean; + // scient(SA-CODEX-PLUGINS): end /** OAuth scopes (space-separated) */ scopes?: string; /** @@ -616,6 +620,9 @@ export class MCPOAuthFlow extends OAuthCallbackFlow { }, body: params.toString(), signal: this.ctrl.signal, + // scient(SA-CODEX-PLUGINS): begin - as Codex's OAuth client, never forward the exchange elsewhere. + ...(this.config.rejectRedirects && { redirect: "error" as const }), + // scient(SA-CODEX-PLUGINS): end }); if (!response.ok) { @@ -886,6 +893,10 @@ export interface RefreshMCPOAuthTokenOptions { * Preserved advertised resources leave this false/undefined. */ stripSameOriginResource?: boolean; + // scient(SA-CODEX-PLUGINS): begin - a server-bound sign-in follows no token-endpoint redirect. + /** Fail the refresh on a redirect instead of following it with the refresh token and secret. */ + rejectRedirects?: boolean; + // scient(SA-CODEX-PLUGINS): end } /** @@ -900,7 +911,10 @@ export async function refreshMCPOAuthToken( resourceOrOpts?: string | RefreshMCPOAuthTokenOptions, opts?: RefreshMCPOAuthTokenOptions, ): Promise { - const optsFromTrailing = typeof resourceOrOpts === "string" ? opts : resourceOrOpts; + // scient(SA-CODEX-PLUGINS): begin - an undefined resource still reads the trailing options (redirect refusal, + // signal, authorization URL), which keying on a string resource alone dropped. + const optsFromTrailing = typeof resourceOrOpts === "object" ? resourceOrOpts : opts; + // scient(SA-CODEX-PLUGINS): end const fetchImpl: FetchImpl = optsFromTrailing?.fetch ?? fetch; const resource = typeof resourceOrOpts === "string" ? resourceOrOpts : undefined; // Filter against the authorization-server origin when known (RFC 8414 @@ -927,6 +941,9 @@ export async function refreshMCPOAuthToken( headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" }, body: params.toString(), signal: optsFromTrailing?.signal, + // scient(SA-CODEX-PLUGINS): begin - as Codex's OAuth client, never forward the refresh elsewhere. + ...(optsFromTrailing?.rejectRedirects && { redirect: "error" as const }), + // scient(SA-CODEX-PLUGINS): end }); if (!response.ok) { diff --git a/packages/coding-agent/src/mcp/oauth-reauth.ts b/packages/coding-agent/src/mcp/oauth-reauth.ts index 420e61e58d..2428ad5f5a 100644 --- a/packages/coding-agent/src/mcp/oauth-reauth.ts +++ b/packages/coding-agent/src/mcp/oauth-reauth.ts @@ -576,6 +576,9 @@ export async function runMcpOAuthFlow(opts: McpOAuthFlowRunOptions): Promise { try { + // A spent deadline starts no reconnect. + if (deadlineAt !== undefined && performance.now() >= deadlineAt) return null; const pending = untilAborted(signal, () => reconnect(options)); if (deadlineAt === undefined) return await pending; void pending.catch(() => {}); diff --git a/packages/coding-agent/src/mcp/transports/stdio.ts b/packages/coding-agent/src/mcp/transports/stdio.ts index 3dbd860ea2..54af24028a 100644 --- a/packages/coding-agent/src/mcp/transports/stdio.ts +++ b/packages/coding-agent/src/mcp/transports/stdio.ts @@ -924,6 +924,25 @@ export class StdioTransport implements MCPTransport { }, delay); }; if (isMCPTimeoutEnabled(timeout)) armTimer(); + // scient(SA-CODEX-PLUGINS): begin - a capped tool call whose budget ran out while it was being prepared is + // not written: no timer would be left to end it. + if ( + method === "tools/call" && + this.config.toolTimeoutMs !== undefined && + performance.now() >= startedAt + maxTotalMs + ) { + settle( + new MCPTransportError({ + transport: "stdio", + stage: "send", + failure: "timeout", + message: `Request timeout after ${maxTotalMs}ms`, + retryable: false, + }), + ); + return promise; + } + // scient(SA-CODEX-PLUGINS): end if (signal) { signal.addEventListener("abort", onAbort, { once: true }); diff --git a/packages/coding-agent/src/scient/codex-plugins.ts b/packages/coding-agent/src/scient/codex-plugins.ts index 044c03906b..6a5c3b2362 100644 --- a/packages/coding-agent/src/scient/codex-plugins.ts +++ b/packages/coding-agent/src/scient/codex-plugins.ts @@ -61,6 +61,9 @@ const HOST_LAUNCHER = "computer-use-client-launcher"; /** Codex's tool-call limit when a server sets no tool_timeout_sec (codex-mcp DEFAULT_TOOL_TIMEOUT). */ const CODEX_DEFAULT_TOOL_TIMEOUT_MS = 300_000; +/** The server names Codex starts (codex-mcp validate_mcp_server_name). */ +const CODEX_SERVER_NAME = /^[a-zA-Z0-9_:@/.-]+$/; + const IS_WINDOWS = process.platform === "win32"; /** @@ -1265,6 +1268,11 @@ async function loadMCPServers(ctx: LoadContext): Promise> logger.debug(`${skip}: ${reason}`); continue; } + // Codex refuses to start a server whose name does not match its pattern (validate_mcp_server_name). + if (!CODEX_SERVER_NAME.test(serverName)) { + warnings.push(`${skip}: Codex refuses its name, which must match ${CODEX_SERVER_NAME.source}`); + continue; + } // Codex registers the config's own `[mcp_servers.]` over a plugin's server of that name, // so its settings, `enabled = false` included, are what Codex runs. if (configuredServers.has(serverName)) { diff --git a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts index fbdeae5486..954ddb47d6 100644 --- a/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins-oauth.test.ts @@ -3,6 +3,7 @@ import { afterEach, beforeAll, describe, expect, it, vi } from "bun:test"; import * as path from "node:path"; import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-ai"; import { initializeWithSettings } from "@oh-my-pi/pi-coding-agent/capability"; +import { refreshBrokerOAuthCredential } from "@oh-my-pi/pi-coding-agent/cli/auth-broker-cli"; import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; import * as mcpClient from "@oh-my-pi/pi-coding-agent/mcp/client"; import { loadAllMCPConfigs } from "@oh-my-pi/pi-coding-agent/mcp/config"; @@ -129,9 +130,9 @@ describe("an imported Codex plugin server's own sign-in", () => { 'HTTP 401: {"authorization_url":"https://auth.example.test/authorize","token_url":"https://auth.example.test/token"}', ), ); - const flows: { clientSecret?: string }[] = []; + const flows: { clientSecret?: string; rejectRedirects?: boolean }[] = []; vi.spyOn(oauthFlow.MCPOAuthFlow.prototype, "login").mockImplementation( - async function (this: { config: { clientSecret?: string } }) { + async function (this: { config: { clientSecret?: string; rejectRedirects?: boolean } }) { flows.push(this.config); return { access: "fresh", refresh: "fresh-refresh", expires: Date.now() + 3_600_000 }; }, @@ -145,6 +146,8 @@ describe("an imported Codex plugin server's own sign-in", () => { ); expect(flows).toHaveLength(1); expect(flows[0]?.clientSecret).toBeUndefined(); + // Its code exchange follows no redirect, which could carry the code, verifier and secret elsewhere. + expect(flows[0]?.rejectRedirects).toBe(true); // The new sign-in keeps no secret either. expect(storage.credentials.get(credentialId)).toMatchObject({ access: "fresh", clientId: "client-a" }); expect(storage.credentials.get(credentialId)).not.toHaveProperty("clientSecret", expect.anything()); @@ -228,8 +231,10 @@ describe("an imported Codex plugin server's own sign-in", () => { const storage = freshStorage(); const credentialId = oauthFlow.mcpOAuthServerCredentialId(NAME, SERVER_URL); const sent: (string | null)[] = []; + const redirects: (string | undefined)[] = []; vi.spyOn(globalThis, "fetch").mockImplementation((async (_url: string, init?: RequestInit) => { sent.push(new URLSearchParams(String(init?.body)).get("client_secret")); + redirects.push(init?.redirect); return Response.json({ access_token: "fresh", refresh_token: "r2", expires_in: 3600 }); }) as never); const stored = (): oauthFlow.MCPStoredOAuthCredential => ({ @@ -249,7 +254,13 @@ describe("an imported Codex plugin server's own sign-in", () => { const { credential } = await refreshStoredManagedMcpOAuthCredential(storage, credentialId, { forceRefresh: true, }); - expect(sent).toEqual(["current", null]); + // The broker, which has no definition, refreshes the same row without the stored secret too. + await refreshBrokerOAuthCredential(credentialId, stored()); + // An ordinary server's sign-in keeps its stored secret and the default redirect handling. + await refreshBrokerOAuthCredential(oauthFlow.mcpOAuthCredentialId(SERVER_URL), stored()); + expect(sent).toEqual(["current", null, null, "retired"]); + // A server-bound exchange follows no redirect, which could carry the token and secret elsewhere. + expect(redirects).toEqual(["error", "error", "error", undefined]); // Nothing keeps a secret for the next refresh. expect(credential?.clientSecret).toBeUndefined(); expect(storage.credentials.get(credentialId)).not.toHaveProperty("clientSecret", expect.anything()); diff --git a/packages/coding-agent/test/scient/codex-plugins.test.ts b/packages/coding-agent/test/scient/codex-plugins.test.ts index a6f5ddabe8..6d18d29388 100644 --- a/packages/coding-agent/test/scient/codex-plugins.test.ts +++ b/packages/coding-agent/test/scient/codex-plugins.test.ts @@ -292,6 +292,8 @@ describe("installed Codex plugins", () => { numericalias: { url: "https://mcp.example.test/j", oauth: { client_id: "id", clientSecret: 7 } }, // Rust trims U+0085 (NEL) as whitespace, so Codex finds this secret blank. nelsecret: { url: "https://mcp.example.test/k", oauth: { client_id: "id", client_secret: "\u0085" } }, + // Codex refuses to start a server whose name has characters outside its pattern. + "my server": { command: "node", args: ["spaced.mjs"] }, }); const { names, warnings } = await limits(); expect(names.sort()).toEqual(["limits@market:cased", "limits@market:kept", "limits@market:shown"]); @@ -315,6 +317,7 @@ describe("installed Codex plugins", () => { expect(warnings).toContainEqual(expect.stringContaining('"nullsnake" of limits@market: its OAuth client_secret')); expect(warnings).toContainEqual(expect.stringContaining('"numericalias" of limits@market: invalid oauth')); expect(warnings).toContainEqual(expect.stringContaining('"nelsecret" of limits@market: its OAuth client_secret')); + expect(warnings).toContainEqual(expect.stringContaining('"my server" of limits@market: Codex refuses its name')); expect(warnings).toContainEqual( expect.stringContaining('"issuer" of limits@market: its OAuth authorization_server_issuer needs ema_auth'), ); @@ -468,6 +471,49 @@ describe("installed Codex plugins", () => { ); expect((await late.execute("call", {}, () => {}, ctx)).isError).toBe(true); expect(deadlines).toHaveLength(1); + // A deadline already spent when the call fails starts no reconnect at all. + let reconnects = 0; + const spent = new MCPTool( + connection(async () => { + skew += 400_000; + throw unavailable; + }), + definition, + async () => { + reconnects++; + return connection(async () => ({ content: [] })); + }, + ); + expect((await spent.execute("call", {}, () => {}, ctx)).isError).toBe(true); + expect(reconnects).toBe(0); + }); + + it("never writes a stdio tool call whose cap ran out while it was being prepared", async () => { + const transport = new StdioTransport({ + type: "stdio", + command: process.execPath, + // Stays up and never answers, so only a timer could end a call it was sent. + args: ["-e", "setInterval(() => {}, 1000)"], + toolTimeoutMs: 5_000, + }); + const realNow = performance.now.bind(performance); + let skew = 0; + vi.spyOn(performance, "now").mockImplementation(() => realNow() + skew); + await transport.connect(); + try { + // Serializing the request spends the whole cap. + const slow = { + toJSON: () => { + skew += 10_000; + return {}; + }, + }; + await expect(transport.request("tools/call", { name: "slow", arguments: slow })).rejects.toThrow( + "Request timeout after 5000ms", + ); + } finally { + await transport.close(); + } }); it("skips a server whose config.toml policy narrows its tools with disabled_tools", async () => { diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index e2165e711b..b09722cfd9 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one monotonic deadline (MCPRequestOptions.deadlineAt, on performance.now(), the clock createMCPTimeout and the stdio transport also measure progress restarts on) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts); no refresh, retry or replay starts once it is spent, and a reconnect still pending at it or landing past it ends the call with its own error; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. Its oauth table is read as Codex reads a plugin's: the camelCase aliases clientId, clientSecret, callbackUrl and callbackPort fold into their snake_case keys by key presence, a present snake_case key winning even when null, before any type is checked. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or either of them is blank by Rust's Unicode White_Space (an empty secret included), or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; a blank client ID without a secret counts as none; and a client ID or secret with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server too. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in on another one; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it. Like Codex, a server-bound sign-in stores no client secret: the sign-in flow (mcp/oauth-reauth.ts) keeps none, and refresh (refreshStoredManagedMcpOAuthCredential's clientSecret, which the manager fills from the current definition) and reauthorization send the secret the current definition configures, or none, never a stored or retired one. Reauthorization and TUI /mcp unauth act on a bound id only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A server whose declared name does not match Codex's ^[a-zA-Z0-9_:@/.-]+$ (validate_mcp_server_name) is skipped, as Codex refuses to start it. A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one monotonic deadline (MCPRequestOptions.deadlineAt, on performance.now(), the clock createMCPTimeout and the stdio transport also measure progress restarts on) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts); no refresh, retry, reconnect or replay starts once it is spent, a reconnect still pending at it or landing past it ends the call with its own error, and the stdio transport writes no capped call whose cap ran out while it was being prepared; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. Its oauth table is read as Codex reads a plugin's: the camelCase aliases clientId, clientSecret, callbackUrl and callbackPort fold into their snake_case keys by key presence, a present snake_case key winning even when null, before any type is checked. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or either of them is blank by Rust's Unicode White_Space (an empty secret included), or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; a blank client ID without a secret counts as none; and a client ID or secret with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server too. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in on another one; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it. Like Codex, a server-bound sign-in stores no client secret and follows no token-endpoint redirect: the sign-in flow (mcp/oauth-reauth.ts) keeps none and its code exchange refuses redirects (MCPOAuthConfig.rejectRedirects); every refresh path, the manager's, `omp token`'s and the auth broker's (cli/auth-broker-cli.ts), names the row to refreshManagedMcpOAuthCredential, which for a bound row sends the stored client ID with only the secret the current definition configures (the manager passes it; callers without a definition send none), never a stored or retired one, and refuses redirects (refreshMCPOAuthToken's rejectRedirects, whose trailing options are read whether or not a resource is given); reauthorization likewise takes its secret from the current definition only. A secret an earlier build of this change left on a bound row is never sent; the broker's generic merge may keep it on the row. Reauthorization and TUI /mcp unauth act on a bound id only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", @@ -530,6 +530,7 @@ "packages/coding-agent/scripts/compile-binary.ts", "packages/coding-agent/src/capability/index.ts", "packages/coding-agent/src/capability/mcp.ts", + "packages/coding-agent/src/cli/auth-broker-cli.ts", "packages/coding-agent/src/discovery/index.ts", "packages/coding-agent/src/mcp/config.ts", "packages/coding-agent/src/mcp/manager.ts", From 3987cf3949fe5664223ee5703ce794038969dc85 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 18:21:37 +0200 Subject: [PATCH 20/30] feat(mcp): host status and sign-in command, live pickup of new sign-ins - scient-agent mcp list prints each MCP server's status (connected, needs-auth, failed, pending), plugin ID, tool count and whether a browser sign-in serves it; mcp login runs the /mcp reauth flow and prints the authorization URL for the host to open. - A bound server refused by an authorization server that registers only approved clients (Figma) says so in plain words. - RPC sessions reconnect a disconnected sign-in server before a prompt once its stored sign-in is new or changed, so a sign-in from the host reaches running conversations without a restart. - --runtime-info lists mcp in hostCommands; ledger, SCIENT.md and CHANGELOG updated. --- SCIENT.md | 1 + packages/coding-agent/CHANGELOG.md | 2 + packages/coding-agent/src/cli-commands.ts | 10 + .../coding-agent/src/modes/rpc/rpc-mode.ts | 15 ++ .../coding-agent/src/scient/command-help.ts | 4 + .../coding-agent/src/scient/mcp-command.ts | 174 ++++++++++++++++++ .../src/scient/mcp-sign-in-pickup.ts | 59 ++++++ .../coding-agent/src/scient/runtime-info.ts | 2 +- .../test/scient/mcp-host-command.test.ts | 134 ++++++++++++++ .../test/scient/runtime-info.test.ts | 2 +- scripts/scient/divergences.json | 11 +- 11 files changed, 409 insertions(+), 5 deletions(-) create mode 100644 packages/coding-agent/src/scient/mcp-command.ts create mode 100644 packages/coding-agent/src/scient/mcp-sign-in-pickup.ts create mode 100644 packages/coding-agent/test/scient/mcp-host-command.test.ts diff --git a/SCIENT.md b/SCIENT.md index be1349a8b6..d7e311f1eb 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -16,6 +16,7 @@ Everything else is upstream's code and behavior. | Variables | The variables that choose where state lives have `SCIENT_AGENT_*` names: `CONFIG_DIR`, `CONFIG_FILES`, `DIR`, `PROFILE`, `SESSION_DIR`, `WORKTREE_DIR`, `AUTH_BROKER_*`, `NATIVES_DIR`, the cache database overrides, the directory, TCP address and re-exec hand-over of Oh My Pi's per-user daemon, and the configuration the agent hands its own workers. OMP's names for them are ignored. Tuning variables (`PI_*`) are unchanged. | | Discovery | A host can add foreign user-level sources with `SCIENT_AGENT_ENABLE_SOURCES` (provider ids separated by commas or whitespace). These are unioned with `enabledProviders` for discovery without changing or persisting that setting; `disabledProviders` still wins. The same aliases apply, including `claude` enabling `claude-plugins`. Under a host root, project and home `.env` files cannot add sources. `--runtime-info` lists the plugin sources a host may add (`enableSources`). | | Codex plugins | The opt-in source `codex-plugins` loads the skills and MCP servers of the Codex plugins a person installed and enabled (`[plugins."@"]` in `$CODEX_HOME/config.toml`, default `~/.codex`, or `[plugins._default]`), from the version Codex runs in its plugin cache, under Codex's own rules: its manifest precedence (a root Agent Plugins `plugin.json`, then `.codex-plugin`, `.claude-plugin`, `.cursor-plugin`), its field types and `./` paths (falling back to `skills/` and `.mcp.json` as Codex does), its skill scan, skill names and the user's `[[skills.config]]` rules, one transport per server with only that transport's fields, package files, skills and launch paths inside the package, a stdio server inheriting only the variables Codex passes (`HOME`, `PATH` and the like, plus its `env_vars`), plugin values taken literally, and each tool call held to the server's `tool_timeout_sec`, or Codex's five-minute default, on a clock that never runs backwards, whatever the call asks to wait and across a sign-in refresh, a reconnect and a replay of the same call (a server with `tool_timeout_sec = 0`, whose every call Codex times out, is skipped). Servers are named `@:`; `/mcp enable` and `/mcp disable` toggle them through `disabledServers`. Each imported HTTP server signs in under its own credential (`mcp_oauth:server:…`), which only a definition the `codex-plugins` source supplies can claim and which never stands in for another server's at the same URL: re-signing in keeps its values literal and asks for its configured scopes and `oauth_resource`, reconnecting keeps that sign-in, a sign-in made for another confidential client ID than the plugin now configures (compared exactly) is not used, the sign-in keeps no client secret, so refreshing and re-signing in send only the secret the plugin configures now, a sign-in challenge from the live server is answered for that server and never for a same-named config-file entry, and `/mcp unauth` removes only it. Its OAuth settings are read as Codex reads them (camelCase aliases giving way to snake_case keys); settings Codex refuses (a client secret without a client ID, or either of them blank; `authorization_server_issuer` or `token_endpoint_auth_method` without `ema_auth`) or a client ID or secret with surrounding whitespace skip the server. A server that names its own token (`bearer_token_env_var`) keeps that token and is never signed in with OAuth, and is skipped when the token's variable is unset, as in Codex. `CODEX_HOME`, header credentials and servers' variables come from the environment Scient Agent was launched with, never a `.env` file; a run that cannot tell that environment apart (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`) loads nothing. A server whose tools Codex narrows (`enabled_tools`, or a non-empty `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval; `auto` follows the session's approval policy, as Scient Agent's approval mode does) is skipped with a warning, because Scient Agent cannot enforce either; so is a server Codex turns off (`enabled = false`, `ema_auth`), one the user's own `[mcp_servers]` replaces, one two plugins declare under the same name, and one with malformed or unsupported fields, while its siblings load. Nothing loads while an organization policy manages Codex, or may and cannot be checked, while `[features] plugins` is off, or when Codex cannot read its `[plugins]` settings. Plugins and servers that run only inside OpenAI's apps (browser, Chrome, computer use, the Codex app's tools, the Computer Use client launcher, Codex's ChatGPT session) are left out, and so are app references, which the ChatGPT apps cover. `codex` does not enable it. | +| MCP sign-in | `scient-agent mcp list` prints each MCP server a conversation loads with its status (connected with its tool count, needs sign-in, failed) and whether a browser sign-in serves it; `scient-agent mcp login ` runs that sign-in and prints the page to open, for a host's own Sign in button (`--runtime-info` lists `mcp` in `hostCommands`). A conversation in RPC mode picks up a sign-in made elsewhere at its next prompt. An authorization server that registers only the apps it approved (Figma) refuses Scient Agent until it approves it. | | Native addon cache | A compiled binary extracts its addons into a directory named for their content, so two builds on one upstream version never load each other's, and it loads only from that directory. A modern addon that fails to load falls back to the same build's baseline addon; an addon found beside the executable is not a fallback. | | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 2ec92412fd..5784a70102 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -7,6 +7,8 @@ - The skills and MCP servers of the Codex plugins you installed and enabled load when the `codex-plugins` source is on (`enabledProviders`, or a host's `SCIENT_AGENT_ENABLE_SOURCES`), as `@:` servers, under Codex's own rules: only the plugins Codex would run (`[plugins._default]`, `[features] plugins`, its manifest formats and field types included), only the skills your Codex config leaves on (`[[skills.config]]`), servers that inherit only the variables Codex would pass them from the environment Scient Agent was launched with (never a `.env` file), plugin values taken literally, tool calls held to the plugin's `tool_timeout_sec` (five minutes unless set, as in Codex), and package paths and skills kept inside the package. Each imported HTTP server signs in on its own, never with another server's sign-in at the same URL, asking for the scopes and resource the plugin configures and signing in again when the plugin changes its OAuth client; one that names its own token keeps it and is never signed in with OAuth. Plugins that only work inside OpenAI's apps are left out, and so, with a warning, are servers whose tools Codex narrows (`enabled_tools` or `disabled_tools`), hides (`omit_tools_from`), caps (`output_token_limit`) or always asks about (`prompt` or `writes` approval), servers Codex turns off (`enabled = false`, `ema_auth`) or replaces with your own `[mcp_servers]` entry, servers two plugins declare under one name, and servers with malformed or unsupported fields. Nothing loads while an organization policy manages Codex, or may and cannot be checked, when Codex cannot read its `[plugins]` settings, or when Scient Agent cannot tell its launch environment from `.env` files (from source without `--no-env-file` outside Linux, or with an explicit `--env-file`). +- `scient-agent mcp list` prints each MCP server a conversation loads with its status (connected with its tool count, needs sign-in, failed) and whether a browser sign-in serves it, and `scient-agent mcp login ` runs that sign-in and prints the page to open, for a host's own Sign in button (`--runtime-info` lists `mcp` in `hostCommands`). A conversation in RPC mode picks up a sign-in made elsewhere at its next prompt, without a restart. + ## [19.0.1] - 2026-11-19 diff --git a/packages/coding-agent/src/cli-commands.ts b/packages/coding-agent/src/cli-commands.ts index 8b44838e51..1584f7f8a6 100644 --- a/packages/coding-agent/src/cli-commands.ts +++ b/packages/coding-agent/src/cli-commands.ts @@ -24,6 +24,9 @@ import { dictateHelp } from "./scient/command-help"; // scient(SA-CHATGPT-APPS): begin - Scient's ChatGPT apps command. import { appsHelp } from "./scient/command-help"; // scient(SA-CHATGPT-APPS): end +// scient(SA-CODEX-PLUGINS): begin - the host's MCP status and sign-in command. +import { mcpHelp } from "./scient/command-help"; +// scient(SA-CODEX-PLUGINS): end function loadLaunchHelp(): typeof LaunchHelp.launchHelp { const module: typeof LaunchHelp = require("./commands/launch-help"); @@ -299,6 +302,13 @@ export const commands: CommandEntry[] = [ help: appsHelp, }, // scient(SA-CHATGPT-APPS): end + // scient(SA-CODEX-PLUGINS): begin - the host's MCP status and sign-in command. + { + name: "mcp", + load: () => import("./scient/mcp-command").then(m => m.default), + help: mcpHelp, + }, + // scient(SA-CODEX-PLUGINS): end { name: "usage", load: () => import("./commands/usage").then(m => m.default), diff --git a/packages/coding-agent/src/modes/rpc/rpc-mode.ts b/packages/coding-agent/src/modes/rpc/rpc-mode.ts index 33a118977a..d6bd320952 100644 --- a/packages/coding-agent/src/modes/rpc/rpc-mode.ts +++ b/packages/coding-agent/src/modes/rpc/rpc-mode.ts @@ -109,6 +109,9 @@ import { RpcSessionEventForwarder } from "./rpc-session-events"; import { isSessionSettled } from "../../session/session-settle"; import { RpcSessionSettleWatcher, watchedScheduledTurnProbe } from "./rpc-session-settle"; import { RpcSubagentRegistry, readRpcSubagentTranscript, resolveOwnedLiveSubagent } from "./rpc-subagents"; +// scient(SA-CODEX-PLUGINS): begin - pick up MCP sign-ins made since the last prompt. +import { pickUpMcpSignIns } from "../../scient/mcp-sign-in-pickup"; +// scient(SA-CODEX-PLUGINS): end import type { RpcAbortAndRestoreQueueResult, RpcCommand, @@ -1949,6 +1952,9 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions await emitAvailableCommandsUpdate(); const inputGate = new RpcUserInputGate(); + // scient(SA-CODEX-PLUGINS): begin - the MCP sign-ins each server last reconnected with, per session. + const mcpSignInsTried = new Map(); + // scient(SA-CODEX-PLUGINS): end type OrderedUserInput = Extract; type OrderedInputOutcome = "local" | "cancelled" | "admitted" | "builtin-agent"; const dispatchOrderedUserInput = ( @@ -2052,6 +2058,15 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions } } if (!isCurrent() || !ticket) return "cancelled"; + // scient(SA-CODEX-PLUGINS): begin - a server signed in from the host since the last prompt joins this one. + if (options.mcpManager) { + const manager = options.mcpManager; + await pickUpMcpSignIns(manager, session.modelRegistry.authStorage, mcpSignInsTried, () => + session.refreshMCPTools(manager.getTools()), + ); + if (!isCurrent()) return "cancelled"; + } + // scient(SA-CODEX-PLUGINS): end await watchAndReportPromptResult({ ticket, startPrompt: onPromptAdmitted => diff --git a/packages/coding-agent/src/scient/command-help.ts b/packages/coding-agent/src/scient/command-help.ts index acc177c471..c04aaa6e0a 100644 --- a/packages/coding-agent/src/scient/command-help.ts +++ b/packages/coding-agent/src/scient/command-help.ts @@ -8,3 +8,7 @@ export const dictateHelp = { export const appsHelp = { description: "List the ChatGPT apps (Gmail, Google Drive and others) the ChatGPT sign-in has connected", } satisfies CommandMetadata; + +export const mcpHelp = { + description: "List the MCP servers a conversation loads, with their status, or sign in to one (for a host)", +} satisfies CommandMetadata; diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts new file mode 100644 index 0000000000..3807042dee --- /dev/null +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -0,0 +1,174 @@ +/** + * `scient-agent mcp`: the MCP servers a conversation would load, for a host that shows them and + * signs in to them itself. Each subcommand prints JSON lines on stdout. + * + * - `list [--server ]` connects each server as a conversation would and prints + * `{ "servers": [{ "name", "status", "scope"?, "pluginId"?, "toolCount"?, "error"?, "canStartSignIn"? }] }`, + * status one of `connected`, `needs-auth`, `failed` or `pending`. + * - `login ` runs the server's browser sign-in: it prints `{ "type": "authorization_url", "url" }`, + * keeps the local callback listener open, then prints `{ "type": "completed" }`, or + * `{ "type": "failed", "message" }` and exits 1. SIGTERM cancels it. + * + * A failure before any of that prints `{ "error": { "message" } }` and exits 1. + */ +import { Args, Command, Flags } from "@oh-my-pi/pi-utils/cli"; +import type { SourceMeta } from "../capability/types"; +import { Settings } from "../config/settings"; +import { loadAllMCPConfigs } from "../mcp/config"; +import { type MCPLoadResult, MCPManager } from "../mcp/manager"; +import { analyzeAuthError } from "../mcp/oauth-discovery"; +import { isMcpOAuthServerCredentialId, MCPOAuthClientIdRequiredError } from "../mcp/oauth-flow"; +import { + findMcpServerForAuth, + MCPOAuthCancelledError, + McpReauthRefusalError, + reauthorizeMcpServer, +} from "../mcp/oauth-reauth"; +import { cfgMcpEnableProjectConfig } from "../mcp/settings"; +import type { MCPServerConfig } from "../mcp/types"; +import type { AuthStorage } from "../session/auth-storage"; +import { discoverAuthStorage } from "../sdk"; +import { mcpHelp } from "./command-help"; +import { canSignIn } from "./mcp-sign-in-pickup"; + +/** How long `list` waits for servers to answer, matching a conversation's startup window. */ +const LIST_WAIT_MS = 15_000; +const MAX_ERROR_LENGTH = 500; + +export interface McpServerStatusLine { + name: string; + status: "connected" | "needs-auth" | "failed" | "pending"; + scope?: "user" | "project"; + pluginId?: string; + toolCount?: number; + error?: string; + canStartSignIn?: true; +} + +function print(value: unknown): void { + process.stdout.write(`${JSON.stringify(value)}\n`); +} + +/** One server's status once the startup window closed, for `list`. */ +export function serverStatusLine(manager: MCPManager, name: string, loaded: MCPLoadResult): McpServerStatusLine { + const source = manager.getSource(name); + const config = manager.getServerConfig(name); + const base: McpServerStatusLine = { + name, + status: "pending", + ...(source && { scope: source.level === "project" ? "project" : "user" }), + ...(source?.pluginName && { pluginId: source.pluginName }), + }; + if (loaded.connectedServers.includes(name)) { + const toolCount = manager.getConnection(name)?.tools?.length ?? 0; + return { ...base, status: "connected", ...(toolCount > 0 && { toolCount }) }; + } + const failure = loaded.errors.get(name); + if (failure === undefined) return base; + const error = failure.slice(0, MAX_ERROR_LENGTH); + const url = config && (config.type === "http" || config.type === "sse") ? config.url : undefined; + if (url && config && analyzeAuthError(new Error(failure), url).requiresAuth) { + return { ...base, status: "needs-auth", error, ...(canSignIn(config) && { canStartSignIn: true as const }) }; + } + return { ...base, status: "failed", error }; +} + +/** A sign-in failure in words a person acts on. */ +function loginFailureMessage(name: string, config: MCPServerConfig, error: unknown): string { + if (error instanceof MCPOAuthCancelledError) return "The sign-in was cancelled."; + if (error instanceof MCPOAuthClientIdRequiredError && isMcpOAuthServerCredentialId(config.auth?.credentialId)) { + return `${name} lets only the apps it has approved sign in, and it does not accept Scient Agent yet.`; + } + return error instanceof Error ? error.message : String(error); +} + +export default class Mcp extends Command { + static description = mcpHelp.description; + static args = { + action: Args.string({ description: "list or login", required: true, options: ["list", "login"] }), + server: Args.string({ description: "Server name (login)" }), + }; + static flags = { + server: Flags.string({ description: "List only this server" }), + }; + static examples = ["scient-agent mcp list", "scient-agent mcp login 'figma@openai-curated:figma'"]; + + async run(): Promise { + const { args, flags } = await this.parse(Mcp); + const cwd = process.cwd(); + const settings = await Settings.loadReadOnly(); + const authStorage = await discoverAuthStorage(undefined, { settings }); + const manager = new MCPManager(cwd, null); + manager.setAuthStorage(authStorage); + const abort = new AbortController(); + const onSignal = (): void => abort.abort(); + process.once("SIGTERM", onSignal); + process.once("SIGINT", onSignal); + try { + const only = args.action === "login" ? args.server : flags.server; + if (args.action === "login" && !only) throw new Error("Name the server to sign in to."); + const loaded = await loadAllMCPConfigs(cwd, { enableProjectConfig: cfgMcpEnableProjectConfig.get(settings) }); + const configs = only + ? Object.fromEntries(Object.entries(loaded.configs).filter(([name]) => name === only)) + : loaded.configs; + if (args.action === "login") + await this.#login(only!, configs, loaded.sources, manager, authStorage, abort, cwd, settings); + else await this.#list(configs, loaded.sources, manager); + } catch (error) { + print({ error: { message: error instanceof Error ? error.message : String(error) } }); + process.exitCode = 1; + } finally { + process.off("SIGTERM", onSignal); + process.off("SIGINT", onSignal); + await manager.disconnectAll().catch(() => {}); + authStorage.close(); + } + // The startup window's timer outlives a startup that settled sooner; nothing else is left to do. + process.exit(); + } + + async #list( + configs: Record, + sources: Record, + manager: MCPManager, + ): Promise { + const loaded = await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); + print({ servers: Object.keys(configs).map(name => serverStatusLine(manager, name, loaded)) }); + } + + async #login( + name: string, + configs: Record, + sources: Record, + manager: MCPManager, + authStorage: AuthStorage, + abort: AbortController, + cwd: string, + settings: Settings, + ): Promise { + if (!configs[name]) throw new Error(`No MCP server named "${name}" is configured.`); + // Registers the definition the way a conversation does, so the lookup finds discovered servers. + await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); + const found = await findMcpServerForAuth(name, cwd, manager, { + enableProjectConfig: cfgMcpEnableProjectConfig.get(settings), + }); + if (!found) throw new Error(`No MCP server named "${name}" is configured.`); + try { + await reauthorizeMcpServer(name, found, { + authStorage, + signal: abort.signal, + cwd, + mcpManager: manager, + silent: true, + onAuth: info => print({ type: "authorization_url", url: info.url }), + onProgress: () => {}, + }); + print({ type: "completed" }); + } catch (error) { + const message = + error instanceof McpReauthRefusalError ? error.message : loginFailureMessage(name, found.config, error); + print({ type: "failed", message }); + process.exitCode = 1; + } + } +} diff --git a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts new file mode 100644 index 0000000000..ef54b04a84 --- /dev/null +++ b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts @@ -0,0 +1,59 @@ +import { logger } from "@oh-my-pi/pi-utils"; +import type { MCPManager } from "../mcp/manager"; +import { hasMcpAuthorizationHeader, lookupMcpOAuthCredential } from "../mcp/oauth-credentials"; +import { isMcpOAuthServerCredentialId } from "../mcp/oauth-flow"; +import type { MCPServerConfig } from "../mcp/types"; +import type { AuthStorage } from "../session/auth-storage"; + +/** Whether a browser sign-in can serve this server: http/sse, no token of its own. */ +export function canSignIn(config: MCPServerConfig): boolean { + if (config.type !== "http" && config.type !== "sse") return false; + if (hasMcpAuthorizationHeader(config)) return false; + // A server bound to its own sign-in (an imported Codex plugin server) signs in only with OAuth. + if (isMcpOAuthServerCredentialId(config.auth?.credentialId)) return config.auth?.type === "oauth"; + return config.auth === undefined || config.auth.type === "oauth"; +} + +/** + * Connects the MCP servers a sign-in made elsewhere (the host's Sign in, `scient-agent mcp login`) + * now serves. A session reads sign-ins once, so a server that needed one stays disconnected until + * something reconnects it; this runs before a prompt. + * + * Only a disconnected server that signs in with a browser is considered, and only when its stored + * sign-in differs from the one last tried here (`tried`, per session), so a server whose sign-in + * was refused is not retried on every prompt. Never throws. + */ +export async function pickUpMcpSignIns( + manager: MCPManager, + authStorage: AuthStorage, + tried: Map, + onConnected: () => Promise, +): Promise { + const candidates = manager.getAllServerNames().flatMap(name => { + if (manager.getConnectionStatus(name) !== "disconnected") return []; + const config = manager.getServerConfig(name); + return config && canSignIn(config) ? [{ name, config }] : []; + }); + if (candidates.length === 0) return; + try { + await authStorage.credentials.revalidate(); + } catch (err) { + logger.warn("Re-reading sign-ins for MCP servers failed", { err }); + return; + } + let connected = false; + for (const { name, config } of candidates) { + const lookup = lookupMcpOAuthCredential(authStorage, config); + if (!lookup) continue; + const signature = `${lookup.credentialId}\0${Bun.hash(lookup.credential.access ?? "")}`; + if (tried.get(name) === signature) continue; + tried.set(name, signature); + try { + await manager.reconnectServer(name, { manual: true }); + connected ||= manager.getConnectionStatus(name) === "connected"; + } catch (err) { + logger.warn("Connecting an MCP server with its new sign-in failed", { server: name, err }); + } + } + if (connected) await onConnected(); +} diff --git a/packages/coding-agent/src/scient/runtime-info.ts b/packages/coding-agent/src/scient/runtime-info.ts index efd99f8bb6..e1008b28fd 100644 --- a/packages/coding-agent/src/scient/runtime-info.ts +++ b/packages/coding-agent/src/scient/runtime-info.ts @@ -38,7 +38,7 @@ export function formatRuntimeInfo(): string { upstream: UPSTREAM, rpcProtocolVersions: [1, 2], buildId: BUILD_ID, - hostCommands: ["apps", "dictate"], + hostCommands: ["apps", "dictate", "mcp"], enableSources: ["claude-plugins", "codex-plugins"], }; return `${JSON.stringify(info)}\n`; diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts new file mode 100644 index 0000000000..c0a7f6f094 --- /dev/null +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -0,0 +1,134 @@ +import { Database } from "bun:sqlite"; +import { afterEach, describe, expect, it } from "bun:test"; +import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-ai"; +import type { SourceMeta } from "@oh-my-pi/pi-coding-agent/capability/types"; +import type { MCPLoadResult, MCPManager } from "@oh-my-pi/pi-coding-agent/mcp/manager"; +import { mcpOAuthServerCredentialId } from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow"; +import type { MCPServerConfig } from "@oh-my-pi/pi-coding-agent/mcp/types"; +import { serverStatusLine } from "@oh-my-pi/pi-coding-agent/scient/mcp-command"; +import { pickUpMcpSignIns } from "@oh-my-pi/pi-coding-agent/scient/mcp-sign-in-pickup"; + +const URL_A = "https://mcp.example.test/mcp"; +const NAME = "tools@market:api"; +const CHALLENGE = + 'HTTP 401: Unauthorized [WWW-Authenticate: Bearer resource_metadata="https://mcp.example.test/.well-known/oauth-protected-resource"]'; +const bound = ( + auth: MCPServerConfig["auth"] = { type: "oauth", credentialId: mcpOAuthServerCredentialId(NAME, URL_A) }, +) => ({ type: "http", url: URL_A, auth }) as MCPServerConfig; +const token = (access: string) => ({ type: "oauth" as const, access, refresh: "r", expires: Date.now() + 3_600_000 }); + +/** The few manager members these helpers read; the rest of MCPManager is not involved. */ +function fakeManager( + servers: Record; tools?: number }>, +) { + const connected = new Set(); + const reconnects: string[] = []; + const manager = { + getAllServerNames: () => Object.keys(servers), + getConnectionStatus: (name: string) => (connected.has(name) ? "connected" : "disconnected"), + getServerConfig: (name: string) => servers[name]?.config, + getSource: (name: string) => servers[name]?.source, + getConnection: (name: string) => + connected.has(name) || servers[name]?.tools + ? { tools: Array.from({ length: servers[name]?.tools ?? 0 }) } + : undefined, + reconnectServer: async (name: string) => { + reconnects.push(name); + connected.add(name); + return null; + }, + disconnect: (name: string) => connected.delete(name), + }; + // Structural fake: only the members above are called by the code under test. + return { manager: manager as unknown as MCPManager, reconnects, disconnect: manager.disconnect }; +} + +const loadResult = (connected: string[], errors: Record): MCPLoadResult => ({ + tools: [], + errors: new Map(Object.entries(errors)), + connectedServers: connected, + exaApiKeys: [], +}); + +describe("scient-agent mcp list", () => { + it("reports a server refusing an anonymous call as needing sign-in, and whether a browser sign-in serves it", () => { + const { manager } = fakeManager({ + [NAME]: { config: bound(), source: { level: "user", pluginName: "tools@market" } }, + "own-token": { config: bound({ type: "bearer" } as unknown as MCPServerConfig["auth"]) }, + header: { config: { type: "http", url: URL_A, headers: { Authorization: "Bearer x" } } }, + down: { config: bound() }, + }); + const loaded = loadResult([], { + [NAME]: CHALLENGE, + "own-token": CHALLENGE, + header: CHALLENGE, + down: "getaddrinfo ENOTFOUND", + }); + expect(serverStatusLine(manager, NAME, loaded)).toEqual({ + name: NAME, + status: "needs-auth", + scope: "user", + pluginId: "tools@market", + error: CHALLENGE, + canStartSignIn: true, + }); + expect(serverStatusLine(manager, "own-token", loaded)).not.toHaveProperty("canStartSignIn"); + expect(serverStatusLine(manager, "header", loaded)).not.toHaveProperty("canStartSignIn"); + expect(serverStatusLine(manager, "down", loaded).status).toBe("failed"); + }); + + it("reports a connected server's tool count and a server still starting as pending", () => { + const { manager } = fakeManager({ ok: { config: bound(), tools: 3 }, slow: { config: bound() } }); + const loaded = loadResult(["ok"], {}); + expect(serverStatusLine(manager, "ok", loaded)).toMatchObject({ status: "connected", toolCount: 3 }); + expect(serverStatusLine(manager, "slow", loaded).status).toBe("pending"); + }); +}); + +describe("a conversation picking up a sign-in made elsewhere", () => { + const stores: AuthStorage[] = []; + afterEach(() => { + while (stores.length > 0) stores.pop()?.close(); + }); + const storage = () => { + const store = new AuthStorage(new SqliteAuthCredentialStore(new Database(":memory:"))); + stores.push(store); + return store; + }; + + it("connects a server once its sign-in appears, and retries only when that sign-in changes", async () => { + const auth = storage(); + const { manager, reconnects, disconnect } = fakeManager({ [NAME]: { config: bound() } }); + const tried = new Map(); + let refreshed = 0; + const pickUp = () => pickUpMcpSignIns(manager, auth, tried, async () => void refreshed++); + + await pickUp(); + expect(reconnects).toEqual([]); + + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("first")); + await pickUp(); + expect(reconnects).toEqual([NAME]); + expect(refreshed).toBe(1); + + // The server dropped again, e.g. its sign-in was refused: the same sign-in is not retried each prompt. + disconnect(NAME); + await pickUp(); + expect(reconnects).toEqual([NAME]); + + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("second")); + await pickUp(); + expect(reconnects).toEqual([NAME, NAME]); + }); + + it("never reconnects a server a browser sign-in does not serve", async () => { + const auth = storage(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("t")); + const { manager, reconnects } = fakeManager({ + header: { config: { ...bound(), headers: { Authorization: "Bearer x" } } as MCPServerConfig }, + local: { config: { type: "stdio", command: "server" } as MCPServerConfig }, + }); + await pickUpMcpSignIns(manager, auth, new Map(), async () => {}); + expect(reconnects).toEqual([]); + }); +}); diff --git a/packages/coding-agent/test/scient/runtime-info.test.ts b/packages/coding-agent/test/scient/runtime-info.test.ts index 112bf4fabc..4acde1697f 100644 --- a/packages/coding-agent/test/scient/runtime-info.test.ts +++ b/packages/coding-agent/test/scient/runtime-info.test.ts @@ -15,7 +15,7 @@ describe("--runtime-info", () => { upstream: UPSTREAM, rpcProtocolVersions: [1, 2], buildId: BUILD_ID, - hostCommands: ["apps", "dictate"], + hostCommands: ["apps", "dictate", "mcp"], enableSources: ["claude-plugins", "codex-plugins"], }); }); diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index b09722cfd9..e0f49aa881 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,17 +520,21 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A server whose declared name does not match Codex's ^[a-zA-Z0-9_:@/.-]+$ (validate_mcp_server_name) is skipped, as Codex refuses to start it. A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one monotonic deadline (MCPRequestOptions.deadlineAt, on performance.now(), the clock createMCPTimeout and the stdio transport also measure progress restarts on) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts); no refresh, retry, reconnect or replay starts once it is spent, a reconnect still pending at it or landing past it ends the call with its own error, and the stdio transport writes no capped call whose cap ran out while it was being prepared; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. Its oauth table is read as Codex reads a plugin's: the camelCase aliases clientId, clientSecret, callbackUrl and callbackPort fold into their snake_case keys by key presence, a present snake_case key winning even when null, before any type is checked. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or either of them is blank by Rust's Unicode White_Space (an empty secret included), or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; a blank client ID without a secret counts as none; and a client ID or secret with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server too. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in on another one; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it. Like Codex, a server-bound sign-in stores no client secret and follows no token-endpoint redirect: the sign-in flow (mcp/oauth-reauth.ts) keeps none and its code exchange refuses redirects (MCPOAuthConfig.rejectRedirects); every refresh path, the manager's, `omp token`'s and the auth broker's (cli/auth-broker-cli.ts), names the row to refreshManagedMcpOAuthCredential, which for a bound row sends the stored client ID with only the secret the current definition configures (the manager passes it; callers without a definition send none), never a stored or retired one, and refuses redirects (refreshMCPOAuthToken's rejectRedirects, whose trailing options are read whether or not a resource is given); reauthorization likewise takes its secret from the current definition only. A secret an earlier build of this change left on a bound row is never sent; the broker's generic merge may keep it on the row. Reauthorization and TUI /mcp unauth act on a bound id only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A server whose declared name does not match Codex's ^[a-zA-Z0-9_:@/.-]+$ (validate_mcp_server_name) is skipped, as Codex refuses to start it. A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one monotonic deadline (MCPRequestOptions.deadlineAt, on performance.now(), the clock createMCPTimeout and the stdio transport also measure progress restarts on) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts); no refresh, retry, reconnect or replay starts once it is spent, a reconnect still pending at it or landing past it ends the call with its own error, and the stdio transport writes no capped call whose cap ran out while it was being prepared; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. Its oauth table is read as Codex reads a plugin's: the camelCase aliases clientId, clientSecret, callbackUrl and callbackPort fold into their snake_case keys by key presence, a present snake_case key winning even when null, before any type is checked. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or either of them is blank by Rust's Unicode White_Space (an empty secret included), or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; a blank client ID without a secret counts as none; and a client ID or secret with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server too. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in on another one; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it. Like Codex, a server-bound sign-in stores no client secret and follows no token-endpoint redirect: the sign-in flow (mcp/oauth-reauth.ts) keeps none and its code exchange refuses redirects (MCPOAuthConfig.rejectRedirects); every refresh path, the manager's, `omp token`'s and the auth broker's (cli/auth-broker-cli.ts), names the row to refreshManagedMcpOAuthCredential, which for a bound row sends the stored client ID with only the secret the current definition configures (the manager passes it; callers without a definition send none), never a stored or retired one, and refuses redirects (refreshMCPOAuthToken's rejectRedirects, whose trailing options are read whether or not a resource is given); reauthorization likewise takes its secret from the current definition only. A secret an earlier build of this change left on a bound row is never sent; the broker's generic merge may keep it on the row. Reauthorization and TUI /mcp unauth act on a bound id only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it. A host shows and signs in to MCP servers through scient-agent mcp (scient/mcp-command.ts, registered in cli-commands.ts and listed in --runtime-info hostCommands): mcp list [--server ] connects each enabled server as a conversation would and prints one JSON line of its status (connected with its tool count, needs-auth when an http or sse server answers with an auth challenge, failed, or pending when the startup window closes first), scope, plugin ID and whether a browser sign-in serves it (canSignIn in scient/mcp-sign-in-pickup.ts: http or sse, no Authorization header, and for a bound server only auth.type oauth); mcp login runs the reauthorization /mcp reauth runs, printing the authorization URL as a JSON line for the host to open (it opens no browser itself), then completed or failed, a bound server refused by an authorization server that registers only approved clients saying so. Before each RPC prompt (modes/rpc/rpc-mode.ts), pickUpMcpSignIns reconnects a disconnected server a browser sign-in serves once its stored sign-in is new or differs from the one that session last tried, so a refused sign-in is not retried every prompt, and refreshes the session's tools.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", - "packages/coding-agent/test/scient/codex-plugins-oauth.test.ts" + "packages/coding-agent/test/scient/codex-plugins-oauth.test.ts", + "packages/coding-agent/src/scient/mcp-command.ts", + "packages/coding-agent/src/scient/mcp-sign-in-pickup.ts", + "packages/coding-agent/test/scient/mcp-host-command.test.ts" ], "upstream": [ "packages/coding-agent/scripts/compile-binary.ts", "packages/coding-agent/src/capability/index.ts", "packages/coding-agent/src/capability/mcp.ts", "packages/coding-agent/src/cli/auth-broker-cli.ts", + "packages/coding-agent/src/cli-commands.ts", "packages/coding-agent/src/discovery/index.ts", "packages/coding-agent/src/mcp/config.ts", "packages/coding-agent/src/mcp/manager.ts", @@ -544,12 +548,13 @@ "packages/coding-agent/src/mcp/transports/stdio.ts", "packages/coding-agent/src/mcp/types.ts", "packages/coding-agent/src/modes/controllers/mcp-command-controller.ts", + "packages/coding-agent/src/modes/rpc/rpc-mode.ts", "packages/coding-agent/src/slash-commands/helpers/mcp.ts", "packages/coding-agent/CHANGELOG.md", "packages/utils/src/env.ts" ], "verification": [ - "bun --cwd=packages/coding-agent test test/scient/codex-plugins.test.ts test/scient/codex-plugins-oauth.test.ts" + "bun --cwd=packages/coding-agent test test/scient/codex-plugins.test.ts test/scient/codex-plugins-oauth.test.ts test/scient/mcp-host-command.test.ts" ] } } From 23bd4e4d817a497c75e7343fbecd7a955527e3a3 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 18:34:21 +0200 Subject: [PATCH 21/30] fix(mcp): host command keeps credentials out of its output and prompts unblocked - mcp list and mcp login print only messages written here (an HTTP status at most), never a server's or authorization server's text, which can echo a token, code or secret. - Both commands bind the user's settings, so enabledProviders and disabledProviders apply as in a conversation. - mcp login signs in to the definition discovery selected: an imported server's from the manager, never a same-named config entry it shadowed. - McpSignInPickup is seeded with the session's own sign-ins, runs one at a time, holds a prompt at most 1.5 s and finishes in the background; a broker is re-read at most every 10 s. - A bound server needing its client secret at every refresh is not offered or signed in to while a broker holds the sign-ins. --- packages/coding-agent/src/mcp/oauth-reauth.ts | 9 ++ .../coding-agent/src/modes/rpc/rpc-mode.ts | 19 +-- .../coding-agent/src/scient/mcp-command.ts | 120 +++++++++++++---- .../src/scient/mcp-sign-in-pickup.ts | 125 +++++++++++++----- .../test/scient/mcp-host-command.test.ts | 123 +++++++++++++---- scripts/scient/divergences.json | 2 +- 6 files changed, 304 insertions(+), 94 deletions(-) diff --git a/packages/coding-agent/src/mcp/oauth-reauth.ts b/packages/coding-agent/src/mcp/oauth-reauth.ts index 2428ad5f5a..c12ca5b192 100644 --- a/packages/coding-agent/src/mcp/oauth-reauth.ts +++ b/packages/coding-agent/src/mcp/oauth-reauth.ts @@ -41,6 +41,9 @@ import { mcpOAuthServerCredentialId, // scient(SA-CODEX-PLUGINS): end } from "./oauth-flow"; +// scient(SA-CODEX-PLUGINS): begin - an imported sign-in a broker could not refresh is not offered. +import { boundSignInNeedsLocalSecret } from "../scient/mcp-sign-in-pickup"; +// scient(SA-CODEX-PLUGINS): end import type { AuthStorage } from "../session/auth-storage"; import type { MCPAuthChallenge, MCPAuthConfig, MCPServerConfig } from "./types"; @@ -799,6 +802,12 @@ export async function reauthorizeMcpServer( `Server "${name}" signs in with its own token, so Codex does not sign in to it with OAuth.`, ); } + // A broker refreshes without the definition, so it could not send the client secret every refresh needs. + if (bound && boundSignInNeedsLocalSecret(found.config, authStorage)) { + throw new McpReauthRefusalError( + `Server "${name}" needs its client secret at every refresh, which the shared sign-in service cannot supply, so it cannot be signed in to here.`, + ); + } // scient(SA-CODEX-PLUGINS): end // Resolve endpoints first: this fails fast for stdio transports and // probes http/sse with { oauth: false }, so nothing destructive has diff --git a/packages/coding-agent/src/modes/rpc/rpc-mode.ts b/packages/coding-agent/src/modes/rpc/rpc-mode.ts index d6bd320952..5c43141952 100644 --- a/packages/coding-agent/src/modes/rpc/rpc-mode.ts +++ b/packages/coding-agent/src/modes/rpc/rpc-mode.ts @@ -110,7 +110,7 @@ import { isSessionSettled } from "../../session/session-settle"; import { RpcSessionSettleWatcher, watchedScheduledTurnProbe } from "./rpc-session-settle"; import { RpcSubagentRegistry, readRpcSubagentTranscript, resolveOwnedLiveSubagent } from "./rpc-subagents"; // scient(SA-CODEX-PLUGINS): begin - pick up MCP sign-ins made since the last prompt. -import { pickUpMcpSignIns } from "../../scient/mcp-sign-in-pickup"; +import { McpSignInPickup } from "../../scient/mcp-sign-in-pickup"; // scient(SA-CODEX-PLUGINS): end import type { RpcAbortAndRestoreQueueResult, @@ -1952,8 +1952,14 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions await emitAvailableCommandsUpdate(); const inputGate = new RpcUserInputGate(); - // scient(SA-CODEX-PLUGINS): begin - the MCP sign-ins each server last reconnected with, per session. - const mcpSignInsTried = new Map(); + // scient(SA-CODEX-PLUGINS): begin - pick up MCP sign-ins made elsewhere, starting from this session's own. + const mcpManager = options.mcpManager; + const mcpSignInPickup = mcpManager + ? new McpSignInPickup(mcpManager, session.modelRegistry.authStorage, () => + session.refreshMCPTools(mcpManager.getTools()), + ) + : undefined; + mcpSignInPickup?.seed(); // scient(SA-CODEX-PLUGINS): end type OrderedUserInput = Extract; type OrderedInputOutcome = "local" | "cancelled" | "admitted" | "builtin-agent"; @@ -2059,11 +2065,8 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions } if (!isCurrent() || !ticket) return "cancelled"; // scient(SA-CODEX-PLUGINS): begin - a server signed in from the host since the last prompt joins this one. - if (options.mcpManager) { - const manager = options.mcpManager; - await pickUpMcpSignIns(manager, session.modelRegistry.authStorage, mcpSignInsTried, () => - session.refreshMCPTools(manager.getTools()), - ); + if (mcpSignInPickup) { + await mcpSignInPickup.beforePrompt(); if (!isCurrent()) return "cancelled"; } // scient(SA-CODEX-PLUGINS): end diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts index 3807042dee..067cbcfb92 100644 --- a/packages/coding-agent/src/scient/mcp-command.ts +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -9,16 +9,21 @@ * keeps the local callback listener open, then prints `{ "type": "completed" }`, or * `{ "type": "failed", "message" }` and exits 1. SIGTERM cancels it. * - * A failure before any of that prints `{ "error": { "message" } }` and exits 1. + * A failure before any of that prints `{ "error": { "message" } }` and exits 1. Every message is + * written here: a server's or authorization server's own text, which can echo a token, a code or a + * secret, never reaches the output. */ +import { getMCPConfigPath } from "@oh-my-pi/pi-utils"; import { Args, Command, Flags } from "@oh-my-pi/pi-utils/cli"; +import { initializeWithSettings } from "../capability"; import type { SourceMeta } from "../capability/types"; import { Settings } from "../config/settings"; import { loadAllMCPConfigs } from "../mcp/config"; import { type MCPLoadResult, MCPManager } from "../mcp/manager"; import { analyzeAuthError } from "../mcp/oauth-discovery"; -import { isMcpOAuthServerCredentialId, MCPOAuthClientIdRequiredError } from "../mcp/oauth-flow"; +import { MCPOAuthClientIdRequiredError } from "../mcp/oauth-flow"; import { + type FoundMcpServer, findMcpServerForAuth, MCPOAuthCancelledError, McpReauthRefusalError, @@ -29,11 +34,10 @@ import type { MCPServerConfig } from "../mcp/types"; import type { AuthStorage } from "../session/auth-storage"; import { discoverAuthStorage } from "../sdk"; import { mcpHelp } from "./command-help"; -import { canSignIn } from "./mcp-sign-in-pickup"; +import { boundSignInNeedsLocalSecret, canSignIn } from "./mcp-sign-in-pickup"; /** How long `list` waits for servers to answer, matching a conversation's startup window. */ const LIST_WAIT_MS = 15_000; -const MAX_ERROR_LENGTH = 500; export interface McpServerStatusLine { name: string; @@ -49,8 +53,24 @@ function print(value: unknown): void { process.stdout.write(`${JSON.stringify(value)}\n`); } +/** A connection failure in words written here: the HTTP status at most, never the server's text. */ +function connectionFailure(config: MCPServerConfig | undefined, failure: string): string { + const status = /^HTTP (\d{3})\b/.exec(failure)?.[1]; + if (status) return `The server answered HTTP ${status}.`; + if (/ENOTFOUND|ECONNREFUSED|ECONNRESET|ETIMEDOUT|EAI_AGAIN|timed? ?out/i.test(failure)) { + return "The server could not be reached."; + } + if (config?.type === "stdio" || config?.type === undefined) return "The server's process did not start."; + return "The server could not be connected."; +} + /** One server's status once the startup window closed, for `list`. */ -export function serverStatusLine(manager: MCPManager, name: string, loaded: MCPLoadResult): McpServerStatusLine { +export function serverStatusLine( + manager: MCPManager, + name: string, + loaded: MCPLoadResult, + authStorage?: AuthStorage, +): McpServerStatusLine { const source = manager.getSource(name); const config = manager.getServerConfig(name); const base: McpServerStatusLine = { @@ -65,21 +85,33 @@ export function serverStatusLine(manager: MCPManager, name: string, loaded: MCPL } const failure = loaded.errors.get(name); if (failure === undefined) return base; - const error = failure.slice(0, MAX_ERROR_LENGTH); const url = config && (config.type === "http" || config.type === "sse") ? config.url : undefined; if (url && config && analyzeAuthError(new Error(failure), url).requiresAuth) { - return { ...base, status: "needs-auth", error, ...(canSignIn(config) && { canStartSignIn: true as const }) }; + const signIn = canSignIn(config) && !(authStorage && boundSignInNeedsLocalSecret(config, authStorage)); + return { + ...base, + status: "needs-auth", + error: "Sign-in needed.", + ...(signIn && { canStartSignIn: true as const }), + }; } - return { ...base, status: "failed", error }; + return { ...base, status: "failed", error: connectionFailure(config, failure) }; } -/** A sign-in failure in words a person acts on. */ -function loginFailureMessage(name: string, config: MCPServerConfig, error: unknown): string { +/** A sign-in failure in words written here. */ +export function loginFailureMessage(name: string, bound: boolean, error: unknown): string { + if (error instanceof McpReauthRefusalError) return error.message; if (error instanceof MCPOAuthCancelledError) return "The sign-in was cancelled."; - if (error instanceof MCPOAuthClientIdRequiredError && isMcpOAuthServerCredentialId(config.auth?.credentialId)) { - return `${name} lets only the apps it has approved sign in, and it does not accept Scient Agent yet.`; + if (error instanceof MCPOAuthClientIdRequiredError) { + return bound + ? `${name} lets only the apps it has approved sign in, and it does not accept Scient Agent yet.` + : `${name} needs an OAuth client ID configured before Scient Agent can sign in.`; } - return error instanceof Error ? error.message : String(error); + const message = error instanceof Error ? error.message : ""; + if (message.startsWith("OAuth flow timed out")) return "The sign-in was not finished in time. Try again."; + const status = /^Token exchange failed: (\d{3})\b/.exec(message)?.[1]; + if (status) return `${name}'s sign-in server refused the sign-in (HTTP ${status}). Try again.`; + return `Signing in to ${name} did not complete. Try again.`; } export default class Mcp extends Command { @@ -97,6 +129,8 @@ export default class Mcp extends Command { const { args, flags } = await this.parse(Mcp); const cwd = process.cwd(); const settings = await Settings.loadReadOnly(); + // Discovery honours the user's provider switches, as a conversation's does. + const releaseSettings = initializeWithSettings(settings); const authStorage = await discoverAuthStorage(undefined, { settings }); const manager = new MCPManager(cwd, null); manager.setAuthStorage(authStorage); @@ -106,22 +140,25 @@ export default class Mcp extends Command { process.once("SIGINT", onSignal); try { const only = args.action === "login" ? args.server : flags.server; - if (args.action === "login" && !only) throw new Error("Name the server to sign in to."); + if (args.action === "login" && !only) throw new McpReauthRefusalError("Name the server to sign in to."); const loaded = await loadAllMCPConfigs(cwd, { enableProjectConfig: cfgMcpEnableProjectConfig.get(settings) }); const configs = only ? Object.fromEntries(Object.entries(loaded.configs).filter(([name]) => name === only)) : loaded.configs; if (args.action === "login") await this.#login(only!, configs, loaded.sources, manager, authStorage, abort, cwd, settings); - else await this.#list(configs, loaded.sources, manager); + else await this.#list(configs, loaded.sources, manager, authStorage); } catch (error) { - print({ error: { message: error instanceof Error ? error.message : String(error) } }); + const message = + error instanceof McpReauthRefusalError ? error.message : "Scient Agent could not read its MCP servers."; + print({ error: { message } }); process.exitCode = 1; } finally { process.off("SIGTERM", onSignal); process.off("SIGINT", onSignal); await manager.disconnectAll().catch(() => {}); authStorage.close(); + releaseSettings(); } // The startup window's timer outlives a startup that settled sooner; nothing else is left to do. process.exit(); @@ -131,9 +168,10 @@ export default class Mcp extends Command { configs: Record, sources: Record, manager: MCPManager, + authStorage: AuthStorage, ): Promise { const loaded = await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); - print({ servers: Object.keys(configs).map(name => serverStatusLine(manager, name, loaded)) }); + print({ servers: Object.keys(configs).map(name => serverStatusLine(manager, name, loaded, authStorage)) }); } async #login( @@ -146,13 +184,16 @@ export default class Mcp extends Command { cwd: string, settings: Settings, ): Promise { - if (!configs[name]) throw new Error(`No MCP server named "${name}" is configured.`); + const config = configs[name]; + const source = sources[name]; + if (!config) throw new McpReauthRefusalError(`No MCP server named "${name}" is configured.`); // Registers the definition the way a conversation does, so the lookup finds discovered servers. await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); - const found = await findMcpServerForAuth(name, cwd, manager, { - enableProjectConfig: cfgMcpEnableProjectConfig.get(settings), - }); - if (!found) throw new Error(`No MCP server named "${name}" is configured.`); + const found = await selectedServer(name, config, source, manager, cwd, settings); + if (!found) { + throw new McpReauthRefusalError(`"${name}" is configured somewhere Scient Agent cannot sign in to it from.`); + } + const bound = source?.provider === CODEX_PLUGINS_PROVIDER; try { await reauthorizeMcpServer(name, found, { authStorage, @@ -165,10 +206,39 @@ export default class Mcp extends Command { }); print({ type: "completed" }); } catch (error) { - const message = - error instanceof McpReauthRefusalError ? error.message : loginFailureMessage(name, found.config, error); - print({ type: "failed", message }); + print({ type: "failed", message: loginFailureMessage(name, bound, error) }); process.exitCode = 1; } } } + +const CODEX_PLUGINS_PROVIDER = "codex-plugins"; + +/** + * The definition discovery selected for `name`, as the sign-in needs it. An imported Codex plugin + * server is answered from the manager, never a same-named config file entry that lost to it; an + * ordinary server comes from the file that holds it, so a sign-in writes back where `/mcp reauth` + * would, and only when that entry is the one discovery selected (same transport and URL). + */ +async function selectedServer( + name: string, + config: MCPServerConfig, + source: SourceMeta | undefined, + manager: MCPManager, + cwd: string, + settings: Settings, +): Promise { + if (source?.provider === CODEX_PLUGINS_PROVIDER) { + const selected = manager.getServerConfig(name) ?? config; + return { filePath: getMCPConfigPath("user", cwd), scope: "user", config: selected, discovered: true }; + } + const found = await findMcpServerForAuth(name, cwd, manager, { + enableProjectConfig: cfgMcpEnableProjectConfig.get(settings), + }); + if (!found) return null; + const url = (entry: MCPServerConfig): string | undefined => + entry.type === "http" || entry.type === "sse" ? entry.url : undefined; + if (found.discovered) return found; + const fileUrl = url(found.config); + return found.config.type === config.type && (fileUrl === url(config) || fileUrl?.includes("${")) ? found : null; +} diff --git a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts index ef54b04a84..f87dc4a420 100644 --- a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts +++ b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts @@ -14,46 +14,107 @@ export function canSignIn(config: MCPServerConfig): boolean { return config.auth === undefined || config.auth.type === "oauth"; } +/** + * Whether an imported server's sign-in would need a client secret at every refresh that the + * sign-in store cannot give: a bound sign-in keeps no secret, and a broker refreshes without the + * server's definition, so such a sign-in could not be refreshed and is not offered. + */ +export function boundSignInNeedsLocalSecret(config: MCPServerConfig, authStorage: AuthStorage): boolean { + return ( + isMcpOAuthServerCredentialId(config.auth?.credentialId) && + typeof config.oauth?.clientSecret === "string" && + authStorage.credentials.heldByBroker + ); +} + +/** How long a prompt waits for a pickup; the rest finishes in the background. */ +const PROMPT_BUDGET_MS = 1_500; +/** A broker's sign-ins are re-read at most this often, since each read is a request to it. */ +const BROKER_REREAD_MS = 10_000; + /** * Connects the MCP servers a sign-in made elsewhere (the host's Sign in, `scient-agent mcp login`) * now serves. A session reads sign-ins once, so a server that needed one stays disconnected until - * something reconnects it; this runs before a prompt. + * something reconnects it; a host calls {@link beforePrompt} before each prompt. * * Only a disconnected server that signs in with a browser is considered, and only when its stored - * sign-in differs from the one last tried here (`tried`, per session), so a server whose sign-in - * was refused is not retried on every prompt. Never throws. + * sign-in differs from the one it started with or last tried here, so a refused sign-in is not + * retried every prompt. One pickup runs at a time, and a prompt waits for it only briefly. */ -export async function pickUpMcpSignIns( - manager: MCPManager, - authStorage: AuthStorage, - tried: Map, - onConnected: () => Promise, -): Promise { - const candidates = manager.getAllServerNames().flatMap(name => { - if (manager.getConnectionStatus(name) !== "disconnected") return []; - const config = manager.getServerConfig(name); - return config && canSignIn(config) ? [{ name, config }] : []; - }); - if (candidates.length === 0) return; - try { - await authStorage.credentials.revalidate(); - } catch (err) { - logger.warn("Re-reading sign-ins for MCP servers failed", { err }); - return; +export class McpSignInPickup { + readonly #tried = new Map(); + #running: Promise | undefined; + #lastReread = Number.NEGATIVE_INFINITY; + + constructor( + private readonly manager: MCPManager, + private readonly authStorage: AuthStorage, + private readonly onConnected: () => Promise, + ) {} + + /** Records the sign-ins the session started with: its own startup already connected with them. */ + seed(): void { + for (const name of this.manager.getAllServerNames()) { + const config = this.manager.getServerConfig(name); + const signature = config && canSignIn(config) ? this.#signature(config) : undefined; + if (signature) this.#tried.set(name, signature); + } } - let connected = false; - for (const { name, config } of candidates) { - const lookup = lookupMcpOAuthCredential(authStorage, config); - if (!lookup) continue; - const signature = `${lookup.credentialId}\0${Bun.hash(lookup.credential.access ?? "")}`; - if (tried.get(name) === signature) continue; - tried.set(name, signature); + + /** Starts a pickup unless one runs, and waits for it at most a short while. Never throws. */ + async beforePrompt(budgetMs = PROMPT_BUDGET_MS): Promise { + this.#running ??= this.#pickUp().finally(() => { + this.#running = undefined; + }); + const { promise, resolve } = Promise.withResolvers(); + const timer = setTimeout(resolve, budgetMs); try { - await manager.reconnectServer(name, { manual: true }); - connected ||= manager.getConnectionStatus(name) === "connected"; - } catch (err) { - logger.warn("Connecting an MCP server with its new sign-in failed", { server: name, err }); + await Promise.race([this.#running, promise]); + } finally { + clearTimeout(timer); + } + } + + #signature(config: MCPServerConfig): string | undefined { + const lookup = lookupMcpOAuthCredential(this.authStorage, config); + return lookup ? `${lookup.credentialId}\0${Bun.hash(lookup.credential.access ?? "")}` : undefined; + } + + async #pickUp(): Promise { + const candidates = this.manager.getAllServerNames().flatMap(name => { + if (this.manager.getConnectionStatus(name) !== "disconnected") return []; + const config = this.manager.getServerConfig(name); + return config && canSignIn(config) ? [{ name, config }] : []; + }); + if (candidates.length === 0) return; + const broker = this.authStorage.credentials.heldByBroker; + if (!broker || performance.now() - this.#lastReread >= BROKER_REREAD_MS) { + this.#lastReread = performance.now(); + try { + await this.authStorage.credentials.revalidate(); + } catch (err) { + logger.warn("Re-reading sign-ins for MCP servers failed", { err }); + return; + } + } + let connected = false; + for (const { name, config } of candidates) { + const signature = this.#signature(config); + if (!signature || this.#tried.get(name) === signature) continue; + this.#tried.set(name, signature); + try { + await this.manager.reconnectServer(name, { manual: true }); + connected ||= this.manager.getConnectionStatus(name) === "connected"; + } catch (err) { + logger.warn("Connecting an MCP server with its new sign-in failed", { server: name, err }); + } + } + if (connected) { + try { + await this.onConnected(); + } catch (err) { + logger.warn("Refreshing tools after an MCP sign-in failed", { err }); + } } } - if (connected) await onConnected(); } diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index c0a7f6f094..22fab02856 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -1,25 +1,28 @@ import { Database } from "bun:sqlite"; -import { afterEach, describe, expect, it } from "bun:test"; +import { afterEach, describe, expect, it, vi } from "bun:test"; import { AuthStorage, SqliteAuthCredentialStore } from "@oh-my-pi/pi-ai"; import type { SourceMeta } from "@oh-my-pi/pi-coding-agent/capability/types"; import type { MCPLoadResult, MCPManager } from "@oh-my-pi/pi-coding-agent/mcp/manager"; import { mcpOAuthServerCredentialId } from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow"; +import { McpReauthRefusalError, reauthorizeMcpServer } from "@oh-my-pi/pi-coding-agent/mcp/oauth-reauth"; import type { MCPServerConfig } from "@oh-my-pi/pi-coding-agent/mcp/types"; -import { serverStatusLine } from "@oh-my-pi/pi-coding-agent/scient/mcp-command"; -import { pickUpMcpSignIns } from "@oh-my-pi/pi-coding-agent/scient/mcp-sign-in-pickup"; +import { loginFailureMessage, serverStatusLine } from "@oh-my-pi/pi-coding-agent/scient/mcp-command"; +import { McpSignInPickup } from "@oh-my-pi/pi-coding-agent/scient/mcp-sign-in-pickup"; const URL_A = "https://mcp.example.test/mcp"; const NAME = "tools@market:api"; -const CHALLENGE = - 'HTTP 401: Unauthorized [WWW-Authenticate: Bearer resource_metadata="https://mcp.example.test/.well-known/oauth-protected-resource"]'; +const LEAKED = "sk-live-SECRET"; +const CHALLENGE = `HTTP 401: Unauthorized {"echo":"Bearer ${LEAKED}"} [WWW-Authenticate: Bearer resource_metadata="https://mcp.example.test/.well-known/oauth-protected-resource"]`; const bound = ( auth: MCPServerConfig["auth"] = { type: "oauth", credentialId: mcpOAuthServerCredentialId(NAME, URL_A) }, -) => ({ type: "http", url: URL_A, auth }) as MCPServerConfig; + extra: Partial = {}, +) => ({ type: "http", url: URL_A, auth, ...extra }) as MCPServerConfig; const token = (access: string) => ({ type: "oauth" as const, access, refresh: "r", expires: Date.now() + 3_600_000 }); /** The few manager members these helpers read; the rest of MCPManager is not involved. */ function fakeManager( servers: Record; tools?: number }>, + reconnect: (name: string) => Promise = async () => {}, ) { const connected = new Set(); const reconnects: string[] = []; @@ -29,18 +32,20 @@ function fakeManager( getServerConfig: (name: string) => servers[name]?.config, getSource: (name: string) => servers[name]?.source, getConnection: (name: string) => - connected.has(name) || servers[name]?.tools - ? { tools: Array.from({ length: servers[name]?.tools ?? 0 }) } - : undefined, + servers[name]?.tools ? { tools: Array.from({ length: servers[name]?.tools ?? 0 }) } : undefined, reconnectServer: async (name: string) => { reconnects.push(name); + await reconnect(name); connected.add(name); return null; }, - disconnect: (name: string) => connected.delete(name), }; // Structural fake: only the members above are called by the code under test. - return { manager: manager as unknown as MCPManager, reconnects, disconnect: manager.disconnect }; + return { + manager: manager as unknown as MCPManager, + reconnects, + disconnect: (name: string) => connected.delete(name), + }; } const loadResult = (connected: string[], errors: Record): MCPLoadResult => ({ @@ -56,25 +61,29 @@ describe("scient-agent mcp list", () => { [NAME]: { config: bound(), source: { level: "user", pluginName: "tools@market" } }, "own-token": { config: bound({ type: "bearer" } as unknown as MCPServerConfig["auth"]) }, header: { config: { type: "http", url: URL_A, headers: { Authorization: "Bearer x" } } }, - down: { config: bound() }, - }); - const loaded = loadResult([], { - [NAME]: CHALLENGE, - "own-token": CHALLENGE, - header: CHALLENGE, - down: "getaddrinfo ENOTFOUND", }); + const loaded = loadResult([], { [NAME]: CHALLENGE, "own-token": CHALLENGE, header: CHALLENGE }); expect(serverStatusLine(manager, NAME, loaded)).toEqual({ name: NAME, status: "needs-auth", scope: "user", pluginId: "tools@market", - error: CHALLENGE, + error: "Sign-in needed.", canStartSignIn: true, }); expect(serverStatusLine(manager, "own-token", loaded)).not.toHaveProperty("canStartSignIn"); expect(serverStatusLine(manager, "header", loaded)).not.toHaveProperty("canStartSignIn"); - expect(serverStatusLine(manager, "down", loaded).status).toBe("failed"); + }); + + it("never prints what a server sent back, which can echo a token", () => { + const { manager } = fakeManager({ [NAME]: { config: bound() }, down: { config: bound() } }); + const loaded = loadResult([], { + [NAME]: CHALLENGE, + down: `HTTP 500: Internal Server Error {"authorization":"Bearer ${LEAKED}"}`, + }); + const lines = [serverStatusLine(manager, NAME, loaded), serverStatusLine(manager, "down", loaded)]; + expect(JSON.stringify(lines)).not.toContain(LEAKED); + expect(lines[1]).toMatchObject({ status: "failed", error: "The server answered HTTP 500." }); }); it("reports a connected server's tool count and a server still starting as pending", () => { @@ -85,6 +94,33 @@ describe("scient-agent mcp list", () => { }); }); +describe("scient-agent mcp login", () => { + it("never prints what the sign-in server sent back", () => { + const exchange = new Error(`Token exchange failed: 400 {"code":"${LEAKED}","client_secret":"${LEAKED}"}`); + const message = loginFailureMessage(NAME, true, exchange); + expect(message).not.toContain(LEAKED); + expect(message).toContain("HTTP 400"); + expect(loginFailureMessage(NAME, true, new Error(`boom ${LEAKED}`))).not.toContain(LEAKED); + }); + + it("does not offer an imported sign-in a broker could not refresh", async () => { + // A broker-held store: refresh happens without the server's definition, so without its secret. + const authStorage = { credentials: { heldByBroker: true } } as unknown as AuthStorage; + const config = bound(undefined, { + oauth: { clientId: "id", clientSecret: "secret" }, + } as Partial); + const fetchSpy = vi.spyOn(globalThis, "fetch"); + try { + await expect( + reauthorizeMcpServer(NAME, { filePath: "", scope: "user", config, discovered: true }, { authStorage }), + ).rejects.toBeInstanceOf(McpReauthRefusalError); + expect(fetchSpy).not.toHaveBeenCalled(); + } finally { + fetchSpy.mockRestore(); + } + }); +}); + describe("a conversation picking up a sign-in made elsewhere", () => { const stores: AuthStorage[] = []; afterEach(() => { @@ -99,36 +135,67 @@ describe("a conversation picking up a sign-in made elsewhere", () => { it("connects a server once its sign-in appears, and retries only when that sign-in changes", async () => { const auth = storage(); const { manager, reconnects, disconnect } = fakeManager({ [NAME]: { config: bound() } }); - const tried = new Map(); let refreshed = 0; - const pickUp = () => pickUpMcpSignIns(manager, auth, tried, async () => void refreshed++); + const pickup = new McpSignInPickup(manager, auth, async () => void refreshed++); + pickup.seed(); - await pickUp(); + await pickup.beforePrompt(); expect(reconnects).toEqual([]); await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("first")); - await pickUp(); + await pickup.beforePrompt(); expect(reconnects).toEqual([NAME]); expect(refreshed).toBe(1); // The server dropped again, e.g. its sign-in was refused: the same sign-in is not retried each prompt. disconnect(NAME); - await pickUp(); + await pickup.beforePrompt(); expect(reconnects).toEqual([NAME]); await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("second")); - await pickUp(); + await pickup.beforePrompt(); expect(reconnects).toEqual([NAME, NAME]); }); + it("does not retry the sign-in the session started with", async () => { + const auth = storage(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("startup")); + const { manager, reconnects } = fakeManager({ [NAME]: { config: bound() } }); + const pickup = new McpSignInPickup(manager, auth, async () => {}); + pickup.seed(); + await pickup.beforePrompt(); + expect(reconnects).toEqual([]); + }); + + it("holds a prompt only briefly while a reconnect is slow, and finishes it in the background", async () => { + const auth = storage(); + const gate = Promise.withResolvers(); + const { manager, reconnects } = fakeManager({ [NAME]: { config: bound() } }, () => gate.promise); + let refreshed = 0; + const pickup = new McpSignInPickup(manager, auth, async () => void refreshed++); + pickup.seed(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("new")); + + await pickup.beforePrompt(10); + expect(reconnects).toEqual([NAME]); + expect(refreshed).toBe(0); + // A second prompt joins the running pickup instead of starting another. + await pickup.beforePrompt(10); + expect(reconnects).toEqual([NAME]); + + gate.resolve(); + await pickup.beforePrompt(); + expect(refreshed).toBe(1); + }); + it("never reconnects a server a browser sign-in does not serve", async () => { const auth = storage(); await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("t")); const { manager, reconnects } = fakeManager({ - header: { config: { ...bound(), headers: { Authorization: "Bearer x" } } as MCPServerConfig }, + header: { config: bound(undefined, { headers: { Authorization: "Bearer x" } } as Partial) }, local: { config: { type: "stdio", command: "server" } as MCPServerConfig }, }); - await pickUpMcpSignIns(manager, auth, new Map(), async () => {}); + await new McpSignInPickup(manager, auth, async () => {}).beforePrompt(); expect(reconnects).toEqual([]); }); }); diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index e0f49aa881..32ea71c0de 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -520,7 +520,7 @@ }, "SA-CODEX-PLUGINS": { "status": "active", - "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A server whose declared name does not match Codex's ^[a-zA-Z0-9_:@/.-]+$ (validate_mcp_server_name) is skipped, as Codex refuses to start it. A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one monotonic deadline (MCPRequestOptions.deadlineAt, on performance.now(), the clock createMCPTimeout and the stdio transport also measure progress restarts on) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts); no refresh, retry, reconnect or replay starts once it is spent, a reconnect still pending at it or landing past it ends the call with its own error, and the stdio transport writes no capped call whose cap ran out while it was being prepared; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. Its oauth table is read as Codex reads a plugin's: the camelCase aliases clientId, clientSecret, callbackUrl and callbackPort fold into their snake_case keys by key presence, a present snake_case key winning even when null, before any type is checked. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or either of them is blank by Rust's Unicode White_Space (an empty secret included), or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; a blank client ID without a secret counts as none; and a client ID or secret with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server too. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in on another one; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it. Like Codex, a server-bound sign-in stores no client secret and follows no token-endpoint redirect: the sign-in flow (mcp/oauth-reauth.ts) keeps none and its code exchange refuses redirects (MCPOAuthConfig.rejectRedirects); every refresh path, the manager's, `omp token`'s and the auth broker's (cli/auth-broker-cli.ts), names the row to refreshManagedMcpOAuthCredential, which for a bound row sends the stored client ID with only the secret the current definition configures (the manager passes it; callers without a definition send none), never a stored or retired one, and refuses redirects (refreshMCPOAuthToken's rejectRedirects, whose trailing options are read whether or not a resource is given); reauthorization likewise takes its secret from the current definition only. A secret an earlier build of this change left on a bound row is never sent; the broker's generic merge may keep it on the row. Reauthorization and TUI /mcp unauth act on a bound id only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it. A host shows and signs in to MCP servers through scient-agent mcp (scient/mcp-command.ts, registered in cli-commands.ts and listed in --runtime-info hostCommands): mcp list [--server ] connects each enabled server as a conversation would and prints one JSON line of its status (connected with its tool count, needs-auth when an http or sse server answers with an auth challenge, failed, or pending when the startup window closes first), scope, plugin ID and whether a browser sign-in serves it (canSignIn in scient/mcp-sign-in-pickup.ts: http or sse, no Authorization header, and for a bound server only auth.type oauth); mcp login runs the reauthorization /mcp reauth runs, printing the authorization URL as a JSON line for the host to open (it opens no browser itself), then completed or failed, a bound server refused by an authorization server that registers only approved clients saying so. Before each RPC prompt (modes/rpc/rpc-mode.ts), pickUpMcpSignIns reconnects a disconnected server a browser sign-in serves once its stored sign-in is new or differs from the one that session last tried, so a refused sign-in is not retried every prompt, and refreshes the session's tools.", + "contract": "The opt-in user source codex-plugins (enabledProviders or SCIENT_AGENT_ENABLE_SOURCES; codex does not imply it) loads the skills and MCP servers of the Codex plugins enabled in $CODEX_HOME/config.toml (CODEX_HOME exactly as set, which must be a folder and is canonicalized, else ~/.codex), merged at every depth over Codex's system config.toml (/etc/codex, or OpenAI\\Codex in the ProgramData known folder), a table without enabled taking [plugins._default]. Nothing loads while [features] plugins is anything but true when set, or when the merged [plugins] table has a field of a type Codex rejects (PluginsConfigToml, apps' AppConfig included), as in Codex. CODEX_HOME, header credentials and servers' variables come from getLaunchEnv() (packages/utils env.ts: the exec environment from procfs or --no-env-file, or the compiled binary's environment before the agent's .env files load, the binary marked by __SCIENT_AGENT_NO_DOTENV__, which only scripts/compile-binary.ts defines, beside autoloadDotenv false; never with an explicit --env-file), never a .env file; when it is unknown nothing loads, with a warning. It reads the version Codex runs in $CODEX_HOME/plugins/cache/// with Codex's manifest precedence: a root Agent Plugins plugin.json (its skills/ and a regular-file mcp.json, read under that standard, sse servers left out, a stdio server taking the env_vars a valid .codex-plugin declaration of the same server as a valid stdio server adds), else .codex-plugin, .claude-plugin or .cursor-plugin/plugin.json, a link or non-file there, a linked folder holding it, or a field of a type Codex rejects rejecting the plugin. Manifest paths Codex does not take (not ./-relative, rooted, or holding .., a backslash separating parts only on Windows) are dropped, falling back as in Codex to skills/ and .mcp.json; paths it takes must resolve inside the package. Skills are found as Codex scans (every SKILL.md six levels down for a legacy plugin, direct children for an Agent Plugins package, hidden folders skipped), each folder and SKILL.md resolved inside the package before it is read, carry containRoot so skill:// stays in the package and the plugin ID as their plugin name, take their frontmatter name, or else their folder's, with its Unicode whitespace collapsed as Codex does, and follow the user config's [[skills.config]] rules in Codex's order (an absolute path, canonicalized, or a name trimmed of Unicode whitespace matching the name Codex gives the skill, :, the package folder's name standing in for a manifest name of Unicode whitespace or nothing; a [skills] table Codex cannot read gives no rules). A server whose declared name does not match Codex's ^[a-zA-Z0-9_:@/.-]+$ (validate_mcp_server_name) is skipped, as Codex refuses to start it. A legacy server must declare exactly one transport, only that transport's Codex fields, every field of the type Codex accepts (durations as Rust's Duration and u64 take them), and no other Codex environment; cwd and relative commands must stay inside the symlink-resolved package. Every imported server's tool calls have a hard cap, its tool_timeout_sec or else Codex's 300-second default, in both formats (MCPServer and MCPServerConfig toolTimeoutMs, honoured by resolveMCPRequestDeadline in mcp/timeout.ts for the stdio, http and sse transports, never extended by a call's own wait arguments or progress); one monotonic deadline (MCPRequestOptions.deadlineAt, on performance.now(), the clock createMCPTimeout and the stdio transport also measure progress restarts on) holds a logical tool call through the HTTP transport's sign-in refresh and retry after a 401 or 403 (mcp/transports/http.ts) and through the tool bridge's auth reconnect, its reconnect after a 503 or an expired session and the replay (mcp/tool-bridge.ts); no refresh, retry, reconnect or replay starts once it is spent, a reconnect still pending at it or landing past it ends the call with its own error, and the stdio transport writes no capped call whose cap ran out while it was being prepared; a server with tool_timeout_sec = 0, whose every tool call Codex times out, is skipped. A stdio server's env is what Codex would give it (its variable list, certificate bundles as absolute paths, its env_vars, then its literal env, without Codex's non-inheritable names in any case) and it inherits nothing else (MCPServer.envInherit = []); an HTTP server carries literal, origin-locked headers set in Codex's order, each replacing a header of the same name in any case, a header whose name is not an HTTP token or whose value has characters other than tab and visible ASCII skipped (http_headers, env_http_headers values used as set unless only Unicode whitespace, then the bearer_token_env_var value, which must be set, non-empty and a valid header value or the server is skipped, as Codex refuses to start it), and its OAuth client settings, scopes (an empty list kept) and oauth_resource. Its oauth table is read as Codex reads a plugin's: the camelCase aliases clientId, clientSecret, callbackUrl and callbackPort fold into their snake_case keys by key presence, a present snake_case key winning even when null, before any type is checked. The server is skipped, as Codex refuses to start it, when its client secret is present without a client ID or either of them is blank by Rust's Unicode White_Space (an empty secret included), or when it sets authorization_server_issuer or token_endpoint_auth_method, which Codex allows only with ema_auth; a blank client ID without a secret counts as none; and a client ID or secret with surrounding whitespace, which Scient Agent's sign-in would trim where Codex keeps it, skips the server too. An HTTP server signs in under its own credential, as Codex keys sign-ins by server name and URL: auth.credentialId is mcpOAuthServerCredentialId (mcp/oauth-flow.ts, mcp_oauth:server:::, which the profile and URL parsers read). Only the codex-plugins source binds one: trustedMcpAuth (mcp/oauth-flow.ts) drops a server-bound id from any definition but the one that source imported under the id its name and URL give, in mcp/config.ts, at every MCPManager connection (mcp/manager.ts, judged by the source supplied with that definition, never one an earlier definition left registered under the name) and in /mcp test, where the provenance is the selected definition's own (the TUI controller takes the live source only for a discovered selection and connects the tested definition to the manager with that source alone; slash-commands/helpers/mcp.ts reads config files, which never bind); a malformed non-string id is left alone. mcp/oauth-credentials.ts answers a bound id only for its own URL under the active profile and, when the server configures a confidential OAuth client (a client secret), only for a credential made with exactly that client ID (Codex asks for a fresh sign-in on another one; credentials that are not server-bound are looked up as before, whatever the client settings hold), never falls back from it to the URL-keyed row, and lets a configured Authorization header win over it. Like Codex, a server-bound sign-in stores no client secret and follows no token-endpoint redirect: the sign-in flow (mcp/oauth-reauth.ts) keeps none and its code exchange refuses redirects (MCPOAuthConfig.rejectRedirects); every refresh path, the manager's, `omp token`'s and the auth broker's (cli/auth-broker-cli.ts), names the row to refreshManagedMcpOAuthCredential, which for a bound row sends the stored client ID with only the secret the current definition configures (the manager passes it; callers without a definition send none), never a stored or retired one, and refuses redirects (refreshMCPOAuthToken's rejectRedirects, whose trailing options are read whether or not a resource is given); reauthorization likewise takes its secret from the current definition only. A secret an earlier build of this change left on a bound row is never sent; the broker's generic merge may keep it on the row. Reauthorization and TUI /mcp unauth act on a bound id only for a discovered server whose name and URL give it, and a sign-in challenge from a live imported server is answered for that live definition, never a same-named config-file entry. A server declaring bearer_token_env_var is bound with auth.type apikey, so no sign-in answers it and reauthorization refuses it, as Codex never offers it OAuth login. Reauthorization takes a bound server's values literally, asks for its configured scopes (an empty list meaning none, discovery's scope parameter dropped) and its oauth_resource before a discovered one, stores the credential under that id and writes nothing back; reconnecting keeps its auth block and headers and matches only a manager entry with the same credential id; TUI /mcp unauth (modes/controllers/mcp-command-controller.ts) removes only its own credential and rewrites no config, and /mcp enable and disable toggle an imported server through disabledServers. Servers are named @:, so another plugin's arrival never renames one and settings keyed by the name stay put. Left out with a warning: a plugin server the config's own [mcp_servers] names, as Codex runs that entry instead; servers two enabled plugins declare under one name, since Codex runs only one of them; and a server whose declaration or plugin mcp_servers. table says enabled = false or has an ema_auth key, so no enabledServers entry can start it. Skipped with a warning while siblings load: servers whose tools Codex narrows (enabled_tools, or a non-empty disabled_tools), hides from the model (a non-empty omit_tools_from), caps (tools..output_token_limit) or asks about regardless of its session policy (default_tools_approval_mode or tools..approval_mode of prompt or writes; auto defers, like Scient Agent's approval mode, to the session's policy), which Scient Agent cannot enforce, and servers with malformed or unsupported fields (http_headers_helper, auth other than oauth). OpenAI host-only plugins (browser, chrome, codex-app-tools, computer-use, unified-computer-use from openai-bundled), servers launched by computer-use-client-launcher or signed in with Codex's ChatGPT session, app references and hooks are not loaded, and nothing loads while an organization policy manages Codex or a policy location cannot be checked (requirements.toml in the system folder or at the 64-bit Group Policy RequirementsPath, both read on Windows through one PowerShell query that stops on any error, only its explicit success counting as no policy; /etc/codex/managed_config.toml; macOS managed preferences for com.openai.codex). Not applied: Codex profiles, project .codex/config.toml layers and cloud-delivered config. Known gaps: servers whose names differ only in characters that tool names drop (twin.a@m:api and twin-a@m:api) mint the same tool names, and the registry keeps one; a JSON timeout written 1.0 passes where Codex's u64 field rejects it. A host shows and signs in to MCP servers through scient-agent mcp (scient/mcp-command.ts, registered in cli-commands.ts and listed in --runtime-info hostCommands), which discovers with the user's settings bound (initializeWithSettings), so enabledProviders and disabledProviders apply as in a conversation: mcp list [--server ] connects each enabled server as a conversation would and prints one JSON line of its status (connected with its tool count, needs-auth when an http or sse server answers with an auth challenge, failed, or pending when the startup window closes first), scope, plugin ID and whether a browser sign-in serves it (canSignIn in scient/mcp-sign-in-pickup.ts: http or sse, no Authorization header, and for a bound server only auth.type oauth); mcp login runs the reauthorization /mcp reauth runs on the definition discovery selected (an imported server's from the manager, never a same-named config-file entry it shadowed; an ordinary server's from the file holding that same definition), printing the authorization URL as a JSON line for the host to open (it opens no browser itself), then completed or failed. Every message either command prints is written there, an HTTP status at most, never a server's or authorization server's own text, which can echo a token, a code or a secret. While a broker holds the sign-ins, a bound server that configures a client secret is neither offered (no canStartSignIn) nor signed in to (reauthorization refuses it in mcp/oauth-reauth.ts), since the broker refreshes without the definition and so without the secret. In RPC mode (modes/rpc/rpc-mode.ts) an McpSignInPickup, seeded with the sign-ins the session started with, runs before each prompt and reconnects a disconnected server a browser sign-in serves once its stored sign-in differs from the one it started with or last tried, so a refused sign-in is not retried every prompt; one pickup runs at a time, a prompt waits for it at most 1.5 seconds while the rest finishes in the background, a broker's sign-ins are re-read at most every 10 seconds, and the session's tools are refreshed when a server connects. Unchanged from Oh My Pi and the same for every MCP server: the transport's connection error and the token exchange's error carry the response text, and a failed refresh is logged with its error.", "owned": [ "packages/coding-agent/src/scient/codex-plugins.ts", "packages/coding-agent/test/scient/codex-plugins.test.ts", From fe1fe04045a3ec35a5fa208262a0707f67a8770e Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 18:41:25 +0200 Subject: [PATCH 22/30] fix(mcp): mcp login shows no keyed sign-in page and signs in to the selected entry - mcp login withholds a sign-in page whose resource carries a user name, password or query (a server's own key) from the host and stops the flow. - An ordinary server signs in from the file discovery read it from, only when that entry expanded matches the selected definition; a definition no file holds signs in as discovered. - The sign-in pickup treats a malformed definition's lookup as no sign-in, so seeding cannot stop RPC startup. --- .../coding-agent/src/scient/mcp-command.ts | 90 +++++++++++++------ .../src/scient/mcp-sign-in-pickup.ts | 9 +- .../test/scient/mcp-host-command.test.ts | 52 ++++++++++- 3 files changed, 123 insertions(+), 28 deletions(-) diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts index 067cbcfb92..cd31729114 100644 --- a/packages/coding-agent/src/scient/mcp-command.ts +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -18,13 +18,14 @@ import { Args, Command, Flags } from "@oh-my-pi/pi-utils/cli"; import { initializeWithSettings } from "../capability"; import type { SourceMeta } from "../capability/types"; import { Settings } from "../config/settings"; +import { expandEnvVarsDeep } from "../discovery/helpers"; import { loadAllMCPConfigs } from "../mcp/config"; +import { readMCPConfigFile } from "../mcp/config-writer"; import { type MCPLoadResult, MCPManager } from "../mcp/manager"; import { analyzeAuthError } from "../mcp/oauth-discovery"; import { MCPOAuthClientIdRequiredError } from "../mcp/oauth-flow"; import { type FoundMcpServer, - findMcpServerForAuth, MCPOAuthCancelledError, McpReauthRefusalError, reauthorizeMcpServer, @@ -146,7 +147,7 @@ export default class Mcp extends Command { ? Object.fromEntries(Object.entries(loaded.configs).filter(([name]) => name === only)) : loaded.configs; if (args.action === "login") - await this.#login(only!, configs, loaded.sources, manager, authStorage, abort, cwd, settings); + await this.#login(only!, configs, loaded.sources, manager, authStorage, abort, cwd); else await this.#list(configs, loaded.sources, manager, authStorage); } catch (error) { const message = @@ -182,18 +183,18 @@ export default class Mcp extends Command { authStorage: AuthStorage, abort: AbortController, cwd: string, - settings: Settings, ): Promise { const config = configs[name]; const source = sources[name]; if (!config) throw new McpReauthRefusalError(`No MCP server named "${name}" is configured.`); - // Registers the definition the way a conversation does, so the lookup finds discovered servers. + // Registers the definition the way a conversation does, so the sign-in uses the one it selected. await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); - const found = await selectedServer(name, config, source, manager, cwd, settings); + const found = await selectedServer(name, config, source, manager, cwd); if (!found) { throw new McpReauthRefusalError(`"${name}" is configured somewhere Scient Agent cannot sign in to it from.`); } const bound = source?.provider === CODEX_PLUGINS_PROVIDER; + let withheld = false; try { await reauthorizeMcpServer(name, found, { authStorage, @@ -201,12 +202,22 @@ export default class Mcp extends Command { cwd, mcpManager: manager, silent: true, - onAuth: info => print({ type: "authorization_url", url: info.url }), + onAuth: info => { + if (!authorizationUrlIsPublic(info.url)) { + withheld = true; + abort.abort(); + return; + } + print({ type: "authorization_url", url: info.url }); + }, onProgress: () => {}, }); print({ type: "completed" }); } catch (error) { - print({ type: "failed", message: loginFailureMessage(name, bound, error) }); + const message = withheld + ? `${name}'s address carries values of its own, which Scient Agent does not send to a sign-in page.` + : loginFailureMessage(name, bound, error); + print({ type: "failed", message }); process.exitCode = 1; } } @@ -214,31 +225,60 @@ export default class Mcp extends Command { const CODEX_PLUGINS_PROVIDER = "codex-plugins"; +/** + * Whether a sign-in page may be shown: the resource it names (the server's address, when discovery + * gave no other) carries no user name, password or query, which could hold the server's own key. + */ +export function authorizationUrlIsPublic(authorizationUrl: string): boolean { + try { + return new URL(authorizationUrl).searchParams.getAll("resource").every(resource => { + const url = new URL(resource); + return !url.username && !url.password && !url.search; + }); + } catch { + return false; + } +} + +const transportOf = (config: MCPServerConfig): string => + config.type ?? ("url" in config && typeof config.url === "string" ? "http" : "stdio"); +const urlOf = (config: MCPServerConfig): string | undefined => + "url" in config && typeof config.url === "string" ? config.url : undefined; + /** * The definition discovery selected for `name`, as the sign-in needs it. An imported Codex plugin - * server is answered from the manager, never a same-named config file entry that lost to it; an - * ordinary server comes from the file that holds it, so a sign-in writes back where `/mcp reauth` - * would, and only when that entry is the one discovery selected (same transport and URL). + * server is answered from the manager, never a same-named config file entry that lost to it. An + * ordinary server comes from the very file discovery read it from, so a sign-in writes back where + * `/mcp reauth` would, and only when that entry, expanded, is the selected one (same transport and + * URL); a definition no readable config file holds (a plugin's) signs in as a discovered server. */ -async function selectedServer( +export async function selectedServer( name: string, config: MCPServerConfig, source: SourceMeta | undefined, manager: MCPManager, cwd: string, - settings: Settings, ): Promise { - if (source?.provider === CODEX_PLUGINS_PROVIDER) { - const selected = manager.getServerConfig(name) ?? config; - return { filePath: getMCPConfigPath("user", cwd), scope: "user", config: selected, discovered: true }; - } - const found = await findMcpServerForAuth(name, cwd, manager, { - enableProjectConfig: cfgMcpEnableProjectConfig.get(settings), - }); - if (!found) return null; - const url = (entry: MCPServerConfig): string | undefined => - entry.type === "http" || entry.type === "sse" ? entry.url : undefined; - if (found.discovered) return found; - const fileUrl = url(found.config); - return found.config.type === config.type && (fileUrl === url(config) || fileUrl?.includes("${")) ? found : null; + const selected = manager.getServerConfig(name) ?? config; + const discovered: FoundMcpServer = { + filePath: getMCPConfigPath("user", cwd), + scope: "user", + config: selected, + discovered: true, + }; + if (source?.provider === CODEX_PLUGINS_PROVIDER) return discovered; + if (!source) return null; + const entry = await readMCPConfigFile(source.path).then( + file => file.mcpServers?.[name], + () => undefined, + ); + if (!entry) return discovered; + const expanded = expandEnvVarsDeep(entry); + if (transportOf(expanded) !== transportOf(selected) || urlOf(expanded) !== urlOf(selected)) return null; + return { + filePath: source.path, + scope: source.level === "project" ? "project" : "user", + config: entry, + discovered: false, + }; } diff --git a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts index f87dc4a420..fb721e64ea 100644 --- a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts +++ b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts @@ -75,9 +75,14 @@ export class McpSignInPickup { } } + /** The stored sign-in a server would use; a malformed definition counts as none, never as a failure. */ #signature(config: MCPServerConfig): string | undefined { - const lookup = lookupMcpOAuthCredential(this.authStorage, config); - return lookup ? `${lookup.credentialId}\0${Bun.hash(lookup.credential.access ?? "")}` : undefined; + try { + const lookup = lookupMcpOAuthCredential(this.authStorage, config); + return lookup ? `${lookup.credentialId}\0${Bun.hash(lookup.credential.access ?? "")}` : undefined; + } catch { + return undefined; + } } async #pickUp(): Promise { diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index 22fab02856..01e14eb6a3 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -6,7 +6,14 @@ import type { MCPLoadResult, MCPManager } from "@oh-my-pi/pi-coding-agent/mcp/ma import { mcpOAuthServerCredentialId } from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow"; import { McpReauthRefusalError, reauthorizeMcpServer } from "@oh-my-pi/pi-coding-agent/mcp/oauth-reauth"; import type { MCPServerConfig } from "@oh-my-pi/pi-coding-agent/mcp/types"; -import { loginFailureMessage, serverStatusLine } from "@oh-my-pi/pi-coding-agent/scient/mcp-command"; +import * as path from "node:path"; +import { TempDir } from "@oh-my-pi/pi-utils"; +import { + authorizationUrlIsPublic, + loginFailureMessage, + selectedServer, + serverStatusLine, +} from "@oh-my-pi/pi-coding-agent/scient/mcp-command"; import { McpSignInPickup } from "@oh-my-pi/pi-coding-agent/scient/mcp-sign-in-pickup"; const URL_A = "https://mcp.example.test/mcp"; @@ -199,3 +206,46 @@ describe("a conversation picking up a sign-in made elsewhere", () => { expect(reconnects).toEqual([]); }); }); + +describe("which server mcp login signs in to", () => { + it("never shows a sign-in page whose resource carries the server's own key", () => { + const page = (resource: string) => + `https://auth.example.test/authorize?client_id=c&resource=${encodeURIComponent(resource)}`; + expect(authorizationUrlIsPublic(page("https://mcp.example.test/mcp"))).toBe(true); + expect(authorizationUrlIsPublic(page(`https://mcp.example.test/mcp?key=${LEAKED}`))).toBe(false); + expect(authorizationUrlIsPublic(page(`https://user:${LEAKED}@mcp.example.test/mcp`))).toBe(false); + }); + + it("signs an ordinary server in from the file discovery read, only when that entry is the selected one", async () => { + using temp = TempDir.createSync("@scient-mcp-login-select-"); + const file = path.join(temp.path(), ".mcp.json"); + await Bun.write(file, JSON.stringify({ mcpServers: { api: { type: "http", url: URL_A } } })); + const source = { provider: "mcp-json", providerName: "", path: file, level: "project" } as SourceMeta; + const selected = { type: "http", url: URL_A } as MCPServerConfig; + const { manager } = fakeManager({ api: { config: selected } }); + expect(await selectedServer("api", selected, source, manager, temp.path())).toMatchObject({ + filePath: file, + scope: "project", + discovered: false, + }); + // The file's entry now points elsewhere than the server discovery selected: no sign-in for it. + const other = { type: "http", url: "https://elsewhere.example.test/mcp" } as MCPServerConfig; + const { manager: otherManager } = fakeManager({ api: { config: other } }); + expect(await selectedServer("api", other, source, otherManager, temp.path())).toBeNull(); + }); +}); + +it("a malformed sign-in id never stops a conversation from starting", () => { + const auth = new AuthStorage(new SqliteAuthCredentialStore(new Database(":memory:"))); + try { + const config = { + type: "http", + url: URL_A, + auth: { type: "oauth", credentialId: 42 }, + } as unknown as MCPServerConfig; + const { manager } = fakeManager({ broken: { config } }); + expect(() => new McpSignInPickup(manager, auth, async () => {}).seed()).not.toThrow(); + } finally { + auth.close(); + } +}); From ab3f275cd098ef37497a492c83bef1635aba4504 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 18:46:08 +0200 Subject: [PATCH 23/30] fix(mcp): mcp login signs in to URL-only entries over the inferred transport - A Cursor or Claude entry that gives only a URL reaches reauthorization with the transport discovery inferred, keeping its raw placeholders for write-back; before, reauthorization refused it as stdio. --- packages/coding-agent/src/scient/mcp-command.ts | 4 +++- .../test/scient/mcp-host-command.test.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts index cd31729114..66b8ca8c26 100644 --- a/packages/coding-agent/src/scient/mcp-command.ts +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -275,10 +275,12 @@ export async function selectedServer( if (!entry) return discovered; const expanded = expandEnvVarsDeep(entry); if (transportOf(expanded) !== transportOf(selected) || urlOf(expanded) !== urlOf(selected)) return null; + // The raw entry keeps its placeholders for write-back; a URL-only entry (Cursor, Claude) takes + // the transport discovery inferred, as the sign-in needs it. return { filePath: source.path, scope: source.level === "project" ? "project" : "user", - config: entry, + config: (entry.type ? entry : { ...entry, type: transportOf(selected) }) as MCPServerConfig, discovered: false, }; } diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index 01e14eb6a3..ee75d3de46 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -233,6 +233,18 @@ describe("which server mcp login signs in to", () => { const { manager: otherManager } = fakeManager({ api: { config: other } }); expect(await selectedServer("api", other, source, otherManager, temp.path())).toBeNull(); }); + + it("signs in to a URL-only entry (Cursor, Claude) over the transport discovery inferred", async () => { + using temp = TempDir.createSync("@scient-mcp-login-urlonly-"); + const file = path.join(temp.path(), "mcp.json"); + await Bun.write(file, JSON.stringify({ mcpServers: { api: { url: URL_A } } })); + const source = { provider: "cursor", providerName: "", path: file, level: "user" } as SourceMeta; + const selected = { type: "http", url: URL_A } as MCPServerConfig; + const { manager } = fakeManager({ api: { config: selected } }); + const found = await selectedServer("api", selected, source, manager, temp.path()); + expect(found?.config).toMatchObject({ type: "http", url: URL_A }); + expect(found?.filePath).toBe(file); + }); }); it("a malformed sign-in id never stops a conversation from starting", () => { From da0eaa901b22e0752ee264e6734118a3e628a831 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 18:50:17 +0200 Subject: [PATCH 24/30] fix(mcp): mcp login words every refusal itself - A server a browser sign-in cannot serve (turned off, local, own key, broker without its secret) is refused before reauthorization in words written here; a refusal from the shared engine, whose text can quote a server's expanded arguments, is printed as a fixed message. --- packages/coding-agent/src/scient/mcp-command.ts | 17 +++++++++++++++-- .../test/scient/mcp-host-command.test.ts | 3 +++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts index 66b8ca8c26..41d528b179 100644 --- a/packages/coding-agent/src/scient/mcp-command.ts +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -99,9 +99,9 @@ export function serverStatusLine( return { ...base, status: "failed", error: connectionFailure(config, failure) }; } -/** A sign-in failure in words written here. */ +/** A sign-in failure in words written here; the engine's own refusal text can quote a server's arguments. */ export function loginFailureMessage(name: string, bound: boolean, error: unknown): string { - if (error instanceof McpReauthRefusalError) return error.message; + if (error instanceof McpReauthRefusalError) return `${name} cannot be signed in to with a browser from here.`; if (error instanceof MCPOAuthCancelledError) return "The sign-in was cancelled."; if (error instanceof MCPOAuthClientIdRequiredError) { return bound @@ -193,6 +193,19 @@ export default class Mcp extends Command { if (!found) { throw new McpReauthRefusalError(`"${name}" is configured somewhere Scient Agent cannot sign in to it from.`); } + // Cases a browser sign-in cannot serve, refused here in words written here. + const selected = manager.getServerConfig(name) ?? config; + if (selected.enabled === false) throw new McpReauthRefusalError(`${name} is turned off.`); + if (!canSignIn(selected)) { + throw new McpReauthRefusalError( + `${name} does not sign in with a browser: it runs on this computer or uses its own key.`, + ); + } + if (boundSignInNeedsLocalSecret(selected, authStorage)) { + throw new McpReauthRefusalError( + `${name} needs its client secret at every refresh, which the shared sign-in service cannot supply.`, + ); + } const bound = source?.provider === CODEX_PLUGINS_PROVIDER; let withheld = false; try { diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index ee75d3de46..505b28cfb7 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -108,6 +108,9 @@ describe("scient-agent mcp login", () => { expect(message).not.toContain(LEAKED); expect(message).toContain("HTTP 400"); expect(loginFailureMessage(NAME, true, new Error(`boom ${LEAKED}`))).not.toContain(LEAKED); + // The engine's refusal for a local server quotes its expanded URL argument. + const refusal = new McpReauthRefusalError(`use https://remote.example.test/mcp?token=${LEAKED} instead`); + expect(loginFailureMessage(NAME, false, refusal)).not.toContain(LEAKED); }); it("does not offer an imported sign-in a broker could not refresh", async () => { From dbddbf802b6eaa177df909d2a503f6c4b0b78068 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 18:55:50 +0200 Subject: [PATCH 25/30] fix(mcp): mcp login refuses a keyed server address before any request - A server address with a user name, password or query is refused before reauthorization starts, so no request carries it as a resource. - A sign-in page whose own address carries a user name or password is withheld; its query (OAuth parameters, the sign-in server's settings) is kept. --- .../coding-agent/src/scient/mcp-command.ts | 29 +++++++++++++++---- .../test/scient/mcp-host-command.test.ts | 3 ++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts index 41d528b179..7cd143c27c 100644 --- a/packages/coding-agent/src/scient/mcp-command.ts +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -206,6 +206,12 @@ export default class Mcp extends Command { `${name} needs its client secret at every refresh, which the shared sign-in service cannot supply.`, ); } + // A key in the server's own address would reach the sign-in server as its resource: refused before any request. + if (!addressIsPublic(urlOf(selected))) { + throw new McpReauthRefusalError( + `${name}'s address carries values of its own, which Scient Agent does not send to a sign-in page.`, + ); + } const bound = source?.provider === CODEX_PLUGINS_PROVIDER; let withheld = false; try { @@ -238,16 +244,27 @@ export default class Mcp extends Command { const CODEX_PLUGINS_PROVIDER = "codex-plugins"; +/** Whether an address carries no user name, password or query, any of which could hold a key. */ +function addressIsPublic(address: string | undefined): boolean { + if (address === undefined) return true; + try { + const url = new URL(address); + return !url.username && !url.password && !url.search; + } catch { + return false; + } +} + /** - * Whether a sign-in page may be shown: the resource it names (the server's address, when discovery - * gave no other) carries no user name, password or query, which could hold the server's own key. + * Whether a sign-in page may be shown: it carries no user name or password, and the resource it + * names (the server's address, when discovery gave no other) no user name, password or query. + * The page's own query is the sign-in server's (OAuth parameters, its own settings) and is kept. */ export function authorizationUrlIsPublic(authorizationUrl: string): boolean { try { - return new URL(authorizationUrl).searchParams.getAll("resource").every(resource => { - const url = new URL(resource); - return !url.username && !url.password && !url.search; - }); + const page = new URL(authorizationUrl); + if (page.username || page.password) return false; + return page.searchParams.getAll("resource").every(resource => addressIsPublic(resource)); } catch { return false; } diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index 505b28cfb7..6c00e34ac9 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -217,6 +217,9 @@ describe("which server mcp login signs in to", () => { expect(authorizationUrlIsPublic(page("https://mcp.example.test/mcp"))).toBe(true); expect(authorizationUrlIsPublic(page(`https://mcp.example.test/mcp?key=${LEAKED}`))).toBe(false); expect(authorizationUrlIsPublic(page(`https://user:${LEAKED}@mcp.example.test/mcp`))).toBe(false); + expect(authorizationUrlIsPublic(`https://u:${LEAKED}@auth.example.test/authorize?client_id=c`)).toBe(false); + // The sign-in server's own settings in its page address are its own and kept. + expect(authorizationUrlIsPublic(`https://auth.example.test/authorize?tenant=t&client_id=c`)).toBe(true); }); it("signs an ordinary server in from the file discovery read, only when that entry is the selected one", async () => { From 422c23bae732b71baae7a72266fe882837e2df65 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 19:00:20 +0200 Subject: [PATCH 26/30] fix(mcp): mcp login refuses before connecting; no Sign in for a keyed address - mcp login checks the discovered definition (turned off, no browser sign-in, broker without its secret, keyed address) before any connection, so not even a refresh of a stored sign-in carries a keyed address. - mcp list offers no sign-in for a server whose address carries a user name, password or query. --- .../coding-agent/src/scient/mcp-command.ts | 30 ++++++++++--------- .../test/scient/mcp-host-command.test.ts | 5 +++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts index 7cd143c27c..78a0bdd495 100644 --- a/packages/coding-agent/src/scient/mcp-command.ts +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -88,7 +88,10 @@ export function serverStatusLine( if (failure === undefined) return base; const url = config && (config.type === "http" || config.type === "sse") ? config.url : undefined; if (url && config && analyzeAuthError(new Error(failure), url).requiresAuth) { - const signIn = canSignIn(config) && !(authStorage && boundSignInNeedsLocalSecret(config, authStorage)); + const signIn = + canSignIn(config) && + addressIsPublic(url) && + !(authStorage && boundSignInNeedsLocalSecret(config, authStorage)); return { ...base, status: "needs-auth", @@ -187,31 +190,30 @@ export default class Mcp extends Command { const config = configs[name]; const source = sources[name]; if (!config) throw new McpReauthRefusalError(`No MCP server named "${name}" is configured.`); - // Registers the definition the way a conversation does, so the sign-in uses the one it selected. - await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); - const found = await selectedServer(name, config, source, manager, cwd); - if (!found) { - throw new McpReauthRefusalError(`"${name}" is configured somewhere Scient Agent cannot sign in to it from.`); - } - // Cases a browser sign-in cannot serve, refused here in words written here. - const selected = manager.getServerConfig(name) ?? config; - if (selected.enabled === false) throw new McpReauthRefusalError(`${name} is turned off.`); - if (!canSignIn(selected)) { + // Cases a browser sign-in cannot serve, refused here in words written here, before any connection. + if (config.enabled === false) throw new McpReauthRefusalError(`${name} is turned off.`); + if (!canSignIn(config)) { throw new McpReauthRefusalError( `${name} does not sign in with a browser: it runs on this computer or uses its own key.`, ); } - if (boundSignInNeedsLocalSecret(selected, authStorage)) { + if (boundSignInNeedsLocalSecret(config, authStorage)) { throw new McpReauthRefusalError( `${name} needs its client secret at every refresh, which the shared sign-in service cannot supply.`, ); } - // A key in the server's own address would reach the sign-in server as its resource: refused before any request. - if (!addressIsPublic(urlOf(selected))) { + // A key in the server's own address would reach a sign-in server as its resource, even from a refresh. + if (!addressIsPublic(urlOf(config))) { throw new McpReauthRefusalError( `${name}'s address carries values of its own, which Scient Agent does not send to a sign-in page.`, ); } + // Registers the definition the way a conversation does, so the sign-in uses the one it selected. + await manager.connectServers(configs, sources, undefined, LIST_WAIT_MS); + const found = await selectedServer(name, config, source, manager, cwd); + if (!found) { + throw new McpReauthRefusalError(`"${name}" is configured somewhere Scient Agent cannot sign in to it from.`); + } const bound = source?.provider === CODEX_PLUGINS_PROVIDER; let withheld = false; try { diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index 6c00e34ac9..85815939ec 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -68,8 +68,9 @@ describe("scient-agent mcp list", () => { [NAME]: { config: bound(), source: { level: "user", pluginName: "tools@market" } }, "own-token": { config: bound({ type: "bearer" } as unknown as MCPServerConfig["auth"]) }, header: { config: { type: "http", url: URL_A, headers: { Authorization: "Bearer x" } } }, + keyed: { config: { type: "http", url: `${URL_A}?key=${LEAKED}` } as MCPServerConfig }, }); - const loaded = loadResult([], { [NAME]: CHALLENGE, "own-token": CHALLENGE, header: CHALLENGE }); + const loaded = loadResult([], { [NAME]: CHALLENGE, "own-token": CHALLENGE, header: CHALLENGE, keyed: CHALLENGE }); expect(serverStatusLine(manager, NAME, loaded)).toEqual({ name: NAME, status: "needs-auth", @@ -80,6 +81,8 @@ describe("scient-agent mcp list", () => { }); expect(serverStatusLine(manager, "own-token", loaded)).not.toHaveProperty("canStartSignIn"); expect(serverStatusLine(manager, "header", loaded)).not.toHaveProperty("canStartSignIn"); + // A key in the server's address would ride along as the sign-in's resource: no button for it. + expect(serverStatusLine(manager, "keyed", loaded)).not.toHaveProperty("canStartSignIn"); }); it("never prints what a server sent back, which can echo a token", () => { From 8db5d84b70595cace0ae42cd62d47c2162c23de6 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 19:04:44 +0200 Subject: [PATCH 27/30] fix(mcp): an ordinary server with auth: null no longer stops RPC startup - canSignIn reads a config file's auth: null as no auth block, as the credential lookup does, so seeding the sign-in pickup cannot throw. --- .../coding-agent/src/scient/mcp-sign-in-pickup.ts | 6 ++++-- .../test/scient/mcp-host-command.test.ts | 13 +++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts index fb721e64ea..dbed386448 100644 --- a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts +++ b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts @@ -9,9 +9,11 @@ import type { AuthStorage } from "../session/auth-storage"; export function canSignIn(config: MCPServerConfig): boolean { if (config.type !== "http" && config.type !== "sse") return false; if (hasMcpAuthorizationHeader(config)) return false; + // A config file can hold `auth: null`, which means no auth block, as the credential lookup reads it. + const auth = config.auth ?? undefined; // A server bound to its own sign-in (an imported Codex plugin server) signs in only with OAuth. - if (isMcpOAuthServerCredentialId(config.auth?.credentialId)) return config.auth?.type === "oauth"; - return config.auth === undefined || config.auth.type === "oauth"; + if (isMcpOAuthServerCredentialId(auth?.credentialId)) return auth?.type === "oauth"; + return auth === undefined || auth.type === "oauth"; } /** diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index 85815939ec..e936bdf66c 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -259,12 +259,13 @@ describe("which server mcp login signs in to", () => { it("a malformed sign-in id never stops a conversation from starting", () => { const auth = new AuthStorage(new SqliteAuthCredentialStore(new Database(":memory:"))); try { - const config = { - type: "http", - url: URL_A, - auth: { type: "oauth", credentialId: 42 }, - } as unknown as MCPServerConfig; - const { manager } = fakeManager({ broken: { config } }); + const malformed = { type: "http", url: URL_A, auth: { type: "oauth", credentialId: 42 } }; + // A config file's `auth: null` on an ordinary server means no auth block. + const nullAuth = { type: "http", url: URL_A, auth: null }; + const { manager } = fakeManager({ + broken: { config: malformed as unknown as MCPServerConfig }, + plain: { config: nullAuth as unknown as MCPServerConfig }, + }); expect(() => new McpSignInPickup(manager, auth, async () => {}).seed()).not.toThrow(); } finally { auth.close(); From a661944715f2fb5fe3e58bf43c8f276ea441f939 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 19:12:57 +0200 Subject: [PATCH 28/30] fix(mcp): pickup reconnects servers independently, before skills too; mcp output fully flushed - McpSignInPickup reconnects each newly signed-in server on its own, never twice at once, and publishes tools as each connects, so a stalled server holds up no other. - RPC runs the pickup before skill dispatch, so a skill invoked right after a sign-in sees the new tools. - mcp list and mcp login wait for stdout to drain before exiting; a failed write exits nonzero. --- .../coding-agent/src/modes/rpc/rpc-mode.ts | 12 +-- .../coding-agent/src/scient/mcp-command.ts | 13 ++- .../src/scient/mcp-sign-in-pickup.ts | 84 +++++++++++-------- .../test/scient/mcp-host-command.test.ts | 30 ++++++- 4 files changed, 97 insertions(+), 42 deletions(-) diff --git a/packages/coding-agent/src/modes/rpc/rpc-mode.ts b/packages/coding-agent/src/modes/rpc/rpc-mode.ts index 5c43141952..20a0fc24f2 100644 --- a/packages/coding-agent/src/modes/rpc/rpc-mode.ts +++ b/packages/coding-agent/src/modes/rpc/rpc-mode.ts @@ -1984,6 +1984,12 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions } if (!isCurrent()) return "cancelled"; if (!text.trim() && !images?.length) return "local"; + // scient(SA-CODEX-PLUGINS): begin - a server signed in from the host since the last prompt joins this one, skills included. + if (mcpSignInPickup) { + await mcpSignInPickup.beforePrompt(); + if (!isCurrent()) return "cancelled"; + } + // scient(SA-CODEX-PLUGINS): end if (command.type === "steer") { await session.steer(text, images); return "admitted"; @@ -2064,12 +2070,6 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions } } if (!isCurrent() || !ticket) return "cancelled"; - // scient(SA-CODEX-PLUGINS): begin - a server signed in from the host since the last prompt joins this one. - if (mcpSignInPickup) { - await mcpSignInPickup.beforePrompt(); - if (!isCurrent()) return "cancelled"; - } - // scient(SA-CODEX-PLUGINS): end await watchAndReportPromptResult({ ticket, startPrompt: onPromptAdmitted => diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts index 78a0bdd495..0651a70a35 100644 --- a/packages/coding-agent/src/scient/mcp-command.ts +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -54,6 +54,16 @@ function print(value: unknown): void { process.stdout.write(`${JSON.stringify(value)}\n`); } +/** Waits until everything printed has reached stdout; a write that failed makes the exit nonzero. */ +function flushStdout(): Promise { + const { promise, resolve } = Promise.withResolvers(); + process.stdout.write("", error => { + if (error) process.exitCode = 1; + resolve(); + }); + return promise; +} + /** A connection failure in words written here: the HTTP status at most, never the server's text. */ function connectionFailure(config: MCPServerConfig | undefined, failure: string): string { const status = /^HTTP (\d{3})\b/.exec(failure)?.[1]; @@ -164,7 +174,8 @@ export default class Mcp extends Command { authStorage.close(); releaseSettings(); } - // The startup window's timer outlives a startup that settled sooner; nothing else is left to do. + // The startup window's timer outlives a startup that settled sooner; once the output is out, nothing is left to do. + await flushStdout(); process.exit(); } diff --git a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts index dbed386448..e7e41fdf86 100644 --- a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts +++ b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts @@ -41,12 +41,15 @@ const BROKER_REREAD_MS = 10_000; * * Only a disconnected server that signs in with a browser is considered, and only when its stored * sign-in differs from the one it started with or last tried here, so a refused sign-in is not - * retried every prompt. One pickup runs at a time, and a prompt waits for it only briefly. + * retried every prompt. Each server reconnects on its own and its tools are published as soon as it + * connects, so a stalled server holds up no other; a prompt waits for a pickup only briefly. */ export class McpSignInPickup { readonly #tried = new Map(); - #running: Promise | undefined; + readonly #connecting = new Set(); + #reread: Promise | undefined; #lastReread = Number.NEGATIVE_INFINITY; + #published: Promise = Promise.resolve(); constructor( private readonly manager: MCPManager, @@ -63,15 +66,12 @@ export class McpSignInPickup { } } - /** Starts a pickup unless one runs, and waits for it at most a short while. Never throws. */ + /** Starts a pickup and waits for it at most a short while; reconnects go on in the background. Never throws. */ async beforePrompt(budgetMs = PROMPT_BUDGET_MS): Promise { - this.#running ??= this.#pickUp().finally(() => { - this.#running = undefined; - }); const { promise, resolve } = Promise.withResolvers(); const timer = setTimeout(resolve, budgetMs); try { - await Promise.race([this.#running, promise]); + await Promise.race([this.#pickUp(), promise]); } finally { clearTimeout(timer); } @@ -87,41 +87,57 @@ export class McpSignInPickup { } } - async #pickUp(): Promise { - const candidates = this.manager.getAllServerNames().flatMap(name => { - if (this.manager.getConnectionStatus(name) !== "disconnected") return []; - const config = this.manager.getServerConfig(name); - return config && canSignIn(config) ? [{ name, config }] : []; - }); - if (candidates.length === 0) return; - const broker = this.authStorage.credentials.heldByBroker; - if (!broker || performance.now() - this.#lastReread >= BROKER_REREAD_MS) { + /** Re-reads the sign-in store once for concurrent pickups; a broker at most every few seconds. */ + #rereadSignIns(): Promise { + if (this.authStorage.credentials.heldByBroker && performance.now() - this.#lastReread < BROKER_REREAD_MS) { + return Promise.resolve(true); + } + this.#reread ??= (async () => { this.#lastReread = performance.now(); try { await this.authStorage.credentials.revalidate(); + return true; } catch (err) { logger.warn("Re-reading sign-ins for MCP servers failed", { err }); - return; + return false; + } finally { + this.#reread = undefined; } - } - let connected = false; - for (const { name, config } of candidates) { + })(); + return this.#reread; + } + + async #pickUp(): Promise { + const candidates = this.manager.getAllServerNames().flatMap(name => { + if (this.#connecting.has(name) || this.manager.getConnectionStatus(name) !== "disconnected") return []; + const config = this.manager.getServerConfig(name); + return config && canSignIn(config) ? [{ name, config }] : []; + }); + if (candidates.length === 0 || !(await this.#rereadSignIns())) return; + const reconnects = candidates.flatMap(({ name, config }) => { + if (this.#connecting.has(name)) return []; const signature = this.#signature(config); - if (!signature || this.#tried.get(name) === signature) continue; + if (!signature || this.#tried.get(name) === signature) return []; this.#tried.set(name, signature); - try { - await this.manager.reconnectServer(name, { manual: true }); - connected ||= this.manager.getConnectionStatus(name) === "connected"; - } catch (err) { - logger.warn("Connecting an MCP server with its new sign-in failed", { server: name, err }); - } - } - if (connected) { - try { - await this.onConnected(); - } catch (err) { - logger.warn("Refreshing tools after an MCP sign-in failed", { err }); - } + return [this.#reconnect(name)]; + }); + await Promise.all(reconnects); + } + + /** Reconnects one server and publishes its tools once it connects. Never throws. */ + async #reconnect(name: string): Promise { + this.#connecting.add(name); + try { + await this.manager.reconnectServer(name, { manual: true }); + if (this.manager.getConnectionStatus(name) !== "connected") return; + this.#published = this.#published.then(() => + this.onConnected().catch(err => logger.warn("Refreshing tools after an MCP sign-in failed", { err })), + ); + await this.#published; + } catch (err) { + logger.warn("Connecting an MCP server with its new sign-in failed", { server: name, err }); + } finally { + this.#connecting.delete(name); } } } diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index e936bdf66c..a6843e3ce6 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -192,7 +192,7 @@ describe("a conversation picking up a sign-in made elsewhere", () => { await pickup.beforePrompt(10); expect(reconnects).toEqual([NAME]); expect(refreshed).toBe(0); - // A second prompt joins the running pickup instead of starting another. + // A second prompt starts no second attempt for a server still reconnecting. await pickup.beforePrompt(10); expect(reconnects).toEqual([NAME]); @@ -201,6 +201,34 @@ describe("a conversation picking up a sign-in made elsewhere", () => { expect(refreshed).toBe(1); }); + it("connects every newly signed-in server even while another one stalls", async () => { + const auth = storage(); + const OTHER = "other@market:api"; + const URL_B = "https://other.example.test/mcp"; + const stalled = Promise.withResolvers(); + const { manager, reconnects } = fakeManager( + { + [NAME]: { config: bound() }, + [OTHER]: { + config: bound({ type: "oauth", credentialId: mcpOAuthServerCredentialId(OTHER, URL_B) }, { + url: URL_B, + } as Partial), + }, + }, + name => (name === NAME ? stalled.promise : Promise.resolve()), + ); + let refreshed = 0; + const pickup = new McpSignInPickup(manager, auth, async () => void refreshed++); + pickup.seed(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("a")); + await auth.credentials.set(mcpOAuthServerCredentialId(OTHER, URL_B), token("b")); + + await pickup.beforePrompt(); + expect(reconnects.toSorted()).toEqual([NAME, OTHER].toSorted()); + expect(refreshed).toBe(1); + stalled.resolve(); + }); + it("never reconnects a server a browser sign-in does not serve", async () => { const auth = storage(); await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("t")); From 1ca806a32a22cf68aab6f9c77c6ec6c457ba842e Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 19:19:05 +0200 Subject: [PATCH 29/30] fix(mcp): mcp output written with Bun.write; later prompts join pickups under way; steering unaffected - mcp list and mcp login write each JSON line with Bun.write(Bun.stdout), in order, and exit only after the last one resolved; a failed write exits nonzero. - A later prompt joins a sign-in re-read and any reconnect still under way, within its own wait, without starting a second attempt. - RPC steer and follow-up keep their previous admission path; the pickup runs after them, before skill and builtin dispatch. --- .../coding-agent/src/modes/rpc/rpc-mode.ts | 12 ++-- .../coding-agent/src/scient/mcp-command.ts | 21 ++++--- .../src/scient/mcp-sign-in-pickup.ts | 59 +++++++++++-------- .../test/scient/mcp-host-command.test.ts | 6 +- 4 files changed, 56 insertions(+), 42 deletions(-) diff --git a/packages/coding-agent/src/modes/rpc/rpc-mode.ts b/packages/coding-agent/src/modes/rpc/rpc-mode.ts index 20a0fc24f2..7557c6a94e 100644 --- a/packages/coding-agent/src/modes/rpc/rpc-mode.ts +++ b/packages/coding-agent/src/modes/rpc/rpc-mode.ts @@ -1984,12 +1984,6 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions } if (!isCurrent()) return "cancelled"; if (!text.trim() && !images?.length) return "local"; - // scient(SA-CODEX-PLUGINS): begin - a server signed in from the host since the last prompt joins this one, skills included. - if (mcpSignInPickup) { - await mcpSignInPickup.beforePrompt(); - if (!isCurrent()) return "cancelled"; - } - // scient(SA-CODEX-PLUGINS): end if (command.type === "steer") { await session.steer(text, images); return "admitted"; @@ -1998,6 +1992,12 @@ export async function runRpcMode(session: AgentSession, options: RpcModeOptions await session.followUp(text, images); return "admitted"; } + // scient(SA-CODEX-PLUGINS): begin - a server signed in from the host since the last prompt joins this one, skills included. + if (mcpSignInPickup) { + await mcpSignInPickup.beforePrompt(); + if (!isCurrent()) return "cancelled"; + } + // scient(SA-CODEX-PLUGINS): end if (command.type === "prompt") { if (!ticket) return "cancelled"; const skillResult = await dispatchRpcSkillPrompt({ diff --git a/packages/coding-agent/src/scient/mcp-command.ts b/packages/coding-agent/src/scient/mcp-command.ts index 0651a70a35..8622bd32d8 100644 --- a/packages/coding-agent/src/scient/mcp-command.ts +++ b/packages/coding-agent/src/scient/mcp-command.ts @@ -50,18 +50,21 @@ export interface McpServerStatusLine { canStartSignIn?: true; } -function print(value: unknown): void { - process.stdout.write(`${JSON.stringify(value)}\n`); +let output: Promise = Promise.resolve(); + +/** Writes one JSON line after the ones before it; `Bun.write` resolves once every byte reached stdout. */ +function print(value: unknown): Promise { + output = output.then(() => Bun.write(Bun.stdout, `${JSON.stringify(value)}\n`)); + return output; } /** Waits until everything printed has reached stdout; a write that failed makes the exit nonzero. */ -function flushStdout(): Promise { - const { promise, resolve } = Promise.withResolvers(); - process.stdout.write("", error => { - if (error) process.exitCode = 1; - resolve(); - }); - return promise; +async function flushStdout(): Promise { + try { + await output; + } catch { + process.exitCode = 1; + } } /** A connection failure in words written here: the HTTP status at most, never the server's text. */ diff --git a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts index e7e41fdf86..45531595b5 100644 --- a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts +++ b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts @@ -46,7 +46,8 @@ const BROKER_REREAD_MS = 10_000; */ export class McpSignInPickup { readonly #tried = new Map(); - readonly #connecting = new Set(); + /** Reconnects under way, by server, each settling once its tools are published; later prompts join them. */ + readonly #connecting = new Map>(); #reread: Promise | undefined; #lastReread = Number.NEGATIVE_INFINITY; #published: Promise = Promise.resolve(); @@ -89,10 +90,12 @@ export class McpSignInPickup { /** Re-reads the sign-in store once for concurrent pickups; a broker at most every few seconds. */ #rereadSignIns(): Promise { + // A read under way is joined, so a later prompt waits for the sign-in it may bring. + if (this.#reread) return this.#reread; if (this.authStorage.credentials.heldByBroker && performance.now() - this.#lastReread < BROKER_REREAD_MS) { return Promise.resolve(true); } - this.#reread ??= (async () => { + this.#reread = (async () => { this.#lastReread = performance.now(); try { await this.authStorage.credentials.revalidate(); @@ -108,36 +111,42 @@ export class McpSignInPickup { } async #pickUp(): Promise { + const underWay = [...this.#connecting.values()]; const candidates = this.manager.getAllServerNames().flatMap(name => { if (this.#connecting.has(name) || this.manager.getConnectionStatus(name) !== "disconnected") return []; const config = this.manager.getServerConfig(name); return config && canSignIn(config) ? [{ name, config }] : []; }); - if (candidates.length === 0 || !(await this.#rereadSignIns())) return; - const reconnects = candidates.flatMap(({ name, config }) => { - if (this.#connecting.has(name)) return []; - const signature = this.#signature(config); - if (!signature || this.#tried.get(name) === signature) return []; - this.#tried.set(name, signature); - return [this.#reconnect(name)]; - }); - await Promise.all(reconnects); + const started = + candidates.length > 0 && (await this.#rereadSignIns()) + ? candidates.flatMap(({ name, config }) => { + if (this.#connecting.has(name)) return []; + const signature = this.#signature(config); + if (!signature || this.#tried.get(name) === signature) return []; + this.#tried.set(name, signature); + return [this.#reconnect(name)]; + }) + : []; + await Promise.all([...underWay, ...started]); } /** Reconnects one server and publishes its tools once it connects. Never throws. */ - async #reconnect(name: string): Promise { - this.#connecting.add(name); - try { - await this.manager.reconnectServer(name, { manual: true }); - if (this.manager.getConnectionStatus(name) !== "connected") return; - this.#published = this.#published.then(() => - this.onConnected().catch(err => logger.warn("Refreshing tools after an MCP sign-in failed", { err })), - ); - await this.#published; - } catch (err) { - logger.warn("Connecting an MCP server with its new sign-in failed", { server: name, err }); - } finally { - this.#connecting.delete(name); - } + #reconnect(name: string): Promise { + const attempt = (async () => { + try { + await this.manager.reconnectServer(name, { manual: true }); + if (this.manager.getConnectionStatus(name) !== "connected") return; + this.#published = this.#published.then(() => + this.onConnected().catch(err => logger.warn("Refreshing tools after an MCP sign-in failed", { err })), + ); + await this.#published; + } catch (err) { + logger.warn("Connecting an MCP server with its new sign-in failed", { server: name, err }); + } finally { + this.#connecting.delete(name); + } + })(); + this.#connecting.set(name, attempt); + return attempt; } } diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index a6843e3ce6..e175ebbcaa 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -196,8 +196,10 @@ describe("a conversation picking up a sign-in made elsewhere", () => { await pickup.beforePrompt(10); expect(reconnects).toEqual([NAME]); - gate.resolve(); - await pickup.beforePrompt(); + // A later prompt joins the reconnect under way and sees its tools if it lands within its own wait. + setTimeout(() => gate.resolve(), 20); + await pickup.beforePrompt(1_000); + expect(reconnects).toEqual([NAME]); expect(refreshed).toBe(1); }); From bc97ef14663c2f84e62a1c684ddfb7546a252cd4 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Sun, 22 Nov 2026 19:23:24 +0200 Subject: [PATCH 30/30] fix(mcp): a prompt sharing a sign-in re-read waits for the reconnect it starts - After the shared re-read, a pickup joins a reconnect another prompt started from that read instead of skipping it. --- .../src/scient/mcp-sign-in-pickup.ts | 4 +++- .../test/scient/mcp-host-command.test.ts | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts index 45531595b5..00fe6b244f 100644 --- a/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts +++ b/packages/coding-agent/src/scient/mcp-sign-in-pickup.ts @@ -120,7 +120,9 @@ export class McpSignInPickup { const started = candidates.length > 0 && (await this.#rereadSignIns()) ? candidates.flatMap(({ name, config }) => { - if (this.#connecting.has(name)) return []; + // Another prompt sharing this read may have started it: wait for that one. + const existing = this.#connecting.get(name); + if (existing) return [existing]; const signature = this.#signature(config); if (!signature || this.#tried.get(name) === signature) return []; this.#tried.set(name, signature); diff --git a/packages/coding-agent/test/scient/mcp-host-command.test.ts b/packages/coding-agent/test/scient/mcp-host-command.test.ts index e175ebbcaa..03548e343a 100644 --- a/packages/coding-agent/test/scient/mcp-host-command.test.ts +++ b/packages/coding-agent/test/scient/mcp-host-command.test.ts @@ -203,6 +203,24 @@ describe("a conversation picking up a sign-in made elsewhere", () => { expect(refreshed).toBe(1); }); + it("lets a prompt that arrives during the sign-in re-read wait for the reconnect that read starts", async () => { + const auth = storage(); + const gate = Promise.withResolvers(); + const { manager, reconnects } = fakeManager({ [NAME]: { config: bound() } }, () => gate.promise); + let refreshed = 0; + const pickup = new McpSignInPickup(manager, auth, async () => void refreshed++); + pickup.seed(); + await auth.credentials.set(mcpOAuthServerCredentialId(NAME, URL_A), token("new")); + + const first = pickup.beforePrompt(1_000); + const second = pickup.beforePrompt(1_000); + setTimeout(() => gate.resolve(), 20); + await second; + expect(refreshed).toBe(1); + await first; + expect(reconnects).toEqual([NAME]); + }); + it("connects every newly signed-in server even while another one stalls", async () => { const auth = storage(); const OTHER = "other@market:api";