import {
  fromClaudeCodeSession,
  fromCodexSession,
  fromKimiCodeSession,
  fromOpenCodeSession,
  fromPiSession,
  fromPigraphSession,
  observeCodeAgentSession,
  parseCodeAgentJsonl
} from "../chunk-7VYEGHWF.js";
import {
  createHostedClient
} from "../chunk-ZZUXHH3R.js";
import {
  analyzeRuns,
  summarizeExecution
} from "../chunk-BD2OK6ZW.js";
import {
  REFERENCE_EQUIVALENCE_INPUT_LIMITS,
  REFERENCE_EQUIVALENCE_JUDGE_VERSION,
  buildEvidenceVector,
  composeGate,
  createReferenceEquivalenceJudge,
  defaultProductionGate,
  emitLoopProvenance,
  evolutionaryProposer,
  gepaProposer,
  heldOutGate,
  heldoutSignificance,
  loopProvenanceArgsFromResult,
  paretoPolicy,
  paretoSignificanceGate,
  powerPreflight,
  runEval,
  runImprovementLoop,
  runReferenceEquivalenceJudge,
  surfaceContentHash,
  surfaceHash
} from "../chunk-DHJJACEX.js";
import {
  campaignSplitDigest,
  createRunCostLedger,
  fsCampaignStorage,
  inMemoryCampaignStorage,
  resolveRunDir,
  runCampaign
} from "../chunk-N3QPYIVG.js";
import {
  buildDefaultAnalystRegistry,
  createChatClient
} from "../chunk-SFXDMFGV.js";
import "../chunk-HHWE3POT.js";
import "../chunk-EAWKAVID.js";
import {
  FileSystemOutcomeStore,
  InMemoryOutcomeStore
} from "../chunk-3RF76KTD.js";
import "../chunk-ARU2PZFM.js";
import "../chunk-NJC7U437.js";
import "../chunk-DPZAEKA6.js";
import {
  pairedBootstrap
} from "../chunk-PJQFMIOX.js";
import "../chunk-ZMXDQ4K7.js";
import "../chunk-VI2UW6B6.js";
import {
  recordAggregateMeasurements,
  summarizeExecutionMeasurements
} from "../chunk-H5UD2323.js";
import "../chunk-PXE2VKMX.js";
import "../chunk-ZET2UAYW.js";
import "../chunk-GGE4NNQT.js";
import "../chunk-PXD6ZFNY.js";
import "../chunk-PC4UYEBM.js";
import {
  parseRunRecordSafe
} from "../chunk-S3UZOQ5Y.js";
import "../chunk-MA6HLL3S.js";
import "../chunk-XJYR7XFV.js";
import "../chunk-VSMTAMNK.js";
import "../chunk-ONWEPEDO.js";
import {
  LLM_MODEL_ATTR_KEYS
} from "../chunk-K4DBDHLK.js";
import "../chunk-PZ5AY32C.js";

