Skip to content
TypeScript
5.3 KB
Raw Blame
/**
 * 하네스 per-run 적용 (PRD §10, PRD v2 §W1). 전역 오염 없음 — 조합별로만 적용.
 *  - cc 하네스(superpowers/omc/ecc/ponytail/mattpocock): --plugin-dir <설치경로>
 *  - codex 하네스(omx/lazycodex/omo): CODEX_HOME 격리 홈
 *  - 멀티모델 에이전트(opencode/openclaw/hermes) 하네스(omo/ponytail/mattpocock):
 *    work_dir 에 룰(AGENTS.md/skills) 를 per-run 으로 **materialize**(opencode/ponytail 은 repo
 *    AGENTS.md 를 자동 로드 → 전역 오염 없는 깔끔한 적용 경로).
 */
import { cpSync, existsSync, mkdirSync, readdirSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";

export type HarnessStatus = "none" | "applied" | "unavailable";
export type CodexHomeStatus = "none" | "built" | "pending";

const CC_HARNESSES = new Set(["superpowers", "omc", "ecc", "ponytail", "mattpocock"]);
const CODEX_HARNESSES = new Set(["omx", "lazycodex", "omo"]);
// work_dir 에 룰(AGENTS.md/skills)을 materialize 해서 적용 가능한 하네스.
// 멀티모델 에이전트(opencode/openclaw/hermes) + codex(작업 폴더의 AGENTS.md 를 읽음) 공용 경로.
// superpowers 포함 — config 가 codex 호환으로 선언한 하네스가 조용히 baseline 으로 돌지 않도록.
const RULES_HARNESSES = new Set(["omo", "ponytail", "mattpocock", "superpowers"]);

// 하네스 → 플러그인 디렉터리 이름 후보
const PLUGIN_DIR_NAMES: Record<string, string[]> = {
  superpowers: ["superpowers"],
  omc: ["oh-my-claudecode", "omc"],
  ecc: ["ecc", "enhanced-claude-code"],
  omx: ["oh-my-codex", "omx"],
  lazycodex: ["lazycodex", "lazy-codex"],
  omo: ["oh-my-openagent", "omo"],
  ponytail: ["ponytail"],
  mattpocock: ["mattpocock-skills", "mattpocock", "skills"],
};

function pluginCacheRoot(): string {
  return join(homedir(), ".claude", "plugins", "cache");
}

// 탐지: ~/.claude/plugins/cache/<marketplace>/<plugin>[/<version>]/.claude-plugin/plugin.json
export function findCcPluginDir(harness: string): string | null {
  if (!CC_HARNESSES.has(harness)) return null;
  const root = pluginCacheRoot();
  if (!existsSync(root)) return null;
  const names = PLUGIN_DIR_NAMES[harness] ?? [harness];
  try {
    for (const marketplace of readdirSync(root)) {
      const mp = join(root, marketplace);
      for (const name of names) {
        const candidate = join(mp, name);
        if (existsSync(join(candidate, ".claude-plugin", "plugin.json"))) return candidate;
        // 한 단계 더(버전 폴더)
        if (existsSync(candidate)) {
          for (const sub of safeReaddir(candidate)) {
            const c2 = join(candidate, sub);
            if (existsSync(join(c2, ".claude-plugin", "plugin.json"))) return c2;
          }
        }
      }
    }
  } catch {
    return null;
  }
  return null;
}

function safeReaddir(p: string): string[] {
  try {
    return readdirSync(p);
  } catch {
    return [];
  }
}

export function codexHomePath(harness: string): string {
  return join(homedir(), ".cache", "theseus", "codex-homes", harness);
}

/** 멀티모델 에이전트용 룰 캐시: ~/.cache/theseus/harness-rules/<harness>/ (AGENTS.md, skills/). */
export function harnessRulesPath(harness: string): string {
  return join(homedir(), ".cache", "theseus", "harness-rules", harness);
}

export function harnessClaudeArgs(harness: string): string[] {
  if (harness === "none") return [];
  const dir = findCcPluginDir(harness);
  return dir ? ["--plugin-dir", dir] : [];
}

export function harnessCodexEnv(harness: string): Record<string, string> {
  if (harness === "none" || !CODEX_HARNESSES.has(harness)) return {};
  const home = codexHomePath(harness);
  return existsSync(home) ? { CODEX_HOME: home } : {};
}

/**
 * 룰형 하네스를 per-run materialize — 멀티모델 에이전트 + codex 공용.
 * 룰 캐시가 있으면 AGENTS.md/skills 를 work_dir 로 복사. 반환=적용 여부. 절대 throw 금지.
 */
export function applyHarnessToWorkdir(harness: string, workDir: string): boolean {
  if (harness === "none" || !RULES_HARNESSES.has(harness)) return false;
  const src = harnessRulesPath(harness);
  if (!existsSync(src)) return false;
  try {
    mkdirSync(workDir, { recursive: true });
    cpSync(src, workDir, { recursive: true });
    return true;
  } catch {
    return false;
  }
}

/** 테스트/오프라인용: 최소 룰 캐시(AGENTS.md)를 생성한다(install 의 fallback). */
export function ensureHarnessRulesStub(harness: string, content: string): string {
  const dir = harnessRulesPath(harness);
  try {
    mkdirSync(dir, { recursive: true });
    writeFileSync(join(dir, "AGENTS.md"), content);
  } catch {
    // ignore
  }
  return dir;
}

export function harnessStatus(harness: string): HarnessStatus {
  if (harness === "none") return "none";
  // cc 플러그인 / codex 홈 / 멀티모델 룰 캐시 중 하나라도 있으면 applied
  if (findCcPluginDir(harness)) return "applied";
  if (CODEX_HARNESSES.has(harness) && existsSync(codexHomePath(harness))) return "applied";
  if (RULES_HARNESSES.has(harness) && existsSync(harnessRulesPath(harness))) return "applied";
  return "unavailable";
}

export function codexHomeStatus(harness: string): CodexHomeStatus {
  if (harness === "none" || !CODEX_HARNESSES.has(harness)) return "none";
  return existsSync(codexHomePath(harness)) ? "built" : "pending";
}