@tangle-network/agent-eval 0.65.0 → 0.67.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/dist/adapters/otel.d.ts +1 -1
- package/dist/campaign/index.d.ts +110 -6
- package/dist/campaign/index.js +26 -19
- package/dist/campaign/index.js.map +1 -1
- package/dist/{chunk-7TPYV2ER.js → chunk-6XQIEUQ2.js} +140 -7
- package/dist/chunk-6XQIEUQ2.js.map +1 -0
- package/dist/{chunk-HKINEDRZ.js → chunk-DFS3FEXO.js} +3 -2
- package/dist/chunk-DFS3FEXO.js.map +1 -0
- package/dist/chunk-MZ2IYGGN.js +592 -0
- package/dist/chunk-MZ2IYGGN.js.map +1 -0
- package/dist/{chunk-4ODZXQV2.js → chunk-NV2PF37Q.js} +645 -2
- package/dist/chunk-NV2PF37Q.js.map +1 -0
- package/dist/contract/index.d.ts +11 -9
- package/dist/contract/index.js +11 -12
- package/dist/contract/index.js.map +1 -1
- package/dist/hosted/index.d.ts +1 -1
- package/dist/hosted/index.js +1 -1
- package/dist/{index-CzhtwYBT.d.ts → index-DSEHMwvS.d.ts} +4 -2
- package/dist/index.d.ts +251 -7
- package/dist/index.js +292 -2
- package/dist/index.js.map +1 -1
- package/dist/openapi.json +1 -1
- package/dist/provenance-CChUqexv.d.ts +314 -0
- package/dist/{registry-DPly4_hZ.d.ts → registry-BGKyX6bw.d.ts} +2 -2
- package/dist/release-report-CN8hJlhk.d.ts +233 -0
- package/dist/reporting.d.ts +4 -3
- package/dist/{run-campaign-5J3ED2UJ.js → run-campaign-BVY3RGAZ.js} +2 -3
- package/dist/{provenance-lqyLpOYR.d.ts → run-improvement-loop-BKpM5T4t.d.ts} +51 -329
- package/dist/statistics-B7yCbi9i.d.ts +253 -0
- package/dist/{types-DhqpAi_z.d.ts → types-Croy5h7V.d.ts} +1 -1
- package/package.json +1 -1
- package/dist/chunk-4ODZXQV2.js.map +0 -1
- package/dist/chunk-7TPYV2ER.js.map +0 -1
- package/dist/chunk-CZRKD2X2.js +0 -1104
- package/dist/chunk-CZRKD2X2.js.map +0 -1
- package/dist/chunk-E22YUOAL.js +0 -111
- package/dist/chunk-E22YUOAL.js.map +0 -1
- package/dist/chunk-HKINEDRZ.js.map +0 -1
- package/dist/release-report-DGoeObZT.d.ts +0 -484
- /package/dist/{run-campaign-5J3ED2UJ.js.map → run-campaign-BVY3RGAZ.js.map} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/campaign/skill-patch.ts","../../src/campaign/drivers/skill-opt.ts","../../src/campaign/labeled-store/fs-adapter.ts","../../src/campaign/presets/run-skill-opt.ts","../../src/campaign/presets/compare-drivers.ts","../../src/campaign/presets/run-profile-matrix.ts","../../src/campaign/worktree/index.ts"],"sourcesContent":["/**\n * @experimental\n *\n * SkillOpt patch primitives (Microsoft, arXiv:2605.23904 — \"Executive\n * Strategy for Self-Evolving Agent Skills\"). Where GEPA regenerates a surface\n * by reflection, SkillOpt emits BOUNDED, anchored edits to ONE skill document\n * — add / delete / replace — and accepts an edit only if it strictly improves\n * a held-out score. Bounded edits are the \"textual learning rate\": small,\n * reversible, and cheap to accept/reject, so a good rule introduced earlier is\n * not overwritten by a later sweeping rewrite.\n *\n * This module applies a patch deterministically and reports, per op, what\n * applied and what could not (a missing anchor is a rejected op, never a\n * silently dropped one). Pure, no I/O.\n */\n\n/** A single bounded edit against a skill surface.\n * - `add` — insert `text` after the first line containing `after`\n * (append to the end when `after` is absent/empty).\n * - `delete` — remove the first line containing `anchor`.\n * - `replace` — replace the first line containing `anchor` with `text`.\n * `text` may be multi-line; it is spliced in as multiple lines. Anchors match\n * the FIRST line that contains the substring (deterministic; SkillOpt is\n * expected to anchor on unique text). */\nexport type SkillPatchOp =\n | { op: 'add'; after?: string; text: string }\n | { op: 'delete'; anchor: string }\n | { op: 'replace'; anchor: string; text: string }\n\n/** A named, attributable bundle of ops the optimizer proposes as one edit. */\nexport interface SkillPatch {\n label: string\n rationale: string\n ops: SkillPatchOp[]\n}\n\nexport interface SkillPatchRejection {\n op: SkillPatchOp\n reason: string\n}\n\nexport interface ApplySkillPatchResult {\n surface: string\n /** Count of ops that mutated the surface. */\n applied: number\n /** Ops that could not apply (unanchored / empty), with the reason. The\n * surface still reflects every APPLIED op — partial application is honest,\n * and the caller decides whether a partial patch is worth scoring. */\n rejected: SkillPatchRejection[]\n}\n\n/**\n * Apply a SkillOpt patch to a text surface. Ops apply in array order against\n * the evolving line buffer (an `add after X` followed by a `delete X` sees the\n * inserted lines). A missing anchor rejects only that op; the rest still apply.\n */\nexport function applySkillPatch(surface: string, patch: SkillPatch): ApplySkillPatchResult {\n let lines = surface.split('\\n')\n let applied = 0\n const rejected: SkillPatchRejection[] = []\n const findLine = (anchor: string): number => lines.findIndex((l) => l.includes(anchor))\n\n for (const op of patch.ops) {\n if (op.op === 'add') {\n if (typeof op.text !== 'string' || op.text.trim() === '') {\n rejected.push({ op, reason: 'empty add text' })\n continue\n }\n const insert = op.text.split('\\n')\n if (op.after === undefined || op.after === '') {\n lines = [...lines, ...insert]\n applied++\n continue\n }\n const idx = findLine(op.after)\n if (idx === -1) {\n rejected.push({ op, reason: `add anchor not found: ${truncate(op.after)}` })\n continue\n }\n lines = [...lines.slice(0, idx + 1), ...insert, ...lines.slice(idx + 1)]\n applied++\n } else if (op.op === 'delete') {\n const idx = findLine(op.anchor)\n if (idx === -1) {\n rejected.push({ op, reason: `delete anchor not found: ${truncate(op.anchor)}` })\n continue\n }\n lines = [...lines.slice(0, idx), ...lines.slice(idx + 1)]\n applied++\n } else {\n // replace\n const idx = findLine(op.anchor)\n if (idx === -1) {\n rejected.push({ op, reason: `replace anchor not found: ${truncate(op.anchor)}` })\n continue\n }\n if (typeof op.text !== 'string') {\n rejected.push({ op, reason: 'replace text missing' })\n continue\n }\n lines = [...lines.slice(0, idx), ...op.text.split('\\n'), ...lines.slice(idx + 1)]\n applied++\n }\n }\n\n return { surface: lines.join('\\n'), applied, rejected }\n}\n\n/** Total ops in a patch — the edit-budget axis (SkillOpt's \"textual learning\n * rate\" caps this per epoch). */\nexport function patchEditCount(patch: SkillPatch): number {\n return patch.ops.length\n}\n\nfunction truncate(s: string, max = 48): string {\n return s.length <= max ? s : `${s.slice(0, max)}…`\n}\n","/**\n * @experimental\n *\n * `skillOptDriver` — a patch-mode `ImprovementDriver` implementing SkillOpt\n * (Microsoft, arXiv:2605.23904). Where `gepaDriver` regenerates the whole\n * surface by reflection, SkillOpt proposes BOUNDED, anchored edits\n * (add/delete/replace) to ONE skill document, so a good rule introduced\n * earlier is not clobbered by a later sweeping rewrite. The edit budget is the\n * paper's \"textual learning rate\"; a rejected-edit buffer + a slow-update\n * meta-note steer the optimizer away from dead ends.\n *\n * This module is the PROPOSER — the LLM call that turns evidence into\n * structured patches. The accept-only-if-held-out-improves loop, the budget\n * annealing, and the rejected buffer live in the `runSkillOpt` preset, which\n * owns the epoch hill-climb. The driver also conforms to `ImprovementDriver`\n * (`propose` applies its patches to the current surface and returns the\n * candidate surfaces) so it is a drop-in for `runOptimization` and a fair\n * entrant in `compareDrivers`.\n */\n\nimport { callLlm, type LlmClientOptions } from '../../llm-client'\nimport { applySkillPatch, type SkillPatch, type SkillPatchOp } from '../skill-patch'\nimport type { ImprovementDriver, ProposeContext, ProposedCandidate } from '../types'\n\nconst SKILLOPT_SYSTEM =\n 'You are a SkillOpt optimizer. You improve ONE skill document by proposing ' +\n 'BOUNDED, anchored edits — never a full rewrite. Output ONLY a JSON object of ' +\n 'shape {\"patches\":[{\"label\":string,\"rationale\":string,\"ops\":[op,...]}]} where ' +\n 'each op is one of: {\"op\":\"add\",\"after\":<exact substring of an existing line, ' +\n 'or omit to append>,\"text\":<new line(s)>}, {\"op\":\"delete\",\"anchor\":<exact ' +\n 'substring of the line to remove>}, {\"op\":\"replace\",\"anchor\":<exact substring ' +\n 'of the line to replace>,\"text\":<replacement line(s)>}. Anchors MUST be ' +\n 'verbatim substrings of lines that exist in the document. No prose outside JSON.'\n\n/** Evidence the optimizer reflects on: where the current surface is weakest.\n * Computed by the caller (the preset uses a TRAIN campaign so proposals never\n * see the held-out split; the generic loop derives it from history). */\nexport interface SkillOptEvidence {\n /** Lowest-scoring scenarios (drives WHICH behavior to patch). */\n weakScenarios: Array<{ scenarioId: string; composite: number }>\n /** Lowest-scoring judge dimensions (drives WHAT to patch for). */\n weakDimensions: Array<{ dimension: string; score: number }>\n}\n\n/** A patch that was tried and not accepted — fed back to the model so it does\n * not re-propose a dead end (SkillOpt's rejected-edit buffer). */\nexport interface RejectedEdit {\n label: string\n rationale: string\n reason: string\n}\n\nexport interface ProposePatchesArgs {\n surface: string\n evidence: SkillOptEvidence\n /** Max ops per patch this round (the annealed textual learning rate). */\n editBudget: number\n rejectedBuffer: RejectedEdit[]\n /** Slow-update meta guidance accumulated across epochs. */\n metaNote?: string\n /** How many candidate patches to propose. */\n count: number\n signal: AbortSignal\n}\n\nexport interface SkillOptDriverOptions {\n llm: LlmClientOptions\n model: string\n /** What the skill document governs — orients the prompt. */\n target: string\n /** Default ops-per-patch cap when used as a bare `ImprovementDriver`. The\n * `runSkillOpt` preset overrides this per epoch as it anneals. Default 3. */\n editBudget?: number\n temperature?: number\n maxTokens?: number\n /** Top-K weak scenarios/dimensions surfaced as evidence. Default 3. */\n evidenceK?: number\n}\n\nexport interface SkillOptDriver extends ImprovementDriver {\n /** Patch-native path used by `runSkillOpt` (the SkillOpt epoch loop owns\n * acceptance/budget/buffer). Returns structured patches, NOT surfaces. */\n proposePatches(args: ProposePatchesArgs): Promise<SkillPatch[]>\n}\n\nexport function skillOptDriver(opts: SkillOptDriverOptions): SkillOptDriver {\n const evidenceK = opts.evidenceK ?? 3\n const defaultBudget = opts.editBudget ?? 3\n\n async function proposePatches(args: ProposePatchesArgs): Promise<SkillPatch[]> {\n const userPrompt = buildPatchPrompt({\n target: opts.target,\n surface: args.surface,\n evidence: args.evidence,\n editBudget: args.editBudget,\n rejectedBuffer: args.rejectedBuffer,\n metaNote: args.metaNote,\n count: args.count,\n })\n const result = await callLlm(\n {\n model: opts.model,\n messages: [\n { role: 'system', content: SKILLOPT_SYSTEM },\n { role: 'user', content: userPrompt },\n ],\n jsonMode: true,\n temperature: opts.temperature ?? 0.6,\n maxTokens: opts.maxTokens ?? 4000,\n },\n opts.llm,\n )\n return parseSkillPatchResponse(result.content, args.count, args.editBudget)\n }\n\n return {\n kind: 'skill-opt',\n proposePatches,\n async propose(ctx: ProposeContext): Promise<ProposedCandidate[]> {\n if (typeof ctx.currentSurface !== 'string') {\n throw new Error(\n 'skillOptDriver: surface must be a string skill document (got a CodeSurface). SkillOpt patches text.',\n )\n }\n const surface = ctx.currentSurface\n const patches = await proposePatches({\n surface,\n evidence: evidenceFromHistory(ctx, evidenceK),\n editBudget: defaultBudget,\n rejectedBuffer: [],\n count: ctx.populationSize,\n signal: ctx.signal,\n })\n const out: ProposedCandidate[] = []\n const seen = new Set<string>()\n for (const patch of patches) {\n const { surface: candidate, applied } = applySkillPatch(surface, patch)\n if (applied === 0 || candidate === surface || seen.has(candidate)) continue\n seen.add(candidate)\n out.push({ surface: candidate, label: patch.label, rationale: patch.rationale })\n if (out.length >= ctx.populationSize) break\n }\n return out\n },\n }\n}\n\n/** Derive evidence from the loop's generation history (generic-driver path):\n * the prior best candidate's worst scenarios + weakest dimensions. Empty on\n * generation 0. */\nfunction evidenceFromHistory(ctx: ProposeContext, k: number): SkillOptEvidence {\n const last = ctx.history.at(-1)\n if (!last || last.candidates.length === 0) return { weakScenarios: [], weakDimensions: [] }\n const best = [...last.candidates].sort((a, b) => b.composite - a.composite)[0]\n if (!best) return { weakScenarios: [], weakDimensions: [] }\n const weakScenarios = [...best.scenarios].sort((a, b) => a.composite - b.composite).slice(0, k)\n const weakDimensions = Object.entries(best.dimensions)\n .sort((a, b) => a[1] - b[1])\n .slice(0, k)\n .map(([dimension, score]) => ({ dimension, score }))\n return { weakScenarios, weakDimensions }\n}\n\nfunction buildPatchPrompt(args: {\n target: string\n surface: string\n evidence: SkillOptEvidence\n editBudget: number\n rejectedBuffer: RejectedEdit[]\n metaNote?: string\n count: number\n}): string {\n const lines: string[] = [\n `Skill document governs: ${args.target}.`,\n '',\n 'Current skill document:',\n '```',\n args.surface,\n '```',\n '',\n `Propose ${args.count} candidate patch(es). Each patch is a SMALL bundle of`,\n `at most ${args.editBudget} op(s). Anchors must be verbatim substrings of`,\n 'existing lines. Prefer adding a specific missing rule or sharpening a vague',\n 'one over deleting; never rewrite the whole document.',\n ]\n if (args.evidence.weakScenarios.length > 0) {\n lines.push(\n '',\n 'Weakest scenarios (patch to fix these):',\n ...args.evidence.weakScenarios.map((s) => `- ${s.scenarioId} (${s.composite.toFixed(2)})`),\n )\n }\n if (args.evidence.weakDimensions.length > 0) {\n lines.push(\n '',\n 'Weakest dimensions (what to improve):',\n ...args.evidence.weakDimensions.map((d) => `- ${d.dimension} (${d.score.toFixed(2)})`),\n )\n }\n if (args.rejectedBuffer.length > 0) {\n lines.push(\n '',\n 'Already tried and REJECTED (do not repeat or restate these edits):',\n ...args.rejectedBuffer.map((e) => `- ${e.label}: ${e.rationale} — ${e.reason}`),\n )\n }\n if (args.metaNote) {\n lines.push('', `Strategy note from prior epochs: ${args.metaNote}`)\n }\n return lines.join('\\n')\n}\n\n/** Parse + validate the patch response. Throws `SkillPatchParseError` when the\n * response is not valid JSON at all (a router/model failure the caller must\n * see — never a silent no-op epoch). Returns `[]` only for the legitimate\n * \"valid JSON, zero usable patches\" case. Malformed ops within a patch are\n * dropped (not silently mutated); each patch is truncated to the edit budget. */\nexport class SkillPatchParseError extends Error {\n constructor(message: string) {\n super(message)\n this.name = 'SkillPatchParseError'\n }\n}\n\nexport function parseSkillPatchResponse(\n raw: string,\n maxPatches: number,\n editBudget: number,\n): SkillPatch[] {\n let text = raw.trim()\n if (text.startsWith('```')) text = text.replace(/^```(?:json)?\\n?/, '').replace(/\\n?```$/, '')\n const start = text.indexOf('{')\n const end = text.lastIndexOf('}')\n if (start < 0 || end <= start) {\n throw new SkillPatchParseError(\n `parseSkillPatchResponse: response was not valid JSON (no object found): ${snippet(raw)}`,\n )\n }\n let parsed: { patches?: unknown }\n try {\n parsed = JSON.parse(text.slice(start, end + 1))\n } catch (err) {\n throw new SkillPatchParseError(\n `parseSkillPatchResponse: response was not valid JSON (${\n err instanceof Error ? err.message : String(err)\n }): ${snippet(raw)}`,\n )\n }\n const rawPatches = Array.isArray(parsed.patches) ? parsed.patches : []\n const out: SkillPatch[] = []\n for (const rp of rawPatches) {\n if (typeof rp !== 'object' || rp === null) continue\n const obj = rp as Record<string, unknown>\n const ops = Array.isArray(obj.ops) ? obj.ops.map(normalizeOp).filter(isOp) : []\n if (ops.length === 0) continue\n out.push({\n label: typeof obj.label === 'string' ? obj.label : 'patch',\n rationale: typeof obj.rationale === 'string' ? obj.rationale : '',\n ops: ops.slice(0, editBudget),\n })\n if (out.length >= maxPatches) break\n }\n return out\n}\n\nfunction normalizeOp(raw: unknown): SkillPatchOp | null {\n if (typeof raw !== 'object' || raw === null) return null\n const o = raw as Record<string, unknown>\n if (o.op === 'add') {\n if (typeof o.text !== 'string') return null\n const op: SkillPatchOp = { op: 'add', text: o.text }\n if (typeof o.after === 'string') op.after = o.after\n return op\n }\n if (o.op === 'delete') {\n if (typeof o.anchor !== 'string') return null\n return { op: 'delete', anchor: o.anchor }\n }\n if (o.op === 'replace') {\n if (typeof o.anchor !== 'string' || typeof o.text !== 'string') return null\n return { op: 'replace', anchor: o.anchor, text: o.text }\n }\n return null\n}\n\nfunction isOp(op: SkillPatchOp | null): op is SkillPatchOp {\n return op !== null\n}\n\nfunction snippet(s: string, max = 120): string {\n const t = s.trim().replace(/\\s+/g, ' ')\n return t.length <= max ? t : `${t.slice(0, max)}…`\n}\n","/**\n * @experimental\n *\n * Filesystem `LabeledScenarioStore` adapter. The default capture sink for\n * traces + eval artifacts. Production deployments typically swap for a\n * Turso/SQLite adapter (same interface).\n *\n * Records land as one JSONL file per source under `<root>/<source>.jsonl`.\n * Each line is a `LabeledScenarioRecord`. Append-only — no in-place edits.\n *\n * Safety properties enforced at write-time:\n *\n * - **Provenance required**: writes without `source`, `sourceVersionHash`,\n * `capturedAt`, `redactionStatus` are rejected. Closes the alignment\n * reviewer's data-poisoning gap.\n * - **Per-source rate limits**: optional `rateLimitBucket` + `maxWritesPerMinute`\n * stops a single tenant/source from flooding the store.\n *\n * Safety properties enforced at sample-time:\n *\n * - **Required split + capturedBefore**: substrate refuses to sample without\n * an explicit `split` ('train' | 'test') AND a temporal cutoff. Eliminates\n * accidental train/test contamination.\n * - **Default training-source filter**: when the store is sampled with\n * `split: 'train'`, production-trace records are EXCLUDED unless the\n * caller passes `filter.source: 'production-trace'` explicitly. Closes\n * the contamination-by-default gap flagged by the senior eval engineer.\n */\n\nimport { createHash } from 'node:crypto'\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { join } from 'node:path'\nimport type {\n LabeledScenarioRecord,\n LabeledScenarioSampleArgs,\n LabeledScenarioSource,\n LabeledScenarioStore,\n LabeledScenarioWrite,\n LabelTrust,\n} from '../types'\nimport { labelTrustRank } from '../types'\n\nexport interface FsLabeledScenarioStoreOptions {\n /** Root directory for JSONL files. Created if missing. */\n root: string\n /** Per-source rate limit. When set, writes exceeding the cap are rejected\n * with a typed error. Default: no limit. */\n maxWritesPerMinutePerBucket?: number\n /** Test seam — override `Date.now()` for deterministic tests. */\n now?: () => number\n}\n\nexport class LabeledScenarioStoreError extends Error {\n constructor(\n public readonly code: string,\n message: string,\n ) {\n super(message)\n this.name = 'LabeledScenarioStoreError'\n }\n}\n\ninterface RateLimitState {\n bucket: string\n windowStartMs: number\n count: number\n}\n\nexport class FsLabeledScenarioStore implements LabeledScenarioStore {\n private readonly now: () => number\n private readonly rateLimits = new Map<string, RateLimitState>()\n\n constructor(private readonly options: FsLabeledScenarioStoreOptions) {\n if (!existsSync(options.root)) mkdirSync(options.root, { recursive: true })\n this.now = options.now ?? Date.now\n }\n\n async observe(write: LabeledScenarioWrite): Promise<void> {\n this.assertProvenance(write)\n this.assertRateLimit(write)\n const record = this.toRecord(write)\n const path = this.pathForSource(write.source)\n const line = `${JSON.stringify(record)}\\n`\n // Append atomically. For high-throughput a writev-friendly buffered\n // implementation lands in the Turso adapter; FS adapter is for tests +\n // local dev + small workloads.\n appendLine(path, line)\n }\n\n async sample(args: LabeledScenarioSampleArgs): Promise<LabeledScenarioRecord[]> {\n if (!args.split) {\n throw new LabeledScenarioStoreError(\n 'split_required',\n 'sample() requires an explicit `split` (train | test) — substrate refuses ambiguous reads',\n )\n }\n if (!args.capturedBefore) {\n throw new LabeledScenarioStoreError(\n 'capturedBefore_required',\n 'sample() requires an explicit `capturedBefore` timestamp for temporal-split discipline',\n )\n }\n\n const all: LabeledScenarioRecord[] = []\n for (const source of ALL_SOURCES) {\n // Default training-source filter: when sampling train, EXCLUDE\n // production-trace records unless the caller asks for them.\n if (args.split === 'train' && source === 'production-trace') {\n const explicit = sourceFilterContains(args.filter?.source, 'production-trace')\n if (!explicit) continue\n }\n const path = this.pathForSource(source)\n if (!existsSync(path)) continue\n const lines = readFileSync(path, 'utf8').split('\\n').filter(Boolean)\n for (const line of lines) {\n let record: LabeledScenarioRecord\n try {\n record = JSON.parse(line) as LabeledScenarioRecord\n } catch {\n continue\n }\n if (!matchesFilter(record, args, source)) continue\n all.push(record)\n }\n }\n\n // Deterministic order: by capturedAt ascending, then recordHash.\n all.sort((a, b) => {\n if (a.capturedAt !== b.capturedAt) return a.capturedAt.localeCompare(b.capturedAt)\n return a.recordHash.localeCompare(b.recordHash)\n })\n\n return all.slice(0, args.count)\n }\n\n async size(): Promise<{\n train: number\n test: number\n bySource: Record<string, number>\n byTrust: Record<LabelTrust, number>\n }> {\n const bySource: Record<string, number> = {}\n const byTrust: Record<LabelTrust, number> = {\n unverified: 0,\n 'verified-signal': 0,\n 'human-rated': 0,\n }\n let total = 0\n for (const source of ALL_SOURCES) {\n const path = this.pathForSource(source)\n if (!existsSync(path)) {\n bySource[source] = 0\n continue\n }\n const lines = readFileSync(path, 'utf8').split('\\n').filter(Boolean)\n bySource[source] = lines.length\n total += lines.length\n for (const line of lines) {\n let trust: LabelTrust = 'unverified'\n try {\n trust = (JSON.parse(line) as LabeledScenarioRecord).labelTrust ?? 'unverified'\n } catch {\n // A malformed line counts as unverified — never silently gold.\n }\n byTrust[trust] += 1\n }\n }\n // FS adapter doesn't track split assignments per-record (split is\n // computed at sample-time based on `capturedBefore`). For size(), we\n // report `train`+`test` as the same total — split is a sampling concept.\n return { train: total, test: total, bySource, byTrust }\n }\n\n private assertProvenance(write: LabeledScenarioWrite): void {\n if (!write.source) {\n throw new LabeledScenarioStoreError(\n 'missing_source',\n 'LabeledScenarioWrite requires `source`',\n )\n }\n if (!write.sourceVersionHash || write.sourceVersionHash.length === 0) {\n throw new LabeledScenarioStoreError(\n 'missing_source_version',\n 'LabeledScenarioWrite requires `sourceVersionHash` (git sha or substrate version)',\n )\n }\n if (!write.capturedAt) {\n throw new LabeledScenarioStoreError(\n 'missing_captured_at',\n 'LabeledScenarioWrite requires `capturedAt` ISO timestamp',\n )\n }\n if (!write.redactionStatus) {\n throw new LabeledScenarioStoreError(\n 'missing_redaction_status',\n 'LabeledScenarioWrite requires explicit `redactionStatus` — raw / redacted-pii / redacted-secrets / fully-redacted',\n )\n }\n if (!ALL_SOURCES.includes(write.source)) {\n throw new LabeledScenarioStoreError(\n 'unknown_source',\n `LabeledScenarioWrite.source must be one of: ${ALL_SOURCES.join(', ')}`,\n )\n }\n }\n\n private assertRateLimit(write: LabeledScenarioWrite): void {\n const cap = this.options.maxWritesPerMinutePerBucket\n if (!cap || !write.rateLimitBucket) return\n const now = this.now()\n const windowMs = 60_000\n let state = this.rateLimits.get(write.rateLimitBucket)\n if (!state || now - state.windowStartMs >= windowMs) {\n state = { bucket: write.rateLimitBucket, windowStartMs: now, count: 0 }\n this.rateLimits.set(write.rateLimitBucket, state)\n }\n if (state.count >= cap) {\n throw new LabeledScenarioStoreError(\n 'rate_limit_exceeded',\n `LabeledScenarioStore: bucket ${write.rateLimitBucket} exceeded ${cap} writes/min`,\n )\n }\n state.count += 1\n }\n\n private toRecord(write: LabeledScenarioWrite): LabeledScenarioRecord {\n const recordHash = sha256(\n JSON.stringify({\n id: write.scenario.id,\n src: write.source,\n at: write.capturedAt,\n ver: write.sourceVersionHash,\n }),\n )\n // FS adapter assigns split at sample-time, but we cache a hint here\n // based on capturedAt vs the world's \"now\" — sampler overrides this.\n return {\n ...write,\n recordHash,\n split: 'train',\n }\n }\n\n private pathForSource(source: string): string {\n return join(this.options.root, `${source}.jsonl`)\n }\n}\n\nconst ALL_SOURCES: LabeledScenarioWrite['source'][] = [\n 'production-trace',\n 'eval-run',\n 'manual',\n 'red-team',\n 'synthetic',\n]\n\nfunction sourceFilterContains(\n filter: LabeledScenarioSource | LabeledScenarioSource[] | undefined,\n needle: LabeledScenarioSource,\n): boolean {\n if (!filter) return false\n if (Array.isArray(filter)) return filter.includes(needle)\n return filter === needle\n}\n\nfunction matchesFilter(\n record: LabeledScenarioRecord,\n args: LabeledScenarioSampleArgs,\n source: string,\n): boolean {\n // Temporal cutoff — train must be capturedAt < capturedBefore.\n if (args.split === 'train' && record.capturedAt >= args.capturedBefore) return false\n if (args.split === 'test' && record.capturedAt < args.capturedBefore) return false\n\n const f = args.filter\n if (!f) return true\n if (f.kind && record.scenario.kind !== f.kind) return false\n if (f.source) {\n const sources = Array.isArray(f.source) ? f.source : [f.source]\n if (!sources.includes(source as never)) return false\n }\n if (f.minComposite !== undefined || f.maxComposite !== undefined) {\n const composites = Object.values(record.judgeScores).map((s) => s.composite)\n const max = composites.length === 0 ? 0 : Math.max(...composites)\n if (f.minComposite !== undefined && max < f.minComposite) return false\n if (f.maxComposite !== undefined && max > f.maxComposite) return false\n }\n if (f.minTrust !== undefined && labelTrustRank(record.labelTrust) < labelTrustRank(f.minTrust)) {\n return false\n }\n return true\n}\n\nfunction sha256(input: string): string {\n return createHash('sha256').update(input).digest('hex').slice(0, 16)\n}\n\nfunction appendLine(path: string, line: string): void {\n if (existsSync(path)) {\n const existing = readFileSync(path, 'utf8')\n writeFileSync(path, existing + line)\n } else {\n writeFileSync(path, line)\n }\n}\n","/**\n * @experimental\n *\n * `runSkillOpt` — the SkillOpt epoch hill-climb (Microsoft, arXiv:2605.23904).\n * Unlike `runOptimization`'s population/promote-top-K search, SkillOpt is a\n * sequential, held-out-gated hill-climb on ONE skill document:\n *\n * each epoch:\n * 1. reflect on the CURRENT surface's weakest TRAIN scenarios/dimensions\n * (never the held-out split — proposals must not see the acceptance axis)\n * 2. propose ≤ `patchesPerEpoch` bounded patches (≤ `editBudget` ops each)\n * 3. apply each; score the candidate on the HELD-OUT split\n * 4. ACCEPT the first patch that STRICTLY improves the held-out composite;\n * otherwise push it to the rejected-edit buffer (fed back so the model\n * does not re-propose dead ends)\n * 5. anneal the edit budget down after consecutive rejections (the\n * \"textual learning rate\" decay); refresh the slow-update meta note\n * 6. stop at `maxEpochs` or after `patience` epochs with no acceptance\n *\n * The accept-only-if-held-out-improves rule is the same discipline as\n * `HeldOutGate`/`defaultProductionGate`, applied per edit instead of once at\n * the end — which is why the held-out composite is monotonically\n * non-decreasing and a regression can never ship. `runCampaign` is the\n * measurement; `applySkillPatch` applies the edits; `skillOptDriver` proposes\n * them.\n */\n\nimport type { RejectedEdit, SkillOptDriver, SkillOptEvidence } from '../drivers/skill-opt'\nimport { type RunCampaignOptions, runCampaign } from '../run-campaign'\nimport { campaignBreakdown, campaignMeanComposite } from '../score-utils'\nimport { applySkillPatch } from '../skill-patch'\nimport type { CampaignResult, DispatchContext, Scenario } from '../types'\n\nexport interface RunSkillOptOptions<TScenario extends Scenario, TArtifact>\n extends Omit<RunCampaignOptions<TScenario, TArtifact>, 'dispatch' | 'scenarios'> {\n /** The skill document being optimized. */\n baselineSurface: string\n /** Dispatcher taking the CURRENT skill surface + scenario → artifact. */\n dispatchWithSurface: (\n surface: string,\n scenario: TScenario,\n ctx: DispatchContext,\n ) => Promise<TArtifact>\n driver: SkillOptDriver\n /** Scenarios the optimizer reflects on for evidence. MUST be disjoint from\n * `holdoutScenarios` — proposals never see the acceptance axis. */\n trainScenarios: TScenario[]\n /** Held-out scenarios. An edit is accepted ONLY if it strictly improves the\n * mean composite here. */\n holdoutScenarios: TScenario[]\n maxEpochs: number\n /** Candidate patches proposed per epoch. Default 2. */\n patchesPerEpoch?: number\n /** Initial ops-per-patch cap (the textual learning rate). Default 3. */\n editBudget?: number\n /** Strict acceptance margin: accept iff the held-out composite improves by\n * MORE than this. Default 0 (any strict improvement). */\n minImprovement?: number\n /** Stop after this many consecutive epochs with no acceptance. Default =\n * `maxEpochs` (never early-stop). */\n patience?: number\n /** Shrink the edit budget by 1 after 2 consecutive rejected epochs (min 1).\n * Default true. */\n budgetAnneal?: boolean\n /** Cap on the rejected-edit buffer (most-recent kept). Default 12. */\n rejectedBufferSize?: number\n /** Refresh the slow-update meta note every N epochs. Default 2. 0 disables. */\n slowMetaEvery?: number\n /** Top-K weak scenarios/dimensions surfaced as evidence each epoch. Default 3. */\n evidenceK?: number\n /** Abort signal forwarded to the patch-proposing LLM calls. */\n signal?: AbortSignal\n}\n\nexport interface AcceptedEdit {\n epoch: number\n label: string\n rationale: string\n /** Held-out composite improvement vs the surface before this edit. */\n holdoutDelta: number\n}\n\nexport interface SkillOptEpochRecord {\n epoch: number\n editBudget: number\n proposed: number\n /** The accepted edit this epoch, or null if every proposal was rejected. */\n accepted: AcceptedEdit | null\n rejected: RejectedEdit[]\n /** Held-out composite of the CURRENT surface at the END of the epoch. */\n holdoutComposite: number\n}\n\nexport interface RunSkillOptResult {\n winnerSurface: string\n baselineHoldoutComposite: number\n winnerHoldoutComposite: number\n /** `winnerHoldoutComposite - baselineHoldoutComposite` — monotonically ≥ 0\n * by construction (only strictly-improving edits are accepted). */\n lift: number\n acceptedEdits: AcceptedEdit[]\n rejectedEdits: RejectedEdit[]\n epochsRun: number\n history: SkillOptEpochRecord[]\n /** Total cost across every scoring campaign (train evidence + holdout\n * acceptance) the hill-climb ran. */\n totalCostUsd: number\n}\n\nexport async function runSkillOpt<TScenario extends Scenario, TArtifact>(\n opts: RunSkillOptOptions<TScenario, TArtifact>,\n): Promise<RunSkillOptResult> {\n if (opts.trainScenarios.length === 0) throw new Error('runSkillOpt: trainScenarios is empty')\n if (opts.holdoutScenarios.length === 0) throw new Error('runSkillOpt: holdoutScenarios is empty')\n if (!opts.judges || opts.judges.length === 0) {\n throw new Error(\n 'runSkillOpt: at least one judge is required — scoring (and therefore acceptance) is meaningless without one, and would report a silent zero lift.',\n )\n }\n // train ∩ holdout must be empty: proposals reflect on TRAIN evidence, so any\n // overlap leaks the acceptance axis into the proposal (held-out contamination).\n const holdoutIds = new Set(opts.holdoutScenarios.map((s) => s.id))\n const overlap = opts.trainScenarios.filter((s) => holdoutIds.has(s.id)).map((s) => s.id)\n if (overlap.length > 0) {\n throw new Error(\n `runSkillOpt: trainScenarios and holdoutScenarios must be disjoint (overlap: [${overlap.join(\n ', ',\n )}]) — a shared scenario leaks the held-out acceptance axis into the proposal evidence.`,\n )\n }\n\n const patchesPerEpoch = opts.patchesPerEpoch ?? 2\n const initialBudget = opts.editBudget ?? 3\n const minImprovement = opts.minImprovement ?? 0\n if (minImprovement < 0) {\n throw new Error(\n 'runSkillOpt: minImprovement must be >= 0 — a negative threshold would accept held-out regressions, breaking the monotonic-lift contract.',\n )\n }\n const patience = opts.patience ?? opts.maxEpochs\n const budgetAnneal = opts.budgetAnneal ?? true\n const rejectedBufferSize = opts.rejectedBufferSize ?? 12\n const slowMetaEvery = opts.slowMetaEvery ?? 2\n\n let totalCostUsd = 0\n const scoreHoldout = async (surface: string, tag: string): Promise<number> => {\n const campaign = await runScoringCampaign(opts, opts.holdoutScenarios, surface, tag)\n totalCostUsd += campaign.aggregates.totalCostUsd\n return campaignMeanComposite(campaign)\n }\n const evidenceK = opts.evidenceK ?? 3\n const trainEvidence = async (surface: string, tag: string): Promise<SkillOptEvidence> => {\n const campaign = await runScoringCampaign(opts, opts.trainScenarios, surface, tag)\n totalCostUsd += campaign.aggregates.totalCostUsd\n return toEvidence(campaign, evidenceK)\n }\n\n let current = opts.baselineSurface\n let currentEvidence = await trainEvidence(current, 'baseline-train')\n const baselineHoldout = await scoreHoldout(current, 'baseline-holdout')\n let currentHoldout = baselineHoldout\n\n const buffer: RejectedEdit[] = []\n const acceptedEdits: AcceptedEdit[] = []\n const rejectedAll: RejectedEdit[] = []\n const history: SkillOptEpochRecord[] = []\n let budget = initialBudget\n let sinceAccept = 0\n let metaNote: string | undefined\n let epochsRun = 0\n\n for (let epoch = 0; epoch < opts.maxEpochs; epoch++) {\n epochsRun++\n const patches = await opts.driver.proposePatches({\n surface: current,\n evidence: currentEvidence,\n editBudget: budget,\n rejectedBuffer: buffer,\n metaNote,\n count: patchesPerEpoch,\n signal: opts.signal ?? new AbortController().signal,\n })\n\n let accepted: AcceptedEdit | null = null\n const rejectedThisEpoch: RejectedEdit[] = []\n for (let i = 0; i < patches.length; i++) {\n const patch = patches[i]!\n const { surface: candidate, applied } = applySkillPatch(current, patch)\n if (applied === 0 || candidate === current) {\n rejectedThisEpoch.push({\n label: patch.label,\n rationale: patch.rationale,\n reason: 'no-op (unanchored or zero-change)',\n })\n continue\n }\n const candidateHoldout = await scoreHoldout(candidate, `epoch-${epoch}-cand-${i}-holdout`)\n if (candidateHoldout > currentHoldout + minImprovement) {\n accepted = {\n epoch,\n label: patch.label,\n rationale: patch.rationale,\n holdoutDelta: candidateHoldout - currentHoldout,\n }\n current = candidate\n currentHoldout = candidateHoldout\n // The surface changed — recompute evidence for the next epoch from it.\n currentEvidence = await trainEvidence(current, `epoch-${epoch}-train`)\n break // greedy: take the first strictly-improving edit\n }\n rejectedThisEpoch.push({\n label: patch.label,\n rationale: patch.rationale,\n reason: `held-out ${candidateHoldout.toFixed(3)} ≤ current ${currentHoldout.toFixed(3)}`,\n })\n }\n\n if (accepted) {\n acceptedEdits.push(accepted)\n sinceAccept = 0\n } else {\n sinceAccept++\n if (budgetAnneal && sinceAccept >= 2 && budget > 1) budget--\n }\n // Maintain the bounded rejected-edit buffer (steers the proposer away from\n // dead ends; capped so the prompt stays bounded).\n for (const r of rejectedThisEpoch) {\n buffer.push(r)\n rejectedAll.push(r)\n }\n while (buffer.length > rejectedBufferSize) buffer.shift()\n\n // Slow-update meta note: a cross-epoch digest refreshed on a slow cadence.\n if (slowMetaEvery > 0 && (epoch + 1) % slowMetaEvery === 0) {\n metaNote = buildMetaNote(acceptedEdits, buffer)\n }\n\n history.push({\n epoch,\n editBudget: budget,\n proposed: patches.length,\n accepted,\n rejected: rejectedThisEpoch,\n holdoutComposite: currentHoldout,\n })\n\n if (sinceAccept >= patience) break\n }\n\n return {\n winnerSurface: current,\n baselineHoldoutComposite: baselineHoldout,\n winnerHoldoutComposite: currentHoldout,\n lift: currentHoldout - baselineHoldout,\n acceptedEdits,\n rejectedEdits: rejectedAll,\n epochsRun,\n history,\n totalCostUsd,\n }\n}\n\nfunction runScoringCampaign<TScenario extends Scenario, TArtifact>(\n opts: RunSkillOptOptions<TScenario, TArtifact>,\n scenarios: TScenario[],\n surface: string,\n tag: string,\n): Promise<CampaignResult<TArtifact, TScenario>> {\n return runCampaign<TScenario, TArtifact>({\n ...opts,\n scenarios,\n dispatch: (scenario, ctx) => opts.dispatchWithSurface(surface, scenario, ctx),\n runDir: `${opts.runDir}/${tag}`,\n })\n}\n\nfunction toEvidence<TArtifact, TScenario extends Scenario>(\n campaign: CampaignResult<TArtifact, TScenario>,\n k: number,\n): SkillOptEvidence {\n const { dimensions, scenarios } = campaignBreakdown(campaign)\n const weakScenarios = [...scenarios].sort((a, b) => a.composite - b.composite).slice(0, k)\n const weakDimensions = Object.entries(dimensions)\n .sort((a, b) => a[1] - b[1])\n .slice(0, k)\n .map(([dimension, score]) => ({ dimension, score }))\n return { weakScenarios, weakDimensions }\n}\n\nfunction buildMetaNote(accepted: AcceptedEdit[], rejected: RejectedEdit[]): string {\n const parts: string[] = []\n if (accepted.length > 0) {\n parts.push(\n `Edits that improved held-out so far: ${accepted\n .map((a) => `\"${a.label}\" (+${a.holdoutDelta.toFixed(3)})`)\n .join('; ')}. Build on these.`,\n )\n }\n if (rejected.length > 0) {\n const labels = [...new Set(rejected.map((r) => r.label))].slice(0, 5)\n parts.push(`Dead ends to avoid: ${labels.join(', ')}. Try a different anchor or rule.`)\n }\n parts.push('Keep edits small and anchored to existing lines.')\n return parts.join(' ')\n}\n","/**\n * @experimental\n *\n * `compareDrivers` — a head-to-head lift benchmark across optimizer drivers on\n * ONE corpus. This is the forcing function: optimizer quality (GEPA reflection\n * vs GEPA+Pareto vs SkillOpt) becomes a NUMBER with a confidence interval, so a\n * driver regression — or shipping a simplified driver and calling it the real\n * one — turns a build red instead of going measurement-invisible.\n *\n * Every entrant is scored the SAME way: each driver returns the surface it\n * promoted, then `compareDrivers` scores the baseline + every winner on the\n * SAME held-out scenarios with the SAME judges. Apples-to-apples by\n * construction — the comparison never depends on how a driver measured itself.\n * The per-scenario held-out composites feed a paired bootstrap (`statistics.ts`)\n * for each driver's lift CI and for the pairwise \"which driver wins\" CI.\n */\n\nimport type { LlmClientOptions } from '../../llm-client'\nimport { pairedBootstrap } from '../../statistics'\nimport { gepaDriver } from '../drivers/gepa'\nimport { skillOptDriver } from '../drivers/skill-opt'\nimport { defaultProductionGate } from '../gates/default-production-gate'\nimport { type RunCampaignOptions, runCampaign } from '../run-campaign'\nimport { campaignBreakdown } from '../score-utils'\nimport type {\n CampaignResult,\n DispatchContext,\n JudgeConfig,\n MutableSurface,\n Scenario,\n} from '../types'\nimport { runImprovementLoop } from './run-improvement-loop'\nimport { runSkillOpt } from './run-skill-opt'\n\n/** What an optimizer produced: the surface it promoted + what it cost to get\n * there. `compareDrivers` does the held-out scoring itself, so an entry only\n * needs to run its loop and hand back the winner. */\nexport interface DriverEntry {\n name: string\n optimize: () => Promise<{ winnerSurface: MutableSurface; costUsd: number; durationMs?: number }>\n}\n\nexport interface DriverScore {\n name: string\n /** Mean held-out composite of the baseline (identical across drivers). */\n baselineComposite: number\n /** Mean held-out composite of this driver's promoted surface. */\n winnerComposite: number\n /** Mean per-scenario held-out lift (winner − baseline). */\n lift: number\n /** Paired-bootstrap CI of the per-scenario lift. `low > 0` ⇒ a real gain. */\n liftCi: { low: number; high: number }\n costUsd: number\n durationMs?: number\n winnerSurface: MutableSurface\n /** 1-based, by descending lift. */\n rank: number\n}\n\nexport interface DriverPairwise {\n /** Higher-ranked driver. */\n a: string\n b: string\n /** Mean per-scenario held-out delta (a − b). */\n deltaMean: number\n low: number\n high: number\n /** `a` if the CI clears 0, `b` if it is entirely negative, else `'tie'`. */\n favored: string\n}\n\nexport interface DriverComparison {\n /** Sorted by descending lift; `rank` set accordingly. */\n scores: DriverScore[]\n best: DriverScore\n /** Best vs each other driver, paired-bootstrap on the held-out winners. */\n pairwise: DriverPairwise[]\n holdoutScenarioIds: string[]\n}\n\nexport interface CompareDriversOptions<TScenario extends Scenario, TArtifact>\n extends Omit<RunCampaignOptions<TScenario, TArtifact>, 'dispatch' | 'scenarios'> {\n drivers: DriverEntry[]\n baselineSurface: MutableSurface\n /** The held-out scenarios every winner is scored on. */\n holdoutScenarios: TScenario[]\n /** Scores a surface on a scenario — the same dispatcher the drivers used. */\n dispatchWithSurface: (\n surface: MutableSurface,\n scenario: TScenario,\n ctx: DispatchContext,\n ) => Promise<TArtifact>\n /** Bootstrap resamples for the lift CIs. Default 2000. */\n resamples?: number\n /** CI confidence. Default 0.95. */\n confidence?: number\n}\n\nexport async function compareDrivers<TScenario extends Scenario, TArtifact>(\n opts: CompareDriversOptions<TScenario, TArtifact>,\n): Promise<DriverComparison> {\n if (opts.drivers.length === 0) throw new Error('compareDrivers: no drivers to compare')\n const seed = opts.seed ?? 42\n const resamples = opts.resamples ?? 2000\n const confidence = opts.confidence ?? 0.95\n\n const scoreOnHoldout = async (\n surface: MutableSurface,\n tag: string,\n ): Promise<Record<string, number>> => {\n const campaign: CampaignResult<TArtifact, TScenario> = await runCampaign<TScenario, TArtifact>({\n ...opts,\n scenarios: opts.holdoutScenarios,\n dispatch: (scenario, ctx) => opts.dispatchWithSurface(surface, scenario, ctx),\n runDir: `${opts.runDir}/${tag}`,\n })\n const byScenario: Record<string, number> = {}\n for (const { scenarioId, composite } of campaignBreakdown(campaign).scenarios) {\n byScenario[scenarioId] = composite\n }\n return byScenario\n }\n\n // The comparison axis is the DESIGNED held-out set, not whatever a campaign\n // happened to score. Align every score vector to it and FAIL LOUD if a\n // surface is missing any scenario (an errored cell / a judge that returned no\n // score). Fabricating a 0 there would silently penalize that surface and\n // corrupt the paired-bootstrap lift CI — the exact \"no silent zeros\" trap.\n const scenarioIds = [...new Set(opts.holdoutScenarios.map((s) => s.id))].sort()\n if (scenarioIds.length === 0) throw new Error('compareDrivers: holdoutScenarios is empty')\n const align = (byScenario: Record<string, number>, label: string): number[] => {\n const missing = scenarioIds.filter((id) => !(id in byScenario))\n if (missing.length > 0) {\n throw new Error(\n `compareDrivers: ${label} produced no held-out score for scenario(s) [${missing.join(\n ', ',\n )}] — a cell errored or its judges returned nothing. Refusing to fabricate a 0 (it would corrupt the lift comparison). Fix the dispatch/judge or drop the scenario.`,\n )\n }\n return scenarioIds.map((id) => byScenario[id]!)\n }\n\n const baselineArr = align(\n await scoreOnHoldout(opts.baselineSurface, 'compare-baseline'),\n 'baseline',\n )\n\n // Run + uniformly re-score every entrant on the SAME held-out axis.\n const winners: Array<{\n name: string\n winnerSurface: MutableSurface\n costUsd: number\n durationMs?: number\n arr: number[]\n }> = []\n for (const d of opts.drivers) {\n const out = await d.optimize()\n const byScenario = await scoreOnHoldout(out.winnerSurface, `compare-${slug(d.name)}`)\n winners.push({\n name: d.name,\n winnerSurface: out.winnerSurface,\n costUsd: out.costUsd,\n durationMs: out.durationMs,\n arr: align(byScenario, `driver \"${d.name}\"`),\n })\n }\n\n const scores: DriverScore[] = winners.map((w) => {\n const boot = pairedBootstrap(baselineArr, w.arr, {\n seed,\n resamples,\n confidence,\n statistic: 'mean',\n })\n const score: DriverScore = {\n name: w.name,\n baselineComposite: mean(baselineArr),\n winnerComposite: mean(w.arr),\n lift: boot.mean,\n liftCi: { low: boot.low, high: boot.high },\n costUsd: w.costUsd,\n winnerSurface: w.winnerSurface,\n rank: 0,\n }\n if (w.durationMs !== undefined) score.durationMs = w.durationMs\n return score\n })\n // Sort by lift; tie-break by lower cost (cheaper wins a tie).\n scores.sort((a, b) => b.lift - a.lift || a.costUsd - b.costUsd)\n scores.forEach((s, i) => {\n s.rank = i + 1\n })\n const best = scores[0]!\n\n const byName = new Map(winners.map((w) => [w.name, w]))\n const bestArr = byName.get(best.name)!.arr\n const pairwise: DriverPairwise[] = scores.slice(1).map((other) => {\n const otherArr = byName.get(other.name)!.arr\n // before = other, after = best ⇒ delta = best − other on the held-out set.\n const boot = pairedBootstrap(otherArr, bestArr, {\n seed,\n resamples,\n confidence,\n statistic: 'mean',\n })\n const favored = boot.low > 0 ? best.name : boot.high < 0 ? other.name : 'tie'\n return {\n a: best.name,\n b: other.name,\n deltaMean: boot.mean,\n low: boot.low,\n high: boot.high,\n favored,\n }\n })\n\n return { scores, best, pairwise, holdoutScenarioIds: scenarioIds }\n}\n\nfunction mean(xs: number[]): number {\n return xs.length === 0 ? 0 : xs.reduce((a, b) => a + b, 0) / xs.length\n}\n\nfunction slug(name: string): string {\n return name.replace(/[^a-z0-9]+/gi, '-').toLowerCase()\n}\n\n// ── Built-in entries — wire the real optimizers for a live comparison ───────\n\n/** Shared corpus + transport for the three built-in optimizer entries. */\nexport interface OptimizerEntryConfig<TScenario extends Scenario, TArtifact> {\n baselineSurface: string\n /** Training scenarios the drivers reflect on. */\n trainScenarios: TScenario[]\n /** Held-out scenarios (the gate axis + the benchmark scoring axis). */\n holdoutScenarios: TScenario[]\n dispatchWithSurface: (\n surface: MutableSurface,\n scenario: TScenario,\n ctx: DispatchContext,\n ) => Promise<TArtifact>\n judges: JudgeConfig<TArtifact, TScenario>[]\n llm: LlmClientOptions\n model: string\n target: string\n runDir: string\n seed?: number\n /** GEPA population per generation. Default 2. */\n populationSize?: number\n /** GEPA generations. Default 3. */\n maxGenerations?: number\n /** SkillOpt epochs. Default 6. */\n maxEpochs?: number\n mutationPrimitives?: string[]\n}\n\n/** GEPA, reflection-only (single-parent, no Pareto combine). */\nexport function gepaReflectionEntry<TScenario extends Scenario, TArtifact>(\n config: OptimizerEntryConfig<TScenario, TArtifact>,\n name = 'gepa-reflection',\n): DriverEntry {\n return gepaEntry(config, false, name)\n}\n\n/** GEPA with the Pareto frontier + combine-complementary-lessons. */\nexport function gepaParetoEntry<TScenario extends Scenario, TArtifact>(\n config: OptimizerEntryConfig<TScenario, TArtifact>,\n name = 'gepa-pareto',\n): DriverEntry {\n return gepaEntry(config, true, name)\n}\n\nfunction gepaEntry<TScenario extends Scenario, TArtifact>(\n config: OptimizerEntryConfig<TScenario, TArtifact>,\n combineParents: boolean,\n name: string,\n): DriverEntry {\n return {\n name,\n async optimize() {\n const started = Date.now()\n const driver = gepaDriver({\n llm: config.llm,\n model: config.model,\n target: config.target,\n combineParents,\n ...(config.mutationPrimitives ? { mutationPrimitives: config.mutationPrimitives } : {}),\n })\n const result = await runImprovementLoop<TScenario, TArtifact>({\n scenarios: config.trainScenarios,\n holdoutScenarios: config.holdoutScenarios,\n baselineSurface: config.baselineSurface,\n dispatchWithSurface: config.dispatchWithSurface,\n judges: config.judges,\n driver,\n populationSize: config.populationSize ?? 2,\n maxGenerations: config.maxGenerations ?? 3,\n gate: defaultProductionGate<TArtifact, TScenario>({\n holdoutScenarios: config.holdoutScenarios,\n deltaThreshold: 0,\n }),\n autoOnPromote: 'none',\n runDir: `${config.runDir}/${slug(name)}-loop`,\n ...(config.seed !== undefined ? { seed: config.seed } : {}),\n })\n const costUsd =\n result.baselineCampaign.aggregates.totalCostUsd +\n result.generations.reduce(\n (sum, g) =>\n sum + g.surfaces.reduce((s, sf) => s + sf.campaign.aggregates.totalCostUsd, 0),\n 0,\n )\n return { winnerSurface: result.winnerSurface, costUsd, durationMs: Date.now() - started }\n },\n }\n}\n\n/** SkillOpt patch-mode hill-climb. */\nexport function skillOptEntry<TScenario extends Scenario, TArtifact>(\n config: OptimizerEntryConfig<TScenario, TArtifact>,\n name = 'skill-opt',\n): DriverEntry {\n return {\n name,\n async optimize() {\n const started = Date.now()\n const driver = skillOptDriver({ llm: config.llm, model: config.model, target: config.target })\n const result = await runSkillOpt<TScenario, TArtifact>({\n baselineSurface: config.baselineSurface,\n dispatchWithSurface: config.dispatchWithSurface,\n judges: config.judges,\n driver,\n trainScenarios: config.trainScenarios,\n holdoutScenarios: config.holdoutScenarios,\n maxEpochs: config.maxEpochs ?? 6,\n runDir: `${config.runDir}/${slug(name)}-loop`,\n ...(config.seed !== undefined ? { seed: config.seed } : {}),\n })\n return {\n winnerSurface: result.winnerSurface,\n costUsd: result.totalCostUsd,\n durationMs: Date.now() - started,\n }\n },\n }\n}\n","/**\n * @experimental\n *\n * `runProfileMatrix` — the missing keystone between `runAgentMatrix` and the\n * backend-integrity guard.\n *\n * The gap it closes: `runAgentMatrix` is a topology-opaque scheduler whose\n * cells return a bare `{ output, verdict, costUsd }` — no `tokenUsage`, not a\n * `RunRecord`. `assertRealBackend` / `summarizeBackendIntegrity` key on\n * `RunRecord.tokenUsage`, so they cannot run on a raw matrix result. Every\n * consumer therefore hand-writes the same bridge: fan a profile × scenario\n * cartesian, call dispatch, fabricate a `RunRecord` with token usage, thread it\n * back, run the integrity guard. That hand-rolled bridge is exactly the pile of\n * bespoke `eval:*` scripts the adoption skills keep trying (and failing) to\n * forbid.\n *\n * `runProfileMatrix` IS that bridge, once:\n *\n * - axis 3 (PROFILE) = `profiles: AgentProfile[]`\n * - axis 1 (PERSONA/SCENARIO) = `scenarios: Scenario[]` (each scenario carries\n * its persona; `personaOf` groups them for the `byPersona` pivot)\n * - the scoring axis = `judges`\n *\n * It runs `runCampaign` once per profile (reusing its seeds, reps, bootstrap\n * CIs, resumability, and the `LabeledScenarioStore` capture flywheel), maps\n * every cell to a validated `RunRecord` carrying the real `tokenUsage` the\n * dispatch reported via `ctx.cost.observeTokens`, and runs `assertRealBackend`\n * BY CONSTRUCTION before returning — so a stub-backend run fails loudly instead\n * of reporting a clean 0/N leaderboard.\n *\n * Dispatch contract: a dispatch that calls an LLM MUST report usage via\n * `ctx.cost.observeTokens({ input, output })` (and cost via `ctx.cost.observe`).\n * A dispatch that reports zero tokens is indistinguishable from a stub and the\n * integrity guard treats it as one.\n */\n\nimport { createHash } from 'node:crypto'\nimport { join } from 'node:path'\nimport { type AgentProfile, agentProfileHash } from '../../agent-profile'\nimport { AgentEvalError } from '../../errors'\nimport {\n assertRealBackend,\n type BackendIntegrityReport,\n summarizeBackendIntegrity,\n} from '../../integrity/backend-integrity'\nimport {\n type RunOutcome,\n type RunRecord,\n type RunSplitTag,\n validateRunRecord,\n} from '../../run-record'\nimport { runCampaign } from '../run-campaign'\nimport type { CampaignStorage } from '../storage'\nimport type {\n CampaignCellResult,\n CampaignResult,\n DispatchContext,\n JudgeConfig,\n LabeledScenarioSource,\n LabeledScenarioStore,\n Scenario,\n} from '../types'\n\n/** Thrown when the matrix is misconfigured (no profiles, a profile whose model\n * lacks a snapshot version, etc.). Distinct from `BackendIntegrityError`,\n * which signals a stub backend at run time. */\nexport class ProfileMatrixError extends AgentEvalError {\n constructor(message: string) {\n super('profile_matrix', message)\n }\n}\n\n/** Dispatch for one cell: render `profile` against `scenario`, returning the\n * artifact the judges score. Report LLM usage via `ctx.cost.observeTokens`\n * and `ctx.cost.observe` — the integrity guard depends on it. */\nexport type ProfileDispatchFn<TScenario extends Scenario, TArtifact> = (\n profile: AgentProfile,\n scenario: TScenario,\n ctx: DispatchContext,\n) => Promise<TArtifact>\n\nexport interface RunProfileMatrixOptions<TScenario extends Scenario, TArtifact> {\n /** Axis 3 — the agent-under-test configurations. Each is one column. */\n profiles: AgentProfile[]\n /** Axis 1 — the persona/scenario corpus, run against every profile. */\n scenarios: TScenario[]\n /** Renders one (profile, scenario) cell. */\n dispatch: ProfileDispatchFn<TScenario, TArtifact>\n /** The scoring axis. */\n judges?: JudgeConfig<TArtifact, TScenario>[]\n /** Where each profile's campaign writes artifacts/traces. One subdir per\n * profile. */\n runDir: string\n /** Git SHA the harness ran from — stamped onto every RunRecord (mandatory\n * for paper-grade records). */\n commitSha: string\n /** Logical experiment id shared across the whole matrix so the promotion\n * gate can pair profiles on matched scenarios. Default: a hash of the\n * profile + scenario ids. */\n experimentId?: string\n /** Which split these runs belong to. Default `'search'`. */\n splitTag?: RunSplitTag\n /** Replicates per (profile, scenario) cell for CI bands. Default 1. */\n reps?: number\n /** Campaign seed (per profile). Default 42. */\n seed?: number\n /**\n * Backend-integrity posture, enforced AFTER the matrix completes:\n * - `'assert'` (default) — throw `BackendIntegrityError` if the run was a\n * stub (and, with `allowMixed:false`, if it was mixed).\n * - `'warn'` — log the verdict but never throw.\n * - `'off'` — skip the guard entirely (only for offline/replay analysis).\n */\n integrity?: 'assert' | 'warn' | 'off'\n /** Forwarded to `assertRealBackend`. Default true (tolerate partial 429\n * cascades); set false for strict CI gates. */\n allowMixed?: boolean\n /** Max concurrent cells WITHIN each profile's campaign. Default 2.\n * Profiles run sequentially so the cost ceiling is honored deterministically. */\n maxConcurrency?: number\n /** Cumulative USD cap per profile campaign. */\n costCeiling?: number\n /** Capture flywheel — forwarded to each campaign. */\n labeledStore?: LabeledScenarioStore | 'off'\n captureSource?: LabeledScenarioSource\n /** Storage backend. Default `fsCampaignStorage`. Pass\n * `inMemoryCampaignStorage()` for edge/CF-Worker/test runs. */\n storage?: CampaignStorage\n /** Test seam — override the wall clock. */\n now?: () => Date\n /** Optional persona key per scenario — drives the `byPersona` pivot. When\n * unset, `byPersona` is omitted. */\n personaOf?: (scenario: TScenario) => string\n /** Validate every produced RunRecord with `validateRunRecord` (fail-loud).\n * Default true — catches bad model snapshots and non-finite judge dims at\n * the boundary instead of letting them poison downstream analysis. */\n validate?: boolean\n}\n\nexport interface ProfileSummary {\n profileId: string\n profileHash: string\n model: string\n /** RunRecords produced for this profile (= scenarios × reps). */\n records: number\n /** Mean composite across this profile's records. */\n meanComposite: number\n totalCostUsd: number\n /** Per-profile integrity verdict — surfaces a single profile that ran stub\n * even when the matrix as a whole looks real. */\n integrity: BackendIntegrityReport\n}\n\nexport interface ScenarioRollup {\n meanComposite: number\n n: number\n}\n\nexport interface RunProfileMatrixResult<TArtifact, TScenario extends Scenario> {\n matrixId: string\n experimentId: string\n /** One RunRecord per (profile, scenario, rep) cell — the integrity-checked,\n * paper-grade output. Feed straight into `analyzeRuns`, `HeldOutGate`,\n * scorecards, the hosted wire format. */\n records: RunRecord[]\n byProfile: Record<string, ProfileSummary>\n byScenario: Record<string, ScenarioRollup>\n /** Present only when `personaOf` was supplied. */\n byPersona?: Record<string, ScenarioRollup>\n /** Whole-matrix integrity report (the one `integrity:'assert'` enforces). */\n integrity: BackendIntegrityReport\n /** The raw per-profile campaign results, keyed by profile id. */\n campaigns: Record<string, CampaignResult<TArtifact, TScenario>>\n}\n\nfunction sanitize(id: string): string {\n return id.replace(/[^a-zA-Z0-9_-]/g, '_')\n}\n\nfunction sha(input: unknown): string {\n return createHash('sha256').update(JSON.stringify(input)).digest('hex')\n}\n\nfunction mean(xs: number[]): number {\n return xs.length === 0 ? 0 : xs.reduce((a, b) => a + b, 0) / xs.length\n}\n\nfunction cellComposite(cell: CampaignCellResult<unknown>): number {\n const composites = Object.values(cell.judgeScores).map((s) => s.composite)\n return composites.length === 0 ? 0 : mean(composites)\n}\n\ninterface BuildRecordArgs<TArtifact> {\n cell: CampaignCellResult<TArtifact>\n profile: AgentProfile\n profileHash: string\n configHash: string\n experimentId: string\n splitTag: RunSplitTag\n commitSha: string\n matrixId: string\n}\n\nfunction buildRunRecord<TArtifact>(args: BuildRecordArgs<TArtifact>): RunRecord {\n const { cell, profile, profileHash, configHash, experimentId, splitTag, commitSha, matrixId } =\n args\n const composite = cellComposite(cell)\n\n // Flatten judge dimensions (judge-prefixed to avoid collisions) into raw.\n const raw: Record<string, number> = { composite }\n const perJudge: Record<string, Record<string, number>> = {}\n const dimAccum: Record<string, number[]> = {}\n const notes: string[] = []\n for (const [judgeName, js] of Object.entries(cell.judgeScores)) {\n perJudge[judgeName] = { ...js.dimensions }\n for (const [dim, value] of Object.entries(js.dimensions)) {\n raw[`${judgeName}.${dim}`] = value\n ;(dimAccum[dim] ??= []).push(value)\n }\n if (js.notes) notes.push(`${judgeName}: ${js.notes}`)\n }\n const perDimMean: Record<string, number> = {}\n for (const [dim, values] of Object.entries(dimAccum)) perDimMean[dim] = mean(values)\n\n const outcome: RunOutcome =\n splitTag === 'holdout' ? { holdoutScore: composite, raw } : { searchScore: composite, raw }\n if (Object.keys(perJudge).length > 0) {\n outcome.judgeScores = {\n perJudge,\n perDimMean,\n composite,\n ...(notes.length > 0 ? { notes: notes.join(' | ') } : {}),\n }\n }\n\n return {\n runId: `${matrixId}:${profile.id}:${cell.cellId}`,\n experimentId,\n candidateId: profile.id,\n seed: cell.seed,\n model: profile.model,\n promptHash: profileHash,\n configHash,\n commitSha,\n wallMs: cell.durationMs,\n costUsd: cell.costUsd,\n tokenUsage: cell.tokenUsage,\n outcome,\n splitTag,\n scenarioId: cell.scenarioId,\n ...(cell.error ? { failureMode: cell.error } : {}),\n }\n}\n\nexport async function runProfileMatrix<TScenario extends Scenario, TArtifact>(\n opts: RunProfileMatrixOptions<TScenario, TArtifact>,\n): Promise<RunProfileMatrixResult<TArtifact, TScenario>> {\n if (opts.profiles.length === 0) throw new ProfileMatrixError('profiles must not be empty')\n if (opts.scenarios.length === 0) throw new ProfileMatrixError('scenarios must not be empty')\n\n const splitTag = opts.splitTag ?? 'search'\n const seed = opts.seed ?? 42\n const validate = opts.validate ?? true\n const integrityMode = opts.integrity ?? 'assert'\n const profileIds = opts.profiles.map((p) => p.id)\n const experimentId =\n opts.experimentId ??\n `pm_${sha({ profileIds, scenarios: opts.scenarios.map((s) => s.id) }).slice(0, 16)}`\n const matrixId = `mtx_${sha({ experimentId, profileIds, seed, splitTag }).slice(0, 16)}`\n\n // Preflight: every profile must hash (non-empty model) AND its model must\n // carry a snapshot version, BEFORE any LLM spend. A probe record run through\n // validateRunRecord catches both in the exact place they'd otherwise surface\n // far downstream.\n for (const profile of opts.profiles) {\n const profileHash = agentProfileHash(profile)\n try {\n validateRunRecord({\n runId: `${matrixId}:${profile.id}:probe`,\n experimentId,\n candidateId: profile.id,\n seed,\n model: profile.model,\n promptHash: profileHash,\n configHash: profileHash,\n commitSha: opts.commitSha,\n wallMs: 0,\n costUsd: 0,\n tokenUsage: { input: 0, output: 0 },\n outcome:\n splitTag === 'holdout' ? { holdoutScore: 0, raw: {} } : { searchScore: 0, raw: {} },\n splitTag,\n })\n } catch (err) {\n throw new ProfileMatrixError(\n `profile '${profile.id}' is not recordable: ${err instanceof Error ? err.message : String(err)}`,\n )\n }\n }\n\n const records: RunRecord[] = []\n const campaigns: Record<string, CampaignResult<TArtifact, TScenario>> = {}\n const byProfile: Record<string, ProfileSummary> = {}\n\n for (const profile of opts.profiles) {\n const profileHash = agentProfileHash(profile)\n const configHash = sha({\n profile: profileHash,\n judges: (opts.judges ?? []).map((j) => j.name),\n seed,\n splitTag,\n })\n\n // Bind the profile into a campaign dispatch. Name it so the campaign's\n // manifest hash is stable + distinct per profile.\n const dispatch = (scenario: TScenario, ctx: DispatchContext): Promise<TArtifact> =>\n opts.dispatch(profile, scenario, ctx)\n Object.defineProperty(dispatch, 'name', { value: `profile_${sanitize(profile.id)}` })\n\n const campaign = await runCampaign<TScenario, TArtifact>({\n scenarios: opts.scenarios,\n dispatch,\n judges: opts.judges,\n seed,\n reps: opts.reps,\n maxConcurrency: opts.maxConcurrency,\n costCeiling: opts.costCeiling,\n labeledStore: opts.labeledStore,\n captureSource: opts.captureSource,\n storage: opts.storage,\n now: opts.now,\n runDir: join(opts.runDir, sanitize(profile.id)),\n })\n campaigns[profile.id] = campaign\n\n const profileRecords: RunRecord[] = []\n for (const cell of campaign.cells) {\n const record = buildRunRecord({\n cell,\n profile,\n profileHash,\n configHash,\n experimentId,\n splitTag,\n commitSha: opts.commitSha,\n matrixId,\n })\n if (validate) validateRunRecord(record)\n profileRecords.push(record)\n records.push(record)\n }\n\n byProfile[profile.id] = {\n profileId: profile.id,\n profileHash,\n model: profile.model,\n records: profileRecords.length,\n meanComposite: mean(profileRecords.map(compositeOf)),\n totalCostUsd: profileRecords.reduce((a, r) => a + r.costUsd, 0),\n integrity: summarizeBackendIntegrity(profileRecords),\n }\n }\n\n // Integrity by construction — the whole point of the primitive.\n const integrity = summarizeBackendIntegrity(records)\n if (integrityMode === 'assert') {\n assertRealBackend(records, { allowMixed: opts.allowMixed ?? true })\n } else if (integrityMode === 'warn' && integrity.verdict !== 'real') {\n // eslint-disable-next-line no-console\n console.warn(\n `[runProfileMatrix] backend integrity: ${integrity.verdict} — ${integrity.diagnosis}`,\n )\n }\n\n // Pivots.\n const byScenario = rollup(records, (r) => r.scenarioId)\n const byPersona = opts.personaOf\n ? rollupByPersona(records, opts.scenarios, opts.personaOf)\n : undefined\n\n return { matrixId, experimentId, records, byProfile, byScenario, byPersona, integrity, campaigns }\n}\n\n/** Composite for a produced RunRecord (the split score it carries). */\nfunction compositeOf(r: RunRecord): number {\n return r.outcome.holdoutScore ?? r.outcome.searchScore ?? 0\n}\n\nfunction rollup(\n records: RunRecord[],\n keyOf: (r: RunRecord) => string | undefined,\n): Record<string, ScenarioRollup> {\n const groups = new Map<string, number[]>()\n for (const r of records) {\n const key = keyOf(r)\n if (key === undefined) continue\n const arr = groups.get(key) ?? []\n arr.push(compositeOf(r))\n groups.set(key, arr)\n }\n const out: Record<string, ScenarioRollup> = {}\n for (const [key, xs] of groups) out[key] = { meanComposite: mean(xs), n: xs.length }\n return out\n}\n\nfunction rollupByPersona<TScenario extends Scenario>(\n records: RunRecord[],\n scenarios: TScenario[],\n personaOf: (s: TScenario) => string,\n): Record<string, ScenarioRollup> {\n const personaByScenarioId = new Map<string, string>()\n for (const s of scenarios) personaByScenarioId.set(s.id, personaOf(s))\n return rollup(records, (r) => (r.scenarioId ? personaByScenarioId.get(r.scenarioId) : undefined))\n}\n","/**\n * @experimental\n *\n * VCS-pluggable worktree adapter. One improvement = one worktree, PR-like\n * (multiple commits allowed). A code-tier driver's `propose()` creates a\n * worktree, an agent commits the change into it, and `finalize()` returns a\n * `CodeSurface{ worktreeRef }` the measurement checks out to run the worker\n * against the changed code. On promotion the worktree becomes the PR branch.\n *\n * The interface is VCS-agnostic so a future `jj` ([jj-vcs](https://github.com/jj-vcs/jj))\n * adapter can slot in without touching driver code. Only the git adapter\n * ships today. See `docs/design/self-improvement-engine.md`.\n */\n\nimport { execFileSync } from 'node:child_process'\nimport { existsSync } from 'node:fs'\nimport { basename, isAbsolute, join } from 'node:path'\nimport type { CodeSurface } from '../types'\n\nexport interface Worktree {\n /** Absolute path to the checked-out worktree directory. */\n path: string\n /** The branch the worktree is on (becomes the PR branch on promotion). */\n branch: string\n /** The ref the worktree was forked from. */\n baseRef: string\n}\n\nexport interface WorktreeAdapter {\n /** Create an isolated worktree on a fresh branch off `baseRef`. */\n create(opts: { baseRef: string; label: string }): Promise<Worktree>\n /** Commit any pending changes in the worktree, then return a CodeSurface\n * pointing at it. The agent has already written its change into\n * `worktree.path` by the time this is called. */\n finalize(worktree: Worktree, summary: string): Promise<CodeSurface>\n /** Remove the worktree (and its branch) — called for losing candidates. */\n discard(worktree: Worktree): Promise<void>\n}\n\nexport class WorktreeAdapterError extends Error {\n constructor(\n message: string,\n readonly cause?: unknown,\n ) {\n super(message)\n this.name = 'WorktreeAdapterError'\n }\n}\n\nexport interface GitWorktreeAdapterOptions {\n /** Repo root the worktrees fork from. */\n repoRoot: string\n /** Directory worktrees are created under. Default: `<repoRoot>/.worktrees`. */\n worktreeDir?: string\n /** Branch-name prefix. Default: `improve`. */\n branchPrefix?: string\n /** Test seam — defaults to a real `git` runner. */\n git?: (args: string[], cwd: string) => string\n}\n\nfunction defaultGit(args: string[], cwd: string): string {\n try {\n return execFileSync('git', args, { cwd, encoding: 'utf8' }).trim()\n } catch (err) {\n const stderr =\n err && typeof err === 'object' && 'stderr' in err\n ? String((err as { stderr: unknown }).stderr)\n : ''\n throw new WorktreeAdapterError(`git ${args.join(' ')} failed: ${stderr || String(err)}`, err)\n }\n}\n\n/** Slugify a label into a branch-safe segment. */\nfunction slug(label: string): string {\n return (\n label\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '-')\n .replace(/^-+|-+$/g, '')\n .slice(0, 48) || 'candidate'\n )\n}\n\nexport function gitWorktreeAdapter(opts: GitWorktreeAdapterOptions): WorktreeAdapter {\n const git = opts.git ?? defaultGit\n const worktreeDir = opts.worktreeDir ?? join(opts.repoRoot, '.worktrees')\n const branchPrefix = opts.branchPrefix ?? 'improve'\n\n return {\n async create({ baseRef, label }) {\n const id = `${slug(label)}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`\n const branch = `${branchPrefix}/${id}`\n const path = join(worktreeDir, id)\n git(['worktree', 'add', '-b', branch, path, baseRef], opts.repoRoot)\n return { path, branch, baseRef }\n },\n\n async finalize(worktree, summary) {\n // Stage + commit any pending changes the agent left in the worktree.\n // A no-op commit is refused by git, so only commit when the tree is dirty.\n const status = git(['status', '--porcelain'], worktree.path)\n if (status.length > 0) {\n git(['add', '-A'], worktree.path)\n git(['commit', '-m', summary], worktree.path)\n }\n return {\n kind: 'code',\n worktreeRef: worktree.path,\n baseRef: worktree.baseRef,\n summary,\n }\n },\n\n async discard(worktree) {\n // Remove the worktree, then delete its branch. Force-remove because the\n // worktree may hold uncommitted experiment state we're discarding.\n git(['worktree', 'remove', '--force', worktree.path], opts.repoRoot)\n git(['branch', '-D', worktree.branch], opts.repoRoot)\n },\n }\n}\n\n/** Resolve a `CodeSurface`'s worktreeRef to a directory the measurement can\n * run the worker in. A path ref is returned as-is; anything else is treated\n * as a ref under the adapter's worktree dir. */\nexport function resolveWorktreePath(surface: CodeSurface, worktreeDir?: string): string {\n if (isAbsolute(surface.worktreeRef) && existsSync(surface.worktreeRef)) return surface.worktreeRef\n if (worktreeDir) return join(worktreeDir, basename(surface.worktreeRef))\n return surface.worktreeRef\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDO,SAAS,gBAAgB,SAAiB,OAA0C;AACzF,MAAI,QAAQ,QAAQ,MAAM,IAAI;AAC9B,MAAI,UAAU;AACd,QAAM,WAAkC,CAAC;AACzC,QAAM,WAAW,CAAC,WAA2B,MAAM,UAAU,CAAC,MAAM,EAAE,SAAS,MAAM,CAAC;AAEtF,aAAW,MAAM,MAAM,KAAK;AAC1B,QAAI,GAAG,OAAO,OAAO;AACnB,UAAI,OAAO,GAAG,SAAS,YAAY,GAAG,KAAK,KAAK,MAAM,IAAI;AACxD,iBAAS,KAAK,EAAE,IAAI,QAAQ,iBAAiB,CAAC;AAC9C;AAAA,MACF;AACA,YAAM,SAAS,GAAG,KAAK,MAAM,IAAI;AACjC,UAAI,GAAG,UAAU,UAAa,GAAG,UAAU,IAAI;AAC7C,gBAAQ,CAAC,GAAG,OAAO,GAAG,MAAM;AAC5B;AACA;AAAA,MACF;AACA,YAAM,MAAM,SAAS,GAAG,KAAK;AAC7B,UAAI,QAAQ,IAAI;AACd,iBAAS,KAAK,EAAE,IAAI,QAAQ,yBAAyB,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC;AAC3E;AAAA,MACF;AACA,cAAQ,CAAC,GAAG,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,GAAG,QAAQ,GAAG,MAAM,MAAM,MAAM,CAAC,CAAC;AACvE;AAAA,IACF,WAAW,GAAG,OAAO,UAAU;AAC7B,YAAM,MAAM,SAAS,GAAG,MAAM;AAC9B,UAAI,QAAQ,IAAI;AACd,iBAAS,KAAK,EAAE,IAAI,QAAQ,4BAA4B,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC;AAC/E;AAAA,MACF;A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AAS,IAAI;AAChC,YAAI,CAAC,UAAU,MAAM,OAAO,GAAG,SAAS,IAAI;AAAA,MAC9C;AACA,aAAO;AAAA,QACL,MAAM;AAAA,QACN,aAAa,SAAS;AAAA,QACtB,SAAS,SAAS;AAAA,QAClB;AAAA,MACF;AAAA,IACF;AAAA,IAEA,MAAM,QAAQ,UAAU;AAGtB,UAAI,CAAC,YAAY,UAAU,WAAW,SAAS,IAAI,GAAG,KAAK,QAAQ;AACnE,UAAI,CAAC,UAAU,MAAM,SAAS,MAAM,GAAG,KAAK,QAAQ;AAAA,IACtD;AAAA,EACF;AACF;AAKO,SAAS,oBAAoB,SAAsB,aAA8B;AACtF,MAAI,WAAW,QAAQ,WAAW,KAAKD,YAAW,QAAQ,WAAW,EAAG,QAAO,QAAQ;AACvF,MAAI,YAAa,QAAOC,MAAK,aAAa,SAAS,QAAQ,WAAW,CAAC;AACvE,SAAO,QAAQ;AACjB;","names":["createHash","join","createHash","mean","join","existsSync","join","slug"]}
|
|
1
|
+
{"version":3,"sources":["../../src/campaign/skill-patch.ts","../../src/campaign/drivers/skill-opt.ts","../../src/campaign/labeled-store/fs-adapter.ts","../../src/campaign/presets/run-skill-opt.ts","../../src/campaign/presets/compare-drivers.ts","../../src/campaign/presets/run-profile-matrix.ts","../../src/campaign/worktree/index.ts"],"sourcesContent":["/**\n * @experimental\n *\n * SkillOpt patch primitives (Microsoft, arXiv:2605.23904 — \"Executive\n * Strategy for Self-Evolving Agent Skills\"). Where GEPA regenerates a surface\n * by reflection, SkillOpt emits BOUNDED, anchored edits to ONE skill document\n * — add / delete / replace — and accepts an edit only if it strictly improves\n * a held-out score. Bounded edits are the \"textual learning rate\": small,\n * reversible, and cheap to accept/reject, so a good rule introduced earlier is\n * not overwritten by a later sweeping rewrite.\n *\n * This module applies a patch deterministically and reports, per op, what\n * applied and what could not (a missing anchor is a rejected op, never a\n * silently dropped one). Pure, no I/O.\n */\n\n/** A single bounded edit against a skill surface.\n * - `add` — insert `text` after the first line containing `after`\n * (append to the end when `after` is absent/empty).\n * - `delete` — remove the first line containing `anchor`.\n * - `replace` — replace the first line containing `anchor` with `text`.\n * `text` may be multi-line; it is spliced in as multiple lines. Anchors match\n * the FIRST line that contains the substring (deterministic; SkillOpt is\n * expected to anchor on unique text). */\nexport type SkillPatchOp =\n | { op: 'add'; after?: string; text: string }\n | { op: 'delete'; anchor: string }\n | { op: 'replace'; anchor: string; text: string }\n\n/** A named, attributable bundle of ops the optimizer proposes as one edit. */\nexport interface SkillPatch {\n label: string\n rationale: string\n ops: SkillPatchOp[]\n}\n\nexport interface SkillPatchRejection {\n op: SkillPatchOp\n reason: string\n}\n\nexport interface ApplySkillPatchResult {\n surface: string\n /** Count of ops that mutated the surface. */\n applied: number\n /** Ops that could not apply (unanchored / empty), with the reason. The\n * surface still reflects every APPLIED op — partial application is honest,\n * and the caller decides whether a partial patch is worth scoring. */\n rejected: SkillPatchRejection[]\n}\n\n/**\n * Apply a SkillOpt patch to a text surface. Ops apply in array order against\n * the evolving line buffer (an `add after X` followed by a `delete X` sees the\n * inserted lines). A missing anchor rejects only that op; the rest still apply.\n */\nexport function applySkillPatch(surface: string, patch: SkillPatch): ApplySkillPatchResult {\n let lines = surface.split('\\n')\n let applied = 0\n const rejected: SkillPatchRejection[] = []\n const findLine = (anchor: string): number => lines.findIndex((l) => l.includes(anchor))\n\n for (const op of patch.ops) {\n if (op.op === 'add') {\n if (typeof op.text !== 'string' || op.text.trim() === '') {\n rejected.push({ op, reason: 'empty add text' })\n continue\n }\n const insert = op.text.split('\\n')\n if (op.after === undefined || op.after === '') {\n lines = [...lines, ...insert]\n applied++\n continue\n }\n const idx = findLine(op.after)\n if (idx === -1) {\n rejected.push({ op, reason: `add anchor not found: ${truncate(op.after)}` })\n continue\n }\n lines = [...lines.slice(0, idx + 1), ...insert, ...lines.slice(idx + 1)]\n applied++\n } else if (op.op === 'delete') {\n const idx = findLine(op.anchor)\n if (idx === -1) {\n rejected.push({ op, reason: `delete anchor not found: ${truncate(op.anchor)}` })\n continue\n }\n lines = [...lines.slice(0, idx), ...lines.slice(idx + 1)]\n applied++\n } else {\n // replace\n const idx = findLine(op.anchor)\n if (idx === -1) {\n rejected.push({ op, reason: `replace anchor not found: ${truncate(op.anchor)}` })\n continue\n }\n if (typeof op.text !== 'string') {\n rejected.push({ op, reason: 'replace text missing' })\n continue\n }\n lines = [...lines.slice(0, idx), ...op.text.split('\\n'), ...lines.slice(idx + 1)]\n applied++\n }\n }\n\n return { surface: lines.join('\\n'), applied, rejected }\n}\n\n/** Total ops in a patch — the edit-budget axis (SkillOpt's \"textual learning\n * rate\" caps this per epoch). */\nexport function patchEditCount(patch: SkillPatch): number {\n return patch.ops.length\n}\n\nfunction truncate(s: string, max = 48): string {\n return s.length <= max ? s : `${s.slice(0, max)}…`\n}\n","/**\n * @experimental\n *\n * `skillOptDriver` — a patch-mode `ImprovementDriver` implementing SkillOpt\n * (Microsoft, arXiv:2605.23904). Where `gepaDriver` regenerates the whole\n * surface by reflection, SkillOpt proposes BOUNDED, anchored edits\n * (add/delete/replace) to ONE skill document, so a good rule introduced\n * earlier is not clobbered by a later sweeping rewrite. The edit budget is the\n * paper's \"textual learning rate\"; a rejected-edit buffer + a slow-update\n * meta-note steer the optimizer away from dead ends.\n *\n * This module is the PROPOSER — the LLM call that turns evidence into\n * structured patches. The accept-only-if-held-out-improves loop, the budget\n * annealing, and the rejected buffer live in the `runSkillOpt` preset, which\n * owns the epoch hill-climb. The driver also conforms to `ImprovementDriver`\n * (`propose` applies its patches to the current surface and returns the\n * candidate surfaces) so it is a drop-in for `runOptimization` and a fair\n * entrant in `compareDrivers`.\n */\n\nimport { callLlm, type LlmClientOptions } from '../../llm-client'\nimport { applySkillPatch, type SkillPatch, type SkillPatchOp } from '../skill-patch'\nimport type { ImprovementDriver, ProposeContext, ProposedCandidate } from '../types'\n\nconst SKILLOPT_SYSTEM =\n 'You are a SkillOpt optimizer. You improve ONE skill document by proposing ' +\n 'BOUNDED, anchored edits — never a full rewrite. Output ONLY a JSON object of ' +\n 'shape {\"patches\":[{\"label\":string,\"rationale\":string,\"ops\":[op,...]}]} where ' +\n 'each op is one of: {\"op\":\"add\",\"after\":<exact substring of an existing line, ' +\n 'or omit to append>,\"text\":<new line(s)>}, {\"op\":\"delete\",\"anchor\":<exact ' +\n 'substring of the line to remove>}, {\"op\":\"replace\",\"anchor\":<exact substring ' +\n 'of the line to replace>,\"text\":<replacement line(s)>}. Anchors MUST be ' +\n 'verbatim substrings of lines that exist in the document. No prose outside JSON.'\n\n/** Evidence the optimizer reflects on: where the current surface is weakest.\n * Computed by the caller (the preset uses a TRAIN campaign so proposals never\n * see the held-out split; the generic loop derives it from history). */\nexport interface SkillOptEvidence {\n /** Lowest-scoring scenarios (drives WHICH behavior to patch). */\n weakScenarios: Array<{ scenarioId: string; composite: number }>\n /** Lowest-scoring judge dimensions (drives WHAT to patch for). */\n weakDimensions: Array<{ dimension: string; score: number }>\n}\n\n/** A patch that was tried and not accepted — fed back to the model so it does\n * not re-propose a dead end (SkillOpt's rejected-edit buffer). */\nexport interface RejectedEdit {\n label: string\n rationale: string\n reason: string\n}\n\nexport interface ProposePatchesArgs {\n surface: string\n evidence: SkillOptEvidence\n /** Max ops per patch this round (the annealed textual learning rate). */\n editBudget: number\n rejectedBuffer: RejectedEdit[]\n /** Slow-update meta guidance accumulated across epochs. */\n metaNote?: string\n /** How many candidate patches to propose. */\n count: number\n signal: AbortSignal\n}\n\nexport interface SkillOptDriverOptions {\n llm: LlmClientOptions\n model: string\n /** What the skill document governs — orients the prompt. */\n target: string\n /** Default ops-per-patch cap when used as a bare `ImprovementDriver`. The\n * `runSkillOpt` preset overrides this per epoch as it anneals. Default 3. */\n editBudget?: number\n temperature?: number\n maxTokens?: number\n /** Top-K weak scenarios/dimensions surfaced as evidence. Default 3. */\n evidenceK?: number\n}\n\nexport interface SkillOptDriver extends ImprovementDriver {\n /** Patch-native path used by `runSkillOpt` (the SkillOpt epoch loop owns\n * acceptance/budget/buffer). Returns structured patches, NOT surfaces. */\n proposePatches(args: ProposePatchesArgs): Promise<SkillPatch[]>\n}\n\nexport function skillOptDriver(opts: SkillOptDriverOptions): SkillOptDriver {\n const evidenceK = opts.evidenceK ?? 3\n const defaultBudget = opts.editBudget ?? 3\n\n async function proposePatches(args: ProposePatchesArgs): Promise<SkillPatch[]> {\n const userPrompt = buildPatchPrompt({\n target: opts.target,\n surface: args.surface,\n evidence: args.evidence,\n editBudget: args.editBudget,\n rejectedBuffer: args.rejectedBuffer,\n metaNote: args.metaNote,\n count: args.count,\n })\n const result = await callLlm(\n {\n model: opts.model,\n messages: [\n { role: 'system', content: SKILLOPT_SYSTEM },\n { role: 'user', content: userPrompt },\n ],\n jsonMode: true,\n temperature: opts.temperature ?? 0.6,\n maxTokens: opts.maxTokens ?? 4000,\n },\n opts.llm,\n )\n return parseSkillPatchResponse(result.content, args.count, args.editBudget)\n }\n\n return {\n kind: 'skill-opt',\n proposePatches,\n async propose(ctx: ProposeContext): Promise<ProposedCandidate[]> {\n if (typeof ctx.currentSurface !== 'string') {\n throw new Error(\n 'skillOptDriver: surface must be a string skill document (got a CodeSurface). SkillOpt patches text.',\n )\n }\n const surface = ctx.currentSurface\n const patches = await proposePatches({\n surface,\n evidence: evidenceFromHistory(ctx, evidenceK),\n editBudget: defaultBudget,\n rejectedBuffer: [],\n count: ctx.populationSize,\n signal: ctx.signal,\n })\n const out: ProposedCandidate[] = []\n const seen = new Set<string>()\n for (const patch of patches) {\n const { surface: candidate, applied } = applySkillPatch(surface, patch)\n if (applied === 0 || candidate === surface || seen.has(candidate)) continue\n seen.add(candidate)\n out.push({ surface: candidate, label: patch.label, rationale: patch.rationale })\n if (out.length >= ctx.populationSize) break\n }\n return out\n },\n }\n}\n\n/** Derive evidence from the loop's generation history (generic-driver path):\n * the prior best candidate's worst scenarios + weakest dimensions. Empty on\n * generation 0. */\nfunction evidenceFromHistory(ctx: ProposeContext, k: number): SkillOptEvidence {\n const last = ctx.history.at(-1)\n if (!last || last.candidates.length === 0) return { weakScenarios: [], weakDimensions: [] }\n const best = [...last.candidates].sort((a, b) => b.composite - a.composite)[0]\n if (!best) return { weakScenarios: [], weakDimensions: [] }\n const weakScenarios = [...best.scenarios].sort((a, b) => a.composite - b.composite).slice(0, k)\n const weakDimensions = Object.entries(best.dimensions)\n .sort((a, b) => a[1] - b[1])\n .slice(0, k)\n .map(([dimension, score]) => ({ dimension, score }))\n return { weakScenarios, weakDimensions }\n}\n\nfunction buildPatchPrompt(args: {\n target: string\n surface: string\n evidence: SkillOptEvidence\n editBudget: number\n rejectedBuffer: RejectedEdit[]\n metaNote?: string\n count: number\n}): string {\n const lines: string[] = [\n `Skill document governs: ${args.target}.`,\n '',\n 'Current skill document:',\n '```',\n args.surface,\n '```',\n '',\n `Propose ${args.count} candidate patch(es). Each patch is a SMALL bundle of`,\n `at most ${args.editBudget} op(s). Anchors must be verbatim substrings of`,\n 'existing lines. Prefer adding a specific missing rule or sharpening a vague',\n 'one over deleting; never rewrite the whole document.',\n ]\n if (args.evidence.weakScenarios.length > 0) {\n lines.push(\n '',\n 'Weakest scenarios (patch to fix these):',\n ...args.evidence.weakScenarios.map((s) => `- ${s.scenarioId} (${s.composite.toFixed(2)})`),\n )\n }\n if (args.evidence.weakDimensions.length > 0) {\n lines.push(\n '',\n 'Weakest dimensions (what to improve):',\n ...args.evidence.weakDimensions.map((d) => `- ${d.dimension} (${d.score.toFixed(2)})`),\n )\n }\n if (args.rejectedBuffer.length > 0) {\n lines.push(\n '',\n 'Already tried and REJECTED (do not repeat or restate these edits):',\n ...args.rejectedBuffer.map((e) => `- ${e.label}: ${e.rationale} — ${e.reason}`),\n )\n }\n if (args.metaNote) {\n lines.push('', `Strategy note from prior epochs: ${args.metaNote}`)\n }\n return lines.join('\\n')\n}\n\n/** Parse + validate the patch response. Throws `SkillPatchParseError` when the\n * response is not valid JSON at all (a router/model failure the caller must\n * see — never a silent no-op epoch). Returns `[]` only for the legitimate\n * \"valid JSON, zero usable patches\" case. Malformed ops within a patch are\n * dropped (not silently mutated); each patch is truncated to the edit budget. */\nexport class SkillPatchParseError extends Error {\n constructor(message: string) {\n super(message)\n this.name = 'SkillPatchParseError'\n }\n}\n\nexport function parseSkillPatchResponse(\n raw: string,\n maxPatches: number,\n editBudget: number,\n): SkillPatch[] {\n let text = raw.trim()\n if (text.startsWith('```')) text = text.replace(/^```(?:json)?\\n?/, '').replace(/\\n?```$/, '')\n const start = text.indexOf('{')\n const end = text.lastIndexOf('}')\n if (start < 0 || end <= start) {\n throw new SkillPatchParseError(\n `parseSkillPatchResponse: response was not valid JSON (no object found): ${snippet(raw)}`,\n )\n }\n let parsed: { patches?: unknown }\n try {\n parsed = JSON.parse(text.slice(start, end + 1))\n } catch (err) {\n throw new SkillPatchParseError(\n `parseSkillPatchResponse: response was not valid JSON (${\n err instanceof Error ? err.message : String(err)\n }): ${snippet(raw)}`,\n )\n }\n const rawPatches = Array.isArray(parsed.patches) ? parsed.patches : []\n const out: SkillPatch[] = []\n for (const rp of rawPatches) {\n if (typeof rp !== 'object' || rp === null) continue\n const obj = rp as Record<string, unknown>\n const ops = Array.isArray(obj.ops) ? obj.ops.map(normalizeOp).filter(isOp) : []\n if (ops.length === 0) continue\n out.push({\n label: typeof obj.label === 'string' ? obj.label : 'patch',\n rationale: typeof obj.rationale === 'string' ? obj.rationale : '',\n ops: ops.slice(0, editBudget),\n })\n if (out.length >= maxPatches) break\n }\n return out\n}\n\nfunction normalizeOp(raw: unknown): SkillPatchOp | null {\n if (typeof raw !== 'object' || raw === null) return null\n const o = raw as Record<string, unknown>\n if (o.op === 'add') {\n if (typeof o.text !== 'string') return null\n const op: SkillPatchOp = { op: 'add', text: o.text }\n if (typeof o.after === 'string') op.after = o.after\n return op\n }\n if (o.op === 'delete') {\n if (typeof o.anchor !== 'string') return null\n return { op: 'delete', anchor: o.anchor }\n }\n if (o.op === 'replace') {\n if (typeof o.anchor !== 'string' || typeof o.text !== 'string') return null\n return { op: 'replace', anchor: o.anchor, text: o.text }\n }\n return null\n}\n\nfunction isOp(op: SkillPatchOp | null): op is SkillPatchOp {\n return op !== null\n}\n\nfunction snippet(s: string, max = 120): string {\n const t = s.trim().replace(/\\s+/g, ' ')\n return t.length <= max ? t : `${t.slice(0, max)}…`\n}\n","/**\n * @experimental\n *\n * Filesystem `LabeledScenarioStore` adapter. The default capture sink for\n * traces + eval artifacts. Production deployments typically swap for a\n * Turso/SQLite adapter (same interface).\n *\n * Records land as one JSONL file per source under `<root>/<source>.jsonl`.\n * Each line is a `LabeledScenarioRecord`. Append-only — no in-place edits.\n *\n * Safety properties enforced at write-time:\n *\n * - **Provenance required**: writes without `source`, `sourceVersionHash`,\n * `capturedAt`, `redactionStatus` are rejected. Closes the alignment\n * reviewer's data-poisoning gap.\n * - **Per-source rate limits**: optional `rateLimitBucket` + `maxWritesPerMinute`\n * stops a single tenant/source from flooding the store.\n *\n * Safety properties enforced at sample-time:\n *\n * - **Required split + capturedBefore**: substrate refuses to sample without\n * an explicit `split` ('train' | 'test') AND a temporal cutoff. Eliminates\n * accidental train/test contamination.\n * - **Default training-source filter**: when the store is sampled with\n * `split: 'train'`, production-trace records are EXCLUDED unless the\n * caller passes `filter.source: 'production-trace'` explicitly. Closes\n * the contamination-by-default gap flagged by the senior eval engineer.\n */\n\nimport { createHash } from 'node:crypto'\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { join } from 'node:path'\nimport type {\n LabeledScenarioRecord,\n LabeledScenarioSampleArgs,\n LabeledScenarioSource,\n LabeledScenarioStore,\n LabeledScenarioWrite,\n LabelTrust,\n} from '../types'\nimport { labelTrustRank } from '../types'\n\nexport interface FsLabeledScenarioStoreOptions {\n /** Root directory for JSONL files. Created if missing. */\n root: string\n /** Per-source rate limit. When set, writes exceeding the cap are rejected\n * with a typed error. Default: no limit. */\n maxWritesPerMinutePerBucket?: number\n /** Test seam — override `Date.now()` for deterministic tests. */\n now?: () => number\n}\n\nexport class LabeledScenarioStoreError extends Error {\n constructor(\n public readonly code: string,\n message: string,\n ) {\n super(message)\n this.name = 'LabeledScenarioStoreError'\n }\n}\n\ninterface RateLimitState {\n bucket: string\n windowStartMs: number\n count: number\n}\n\nexport class FsLabeledScenarioStore implements LabeledScenarioStore {\n private readonly now: () => number\n private readonly rateLimits = new Map<string, RateLimitState>()\n\n constructor(private readonly options: FsLabeledScenarioStoreOptions) {\n if (!existsSync(options.root)) mkdirSync(options.root, { recursive: true })\n this.now = options.now ?? Date.now\n }\n\n async observe(write: LabeledScenarioWrite): Promise<void> {\n this.assertProvenance(write)\n this.assertRateLimit(write)\n const record = this.toRecord(write)\n const path = this.pathForSource(write.source)\n const line = `${JSON.stringify(record)}\\n`\n // Append atomically. For high-throughput a writev-friendly buffered\n // implementation lands in the Turso adapter; FS adapter is for tests +\n // local dev + small workloads.\n appendLine(path, line)\n }\n\n async sample(args: LabeledScenarioSampleArgs): Promise<LabeledScenarioRecord[]> {\n if (!args.split) {\n throw new LabeledScenarioStoreError(\n 'split_required',\n 'sample() requires an explicit `split` (train | test) — substrate refuses ambiguous reads',\n )\n }\n if (!args.capturedBefore) {\n throw new LabeledScenarioStoreError(\n 'capturedBefore_required',\n 'sample() requires an explicit `capturedBefore` timestamp for temporal-split discipline',\n )\n }\n\n const all: LabeledScenarioRecord[] = []\n for (const source of ALL_SOURCES) {\n // Default training-source filter: when sampling train, EXCLUDE\n // production-trace records unless the caller asks for them.\n if (args.split === 'train' && source === 'production-trace') {\n const explicit = sourceFilterContains(args.filter?.source, 'production-trace')\n if (!explicit) continue\n }\n const path = this.pathForSource(source)\n if (!existsSync(path)) continue\n const lines = readFileSync(path, 'utf8').split('\\n').filter(Boolean)\n for (const line of lines) {\n let record: LabeledScenarioRecord\n try {\n record = JSON.parse(line) as LabeledScenarioRecord\n } catch {\n continue\n }\n if (!matchesFilter(record, args, source)) continue\n all.push(record)\n }\n }\n\n // Deterministic order: by capturedAt ascending, then recordHash.\n all.sort((a, b) => {\n if (a.capturedAt !== b.capturedAt) return a.capturedAt.localeCompare(b.capturedAt)\n return a.recordHash.localeCompare(b.recordHash)\n })\n\n return all.slice(0, args.count)\n }\n\n async size(): Promise<{\n train: number\n test: number\n bySource: Record<string, number>\n byTrust: Record<LabelTrust, number>\n }> {\n const bySource: Record<string, number> = {}\n const byTrust: Record<LabelTrust, number> = {\n unverified: 0,\n 'verified-signal': 0,\n 'human-rated': 0,\n }\n let total = 0\n for (const source of ALL_SOURCES) {\n const path = this.pathForSource(source)\n if (!existsSync(path)) {\n bySource[source] = 0\n continue\n }\n const lines = readFileSync(path, 'utf8').split('\\n').filter(Boolean)\n bySource[source] = lines.length\n total += lines.length\n for (const line of lines) {\n let trust: LabelTrust = 'unverified'\n try {\n trust = (JSON.parse(line) as LabeledScenarioRecord).labelTrust ?? 'unverified'\n } catch {\n // A malformed line counts as unverified — never silently gold.\n }\n byTrust[trust] += 1\n }\n }\n // FS adapter doesn't track split assignments per-record (split is\n // computed at sample-time based on `capturedBefore`). For size(), we\n // report `train`+`test` as the same total — split is a sampling concept.\n return { train: total, test: total, bySource, byTrust }\n }\n\n private assertProvenance(write: LabeledScenarioWrite): void {\n if (!write.source) {\n throw new LabeledScenarioStoreError(\n 'missing_source',\n 'LabeledScenarioWrite requires `source`',\n )\n }\n if (!write.sourceVersionHash || write.sourceVersionHash.length === 0) {\n throw new LabeledScenarioStoreError(\n 'missing_source_version',\n 'LabeledScenarioWrite requires `sourceVersionHash` (git sha or substrate version)',\n )\n }\n if (!write.capturedAt) {\n throw new LabeledScenarioStoreError(\n 'missing_captured_at',\n 'LabeledScenarioWrite requires `capturedAt` ISO timestamp',\n )\n }\n if (!write.redactionStatus) {\n throw new LabeledScenarioStoreError(\n 'missing_redaction_status',\n 'LabeledScenarioWrite requires explicit `redactionStatus` — raw / redacted-pii / redacted-secrets / fully-redacted',\n )\n }\n if (!ALL_SOURCES.includes(write.source)) {\n throw new LabeledScenarioStoreError(\n 'unknown_source',\n `LabeledScenarioWrite.source must be one of: ${ALL_SOURCES.join(', ')}`,\n )\n }\n }\n\n private assertRateLimit(write: LabeledScenarioWrite): void {\n const cap = this.options.maxWritesPerMinutePerBucket\n if (!cap || !write.rateLimitBucket) return\n const now = this.now()\n const windowMs = 60_000\n let state = this.rateLimits.get(write.rateLimitBucket)\n if (!state || now - state.windowStartMs >= windowMs) {\n state = { bucket: write.rateLimitBucket, windowStartMs: now, count: 0 }\n this.rateLimits.set(write.rateLimitBucket, state)\n }\n if (state.count >= cap) {\n throw new LabeledScenarioStoreError(\n 'rate_limit_exceeded',\n `LabeledScenarioStore: bucket ${write.rateLimitBucket} exceeded ${cap} writes/min`,\n )\n }\n state.count += 1\n }\n\n private toRecord(write: LabeledScenarioWrite): LabeledScenarioRecord {\n const recordHash = sha256(\n JSON.stringify({\n id: write.scenario.id,\n src: write.source,\n at: write.capturedAt,\n ver: write.sourceVersionHash,\n }),\n )\n // FS adapter assigns split at sample-time, but we cache a hint here\n // based on capturedAt vs the world's \"now\" — sampler overrides this.\n return {\n ...write,\n recordHash,\n split: 'train',\n }\n }\n\n private pathForSource(source: string): string {\n return join(this.options.root, `${source}.jsonl`)\n }\n}\n\nconst ALL_SOURCES: LabeledScenarioWrite['source'][] = [\n 'production-trace',\n 'eval-run',\n 'manual',\n 'red-team',\n 'synthetic',\n]\n\nfunction sourceFilterContains(\n filter: LabeledScenarioSource | LabeledScenarioSource[] | undefined,\n needle: LabeledScenarioSource,\n): boolean {\n if (!filter) return false\n if (Array.isArray(filter)) return filter.includes(needle)\n return filter === needle\n}\n\nfunction matchesFilter(\n record: LabeledScenarioRecord,\n args: LabeledScenarioSampleArgs,\n source: string,\n): boolean {\n // Temporal cutoff — train must be capturedAt < capturedBefore.\n if (args.split === 'train' && record.capturedAt >= args.capturedBefore) return false\n if (args.split === 'test' && record.capturedAt < args.capturedBefore) return false\n\n const f = args.filter\n if (!f) return true\n if (f.kind && record.scenario.kind !== f.kind) return false\n if (f.source) {\n const sources = Array.isArray(f.source) ? f.source : [f.source]\n if (!sources.includes(source as never)) return false\n }\n if (f.minComposite !== undefined || f.maxComposite !== undefined) {\n const composites = Object.values(record.judgeScores).map((s) => s.composite)\n const max = composites.length === 0 ? 0 : Math.max(...composites)\n if (f.minComposite !== undefined && max < f.minComposite) return false\n if (f.maxComposite !== undefined && max > f.maxComposite) return false\n }\n if (f.minTrust !== undefined && labelTrustRank(record.labelTrust) < labelTrustRank(f.minTrust)) {\n return false\n }\n return true\n}\n\nfunction sha256(input: string): string {\n return createHash('sha256').update(input).digest('hex').slice(0, 16)\n}\n\nfunction appendLine(path: string, line: string): void {\n if (existsSync(path)) {\n const existing = readFileSync(path, 'utf8')\n writeFileSync(path, existing + line)\n } else {\n writeFileSync(path, line)\n }\n}\n","/**\n * @experimental\n *\n * `runSkillOpt` — the SkillOpt epoch hill-climb (Microsoft, arXiv:2605.23904).\n * Unlike `runOptimization`'s population/promote-top-K search, SkillOpt is a\n * sequential, held-out-gated hill-climb on ONE skill document:\n *\n * each epoch:\n * 1. reflect on the CURRENT surface's weakest TRAIN scenarios/dimensions\n * (never the held-out split — proposals must not see the acceptance axis)\n * 2. propose ≤ `patchesPerEpoch` bounded patches (≤ `editBudget` ops each)\n * 3. apply each; score the candidate on the HELD-OUT split\n * 4. ACCEPT the first patch that STRICTLY improves the held-out composite;\n * otherwise push it to the rejected-edit buffer (fed back so the model\n * does not re-propose dead ends)\n * 5. anneal the edit budget down after consecutive rejections (the\n * \"textual learning rate\" decay); refresh the slow-update meta note\n * 6. stop at `maxEpochs` or after `patience` epochs with no acceptance\n *\n * The accept-only-if-held-out-improves rule is the same discipline as\n * `HeldOutGate`/`defaultProductionGate`, applied per edit instead of once at\n * the end — which is why the held-out composite is monotonically\n * non-decreasing and a regression can never ship. `runCampaign` is the\n * measurement; `applySkillPatch` applies the edits; `skillOptDriver` proposes\n * them.\n */\n\nimport type { RejectedEdit, SkillOptDriver, SkillOptEvidence } from '../drivers/skill-opt'\nimport { type RunCampaignOptions, runCampaign } from '../run-campaign'\nimport { campaignBreakdown, campaignMeanComposite } from '../score-utils'\nimport { applySkillPatch } from '../skill-patch'\nimport type { CampaignResult, DispatchContext, Scenario } from '../types'\n\nexport interface RunSkillOptOptions<TScenario extends Scenario, TArtifact>\n extends Omit<RunCampaignOptions<TScenario, TArtifact>, 'dispatch' | 'scenarios'> {\n /** The skill document being optimized. */\n baselineSurface: string\n /** Dispatcher taking the CURRENT skill surface + scenario → artifact. */\n dispatchWithSurface: (\n surface: string,\n scenario: TScenario,\n ctx: DispatchContext,\n ) => Promise<TArtifact>\n driver: SkillOptDriver\n /** Scenarios the optimizer reflects on for evidence. MUST be disjoint from\n * `holdoutScenarios` — proposals never see the acceptance axis. */\n trainScenarios: TScenario[]\n /** Held-out scenarios. An edit is accepted ONLY if it strictly improves the\n * mean composite here. */\n holdoutScenarios: TScenario[]\n maxEpochs: number\n /** Candidate patches proposed per epoch. Default 2. */\n patchesPerEpoch?: number\n /** Initial ops-per-patch cap (the textual learning rate). Default 3. */\n editBudget?: number\n /** Strict acceptance margin: accept iff the held-out composite improves by\n * MORE than this. Default 0 (any strict improvement). */\n minImprovement?: number\n /** Stop after this many consecutive epochs with no acceptance. Default =\n * `maxEpochs` (never early-stop). */\n patience?: number\n /** Shrink the edit budget by 1 after 2 consecutive rejected epochs (min 1).\n * Default true. */\n budgetAnneal?: boolean\n /** Cap on the rejected-edit buffer (most-recent kept). Default 12. */\n rejectedBufferSize?: number\n /** Refresh the slow-update meta note every N epochs. Default 2. 0 disables. */\n slowMetaEvery?: number\n /** Top-K weak scenarios/dimensions surfaced as evidence each epoch. Default 3. */\n evidenceK?: number\n /** Abort signal forwarded to the patch-proposing LLM calls. */\n signal?: AbortSignal\n}\n\nexport interface AcceptedEdit {\n epoch: number\n label: string\n rationale: string\n /** Held-out composite improvement vs the surface before this edit. */\n holdoutDelta: number\n}\n\nexport interface SkillOptEpochRecord {\n epoch: number\n editBudget: number\n proposed: number\n /** The accepted edit this epoch, or null if every proposal was rejected. */\n accepted: AcceptedEdit | null\n rejected: RejectedEdit[]\n /** Held-out composite of the CURRENT surface at the END of the epoch. */\n holdoutComposite: number\n}\n\nexport interface RunSkillOptResult {\n winnerSurface: string\n baselineHoldoutComposite: number\n winnerHoldoutComposite: number\n /** `winnerHoldoutComposite - baselineHoldoutComposite` — monotonically ≥ 0\n * by construction (only strictly-improving edits are accepted). */\n lift: number\n acceptedEdits: AcceptedEdit[]\n rejectedEdits: RejectedEdit[]\n epochsRun: number\n history: SkillOptEpochRecord[]\n /** Total cost across every scoring campaign (train evidence + holdout\n * acceptance) the hill-climb ran. */\n totalCostUsd: number\n}\n\nexport async function runSkillOpt<TScenario extends Scenario, TArtifact>(\n opts: RunSkillOptOptions<TScenario, TArtifact>,\n): Promise<RunSkillOptResult> {\n if (opts.trainScenarios.length === 0) throw new Error('runSkillOpt: trainScenarios is empty')\n if (opts.holdoutScenarios.length === 0) throw new Error('runSkillOpt: holdoutScenarios is empty')\n if (!opts.judges || opts.judges.length === 0) {\n throw new Error(\n 'runSkillOpt: at least one judge is required — scoring (and therefore acceptance) is meaningless without one, and would report a silent zero lift.',\n )\n }\n // train ∩ holdout must be empty: proposals reflect on TRAIN evidence, so any\n // overlap leaks the acceptance axis into the proposal (held-out contamination).\n const holdoutIds = new Set(opts.holdoutScenarios.map((s) => s.id))\n const overlap = opts.trainScenarios.filter((s) => holdoutIds.has(s.id)).map((s) => s.id)\n if (overlap.length > 0) {\n throw new Error(\n `runSkillOpt: trainScenarios and holdoutScenarios must be disjoint (overlap: [${overlap.join(\n ', ',\n )}]) — a shared scenario leaks the held-out acceptance axis into the proposal evidence.`,\n )\n }\n\n const patchesPerEpoch = opts.patchesPerEpoch ?? 2\n const initialBudget = opts.editBudget ?? 3\n const minImprovement = opts.minImprovement ?? 0\n if (minImprovement < 0) {\n throw new Error(\n 'runSkillOpt: minImprovement must be >= 0 — a negative threshold would accept held-out regressions, breaking the monotonic-lift contract.',\n )\n }\n const patience = opts.patience ?? opts.maxEpochs\n const budgetAnneal = opts.budgetAnneal ?? true\n const rejectedBufferSize = opts.rejectedBufferSize ?? 12\n const slowMetaEvery = opts.slowMetaEvery ?? 2\n\n let totalCostUsd = 0\n const scoreHoldout = async (surface: string, tag: string): Promise<number> => {\n const campaign = await runScoringCampaign(opts, opts.holdoutScenarios, surface, tag)\n totalCostUsd += campaign.aggregates.totalCostUsd\n return campaignMeanComposite(campaign)\n }\n const evidenceK = opts.evidenceK ?? 3\n const trainEvidence = async (surface: string, tag: string): Promise<SkillOptEvidence> => {\n const campaign = await runScoringCampaign(opts, opts.trainScenarios, surface, tag)\n totalCostUsd += campaign.aggregates.totalCostUsd\n return toEvidence(campaign, evidenceK)\n }\n\n let current = opts.baselineSurface\n let currentEvidence = await trainEvidence(current, 'baseline-train')\n const baselineHoldout = await scoreHoldout(current, 'baseline-holdout')\n let currentHoldout = baselineHoldout\n\n const buffer: RejectedEdit[] = []\n const acceptedEdits: AcceptedEdit[] = []\n const rejectedAll: RejectedEdit[] = []\n const history: SkillOptEpochRecord[] = []\n let budget = initialBudget\n let sinceAccept = 0\n let metaNote: string | undefined\n let epochsRun = 0\n\n for (let epoch = 0; epoch < opts.maxEpochs; epoch++) {\n epochsRun++\n const patches = await opts.driver.proposePatches({\n surface: current,\n evidence: currentEvidence,\n editBudget: budget,\n rejectedBuffer: buffer,\n metaNote,\n count: patchesPerEpoch,\n signal: opts.signal ?? new AbortController().signal,\n })\n\n let accepted: AcceptedEdit | null = null\n const rejectedThisEpoch: RejectedEdit[] = []\n for (let i = 0; i < patches.length; i++) {\n const patch = patches[i]!\n const { surface: candidate, applied } = applySkillPatch(current, patch)\n if (applied === 0 || candidate === current) {\n rejectedThisEpoch.push({\n label: patch.label,\n rationale: patch.rationale,\n reason: 'no-op (unanchored or zero-change)',\n })\n continue\n }\n const candidateHoldout = await scoreHoldout(candidate, `epoch-${epoch}-cand-${i}-holdout`)\n if (candidateHoldout > currentHoldout + minImprovement) {\n accepted = {\n epoch,\n label: patch.label,\n rationale: patch.rationale,\n holdoutDelta: candidateHoldout - currentHoldout,\n }\n current = candidate\n currentHoldout = candidateHoldout\n // The surface changed — recompute evidence for the next epoch from it.\n currentEvidence = await trainEvidence(current, `epoch-${epoch}-train`)\n break // greedy: take the first strictly-improving edit\n }\n rejectedThisEpoch.push({\n label: patch.label,\n rationale: patch.rationale,\n reason: `held-out ${candidateHoldout.toFixed(3)} ≤ current ${currentHoldout.toFixed(3)}`,\n })\n }\n\n if (accepted) {\n acceptedEdits.push(accepted)\n sinceAccept = 0\n } else {\n sinceAccept++\n if (budgetAnneal && sinceAccept >= 2 && budget > 1) budget--\n }\n // Maintain the bounded rejected-edit buffer (steers the proposer away from\n // dead ends; capped so the prompt stays bounded).\n for (const r of rejectedThisEpoch) {\n buffer.push(r)\n rejectedAll.push(r)\n }\n while (buffer.length > rejectedBufferSize) buffer.shift()\n\n // Slow-update meta note: a cross-epoch digest refreshed on a slow cadence.\n if (slowMetaEvery > 0 && (epoch + 1) % slowMetaEvery === 0) {\n metaNote = buildMetaNote(acceptedEdits, buffer)\n }\n\n history.push({\n epoch,\n editBudget: budget,\n proposed: patches.length,\n accepted,\n rejected: rejectedThisEpoch,\n holdoutComposite: currentHoldout,\n })\n\n if (sinceAccept >= patience) break\n }\n\n return {\n winnerSurface: current,\n baselineHoldoutComposite: baselineHoldout,\n winnerHoldoutComposite: currentHoldout,\n lift: currentHoldout - baselineHoldout,\n acceptedEdits,\n rejectedEdits: rejectedAll,\n epochsRun,\n history,\n totalCostUsd,\n }\n}\n\nfunction runScoringCampaign<TScenario extends Scenario, TArtifact>(\n opts: RunSkillOptOptions<TScenario, TArtifact>,\n scenarios: TScenario[],\n surface: string,\n tag: string,\n): Promise<CampaignResult<TArtifact, TScenario>> {\n return runCampaign<TScenario, TArtifact>({\n ...opts,\n scenarios,\n dispatch: (scenario, ctx) => opts.dispatchWithSurface(surface, scenario, ctx),\n runDir: `${opts.runDir}/${tag}`,\n })\n}\n\nfunction toEvidence<TArtifact, TScenario extends Scenario>(\n campaign: CampaignResult<TArtifact, TScenario>,\n k: number,\n): SkillOptEvidence {\n const { dimensions, scenarios } = campaignBreakdown(campaign)\n const weakScenarios = [...scenarios].sort((a, b) => a.composite - b.composite).slice(0, k)\n const weakDimensions = Object.entries(dimensions)\n .sort((a, b) => a[1] - b[1])\n .slice(0, k)\n .map(([dimension, score]) => ({ dimension, score }))\n return { weakScenarios, weakDimensions }\n}\n\nfunction buildMetaNote(accepted: AcceptedEdit[], rejected: RejectedEdit[]): string {\n const parts: string[] = []\n if (accepted.length > 0) {\n parts.push(\n `Edits that improved held-out so far: ${accepted\n .map((a) => `\"${a.label}\" (+${a.holdoutDelta.toFixed(3)})`)\n .join('; ')}. Build on these.`,\n )\n }\n if (rejected.length > 0) {\n const labels = [...new Set(rejected.map((r) => r.label))].slice(0, 5)\n parts.push(`Dead ends to avoid: ${labels.join(', ')}. Try a different anchor or rule.`)\n }\n parts.push('Keep edits small and anchored to existing lines.')\n return parts.join(' ')\n}\n","/**\n * @experimental\n *\n * `compareDrivers` — a head-to-head lift benchmark across optimizer drivers on\n * ONE corpus. This is the forcing function: optimizer quality (GEPA reflection\n * vs GEPA+Pareto vs SkillOpt) becomes a NUMBER with a confidence interval, so a\n * driver regression — or shipping a simplified driver and calling it the real\n * one — turns a build red instead of going measurement-invisible.\n *\n * Every entrant is scored the SAME way: each driver returns the surface it\n * promoted, then `compareDrivers` scores the baseline + every winner on the\n * SAME held-out scenarios with the SAME judges. Apples-to-apples by\n * construction — the comparison never depends on how a driver measured itself.\n * The per-scenario held-out composites feed a paired bootstrap (`statistics.ts`)\n * for each driver's lift CI and for the pairwise \"which driver wins\" CI.\n */\n\nimport type { LlmClientOptions } from '../../llm-client'\nimport { pairedBootstrap } from '../../statistics'\nimport { gepaDriver } from '../drivers/gepa'\nimport { skillOptDriver } from '../drivers/skill-opt'\nimport { defaultProductionGate } from '../gates/default-production-gate'\nimport { type RunCampaignOptions, runCampaign } from '../run-campaign'\nimport { campaignBreakdown } from '../score-utils'\nimport type {\n CampaignResult,\n DispatchContext,\n JudgeConfig,\n MutableSurface,\n Scenario,\n} from '../types'\nimport { runImprovementLoop } from './run-improvement-loop'\nimport { runSkillOpt } from './run-skill-opt'\n\n/** What an optimizer produced: the surface it promoted + what it cost to get\n * there. `compareDrivers` does the held-out scoring itself, so an entry only\n * needs to run its loop and hand back the winner. */\nexport interface DriverEntry {\n name: string\n optimize: () => Promise<{ winnerSurface: MutableSurface; costUsd: number; durationMs?: number }>\n}\n\nexport interface DriverScore {\n name: string\n /** Mean held-out composite of the baseline (identical across drivers). */\n baselineComposite: number\n /** Mean held-out composite of this driver's promoted surface. */\n winnerComposite: number\n /** Mean per-scenario held-out lift (winner − baseline). */\n lift: number\n /** Paired-bootstrap CI of the per-scenario lift. `low > 0` ⇒ a real gain. */\n liftCi: { low: number; high: number }\n costUsd: number\n durationMs?: number\n winnerSurface: MutableSurface\n /** 1-based, by descending lift. */\n rank: number\n}\n\nexport interface DriverPairwise {\n /** Higher-ranked driver. */\n a: string\n b: string\n /** Mean per-scenario held-out delta (a − b). */\n deltaMean: number\n low: number\n high: number\n /** `a` if the CI clears 0, `b` if it is entirely negative, else `'tie'`. */\n favored: string\n}\n\nexport interface DriverComparison {\n /** Sorted by descending lift; `rank` set accordingly. */\n scores: DriverScore[]\n best: DriverScore\n /** Best vs each other driver, paired-bootstrap on the held-out winners. */\n pairwise: DriverPairwise[]\n holdoutScenarioIds: string[]\n}\n\nexport interface CompareDriversOptions<TScenario extends Scenario, TArtifact>\n extends Omit<RunCampaignOptions<TScenario, TArtifact>, 'dispatch' | 'scenarios'> {\n drivers: DriverEntry[]\n baselineSurface: MutableSurface\n /** The held-out scenarios every winner is scored on. */\n holdoutScenarios: TScenario[]\n /** Scores a surface on a scenario — the same dispatcher the drivers used. */\n dispatchWithSurface: (\n surface: MutableSurface,\n scenario: TScenario,\n ctx: DispatchContext,\n ) => Promise<TArtifact>\n /** Bootstrap resamples for the lift CIs. Default 2000. */\n resamples?: number\n /** CI confidence. Default 0.95. */\n confidence?: number\n}\n\nexport async function compareDrivers<TScenario extends Scenario, TArtifact>(\n opts: CompareDriversOptions<TScenario, TArtifact>,\n): Promise<DriverComparison> {\n if (opts.drivers.length === 0) throw new Error('compareDrivers: no drivers to compare')\n const seed = opts.seed ?? 42\n const resamples = opts.resamples ?? 2000\n const confidence = opts.confidence ?? 0.95\n\n const scoreOnHoldout = async (\n surface: MutableSurface,\n tag: string,\n ): Promise<Record<string, number>> => {\n const campaign: CampaignResult<TArtifact, TScenario> = await runCampaign<TScenario, TArtifact>({\n ...opts,\n scenarios: opts.holdoutScenarios,\n dispatch: (scenario, ctx) => opts.dispatchWithSurface(surface, scenario, ctx),\n runDir: `${opts.runDir}/${tag}`,\n })\n const byScenario: Record<string, number> = {}\n for (const { scenarioId, composite } of campaignBreakdown(campaign).scenarios) {\n byScenario[scenarioId] = composite\n }\n return byScenario\n }\n\n // The comparison axis is the DESIGNED held-out set, not whatever a campaign\n // happened to score. Align every score vector to it and FAIL LOUD if a\n // surface is missing any scenario (an errored cell / a judge that returned no\n // score). Fabricating a 0 there would silently penalize that surface and\n // corrupt the paired-bootstrap lift CI — the exact \"no silent zeros\" trap.\n const scenarioIds = [...new Set(opts.holdoutScenarios.map((s) => s.id))].sort()\n if (scenarioIds.length === 0) throw new Error('compareDrivers: holdoutScenarios is empty')\n const align = (byScenario: Record<string, number>, label: string): number[] => {\n const missing = scenarioIds.filter((id) => !(id in byScenario))\n if (missing.length > 0) {\n throw new Error(\n `compareDrivers: ${label} produced no held-out score for scenario(s) [${missing.join(\n ', ',\n )}] — a cell errored or its judges returned nothing. Refusing to fabricate a 0 (it would corrupt the lift comparison). Fix the dispatch/judge or drop the scenario.`,\n )\n }\n return scenarioIds.map((id) => byScenario[id]!)\n }\n\n const baselineArr = align(\n await scoreOnHoldout(opts.baselineSurface, 'compare-baseline'),\n 'baseline',\n )\n\n // Run + uniformly re-score every entrant on the SAME held-out axis.\n const winners: Array<{\n name: string\n winnerSurface: MutableSurface\n costUsd: number\n durationMs?: number\n arr: number[]\n }> = []\n for (const d of opts.drivers) {\n const out = await d.optimize()\n const byScenario = await scoreOnHoldout(out.winnerSurface, `compare-${slug(d.name)}`)\n winners.push({\n name: d.name,\n winnerSurface: out.winnerSurface,\n costUsd: out.costUsd,\n durationMs: out.durationMs,\n arr: align(byScenario, `driver \"${d.name}\"`),\n })\n }\n\n const scores: DriverScore[] = winners.map((w) => {\n const boot = pairedBootstrap(baselineArr, w.arr, {\n seed,\n resamples,\n confidence,\n statistic: 'mean',\n })\n const score: DriverScore = {\n name: w.name,\n baselineComposite: mean(baselineArr),\n winnerComposite: mean(w.arr),\n lift: boot.mean,\n liftCi: { low: boot.low, high: boot.high },\n costUsd: w.costUsd,\n winnerSurface: w.winnerSurface,\n rank: 0,\n }\n if (w.durationMs !== undefined) score.durationMs = w.durationMs\n return score\n })\n // Sort by lift; tie-break by lower cost (cheaper wins a tie).\n scores.sort((a, b) => b.lift - a.lift || a.costUsd - b.costUsd)\n scores.forEach((s, i) => {\n s.rank = i + 1\n })\n const best = scores[0]!\n\n const byName = new Map(winners.map((w) => [w.name, w]))\n const bestArr = byName.get(best.name)!.arr\n const pairwise: DriverPairwise[] = scores.slice(1).map((other) => {\n const otherArr = byName.get(other.name)!.arr\n // before = other, after = best ⇒ delta = best − other on the held-out set.\n const boot = pairedBootstrap(otherArr, bestArr, {\n seed,\n resamples,\n confidence,\n statistic: 'mean',\n })\n const favored = boot.low > 0 ? best.name : boot.high < 0 ? other.name : 'tie'\n return {\n a: best.name,\n b: other.name,\n deltaMean: boot.mean,\n low: boot.low,\n high: boot.high,\n favored,\n }\n })\n\n return { scores, best, pairwise, holdoutScenarioIds: scenarioIds }\n}\n\nfunction mean(xs: number[]): number {\n return xs.length === 0 ? 0 : xs.reduce((a, b) => a + b, 0) / xs.length\n}\n\nfunction slug(name: string): string {\n return name.replace(/[^a-z0-9]+/gi, '-').toLowerCase()\n}\n\n// ── Built-in entries — wire the real optimizers for a live comparison ───────\n\n/** Shared corpus + transport for the three built-in optimizer entries. */\nexport interface OptimizerEntryConfig<TScenario extends Scenario, TArtifact> {\n baselineSurface: string\n /** Training scenarios the drivers reflect on. */\n trainScenarios: TScenario[]\n /** Held-out scenarios (the gate axis + the benchmark scoring axis). */\n holdoutScenarios: TScenario[]\n dispatchWithSurface: (\n surface: MutableSurface,\n scenario: TScenario,\n ctx: DispatchContext,\n ) => Promise<TArtifact>\n judges: JudgeConfig<TArtifact, TScenario>[]\n llm: LlmClientOptions\n model: string\n target: string\n runDir: string\n seed?: number\n /** GEPA population per generation. Default 2. */\n populationSize?: number\n /** GEPA generations. Default 3. */\n maxGenerations?: number\n /** SkillOpt epochs. Default 6. */\n maxEpochs?: number\n mutationPrimitives?: string[]\n}\n\n/** GEPA, reflection-only (single-parent, no Pareto combine). */\nexport function gepaReflectionEntry<TScenario extends Scenario, TArtifact>(\n config: OptimizerEntryConfig<TScenario, TArtifact>,\n name = 'gepa-reflection',\n): DriverEntry {\n return gepaEntry(config, false, name)\n}\n\n/** GEPA with the Pareto frontier + combine-complementary-lessons. */\nexport function gepaParetoEntry<TScenario extends Scenario, TArtifact>(\n config: OptimizerEntryConfig<TScenario, TArtifact>,\n name = 'gepa-pareto',\n): DriverEntry {\n return gepaEntry(config, true, name)\n}\n\nfunction gepaEntry<TScenario extends Scenario, TArtifact>(\n config: OptimizerEntryConfig<TScenario, TArtifact>,\n combineParents: boolean,\n name: string,\n): DriverEntry {\n return {\n name,\n async optimize() {\n const started = Date.now()\n const driver = gepaDriver({\n llm: config.llm,\n model: config.model,\n target: config.target,\n combineParents,\n ...(config.mutationPrimitives ? { mutationPrimitives: config.mutationPrimitives } : {}),\n })\n const result = await runImprovementLoop<TScenario, TArtifact>({\n scenarios: config.trainScenarios,\n holdoutScenarios: config.holdoutScenarios,\n baselineSurface: config.baselineSurface,\n dispatchWithSurface: config.dispatchWithSurface,\n judges: config.judges,\n driver,\n populationSize: config.populationSize ?? 2,\n maxGenerations: config.maxGenerations ?? 3,\n gate: defaultProductionGate<TArtifact, TScenario>({\n holdoutScenarios: config.holdoutScenarios,\n deltaThreshold: 0,\n }),\n autoOnPromote: 'none',\n runDir: `${config.runDir}/${slug(name)}-loop`,\n ...(config.seed !== undefined ? { seed: config.seed } : {}),\n })\n const costUsd =\n result.baselineCampaign.aggregates.totalCostUsd +\n result.generations.reduce(\n (sum, g) =>\n sum + g.surfaces.reduce((s, sf) => s + sf.campaign.aggregates.totalCostUsd, 0),\n 0,\n )\n return { winnerSurface: result.winnerSurface, costUsd, durationMs: Date.now() - started }\n },\n }\n}\n\n/** SkillOpt patch-mode hill-climb. */\nexport function skillOptEntry<TScenario extends Scenario, TArtifact>(\n config: OptimizerEntryConfig<TScenario, TArtifact>,\n name = 'skill-opt',\n): DriverEntry {\n return {\n name,\n async optimize() {\n const started = Date.now()\n const driver = skillOptDriver({ llm: config.llm, model: config.model, target: config.target })\n const result = await runSkillOpt<TScenario, TArtifact>({\n baselineSurface: config.baselineSurface,\n dispatchWithSurface: config.dispatchWithSurface,\n judges: config.judges,\n driver,\n trainScenarios: config.trainScenarios,\n holdoutScenarios: config.holdoutScenarios,\n maxEpochs: config.maxEpochs ?? 6,\n runDir: `${config.runDir}/${slug(name)}-loop`,\n ...(config.seed !== undefined ? { seed: config.seed } : {}),\n })\n return {\n winnerSurface: result.winnerSurface,\n costUsd: result.totalCostUsd,\n durationMs: Date.now() - started,\n }\n },\n }\n}\n","/**\n * @experimental\n *\n * `runProfileMatrix` — the missing keystone between `runAgentMatrix` and the\n * backend-integrity guard.\n *\n * The gap it closes: `runAgentMatrix` is a topology-opaque scheduler whose\n * cells return a bare `{ output, verdict, costUsd }` — no `tokenUsage`, not a\n * `RunRecord`. `assertRealBackend` / `summarizeBackendIntegrity` key on\n * `RunRecord.tokenUsage`, so they cannot run on a raw matrix result. Every\n * consumer therefore hand-writes the same bridge: fan a profile × scenario\n * cartesian, call dispatch, fabricate a `RunRecord` with token usage, thread it\n * back, run the integrity guard. That hand-rolled bridge is exactly the pile of\n * bespoke `eval:*` scripts the adoption skills keep trying (and failing) to\n * forbid.\n *\n * `runProfileMatrix` IS that bridge, once:\n *\n * - axis 3 (PROFILE) = `profiles: AgentProfile[]`\n * - axis 1 (PERSONA/SCENARIO) = `scenarios: Scenario[]` (each scenario carries\n * its persona; `personaOf` groups them for the `byPersona` pivot)\n * - the scoring axis = `judges`\n *\n * It runs `runCampaign` once per profile (reusing its seeds, reps, bootstrap\n * CIs, resumability, and the `LabeledScenarioStore` capture flywheel), maps\n * every cell to a validated `RunRecord` carrying the real `tokenUsage` the\n * dispatch reported via `ctx.cost.observeTokens`, and runs `assertRealBackend`\n * BY CONSTRUCTION before returning — so a stub-backend run fails loudly instead\n * of reporting a clean 0/N leaderboard.\n *\n * Dispatch contract: a dispatch that calls an LLM MUST report usage via\n * `ctx.cost.observeTokens({ input, output })` (and cost via `ctx.cost.observe`).\n * A dispatch that reports zero tokens is indistinguishable from a stub and the\n * integrity guard treats it as one.\n */\n\nimport { createHash } from 'node:crypto'\nimport { join } from 'node:path'\nimport { type AgentProfile, agentProfileHash } from '../../agent-profile'\nimport { AgentEvalError } from '../../errors'\nimport {\n assertRealBackend,\n type BackendIntegrityReport,\n summarizeBackendIntegrity,\n} from '../../integrity/backend-integrity'\nimport {\n type RunOutcome,\n type RunRecord,\n type RunSplitTag,\n validateRunRecord,\n} from '../../run-record'\nimport { runCampaign } from '../run-campaign'\nimport type { CampaignStorage } from '../storage'\nimport type {\n CampaignCellResult,\n CampaignResult,\n DispatchContext,\n JudgeConfig,\n LabeledScenarioSource,\n LabeledScenarioStore,\n Scenario,\n} from '../types'\n\n/** Thrown when the matrix is misconfigured (no profiles, a profile whose model\n * lacks a snapshot version, etc.). Distinct from `BackendIntegrityError`,\n * which signals a stub backend at run time. */\nexport class ProfileMatrixError extends AgentEvalError {\n constructor(message: string) {\n super('profile_matrix', message)\n }\n}\n\n/** Dispatch for one cell: render `profile` against `scenario`, returning the\n * artifact the judges score. Report LLM usage via `ctx.cost.observeTokens`\n * and `ctx.cost.observe` — the integrity guard depends on it. */\nexport type ProfileDispatchFn<TScenario extends Scenario, TArtifact> = (\n profile: AgentProfile,\n scenario: TScenario,\n ctx: DispatchContext,\n) => Promise<TArtifact>\n\nexport interface RunProfileMatrixOptions<TScenario extends Scenario, TArtifact> {\n /** Axis 3 — the agent-under-test configurations. Each is one column. */\n profiles: AgentProfile[]\n /** Axis 1 — the persona/scenario corpus, run against every profile. */\n scenarios: TScenario[]\n /** Renders one (profile, scenario) cell. */\n dispatch: ProfileDispatchFn<TScenario, TArtifact>\n /** The scoring axis. */\n judges?: JudgeConfig<TArtifact, TScenario>[]\n /** Where each profile's campaign writes artifacts/traces. One subdir per\n * profile. */\n runDir: string\n /** Git SHA the harness ran from — stamped onto every RunRecord (mandatory\n * for paper-grade records). */\n commitSha: string\n /** Logical experiment id shared across the whole matrix so the promotion\n * gate can pair profiles on matched scenarios. Default: a hash of the\n * profile + scenario ids. */\n experimentId?: string\n /** Which split these runs belong to. Default `'search'`. */\n splitTag?: RunSplitTag\n /** Replicates per (profile, scenario) cell for CI bands. Default 1. */\n reps?: number\n /** Campaign seed (per profile). Default 42. */\n seed?: number\n /**\n * Backend-integrity posture, enforced AFTER the matrix completes:\n * - `'assert'` (default) — throw `BackendIntegrityError` if the run was a\n * stub (and, with `allowMixed:false`, if it was mixed).\n * - `'warn'` — log the verdict but never throw.\n * - `'off'` — skip the guard entirely (only for offline/replay analysis).\n */\n integrity?: 'assert' | 'warn' | 'off'\n /** Forwarded to `assertRealBackend`. Default true (tolerate partial 429\n * cascades); set false for strict CI gates. */\n allowMixed?: boolean\n /** Max concurrent cells WITHIN each profile's campaign. Default 2.\n * Profiles run sequentially so the cost ceiling is honored deterministically. */\n maxConcurrency?: number\n /** Cumulative USD cap per profile campaign. */\n costCeiling?: number\n /** Capture flywheel — forwarded to each campaign. */\n labeledStore?: LabeledScenarioStore | 'off'\n captureSource?: LabeledScenarioSource\n /** Storage backend. Default `fsCampaignStorage`. Pass\n * `inMemoryCampaignStorage()` for edge/CF-Worker/test runs. */\n storage?: CampaignStorage\n /** Test seam — override the wall clock. */\n now?: () => Date\n /** Optional persona key per scenario — drives the `byPersona` pivot. When\n * unset, `byPersona` is omitted. */\n personaOf?: (scenario: TScenario) => string\n /** Validate every produced RunRecord with `validateRunRecord` (fail-loud).\n * Default true — catches bad model snapshots and non-finite judge dims at\n * the boundary instead of letting them poison downstream analysis. */\n validate?: boolean\n}\n\nexport interface ProfileSummary {\n profileId: string\n profileHash: string\n model: string\n /** RunRecords produced for this profile (= scenarios × reps). */\n records: number\n /** Mean composite across this profile's records. */\n meanComposite: number\n totalCostUsd: number\n /** Per-profile integrity verdict — surfaces a single profile that ran stub\n * even when the matrix as a whole looks real. */\n integrity: BackendIntegrityReport\n}\n\nexport interface ScenarioRollup {\n meanComposite: number\n n: number\n}\n\nexport interface RunProfileMatrixResult<TArtifact, TScenario extends Scenario> {\n matrixId: string\n experimentId: string\n /** One RunRecord per (profile, scenario, rep) cell — the integrity-checked,\n * paper-grade output. Feed straight into `analyzeRuns`, `HeldOutGate`,\n * scorecards, the hosted wire format. */\n records: RunRecord[]\n byProfile: Record<string, ProfileSummary>\n byScenario: Record<string, ScenarioRollup>\n /** Present only when `personaOf` was supplied. */\n byPersona?: Record<string, ScenarioRollup>\n /** Whole-matrix integrity report (the one `integrity:'assert'` enforces). */\n integrity: BackendIntegrityReport\n /** The raw per-profile campaign results, keyed by profile id. */\n campaigns: Record<string, CampaignResult<TArtifact, TScenario>>\n}\n\nfunction sanitize(id: string): string {\n return id.replace(/[^a-zA-Z0-9_-]/g, '_')\n}\n\nfunction sha(input: unknown): string {\n return createHash('sha256').update(JSON.stringify(input)).digest('hex')\n}\n\nfunction mean(xs: number[]): number {\n return xs.length === 0 ? 0 : xs.reduce((a, b) => a + b, 0) / xs.length\n}\n\nfunction cellComposite(cell: CampaignCellResult<unknown>): number {\n const composites = Object.values(cell.judgeScores).map((s) => s.composite)\n return composites.length === 0 ? 0 : mean(composites)\n}\n\ninterface BuildRecordArgs<TArtifact> {\n cell: CampaignCellResult<TArtifact>\n profile: AgentProfile\n profileHash: string\n configHash: string\n experimentId: string\n splitTag: RunSplitTag\n commitSha: string\n matrixId: string\n}\n\nfunction buildRunRecord<TArtifact>(args: BuildRecordArgs<TArtifact>): RunRecord {\n const { cell, profile, profileHash, configHash, experimentId, splitTag, commitSha, matrixId } =\n args\n const composite = cellComposite(cell)\n\n // Flatten judge dimensions (judge-prefixed to avoid collisions) into raw.\n const raw: Record<string, number> = { composite }\n const perJudge: Record<string, Record<string, number>> = {}\n const dimAccum: Record<string, number[]> = {}\n const notes: string[] = []\n for (const [judgeName, js] of Object.entries(cell.judgeScores)) {\n perJudge[judgeName] = { ...js.dimensions }\n for (const [dim, value] of Object.entries(js.dimensions)) {\n raw[`${judgeName}.${dim}`] = value\n ;(dimAccum[dim] ??= []).push(value)\n }\n if (js.notes) notes.push(`${judgeName}: ${js.notes}`)\n }\n const perDimMean: Record<string, number> = {}\n for (const [dim, values] of Object.entries(dimAccum)) perDimMean[dim] = mean(values)\n\n const outcome: RunOutcome =\n splitTag === 'holdout' ? { holdoutScore: composite, raw } : { searchScore: composite, raw }\n if (Object.keys(perJudge).length > 0) {\n outcome.judgeScores = {\n perJudge,\n perDimMean,\n composite,\n ...(notes.length > 0 ? { notes: notes.join(' | ') } : {}),\n }\n }\n\n return {\n runId: `${matrixId}:${profile.id}:${cell.cellId}`,\n experimentId,\n candidateId: profile.id,\n seed: cell.seed,\n model: profile.model,\n promptHash: profileHash,\n configHash,\n commitSha,\n wallMs: cell.durationMs,\n costUsd: cell.costUsd,\n tokenUsage: cell.tokenUsage,\n outcome,\n splitTag,\n scenarioId: cell.scenarioId,\n ...(cell.error ? { failureMode: cell.error } : {}),\n }\n}\n\nexport async function runProfileMatrix<TScenario extends Scenario, TArtifact>(\n opts: RunProfileMatrixOptions<TScenario, TArtifact>,\n): Promise<RunProfileMatrixResult<TArtifact, TScenario>> {\n if (opts.profiles.length === 0) throw new ProfileMatrixError('profiles must not be empty')\n if (opts.scenarios.length === 0) throw new ProfileMatrixError('scenarios must not be empty')\n\n const splitTag = opts.splitTag ?? 'search'\n const seed = opts.seed ?? 42\n const validate = opts.validate ?? true\n const integrityMode = opts.integrity ?? 'assert'\n const profileIds = opts.profiles.map((p) => p.id)\n const experimentId =\n opts.experimentId ??\n `pm_${sha({ profileIds, scenarios: opts.scenarios.map((s) => s.id) }).slice(0, 16)}`\n const matrixId = `mtx_${sha({ experimentId, profileIds, seed, splitTag }).slice(0, 16)}`\n\n // Preflight: every profile must hash (non-empty model) AND its model must\n // carry a snapshot version, BEFORE any LLM spend. A probe record run through\n // validateRunRecord catches both in the exact place they'd otherwise surface\n // far downstream.\n for (const profile of opts.profiles) {\n const profileHash = agentProfileHash(profile)\n try {\n validateRunRecord({\n runId: `${matrixId}:${profile.id}:probe`,\n experimentId,\n candidateId: profile.id,\n seed,\n model: profile.model,\n promptHash: profileHash,\n configHash: profileHash,\n commitSha: opts.commitSha,\n wallMs: 0,\n costUsd: 0,\n tokenUsage: { input: 0, output: 0 },\n outcome:\n splitTag === 'holdout' ? { holdoutScore: 0, raw: {} } : { searchScore: 0, raw: {} },\n splitTag,\n })\n } catch (err) {\n throw new ProfileMatrixError(\n `profile '${profile.id}' is not recordable: ${err instanceof Error ? err.message : String(err)}`,\n )\n }\n }\n\n const records: RunRecord[] = []\n const campaigns: Record<string, CampaignResult<TArtifact, TScenario>> = {}\n const byProfile: Record<string, ProfileSummary> = {}\n\n for (const profile of opts.profiles) {\n const profileHash = agentProfileHash(profile)\n const configHash = sha({\n profile: profileHash,\n judges: (opts.judges ?? []).map((j) => j.name),\n seed,\n splitTag,\n })\n\n // Bind the profile into a campaign dispatch. Name it so the campaign's\n // manifest hash is stable + distinct per profile.\n const dispatch = (scenario: TScenario, ctx: DispatchContext): Promise<TArtifact> =>\n opts.dispatch(profile, scenario, ctx)\n Object.defineProperty(dispatch, 'name', { value: `profile_${sanitize(profile.id)}` })\n\n const campaign = await runCampaign<TScenario, TArtifact>({\n scenarios: opts.scenarios,\n dispatch,\n judges: opts.judges,\n seed,\n reps: opts.reps,\n maxConcurrency: opts.maxConcurrency,\n costCeiling: opts.costCeiling,\n labeledStore: opts.labeledStore,\n captureSource: opts.captureSource,\n storage: opts.storage,\n now: opts.now,\n runDir: join(opts.runDir, sanitize(profile.id)),\n })\n campaigns[profile.id] = campaign\n\n const profileRecords: RunRecord[] = []\n for (const cell of campaign.cells) {\n const record = buildRunRecord({\n cell,\n profile,\n profileHash,\n configHash,\n experimentId,\n splitTag,\n commitSha: opts.commitSha,\n matrixId,\n })\n if (validate) validateRunRecord(record)\n profileRecords.push(record)\n records.push(record)\n }\n\n byProfile[profile.id] = {\n profileId: profile.id,\n profileHash,\n model: profile.model,\n records: profileRecords.length,\n meanComposite: mean(profileRecords.map(compositeOf)),\n totalCostUsd: profileRecords.reduce((a, r) => a + r.costUsd, 0),\n integrity: summarizeBackendIntegrity(profileRecords),\n }\n }\n\n // Integrity by construction — the whole point of the primitive.\n const integrity = summarizeBackendIntegrity(records)\n if (integrityMode === 'assert') {\n assertRealBackend(records, { allowMixed: opts.allowMixed ?? true })\n } else if (integrityMode === 'warn' && integrity.verdict !== 'real') {\n // eslint-disable-next-line no-console\n console.warn(\n `[runProfileMatrix] backend integrity: ${integrity.verdict} — ${integrity.diagnosis}`,\n )\n }\n\n // Pivots.\n const byScenario = rollup(records, (r) => r.scenarioId)\n const byPersona = opts.personaOf\n ? rollupByPersona(records, opts.scenarios, opts.personaOf)\n : undefined\n\n return { matrixId, experimentId, records, byProfile, byScenario, byPersona, integrity, campaigns }\n}\n\n/** Composite for a produced RunRecord (the split score it carries). */\nfunction compositeOf(r: RunRecord): number {\n return r.outcome.holdoutScore ?? r.outcome.searchScore ?? 0\n}\n\nfunction rollup(\n records: RunRecord[],\n keyOf: (r: RunRecord) => string | undefined,\n): Record<string, ScenarioRollup> {\n const groups = new Map<string, number[]>()\n for (const r of records) {\n const key = keyOf(r)\n if (key === undefined) continue\n const arr = groups.get(key) ?? []\n arr.push(compositeOf(r))\n groups.set(key, arr)\n }\n const out: Record<string, ScenarioRollup> = {}\n for (const [key, xs] of groups) out[key] = { meanComposite: mean(xs), n: xs.length }\n return out\n}\n\nfunction rollupByPersona<TScenario extends Scenario>(\n records: RunRecord[],\n scenarios: TScenario[],\n personaOf: (s: TScenario) => string,\n): Record<string, ScenarioRollup> {\n const personaByScenarioId = new Map<string, string>()\n for (const s of scenarios) personaByScenarioId.set(s.id, personaOf(s))\n return rollup(records, (r) => (r.scenarioId ? personaByScenarioId.get(r.scenarioId) : undefined))\n}\n","/**\n * @experimental\n *\n * VCS-pluggable worktree adapter. One improvement = one worktree, PR-like\n * (multiple commits allowed). A code-tier driver's `propose()` creates a\n * worktree, an agent commits the change into it, and `finalize()` returns a\n * `CodeSurface{ worktreeRef }` the measurement checks out to run the worker\n * against the changed code. On promotion the worktree becomes the PR branch.\n *\n * The interface is VCS-agnostic so a future `jj` ([jj-vcs](https://github.com/jj-vcs/jj))\n * adapter can slot in without touching driver code. Only the git adapter\n * ships today. See `docs/design/self-improvement-engine.md`.\n */\n\nimport { execFileSync } from 'node:child_process'\nimport { existsSync } from 'node:fs'\nimport { basename, isAbsolute, join } from 'node:path'\nimport type { CodeSurface } from '../types'\n\nexport interface Worktree {\n /** Absolute path to the checked-out worktree directory. */\n path: string\n /** The branch the worktree is on (becomes the PR branch on promotion). */\n branch: string\n /** The ref the worktree was forked from. */\n baseRef: string\n}\n\nexport interface WorktreeAdapter {\n /** Create an isolated worktree on a fresh branch off `baseRef`. */\n create(opts: { baseRef: string; label: string }): Promise<Worktree>\n /** Commit any pending changes in the worktree, then return a CodeSurface\n * pointing at it. The agent has already written its change into\n * `worktree.path` by the time this is called. */\n finalize(worktree: Worktree, summary: string): Promise<CodeSurface>\n /** Remove the worktree (and its branch) — called for losing candidates. */\n discard(worktree: Worktree): Promise<void>\n}\n\nexport class WorktreeAdapterError extends Error {\n constructor(\n message: string,\n readonly cause?: unknown,\n ) {\n super(message)\n this.name = 'WorktreeAdapterError'\n }\n}\n\nexport interface GitWorktreeAdapterOptions {\n /** Repo root the worktrees fork from. */\n repoRoot: string\n /** Directory worktrees are created under. Default: `<repoRoot>/.worktrees`. */\n worktreeDir?: string\n /** Branch-name prefix. Default: `improve`. */\n branchPrefix?: string\n /** Test seam — defaults to a real `git` runner. */\n git?: (args: string[], cwd: string) => string\n}\n\nfunction defaultGit(args: string[], cwd: string): string {\n try {\n return execFileSync('git', args, { cwd, encoding: 'utf8' }).trim()\n } catch (err) {\n const stderr =\n err && typeof err === 'object' && 'stderr' in err\n ? String((err as { stderr: unknown }).stderr)\n : ''\n throw new WorktreeAdapterError(`git ${args.join(' ')} failed: ${stderr || String(err)}`, err)\n }\n}\n\n/** Slugify a label into a branch-safe segment. */\nfunction slug(label: string): string {\n return (\n label\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '-')\n .replace(/^-+|-+$/g, '')\n .slice(0, 48) || 'candidate'\n )\n}\n\nexport function gitWorktreeAdapter(opts: GitWorktreeAdapterOptions): WorktreeAdapter {\n const git = opts.git ?? defaultGit\n const worktreeDir = opts.worktreeDir ?? join(opts.repoRoot, '.worktrees')\n const branchPrefix = opts.branchPrefix ?? 'improve'\n\n return {\n async create({ baseRef, label }) {\n const id = `${slug(label)}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`\n const branch = `${branchPrefix}/${id}`\n const path = join(worktreeDir, id)\n git(['worktree', 'add', '-b', branch, path, baseRef], opts.repoRoot)\n return { path, branch, baseRef }\n },\n\n async finalize(worktree, summary) {\n // Stage + commit any pending changes the agent left in the worktree.\n // A no-op commit is refused by git, so only commit when the tree is dirty.\n const status = git(['status', '--porcelain'], worktree.path)\n if (status.length > 0) {\n git(['add', '-A'], worktree.path)\n git(['commit', '-m', summary], worktree.path)\n }\n return {\n kind: 'code',\n worktreeRef: worktree.path,\n baseRef: worktree.baseRef,\n summary,\n }\n },\n\n async discard(worktree) {\n // Remove the worktree, then delete its branch. Force-remove because the\n // worktree may hold uncommitted experiment state we're discarding.\n git(['worktree', 'remove', '--force', worktree.path], opts.repoRoot)\n git(['branch', '-D', worktree.branch], opts.repoRoot)\n },\n }\n}\n\n/** Resolve a `CodeSurface`'s worktreeRef to a directory the measurement can\n * run the worker in. A path ref is returned as-is; anything else is treated\n * as a ref under the adapter's worktree dir. */\nexport function resolveWorktreePath(surface: CodeSurface, worktreeDir?: string): string {\n if (isAbsolute(surface.worktreeRef) && existsSync(surface.worktreeRef)) return surface.worktreeRef\n if (worktreeDir) return join(worktreeDir, basename(surface.worktreeRef))\n return surface.worktreeRef\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDO,SAAS,gBAAgB,SAAiB,OAA0C;AACzF,MAAI,QAAQ,QAAQ,MAAM,IAAI;AAC9B,MAAI,UAAU;AACd,QAAM,WAAkC,CAAC;AACzC,QAAM,WAAW,CAAC,WAA2B,MAAM,UAAU,CAAC,MAAM,EAAE,SAAS,MAAM,CAAC;AAEtF,aAAW,MAAM,MAAM,KAAK;AAC1B,QAAI,GAAG,OAAO,OAAO;AACnB,UAAI,OAAO,GAAG,SAAS,YAAY,GAAG,KAAK,KAAK,MAAM,IAAI;AACxD,iBAAS,KAAK,EAAE,IAAI,QAAQ,iBAAiB,CAAC;AAC9C;AAAA,MACF;AACA,YAAM,SAAS,GAAG,KAAK,MAAM,IAAI;AACjC,UAAI,GAAG,UAAU,UAAa,GAAG,UAAU,IAAI;AAC7C,gBAAQ,CAAC,GAAG,OAAO,GAAG,MAAM;AAC5B;AACA;AAAA,MACF;AACA,YAAM,MAAM,SAAS,GAAG,KAAK;AAC7B,UAAI,QAAQ,IAAI;AACd,iBAAS,KAAK,EAAE,IAAI,QAAQ,yBAAyB,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC;AAC3E;AAAA,MACF;AACA,cAA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