// src/contract/self-improve.ts
import { createHash } from "crypto";
var SelfImproveRunError = class extends Error {
  cost;
  receipts;
  constructor(cause, ledger) {
    const original = cause instanceof Error ? cause : new Error(String(cause));
    super(original.message, { cause: original });
    this.name = "SelfImproveRunError";
    this.cost = ledger.summary();
    this.receipts = ledger.list();
  }
};
function splitTrainHoldout(scenarios, fraction) {
  function hash(s) {
    let h = 2166136261 >>> 0;
    for (let i = 0; i < s.length; i++) {
      h ^= s.charCodeAt(i);
      h = Math.imul(h, 16777619) >>> 0;
    }
    return h;
  }
  const sorted = [...scenarios].sort((a, b) => hash(a.id) - hash(b.id));
  const nHoldout = Math.max(1, Math.min(sorted.length - 1, Math.round(sorted.length * fraction)));
  return {
    holdout: sorted.slice(0, nHoldout),
    train: sorted.slice(nHoldout)
  };
}
function meanComposite(byScenario) {
  const perScenario = {};
  const values = [];
  for (const [id, agg] of Object.entries(byScenario)) {
    perScenario[id] = agg.meanComposite;
    values.push(agg.meanComposite);
  }
  return {
    compositeMean: values.length === 0 ? 0 : values.reduce((s, v) => s + v, 0) / values.length,
    perScenario
  };
}
async function selfImprove(opts) {
  const startedAt = Date.now();
  const requestedRunDir = opts.runDir ?? `mem://selfImprove-${startedAt}`;
  const runDir = resolveRunDir(requestedRunDir);
  const storage = opts.storage ?? (runDir.startsWith("mem://") ? inMemoryCampaignStorage() : fsCampaignStorage());
  const costLedger = createRunCostLedger({
    storage,
    runDir,
    costCeilingUsd: opts.budget?.dollars
  });
  try {
    return await runSelfImprove(opts, costLedger, startedAt, runDir, storage);
  } catch (error) {
    throw new SelfImproveRunError(error, costLedger);
  }
}
async function runSelfImprove(opts, costLedger, startedAt, runDir, storage) {
  const budget = opts.budget ?? {};
  const generations = budget.generations ?? 3;
  const populationSize = budget.populationSize ?? 2;
  const maxConcurrency = budget.maxConcurrency ?? 2;
  const holdoutFraction = budget.holdoutFraction ?? 0.25;
  const expectUsage = opts.expectUsage ?? "assert";
  const explicitHoldout = budget.holdoutScenarios;
  const { train, holdout } = explicitHoldout ? {
    train: opts.scenarios.filter((s) => !explicitHoldout.some((h) => h.id === s.id)),
    holdout: explicitHoldout
  } : splitTrainHoldout(opts.scenarios, holdoutFraction);
  if (train.length === 0) {
    throw new Error(
      "selfImprove: train split is empty. Reduce holdoutFraction or pass more scenarios."
    );
  }
  if (holdout.length === 0) {
    throw new Error("selfImprove: holdout split is empty. Pass more scenarios.");
  }
  const proposer = opts.proposer ?? gepaProposer({
    llm: {
      baseUrl: opts.llm?.baseUrl ?? "https://router.tangle.tools/v1",
      apiKey: opts.llm?.apiKey ?? process.env.OPENAI_API_KEY ?? ""
    },
    model: opts.llm?.model ?? "anthropic/claude-sonnet-4.6",
    target: opts.proposerTarget ?? "agent surface (system prompt or config) being optimized by selfImprove",
    // Pass-through: when unset, gepaProposer falls back to its own
    // domain-neutral engineering primitives.
    mutationPrimitives: opts.mutationPrimitives
  });
  const gate = opts.gate ?? defaultProductionGate({
    holdoutScenarios: holdout,
    deltaThreshold: 0.05
  });
  if (opts.onProgress) {
    opts.onProgress({ kind: "baseline.started", scenarios: opts.scenarios.length });
  }
  const result = await runImprovementLoop({
    scenarios: train,
    baselineSurface: opts.baselineSurface,
    dispatchWithSurface: opts.agent,
    proposer,
    judges: [opts.judge],
    populationSize,
    maxGenerations: generations,
    promoteTopK: budget.promoteTopK,
    reps: budget.reps,
    holdoutScenarios: holdout,
    gate,
    neutralize: opts.neutralize,
    autoOnPromote: opts.autoOnPromote ?? "none",
    ghOwner: opts.ghOwner,
    ghRepo: opts.ghRepo,
    storage,
    runDir,
    maxConcurrency,
    cellPlacement: opts.cellPlacement,
    dispatchTimeoutMs: opts.dispatchTimeoutMs,
    costLedger,
    expectUsage,
    labeledStore: opts.labeledStore,
    captureSource: opts.captureSource,
    analyzeGeneration: opts.analyzeGeneration,
    findings: opts.findings
  });
  const baseline = meanComposite(result.baselineOnHoldout.aggregates.byScenario);
  const winnerStats = meanComposite(result.winnerOnHoldout.aggregates.byScenario);
  let power;
  const baselineHoldoutComposites = result.baselineOnHoldout.cells.filter((cell) => !cell.error).map((cell) => {
    const scores = Object.values(cell.judgeScores);
    return scores.length === 0 ? Number.NaN : scores.reduce((sum, s) => sum + s.composite, 0) / scores.length;
  }).filter((v) => Number.isFinite(v));
  if (baselineHoldoutComposites.length >= 3) {
    power = powerPreflight({
      baselineComposites: baselineHoldoutComposites,
      sharedScorerChannel: true
    });
    if (opts.onProgress) {
      opts.onProgress({
        kind: "power.estimated",
        n: power.n,
        sd: power.sd,
        mde: power.mde,
        underpowered: power.underpowered
      });
    }
    if (power.underpowered && generations > 0) {
      console.warn(`[selfImprove] ${power.recommendation}`);
    }
  }
  if (opts.onProgress) {
    opts.onProgress({
      kind: "baseline.completed",
      compositeMean: baseline.compositeMean,
      durationMs: Date.now() - startedAt
    });
    opts.onProgress({
      kind: "gate.decided",
      decision: result.gateResult.decision,
      lift: winnerStats.compositeMean - baseline.compositeMean
    });
  }
  const cost = result.cost;
  const totalCost = cost.totalCostUsd;
  const insight = await analyzeRuns({
    runs: [
      ...cellsToRunRecords(result.baselineCampaign.cells, "baseline", runDir, opts.baselineSurface),
      ...cellsToRunRecords(result.winnerOnHoldout.cells, "winner", runDir, result.winnerSurface)
    ],
    baselineCandidateId: "baseline",
    candidateCandidateId: "winner"
  });
  const durationMs = Date.now() - startedAt;
  const { record: provenance } = await emitLoopProvenance({
    ...loopProvenanceArgsFromResult({
      runId: `${runDir}#${startedAt}`,
      runDir,
      timestamp: new Date(startedAt).toISOString(),
      baselineSurface: opts.baselineSurface,
      result,
      costReceipts: costLedger.list(),
      totalCostUsd: totalCost,
      totalDurationMs: durationMs
    }),
    storage,
    hostedClient: opts.hostedTenant ? createHostedClient(opts.hostedTenant) : void 0
  });
  if (opts.onProvenance) opts.onProvenance(provenance);
  const summary = {
    baseline,
    winner: {
      ...winnerStats,
      surface: result.winnerSurface,
      ...result.winnerLabel ? { label: result.winnerLabel } : {},
      ...result.winnerRationale ? { rationale: result.winnerRationale } : {}
    },
    lift: winnerStats.compositeMean - baseline.compositeMean,
    diff: result.promotedDiff,
    provenance,
    gateDecision: result.gateResult.decision,
    generationsExplored: result.generations.length,
    durationMs,
    totalCostUsd: totalCost,
    cost,
    receipts: costLedger.list(),
    insight,
    ...power ? { power } : {},
    raw: result
  };
  if (opts.hostedTenant) {
    try {
      await shipEvalRunToHosted(opts.hostedTenant, opts, summary, result, runDir);
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      console.warn(`[agent-eval] hosted ingest failed (continuing): ${msg}`);
    }
  }
  return summary;
}
async function shipEvalRunToHosted(tenant, opts, summary, raw, runDir) {
  const client = createHostedClient(tenant);
  function snapshotFromCampaign(index, surface, campaign, durationMs) {
    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;
      return {
        scenarioId: cell.scenarioId,
        rep: cell.rep,
        compositeMean: composite,
        dimensions: Object.fromEntries(
          Object.entries(cell.judgeScores).map(([name, score]) => [name, score.dimensions])
        ),
        errorMessage: cell.error ?? void 0
      };
    });
    const compositeMean = cells.length === 0 ? 0 : cells.reduce((s, c) => s + c.compositeMean, 0) / cells.length;
    return {
      index,
      surfaceHash: surfaceHash(surface),
      surface,
      cells,
      compositeMean,
      costUsd: campaign.aggregates.totalCostUsd,
      durationMs
    };
  }
  const generations = [];
  generations.push(snapshotFromCampaign(0, opts.baselineSurface, raw.baselineCampaign, 0));
  for (const gen of raw.generations) {
    const winner = gen.surfaces.reduce(
      (best, s) => s.campaign.aggregates.cellsExecuted > 0 && (best === void 0 || averageComposite(s.campaign) > averageComposite(best.campaign)) ? s : best,
      gen.surfaces[0]
    );
    if (!winner) continue;
    generations.push(
      snapshotFromCampaign(gen.record.generationIndex + 1, winner.surface, winner.campaign, 0)
    );
  }
  const event = {
    runId: `${runDir}#${Date.now()}`,
    runDir,
    timestamp: (/* @__PURE__ */ new Date()).toISOString(),
    status: "finished",
    labels: opts.hostedLabels ?? {},
    baseline: generations[0],
    generations,
    gateDecision: summary.gateDecision,
    holdoutLift: summary.lift,
    totalCostUsd: summary.totalCostUsd,
    totalDurationMs: summary.durationMs,
    insightReport: summary.insight
  };
  await client.ingestEvalRun(event);
}
function averageComposite(campaign) {
  const aggs = Object.values(campaign.aggregates.byScenario);
  return aggs.length === 0 ? 0 : aggs.reduce((s, a) => s + a.meanComposite, 0) / aggs.length;
}
function hashString(s) {
  let h = 2166136261 >>> 0;
  for (let i = 0; i < s.length; i++) {
    h ^= s.charCodeAt(i);
    h = Math.imul(h, 16777619) >>> 0;
  }
  return h.toString(16).padStart(8, "0");
}
function cellsToRunRecords(cells, candidateId, runId, surface) {
  const promptHash = surfaceContentHash(surface);
  const configHash = `sha256:${createHash("sha256").update(candidateId).digest("hex")}`;
  return cells.map((cell) => {
    const perJudge = {};
    const perDimMeanAccum = {};
    let compositeSum = 0;
    let compositeCount = 0;
    for (const [judgeId, score] of Object.entries(cell.judgeScores)) {
      perJudge[judgeId] = { ...score.dimensions };
      for (const [dim, value] of Object.entries(score.dimensions)) {
        if (!Number.isFinite(value)) continue;
        const accum = perDimMeanAccum[dim] ?? { sum: 0, n: 0 };
        accum.sum += value;
        accum.n += 1;
        perDimMeanAccum[dim] = accum;
      }
      if (Number.isFinite(score.composite)) {
        compositeSum += score.composite;
        compositeCount += 1;
      }
    }
    const perDimMean = {};
    for (const [dim, { sum, n }] of Object.entries(perDimMeanAccum)) {
      perDimMean[dim] = n === 0 ? 0 : sum / n;
    }
    const composite = compositeCount === 0 ? 0 : compositeSum / compositeCount;
    const judgeScores = {
      perJudge,
      perDimMean,
      composite
    };
    return {
      runId: `${runId}::${candidateId}::${cell.cellId}`,
      experimentId: runId,
      candidateId,
      // Pair on (scenarioId, rep) — analyzeRuns pairs on (experimentId, seed).
      // Synthesize a stable seed for that pairing.
      seed: cell.rep * 1e6 + hashString(cell.scenarioId).slice(0, 6).split("").reduce((a, c) => a * 31 + c.charCodeAt(0) >>> 0, 0),
      model: "campaign-cell",
      promptHash,
      configHash,
      commitSha: "cell",
      wallMs: cell.durationMs,
      costUsd: cell.costUsd,
      tokenUsage: {
        input: cell.tokenUsage.input,
        output: cell.tokenUsage.output,
        ...cell.tokenUsage.reasoning === void 0 ? {} : { reasoning: cell.tokenUsage.reasoning },
        ...cell.tokenUsage.cached === void 0 ? {} : { cached: cell.tokenUsage.cached },
        ...cell.tokenUsage.cacheWrite === void 0 ? {} : { cacheWrite: cell.tokenUsage.cacheWrite }
      },
      outcome: {
        holdoutScore: composite,
        raw: {},
        judgeScores
      },
      splitTag: "holdout",
      ...cell.error ? { failureMode: cell.error } : {}
    };
  });
}

