feat(omo-codex): link bundled component agents into codex home

Discover component .toml agent files and symlink (or copy on Windows)
them into the Codex agents dir during install, recording an installed
agent manifest and wiring agent config_file entries into config.toml.
Add CodexAgentConfig type and support local marketplace source.
This commit is contained in:
YeonGyu-Kim
2026-05-29 11:18:01 +09:00
parent bb8ef30bbe
commit a49acecc3a
12 changed files with 423 additions and 15 deletions
@@ -0,0 +1,60 @@
import assert from "node:assert/strict";
import { lstat, mkdir, readFile, readlink, symlink, 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";
const legacyCodexPluginMarketplace = ["code", "yeongyu", "codex", "plugins"].join("-");
test(
"#given bundled agent roles and stale legacy links #when installing locally #then relinks Codex agents to current cache",
{ skip: process.platform === "win32" ? "Windows copies agent files instead of symlinking them" : false },
async () => {
const repoRoot = await makeTempDir();
const codexHome = await makeTempDir();
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
const pluginRoot = join(codexPackageRoot, "plugin");
const agentsRoot = join(pluginRoot, "components", "ultrawork", "agents");
await writeJson(join(codexPackageRoot, "marketplace.json"), {
name: "sisyphuslabs",
plugins: [{ name: "omo", source: "./plugins/omo" }],
});
await writePluginAt(pluginRoot, "omo", "0.1.0");
await mkdir(agentsRoot, { recursive: true });
for (const agentName of ["explorer", "librarian", "plan"]) {
await writeFile(join(agentsRoot, `${agentName}.toml`), `name = "${agentName}"\n`);
}
await mkdir(join(codexHome, "agents"), { recursive: true });
await symlink(
join(codexHome, "plugins", "cache", legacyCodexPluginMarketplace, "omo", "0.1.0", "components", "ultrawork", "agents", "explorer.toml"),
join(codexHome, "agents", "explorer.toml"),
);
const result = await installMarketplaceLocally({
repoRoot,
codexHome,
platform: "linux",
runCommand: async () => {},
log: () => {},
});
assert.equal(result.installed.length, 1);
const pluginPath = result.installed[0].path;
for (const agentName of ["explorer", "librarian", "plan"]) {
const agentPath = join(codexHome, "agents", `${agentName}.toml`);
assert.equal((await lstat(agentPath)).isSymbolicLink(), true);
assert.equal(await readlink(agentPath), join(pluginPath, "components", "ultrawork", "agents", `${agentName}.toml`));
assert.equal(await readFile(agentPath, "utf8"), `name = "${agentName}"\n`);
}
const installedAgents = JSON.parse(await readFile(join(pluginPath, ".installed-agents.json"), "utf8"));
assert.deepEqual(installedAgents.agents.sort(), [
join(codexHome, "agents", "explorer.toml"),
join(codexHome, "agents", "librarian.toml"),
join(codexHome, "agents", "plan.toml"),
]);
},
);
+35 -2
View File
@@ -9,6 +9,7 @@ import {
pruneMarketplaceCache,
pruneMarketplacePluginCaches,
} from "./install/cache.mjs";
import { linkCachedPluginAgents } from "./install/agents.mjs";
import { updateCodexConfig } from "./install/config.mjs";
import { trustedHookStatesForPlugin } from "./install/hook-trust.mjs";
import { defaultRunCommand } from "./install/process.mjs";
@@ -22,10 +23,23 @@ import {
const LEGACY_CODEX_PLUGIN_MARKETPLACE = ["code", "yeongyu", "codex", "plugins"].join("-");
const SISYPHUS_LEGACY_CACHE_MARKETPLACES = ["lazycodex", LEGACY_CODEX_PLUGIN_MARKETPLACE];
export function resolveCodexInstallerBinDir(options = {}) {
const homeDir = resolve(options.homeDir ?? homedir());
const env = options.env ?? process.env;
const explicitBinDir = nonEmptyEnvValue(env, "CODEX_LOCAL_BIN_DIR");
if (explicitBinDir !== undefined) return explicitBinDir;
const codexHome = resolve(options.codexHome ?? nonEmptyEnvValue(env, "CODEX_HOME") ?? join(homeDir, ".codex"));
const defaultCodexHome = resolve(join(homeDir, ".codex"));
return codexHome === defaultCodexHome ? join(homeDir, ".local", "bin") : join(codexHome, "bin");
}
export async function installMarketplaceLocally(options = {}) {
const repoRoot = resolve(options.repoRoot ?? process.cwd());
const codexHome = resolve(options.codexHome ?? process.env.CODEX_HOME ?? join(homedir(), ".codex"));
const binDir = resolve(options.binDir ?? process.env.CODEX_LOCAL_BIN_DIR ?? join(homedir(), ".local", "bin"));
const env = options.env ?? process.env;
const homeDir = resolve(options.homeDir ?? homedir());
const codexHome = resolve(options.codexHome ?? nonEmptyEnvValue(env, "CODEX_HOME") ?? join(homeDir, ".codex"));
const binDir = resolve(options.binDir ?? resolveCodexInstallerBinDir({ codexHome, env, homeDir }));
const platform = options.platform ?? process.platform;
const runCommand = options.runCommand ?? defaultRunCommand;
const log = options.log ?? console.log;
@@ -34,6 +48,7 @@ export async function installMarketplaceLocally(options = {}) {
marketplacePath: join(codexPackageRoot, "marketplace.json"),
});
const installed = [];
const agentConfigs = new Map();
for (const entry of marketplace.plugins) {
const sourcePath = resolvePluginSource(codexPackageRoot, entry, { pathOverride: "./plugin" });
@@ -59,6 +74,12 @@ export async function installMarketplaceLocally(options = {}) {
for (const link of binLinks) {
log(`Linked ${link.name} -> ${link.target}`);
}
const agentLinks = await linkCachedPluginAgents({ codexHome, pluginRoot: plugin.path, platform });
for (const link of agentLinks) {
log(`Linked agent ${link.name} -> ${link.target}`);
const agentName = agentNameFromToml(link.name);
agentConfigs.set(agentName, { name: agentName, configFile: `./agents/${link.name}` });
}
installed.push(plugin);
}
@@ -84,6 +105,7 @@ export async function installMarketplaceLocally(options = {}) {
marketplaceName: marketplace.name,
pluginNames,
trustedHookStates,
agentConfigs: [...agentConfigs.values()].sort((left, right) => left.name.localeCompare(right.name)),
});
for (const plugin of installed) {
@@ -93,6 +115,17 @@ export async function installMarketplaceLocally(options = {}) {
return { marketplaceName: marketplace.name, installed };
}
function agentNameFromToml(fileName) {
return fileName.endsWith(".toml") ? fileName.slice(0, -".toml".length) : fileName;
}
function nonEmptyEnvValue(env, key) {
const value = env[key];
if (typeof value !== "string") return undefined;
const trimmed = value.trim();
return trimmed.length === 0 ? undefined : value;
}
function legacyCacheMarketplaces(marketplaceName) {
return marketplaceName === "sisyphuslabs" ? SISYPHUS_LEGACY_CACHE_MARKETPLACES : [];
}
@@ -4,12 +4,68 @@ import { dirname, join } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { installMarketplaceLocally } from "./install-local.mjs";
import { installMarketplaceLocally, resolveCodexInstallerBinDir } from "./install-local.mjs";
import { makeTempDir, writeJson, writePluginAt } from "./install-test-fixtures.mjs";
const scriptDir = dirname(fileURLToPath(import.meta.url));
const legacyCodexPluginMarketplace = ["code", "yeongyu", "codex", "plugins"].join("-");
test("#given default CODEX_HOME #when resolving local installer bin dir without override #then preserves user local bin precedence", () => {
const homeDir = join("/tmp", "omo-codex-home-default");
const codexHome = join(homeDir, ".codex");
assert.equal(resolveCodexInstallerBinDir({ codexHome, env: {}, homeDir }), join(homeDir, ".local", "bin"));
});
test("#given custom CODEX_HOME #when resolving local installer bin dir without override #then keeps generated omo inside that Codex home", () => {
const homeDir = join("/tmp", "omo-codex-home-custom");
const codexHome = join("/tmp", "omo-codex-install-custom");
assert.equal(resolveCodexInstallerBinDir({ codexHome, env: {}, homeDir }), join(codexHome, "bin"));
});
test("#given custom CODEX_HOME and PATH without omo #when installing locally without bin override #then bootstraps via local CLI when omo is absent", async () => {
const repoRoot = await makeTempDir();
const codexHome = await makeTempDir();
const homeDir = await makeTempDir();
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
const pluginRoot = join(codexPackageRoot, "plugin");
await writeJson(join(codexPackageRoot, "marketplace.json"), {
name: "sisyphuslabs",
plugins: [{ name: "omo", source: "./plugins/omo" }],
});
await writePluginAt(pluginRoot, "omo", "0.1.0");
const result = await installMarketplaceLocally({
repoRoot,
codexHome,
env: { PATH: "/usr/bin:/bin" },
homeDir,
platform: "linux",
runCommand: async () => {},
log: () => {},
});
assert.equal(result.installed.length, 1);
assert.equal(await readlink(join(codexHome, "bin", "omo")), join(result.installed[0].path, "dist", "cli.js"));
});
test("#given explicit CODEX_LOCAL_BIN_DIR #when resolving local installer bin dir #then preserves installed omo precedence", () => {
const homeDir = join("/tmp", "omo-codex-home-explicit");
const codexHome = join("/tmp", "omo-codex-install-explicit");
const explicitBinDir = join("/tmp", "omo-codex-explicit-bin");
assert.equal(
resolveCodexInstallerBinDir({
codexHome,
env: { CODEX_LOCAL_BIN_DIR: explicitBinDir },
homeDir,
}),
explicitBinDir,
);
});
test("#given omo plugin source #when inspecting identity #then uses sisyphuslabs omo metadata", async () => {
const pluginRoot = join(scriptDir, "..", "plugin");
@@ -144,6 +200,9 @@ 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, /\[agents\.explorer\]\nconfig_file = "\.\/agents\/explorer\.toml"/);
assert.match(config, /\[agents\.librarian\]\nconfig_file = "\.\/agents\/librarian\.toml"/);
assert.match(config, /\[agents\.plan\]\nconfig_file = "\.\/agents\/plan\.toml"/);
assert.doesNotMatch(config, /stale@debug-marketplace/);
});
@@ -21,6 +21,7 @@ export async function writePluginAt(pluginRoot, name, version) {
await mkdir(join(pluginRoot, "dist"), { recursive: true });
await mkdir(join(pluginRoot, "hooks"), { recursive: true });
await mkdir(join(pluginRoot, "skills", name), { recursive: true });
await mkdir(join(pluginRoot, "components", "ultrawork", "agents"), { recursive: true });
await writeJson(join(pluginRoot, ".codex-plugin", "plugin.json"), {
name,
version,
@@ -40,6 +41,9 @@ export async function writePluginAt(pluginRoot, name, version) {
});
await writeJson(join(pluginRoot, "hooks", "hooks.json"), { hooks: {} });
await writeFile(join(pluginRoot, "skills", name, "SKILL.md"), "---\nname: test\n---\n");
await writeFile(join(pluginRoot, "components", "ultrawork", "agents", "explorer.toml"), 'name = "explorer"\n');
await writeFile(join(pluginRoot, "components", "ultrawork", "agents", "librarian.toml"), 'name = "librarian"\n');
await writeFile(join(pluginRoot, "components", "ultrawork", "agents", "plan.toml"), 'name = "plan"\n');
await writeJson(join(pluginRoot, "package.json"), {
name: `@example/${name}`,
version,
@@ -0,0 +1,84 @@
import { basename, join } from "node:path";
import { copyFile, lstat, mkdir, readdir, rm, symlink, writeFile } from "node:fs/promises";
import { exists } from "./utils.mjs";
const MANIFEST_FILE = ".installed-agents.json";
export async function linkCachedPluginAgents({ codexHome, pluginRoot, platform = process.platform }) {
const bundledAgents = await discoverBundledAgents(pluginRoot);
if (bundledAgents.length === 0) {
await writeManifest(pluginRoot, []);
return [];
}
const agentsDir = join(codexHome, "agents");
await mkdir(agentsDir, { recursive: true });
const linked = [];
for (const agentPath of bundledAgents) {
const linkPath = join(agentsDir, basename(agentPath));
if (platform === "win32") {
await replaceWithCopy(linkPath, agentPath);
} else {
await replaceWithSymlink(linkPath, agentPath);
}
linked.push({ name: basename(agentPath), path: linkPath, target: agentPath });
}
await writeManifest(pluginRoot, linked.map((entry) => entry.path));
return linked;
}
async function discoverBundledAgents(pluginRoot) {
const componentsRoot = join(pluginRoot, "components");
if (!(await exists(componentsRoot))) return [];
const componentEntries = await readdir(componentsRoot, { withFileTypes: true });
const agents = [];
for (const entry of componentEntries) {
if (!entry.isDirectory()) continue;
const agentsRoot = join(componentsRoot, entry.name, "agents");
if (!(await exists(agentsRoot))) continue;
const agentEntries = await readdir(agentsRoot, { withFileTypes: true });
for (const file of agentEntries) {
if (!file.isFile() || !file.name.endsWith(".toml")) continue;
agents.push(join(agentsRoot, file.name));
}
}
agents.sort();
return agents;
}
async function replaceWithSymlink(linkPath, target) {
await prepareReplacement(linkPath);
await symlink(target, linkPath);
}
async function replaceWithCopy(linkPath, target) {
await prepareReplacement(linkPath);
await copyFile(target, linkPath);
}
async function prepareReplacement(linkPath) {
if (!(await lstatExists(linkPath))) return;
const entryStat = await lstat(linkPath);
if (entryStat.isDirectory() && !entryStat.isSymbolicLink()) {
throw new Error(`${linkPath} already exists and is a directory; refusing to replace`);
}
await rm(linkPath, { force: true });
}
async function writeManifest(pluginRoot, agentPaths) {
const manifestPath = join(pluginRoot, MANIFEST_FILE);
const payload = { agents: [...agentPaths].sort() };
await writeFile(manifestPath, `${JSON.stringify(payload, null, "\t")}\n`);
}
async function lstatExists(path) {
try {
await lstat(path);
return true;
} catch (error) {
if (error instanceof Error && "code" in error && error.code === "ENOENT") return false;
throw error;
}
}
@@ -18,6 +18,7 @@ export async function updateCodexConfig({
marketplaceSource = defaultMarketplaceSource(marketplaceName, repoRoot),
pluginNames,
trustedHookStates = [],
agentConfigs = [],
}) {
await mkdir(dirname(configPath), { recursive: true });
let config = "";
@@ -39,6 +40,9 @@ export async function updateCodexConfig({
for (const state of trustedHookStates) {
config = ensureHookTrusted(config, state.key, state.trustedHash);
}
for (const agentConfig of agentConfigs) {
config = ensureAgentConfig(config, agentConfig);
}
await writeFile(configPath, config.trimEnd() + "\n");
}
@@ -119,6 +123,18 @@ function ensureHookTrusted(config, key, trustedHash) {
return replaceOrInsertSetting(config, section, "trusted_hash", JSON.stringify(trustedHash));
}
function ensureAgentConfig(config, agentConfig) {
const header = `agents.${tomlKeySegment(agentConfig.name)}`;
const section = findTomlSection(config, header);
const configFile = JSON.stringify(agentConfig.configFile);
if (!section) return appendBlock(config, `[${header}]\nconfig_file = ${configFile}\n`);
return replaceOrInsertSetting(config, section, "config_file", configFile);
}
function tomlKeySegment(value) {
return /^[A-Za-z0-9_-]+$/.test(value) ? value : JSON.stringify(value);
}
function removeTomlSections(config, shouldRemove) {
return splitTomlSections(config)
.filter((section) => section.header === null || !shouldRemove(section.header))