import {
  runCampaign
} from "./chunk-7TPYV2ER.js";
import {
  buildReflectionPrompt,
  paretoFrontier,
  parseReflectionResponse,
  runCanaries,
  scoreRedTeamOutput
} from "./chunk-4ODZXQV2.js";
import {
  summarizeBackendIntegrity
} from "./chunk-E22YUOAL.js";
import {
  detectRewardHacking
} from "./chunk-YV7J7X5N.js";
import {
  callLlm
} from "./chunk-IHDHUN2X.js";

// src/campaign/auto-pr.ts
import { execSync } from "child_process";
import { writeFileSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
function openAutoPr(options) {
  if (options.gate.decision !== "ship") {
    return {
      opened: false,
      dryRun: false,
      reason: `gate verdict was "${options.gate.decision}" \u2014 refusing to open PR`
    };
  }
  const dryRun = options.dryRun ?? !process.env.GH_AUTO_PR_TOKEN;
  const branch = options.branch ?? `auto/${options.result.manifestHash.slice(0, 12)}`;
  const title = options.title ?? `auto: campaign ${options.result.manifestHash.slice(0, 8)} promoted by gate`;
  const body = renderPrBody(options.result, options.gate, options.promotedDiff);
  const bodyPath = join(tmpdir(), `auto-pr-body-${Date.now()}.md`);
  writeFileSync(bodyPath, body);
  if (dryRun) {
    return {
      opened: false,
      dryRun: true,
      reason: `dry-run (GH_AUTO_PR_TOKEN not set). Would create PR on ${options.ghOwner}/${options.ghRepo} branch ${branch}. Body at ${bodyPath}.`
    };
  }
  const ghExec = options.ghExec ?? defaultGhExec;
  const result = ghExec([
    "pr",
    "create",
    "--repo",
    `${options.ghOwner}/${options.ghRepo}`,
    "--head",
    branch,
    "--title",
    title,
    "--body-file",
    bodyPath
  ]);
  if (result.status !== 0) {
    return {
      opened: false,
      dryRun: false,
      reason: `gh pr create failed (exit ${result.status}): ${result.stderr.slice(0, 400)}`
    };
  }
  const prUrl = result.stdout.trim();
  return { opened: true, prUrl, dryRun: false, reason: "PR opened" };
}
function renderPrBody(result, gate, diff) {
  const lines = [];
  lines.push(`## Automated promotion by \`runImprovementLoop\``);
  lines.push("");
  lines.push(`**Manifest**: \`${result.manifestHash}\``);
  lines.push(`**Seed**: ${result.seed}`);
  lines.push(`**Duration**: ${Math.round(result.durationMs / 1e3)}s`);
  lines.push(
    `**Cells**: executed ${result.aggregates.cellsExecuted}, cached ${result.aggregates.cellsCached}, skipped ${result.aggregates.cellsSkipped}, failed ${result.aggregates.cellsFailed}`
  );
  lines.push(`**Total spend**: $${result.aggregates.totalCostUsd.toFixed(2)}`);
  lines.push("");
  lines.push(`### Gate verdict: \`${gate.decision}\``);
  lines.push("");
  for (const reason of gate.reasons) lines.push(`- ${reason}`);
  if (gate.delta !== void 0) lines.push(`- delta: ${gate.delta.toFixed(3)}`);
  lines.push("");
  lines.push("### Contributing gates");
  lines.push("");
  lines.push("| gate | passed | detail |");
  lines.push("|---|---|---|");
  for (const c of gate.contributingGates) {
    const detail = typeof c.detail === "object" ? JSON.stringify(c.detail).slice(0, 80) : String(c.detail).slice(0, 80);
    lines.push(`| ${c.name} | ${c.passed ? "\u2713" : "\u2717"} | ${detail} |`);
  }
  lines.push("");
  lines.push("### Promoted surface");
  lines.push("");
  lines.push("```diff");
  lines.push(diff.slice(0, 8e3));
  lines.push("```");
  lines.push("");
  lines.push("### By-judge aggregates");
  lines.push("");
  lines.push("| judge | mean | ci95 | n |");
  lines.push("|---|---|---|---|");
  for (const [name, agg] of Object.entries(result.aggregates.byJudge)) {
    lines.push(
      `| ${name} | ${agg.mean.toFixed(3)} | [${agg.ci95[0].toFixed(3)}, ${agg.ci95[1].toFixed(3)}] | ${agg.n} |`
    );
  }
  return lines.join("\n");
}
function defaultGhExec(args) {
  try {
    const stdout = execSync(`gh ${args.map(quoteArg).join(" ")}`, {
      env: { ...process.env, GH_TOKEN: process.env.GH_AUTO_PR_TOKEN ?? process.env.GH_TOKEN ?? "" },
      stdio: ["ignore", "pipe", "pipe"]
    }).toString("utf8");
    return { stdout, stderr: "", status: 0 };
  } catch (err) {
    const e = err;
    return {
      stdout: e.stdout?.toString("utf8") ?? "",
      stderr: e.stderr?.toString("utf8") ?? "",
      status: e.status ?? 1
    };
  }
}
function quoteArg(arg) {
  if (/^[a-zA-Z0-9_/\-:.@]+$/.test(arg)) return arg;
  return `"${arg.replace(/"/g, '\\"')}"`;
}

// src/campaign/drivers/evolutionary.ts
function evolutionaryDriver(opts) {
  return {
    kind: `evolutionary:${opts.mutator.kind}`,
    async propose({ currentSurface, findings, populationSize, signal }) {
      return opts.mutator.mutate({
        findings: findings.length > 0 ? findings : opts.findings ?? [],
        currentSurface,
        populationSize,
        signal
      });
    }
  };
}

// src/campaign/drivers/gepa.ts
var REFLECTION_SYSTEM = 'You are an expert prompt engineer. Output ONLY a JSON object of shape {"proposals":[{"label":string,"rationale":string,"payload":string}]} where each `payload` is the FULL improved surface text. No prose outside the JSON.';
var COMBINE_SYSTEM = 'You are an expert prompt engineer performing a GEPA "combine complementary lessons" merge. You are given several non-dominated versions of one surface; each is uniquely best on different scenarios. Produce ONE new version that keeps what makes each version strong on its winning scenarios and resolves conflicts in favor of the more general rule. Output ONLY a JSON object of shape {"proposals":[{"label":string,"rationale":string,"payload":string}]} with exactly one proposal whose `payload` is the FULL merged surface text. No prose outside the JSON.';
function gepaDriver(opts) {
  const evidenceK = opts.evidenceK ?? 3;
  const combineParents = opts.combineParents ?? true;
  const combineMaxParents = opts.combineMaxParents ?? 4;
  if (combineParents && combineMaxParents < 1) {
    throw new Error("gepaDriver: combineMaxParents must be >= 1 when combineParents is enabled");
  }
  return {
    kind: "gepa",
    async propose(ctx) {
      const parent = typeof ctx.currentSurface === "string" ? ctx.currentSurface : JSON.stringify(ctx.currentSurface);
      const constraints = opts.constraints;
      const preserveSections = constraints?.preserveSections !== void 0 ? constraints.preserveSections.length === 0 ? extractH2Sections(parent) : constraints.preserveSections : null;
      const maxEdits = constraints?.maxSentenceEdits;
      const out = [];
      const seen = /* @__PURE__ */ new Set();
      const accept = (payload, label, rationale) => {
        const text = typeof payload === "string" ? payload.trim() : "";
        if (!text || text === parent || seen.has(text)) return;
        if (preserveSections && !validatePreservedSections(text, preserveSections)) return;
        if (maxEdits !== void 0 && countSentenceEdits(parent, text) > maxEdits * 2) return;
        seen.add(text);
        out.push({ surface: text, label, rationale });
      };
      const stringParents = (combineParents ? ctx.paretoParents ?? [] : []).filter((p) => typeof p.surface === "string").sort((a, b) => b.composite - a.composite).slice(0, combineMaxParents);
      if (stringParents.length > 1) {
        const combinePrompt = buildCombinePrompt({
          target: opts.target,
          parents: stringParents,
          evidenceK
        });
        const combineResult = await callLlm(
          {
            model: opts.model,
            messages: [
              { role: "system", content: COMBINE_SYSTEM },
              { role: "user", content: combinePrompt }
            ],
            jsonMode: true,
            temperature: opts.temperature ?? 0.7,
            maxTokens: opts.maxTokens ?? 6e3
          },
          opts.llm
        );
        const merged = parseReflectionResponse(combineResult.content, 1)[0];
        if (merged) {
          accept(
            merged.payload,
            merged.label || "pareto-combine",
            merged.rationale || `combined ${stringParents.length} non-dominated parents (gen ${stringParents.map((p) => p.generation).join(",")})`
          );
        }
      }
      const reflectCount = Math.max(0, ctx.populationSize - out.length);
      if (reflectCount > 0) {
        const { top, bottom, target } = buildEvidence(ctx, evidenceK, opts.target);
        const userPrompt = buildReflectionPrompt({
          target,
          parentPayload: parent,
          topTrials: top,
          bottomTrials: bottom,
          childCount: reflectCount,
          mutationPrimitives: opts.mutationPrimitives
        });
        const result = await callLlm(
          {
            model: opts.model,
            messages: [
              { role: "system", content: REFLECTION_SYSTEM },
              { role: "user", content: userPrompt }
            ],
            jsonMode: true,
            temperature: opts.temperature ?? 0.7,
            maxTokens: opts.maxTokens ?? 6e3
          },
          opts.llm
        );
        for (const proposal of parseReflectionResponse(result.content, reflectCount)) {
          accept(proposal.payload, proposal.label, proposal.rationale);
        }
      }
      return out.slice(0, ctx.populationSize);
    }
  };
}
function buildCombinePrompt(args) {
  const lines = [
    `You are merging ${args.parents.length} versions of: ${args.target}.`,
    "",
    "Each version is on the Pareto frontier \u2014 none dominates the others; each",
    "wins on different scenarios. Combine their complementary strengths into",
    "ONE version. Below, each version lists the scenarios it scores highest on.",
    ""
  ];
  args.parents.forEach((p, i) => {
    const tag = String.fromCharCode(65 + i);
    const best = Object.entries(p.objectives).sort((a, b) => b[1] - a[1]).slice(0, args.evidenceK).map(([id, score]) => `${id} (${score.toFixed(2)})`);
    lines.push(
      `### Version ${tag} (mean ${p.composite.toFixed(2)}; strongest on: ${best.join(", ") || "n/a"})`,
      "```",
      p.surface,
      "```",
      ""
    );
  });
  lines.push(
    "Return ONE merged version that would score well on the union of every",
    "version's winning scenarios. Keep each version's specific winning rule;",
    "where two rules conflict, prefer the more general one and note the choice",
    "in your rationale."
  );
  return lines.join("\n");
}
function extractH2Sections(text) {
  const out = [];
  for (const line of text.split("\n")) {
    const match = /^##\s+(.+?)\s*$/.exec(line);
    if (match) out.push(match[1]);
  }
  return out;
}
function countSentenceEdits(baseline, candidate) {
  const norm = (s) => s.split(/(?<=[.!?])\s+|\n/g).map((p) => p.trim()).filter((p) => p.length > 0);
  const a = new Set(norm(baseline));
  const b = new Set(norm(candidate));
  let edits = 0;
  for (const s of a) if (!b.has(s)) edits++;
  for (const s of b) if (!a.has(s)) edits++;
  return edits;
}
function validatePreservedSections(candidate, required) {
  if (required.length === 0) return true;
  const have = new Set(extractH2Sections(candidate));
  for (const section of required) {
    if (!have.has(section)) return false;
  }
  return true;
}
function buildEvidence(ctx, evidenceK, baseTarget) {
  const last = ctx.history.at(-1);
  if (!last || last.candidates.length === 0) {
    return { top: [], bottom: [], target: baseTarget };
  }
  const best = [...last.candidates].sort((a, b) => b.composite - a.composite)[0];
  if (!best) return { top: [], bottom: [], target: baseTarget };
  const byScore = [...best.scenarios].sort((a, b) => b.composite - a.composite);
  const toTrace = (s) => ({
    id: s.scenarioId,
    score: s.composite
  });
  const top = byScore.slice(0, evidenceK).map(toTrace);
  const bottom = byScore.slice(-evidenceK).reverse().map(toTrace);
  const weakest = Object.entries(best.dimensions).sort((a, b) => a[1] - b[1]).slice(0, 3).map(([dim, value]) => `${dim} (${value.toFixed(2)})`);
  const target = weakest.length > 0 ? `${baseTarget} \u2014 weakest dimensions: ${weakest.join(", ")}` : baseTarget;
  return { top, bottom, target };
}

// src/campaign/gates/compose.ts
function composeGate(...gates) {
  if (gates.length === 0) {
    throw new Error("composeGate requires at least one gate");
  }
  return {
    name: `composed(${gates.map((g) => g.name).join(",")})`,
    async decide(ctx) {
      const results = [];
      for (const gate of gates) {
        const res = await gate.decide(ctx);
        results.push({ gate, res });
      }
      const decisions = results.map((r) => r.res.decision);
      const overall = decisions.every((d) => d === "ship") ? "ship" : decisions.includes("arch_ceiling") ? "arch_ceiling" : decisions.includes("model_ceiling") ? "model_ceiling" : decisions.includes("hold") ? "hold" : "need_more_work";
      const contributing = results.flatMap(
        (r) => r.res.contributingGates.length > 0 ? r.res.contributingGates : [{ name: r.gate.name, passed: r.res.decision === "ship", detail: r.res }]
      );
      const reasons = results.flatMap(
        (r) => r.res.reasons.map((reason) => `[${r.gate.name}] ${reason}`)
      );
      return {
        decision: overall,
        reasons,
        contributingGates: contributing,
        delta: results[0]?.res.delta
      };
    }
  };
}

// src/campaign/gates/default-production-gate.ts
function defaultProductionGate(options) {
  const deltaThreshold = options.deltaThreshold ?? 0.5;
  const blockOnGaming = options.blockOnRewardHackingGaming ?? true;
  return {
    name: "defaultProductionGate",
    async decide(ctx) {
      const reasons = [];
      const contributing = [];
      const baselineComposite = meanComposite(
        ctx.baselineArtifacts,
        ctx.baselineJudgeScores ?? ctx.judgeScores,
        options.holdoutScenarios
      );
      const candidateComposite = meanComposite(
        ctx.candidateArtifacts,
        ctx.judgeScores,
        options.holdoutScenarios
      );
      const delta = candidateComposite - baselineComposite;
      const heldoutPass = delta >= deltaThreshold;
      contributing.push({
        name: "heldout-delta",
        passed: heldoutPass,
        detail: { baselineComposite, candidateComposite, delta, deltaThreshold }
      });
      if (!heldoutPass) {
        reasons.push(`heldout delta ${delta.toFixed(3)} < threshold ${deltaThreshold}`);
      }
      const budgetPass = options.budgetUsd === void 0 || ctx.cost.candidate + ctx.cost.baseline <= options.budgetUsd;
      contributing.push({
        name: "budget",
        passed: budgetPass,
        detail: {
          candidateUsd: ctx.cost.candidate,
          baselineUsd: ctx.cost.baseline,
          budgetUsd: options.budgetUsd
        }
      });
      if (!budgetPass) {
        reasons.push(
          `spend ${(ctx.cost.candidate + ctx.cost.baseline).toFixed(2)} > budget ${options.budgetUsd}`
        );
      }
      const redTeamFindings = options.redTeamBattery ? probeRedTeam(ctx.candidateArtifacts, options.redTeamBattery) : { passed: true, findings: [] };
      contributing.push({
        name: "red-team",
        passed: redTeamFindings.passed,
        detail: {
          failures: redTeamFindings.findings.length,
          sample: redTeamFindings.findings.slice(0, 3)
        }
      });
      if (!redTeamFindings.passed) {
        reasons.push(`red-team probe failed (${redTeamFindings.findings.length} findings)`);
      }
      let rewardHackingReport = null;
      if (options.recentRuns && options.recentRuns.length >= 10) {
        rewardHackingReport = detectRewardHacking({ runs: options.recentRuns });
      }
      const gamingThreshold = 0.6;
      const gamingFindings = (rewardHackingReport?.findings ?? []).filter(
        (f) => f.severity >= gamingThreshold
      );
      const rewardHackingPass = !rewardHackingReport || !blockOnGaming || gamingFindings.length === 0 && rewardHackingReport.verdict !== "gaming";
      contributing.push({
        name: "reward-hacking",
        passed: rewardHackingPass,
        detail: { report: rewardHackingReport, gamingFindingCount: gamingFindings.length }
      });
      if (!rewardHackingPass) {
        reasons.push(
          `reward-hacking detector flagged ${gamingFindings.length} gaming-severity findings (verdict=${rewardHackingReport.verdict})`
        );
      }
      let canaryReport = null;
      if (options.recentRuns && options.recentRuns.length >= 10) {
        canaryReport = runCanaries(options.recentRuns, {});
      }
      const errorAlerts = (canaryReport?.alerts ?? []).filter((a) => a.severity === "error");
      const canaryPass = errorAlerts.length === 0;
      contributing.push({
        name: "canary",
        passed: canaryPass,
        detail: { totalAlerts: canaryReport?.alerts.length ?? 0, errorAlerts: errorAlerts.length }
      });
      if (!canaryPass) {
        reasons.push(`canary error alerts: ${errorAlerts.length}`);
      }
      const allPassed = contributing.every((c) => c.passed);
      const decision = allPassed ? "ship" : "hold";
      return {
        decision,
        reasons: reasons.length > 0 ? reasons : ["all gates passed"],
        contributingGates: contributing,
        delta
      };
    }
  };
}
function meanComposite(artifacts, judgeScoresByCell, scenarios) {
  if (!artifacts || artifacts.size === 0) return 0;
  const scenarioIds = new Set(scenarios.map((s) => s.id));
  const composites = [];
  for (const [cellId, scores] of judgeScoresByCell) {
    const scenarioId = cellId.split(":")[0] ?? "";
    if (!scenarioIds.has(scenarioId)) continue;
    const cellComposites = Object.values(scores).map((s) => s.composite);
    if (cellComposites.length === 0) continue;
    composites.push(cellComposites.reduce((a, b) => a + b, 0) / cellComposites.length);
  }
  if (composites.length === 0) return 0;
  return composites.reduce((a, b) => a + b, 0) / composites.length;
}
function probeRedTeam(artifacts, battery) {
  const findings = [];
  for (const [_cellId, artifact] of artifacts) {
    const text = extractText(artifact);
    if (text === void 0) continue;
    for (const rtCase of battery) {
      const finding = scoreRedTeamOutput(text, [], rtCase);
      if (!finding.passed) {
        findings.push({ scenarioId: rtCase.id, reason: finding.reason ?? "red-team probe failed" });
      }
    }
  }
  return { passed: findings.length === 0, findings };
}
function extractText(artifact) {
  if (typeof artifact === "string") return artifact;
  if (artifact && typeof artifact === "object") {
    const rec = artifact;
    if (typeof rec.text === "string") return rec.text;
    if (typeof rec.output === "string") return rec.output;
    if (typeof rec.content === "string") return rec.content;
  }
  return void 0;
}

// src/campaign/gates/heldout-gate.ts
function heldOutGate(options) {
  const deltaThreshold = options.deltaThreshold ?? 0.5;
  return {
    name: "heldOutGate",
    async decide(ctx) {
      const scenarioIds = new Set(options.scenarios.map((s) => s.id));
      const baseline = meanForScenarios(ctx.baselineJudgeScores ?? ctx.judgeScores, scenarioIds);
      const candidate = meanForScenarios(ctx.judgeScores, scenarioIds);
      const delta = candidate - baseline;
      const passed = delta >= deltaThreshold;
      return {
        decision: passed ? "ship" : "hold",
        reasons: passed ? [`held-out delta ${delta.toFixed(3)} \u2265 ${deltaThreshold}`] : [`held-out delta ${delta.toFixed(3)} < ${deltaThreshold}`],
        contributingGates: [
          { name: "heldOutGate", passed, detail: { baseline, candidate, delta, deltaThreshold } }
        ],
        delta
      };
    }
  };
}
function meanForScenarios(judgeScoresByCell, scenarioIds) {
  const composites = [];
  for (const [cellId, scores] of judgeScoresByCell) {
    const scenarioId = cellId.split(":")[0] ?? "";
    if (!scenarioIds.has(scenarioId)) continue;
    const vals = Object.values(scores).map((s) => s.composite);
    if (vals.length > 0) composites.push(vals.reduce((a, b) => a + b, 0) / vals.length);
  }
  return composites.length === 0 ? 0 : composites.reduce((a, b) => a + b, 0) / composites.length;
}

// src/campaign/types.ts
function isProposedCandidate(value) {
  return typeof value === "object" && value !== null && "surface" in value && "label" in value && "rationale" in value;
}
var LABEL_TRUST_RANK = {
  unverified: 0,
  "verified-signal": 1,
  "human-rated": 2
};
function labelTrustRank(trust) {
  return LABEL_TRUST_RANK[trust ?? "unverified"];
}

// src/campaign/score-utils.ts
function campaignMeanComposite(campaign) {
  const composites = [];
  for (const cell of campaign.cells) {
    const cellComposites = Object.values(cell.judgeScores).map((s) => s.composite);
    if (cellComposites.length > 0) {
      composites.push(cellComposites.reduce((a, b) => a + b, 0) / cellComposites.length);
    }
  }
  return composites.length === 0 ? 0 : composites.reduce((a, b) => a + b, 0) / composites.length;
}
function campaignBreakdown(campaign) {
  const dimSums = {};
  const dimCounts = {};
  const byScenario = /* @__PURE__ */ new Map();
  for (const cell of campaign.cells) {
    const judgeScores = Object.values(cell.judgeScores);
    if (judgeScores.length === 0) continue;
    const cellComposite = judgeScores.reduce((a, s) => a + s.composite, 0) / judgeScores.length;
    const arr = byScenario.get(cell.scenarioId) ?? [];
    arr.push(cellComposite);
    byScenario.set(cell.scenarioId, arr);
    for (const score of judgeScores) {
      for (const [key, value] of Object.entries(score.dimensions)) {
        dimSums[key] = (dimSums[key] ?? 0) + value;
        dimCounts[key] = (dimCounts[key] ?? 0) + 1;
      }
    }
  }
  const dimensions = {};
  for (const key of Object.keys(dimSums)) {
    const count = dimCounts[key] ?? 0;
    dimensions[key] = count > 0 ? (dimSums[key] ?? 0) / count : 0;
  }
  const scenarios = [...byScenario.entries()].map(([scenarioId, comps]) => ({
    scenarioId,
    composite: comps.reduce((a, b) => a + b, 0) / comps.length
  }));
  return { dimensions, scenarios };
}

// src/campaign/presets/run-optimization.ts
import { createHash } from "crypto";
async function runOptimization(opts) {
  const promoteTopK = opts.promoteTopK ?? 2;
  const baselineCampaign = await runCampaign({
    ...opts,
    dispatch: (scenario, ctx) => opts.dispatchWithSurface(opts.baselineSurface, scenario, ctx),
    runDir: `${opts.runDir}/baseline`
  });
  const generations = [];
  const history = [];
  let currentSurfaces = [opts.baselineSurface];
  let winnerSurface = opts.baselineSurface;
  let winnerSurfaceHash = surfaceHash(opts.baselineSurface);
  let winnerComposite = campaignMeanComposite(baselineCampaign);
  let winnerLabel;
  let winnerRationale;
  const scored = [
    toParetoParent(opts.baselineSurface, winnerSurfaceHash, baselineCampaign, -1)
  ];
  for (let gen = 0; gen < opts.maxGenerations; gen++) {
    if (opts.driver.decide?.({ history }).stop) break;
    const paretoParents = computeParetoFrontier(scored);
    const proposed = await opts.driver.propose({
      currentSurface: currentSurfaces[0] ?? opts.baselineSurface,
      history,
      findings: [],
      populationSize: opts.populationSize,
      generation: gen,
      signal: new AbortController().signal,
      report: opts.report,
      dataset: opts.labeledStore && opts.labeledStore !== "off" ? opts.labeledStore : void 0,
      maxImprovementShots: opts.maxImprovementShots,
      paretoParents
    });
    const candidates = proposed.map(
      (p) => isProposedCandidate(p) ? p : { surface: p, label: "", rationale: "" }
    );
    const surfaceResults = [];
    for (let i = 0; i < candidates.length; i++) {
      const { surface, label, rationale } = candidates[i];
      const hash = surfaceHash(surface);
      const campaign = await runCampaign({
        ...opts,
        dispatch: (scenario, ctx) => opts.dispatchWithSurface(surface, scenario, ctx),
        runDir: `${opts.runDir}/gen-${gen}/candidate-${i}`
      });
      const composite = campaignMeanComposite(campaign);
      surfaceResults.push({ surfaceHash: hash, surface, label, rationale, campaign, composite });
      scored.push(
        toParetoParent(surface, hash, campaign, gen, label || void 0, rationale || void 0)
      );
    }
    surfaceResults.sort((a, b) => b.composite - a.composite);
    const promoted = surfaceResults.slice(0, promoteTopK);
    currentSurfaces = promoted.map((p) => p.surface);
    const top = surfaceResults[0];
    if (top && top.composite > winnerComposite) {
      winnerSurface = top.surface;
      winnerSurfaceHash = top.surfaceHash;
      winnerComposite = top.composite;
      winnerLabel = top.label || void 0;
      winnerRationale = top.rationale || void 0;
    }
    const record = {
      generationIndex: gen,
      candidates: surfaceResults.map((s) => {
        const breakdown = campaignBreakdown(s.campaign);
        const candidate = {
          surfaceHash: s.surfaceHash,
          composite: s.composite,
          ci95: [s.composite, s.composite],
          dimensions: breakdown.dimensions,
          scenarios: breakdown.scenarios
        };
        if (s.label) candidate.label = s.label;
        if (s.rationale) candidate.rationale = s.rationale;
        return candidate;
      }),
      promoted: promoted.map((p) => p.surfaceHash)
    };
    history.push(record);
    generations.push({
      record,
      surfaces: surfaceResults.map((s) => ({
        surfaceHash: s.surfaceHash,
        surface: s.surface,
        campaign: s.campaign
      }))
    });
  }
  return {
    generations,
    winnerSurface,
    winnerSurfaceHash,
    winnerLabel,
    winnerRationale,
    baselineCampaign,
    paretoFrontier: computeParetoFrontier(scored)
  };
}
function toParetoParent(surface, hash, campaign, generation, label, rationale) {
  const objectives = {};
  for (const { scenarioId, composite } of campaignBreakdown(campaign).scenarios) {
    objectives[scenarioId] = composite;
  }
  const parent = {
    surface,
    surfaceHash: hash,
    objectives,
    composite: campaignMeanComposite(campaign),
    generation
  };
  if (label) parent.label = label;
  if (rationale) parent.rationale = rationale;
  return parent;
}
function computeParetoFrontier(scored) {
  if (scored.length <= 1) return [...scored];
  const ids = /* @__PURE__ */ new Set();
  for (const p of scored) for (const id of Object.keys(p.objectives)) ids.add(id);
  if (ids.size === 0) return [...scored];
  const floor = {};
  for (const id of ids) {
    let min = Number.POSITIVE_INFINITY;
    for (const p of scored) {
      const v = p.objectives[id];
      if (typeof v === "number" && Number.isFinite(v) && v < min) min = v;
    }
    floor[id] = Number.isFinite(min) ? min : 0;
  }
  const objectives = [...ids].map((id) => ({
    name: id,
    direction: "maximize",
    value: (p) => {
      const v = p.objectives[id];
      return typeof v === "number" && Number.isFinite(v) ? v : floor[id] ?? 0;
    }
  }));
  return paretoFrontier(scored, objectives).frontier;
}
function surfaceHash(surface) {
  const material = typeof surface === "string" ? surface : JSON.stringify({
    kind: surface.kind,
    worktreeRef: surface.worktreeRef,
    baseRef: surface.baseRef ?? null
  });
  return createHash("sha256").update(material).digest("hex").slice(0, 16);
}

// src/campaign/presets/run-improvement-loop.ts
async function runImprovementLoop(opts) {
  if (opts.autoOnPromote === "config") {
    throw new Error(
      "runImprovementLoop: autoOnPromote='config' is deferred to Pass B (requires shadow deploy + rollback + ensemble judges). Use 'pr' or 'none' in v0.40."
    );
  }
  if (opts.tracing === "off" && opts.driver) {
    throw new Error(
      "runImprovementLoop: tracing='off' is forbidden when a driver is wired. The improvement loop without traces is unattributable; candidate surfaces cannot be cited back to spans and the optimization dataset goes unfed."
    );
  }
  if (opts.autoOnPromote === "pr" && (!opts.ghOwner || !opts.ghRepo)) {
    throw new Error("runImprovementLoop: autoOnPromote='pr' requires ghOwner + ghRepo.");
  }
  const optimization = await runOptimization(opts);
  const { runCampaign: runCampaign2 } = await import("./run-campaign-5J3ED2UJ.js");
  const baselineOnHoldout = await runCampaign2({
    ...opts,
    scenarios: opts.holdoutScenarios,
    dispatch: (scenario, ctx) => opts.dispatchWithSurface(opts.baselineSurface, scenario, ctx),
    runDir: `${opts.runDir}/holdout-baseline`
  });
  const winnerOnHoldout = await runCampaign2({
    ...opts,
    scenarios: opts.holdoutScenarios,
    dispatch: (scenario, ctx) => opts.dispatchWithSurface(optimization.winnerSurface, scenario, ctx),
    runDir: `${opts.runDir}/holdout-winner`
  });
  const candidateArtifacts = /* @__PURE__ */ new Map();
  const baselineArtifacts = /* @__PURE__ */ new Map();
  const judgeScores = /* @__PURE__ */ new Map();
  const baselineJudgeScores = /* @__PURE__ */ new Map();
  for (const cell of winnerOnHoldout.cells) {
    candidateArtifacts.set(cell.cellId, cell.artifact);
    judgeScores.set(cell.cellId, cell.judgeScores);
  }
  for (const cell of baselineOnHoldout.cells) {
    baselineArtifacts.set(cell.cellId, cell.artifact);
    baselineJudgeScores.set(cell.cellId, cell.judgeScores);
  }
  const gateResult = await opts.gate.decide({
    candidateArtifacts,
    baselineArtifacts,
    judgeScores,
    baselineJudgeScores,
    scenarios: opts.holdoutScenarios,
    cost: {
      candidate: winnerOnHoldout.aggregates.totalCostUsd,
      baseline: baselineOnHoldout.aggregates.totalCostUsd
    },
    signal: new AbortController().signal
  });
  const render = opts.renderPromotedDiff ?? defaultRenderDiff;
  const promotedDiff = optimization.winnerSurfaceHash === surfaceHash(opts.baselineSurface) ? "" : render(optimization.winnerSurface, opts.baselineSurface);
  let prResult;
  if (opts.autoOnPromote === "pr" && gateResult.decision === "ship") {
    prResult = openAutoPr({
      result: winnerOnHoldout,
      gate: gateResult,
      promotedDiff,
      ghOwner: opts.ghOwner,
      ghRepo: opts.ghRepo
    });
  }
  return {
    ...optimization,
    baselineOnHoldout,
    winnerOnHoldout,
    gateResult,
    promotedDiff,
    prResult
  };
}
function defaultRenderDiff(winnerSurface, baselineSurface) {
  if (typeof winnerSurface !== "string" || typeof baselineSurface !== "string") {
    const fmt = (s) => typeof s === "string" ? "(prompt surface)" : `worktree=${s.worktreeRef}${s.baseRef ? ` base=${s.baseRef}` : ""}${s.summary ? `
${s.summary}` : ""}`;
    return `--- baseline
${fmt(baselineSurface)}
+++ winner
${fmt(winnerSurface)}`;
  }
  const lines = [];
  lines.push("--- baseline");
  lines.push("+++ winner");
  for (const l of baselineSurface.split("\n")) lines.push(`- ${l}`);
  for (const l of winnerSurface.split("\n")) lines.push(`+ ${l}`);
  return lines.join("\n");
}

// src/campaign/presets/run-eval.ts
async function runEval(opts) {
  return runCampaign(opts);
}

// src/campaign/provenance.ts
import { createHash as createHash2 } from "crypto";
import { join as join2 } from "path";
function surfaceContentHash(surface) {
  const material = typeof surface === "string" ? surface : JSON.stringify({
    kind: surface.kind,
    worktreeRef: surface.worktreeRef,
    baseRef: surface.baseRef ?? null
  });
  return `sha256:${createHash2("sha256").update(material).digest("hex")}`;
}
function meanHoldoutComposite(campaign) {
  const xs = [];
  for (const cell of campaign.cells) {
    if (cell.error) continue;
    const cs = Object.values(cell.judgeScores).map((s) => s.composite);
    if (cs.length) xs.push(cs.reduce((a, b) => a + b, 0) / cs.length);
  }
  return xs.length ? xs.reduce((a, b) => a + b, 0) / xs.length : 0;
}
function buildLoopProvenanceRecord(args) {
  const integrity = summarizeBackendIntegrity(args.workerRecords);
  const models = [...new Set(args.workerRecords.map((r) => r.model))].sort();
  const candidates = [];
  for (const gen of args.generations) {
    const promotedSet = new Set(gen.promoted);
    const surfaceByHash = new Map(gen.surfaces.map((s) => [s.surfaceHash, s.surface]));
    for (const c of gen.candidates) {
      const surface = surfaceByHash.get(c.surfaceHash);
      const entry = {
        generation: gen.generationIndex,
        surfaceHash: c.surfaceHash,
        contentHash: surface !== void 0 ? surfaceContentHash(surface) : `sha256:${c.surfaceHash}`,
        composite: c.composite,
        promoted: promotedSet.has(c.surfaceHash)
      };
      if (c.label) entry.label = c.label;
      if (c.rationale) entry.rationale = c.rationale;
      candidates.push(entry);
    }
  }
  const baselineHoldoutComposite = meanHoldoutComposite(args.baselineOnHoldout);
  const winnerHoldoutComposite = meanHoldoutComposite(args.winnerOnHoldout);
  const record = {
    schema: "tangle.loop-provenance.v1",
    runId: args.runId,
    runDir: args.runDir,
    timestamp: args.timestamp,
    baselineContentHash: surfaceContentHash(args.baselineSurface),
    winnerContentHash: surfaceContentHash(args.winnerSurface),
    diff: args.diff,
    candidates,
    gate: {
      decision: args.gate.decision,
      reasons: args.gate.reasons,
      delta: args.gate.delta,
      contributingGates: args.gate.contributingGates.map((g) => ({
        name: g.name,
        passed: g.passed
      }))
    },
    baselineHoldoutComposite,
    winnerHoldoutComposite,
    heldOutLift: winnerHoldoutComposite - baselineHoldoutComposite,
    backend: {
      verdict: integrity.verdict,
      workerCallCount: integrity.totalRecords,
      models,
      totalInputTokens: integrity.totalInputTokens,
      totalOutputTokens: integrity.totalOutputTokens,
      totalCostUsd: integrity.totalCostUsd
    },
    totalCostUsd: args.totalCostUsd,
    totalDurationMs: args.totalDurationMs
  };
  if (args.winnerLabel) record.winnerLabel = args.winnerLabel;
  if (args.winnerRationale) record.winnerRationale = args.winnerRationale;
  return record;
}
var DECISION_OK = ["ship"];
function hashId(parts) {
  return createHash2("sha256").update(parts.join(":")).digest("hex");
}
function gateStatus(decision) {
  return DECISION_OK.includes(decision) ? { code: "OK" } : { code: "ERROR", message: `gate decision: ${decision}` };
}
function loopProvenanceSpans(record, opts = {}) {
  const traceId = hashId(["trace", record.runId]).slice(0, 32);
  const baseNano = (opts.baseTimeMs ?? (Date.parse(record.timestamp) || Date.now())) * 1e6;
  const endNano = baseNano + Math.max(1, record.totalDurationMs) * 1e6;
  const spans = [];
  const rootSpanId = hashId(["root", record.runId]).slice(0, 16);
  spans.push({
    traceId,
    spanId: rootSpanId,
    name: "improvement-loop",
    startTimeUnixNano: baseNano,
    endTimeUnixNano: endNano,
    attributes: {
      "tangle.runId": record.runId,
      "tangle.runDir": record.runDir,
      "tangle.baselineContentHash": record.baselineContentHash,
      "tangle.winnerContentHash": record.winnerContentHash,
      "tangle.heldOutLift": record.heldOutLift,
      "tangle.gateDecision": record.gate.decision,
      "tangle.backendVerdict": record.backend.verdict,
      "tangle.workerCallCount": record.backend.workerCallCount,
      "tangle.totalCostUsd": record.totalCostUsd
    },
    status: gateStatus(record.gate.decision),
    "tangle.runId": record.runId
  });
  const byGen = /* @__PURE__ */ new Map();
  for (const c of record.candidates) {
    const arr = byGen.get(c.generation) ?? [];
    arr.push(c);
    byGen.set(c.generation, arr);
  }
  for (const [generation, cands] of [...byGen.entries()].sort((a, b) => a[0] - b[0])) {
    const genSpanId = hashId(["gen", record.runId, String(generation)]).slice(0, 16);
    const bestComposite = cands.reduce((m, c) => Math.max(m, c.composite), 0);
    spans.push({
      traceId,
      spanId: genSpanId,
      parentSpanId: rootSpanId,
      name: `generation-${generation}`,
      startTimeUnixNano: baseNano,
      endTimeUnixNano: endNano,
      attributes: {
        "tangle.runId": record.runId,
        "tangle.generation": generation,
        "tangle.populationSize": cands.length,
        "tangle.bestComposite": bestComposite
      },
      "tangle.runId": record.runId,
      "tangle.generation": generation
    });
    for (let i = 0; i < cands.length; i++) {
      const c = cands[i];
      const candSpanId = hashId(["cand", record.runId, String(generation), c.surfaceHash]).slice(
        0,
        16
      );
      const attributes = {
        "tangle.runId": record.runId,
        "tangle.generation": generation,
        "tangle.surfaceHash": c.surfaceHash,
        "tangle.contentHash": c.contentHash,
        "tangle.composite": c.composite,
        "tangle.promoted": c.promoted
      };
      if (c.label) attributes["tangle.candidateLabel"] = c.label;
      if (c.rationale) attributes["tangle.candidateRationale"] = c.rationale;
      spans.push({
        traceId,
        spanId: candSpanId,
        parentSpanId: genSpanId,
        name: `candidate-${c.surfaceHash}`,
        startTimeUnixNano: baseNano,
        endTimeUnixNano: endNano,
        attributes,
        "tangle.runId": record.runId,
        "tangle.generation": generation
      });
    }
  }
  const gateSpanId = hashId(["gate", record.runId]).slice(0, 16);
  spans.push({
    traceId,
    spanId: gateSpanId,
    parentSpanId: rootSpanId,
    name: "gate-decision",
    startTimeUnixNano: endNano,
    endTimeUnixNano: endNano,
    attributes: {
      "tangle.runId": record.runId,
      "tangle.gateDecision": record.gate.decision,
      "tangle.gateDelta": record.gate.delta ?? record.heldOutLift,
      "tangle.gateReasons": JSON.stringify(record.gate.reasons),
      "tangle.heldOutLift": record.heldOutLift,
      "tangle.baselineHoldoutComposite": record.baselineHoldoutComposite,
      "tangle.winnerHoldoutComposite": record.winnerHoldoutComposite
    },
    status: gateStatus(record.gate.decision),
    "tangle.runId": record.runId
  });
  return spans;
}
function provenanceRecordPath(runDir) {
  return join2(runDir, "loop-provenance.json");
}
function provenanceSpansPath(runDir) {
  return join2(runDir, "loop-provenance-spans.jsonl");
}
function snapshotFromHoldout(index, surfaceHash2, surface, campaign) {
  const cells = campaign.cells.map((cell) => {
    const judgeScores = Object.values(cell.judgeScores);
    const composite = judgeScores.length === 0 ? 0 : judgeScores.reduce((s, j) => s + j.composite, 0) / judgeScores.length;
    const score = {
      scenarioId: cell.scenarioId,
      rep: cell.rep,
      compositeMean: composite,
      dimensions: Object.fromEntries(
        Object.entries(cell.judgeScores).map(([name, s]) => [name, s.dimensions])
      )
    };
    if (cell.error) score.errorMessage = cell.error;
    return score;
  });
  const compositeMean = cells.length === 0 ? 0 : cells.reduce((s, c) => s + c.compositeMean, 0) / cells.length;
  return {
    index,
    surfaceHash: surfaceHash2,
    surface,
    cells,
    compositeMean,
    costUsd: campaign.aggregates.totalCostUsd,
    durationMs: campaign.durationMs
  };
}
function buildEvalRunEvent(args, record) {
  return {
    runId: args.runId,
    runDir: args.runDir,
    timestamp: args.timestamp,
    status: "finished",
    labels: {},
    baseline: snapshotFromHoldout(
      0,
      record.baselineContentHash,
      args.baselineSurface,
      args.baselineOnHoldout
    ),
    generations: [
      snapshotFromHoldout(1, record.winnerContentHash, args.winnerSurface, args.winnerOnHoldout)
    ],
    gateDecision: args.gate.decision,
    holdoutLift: record.heldOutLift,
    totalCostUsd: args.totalCostUsd,
    totalDurationMs: args.totalDurationMs
  };
}
async function emitLoopProvenance(args) {
  const record = buildLoopProvenanceRecord(args);
  const spans = loopProvenanceSpans(record);
  args.storage.ensureDir(args.runDir);
  const recordPath = provenanceRecordPath(args.runDir);
  const spansPath = provenanceSpansPath(args.runDir);
  args.storage.write(recordPath, JSON.stringify(record, null, 2));
  args.storage.write(spansPath, spans.map((s) => JSON.stringify(s)).join("\n"));
  if (args.hostedClient) {
    try {
      await args.hostedClient.ingestEvalRun(buildEvalRunEvent(args, record));
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      console.warn(`[agent-eval] hosted eval-run ingest failed (continuing): ${msg}`);
    }
    try {
      await args.hostedClient.ingestTraces(spans);
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      console.warn(`[agent-eval] provenance span ingest failed (continuing): ${msg}`);
    }
  }
  return { record, spans, recordPath, spansPath };
}

export {
  openAutoPr,
  evolutionaryDriver,
  gepaDriver,
  extractH2Sections,
  countSentenceEdits,
  composeGate,
  defaultProductionGate,
  heldOutGate,
  isProposedCandidate,
  labelTrustRank,
  campaignMeanComposite,
  campaignBreakdown,
  runOptimization,
  surfaceHash,
  runImprovementLoop,
  defaultRenderDiff,
  runEval,
  surfaceContentHash,
  buildLoopProvenanceRecord,
  loopProvenanceSpans,
  provenanceRecordPath,
  provenanceSpansPath,
  emitLoopProvenance
};
//# sourceMappingURL=chunk-CZRKD2X2.js.map