fix(omo-codex): reuse shared lsp mcp

This commit is contained in:
YeonGyu-Kim
2026-05-28 15:30:24 +09:00
parent 4f75a56ce5
commit 615ed40ba3
79 changed files with 378 additions and 5510 deletions
@@ -0,0 +1,54 @@
import assert from "node:assert/strict";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import test from "node:test";
import { linkCachedPluginBins } from "./install/cache.mjs";
import { makeTempDir, writeJson } from "./install-test-fixtures.mjs";
test("#given Windows platform #when linking cached plugin bins #then writes command shims", async () => {
const root = await makeTempDir();
const pluginRoot = join(root, "plugin");
const binDir = join(root, "bin");
await mkdir(pluginRoot, { recursive: true });
await writeJson(join(pluginRoot, "package.json"), {
name: "@example/alpha",
bin: {
alpha: "./dist/cli.js",
},
});
await mkdir(join(pluginRoot, "dist"), { recursive: true });
await writeFile(join(pluginRoot, "dist", "cli.js"), "#!/usr/bin/env node\n");
const linked = await linkCachedPluginBins({ binDir, pluginRoot, platform: "win32" });
assert.deepEqual(linked, [{ name: "alpha", path: join(binDir, "alpha.cmd"), target: join(pluginRoot, "dist", "cli.js") }]);
const shim = await readFile(join(binDir, "alpha.cmd"), "utf8");
assert.match(shim, /@echo off/);
assert.match(shim, new RegExp(`node "${join(pluginRoot, "dist", "cli.js").replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}" %\\*`));
});
test("#given existing custom Windows command shim #when linking bins #then rejects without overwriting", async () => {
const root = await makeTempDir();
const pluginRoot = join(root, "plugin");
const binDir = join(root, "bin");
await mkdir(pluginRoot, { recursive: true });
await mkdir(binDir, { recursive: true });
await writeJson(join(pluginRoot, "package.json"), {
name: "@example/alpha",
bin: {
alpha: "./dist/cli.js",
},
});
await mkdir(join(pluginRoot, "dist"), { recursive: true });
await writeFile(join(pluginRoot, "dist", "cli.js"), "#!/usr/bin/env node\n");
await writeFile(join(binDir, "alpha.cmd"), "@echo off\r\necho custom\r\n");
await assert.rejects(
linkCachedPluginBins({ binDir, pluginRoot, platform: "win32" }),
/already exists and is not a generated command shim/,
);
assert.match(await readFile(join(binDir, "alpha.cmd"), "utf8"), /echo custom/);
});
+21 -4
View File
@@ -3,7 +3,12 @@ import { homedir } from "node:os";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { installCachedPlugin, linkCachedPluginBins, pruneMarketplaceCache } from "./install/cache.mjs";
import {
installCachedPlugin,
linkCachedPluginBins,
pruneMarketplaceCache,
pruneMarketplacePluginCaches,
} from "./install/cache.mjs";
import { updateCodexConfig } from "./install/config.mjs";
import { trustedHookStatesForPlugin } from "./install/hook-trust.mjs";
import { defaultRunCommand } from "./install/process.mjs";
@@ -14,6 +19,8 @@ import {
validatePathSegment,
} from "./install/marketplace.mjs";
const SISYPHUS_LEGACY_CACHE_MARKETPLACES = ["lazycodex", "code-yeongyu-codex-plugins"];
export async function installMarketplaceLocally(options = {}) {
const repoRoot = resolve(options.repoRoot ?? process.cwd());
const codexHome = resolve(options.codexHome ?? process.env.CODEX_HOME ?? join(homedir(), ".codex"));
@@ -21,11 +28,14 @@ export async function installMarketplaceLocally(options = {}) {
const platform = options.platform ?? process.platform;
const runCommand = options.runCommand ?? defaultRunCommand;
const log = options.log ?? console.log;
const marketplace = await readMarketplace(repoRoot);
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
const marketplace = await readMarketplace(repoRoot, {
marketplacePath: join(codexPackageRoot, "marketplace.json"),
});
const installed = [];
for (const entry of marketplace.plugins) {
const sourcePath = resolvePluginSource(repoRoot, entry);
const sourcePath = resolvePluginSource(codexPackageRoot, entry, { pathOverride: "./plugin" });
const manifest = await readPluginManifest(sourcePath);
if (manifest.name !== entry.name) {
throw new Error(
@@ -64,9 +74,12 @@ export async function installMarketplaceLocally(options = {}) {
)
).flat();
await pruneMarketplaceCache({ codexHome, marketplaceName: marketplace.name, keepPluginNames: pluginNames });
for (const legacyMarketplaceName of legacyCacheMarketplaces(marketplace.name)) {
await pruneMarketplacePluginCaches({ codexHome, marketplaceName: legacyMarketplaceName, pluginNames });
}
await updateCodexConfig({
configPath: join(codexHome, "config.toml"),
repoRoot,
repoRoot: codexPackageRoot,
marketplaceName: marketplace.name,
pluginNames,
trustedHookStates,
@@ -79,6 +92,10 @@ export async function installMarketplaceLocally(options = {}) {
return { marketplaceName: marketplace.name, installed };
}
function legacyCacheMarketplaces(marketplaceName) {
return marketplaceName === "sisyphuslabs" ? SISYPHUS_LEGACY_CACHE_MARKETPLACES : [];
}
async function main() {
const repoRoot = process.argv[2] ? resolve(process.argv[2]) : process.cwd();
const result = await installMarketplaceLocally({ repoRoot });
@@ -4,35 +4,62 @@ import { join } from "node:path";
import test from "node:test";
import { installMarketplaceLocally } from "./install-local.mjs";
import { linkCachedPluginBins } from "./install/cache.mjs";
import { makeTempDir, writeJson, writePlugin } from "./install-test-fixtures.mjs";
import { makeTempDir, writeJson, writePluginAt } from "./install-test-fixtures.mjs";
test("#given local marketplace #when installing #then copies versioned plugins and enables config", async () => {
const repoRoot = await makeTempDir();
const codexHome = await makeTempDir();
const binDir = await makeTempDir();
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
const pluginRoot = join(codexPackageRoot, "plugin");
await mkdir(join(repoRoot, ".agents", "plugins"), { recursive: true });
await writeJson(join(repoRoot, ".agents", "plugins", "marketplace.json"), {
await writeJson(join(codexPackageRoot, "marketplace.json"), {
name: "debug-marketplace",
plugins: [
{
name: "alpha",
source: "./plugins/alpha",
},
{
name: "beta",
source: {
source: "local",
path: "./plugins/beta",
},
},
],
});
await writePlugin(repoRoot, "alpha", "1.2.3");
await writePlugin(repoRoot, "beta", "0.4.0");
await mkdir(join(repoRoot, "plugins", "alpha", "node_modules"), { recursive: true });
await writeFile(join(repoRoot, "plugins", "alpha", "node_modules", "skip.txt"), "skip");
await writePluginAt(pluginRoot, "alpha", "1.2.3");
await mkdir(join(codexPackageRoot, "shared-lsp", "dist"), { recursive: true });
await writeJson(join(codexPackageRoot, "shared-lsp", "package.json"), {
name: "@example/shared-lsp",
version: "0.0.0",
type: "module",
bin: { "shared-lsp": "./dist/cli.js" },
});
await writeFile(join(codexPackageRoot, "shared-lsp", "dist", "cli.js"), "#!/usr/bin/env node\n");
await writeJson(join(pluginRoot, "package.json"), {
name: "@example/alpha",
version: "1.2.3",
bin: {
alpha: "./dist/cli.js",
},
scripts: {
build: "node -e \"require('fs').writeFileSync('dist/cli.js', 'console.log(1)')\"",
},
dependencies: {
"@example/shared-lsp": "file:../shared-lsp",
},
});
await writeJson(join(pluginRoot, ".mcp.json"), {
mcpServers: {
alpha: {
command: "node",
args: ["./dist/cli.js", "mcp"],
cwd: ".",
},
shared: {
command: "node",
args: ["../shared-lsp/dist/cli.js", "mcp"],
cwd: ".",
},
},
});
await mkdir(join(pluginRoot, "node_modules"), { recursive: true });
await writeFile(join(pluginRoot, "node_modules", "skip.txt"), "skip");
await mkdir(join(codexHome, "plugins", "cache", "debug-marketplace", "stale", "0.1.0"), { recursive: true });
await writeFile(
join(codexHome, "config.toml"),
@@ -60,19 +87,23 @@ test("#given local marketplace #when installing #then copies versioned plugins a
assert.deepEqual(
result.installed.map((plugin) => `${plugin.name}@${plugin.version}`),
["alpha@1.2.3", "beta@0.4.0"],
["alpha@1.2.3"],
);
const alphaCacheRoot = join(codexHome, "plugins", "cache", "debug-marketplace", "alpha", "1.2.3");
assert.equal((await stat(join(alphaCacheRoot, ".mcp.json"))).isFile(), true);
assert.equal(await readlink(join(binDir, "alpha")), join(alphaCacheRoot, "dist", "cli.js"));
const alphaMcp = JSON.parse(await readFile(join(alphaCacheRoot, ".mcp.json"), "utf8"));
assert.deepEqual(alphaMcp.mcpServers.alpha.args, [join(alphaCacheRoot, "dist", "cli.js"), "mcp"]);
assert.deepEqual(alphaMcp.mcpServers.shared.args, [join(codexPackageRoot, "shared-lsp", "dist", "cli.js"), "mcp"]);
assert.equal(
Object.hasOwn(alphaMcp.mcpServers.alpha, "cwd"),
false,
"`cwd: \".\"` must be stripped so the spawned MCP server inherits the caller's workspace cwd",
);
assert.equal(Object.hasOwn(alphaMcp.mcpServers.shared, "cwd"), false);
assert.equal(alphaMcp.mcpServers.alpha.command, "node");
const alphaPackageJson = JSON.parse(await readFile(join(alphaCacheRoot, "package.json"), "utf8"));
assert.equal(alphaPackageJson.dependencies["@example/shared-lsp"], `file:${join(codexPackageRoot, "shared-lsp")}`);
await assert.rejects(
stat(join(codexHome, "plugins", "cache", "debug-marketplace", "alpha", "1.2.3", "node_modules")),
/code: 'ENOENT'|ENOENT/,
@@ -84,12 +115,9 @@ test("#given local marketplace #when installing #then copies versioned plugins a
assert.deepEqual(
commands.map(([command, args, cwd]) => [command, args.join(" "), cwd]),
[
["npm", "install", join(repoRoot, "plugins", "alpha")],
["npm", "run build", join(repoRoot, "plugins", "alpha")],
["npm", "install", pluginRoot],
["npm", "run build", pluginRoot],
["npm", "install --omit=dev", join(codexHome, "plugins", "cache", "debug-marketplace", "alpha", "1.2.3")],
["npm", "install", join(repoRoot, "plugins", "beta")],
["npm", "run build", join(repoRoot, "plugins", "beta")],
["npm", "install --omit=dev", join(codexHome, "plugins", "cache", "debug-marketplace", "beta", "0.4.0")],
],
);
@@ -98,20 +126,49 @@ test("#given local marketplace #when installing #then copies versioned plugins a
assert.match(config, /\[marketplaces\.debug-marketplace\]/);
assert.match(config, /source_type = "local"/);
assert.match(config, /\[plugins\."alpha@debug-marketplace"\]\nenabled = true/);
assert.match(config, /\[plugins\."beta@debug-marketplace"\]\nenabled = true/);
assert.doesNotMatch(config, /stale@debug-marketplace/);
});
test("#given sisyphuslabs marketplace #when installing #then registers lazycodex git source", async () => {
const repoRoot = await makeTempDir();
const codexHome = await makeTempDir();
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
await mkdir(join(repoRoot, ".agents", "plugins"), { recursive: true });
await writeJson(join(repoRoot, ".agents", "plugins", "marketplace.json"), {
await writeJson(join(codexPackageRoot, "marketplace.json"), {
name: "sisyphuslabs",
plugins: [{ name: "omo", source: "./plugins/omo" }],
});
await writePlugin(repoRoot, "omo", "0.1.0");
await writePluginAt(join(codexPackageRoot, "plugin"), "omo", "0.1.0");
await mkdir(join(codexHome, "plugins", "cache", "code-yeongyu-codex-plugins", "omo", "0.1.0"), {
recursive: true,
});
await writeJson(join(codexHome, "plugins", "cache", "code-yeongyu-codex-plugins", "omo", "0.1.0", ".mcp.json"), {
mcpServers: {
lsp: {
command: "node",
args: ["old/components/lsp/packages/lsp-tools-mcp/dist/cli.js", "mcp"],
},
},
});
await writeFile(
join(codexHome, "config.toml"),
[
"[marketplaces.code-yeongyu-codex-plugins]",
'last_updated = "2026-05-01T00:00:00Z"',
'source_type = "git"',
'source = "https://github.com/code-yeongyu/codex-plugins.git"',
"",
'[plugins."omo@code-yeongyu-codex-plugins"]',
"enabled = true",
"",
'[plugins."omo@code-yeongyu-codex-plugins".mcp_servers.lsp]',
'enabled = true',
"",
'[hooks.state."omo@code-yeongyu-codex-plugins:hooks/hooks.json:post_tool_use:0:0"]',
'trusted_hash = "sha256:old"',
"",
].join("\n"),
);
await installMarketplaceLocally({
repoRoot,
@@ -129,19 +186,24 @@ test("#given sisyphuslabs marketplace #when installing #then registers lazycodex
assert.doesNotMatch(config, /\[marketplaces\.lazycodex\]/);
assert.doesNotMatch(config, /code-yeongyu-codex-plugins/);
assert.doesNotMatch(config, /source_type = "local"/);
await assert.rejects(
stat(join(codexHome, "plugins", "cache", "code-yeongyu-codex-plugins", "omo")),
/code: 'ENOENT'|ENOENT/,
);
});
test("#given plugin hooks #when installing #then records trusted hook hashes", async () => {
const repoRoot = await makeTempDir();
const codexHome = await makeTempDir();
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
await mkdir(join(repoRoot, ".agents", "plugins"), { recursive: true });
await writeJson(join(repoRoot, ".agents", "plugins", "marketplace.json"), {
await writeJson(join(codexPackageRoot, "marketplace.json"), {
name: "debug-marketplace",
plugins: [{ name: "alpha", source: "./plugins/alpha" }],
});
await writePlugin(repoRoot, "alpha", "1.2.3");
await writeJson(join(repoRoot, "plugins", "alpha", "hooks", "hooks.json"), {
const pluginRoot = join(codexPackageRoot, "plugin");
await writePluginAt(pluginRoot, "alpha", "1.2.3");
await writeJson(join(pluginRoot, "hooks", "hooks.json"), {
hooks: {
UserPromptSubmit: [
{
@@ -173,9 +235,9 @@ test("#given plugin hooks #when installing #then records trusted hook hashes", a
test("#given bad plugin source path #when installing #then rejects traversal", async () => {
const repoRoot = await makeTempDir();
const codexHome = await makeTempDir();
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
await mkdir(join(repoRoot, ".agents", "plugins"), { recursive: true });
await writeJson(join(repoRoot, ".agents", "plugins", "marketplace.json"), {
await writeJson(join(codexPackageRoot, "marketplace.json"), {
name: "debug-marketplace",
plugins: [
{
@@ -190,50 +252,3 @@ test("#given bad plugin source path #when installing #then rejects traversal", a
/local plugin source path must start with \.\//,
);
});
test("#given Windows platform #when linking cached plugin bins #then writes command shims", async () => {
const root = await makeTempDir();
const pluginRoot = join(root, "plugin");
const binDir = join(root, "bin");
await mkdir(pluginRoot, { recursive: true });
await writeJson(join(pluginRoot, "package.json"), {
name: "@example/alpha",
bin: {
alpha: "./dist/cli.js",
},
});
await mkdir(join(pluginRoot, "dist"), { recursive: true });
await writeFile(join(pluginRoot, "dist", "cli.js"), "#!/usr/bin/env node\n");
const linked = await linkCachedPluginBins({ binDir, pluginRoot, platform: "win32" });
assert.deepEqual(linked, [{ name: "alpha", path: join(binDir, "alpha.cmd"), target: join(pluginRoot, "dist", "cli.js") }]);
const shim = await readFile(join(binDir, "alpha.cmd"), "utf8");
assert.match(shim, /@echo off/);
assert.match(shim, new RegExp(`node "${join(pluginRoot, "dist", "cli.js").replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}" %\\*`));
});
test("#given existing custom Windows command shim #when linking bins #then rejects without overwriting", async () => {
const root = await makeTempDir();
const pluginRoot = join(root, "plugin");
const binDir = join(root, "bin");
await mkdir(pluginRoot, { recursive: true });
await mkdir(binDir, { recursive: true });
await writeJson(join(pluginRoot, "package.json"), {
name: "@example/alpha",
bin: {
alpha: "./dist/cli.js",
},
});
await mkdir(join(pluginRoot, "dist"), { recursive: true });
await writeFile(join(pluginRoot, "dist", "cli.js"), "#!/usr/bin/env node\n");
await writeFile(join(binDir, "alpha.cmd"), "@echo off\r\necho custom\r\n");
await assert.rejects(
linkCachedPluginBins({ binDir, pluginRoot, platform: "win32" }),
/already exists and is not a generated command shim/,
);
assert.match(await readFile(join(binDir, "alpha.cmd"), "utf8"), /echo custom/);
});
@@ -13,6 +13,10 @@ export async function writeJson(path, value) {
export async function writePlugin(root, name, version) {
const pluginRoot = join(root, "plugins", name);
await writePluginAt(pluginRoot, name, version);
}
export async function writePluginAt(pluginRoot, name, version) {
await mkdir(join(pluginRoot, ".codex-plugin"), { recursive: true });
await mkdir(join(pluginRoot, "dist"), { recursive: true });
await mkdir(join(pluginRoot, "hooks"), { recursive: true });
@@ -28,6 +28,17 @@ export async function pruneMarketplaceCache({ codexHome, marketplaceName, keepPl
}
}
export async function pruneMarketplacePluginCaches({ codexHome, marketplaceName, pluginNames }) {
const cacheRoot = join(codexHome, "plugins", "cache", marketplaceName);
if (!(await exists(cacheRoot))) return;
for (const pluginName of pluginNames) {
await rm(join(cacheRoot, pluginName), { recursive: true, force: true });
}
if ((await readdir(cacheRoot)).length === 0) {
await rm(cacheRoot, { recursive: true, force: true });
}
}
export async function linkCachedPluginBins({ binDir, pluginRoot, platform = process.platform }) {
const binLinks = await discoverPackageBins(pluginRoot);
await mkdir(binDir, { recursive: true });
+35 -3
View File
@@ -8,6 +8,7 @@ const LAZYCODEX_MARKETPLACE_SOURCE = {
source: "https://github.com/code-yeongyu/lazycodex.git",
ref: "main",
};
const SISYPHUS_LEGACY_MARKETPLACES = ["lazycodex", "code-yeongyu-codex-plugins"];
export async function updateCodexConfig({
configPath,
@@ -21,6 +22,11 @@ export async function updateCodexConfig({
let config = "";
if (await exists(configPath)) config = await readFile(configPath, "utf8");
for (const legacyMarketplaceName of legacyMarketplaceNames(marketplaceName)) {
config = removeMarketplaceBlock(config, legacyMarketplaceName);
config = removeStaleMarketplacePluginBlocks(config, legacyMarketplaceName, new Set());
config = removeStaleMarketplaceHookStateBlocks(config, legacyMarketplaceName, new Set());
}
config = removeStaleMarketplacePluginBlocks(config, marketplaceName, new Set(pluginNames));
config = removeStaleMarketplaceHookStateBlocks(config, marketplaceName, new Set(pluginNames));
config = ensureFeatureEnabled(config, "plugins");
@@ -36,6 +42,14 @@ export async function updateCodexConfig({
await writeFile(configPath, config.trimEnd() + "\n");
}
function legacyMarketplaceNames(marketplaceName) {
return marketplaceName === "sisyphuslabs" ? SISYPHUS_LEGACY_MARKETPLACES : [];
}
function removeMarketplaceBlock(config, marketplaceName) {
return removeTomlSections(config, (header) => header === `marketplaces.${marketplaceName}`);
}
function defaultMarketplaceSource(marketplaceName, repoRoot) {
if (marketplaceName === "sisyphuslabs") return LAZYCODEX_MARKETPLACE_SOURCE;
return {
@@ -46,7 +60,7 @@ function defaultMarketplaceSource(marketplaceName, repoRoot) {
function removeStaleMarketplacePluginBlocks(config, marketplaceName, keepPluginNames) {
return removeTomlSections(config, (header) => {
const pluginKey = parseQuotedPluginHeader(header);
const pluginKey = parsePluginHeaderKey(header);
if (pluginKey === null) return false;
const suffix = `@${marketplaceName}`;
if (!pluginKey.endsWith(suffix)) return false;
@@ -170,10 +184,28 @@ function parseTomlHeader(line) {
return trimmed.slice(1, -1);
}
function parseQuotedPluginHeader(header) {
function parsePluginHeaderKey(header) {
const prefix = "plugins.";
if (!header.startsWith(prefix)) return null;
return parseJsonString(header.slice(prefix.length));
return parseLeadingJsonString(header.slice(prefix.length));
}
function parseLeadingJsonString(value) {
if (!value.startsWith('"')) return parseJsonString(value);
let escaped = false;
for (let index = 1; index < value.length; index += 1) {
const char = value[index];
if (escaped) {
escaped = false;
continue;
}
if (char === "\\") {
escaped = true;
continue;
}
if (char === '"') return parseJsonString(value.slice(0, index + 1));
}
return null;
}
function parseJsonString(value) {
@@ -3,10 +3,10 @@ import { join } from "node:path";
import { isRecord } from "./utils.mjs";
const MARKETPLACE_PATH = ".agents/plugins/marketplace.json";
const DEFAULT_MARKETPLACE_PATH = "packages/omo-codex/marketplace.json";
export async function readMarketplace(repoRoot) {
const marketplacePath = join(repoRoot, MARKETPLACE_PATH);
export async function readMarketplace(repoRoot, options = {}) {
const marketplacePath = options.marketplacePath ?? join(repoRoot, DEFAULT_MARKETPLACE_PATH);
const raw = await readFile(marketplacePath, "utf8");
const parsed = JSON.parse(raw);
if (!isRecord(parsed)) throw new Error("marketplace.json must be an object");
@@ -22,10 +22,10 @@ export async function readMarketplace(repoRoot) {
};
}
export function resolvePluginSource(repoRoot, plugin) {
const sourcePath = localSourcePath(plugin.source);
export function resolvePluginSource(marketplaceRoot, plugin, options = {}) {
const sourcePath = localSourcePath(options.pathOverride ?? plugin.source);
const relativePath = sourcePath.slice(2);
return join(repoRoot, ...relativePath.split(/[\\/]/));
return join(marketplaceRoot, ...relativePath.split(/[\\/]/));
}
export async function readPluginManifest(pluginRoot) {
@@ -60,10 +60,21 @@ function normalizeMarketplacePlugin(plugin, index) {
throw new Error(`marketplace plugin ${index} name must be a non-empty string`);
}
validatePathSegment(plugin.name, "plugin name");
return {
name: plugin.name,
source: plugin.source,
};
if (plugin.source === undefined || typeof plugin.source === "string") {
if (typeof plugin.source === "string") validateLocalSourcePath(plugin.source);
return {
name: plugin.name,
source: plugin.source,
};
}
if (isRecord(plugin.source) && plugin.source.source === "local" && typeof plugin.source.path === "string") {
validateLocalSourcePath(plugin.source.path);
return {
name: plugin.name,
source: { source: "local", path: plugin.source.path },
};
}
throw new Error("local plugin source must be a string path or { source: \"local\", path } object");
}
function localSourcePath(source) {