Skip to content
TypeScript
3.4 KB
Raw Blame
/**
 * Codex CLI 러너 (PRD §7).
 * `codex exec "<instruction>" --json [--model <id>] --dangerously-bypass-approvals-and-sandbox -o <last>`
 * stdout 의 thread-event JSONL = transcript. CODEX_HOME 격리(하네스). model_id='default' 면 --model 생략.
 * 401 등 실패는 fail-soft.
 */
import { mkdirSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { instructionFor, isLocalBench, setupTask, taskIds, verify } from "../benches/index";
import { parseComboId } from "../combos";
import { resolveModelAlias } from "../config";
import { applyHarnessToWorkdir, harnessCodexEnv } from "../harness";
import type { RawRunArtifact, RunInvocation } from "../models";
import { type RunnerAdapter, RunnerUnavailable } from "./base";
import { commandExists, execCapture } from "./util";

export class CodexCliRunner implements RunnerAdapter {
  name = "codex";

  isAvailable(): boolean {
    return commandExists("codex");
  }

  loadTaskCatalog(bench: string): string[] {
    if (isLocalBench(bench)) return taskIds(bench);
    throw new RunnerUnavailable(`codex runner: external bench '${bench}' not supported directly`);
  }

  async run(inv: RunInvocation): Promise<RawRunArtifact> {
    const { model, harness } = parseComboId(inv.combo.comboId);
    const work = inv.workDir;
    mkdirSync(work, { recursive: true });
    const resultPath = join(work, "output.txt");
    const transcriptPath = join(work, "transcript.jsonl");

    if (isLocalBench(inv.bench)) setupTask(inv.taskId, work);

    const instruction =
      inv.instruction ?? instructionFor(inv.taskId) ?? `Complete task ${inv.taskId}`;
    const resolved = resolveModelAlias(model);
    const lastPath = join(work, "codex-last.txt");
    const args = [
      "exec",
      instruction,
      "--json",
      "--dangerously-bypass-approvals-and-sandbox",
      "-o",
      lastPath,
      // 계정 기본('default')은 명시 override 거부 → --model 생략
      ...(resolved && resolved !== "default" ? ["--model", resolved] : []),
    ];

    const env = harnessCodexEnv(harness);
    // 룰형 하네스(superpowers/ponytail/mattpocock)는 work_dir 에 AGENTS.md 를 materialize —
    // codex 는 cwd 의 AGENTS.md 를 읽으므로 이것이 실제 적용 경로다(조용한 baseline 방지).
    applyHarnessToWorkdir(harness, work);
    const res = await execCapture("codex", args, { cwd: work, timeoutS: inv.timeoutS, env });

    // stdout = thread-event JSONL
    writeFileSync(transcriptPath, res.stdout || "");
    writeFileSync(resultPath, res.stdout ? lastSummary(res.stdout) : res.stderr || "(no output)");

    const ok = res.error === null && !res.timedOut && res.code === 0;
    const rawResolved = isLocalBench(inv.bench) ? verify(inv.taskId, work) : null;

    return {
      invocation: inv,
      success: ok,
      resultPath,
      transcriptPath,
      wallTimeMs: res.wallTimeMs,
      rawResolved,
      totalCostUsd: null, // 구독제 → 추정으로 위임
      numTurns: null,
      error: res.error ?? (res.timedOut ? "timeout" : res.code !== 0 ? `exit ${res.code}` : null),
    };
  }
}

function lastSummary(stdout: string): string {
  const texts: string[] = [];
  for (const line of stdout.split("\n")) {
    const s = line.trim();
    if (!s) continue;
    try {
      const o = JSON.parse(s);
      const item = o.item ?? o.msg?.item;
      if (item?.text) texts.push(String(item.text));
    } catch {
      // ignore
    }
  }
  return texts.join("\n") || "(no result text)";
}