Files
everything-claude-code/scripts/lib/mcp-inventory/readers/codex.js
Affaan Mustafa c8caf193c4 feat: worktree-lifecycle service (deterministic conflict prediction + safe GC) (#2164)
* feat: add worktree-lifecycle service (ecc.worktree-lifecycle.v1)

The "unowned moat" from the orchestrator landscape research: no existing
tool ships deterministic merge-conflict prediction or a safe worktree GC.

- scripts/lib/worktree-lifecycle/git.js: injectable, hermetic git layer.
  Predicts merge conflicts WITHOUT touching the working tree via
  `git merge-tree`. Strips inherited GIT_* env so it is safe inside hooks.
- scripts/lib/worktree-lifecycle/lifecycle.js: deterministic state machine
  (main/dirty/conflict/merge-ready/merged/stale/idle) + planCleanup that
  buckets worktrees into remove / salvage / keep. Only fully-merged trees
  are auto-removable; stale (unmerged+inactive) => salvage, never deleted.
- scripts/worktree-lifecycle.js: CLI (--json/--conflicts/--stale/
  --cleanup-plan/--base/--stale-days/--repo).
- tests/lib/worktree-lifecycle.test.js: 11 tests (fake-git + real-git).

Safety model mirrors the reference-arch salvage rule, validated by the
2026-06-05 MacBook->Mac Mini consolidation. Tests: 11/0.

* fix: hermetic git env in session adapters + mcp-inventory lint

- session adapters (codex-worktree, opencode): resolveGitBranch stripped
  no git env, so the "outside a repo" path returned the host branch when
  run inside a git hook (GIT_DIR set). Strip GIT_* before rev-parse.
- mcp-inventory: fix eslint no-unused-vars (signatures) and a stale
  eslint-disable directive in the merged code.

* test: run each test with inherited git env stripped (hermetic runner)

When the suite runs inside a git hook (pre-push), git sets GIT_DIR/
GIT_WORK_TREE, which hijack 'git -C <dir>' calls in tests that exercise
real git, making them operate on the host repo. Strip GIT_* before
spawning each test so the suite is isolated from ambient git state.

---------

Co-authored-by: ECC Test <ecc@example.test>
2026-06-07 13:00:08 +08:00

83 lines
2.0 KiB
JavaScript

'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
// Codex stores MCP servers in ~/.codex/config.toml as TOML tables:
// [mcp_servers.NAME]
// command = "npx"
// args = ["-y", "pkg"]
// url = "https://..." # http transport
// [mcp_servers.NAME.env] # secret values live here
// [mcp_servers.NAME.http_headers]
// We parse with @iarna/toml when available and fall back to a minimal
// section parser so the reader degrades gracefully without the dependency.
function loadTomlParser(parseTomlImpl) {
if (typeof parseTomlImpl === 'function') {
return parseTomlImpl;
}
try {
return require('@iarna/toml').parse;
} catch {
return null;
}
}
function mapCodexServer(name, raw, configPath) {
if (!raw || typeof raw !== 'object') {
return null;
}
const type = raw.url ? 'http' : 'stdio';
return {
name,
type,
command: typeof raw.command === 'string' ? raw.command : null,
args: Array.isArray(raw.args) ? raw.args : [],
url: typeof raw.url === 'string' ? raw.url : null,
env: raw.env && typeof raw.env === 'object' ? raw.env : {},
enabled: raw.enabled === false ? false : true,
source: {
harness: 'codex',
scope: 'user',
configPath
}
};
}
function readCodexMcp(options = {}) {
const homeDir = options.homeDir || os.homedir();
const configPath = options.configPath || path.join(homeDir, '.codex', 'config.toml');
if (!fs.existsSync(configPath) || !fs.statSync(configPath).isFile()) {
return [];
}
const parseToml = loadTomlParser(options.parseTomlImpl);
if (!parseToml) {
return [];
}
let parsed;
try {
parsed = parseToml(fs.readFileSync(configPath, 'utf8'));
} catch {
return [];
}
const block = parsed && typeof parsed.mcp_servers === 'object' && parsed.mcp_servers
? parsed.mcp_servers
: {};
return Object.entries(block)
.map(([name, raw]) => mapCodexServer(name, raw, configPath))
.filter(Boolean);
}
module.exports = {
readCodexMcp,
mapCodexServer
};