// src/contract/define-agent-eval.ts
function defineAgentEval(defaults) {
  const defaultEvaluateOptions = evaluateDefaults(defaults);
  return {
    scenarios: defaults.scenarios,
    baselineSurface: defaults.baselineSurface,
    async evaluate(opts = {}) {
      const { agent, judge, judges, runDir, scenarios, surface, ...campaignOpts } = opts;
      const selectedAgent = agent ?? defaults.agent;
      const selectedSurface = surface ?? defaults.baselineSurface;
      const selectedRunDir = runDir ?? defaults.runDir ?? `mem://defineAgentEval-${Date.now()}`;
      const selectedStorage = campaignOpts.storage ?? defaultEvaluateOptions.storage ?? (selectedRunDir.startsWith("mem://") ? inMemoryCampaignStorage() : void 0);
      const evalOptions = {
        ...defaultEvaluateOptions,
        ...campaignOpts,
        ...selectedStorage ? { storage: selectedStorage } : {},
        runDir: selectedRunDir,
        scenarios: scenarios ?? defaults.scenarios,
        dispatch: (scenario, ctx) => selectedAgent(selectedSurface, scenario, ctx),
        judges: evaluateJudges(judges, judge ?? defaults.judge)
      };
      if (evalOptions.reps !== void 0)
        evalOptions.reps = requirePositiveInteger(evalOptions.reps, "reps");
      return runEval(evalOptions);
    },
    async improve(opts = {}) {
      const {
        budget: budgetOverride,
        hostedTenant: hostedTenantOverride,
        llm: llmOverride,
        ...topLevelOverrides
      } = opts;
      const merged = mergeDefined(defaults, topLevelOverrides);
      const budget = mergeBudget(defaults.budget, budgetOverride);
      const llm = mergeLlm(defaults.llm, llmOverride);
      const hostedTenant = mergeHostedTenant(defaults.hostedTenant, hostedTenantOverride);
      return selfImprove({
        ...merged,
        ...budget ? { budget } : {},
        ...llm ? { llm } : {},
        ...hostedTenant ? { hostedTenant } : {}
      });
    }
  };
}
function evaluateDefaults(defaults) {
  const out = {};
  if (defaults.storage) out.storage = defaults.storage;
  if (defaults.labeledStore) out.labeledStore = defaults.labeledStore;
  if (defaults.captureSource) out.captureSource = defaults.captureSource;
  if (defaults.cellPlacement) out.cellPlacement = defaults.cellPlacement;
  if (defaults.expectUsage) out.expectUsage = defaults.expectUsage;
  if (defaults.budget?.dollars !== void 0) out.costCeiling = defaults.budget.dollars;
  if (defaults.budget?.maxConcurrency !== void 0)
    out.maxConcurrency = defaults.budget.maxConcurrency;
  if (defaults.budget?.reps !== void 0)
    out.reps = requirePositiveInteger(defaults.budget.reps, "budget.reps");
  return out;
}
function mergeBudget(defaults, overrides) {
  const merged = mergeOptionalObject(defaults, overrides);
  if (merged?.reps !== void 0) merged.reps = requirePositiveInteger(merged.reps, "budget.reps");
  return merged;
}
function mergeLlm(defaults, overrides) {
  return mergeOptionalObject(defaults, overrides);
}
function mergeHostedTenant(defaults, overrides) {
  const merged = mergeOptionalObject(defaults, overrides);
  if (!merged) return void 0;
  if (!merged.endpoint?.trim() || !merged.apiKey?.trim() || !merged.tenantId?.trim()) {
    throw new Error(
      "defineAgentEval.improve: hostedTenant requires endpoint, apiKey, and tenantId after merging defaults and overrides"
    );
  }
  return merged;
}
function mergeDefined(defaults, overrides) {
  if (!overrides) return defaults;
  const merged = { ...defaults };
  for (const [key, value] of Object.entries(overrides)) {
    if (value !== void 0) merged[key] = value;
  }
  return merged;
}
function mergeOptionalObject(defaults, overrides) {
  if (!defaults && !overrides) return void 0;
  return mergeDefined(defaults ?? {}, overrides);
}
function evaluateJudges(judges, defaultJudge) {
  if (judges !== void 0) {
    if (judges.length === 0) {
      throw new Error("defineAgentEval.evaluate: judges must not be empty");
    }
    return judges;
  }
  return [defaultJudge];
}
function requirePositiveInteger(value, field) {
  if (!Number.isInteger(value) || value < 1) {
    throw new Error(`defineAgentEval: ${field} must be a positive integer`);
  }
  return value;
}

