fix(codex): support packaged lazycodex installs

This commit is contained in:
YeonGyu-Kim
2026-05-31 14:39:03 +09:00
parent 247bba39e5
commit d0d1735e6b
15 changed files with 335 additions and 19 deletions
+6
View File
@@ -5,6 +5,12 @@
"args": ["../../ast-grep-mcp/dist/cli.js", "mcp"],
"cwd": "."
},
"grep_app": {
"url": "https://mcp.grep.app"
},
"context7": {
"url": "https://mcp.context7.com/mcp"
},
"git_bash": {
"command": "node",
"args": ["../../git-bash-mcp/dist/cli.js", "mcp"],
@@ -55,7 +55,7 @@ If the user names a version ("React 18", "Next.js 14", "v2.x"):
## Step 4 - targeted investigation
- `webfetch(<specific-doc-page-from-sitemap>)`.
- If a docs-indexer / library-index tool is available, query it for the specific topic. Otherwise rely on the sitemap-driven webfetch pages.
- If `context7` is available, query it for the specific topic. Otherwise rely on the sitemap-driven webfetch pages.
## Skip Phase 0.5 when
- TYPE B (implementation) - you're cloning the repo anyway.
@@ -70,7 +70,7 @@ If the user names a version ("React 18", "Next.js 14", "v2.x"):
Run Phase 0.5 first, then in parallel:
- `web_search` for current-year usage examples + best practices.
- `webfetch` for the targeted doc pages identified by the sitemap.
- `gh search code "<usage pattern>" --language <lang>` for real-world code samples.
- `grep_app` for broad GitHub code search; fall back to `gh search code "<usage pattern>" --language <lang>`.
## TYPE B - IMPLEMENTATION REFERENCE
Execute in sequence:
@@ -81,9 +81,9 @@ Execute in sequence:
Parallel acceleration (4+ calls in one batch when independent):
- Shallow clone.
- `gh search code "<function-name>" --repo <owner>/<repo>`.
- `grep_app` broad code search or `gh search code "<function-name>" --repo <owner>/<repo>`.
- `gh api repos/<owner>/<repo>/commits/HEAD --jq '.sha'`.
- Sitemap-targeted `webfetch` of the relevant docs page for the same API surface.
- `context7` or sitemap-targeted `webfetch` of the relevant docs page for the same API surface.
## TYPE C - CONTEXT & HISTORY
Execute in parallel (4+ calls):
@@ -100,7 +100,7 @@ For a specific issue / PR:
## TYPE D - COMPREHENSIVE
Run Phase 0.5 first, then execute 6+ parallel calls:
- 2 docs calls: `webfetch` targeted doc pages + (if available) a docs-indexer query.
- 2 code-search calls: `gh search code` with varied queries (different angles).
- 2 code-search calls: `grep_app` or `gh search code` with varied queries (different angles).
- 1 source clone for deep inspection.
- 1 issues/PRs query for context.
@@ -147,8 +147,9 @@ Never link to a branch name (`/blob/main/...`) - always pin to a SHA so the line
- Sitemap -> `webfetch(<base>/sitemap.xml)` (fallbacks: `/sitemap-0.xml`, `/sitemap_index.xml`).
- Read a specific page -> `webfetch(<page-url>)`.
- Latest info -> `web_search("<query> <CURRENT_YEAR>")`.
- Code search (fast, broad) -> `gh search code "<query>" --language <lang>` (org-wide or repo-scoped).
- Code search (deep, repo-scoped) -> after cloning, `rg` / `ast_grep_search` over the clone.
- Docs index -> `context7` when available; use sitemap-driven pages when it is not.
- Code search (fast, broad) -> `grep_app` for web-scale GitHub search; `gh search code "<query>" --language <lang>` when you need GitHub CLI filters.
- Code search (deep, repo-scoped) -> after cloning, `rg` / `ast_grep` over the clone.
- Clone -> `gh repo clone <o>/<r> "${TMPDIR:-/tmp}/<name>" -- --depth 1`.
- Issues / PRs -> `gh search issues|prs`, `gh issue|pr view <n> --comments`.
- Release info -> `gh api repos/<o>/<r>/releases/latest`.
@@ -34,6 +34,31 @@ describe("codex ultrawork package metadata", () => {
expect(hookCommands).toContain(`node "${pluginRoot}/dist/cli.js" hook user-prompt-submit`);
expect(hookCommands).not.toContainEqual(expect.stringMatching(/\bpython3?\b|ultrawork-detector\.py/));
});
it("#given explorer guidance #when inspected #then names the packaged code-search MCP surface", () => {
// given
const explorer = readFileSync("agents/explorer.toml", "utf8");
// when
const guidance = explorer.toLowerCase();
// then
expect(guidance).toContain("ast_grep");
expect(guidance).toContain("structural");
});
it("#given librarian guidance #when inspected #then names the packaged research MCP surfaces", () => {
// given
const librarian = readFileSync("agents/librarian.toml", "utf8");
// when
const guidance = librarian.toLowerCase();
// then
expect(guidance).toContain("grep_app");
expect(guidance).toContain("context7");
expect(guidance).toContain("ast_grep");
});
});
function readJson(path: string): unknown {
@@ -0,0 +1,21 @@
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
const root = dirname(dirname(fileURLToPath(import.meta.url)));
test("#given aggregate MCP config #when inspected #then registers research and structural-search MCPs", async () => {
// given
const mcp = JSON.parse(await readFile(join(root, ".mcp.json"), "utf8"));
// when
const serverNames = Object.keys(mcp.mcpServers).sort();
// then
assert.deepEqual(serverNames, ["ast_grep", "context7", "git_bash", "grep_app", "lsp"]);
assert.equal(mcp.mcpServers.grep_app.url, "https://mcp.grep.app");
assert.equal(mcp.mcpServers.context7.url, "https://mcp.context7.com/mcp");
assert.deepEqual(mcp.mcpServers.ast_grep.args, ["../../ast-grep-mcp/dist/cli.js", "mcp"]);
});
+12 -1
View File
@@ -1,5 +1,6 @@
#!/usr/bin/env node
import { mkdir, writeFile } from "node:fs/promises";
import { existsSync } from "node:fs";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
@@ -46,6 +47,7 @@ export async function installMarketplaceLocally(options = {}) {
const platform = options.platform ?? process.platform;
const runCommand = options.runCommand ?? defaultRunCommand;
const log = options.log ?? console.log;
const buildSource = await shouldBuildSourcePackages(repoRoot);
const gitBashResolution = await prepareGitBashForInstall({
platform,
env,
@@ -79,6 +81,7 @@ export async function installMarketplaceLocally(options = {}) {
log(`Building ${entry.name}@${version}`);
const plugin = await installCachedPlugin({
buildSource,
codexHome,
marketplaceName: marketplace.name,
name: entry.name,
@@ -191,6 +194,14 @@ function legacyCacheMarketplaces(marketplaceName) {
return marketplaceName === "sisyphuslabs" ? SISYPHUS_LEGACY_CACHE_MARKETPLACES : [];
}
async function shouldBuildSourcePackages(repoRoot) {
if (existsSync(join(repoRoot, "src", "index.ts"))) return true;
const packageJsonPath = join(repoRoot, "package.json");
if (!existsSync(packageJsonPath)) return true;
const packageJson = JSON.parse(await readFile(packageJsonPath, "utf8"));
return !["@code-yeongyu/lazycodex", "lazycodex", "oh-my-opencode", "oh-my-openagent"].includes(packageJson?.name);
}
async function main() {
const repoRoot = process.argv[2] ? resolve(process.argv[2]) : process.cwd();
const result = await installMarketplaceLocally({ repoRoot });
@@ -188,3 +188,46 @@ test("#given structurally valid external MCP package without mcp suffix #when in
assert.deepEqual(cachedMcp.mcpServers.language_tools.args, [copiedCli, "mcp", join(sourceRoot, "..", "local-config.json")]);
assert.equal((await stat(copiedCli)).isFile(), true);
});
test("#given packaged external MCP runtime has only dist files #when installing cached plugin #then runtime is copied into the plugin cache", async () => {
// given
const repoRoot = await makeTempDir();
const codexHome = await makeTempDir();
const sourceRoot = join(repoRoot, "packages", "omo-codex", "plugin");
const lspPackageRoot = join(repoRoot, "packages", "lsp-tools-mcp");
await writeJson(join(sourceRoot, "package.json"), {
name: "@example/omo",
version: "0.1.0",
});
await writeJson(join(sourceRoot, ".mcp.json"), {
mcpServers: {
lsp: {
command: "node",
args: ["../../lsp-tools-mcp/dist/cli.js", "mcp"],
cwd: ".",
},
},
});
await writeJson(join(lspPackageRoot, "dist", "cli.js"), { executable: true });
await writeJson(join(lspPackageRoot, "dist", "lsp", "manager.js"), { copied: true });
// when
const result = await installCachedPlugin({
codexHome,
marketplaceName: "sisyphuslabs",
name: "omo",
runCommand: async () => {},
sourcePath: sourceRoot,
version: "0.1.0",
});
// then
const cachedMcp = JSON.parse(await readFile(join(result.path, ".mcp.json"), "utf8"));
const copiedCli = join(result.path, "mcp", "lsp", "dist", "cli.js");
assert.deepEqual(cachedMcp.mcpServers.lsp.args, [copiedCli, "mcp"]);
assert.equal(Object.hasOwn(cachedMcp.mcpServers.lsp, "cwd"), false);
assert.equal((await stat(copiedCli)).isFile(), true);
assert.equal((await stat(join(result.path, "mcp", "lsp", "dist", "lsp", "manager.js"))).isFile(), true);
assert.notEqual(cachedMcp.mcpServers.lsp.args[0], join(lspPackageRoot, "dist", "cli.js"));
});
@@ -0,0 +1,67 @@
import assert from "node:assert/strict";
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
import { join } from "node:path";
import test from "node:test";
import { installMarketplaceLocally } from "./install-local.mjs";
import { makeTempDir, writeJson, writePluginAt } from "./install-test-fixtures.mjs";
test("#given packaged lazycodex adapter #when installing locally #then uses bundled artifacts without source builds", async () => {
// given
const repoRoot = await makeTempDir();
const codexHome = await makeTempDir();
const binDir = await makeTempDir();
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
const pluginRoot = join(codexPackageRoot, "plugin");
const lspRuntimeRoot = join(repoRoot, "packages", "lsp-tools-mcp");
await writeJson(join(repoRoot, "package.json"), {
name: "@code-yeongyu/lazycodex",
version: "0.1.2",
});
await writeJson(join(codexPackageRoot, "marketplace.json"), {
name: "sisyphuslabs",
plugins: [{ name: "omo", source: "./plugins/omo" }],
});
await writePluginAt(pluginRoot, "omo", "0.1.0");
await writeJson(join(pluginRoot, ".mcp.json"), {
mcpServers: {
lsp: {
command: "node",
args: ["../../lsp-tools-mcp/dist/cli.js", "mcp"],
cwd: ".",
},
},
});
await mkdir(join(pluginRoot, "dist"), { recursive: true });
await writeFile(join(pluginRoot, "dist", "cli.js"), "#!/usr/bin/env node\nconsole.log('prebuilt')\n");
await writeJson(join(lspRuntimeRoot, "dist", "cli.js"), { executable: true });
const commands = [];
// when
const result = await installMarketplaceLocally({
repoRoot,
codexHome,
binDir,
platform: "linux",
runCommand: async (command, args, options) => {
commands.push([command, args.join(" "), options.cwd]);
},
log: () => {},
});
// then
const pluginCacheRoot = join(codexHome, "plugins", "cache", "sisyphuslabs", "omo", "0.1.0");
const cachedMcp = JSON.parse(await readFile(join(pluginCacheRoot, ".mcp.json"), "utf8"));
const cachedLspCli = join(pluginCacheRoot, "mcp", "lsp", "dist", "cli.js");
assert.deepEqual(result.installed.map((plugin) => `${plugin.name}@${plugin.version}`), ["omo@0.1.0"]);
assert.deepEqual(
commands.map(([command, args, cwd]) => [command, args, cwd]),
[["npm", "install --omit=dev", pluginCacheRoot]],
);
assert.deepEqual(cachedMcp.mcpServers.lsp.args, [cachedLspCli, "mcp"]);
assert.equal((await stat(cachedLspCli)).isFile(), true);
assert.notEqual(cachedMcp.mcpServers.lsp.args[0], join(lspRuntimeRoot, "dist", "cli.js"));
});
+5 -3
View File
@@ -6,9 +6,11 @@ import { exists, isRecord } from "./utils.mjs";
import { COMMAND_SHIM_MARKER } from "./command-shim.mjs";
import { removeLegacyCodexComponentBins } from "./legacy-bins.mjs";
export async function installCachedPlugin({ codexHome, marketplaceName, name, runCommand, sourcePath, version }) {
await maybeRunNpmInstall(sourcePath, runCommand);
await maybeRunNpmBuild(sourcePath, runCommand);
export async function installCachedPlugin({ buildSource = true, codexHome, marketplaceName, name, runCommand, sourcePath, version }) {
if (buildSource) {
await maybeRunNpmInstall(sourcePath, runCommand);
await maybeRunNpmBuild(sourcePath, runCommand);
}
const targetPath = join(codexHome, "plugins", "cache", marketplaceName, name, version);
await replaceDirectory(sourcePath, targetPath, shouldCopyPluginPath);
@@ -40,7 +40,7 @@ function resolveExternalMcpPackageRoot(runtimePath, sourceRoot) {
if (!isPathInside(runtimePath, packagesRoot)) return undefined;
let packageRoot = dirname(runtimePath);
while (packageRoot !== packagesRoot) {
if (existsSync(join(packageRoot, "package.json")) && isPathInside(runtimePath, join(packageRoot, "dist"))) {
if (isPathInside(runtimePath, join(packageRoot, "dist")) && isRuntimePackageRoot(packageRoot)) {
return packageRoot;
}
const parent = dirname(packageRoot);
@@ -50,6 +50,10 @@ function resolveExternalMcpPackageRoot(runtimePath, sourceRoot) {
return undefined;
}
function isRuntimePackageRoot(packageRoot) {
return existsSync(join(packageRoot, "package.json")) || existsSync(join(packageRoot, "dist"));
}
function findPackagesRoot(path) {
let current = resolve(path);
for (let index = 0; index < 8; index++) {