// Extracted from a private product repo for methodology illustration. // Not runnable standalone: imports reference the private engine. Shapes, // checks, judge prompts, and flow are shown as actually used, July 2026. /* eslint-disable @typescript-eslint/no-explicit-any */ // GEPA-lite: one reflective optimization cycle for the voice register. // baseline batch → natural-language reflection over the failures → candidate // register → candidate batch → side-by-side comparison. A human adopts or // rejects; nothing self-installs. // npx tsx --env-file=.env.local scripts/optimize-register.ts --n 6 import { mkdirSync, writeFileSync } from "node:fs"; import { callClaude, MODEL_SYNTHESIS, TEXT_REGISTER } from "../src/engine"; import { loadRolesFromDb, printSummary, runBatch } from "./eval-lib"; function arg(name: string, fallback?: string) { const i = process.argv.indexOf(`--${name}`); return i > -1 ? process.argv[i + 1] : fallback; } async function main() { const n = Number(arg("n", "6")); const { roles, titles } = await loadRolesFromDb(); const seedData = { roles }; console.log(`— Phase 1: baseline (${n} interviews)`); const baseline = await runBatch({ n, registerText: TEXT_REGISTER, label: "baseline", seedData, titles }); printSummary(baseline.summary); // Reflection: show the optimizer the register, the metrics, and the worst // turns with the judge's reasons — ask for a revised register. const worstTurns = baseline.results .filter((r: any) => !r.error && r.judge?.worst_turn?.quote) .map((r: any) => `- [${r.role}] "${r.judge.worst_turn.quote}" — ${r.judge.worst_turn.why}`) .slice(0, 8) .join("\n"); const judgeNotes = baseline.results .filter((r: any) => !r.error) .flatMap((r: any) => ["spoken_register", "their_words", "probe_depth", "question_quality"] .filter((k) => (r.judge?.[k]?.score ?? 5) <= 3) .map((k) => `- [${r.role}] ${k}=${r.judge[k].score}: ${r.judge[k].why}`) ) .slice(0, 10) .join("\n"); console.log(`\n— Phase 2: reflection (Opus proposes a revised register)`); const candidate = ( await callClaude( [ { role: "user", content: `You are optimizing the VOICE REGISTER block appended to a spoken AI interviewer's system prompt. The interviewer's core prompt (coverage steering, probing strategy, participant-safety language) is fixed — you may ONLY rewrite the register block below. The register's job: make a writing model produce genuine speech. CURRENT REGISTER: ${TEXT_REGISTER} MEASURED RESULTS (${baseline.summary.completed} simulated interviews): ${JSON.stringify(baseline.summary, null, 2)} WEAKEST INTERVIEWER TURNS (with judge's reasons): ${worstTurns || "(none captured)"} LOW JUDGE SCORES: ${judgeNotes || "(none below 3)"} Rewrite the register to fix what the evidence shows is failing, while preserving what scores well. Constraints: keep the one-question rule and the never-frame-AI-as-replacement spirit (that lives in the core prompt); the first spoken turn must also obey the register (openings are currently the worst offender); prefer exemplars over added rules — you may replace rules with BAD/GOOD pairs; do not grow the block beyond roughly its current length. Return ONLY the new register text, starting with a blank line then "VOICE REGISTER", exactly as it will be appended to the system prompt. No commentary.`, }, ], "You are a rigorous prompt engineer optimizing against measured evidence. Return only the revised register text.", MODEL_SYNTHESIS, { max_tokens: 1500 } ) ).trim(); mkdirSync("evals/candidates", { recursive: true }); const stamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19); const candPath = `evals/candidates/register-${stamp}.txt`; writeFileSync(candPath, "\n" + candidate + "\n"); console.log(`Candidate written to ${candPath}\n`); console.log(`— Phase 3: candidate (${n} interviews)`); const cand = await runBatch({ n, registerText: "\n" + candidate, label: "candidate", seedData, titles }); printSummary(baseline.summary); printSummary(cand.summary); mkdirSync("evals/results", { recursive: true }); writeFileSync( `evals/results/optimize-${stamp}.json`, JSON.stringify({ baseline: baseline.summary, candidate: cand.summary, candidatePath: candPath, baselineResults: baseline.results, candidateResults: cand.results }, null, 2) ); const bj = baseline.summary.judge; const cj = cand.summary.judge; const delta = cj.spoken_register - bj.spoken_register + (cj.their_words - bj.their_words) + (cj.probe_depth - bj.probe_depth) + (cj.question_quality - bj.question_quality); console.log(`\nJudge delta (sum of spoken/their-words/probe/questions): ${delta >= 0 ? "+" : ""}${delta.toFixed(2)}`); console.log(delta > 0.3 ? "RECOMMEND: adopt candidate (update TEXT_REGISTER in src/engine/voice.ts)." : delta < -0.3 ? "RECOMMEND: reject candidate." : "RECOMMEND: inconclusive — consider a larger n."); } main().catch((e) => { console.error(e); process.exit(1); });