// src/contract/measured-comparison.ts
import {
  agentCandidateBenchmarkSuiteSchema,
  agentCandidateBenchmarkTaskSchema,
  agentCandidateBundleSchema,
  agentCandidateExperimentSchema,
  agentImprovementMeasuredComparisonSchema,
  candidateExecutionEvidenceSchema,
  canonicalCandidateDigest,
  omitTopLevelDigest
} from "@tangle-network/agent-interface";
function sealCandidateBenchmarkTask(material) {
  return agentCandidateBenchmarkTaskSchema.parse({
    ...material,
    digest: canonicalCandidateDigest(material)
  });
}
function sealCandidateBenchmarkSuite(options) {
  for (const task of options.tasks) verifyCandidateBenchmarkTask(task);
  const material = {
    kind: "agent-candidate-benchmark-suite",
    digestAlgorithm: "rfc8785-sha256",
    taskDigests: options.tasks.map((task) => task.digest),
    reps: options.reps,
    seeds: options.seeds
  };
  const suite = agentCandidateBenchmarkSuiteSchema.parse({
    ...material,
    digest: canonicalCandidateDigest(material)
  });
  return { suite, tasks: options.tasks };
}
function sealCandidateExperiment(material) {
  const parsed = agentCandidateExperimentSchema.parse({
    ...material,
    digest: canonicalCandidateDigest(material)
  });
  return verifyCandidateExperiment(parsed);
}
function verifyCandidateExperiment(input) {
  const experiment = agentCandidateExperimentSchema.parse(input);
  verifySelfAddressed(experiment, "candidate experiment");
  verifyBundle(experiment.baseline, "baseline bundle");
  verifyBundle(experiment.candidate, "candidate bundle");
  if (experiment.baseline.digest === experiment.candidate.digest) {
    throw new Error("candidate experiment baseline and candidate bundles are identical");
  }
  verifyCandidateBenchmarkSuiteInputs(experiment.benchmark);
  return experiment;
}
async function runCandidateExperiment(options) {
  const experiment = verifyCandidateExperiment(options.experiment);
  const { suite, tasks } = experiment.benchmark;
  const maxConcurrency = options.maxConcurrency ?? 2;
  if (!Number.isSafeInteger(maxConcurrency) || maxConcurrency < 1) {
    throw new Error("candidate experiment maxConcurrency must be a positive integer");
  }
  const measurements = new Array(
    suite.taskDigests.length * suite.reps
  );
  let nextIndex = 0;
  const lanes = Array.from({ length: Math.min(maxConcurrency, measurements.length) }, async () => {
    while (true) {
      if (options.signal?.aborted) throw abortError(options.signal);
      const index = nextIndex;
      nextIndex += 1;
      if (index >= measurements.length) return;
      const taskIndex = Math.floor(index / suite.reps);
      const repetition = index % suite.reps;
      const task = tasks[taskIndex];
      const seed = suite.seeds[index];
      if (!task || seed === void 0) {
        throw new Error(`candidate experiment cell ${index} has no signed task or seed`);
      }
      const benchmarkCell = {
        suiteDigest: suite.digest,
        taskIndex,
        repetition
      };
      const [baseline, candidate] = await Promise.all([
        options.execute({
          experiment,
          arm: "baseline",
          bundle: experiment.baseline,
          task,
          benchmarkCell,
          seed,
          ...options.signal ? { signal: options.signal } : {}
        }),
        options.execute({
          experiment,
          arm: "candidate",
          bundle: experiment.candidate,
          task,
          benchmarkCell,
          seed,
          ...options.signal ? { signal: options.signal } : {}
        })
      ]);
      const measurement = { baseline, candidate };
      verifyMeasurement(experiment, measurement, index);
      measurements[index] = measurement;
    }
  });
  await Promise.all(lanes);
  return measurements;
}
function measuredComparisonFromCandidateExperiment(options) {
  const experiment = verifyCandidateExperiment(options.experiment);
  const measurements = options.measurements.map(
    (measurement, index) => verifyMeasurement(experiment, measurement, index)
  );
  const expectedN = experiment.benchmark.suite.taskDigests.length * experiment.benchmark.suite.reps;
  if (measurements.length !== expectedN) {
    throw new Error(
      `candidate experiment is incomplete (${measurements.length}/${expectedN} paired cells)`
    );
  }
  verifyStableProfileMaterialization(measurements);
  if (!options.runId.trim()) throw new Error("candidate experiment runId is required");
  const {
    confidenceLevel: confidence,
    resamples,
    bootstrapSeed,
    deltaThreshold,
    minProductiveRuns,
    budgetUsd,
    criticalDimensions,
    regressionTolerance
  } = experiment.policy;
  const baselineScores = measurements.map(scoreOf("baseline"));
  const candidateScores = measurements.map(scoreOf("candidate"));
  const overall = measuredEstimate(baselineScores, candidateScores, {
    confidence,
    resamples,
    seed: bootstrapSeed
  });
  const dimensions = sharedDimensions(measurements);
  const objectives = [
    {
      kind: "objective",
      name: "benchmark-score",
      direction: "higher-is-better",
      unit: "score",
      availability: "measured",
      ...overall
    },
    ...dimensions.map((name, index) => ({
      kind: "dimension",
      objective: "benchmark-score",
      name,
      direction: "higher-is-better",
      unit: "score",
      availability: "measured",
      ...measuredEstimate(
        measurements.map(dimensionOf("baseline", name)),
        measurements.map(dimensionOf("candidate", name)),
        { confidence, resamples, seed: bootstrapSeed + index + 1 }
      )
    }))
  ];
  const cost = measuredEstimate(
    measurements.map(costOf("baseline")),
    measurements.map(costOf("candidate")),
    { confidence, resamples, seed: bootstrapSeed + dimensions.length + 1 }
  );
  const latency = measuredEstimate(
    measurements.map(latencyOf("baseline")),
    measurements.map(latencyOf("candidate")),
    { confidence, resamples, seed: bootstrapSeed + dimensions.length + 2 }
  );
  objectives.push(
    {
      kind: "cost",
      name: "cost",
      direction: "lower-is-better",
      unit: "usd",
      availability: "measured",
      ...cost
    },
    {
      kind: "latency",
      name: "latency",
      direction: "lower-is-better",
      unit: "milliseconds",
      availability: "measured",
      ...latency
    }
  );
  const significance = heldoutSignificance(
    { before: baselineScores, after: candidateScores, cellIds: cellIds(experiment) },
    {
      confidence,
      resamples,
      seed: bootstrapSeed,
      statistic: "mean",
      deltaThreshold,
      minProductiveRuns
    }
  );
  const power = baselineScores.length >= 3 ? powerPreflight({
    baselineComposites: baselineScores,
    pairedN: baselineScores.length,
    deltaThreshold,
    confidence,
    sharedScorerChannel: true
  }) : void 0;
  const powerSufficient = baselineScores.length >= minProductiveRuns && power !== void 0 && !power.underpowered;
  const guardedDimensions = new Set(criticalDimensions);
  const missingCriticalDimensions = criticalDimensions.filter(
    (dimension) => !dimensions.includes(dimension)
  );
  const regressions = objectives.filter(
    (objective) => objective.kind === "dimension" && guardedDimensions.has(objective.name) && objective.availability === "measured" && objective.confidenceInterval.lower < -regressionTolerance
  );
  const executionCostUsd = measurements.reduce(
    (sum, measurement) => sum + costFromEvidence(measurement.baseline) + costFromEvidence(measurement.candidate),
    0
  );
  const searchCostUsd = options.searchCostUsd ?? 0;
  const totalCostUsd = executionCostUsd + searchCostUsd;
  const budgetPassed = budgetUsd === void 0 || totalCostUsd <= budgetUsd;
  const completedRuns = measurements.flatMap((measurement) => [
    measurement.baseline,
    measurement.candidate
  ]);
  const incompleteRuns = completedRuns.filter((evidence) => !completedSuccessfully(evidence));
  const failedCandidateResults = measurements.filter(
    (measurement) => !measurement.candidate.receipt.benchmarkResult.material.passed
  );
  const checks = [
    { name: "paired-significance", passed: significance.significant },
    { name: "statistical-power", passed: powerSufficient },
    { name: "all-runs-completed", passed: incompleteRuns.length === 0 },
    { name: "candidate-task-pass", passed: failedCandidateResults.length === 0 },
    {
      name: "critical-dimensions",
      passed: regressions.length === 0 && missingCriticalDimensions.length === 0
    },
    { name: "budget", passed: budgetPassed }
  ];
  const shipped = checks.every((check) => check.passed);
  const reasons = [
    ...significance.significant ? [] : [
      significance.fewRuns ? `only ${significance.n} paired runs; ${minProductiveRuns} required` : `paired interval lower bound ${significance.bootstrap.low} did not clear ${deltaThreshold}`
    ],
    ...powerSufficient ? [] : [power?.recommendation ?? `need at least ${Math.max(3, minProductiveRuns)} paired runs`],
    ...regressions.length === 0 ? [] : [`critical dimensions regressed: ${regressions.map((entry) => entry.name).join(", ")}`],
    ...missingCriticalDimensions.length === 0 ? [] : [`critical dimensions missing: ${missingCriticalDimensions.join(", ")}`],
    ...incompleteRuns.length === 0 ? [] : [`${incompleteRuns.length} benchmark executions did not exit successfully`],
    ...failedCandidateResults.length === 0 ? [] : [`candidate failed ${failedCandidateResults.length} benchmark tasks`],
    ...budgetPassed ? [] : [`total cost ${totalCostUsd} exceeded budget ${budgetUsd}`]
  ];
  const diff = deriveCandidateBundleDiff(experiment);
  const executionDurationMs = measurements.reduce(
    (sum, measurement) => sum + latencyFromEvidence(measurement.baseline) + latencyFromEvidence(measurement.candidate),
    0
  );
  const searchDurationMs = options.searchDurationMs ?? 0;
  const durationMs = executionDurationMs + searchDurationMs;
  const provisional = agentImprovementMeasuredComparisonSchema.parse({
    kind: "agent-improvement-measured-comparison",
    experiment,
    measurements,
    overall: {
      name: "composite",
      direction: "higher-is-better",
      unit: "score",
      ...overall
    },
    objectives,
    ...options.candidate ? { candidate: options.candidate } : {},
    decision: {
      outcome: shipped ? "ship" : significance.fewRuns || !powerSufficient ? "need_more_work" : "hold",
      reasons: reasons.length > 0 ? reasons : ["all measured checks passed"],
      contributingChecks: checks
    },
    power: {
      sufficient: powerSufficient,
      n: baselineScores.length,
      minimumDetectableDelta: power?.mde ?? 1,
      confidenceLevel: confidence,
      scaleAssumed: power?.scaleAssumed ?? true,
      sharedScorerChannel: true,
      reason: power?.recommendation ?? `need at least ${Math.max(3, minProductiveRuns)} paired runs`
    },
    provenance: {
      kind: "agent-eval-loop",
      schema: "agent-candidate-experiment",
      runId: options.runId,
      recordDigest: canonicalCandidateDigest({}),
      baselineContentHash: experiment.baseline.digest,
      candidateContentHash: experiment.candidate.digest
    },
    diff,
    evaluation: {
      generationsExplored: options.generationsExplored ?? 0,
      searchDurationMs,
      executionDurationMs,
      durationMs,
      searchCostUsd,
      executionCostUsd,
      totalCostUsd
    },
    ...options.metadata ? { metadata: options.metadata } : {}
  });
  const { recordDigest: _recordDigest, ...provenance } = provisional.provenance;
  return agentImprovementMeasuredComparisonSchema.parse({
    ...provisional,
    provenance: {
      ...provenance,
      recordDigest: canonicalCandidateDigest({ ...provisional, provenance })
    }
  });
}
function verifyCandidateExperimentComparison(input) {
  const comparison = agentImprovementMeasuredComparisonSchema.parse(input);
  const recomputed = measuredComparisonFromCandidateExperiment({
    experiment: comparison.experiment,
    measurements: comparison.measurements,
    runId: comparison.provenance.runId,
    ...comparison.candidate ? { candidate: comparison.candidate } : {},
    generationsExplored: comparison.evaluation.generationsExplored,
    searchDurationMs: comparison.evaluation.searchDurationMs,
    searchCostUsd: comparison.evaluation.searchCostUsd,
    ...comparison.metadata ? { metadata: comparison.metadata } : {}
  });
  if (canonicalCandidateDigest(recomputed) !== canonicalCandidateDigest(comparison)) {
    throw new Error("candidate experiment comparison does not match its Runtime receipts");
  }
  return comparison;
}
function deriveCandidateBundleDiff(experiment) {
  const surfaces = ["profile", "code", "execution", "knowledge", "memory"];
  const changed = surfaces.flatMap((surface) => {
    const baseline = experiment.baseline[surface] ?? null;
    const candidate = experiment.candidate[surface] ?? null;
    const baselineDigest = canonicalCandidateDigest(baseline);
    const candidateDigest = canonicalCandidateDigest(candidate);
    if (baselineDigest === candidateDigest) return [];
    return [
      [
        `--- baseline/${surface} (${baselineDigest})`,
        `+++ candidate/${surface} (${candidateDigest})`,
        JSON.stringify({ baseline, candidate }, null, 2)
      ].join("\n")
    ];
  });
  if (changed.length === 0) {
    throw new Error("candidate experiment has no changed candidate surface");
  }
  return changed.join("\n\n");
}
function verifyCandidateBenchmarkTask(input) {
  const task = agentCandidateBenchmarkTaskSchema.parse(input);
  verifySelfAddressed(task, "candidate benchmark task");
  return task;
}
function verifyCandidateBenchmarkSuiteInputs(input) {
  if (input === null || typeof input !== "object" || Array.isArray(input)) {
    throw new Error("candidate benchmark suite inputs must be an object");
  }
  const candidate = input;
  const suite = verifyCandidateBenchmarkSuite(candidate.suite);
  if (!Array.isArray(candidate.tasks) || candidate.tasks.length !== suite.taskDigests.length) {
    throw new Error("candidate benchmark suite task count does not match its signed digests");
  }
  candidate.tasks.forEach((task, index) => {
    const verified = verifyCandidateBenchmarkTask(task);
    if (verified.digest !== suite.taskDigests[index]) {
      throw new Error(`candidate benchmark task ${index} does not match the signed suite`);
    }
  });
  return { suite, tasks: candidate.tasks };
}
function verifyCandidateBenchmarkSuite(input) {
  const suite = agentCandidateBenchmarkSuiteSchema.parse(input);
  verifySelfAddressed(suite, "candidate benchmark suite");
  return suite;
}
function verifyBundle(input, label) {
  const bundle = agentCandidateBundleSchema.parse(input);
  verifySelfAddressed(bundle, label);
  return bundle;
}
function verifyMeasurement(experiment, input, index) {
  const suite = experiment.benchmark.suite;
  const taskIndex = Math.floor(index / suite.reps);
  const repetition = index % suite.reps;
  const task = experiment.benchmark.tasks[taskIndex];
  const seed = suite.seeds[index];
  if (!task || seed === void 0) {
    throw new Error(`candidate experiment measurement ${index} is outside the signed suite`);
  }
  const baseline = verifyExecutionEvidence(input.baseline);
  const candidate = verifyExecutionEvidence(input.candidate);
  for (const [arm, evidence] of [
    ["baseline", baseline],
    ["candidate", candidate]
  ]) {
    const bundle = experiment[arm];
    const materialization = evidence.materializationReceipt;
    const plan = materialization.executionPlan.material;
    const runCell = plan.runCell;
    verifySelfAddressed(runCell, "candidate run cell");
    if (runCell.experimentDigest !== experiment.digest || runCell.arm !== arm || runCell.bundleDigest !== bundle.digest || runCell.suiteDigest !== suite.digest || runCell.taskDigest !== task.digest || runCell.taskIndex !== taskIndex || runCell.repetition !== repetition || runCell.seed !== seed || runCell.attempt > task.attempt.maxAttempts || materialization.bundleDigest !== bundle.digest || materialization.benchmark.suite.digest !== suite.digest || materialization.benchmark.task.digest !== task.digest || materialization.codeKind !== bundle.code.kind || materialization.profileActivation.profilePlan.material.sourceProfileDigest !== canonicalCandidateDigest(bundle.profile) || evidence.receipt.runCellDigest !== runCell.digest || JSON.stringify(materialization.resolvedModel) !== JSON.stringify(task.model)) {
      throw new Error(`candidate experiment measurement ${index} substituted its ${arm} arm`);
    }
    verifyTaskOutcome(task, evidence, index, arm);
  }
  const baselinePlan = baseline.materializationReceipt.executionPlan.material;
  const candidatePlan = candidate.materializationReceipt.executionPlan.material;
  if (baselinePlan.executionId === candidatePlan.executionId || baselinePlan.runCell.digest === candidatePlan.runCell.digest || baseline.materializationReceipt.digest === candidate.materializationReceipt.digest || baseline.receipt.digest === candidate.receipt.digest || baseline.digest === candidate.digest) {
    throw new Error(`candidate experiment measurement ${index} reused one execution across arms`);
  }
  return { baseline, candidate };
}
function verifyExecutionEvidence(input) {
  const evidence = candidateExecutionEvidenceSchema.parse(input);
  verifySelfAddressed(evidence, "candidate execution evidence");
  verifySelfAddressed(evidence.materializationReceipt, "candidate materialization receipt");
  verifySelfAddressed(
    evidence.materializationReceipt.profileActivation,
    "candidate profile activation"
  );
  verifyMaterialAddressed(
    evidence.materializationReceipt.profileActivation.profilePlan,
    "candidate profile plan"
  );
  verifyMaterialAddressed(evidence.materializationReceipt.executionPlan, "candidate execution plan");
  verifySelfAddressed(evidence.receipt, "candidate run receipt");
  verifyMaterialAddressed(evidence.receipt.modelSettlement, "candidate model settlement");
  verifyMaterialAddressed(evidence.receipt.taskOutcome, "candidate task outcome");
  verifyMaterialAddressed(evidence.receipt.benchmarkResult, "candidate benchmark result");
  return evidence;
}
function verifySelfAddressed(document, label) {
  if (canonicalCandidateDigest(omitTopLevelDigest(document)) !== document.digest) {
    throw new Error(`${label} digest is invalid`);
  }
}
function verifyTaskOutcome(task, evidence, index, arm) {
  const outcome = evidence.receipt.taskOutcome.material.outcome;
  const result = evidence.receipt.benchmarkResult.material;
  const prefix = `candidate experiment measurement ${index} ${arm}`;
  if (result.evidence.sha256 === task.grader.artifact.sha256) {
    throw new Error(`${prefix} reused grader bytes as grading evidence`);
  }
  const usage = combinedUsage(evidence);
  const usageChecks = [
    [usage.modelCalls, task.limits.maxModelCalls, "model calls"],
    [usage.inputTokens, task.limits.maxInputTokens, "input tokens"],
    [usage.outputTokens, task.limits.maxOutputTokens, "output tokens"],
    [usage.costUsdNanos, Math.round(task.limits.maxCostUsd * 1e9), "cost"]
  ];
  for (const [actual, maximum, label] of usageChecks) {
    if (actual > maximum) {
      throw new Error(`${prefix} ${label} ${actual} exceeds the signed limit ${maximum}`);
    }
  }
  if (outcome.kind !== task.outcome.kind) {
    throw new Error(`${prefix} returned an outcome outside the signed task contract`);
  }
  if (task.outcome.kind === "output") {
    if (outcome.kind !== "output" || outcome.spec.mediaType !== task.outcome.mediaType || outcome.spec.maxBytes !== task.outcome.maxBytes) {
      throw new Error(`${prefix} changed the signed output contract`);
    }
    return;
  }
  const repository = task.repository;
  if (outcome.kind !== "workspace" || repository === void 0 || outcome.baseRepository.identity !== repository.identity || outcome.baseRepository.rootIdentity !== repository.rootIdentity || outcome.baseRepository.commit !== repository.baseCommit || outcome.baseRepository.tree !== repository.baseTree) {
    throw new Error(`${prefix} did not start from the signed repository state`);
  }
}
function verifyStableProfileMaterialization(measurements) {
  for (const arm of ["baseline", "candidate"]) {
    const expected = measurements[0]?.[arm].materializationReceipt.profileActivation;
    if (!expected) throw new Error("candidate experiment contains no profile materialization");
    const expectedDigest = canonicalCandidateDigest({
      profilePlanDigest: expected.profilePlan.digest,
      files: expected.files
    });
    for (const [index, measurement] of measurements.entries()) {
      const activation = measurement[arm].materializationReceipt.profileActivation;
      if (canonicalCandidateDigest({
        profilePlanDigest: activation.profilePlan.digest,
        files: activation.files
      }) !== expectedDigest) {
        throw new Error(
          `candidate experiment measurement ${index} ${arm} materialized a different profile`
        );
      }
    }
  }
}
function completedSuccessfully(evidence) {
  const termination = evidence.receipt.termination;
  return termination.kind === "exit" && termination.exitCode === 0;
}
function verifyMaterialAddressed(evidence, label) {
  if (canonicalCandidateDigest(evidence.material) !== evidence.digest) {
    throw new Error(`${label} digest is invalid`);
  }
}
function measuredEstimate(baseline, candidate, options) {
  const bootstrap = pairedBootstrap(baseline, candidate, {
    confidence: options.confidence,
    resamples: options.resamples,
    statistic: "mean",
    seed: options.seed
  });
  const baselineMean = mean(baseline);
  const candidateMean = mean(candidate);
  const delta = candidateMean - baselineMean;
  return {
    baseline: baselineMean,
    candidate: candidateMean,
    delta,
    confidenceInterval: {
      level: bootstrap.confidence,
      lower: Math.min(bootstrap.low, delta),
      upper: Math.max(bootstrap.high, delta),
      method: "paired-bootstrap",
      statistic: "mean",
      resamples: bootstrap.resamples
    },
    n: bootstrap.n
  };
}
function sharedDimensions(measurements) {
  const expected = dimensionNames(measurements[0]?.baseline);
  for (const [index, measurement] of measurements.entries()) {
    for (const [arm, evidence] of [
      ["baseline", measurement.baseline],
      ["candidate", measurement.candidate]
    ]) {
      const actual = dimensionNames(evidence);
      if (JSON.stringify(actual) !== JSON.stringify(expected)) {
        throw new Error(
          `candidate experiment measurement ${index} ${arm} dimensions do not match the suite`
        );
      }
    }
  }
  return expected;
}
function dimensionNames(evidence) {
  return (evidence?.receipt.benchmarkResult.material.dimensions ?? []).map(
    (dimension) => dimension.name
  );
}
function scoreOf(arm) {
  return (measurement) => measurement[arm].receipt.benchmarkResult.material.score;
}
function dimensionOf(arm, name) {
  return (measurement) => {
    const dimension = measurement[arm].receipt.benchmarkResult.material.dimensions.find(
      (entry) => entry.name === name
    );
    if (!dimension) throw new Error(`candidate experiment is missing dimension '${name}'`);
    return dimension.score;
  };
}
function costOf(arm) {
  return (measurement) => costFromEvidence(measurement[arm]);
}
function latencyOf(arm) {
  return (measurement) => latencyFromEvidence(measurement[arm]);
}
function costFromEvidence(evidence) {
  return combinedUsage(evidence).costUsdNanos / 1e9;
}
function latencyFromEvidence(evidence) {
  return evidence.receipt.timing.durationMs + evidence.receipt.benchmarkResult.material.grading.timing.durationMs;
}
function combinedUsage(evidence) {
  const candidate = evidence.receipt.modelSettlement.material.usage;
  const grader = evidence.receipt.benchmarkResult.material.grading.usage;
  return {
    inputTokens: candidate.inputTokens + grader.inputTokens,
    outputTokens: candidate.outputTokens + grader.outputTokens,
    cachedInputTokens: candidate.cachedInputTokens + grader.cachedInputTokens,
    reasoningTokens: candidate.reasoningTokens + grader.reasoningTokens,
    modelCalls: candidate.modelCalls + grader.modelCalls,
    costUsdNanos: candidate.costUsdNanos + grader.costUsdNanos
  };
}
function cellIds(experiment) {
  const { suite, tasks } = experiment.benchmark;
  return suite.seeds.map((_, index) => {
    const taskIndex = Math.floor(index / suite.reps);
    const repetition = index % suite.reps;
    return `${tasks[taskIndex]?.scenario.id ?? taskIndex}:${repetition}`;
  });
}
function mean(values) {
  if (values.length === 0) throw new Error("candidate experiment requires measured values");
  return values.reduce((sum, value) => sum + value, 0) / values.length;
}
function abortError(signal) {
  return signal.reason instanceof Error ? signal.reason : new Error("candidate experiment aborted");
}

