File size: 2,724 Bytes
914f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { execFileSync } from 'node:child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { loadConfig } from './config.js';
import { buildReviewContext } from './review.js';
import type { PrAgentState } from './types.js';

/**

 * Codex CLI reviewer: a true third-vendor slot (OpenAI, not Cursor-billed).

 * Two prompts on one model are not two independent reviewers — Codex has

 * previously caught bugs both Claude slots missed. Runs `codex exec` headless

 * in read-only sandbox mode and writes the same review-<slot>.txt contract the

 * aggregate step already consumes.

 */
export async function runCodexReview(

  repoRoot: string,

  prNumber: number,

  baseRef: string,

  state: PrAgentState,

): Promise<void> {
  const config = loadConfig(repoRoot);
  const apiKey = process.env.OPENAI_API_KEY;
  if (!apiKey) {
    throw new Error('OPENAI_API_KEY is required for the codex review slot');
  }

  // Codex CLI (0.149+) does not read OPENAI_API_KEY from the environment for
  // API auth — without an explicit login it sends no Authorization header at
  // all ("401 Missing bearer or basic authentication"). Register the key via
  // `codex login --with-api-key`, which reads it from stdin.
  execFileSync('codex', ['login', '--with-api-key'], {
    input: apiKey,
    stdio: ['pipe', 'inherit', 'inherit'],
  });

  const promptPath = join(repoRoot, '.github/pr-agent/prompts/codex-review.md');
  const basePrompt = readFileSync(promptPath, 'utf8');
  const context = buildReviewContext(repoRoot, prNumber, baseRef, state);
  const fullPrompt = `${basePrompt}\n\n---\n\n${context}`;

  const outDir = join(repoRoot, '.pr-agent');
  mkdirSync(outDir, { recursive: true });
  const outPath = join(outDir, 'review-codex.txt');

  const model = process.env.PR_AGENT_CODEX_MODEL ?? config.codexModel;

  // Prompt goes via stdin ("-"): it embeds a full unified diff and can exceed
  // the kernel's per-argument size limit as an argv entry.
  execFileSync(
    'codex',
    [
      'exec',
      '--sandbox',
      'read-only',
      '--skip-git-repo-check',
      '-m',
      model,
      '--output-last-message',
      outPath,
      '-',
    ],
    {
      cwd: repoRoot,
      input: fullPrompt,
      stdio: ['pipe', 'inherit', 'inherit'],
      timeout: 20 * 60_000,
    },
  );

  if (!existsSync(outPath)) {
    // Aggregate treats a missing artifact as "slot did not run"; an empty file
    // means "ran but produced no verdict" — either way the cycle stays safe.
    writeFileSync(outPath, '', 'utf8');
  }
  console.log('Review codex complete.');
}