// src/contract/eval-reporting-suite.ts
import { mkdir, writeFile } from "fs/promises";
import { dirname, join as join2 } from "path";

// src/contract/intake/run-record-dir.ts
import { readdir, readFile, stat } from "fs/promises";
import { join } from "path";
var ANALYSIS_ARTIFACT = "analysis.json";
function defaultInclude(fileName) {
  if (fileName === ANALYSIS_ARTIFACT) return false;
  return fileName.endsWith(".json") || fileName.endsWith(".jsonl");
}
async function fromRunRecordDir(path, options = {}) {
  const onInvalid = options.onInvalid ?? "throw";
  const include = options.include ?? defaultInclude;
  const stats = await stat(path);
  const filePaths = stats.isDirectory() ? await collectFiles(path, include, options.recursive ?? false) : [path];
  const runs = [];
  const rejected = [];
  for (const file of filePaths) {
    const raw = await parseRecordFile(file);
    for (const { index, value } of raw) {
      const parsed = parseRunRecordSafe(value);
      if (parsed.ok) {
        runs.push(parsed.value);
        continue;
      }
      const rejection = { file, index, reason: parsed.error.message };
      if (onInvalid === "throw") {
        throw new Error(
          `fromRunRecordDir: invalid RunRecord in '${file}' at index ${index}: ${parsed.error.message}`
        );
      }
      rejected.push(rejection);
    }
  }
  return { runs, rejected, files: filePaths };
}
async function parseRecordFile(file) {
  const text = await readFile(file, "utf8");
  const trimmed = text.trim();
  if (trimmed.length === 0) return [];
  if (trimmed.startsWith("[")) {
    const parsed = JSON.parse(trimmed);
    if (!Array.isArray(parsed)) {
      throw new Error(`fromRunRecordDir: file '${file}' did not parse to an array`);
    }
    return parsed.map((value, index) => ({ index, value }));
  }
  const out = [];
  const lines = trimmed.split("\n");
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i].trim();
    if (line.length === 0) continue;
    try {
      out.push({ index: i, value: JSON.parse(line) });
    } catch (err) {
      throw new Error(
        `fromRunRecordDir: file '${file}' line ${i + 1} is not valid JSON: ${err instanceof Error ? err.message : String(err)}`
      );
    }
  }
  return out;
}
async function collectFiles(dir, include, recursive) {
  const entries = await readdir(dir, { withFileTypes: true });
  const files = [];
  const subdirs = [];
  for (const entry of entries) {
    if (entry.isDirectory()) {
      if (recursive) subdirs.push(join(dir, entry.name));
      continue;
    }
    if (include(entry.name)) files.push(join(dir, entry.name));
  }
  files.sort();
  subdirs.sort();
  for (const sub of subdirs) {
    files.push(...await collectFiles(sub, include, recursive));
  }
  return files;
}

// src/contract/eval-reporting-suite.ts
var ANALYSIS_ARTIFACT2 = "analysis.json";
async function evalReportingSuite(input, options = {}) {
  const fromPath = typeof input === "string";
  let runs;
  let files = [];
  let rejected = [];
  if (fromPath) {
    const loaded = await fromRunRecordDir(input, options.load);
    runs = loaded.runs;
    files = loaded.files;
    rejected = loaded.rejected;
  } else {
    runs = input;
  }
  if (runs.length === 0) {
    throw new Error(
      fromPath ? `evalReportingSuite: no RunRecords found at '${input}'` : "evalReportingSuite: no RunRecords to analyze"
    );
  }
  const report = await analyzeRuns({ ...options.analyze, runs });
  const result = {
    report,
    provenance: {
      generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
      runCount: runs.length,
      sourcePath: fromPath ? input : null,
      files,
      rejected
    },
    writtenTo: null
  };
  const target = resolveWriteTarget(options.write, fromPath ? input : null);
  if (target) {
    await mkdir(dirname(target), { recursive: true });
    await writeFile(target, `${JSON.stringify(result, null, 2)}
`, "utf8");
    result.writtenTo = target;
  }
  return result;
}
function resolveWriteTarget(write, sourcePath) {
  if (!write) return null;
  if (typeof write === "string") {
    const looksLikeDir = write.endsWith("/") || !write.endsWith(".json") && !write.endsWith(".jsonl");
    return looksLikeDir ? join2(write, ANALYSIS_ARTIFACT2) : write;
  }
  if (sourcePath === null) {
    throw new Error(
      "evalReportingSuite: write:true needs a source path to anchor analysis.json \u2014 pass an explicit output path when analyzing in-memory records"
    );
  }
  const isFile = sourcePath.endsWith(".json") || sourcePath.endsWith(".jsonl");
  return isFile ? join2(dirname(sourcePath), ANALYSIS_ARTIFACT2) : join2(sourcePath, ANALYSIS_ARTIFACT2);
}

// src/contract/diff.ts
function keyForCell(cell) {
  return JSON.stringify([cell.scenarioId, cell.rep]);
}
function diffDimensions(before, after) {
  const out = {};
  const judges = /* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)]);
  for (const judge of judges) {
    const beforeDims = before[judge] ?? {};
    const afterDims = after[judge] ?? {};
    const dims = /* @__PURE__ */ new Set([...Object.keys(beforeDims), ...Object.keys(afterDims)]);
    const judgeOut = {};
    for (const dim of dims) {
      const rawBefore = beforeDims[dim];
      const rawAfter = afterDims[dim];
      const b = typeof rawBefore === "number" && Number.isFinite(rawBefore) ? rawBefore : null;
      const a = typeof rawAfter === "number" && Number.isFinite(rawAfter) ? rawAfter : null;
      judgeOut[dim] = {
        before: b,
        after: a,
        delta: b !== null && a !== null ? a - b : null
      };
    }
    out[judge] = judgeOut;
  }
  return out;
}
function diffGenerations(before, after) {
  const beforeMap = new Map(before.cells.map((c) => [keyForCell(c), c]));
  const afterMap = new Map(after.cells.map((c) => [keyForCell(c), c]));
  const matched = [];
  const removed = [];
  const added = [];
  for (const [key, beforeCell] of beforeMap) {
    const afterCell = afterMap.get(key);
    if (!afterCell) {
      removed.push(beforeCell);
      continue;
    }
    matched.push({
      scenarioId: beforeCell.scenarioId,
      rep: beforeCell.rep,
      compositeBefore: beforeCell.compositeMean,
      compositeAfter: afterCell.compositeMean,
      compositeDelta: afterCell.compositeMean - beforeCell.compositeMean,
      dimensions: diffDimensions(beforeCell.dimensions, afterCell.dimensions)
    });
  }
  for (const [key, afterCell] of afterMap) {
    if (!beforeMap.has(key)) added.push(afterCell);
  }
  return {
    beforeIndex: before.index,
    afterIndex: after.index,
    beforeSurfaceHash: before.surfaceHash,
    afterSurfaceHash: after.surfaceHash,
    surfaceChanged: before.surfaceHash !== after.surfaceHash,
    matched,
    removed,
    added,
    compositeBefore: before.compositeMean,
    compositeAfter: after.compositeMean,
    compositeDelta: after.compositeMean - before.compositeMean,
    costUsdBefore: before.costUsd,
    costUsdAfter: after.costUsd,
    costUsdDelta: after.costUsd - before.costUsd,
    durationMsBefore: before.durationMs,
    durationMsAfter: after.durationMs,
    durationMsDelta: after.durationMs - before.durationMs
  };
}
function winnerOf(run) {
  if (run.generations.length === 0) return null;
  let winner = run.generations[0];
  for (const gen of run.generations) {
    if (gen.index > winner.index) winner = gen;
  }
  return winner;
}
function diffRuns(before, after) {
  const beforeWinner = winnerOf(before);
  const afterWinner = winnerOf(after);
  const baselineDiff = before.baseline && after.baseline ? diffGenerations(before.baseline, after.baseline) : null;
  const winnersDiff = beforeWinner && afterWinner ? diffGenerations(beforeWinner, afterWinner) : null;
  const beforeLift = before.holdoutLift ?? null;
  const afterLift = after.holdoutLift ?? null;
  return {
    beforeRunId: before.runId,
    afterRunId: after.runId,
    beforeTimestamp: before.timestamp,
    afterTimestamp: after.timestamp,
    beforeGateDecision: before.gateDecision ?? null,
    afterGateDecision: after.gateDecision ?? null,
    beforeHoldoutLift: beforeLift,
    afterHoldoutLift: afterLift,
    holdoutLiftDelta: beforeLift !== null && afterLift !== null ? afterLift - beforeLift : null,
    beforeTotalCostUsd: before.totalCostUsd,
    afterTotalCostUsd: after.totalCostUsd,
    totalCostUsdDelta: after.totalCostUsd - before.totalCostUsd,
    beforeTotalDurationMs: before.totalDurationMs,
    afterTotalDurationMs: after.totalDurationMs,
    totalDurationMsDelta: after.totalDurationMs - before.totalDurationMs,
    baselineDiff,
    winnersDiff
  };
}
function diffRunBaselineToWinner(run) {
  if (!run.baseline) return null;
  const winner = winnerOf(run);
  if (!winner || winner.index === run.baseline.index) return null;
  return diffGenerations(run.baseline, winner);
}

// src/contract/intake/agent-trace.ts
function rangeLines(r) {
  return Math.max(0, r.end_line - r.start_line + 1);
}
function parseAgentTrace(records) {
  const acc = /* @__PURE__ */ new Map();
  for (const record of records) {
    const sha = record.vcs?.revision;
    if (!sha) continue;
    let a = acc.get(sha);
    if (!a) {
      a = {
        models: /* @__PURE__ */ new Set(),
        tools: /* @__PURE__ */ new Set(),
        files: /* @__PURE__ */ new Set(),
        conversationCount: 0,
        lineCount: 0,
        humanInvolved: false
      };
      acc.set(sha, a);
    }
    if (record.tool?.name) a.tools.add(record.tool.name);
    for (const file of record.files ?? []) {
      a.files.add(file.path);
      for (const conv of file.conversations ?? []) {
        a.conversationCount += 1;
        for (const range of conv.ranges ?? []) {
          const contributor = range.contributor ?? conv.contributor;
          a.lineCount += rangeLines(range);
          if (!contributor) continue;
          if (contributor.type === "human" || contributor.type === "mixed") {
            a.humanInvolved = true;
          }
          if ((contributor.type === "ai" || contributor.type === "mixed") && contributor.model_id) {
            a.models.add(contributor.model_id);
          }
        }
      }
    }
  }
  const index = /* @__PURE__ */ new Map();
  for (const [sha, a] of acc) {
    index.set(sha, {
      commitSha: sha,
      aiModels: [...a.models].sort(),
      tools: [...a.tools].sort(),
      conversationCount: a.conversationCount,
      fileCount: a.files.size,
      lineCount: a.lineCount,
      humanInvolved: a.humanInvolved
    });
  }
  return index;
}
function partitionRunsByAuthoringModel(runs, index) {
  const byModel = /* @__PURE__ */ new Map();
  const unattributed = [];
  for (const run of runs) {
    const provenance = index.get(run.commitSha);
    if (!provenance || provenance.aiModels.length === 0) {
      unattributed.push(run);
      continue;
    }
    for (const model of provenance.aiModels) {
      const cohort = byModel.get(model) ?? [];
      cohort.push(run);
      byModel.set(model, cohort);
    }
  }
  return { byModel, unattributed };
}

// src/contract/intake/feedback-table.ts
function fromFeedbackTable(opts) {
  const { ratings, meta = [], scale, emitRaterScores = true } = opts;
  const metaByRun = new Map(meta.map((m) => [m.runId, m]));
  const normalise = (rating) => {
    if (typeof rating === "boolean") return rating ? 1 : 0;
    if (!Number.isFinite(rating)) return Number.NaN;
    if (!scale) return rating;
    const { min, max } = scale;
    if (max === min) return rating;
    return (rating - min) / (max - min);
  };
  const byRun = /* @__PURE__ */ new Map();
  for (const row of ratings) {
    const list = byRun.get(row.runId) ?? [];
    list.push(row);
    byRun.set(row.runId, list);
  }
  const runs = [];
  const raterScores = [];
  for (const [runId, rowsForRun] of byRun) {
    const normalised = rowsForRun.map((r) => ({ rater: r.rater, score: normalise(r.rating) })).filter((r) => Number.isFinite(r.score));
    if (normalised.length === 0) continue;
    const meanScore = normalised.reduce((s, r) => s + r.score, 0) / normalised.length;
    const runMeta = metaByRun.get(runId) ?? { runId };
    const judgeScores = {
      perJudge: Object.fromEntries(normalised.map((r) => [r.rater, { rating: r.score }])),
      perDimMean: { rating: meanScore },
      composite: meanScore
    };
    const outcome = {
      // Feedback corpora ARE the holdout signal — score lands on
      // `holdoutScore` so downstream substrate primitives (`paretoChart`,
      // promotion gates) read it correctly by default.
      holdoutScore: meanScore,
      raw: Object.fromEntries(normalised.map((r) => [`rater:${r.rater}`, r.score])),
      judgeScores
    };
    runs.push({
      runId,
      experimentId: runMeta.experimentId ?? "feedback-corpus",
      candidateId: runMeta.candidateId ?? runId,
      seed: 0,
      model: runMeta.model ?? "unknown@unknown",
      promptHash: runMeta.promptHash ?? "sha256:unknown",
      configHash: runMeta.configHash ?? "sha256:unknown",
      commitSha: runMeta.commitSha ?? "unknown",
      wallMs: runMeta.wallMs ?? 0,
      costUsd: runMeta.costUsd ?? 0,
      tokenUsage: { input: 0, output: 0 },
      outcome,
      splitTag: runMeta.splitTag ?? "holdout"
    });
    if (emitRaterScores) {
      for (const r of normalised) raterScores.push({ runId, rater: r.rater, score: r.score });
    }
  }
  return { runs, raterScores };
}

// src/contract/intake/otel-spans.ts
var SCORE_KEYS = ["tangle.score", "eval.score", "score"];
var MODEL_KEYS = ["tangle.model", ...LLM_MODEL_ATTR_KEYS, "model"];
var PROMPT_HASH_KEYS = ["tangle.prompt_hash", "prompt.hash"];
var CONFIG_HASH_KEYS = ["tangle.config_hash", "config.hash"];
function fromOtelSpans(opts) {
  const { spans, defaultSplit = "holdout", experimentId = "otel-corpus" } = opts;
  const grouped = groupSpans(spans);
  const runs = [];
  for (const [groupKey, groupSpans2] of grouped) {
    const root = findRoot(groupSpans2);
    if (!root) continue;
    const measurements = summarizeExecutionMeasurements(
      groupSpans2.map((span) => ({
        id: span.spanId,
        ...span.parentSpanId ? { parentId: span.parentSpanId } : {},
        attributes: span.attributes,
        modelCall: isExplicitModelCall(span),
        aggregate: isExplicitAggregate(span)
      }))
    );
    const callSpanIds = new Set(measurements.callSpanIds);
    const callSpans = groupSpans2.filter((span) => callSpanIds.has(span.spanId));
    const wallMs = Math.max(0, (root.endTimeUnixNano - root.startTimeUnixNano) / 1e6);
    const model = readAttrString(callSpans, MODEL_KEYS) ?? readAttrString(groupSpans2, MODEL_KEYS) ?? "unknown@unknown";
    const capturedCost = (measurements.cost.complete ? measurements.cost.value : void 0) ?? measurements.aggregate?.costUsd;
    const costUsd = capturedCost ?? 0;
    const promptHash = readAttrString(groupSpans2, PROMPT_HASH_KEYS) ?? "sha256:unknown";
    const configHash = readAttrString(groupSpans2, CONFIG_HASH_KEYS) ?? "sha256:unknown";
    const score = readAttrNumber(groupSpans2, SCORE_KEYS);
    const rawNumeric = collectNumericAttrs(groupSpans2);
    rawNumeric.error_span_count = groupSpans2.filter((span) => span.status?.code === "ERROR").length;
    rawNumeric.llm_span_count = measurements.modelCallCount;
    if (measurements.cost.value !== void 0 && !measurements.cost.complete) {
      rawNumeric.partial_observed_cost_usd = measurements.cost.value;
    }
    recordAggregateMeasurements(rawNumeric, measurements.aggregate);
    const judgeScores = score !== void 0 ? {
      perJudge: { "otel-derived": { score } },
      perDimMean: { score },
      composite: score
    } : void 0;
    const errorSpan = groupSpans2.find((s) => s.status?.code === "ERROR");
    const outcome = {
      ...opts.defaultSplit === "search" ? { searchScore: score } : { holdoutScore: score },
      raw: rawNumeric,
      ...judgeScores ? { judgeScores } : {}
    };
    runs.push({
      runId: groupKey,
      experimentId,
      candidateId: root.attributes["tangle.candidateId"] ?? "otel-default",
      seed: 0,
      model,
      promptHash,
      configHash,
      commitSha: root.attributes["tangle.commit_sha"] ?? "unknown",
      wallMs,
      costUsd,
      costProvenance: capturedCost === void 0 ? { kind: "uncaptured", usd: null } : { kind: "observed", usd: capturedCost },
      tokenUsage: measurements.tokenUsage,
      outcome,
      splitTag: defaultSplit,
      ...errorSpan ? { failureMode: errorSpan.name } : {}
    });
  }
  return runs;
}
function isExplicitModelCall(span) {
  const kind = span.attributes["openinference.span.kind"];
  if (typeof kind === "string") return kind.toUpperCase() === "LLM";
  const spanType = span.attributes["span.type"];
  return typeof spanType === "string" && spanType.toLowerCase() === "llm_request" || span.name.toLowerCase().includes("llm") || readAttrString([span], MODEL_KEYS) !== void 0 || typeof span.attributes["gen_ai.operation.name"] === "string";
}
function isExplicitAggregate(span) {
  const kind = span.attributes["openinference.span.kind"];
  return typeof kind === "string" && kind.toUpperCase() !== "LLM";
}
function groupSpans(spans) {
  const m = /* @__PURE__ */ new Map();
  for (const span of spans) {
    const key = span["tangle.runId"] ?? span.traceId;
    const list = m.get(key) ?? [];
    list.push(span);
    m.set(key, list);
  }
  return m;
}
function findRoot(group) {
  return group.find((s) => !s.parentSpanId) ?? group[0];
}
function readAttrString(spans, keys) {
  for (const span of spans) {
    for (const key of keys) {
      const v = span.attributes[key];
      if (typeof v === "string" && v.length > 0) return v;
    }
  }
  return void 0;
}
function readAttrNumber(spans, keys) {
  for (const span of spans) {
    for (const key of keys) {
      const v = span.attributes[key];
      if (typeof v === "number" && Number.isFinite(v)) return v;
      if (typeof v === "string") {
        const parsed = Number(v);
        if (Number.isFinite(parsed)) return parsed;
      }
    }
  }
  return void 0;
}
function collectNumericAttrs(spans) {
  const raw = {};
  for (const span of spans) {
    for (const [k, v] of Object.entries(span.attributes)) {
      if (typeof v === "number" && Number.isFinite(v)) raw[k] = v;
    }
  }
  return raw;
}
export {
  FileSystemOutcomeStore,
  InMemoryOutcomeStore,
  REFERENCE_EQUIVALENCE_INPUT_LIMITS,
  REFERENCE_EQUIVALENCE_JUDGE_VERSION,
  SelfImproveRunError,
  analyzeRuns,
  buildDefaultAnalystRegistry,
  buildEvidenceVector,
  campaignSplitDigest,
  composeGate,
  createChatClient,
  createReferenceEquivalenceJudge,
  defaultProductionGate,
  defineAgentEval,
  diffGenerations,
  diffRunBaselineToWinner,
  diffRuns,
  evalReportingSuite,
  evolutionaryProposer,
  fromClaudeCodeSession,
  fromCodexSession,
  fromFeedbackTable,
  fromKimiCodeSession,
  fromOpenCodeSession,
  fromOtelSpans,
  fromPiSession,
  fromPigraphSession,
  fromRunRecordDir,
  fsCampaignStorage,
  gepaProposer,
  heldOutGate,
  inMemoryCampaignStorage,
  measuredComparisonFromCandidateExperiment,
  observeCodeAgentSession,
  paretoPolicy,
  paretoSignificanceGate,
  parseAgentTrace,
  parseCodeAgentJsonl,
  partitionRunsByAuthoringModel,
  runCampaign,
  runCandidateExperiment,
  runEval,
  runImprovementLoop,
  runReferenceEquivalenceJudge,
  sealCandidateBenchmarkSuite,
  sealCandidateBenchmarkTask,
  sealCandidateExperiment,
  selfImprove,
  summarizeExecution,
  verifyCandidateBenchmarkSuite,
  verifyCandidateBenchmarkSuiteInputs,
  verifyCandidateBenchmarkTask,
  verifyCandidateExperiment,
  verifyCandidateExperimentComparison
};
//# sourceMappingURL=index.js.map