@tangle-network/agent-eval 0.119.0 → 0.120.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 -3
- package/README.md +0 -26
- package/dist/analyst/index.d.ts +30 -39
- package/dist/analyst/index.js +9 -5
- package/dist/analyst/index.js.map +1 -1
- package/dist/benchmarks/index.d.ts +11 -5
- package/dist/benchmarks/index.js +7 -7
- package/dist/campaign/index.d.ts +121 -66
- package/dist/campaign/index.js +26 -10
- package/dist/{chunk-RLCJQ6VZ.js → chunk-32BZXMSO.js} +385 -120
- package/dist/chunk-32BZXMSO.js.map +1 -0
- package/dist/{chunk-XJ7JVCHB.js → chunk-3A246TSA.js} +2 -2
- package/dist/{chunk-LMJ2TGWJ.js → chunk-JM2SKQMS.js} +2 -2
- package/dist/{chunk-F6YUH3L4.js → chunk-JN2FCO5W.js} +14 -168
- package/dist/chunk-JN2FCO5W.js.map +1 -0
- package/dist/{chunk-D7AEXSM5.js → chunk-PICTDURQ.js} +2 -2
- package/dist/{chunk-6QIM2EAP.js → chunk-PXD6ZFNY.js} +99 -1
- package/dist/chunk-PXD6ZFNY.js.map +1 -0
- package/dist/{chunk-GERDAIAL.js → chunk-QWMPPZ3X.js} +3 -3
- package/dist/{chunk-7A4LIMMY.js → chunk-S5TT5R3L.js} +229 -97
- package/dist/chunk-S5TT5R3L.js.map +1 -0
- package/dist/{chunk-ZYHJNKI3.js → chunk-ULOKLHIQ.js} +42 -5
- package/dist/chunk-ULOKLHIQ.js.map +1 -0
- package/dist/{chunk-FIUFRXP3.js → chunk-WW2A73HW.js} +49 -91
- package/dist/chunk-WW2A73HW.js.map +1 -0
- package/dist/{chunk-PAHNGS65.js → chunk-XDIRG3TO.js} +175 -11
- package/dist/chunk-XDIRG3TO.js.map +1 -0
- package/dist/contract/index.d.ts +76 -43
- package/dist/contract/index.js +596 -32
- package/dist/contract/index.js.map +1 -1
- package/dist/index.d.ts +92 -75
- package/dist/index.js +38 -35
- package/dist/index.js.map +1 -1
- package/dist/openapi.json +1 -1
- package/dist/rl.d.ts +11 -5
- package/dist/{run-campaign-OBXSN5WK.js → run-campaign-HNFPJET4.js} +2 -2
- package/dist/traces.d.ts +39 -25
- package/dist/traces.js +3 -5
- package/docs/trace-analysis.md +6 -2
- package/package.json +2 -7
- package/dist/chunk-6QIM2EAP.js.map +0 -1
- package/dist/chunk-7A4LIMMY.js.map +0 -1
- package/dist/chunk-F6YUH3L4.js.map +0 -1
- package/dist/chunk-FIUFRXP3.js.map +0 -1
- package/dist/chunk-PAHNGS65.js.map +0 -1
- package/dist/chunk-RLCJQ6VZ.js.map +0 -1
- package/dist/chunk-ZYHJNKI3.js.map +0 -1
- package/dist/primeintellect/index.d.ts +0 -311
- package/dist/primeintellect/index.js +0 -348
- package/dist/primeintellect/index.js.map +0 -1
- /package/dist/{chunk-XJ7JVCHB.js.map → chunk-3A246TSA.js.map} +0 -0
- /package/dist/{chunk-LMJ2TGWJ.js.map → chunk-JM2SKQMS.js.map} +0 -0
- /package/dist/{chunk-D7AEXSM5.js.map → chunk-PICTDURQ.js.map} +0 -0
- /package/dist/{chunk-GERDAIAL.js.map → chunk-QWMPPZ3X.js.map} +0 -0
- /package/dist/{run-campaign-OBXSN5WK.js.map → run-campaign-HNFPJET4.js.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/campaign/run-campaign.ts","../src/integrity/backend-integrity.ts","../src/verdict-cache.ts","../src/campaign/coverage.ts","../src/campaign/run-dir.ts","../src/campaign/storage.ts","../src/campaign/search-ledger-file.ts","../src/campaign/search-ledger-errors.ts"],"sourcesContent":["/**\n * `runCampaign` — Pass A substrate primitive. ONE function that orchestrates\n * scenarios → dispatch → artifacts → judges → aggregates, with full\n * reproducibility (seed + manifest hash), cell-level resumability, bootstrap\n * CIs, and the `LabeledScenarioStore` capture flywheel.\n *\n * Improvement loops (optimizer / gate / autoOnPromote) ride on top of this\n * primitive but live in `presets/run-improvement-loop.ts`. This file keeps\n * the core orchestrator minimal — Phase 1 of the Pass A track.\n */\n\nimport { join } from 'node:path'\nimport {\n CostAccountingIncompleteError,\n type CostLedgerHandle,\n type CostLedgerSummary,\n} from '../cost-ledger'\nimport { BackendIntegrityError, type BackendIntegrityReport } from '../integrity/backend-integrity'\nimport { confidenceInterval } from '../statistics'\nimport { contentHash } from '../verdict-cache'\nimport { assertCampaignDesign, campaignScenarioIdentity, campaignSplitDigest } from './coverage'\nimport { resolveRunDir } from './run-dir'\nimport { type CampaignStorage, createRunCostLedger, fsCampaignStorage } from './storage'\nimport type {\n CampaignAggregates,\n CampaignArtifactWriter,\n CampaignCellResult,\n CampaignCostMeter,\n CampaignResult,\n CampaignTokenUsage,\n CampaignTraceWriter,\n DispatchContext,\n DispatchFn,\n JudgeAggregate,\n JudgeConfig,\n JudgeScore,\n LabeledScenarioStore,\n Scenario,\n ScenarioAggregate,\n TraceSpan,\n} from './types'\n\nexport interface RunCampaignOptions<TScenario extends Scenario, TArtifact> {\n scenarios: TScenario[]\n dispatch: DispatchFn<TScenario, TArtifact>\n /**\n * Stable identity for the dispatch behavior, included in the manifest/cache\n * key. Set this when the same function name can run different models,\n * prompts, tools, or external config.\n */\n dispatchRef?: string\n judges?: JudgeConfig<TArtifact, TScenario>[]\n /** Required for reproducibility. Default 42. */\n seed?: number\n /** Per-scenario replicates for CI bands. Default 1; raise to 5+ for\n * bootstrap-tight intervals on critical eval. */\n reps?: number\n /** When true (default), completed cells are cached by\n * (manifestHash, scenarioId, rep, generation). Re-runs skip cached cells. */\n resumable?: boolean\n /** Optional store — when present, every artifact + judge score is captured\n * with the configured `captureSource`. Capture is default ON; pass `'off'`\n * to disable. */\n labeledStore?: LabeledScenarioStore | 'off'\n captureSource?: 'production-trace' | 'eval-run' | 'manual' | 'red-team' | 'synthetic'\n captureSourceVersionHash?: string\n /** Hard spend cap. Each paid call reserves its enforced maximum before dispatch. */\n costCeiling?: number\n /** Shared spend account. Improvement loops pass one ledger through every\n * campaign so the ceiling and returned total are run-wide. */\n costLedger?: CostLedgerHandle\n /** Attribution label for receipts recorded by this campaign. */\n costPhase?: string\n /** Max concurrent cells. Default 2. */\n maxConcurrency?: number\n /**\n * Per-cell dispatch deadline in ms. A `dispatch` that neither resolves nor\n * rejects within this window is a hang (a stalled model request, an\n * exhausted runtime resource, a backend that never closes its stream). When\n * set, the cell's `ctx.signal` is aborted and the cell is recorded as a LOUD\n * error (`dispatch exceeded <N>ms`) so the campaign proceeds and the failure\n * is visible — instead of one wedged cell silently hanging the whole run (and\n * every loop/CI job above it) forever. `undefined`/`0` = unbounded (legacy).\n */\n dispatchTimeoutMs?: number\n /** Required: where artifacts + traces land. A bare name (not an absolute path)\n * resolves to the shared `~/.tangle/traces/<repo>/runs/<name>` root so run\n * bundles never pollute a repo working tree. Pass an absolute path to override. */\n runDir: string\n /** Subject repo for the shared run-dir root (defaults to the CWD basename).\n * Only consulted when `runDir` is a bare name. */\n repo?: string\n /** Tracing posture. Default is the substrate's `FileSystemTraceStore` rooted\n * at `<runDir>/traces/`. `'off'` disables capture entirely — substrate\n * refuses this when the caller wires `autoOnPromote !== 'none'`. */\n tracing?: 'on' | 'off'\n /**\n * Per-cell usage expectation — the early, fine-grained sibling of the\n * batch `assertRealBackend` guard. A cell that produced an artifact (no\n * error) but reported `costUsd === 0` AND zero tokens is a stub: the\n * dispatch never reported LLM activity via `ctx.cost`. Modes:\n * - `'warn'` (default) — log the offending cell loudly, keep going.\n * - `'assert'` — throw `BackendIntegrityError` on the first such cell\n * (fail-fast; recommended for CI campaigns expecting real LLM calls).\n * - `'off'` — no check (replay / deterministic-only / offline analysis).\n */\n expectUsage?: 'assert' | 'warn' | 'off'\n /** Test seam — override the wall clock for deterministic tests. */\n now?: () => Date\n /** Test seam — override per-cell trace writer factory. */\n buildTraceWriter?: (cellId: string, dir: string) => CampaignTraceWriter\n /** Storage backend for run/cell dirs, the resumability cache, artifacts,\n * and trace spans. Default: the Node filesystem (`fsCampaignStorage`).\n * Pass `inMemoryCampaignStorage()` to run in a filesystem-less runtime\n * (Cloudflare Workers, Deno, edge) — the `CampaignResult` is still\n * produced; artifacts/traces just aren't persisted to disk. */\n storage?: CampaignStorage\n /**\n * Optional per-cell placement strategy. Returns an opaque string the\n * substrate forwards as `ctx.placement` to the Dispatch — placement-aware\n * Dispatches (e.g. `httpDispatch` from `/adapters/http`) use it to route\n * each cell to the right worker, region, or sandbox. When unset, every\n * cell receives `ctx.placement = undefined` and behaves identically to\n * the in-process case.\n *\n * @example\n * cellPlacement: ({ scenario }) => scenario.tags?.includes('eu') ? 'eu-west' : 'us-east'\n */\n cellPlacement?: (input: {\n scenario: TScenario\n rep: number\n generation?: number\n }) => string | undefined\n}\n\n/**\n * Core campaign orchestrator: fan scenarios through dispatch, score with judges, aggregate bootstrap CIs, and persist reproducible `CampaignResult` records.\n */\nexport async function runCampaign<TScenario extends Scenario, TArtifact>(\n opts: RunCampaignOptions<TScenario, TArtifact>,\n): Promise<CampaignResult<TArtifact, TScenario>> {\n const seed = opts.seed ?? 42\n const reps = opts.reps ?? 1\n const resumable = opts.resumable ?? true\n const now = opts.now ?? (() => new Date())\n const judges = opts.judges ?? []\n const storage = opts.storage ?? fsCampaignStorage()\n const costPhase = opts.costPhase ?? 'campaign'\n\n assertCampaignDesign(opts.scenarios, reps)\n\n if (typeof opts.runDir !== 'string' || opts.runDir.trim().length === 0) {\n throw new Error('runCampaign: runDir is required and must be a non-empty string')\n }\n opts.runDir = resolveRunDir(opts.runDir, opts.repo)\n storage.ensureDir(opts.runDir)\n const costLedger =\n opts.costLedger ??\n createRunCostLedger({\n storage,\n runDir: opts.runDir,\n costCeilingUsd: opts.costCeiling,\n })\n if (opts.costCeiling !== undefined && costLedger.costCeilingUsd !== opts.costCeiling) {\n throw new Error('runCampaign: costCeiling must match the shared CostLedger ceiling')\n }\n const maxConcurrency = opts.maxConcurrency ?? 2\n\n const manifestHash = computeManifestHash({\n scenarios: opts.scenarios,\n judges: judges as unknown as JudgeConfig<unknown>[],\n dispatchRef: dispatchRefFor(opts.dispatch, opts.dispatchRef),\n seed,\n reps,\n })\n const splitDigest = campaignSplitDigest(opts.scenarios, reps)\n\n const startedAt = now()\n const runAttemptId = globalThis.crypto.randomUUID()\n const cells: CampaignCellResult<TArtifact>[] = []\n const artifactsByPath: Record<string, string> = {}\n\n // Build the cell schedule (scenario × rep).\n const schedule = buildCellSchedule(opts.scenarios, seed, reps)\n\n // Concurrency-limited execution.\n const abortController = new AbortController()\n // Concurrency lanes that drain the cell schedule. Named \"lanes\" — not\n // \"workers\" — to avoid clashing with the taxonomy's worker (= the agent\n // harness in a sandbox, invoked behind `dispatch`). See loop-taxonomy.md.\n const lanes: Promise<void>[] = []\n let nextIdx = 0\n const cellsRef = cells\n\n for (let i = 0; i < maxConcurrency; i++) {\n lanes.push(\n (async () => {\n while (true) {\n const myIdx = nextIdx++\n if (myIdx >= schedule.length) return\n const slot = schedule[myIdx]!\n const result = await executeCell({\n slot,\n opts,\n manifestHash,\n resumable,\n now,\n storage,\n buildTraceWriter: opts.buildTraceWriter ?? defaultBuildTraceWriter(storage),\n signal: abortController.signal,\n dispatchTimeoutMs: opts.dispatchTimeoutMs,\n costLedger,\n costPhase,\n runAttemptId,\n })\n cellsRef.push(result.cell)\n Object.assign(artifactsByPath, result.artifactsByPath)\n // Capture into LabeledScenarioStore unless explicitly disabled.\n if (opts.labeledStore && opts.labeledStore !== 'off' && !result.cell.error) {\n await captureToStore({\n store: opts.labeledStore,\n cell: result.cell,\n scenario: slot.scenario,\n opts,\n now,\n }).catch((err) => {\n // Capture failures are non-fatal — log but don't crash the campaign.\n // (Trace would normally land here.)\n console.warn(\n `[runCampaign] capture failed for ${result.cell.cellId}: ${err instanceof Error ? err.message : String(err)}`,\n )\n })\n }\n }\n })(),\n )\n }\n await Promise.all(lanes)\n\n const endedAt = now()\n cellsRef.sort((a, b) => a.cellId.localeCompare(b.cellId))\n\n const campaignCost = costLedger.summary({ tags: { runDir: opts.runDir } })\n const aggregates = computeAggregates(\n cellsRef,\n judges as unknown as JudgeConfig<TArtifact>[],\n seed,\n campaignCost,\n )\n\n return {\n manifestHash,\n splitDigest,\n seed,\n reps,\n startedAt: startedAt.toISOString(),\n endedAt: endedAt.toISOString(),\n durationMs: endedAt.getTime() - startedAt.getTime(),\n cells: cellsRef,\n aggregates,\n runDir: opts.runDir,\n artifactsByPath,\n scenarios: opts.scenarios.map(campaignScenarioIdentity),\n }\n}\n\n// ── Internals ─────────────────────────────────────────────────────────\n\ninterface ExecuteCellArgs<TScenario extends Scenario, TArtifact> {\n slot: { scenario: TScenario; rep: number; cellId: string; cellSeed: number }\n opts: RunCampaignOptions<TScenario, TArtifact>\n manifestHash: string\n resumable: boolean\n now: () => Date\n storage: CampaignStorage\n buildTraceWriter: (cellId: string, dir: string) => CampaignTraceWriter\n signal: AbortSignal\n dispatchTimeoutMs?: number\n costLedger: CostLedgerHandle\n costPhase: string\n runAttemptId: string\n}\n\nasync function executeCell<TScenario extends Scenario, TArtifact>(\n args: ExecuteCellArgs<TScenario, TArtifact>,\n): Promise<{ cell: CampaignCellResult<TArtifact>; artifactsByPath: Record<string, string> }> {\n const storage = args.storage\n const cellDir = join(args.opts.runDir, args.slot.cellId.replace(/[^a-zA-Z0-9_-]/g, '_'))\n storage.ensureDir(cellDir)\n const stableCostTags = {\n runDir: args.opts.runDir,\n cellId: args.slot.cellId,\n scenarioId: args.slot.scenario.id,\n rep: String(args.slot.rep),\n }\n const costTags = { ...stableCostTags, runAttemptId: args.runAttemptId }\n\n // Resumability: cache key = (manifestHash, scenarioId, rep)\n const cachePath = join(cellDir, 'cached-result.json')\n if (args.resumable) {\n const cached = readCachedCell<TArtifact>({\n storage,\n cachePath,\n cellId: args.slot.cellId,\n manifestHash: args.manifestHash,\n })\n if (cached.status === 'hit') {\n enforceDispatchUsage(cached.cell, args.opts.expectUsage ?? 'warn')\n const cachedHasUsage =\n cached.cell.costUsd > 0 ||\n cached.cell.tokenUsage.input > 0 ||\n cached.cell.tokenUsage.output > 0\n if (cached.cell.costCallIds === undefined) {\n if (cachedHasUsage || Object.keys(cached.cell.judgeScores).length > 0) {\n throw new CostAccountingIncompleteError(\n `runCampaign: cached cell '${args.slot.cellId}' does not identify its ledger receipts`,\n )\n }\n } else if (\n !Array.isArray(cached.cell.costCallIds) ||\n cached.cell.costCallIds.some(\n (callId) => typeof callId !== 'string' || callId.trim().length === 0,\n ) ||\n new Set(cached.cell.costCallIds).size !== cached.cell.costCallIds.length\n ) {\n throw new CostAccountingIncompleteError(\n `runCampaign: cached cell '${args.slot.cellId}' has invalid ledger receipt IDs`,\n )\n } else {\n const restoredCallIds = new Set(\n args.costLedger.list({ tags: stableCostTags }).map((receipt) => receipt.callId),\n )\n const missingCallIds = cached.cell.costCallIds.filter(\n (callId) => !restoredCallIds.has(callId),\n )\n if (missingCallIds.length > 0) {\n throw new CostAccountingIncompleteError(\n `runCampaign: cached cell '${args.slot.cellId}' is missing ledger receipt(s): ${missingCallIds.join(', ')}`,\n )\n }\n }\n return { cell: { ...cached.cell, cached: true }, artifactsByPath: {} }\n }\n }\n\n const startMs = Date.now()\n const trace = args.buildTraceWriter(args.slot.cellId, cellDir)\n const artifactsByPath: Record<string, string> = {}\n const artifacts: CampaignArtifactWriter = {\n async write(path, content) {\n const fullPath = join(cellDir, path)\n storage.ensureDir(join(fullPath, '..'))\n storage.write(fullPath, content)\n artifactsByPath[`${args.slot.cellId}/${path}`] = fullPath\n return fullPath\n },\n async writeJson(path, value) {\n return artifacts.write(path, JSON.stringify(value, null, 2))\n },\n }\n const cost: CampaignCostMeter = {\n async runPaidCall(input) {\n const result = await args.costLedger.runPaidCall({\n ...input,\n channel: input.channel ?? 'agent',\n phase: args.costPhase,\n actor: input.actor,\n tags: costTags,\n signal: cellAbort.signal,\n })\n if (result.receipt) {\n trace.span(`cost.${result.receipt.actor}`, { amountUsd: result.receipt.costUsd }).end()\n }\n return result\n },\n }\n\n const placement = args.opts.cellPlacement?.({\n scenario: args.slot.scenario,\n rep: args.slot.rep,\n })\n\n // Per-cell abort signal, chained to the campaign signal. The dispatch sees\n // THIS signal so a timeout (below) can abort just this cell's in-flight work\n // without tearing down sibling cells — and a signal-honoring dispatch\n // releases its open request instead of leaking it past the deadline.\n const cellAbort = new AbortController()\n const onCampaignAbort = () => cellAbort.abort((args.signal as { reason?: unknown }).reason)\n if (args.signal.aborted) cellAbort.abort((args.signal as { reason?: unknown }).reason)\n else args.signal.addEventListener('abort', onCampaignAbort, { once: true })\n\n const ctx: DispatchContext = {\n cellId: args.slot.cellId,\n rep: args.slot.rep,\n seed: args.slot.cellSeed,\n signal: cellAbort.signal,\n trace,\n artifacts,\n cost,\n placement,\n }\n\n let artifact: TArtifact | undefined\n let errorMessage: string | undefined\n const timeoutMs = args.dispatchTimeoutMs\n let timeoutTimer: ReturnType<typeof setTimeout> | undefined\n try {\n const dispatched = args.opts.dispatch(args.slot.scenario, ctx)\n if (timeoutMs !== undefined && timeoutMs > 0) {\n // A dispatch that never settles (stalled model request, exhausted runtime\n // resource, a stream that never closes) must NOT hang the cell — and with\n // it the lane, the campaign, the loop, the CI job — forever. Race it\n // against the deadline; on timeout, abort the cell and fail it LOUD.\n artifact = await Promise.race([\n dispatched,\n new Promise<never>((_, reject) => {\n timeoutTimer = setTimeout(() => {\n cellAbort.abort(new Error('dispatch timeout'))\n reject(\n new Error(\n `dispatch exceeded ${timeoutMs}ms for cell '${args.slot.cellId}' — aborted and failed loud (no silent hang)`,\n ),\n )\n }, timeoutMs)\n if (typeof (timeoutTimer as { unref?: () => void }).unref === 'function')\n (timeoutTimer as { unref: () => void }).unref()\n }),\n ])\n } else {\n artifact = await dispatched\n }\n } catch (err) {\n errorMessage = err instanceof Error ? err.message : String(err)\n } finally {\n if (timeoutTimer) clearTimeout(timeoutTimer)\n args.signal.removeEventListener('abort', onCampaignAbort)\n }\n\n const agentReceipts = args.costLedger.list({ channel: 'agent', tags: costTags })\n const agentCost = args.costLedger.summary({ channel: 'agent', tags: costTags })\n const tokenUsage: CampaignTokenUsage = {\n input: agentCost.inputTokens,\n output: agentCost.outputTokens,\n ...(agentCost.cachedTokens > 0 ? { cached: agentCost.cachedTokens } : {}),\n }\n const resolvedModel = agentReceipts.at(-1)?.model\n const dispatchResult = {\n cellId: args.slot.cellId,\n artifact,\n error: errorMessage,\n costUsd: agentCost.totalCostUsd,\n tokenUsage,\n }\n try {\n enforceDispatchUsage(dispatchResult, args.opts.expectUsage ?? 'warn')\n } catch (error) {\n await trace.flush()\n throw error\n }\n\n // Run judges (only if we have an artifact). A judge that throws invalidates\n // the cell — recorded as `error`, NOT folded into a fake composite:0 (a fake\n // zero is indistinguishable from a real zero and poisons every aggregate).\n const judgeScores: Record<string, JudgeScore> = {}\n if (artifact !== undefined) {\n for (const judge of args.opts.judges ?? []) {\n if (judge.appliesTo && !judge.appliesTo(args.slot.scenario)) continue\n try {\n const score = await runJudgeCell(judge, {\n artifact,\n scenario: args.slot.scenario,\n signal: args.signal,\n costLedger: args.costLedger,\n costPhase: args.costPhase,\n costTags,\n })\n judgeScores[judge.name] = score\n } catch (err) {\n if (err instanceof CostAccountingIncompleteError) {\n await trace.flush()\n throw err\n }\n errorMessage = `judge '${judge.name}' failed: ${err instanceof Error ? err.message : String(err)}`\n break\n }\n }\n }\n\n await trace.flush()\n\n const costCallIds = args.costLedger\n .list({ tags: costTags })\n .map((receipt) => receipt.callId)\n .sort()\n\n const cell: CampaignCellResult<TArtifact> = {\n manifestHash: args.manifestHash,\n cellId: args.slot.cellId,\n scenarioId: args.slot.scenario.id,\n rep: args.slot.rep,\n artifact: (artifact ?? null) as TArtifact,\n judgeScores,\n costUsd: agentCost.totalCostUsd,\n costEstimated: agentReceipts.some(\n (receipt) => receipt.actualCostUsd === undefined && !receipt.costUnknown,\n ),\n costCallIds,\n tokenUsage,\n ...(resolvedModel ? { resolvedModel } : {}),\n durationMs: Date.now() - startMs,\n seed: args.slot.cellSeed,\n cached: false,\n error: errorMessage,\n }\n\n if (!errorMessage && args.resumable) {\n storage.write(cachePath, JSON.stringify(cell))\n }\n\n return { cell, artifactsByPath }\n}\n\nexport interface CampaignRunPlanCell {\n cellId: string\n scenarioId: string\n rep: number\n seed: number\n cachePath: string\n status: 'cached' | 'run'\n reason?: 'missing' | 'manifest-mismatch' | 'cell-mismatch' | 'corrupt' | 'resumable-off'\n}\n\nexport interface CampaignRunPlan {\n manifestHash: string\n splitDigest: `sha256:${string}`\n totalCells: number\n cellsCached: number\n cellsToRun: number\n cells: CampaignRunPlanCell[]\n}\n\nexport interface PlanCampaignRunOptions<TScenario extends Scenario, TArtifact> {\n scenarios: TScenario[]\n dispatch?: DispatchFn<TScenario, TArtifact>\n dispatchRef?: string\n judges?: JudgeConfig<TArtifact, TScenario>[]\n seed?: number\n reps?: number\n resumable?: boolean\n runDir: string\n /** Subject repo for the shared run-dir root (see RunCampaignOptions.repo). */\n repo?: string\n storage?: CampaignStorage\n}\n\n/**\n * Plan a campaign WITHOUT dispatching: computes the manifest hash and the per-cell\n * run-vs-cached schedule so callers can preview cost and resumability before spending.\n */\nexport function planCampaignRun<TScenario extends Scenario, TArtifact>(\n opts: PlanCampaignRunOptions<TScenario, TArtifact>,\n): CampaignRunPlan {\n const seed = opts.seed ?? 42\n const reps = opts.reps ?? 1\n const resumable = opts.resumable ?? true\n const storage = opts.storage ?? fsCampaignStorage()\n\n assertCampaignDesign(opts.scenarios, reps)\n\n if (typeof opts.runDir !== 'string' || opts.runDir.trim().length === 0) {\n throw new Error('planCampaignRun: runDir is required and must be a non-empty string')\n }\n opts.runDir = resolveRunDir(opts.runDir, opts.repo)\n\n const manifestHash = computeManifestHash({\n scenarios: opts.scenarios,\n judges: (opts.judges ?? []) as unknown as JudgeConfig<unknown>[],\n dispatchRef: dispatchRefFor(opts.dispatch, opts.dispatchRef),\n seed,\n reps,\n })\n const splitDigest = campaignSplitDigest(opts.scenarios, reps)\n\n const cells = buildCellSchedule(opts.scenarios, seed, reps).map((slot): CampaignRunPlanCell => {\n const cachePath = join(\n opts.runDir,\n slot.cellId.replace(/[^a-zA-Z0-9_-]/g, '_'),\n 'cached-result.json',\n )\n if (!resumable) {\n return {\n cellId: slot.cellId,\n scenarioId: slot.scenario.id,\n rep: slot.rep,\n seed: slot.cellSeed,\n cachePath,\n status: 'run',\n reason: 'resumable-off',\n }\n }\n\n const cached = readCachedCell<unknown>({\n storage,\n cachePath,\n cellId: slot.cellId,\n manifestHash,\n })\n if (cached.status === 'hit') {\n return {\n cellId: slot.cellId,\n scenarioId: slot.scenario.id,\n rep: slot.rep,\n seed: slot.cellSeed,\n cachePath,\n status: 'cached',\n }\n }\n\n return {\n cellId: slot.cellId,\n scenarioId: slot.scenario.id,\n rep: slot.rep,\n seed: slot.cellSeed,\n cachePath,\n status: 'run',\n reason: cached.reason,\n }\n })\n\n const cellsCached = cells.filter((cell) => cell.status === 'cached').length\n return {\n manifestHash,\n splitDigest,\n totalCells: cells.length,\n cellsCached,\n cellsToRun: cells.length - cellsCached,\n cells,\n }\n}\n\n/**\n * Per-dispatch stub guard. An artifact produced with `costUsd === 0` AND zero\n * tokens means the dispatch never called `ctx.cost` —\n * i.e. it ran against a stub or silently dropped its usage. `'warn'` logs it,\n * `'assert'` throws (fail-fast), and `'off'` skips the check.\n */\nfunction enforceDispatchUsage(\n cell: Pick<\n CampaignCellResult<unknown>,\n 'cellId' | 'artifact' | 'error' | 'costUsd' | 'tokenUsage'\n >,\n mode: 'assert' | 'warn' | 'off',\n): void {\n if (mode === 'off') return\n if (cell.artifact === null || cell.artifact === undefined) return\n const zeroTokens = cell.tokenUsage.input === 0 && cell.tokenUsage.output === 0\n if (cell.costUsd !== 0 || !zeroTokens) return\n const msg = `cell '${cell.cellId}' produced an artifact but reported zero cost and zero tokens — the dispatch made no paid call through ctx.cost.runPaidCall (a stub cell)`\n if (mode === 'assert') {\n const report: BackendIntegrityReport = {\n totalRecords: 1,\n stubRecords: 1,\n realRecords: 0,\n uncostedRecords: 0,\n totalInputTokens: 0,\n totalOutputTokens: 0,\n totalCostUsd: 0,\n verdict: 'stub',\n diagnosis: msg,\n }\n throw new BackendIntegrityError(`expectUsage: ${msg}`, report)\n }\n // eslint-disable-next-line no-console\n console.warn(`[runCampaign] expectUsage: ${msg}`)\n}\n\nasync function runJudgeCell<TArtifact, TScenario extends Scenario>(\n judge: JudgeConfig<TArtifact, TScenario>,\n input: Parameters<JudgeConfig<TArtifact, TScenario>['score']>[0],\n): Promise<JudgeScore> {\n const previousJudgeCalls = new Set(\n input.costLedger\n ?.list({ channel: 'judge', tags: input.costTags })\n .map((receipt) => receipt.callId) ?? [],\n )\n try {\n const score = await judge.score(input)\n assertReportedJudgeCallRecorded(judge.name, score, input, previousJudgeCalls)\n return score\n } catch (error) {\n assertReportedJudgeCallRecorded(judge.name, error, input, previousJudgeCalls, error)\n throw error\n }\n}\n\nfunction assertReportedJudgeCallRecorded(\n judgeName: string,\n value: unknown,\n input: Parameters<JudgeConfig<unknown>['score']>[0],\n previousCallIds: ReadonlySet<string>,\n cause?: unknown,\n): void {\n if (!hasLlmCall(value)) return\n const recorded = input.costLedger\n ?.list({ channel: 'judge', tags: input.costTags })\n .some((receipt) => !previousCallIds.has(receipt.callId))\n if (recorded) return\n throw new CostAccountingIncompleteError(\n `runCampaign: judge '${judgeName}' reported a paid LLM call without a CostLedger receipt`,\n cause === undefined ? undefined : { cause },\n )\n}\n\nfunction hasLlmCall(value: unknown): value is { llmCall: unknown } {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'llmCall' in value &&\n (value as { llmCall?: unknown }).llmCall !== undefined\n )\n}\n\nfunction defaultBuildTraceWriter(\n storage: CampaignStorage,\n): (cellId: string, dir: string) => CampaignTraceWriter {\n return (cellId, dir) => {\n const spans: Array<Record<string, unknown>> = []\n return {\n span(name, attributes) {\n const startMs = Date.now()\n const record: Record<string, unknown> = { name, cellId, startMs, ...(attributes ?? {}) }\n const finish: TraceSpan = {\n end(endAttrs) {\n record.durationMs = Date.now() - startMs\n if (endAttrs) Object.assign(record, endAttrs)\n spans.push(record)\n },\n setAttribute(key, value) {\n record[key] = value\n },\n }\n return finish\n },\n async flush() {\n storage.write(join(dir, 'spans.jsonl'), spans.map((s) => JSON.stringify(s)).join('\\n'))\n },\n }\n }\n}\n\nfunction buildCellSchedule<TScenario extends Scenario>(\n scenarios: TScenario[],\n seed: number,\n reps: number,\n): Array<{ scenario: TScenario; rep: number; cellId: string; cellSeed: number }> {\n const schedule: Array<{ scenario: TScenario; rep: number; cellId: string; cellSeed: number }> = []\n let cellIndex = 0\n for (const scenario of scenarios) {\n for (let rep = 0; rep < reps; rep++) {\n const cellId = `${scenario.id}:${rep}`\n const cellSeed = seed + cellIndex\n schedule.push({ scenario, rep, cellId, cellSeed })\n cellIndex += 1\n }\n }\n return schedule\n}\n\nfunction dispatchRefFor<TScenario extends Scenario, TArtifact>(\n dispatch: DispatchFn<TScenario, TArtifact> | undefined,\n override: string | undefined,\n): string {\n const ref = override ?? dispatch?.name ?? 'anonymous'\n if (typeof ref !== 'string' || ref.trim().length === 0) {\n throw new Error('runCampaign: dispatchRef must be a non-empty string when provided')\n }\n return ref\n}\n\ntype CacheRead<TArtifact> =\n | { status: 'hit'; cell: CampaignCellResult<TArtifact> }\n | { status: 'miss'; reason: 'missing' | 'manifest-mismatch' | 'cell-mismatch' | 'corrupt' }\n\nfunction readCachedCell<TArtifact>(args: {\n storage: CampaignStorage\n cachePath: string\n cellId: string\n manifestHash: string\n}): CacheRead<TArtifact> {\n const raw = args.storage.read(args.cachePath)\n if (raw === undefined) return { status: 'miss', reason: 'missing' }\n\n try {\n const cached = JSON.parse(raw) as CampaignCellResult<TArtifact>\n if (cached.cellId !== args.cellId) return { status: 'miss', reason: 'cell-mismatch' }\n if (cached.manifestHash !== args.manifestHash) {\n return { status: 'miss', reason: 'manifest-mismatch' }\n }\n return { status: 'hit', cell: cached }\n } catch {\n return { status: 'miss', reason: 'corrupt' }\n }\n}\n\ninterface CaptureArgs<TScenario extends Scenario, TArtifact> {\n store: LabeledScenarioStore\n cell: CampaignCellResult<TArtifact>\n scenario: TScenario\n opts: RunCampaignOptions<TScenario, TArtifact>\n now: () => Date\n}\n\nasync function captureToStore<TScenario extends Scenario, TArtifact>(\n args: CaptureArgs<TScenario, TArtifact>,\n): Promise<void> {\n await args.store.observe({\n scenario: args.scenario,\n artifact: args.cell.artifact,\n judgeScores: args.cell.judgeScores,\n source: args.opts.captureSource ?? 'eval-run',\n sourceVersionHash: args.opts.captureSourceVersionHash ?? 'unknown',\n capturedAt: args.now().toISOString(),\n redactionStatus: 'raw',\n })\n}\n\n// ── Aggregates + manifest hash ────────────────────────────────────────\n\nfunction computeManifestHash(input: {\n scenarios: Scenario[]\n judges: JudgeConfig<unknown>[]\n dispatchRef: string\n seed: number\n reps: number\n}): string {\n return contentHash({\n scenarios: input.scenarios,\n judges: input.judges.map((judge) => ({\n name: judge.name,\n dims: judge.dimensions,\n version: judgeVersionFor(judge),\n })),\n dispatch: input.dispatchRef,\n seed: input.seed,\n reps: input.reps,\n })\n}\n\nfunction judgeVersionFor(judge: JudgeConfig<unknown>): string {\n if (judge.judgeVersion !== undefined) {\n const version = judge.judgeVersion.trim()\n if (version.length === 0) {\n throw new Error(`runCampaign: judge '${judge.name}' has an empty judgeVersion`)\n }\n return version\n }\n return contentHash({\n score: judge.score.toString(),\n appliesTo: judge.appliesTo?.toString() ?? null,\n })\n}\n\nfunction computeAggregates<TArtifact>(\n cells: CampaignCellResult<TArtifact>[],\n judges: JudgeConfig<TArtifact>[],\n seed: number,\n cost: CostLedgerSummary,\n): CampaignAggregates {\n const byJudge: Record<string, JudgeAggregate> = {}\n for (const judge of judges) {\n const scores: number[] = []\n for (const cell of cells) {\n const s = cell.judgeScores[judge.name]\n if (s !== undefined) scores.push(s.composite)\n }\n byJudge[judge.name] = aggregate(scores, seed)\n }\n const byScenario: Record<string, ScenarioAggregate> = {}\n const scenarioGroups = new Map<string, number[]>()\n for (const cell of cells) {\n const composites = Object.values(cell.judgeScores).map((s) => s.composite)\n if (composites.length === 0) continue\n const mean = composites.reduce((a, b) => a + b, 0) / composites.length\n const arr = scenarioGroups.get(cell.scenarioId) ?? []\n arr.push(mean)\n scenarioGroups.set(cell.scenarioId, arr)\n }\n for (const [scenarioId, samples] of scenarioGroups) {\n const ag = aggregate(samples, seed)\n byScenario[scenarioId] = { meanComposite: ag.mean, ci95: ag.ci95, n: ag.n }\n }\n return {\n byJudge,\n byScenario,\n cost,\n totalCostUsd: cost.totalCostUsd,\n cellsExecuted: cells.filter((c) => !c.error).length,\n cellsSkipped: cells.filter((c) => c.error?.startsWith('skipped:')).length,\n cellsCached: cells.filter((c) => c.cached).length,\n cellsFailed: cells.filter((c) => c.error && !c.error.startsWith('skipped:')).length,\n }\n}\n\n// Percentile bootstrap CI95 via seeded resampling. Deterministic for a given\n// seed — same campaign re-run produces identical CI bands. Falls back to\n// degenerate intervals at n<=1 (the bootstrap is undefined there).\nfunction aggregate(samples: number[], seed: number): JudgeAggregate {\n const n = samples.length\n if (n === 0) return { mean: 0, stdev: 0, ci95: [0, 0], n: 0 }\n const mean = samples.reduce((a, b) => a + b, 0) / n\n const variance = samples.reduce((a, b) => a + (b - mean) ** 2, 0) / Math.max(1, n - 1)\n const stdev = Math.sqrt(variance)\n const ci = confidenceInterval(samples, 0.95, { seed, resamples: 1000 })\n return { mean, stdev, ci95: [ci.lower, ci.upper], n }\n}\n","/**\n * Backend-integrity guard: distinguish \"agent failed\" from \"eval ran against\n * a stub / unconfigured backend.\" Without this guard a canonical eval can\n * silently report `0/N passed` and look like an agent-quality problem when\n * the LLM was never actually called — the failure mode we just hit running\n * the 4-vertical parallel eval (legal-sandbox-stub returned hard-coded 33-104\n * char strings; gtm/creative defaulted to a cli-bridge that wasn't running).\n *\n * The shape:\n *\n * const report = summarizeBackendIntegrity(records)\n * assertRealBackend(records) // throws BackendIntegrityError if 100% stub\n *\n * A record is \"stub-mode\" if its `tokenUsage.input === 0 && tokenUsage.output === 0`.\n * (`costUsd` alone is unreliable — some backends successfully call LLMs but\n * don't propagate pricing, producing real tokens with $0 cost.)\n *\n * Verdicts:\n * - `real` — at least one record has nonzero token usage\n * - `stub` — every record is stub-mode (eval ran blind)\n * - `mixed` — some records real, some stub (partial backend failure;\n * often the 429-cascade or auth-half-failed case)\n */\n\nimport type { CostReceipt } from '../cost-ledger'\nimport { AgentEvalError } from '../errors'\nimport type { RunRecord } from '../run-record'\n\nexport interface BackendIntegrityReport {\n /** Total records inspected. */\n totalRecords: number\n /** Records with input=0 AND output=0 (a stub fingerprint). */\n stubRecords: number\n /** Records with nonzero token usage (real LLM activity). */\n realRecords: number\n /** Records where output>0 but costUsd=0 (real LLM, broken cost ledger). */\n uncostedRecords: number\n /** Sum of input tokens across all records. */\n totalInputTokens: number\n /** Sum of output tokens across all records. */\n totalOutputTokens: number\n /** Sum of costUsd across all records. */\n totalCostUsd: number\n /** Worst-case integrity verdict. */\n verdict: 'real' | 'mixed' | 'stub'\n /** Human-readable diagnosis suitable for terminal output. */\n diagnosis: string\n}\n\n/**\n * Error thrown when an integrity assertion fails. Caller can pattern-match\n * by `code === 'AGENT_EVAL_BACKEND_STUB'` to differentiate from other\n * errors.\n */\nexport class BackendIntegrityError extends AgentEvalError {\n constructor(\n message: string,\n public readonly report: BackendIntegrityReport,\n ) {\n super('backend_integrity', message)\n }\n}\n\n/**\n * Inspect a batch of RunRecords and return an integrity report. Pure\n * function — no I/O, no logging. The caller decides what to do with the\n * verdict (print warning, throw, gate CI, etc.).\n */\nexport function summarizeBackendIntegrity(\n records: ReadonlyArray<RunRecord>,\n): BackendIntegrityReport {\n return summarizeBackendUsage(\n records.map((record) => ({\n inputTokens: record.tokenUsage.input,\n outputTokens: record.tokenUsage.output,\n costUsd: record.costUsd,\n })),\n )\n}\n\n/** Inspect settled agent calls from the canonical cost ledger. */\nexport function summarizeAgentReceiptIntegrity(\n receipts: ReadonlyArray<CostReceipt>,\n): BackendIntegrityReport {\n return summarizeBackendUsage(\n receipts\n .filter((receipt) => receipt.channel === 'agent')\n .map((receipt) => ({\n inputTokens: receipt.inputTokens,\n outputTokens: receipt.outputTokens,\n costUsd: receipt.costUsd,\n })),\n )\n}\n\ninterface BackendUsage {\n inputTokens: number\n outputTokens: number\n costUsd: number\n}\n\nfunction summarizeBackendUsage(records: readonly BackendUsage[]): BackendIntegrityReport {\n const totalRecords = records.length\n let stubRecords = 0\n let realRecords = 0\n let uncostedRecords = 0\n let totalInputTokens = 0\n let totalOutputTokens = 0\n let totalCostUsd = 0\n for (const rec of records) {\n totalInputTokens += rec.inputTokens\n totalOutputTokens += rec.outputTokens\n totalCostUsd += rec.costUsd\n if (rec.inputTokens === 0 && rec.outputTokens === 0) stubRecords++\n else realRecords++\n if (rec.outputTokens > 0 && rec.costUsd === 0) uncostedRecords++\n }\n const verdict: BackendIntegrityReport['verdict'] =\n totalRecords === 0\n ? 'stub'\n : stubRecords === totalRecords\n ? 'stub'\n : stubRecords === 0\n ? 'real'\n : 'mixed'\n const diagnosis = buildDiagnosis({\n totalRecords,\n stubRecords,\n realRecords,\n uncostedRecords,\n totalInputTokens,\n totalOutputTokens,\n totalCostUsd,\n verdict,\n })\n return {\n totalRecords,\n stubRecords,\n realRecords,\n uncostedRecords,\n totalInputTokens,\n totalOutputTokens,\n totalCostUsd,\n verdict,\n diagnosis,\n }\n}\n\nfunction buildDiagnosis(r: Omit<BackendIntegrityReport, 'diagnosis'>): string {\n if (r.totalRecords === 0) {\n return 'no records — eval produced zero runs; backend likely failed before first turn'\n }\n if (r.verdict === 'stub') {\n return [\n `all ${r.totalRecords} records have zero token usage — the LLM backend was never called.`,\n 'common causes: --backend sandbox without a sandbox bridge running; stub model returning hard-coded strings;',\n 'auth misconfigured so requests were silently dropped before the LLM. Re-run with --backend tcloud and TANGLE_API_KEY set,',\n 'or boot the cli-bridge / sandbox before invoking the eval.',\n ].join(' ')\n }\n if (r.verdict === 'mixed') {\n const pct = ((r.stubRecords / r.totalRecords) * 100).toFixed(0)\n return [\n `${r.stubRecords}/${r.totalRecords} records (${pct}%) have zero token usage — the backend partially failed.`,\n 'common causes: rate-limit cascade (429s after the first N personas);',\n 'transient auth expiry mid-run; provider outage. Treat the affected records as missing data, not agent failures.',\n ].join(' ')\n }\n // verdict === 'real'\n if (r.uncostedRecords > 0) {\n const pct = ((r.uncostedRecords / r.totalRecords) * 100).toFixed(0)\n return [\n `${r.totalRecords} records with real LLM activity (in=${r.totalInputTokens}, out=${r.totalOutputTokens} tokens).`,\n `${r.uncostedRecords} (${pct}%) have output tokens but costUsd=0. Two distinct roots:`,\n '(a) cost ledger mis-wired — no usage propagation from the runtime stream into RunRecord; or',\n '(b) the model is unpriced at the source (sandbox/router returned $0 despite real tokens).',\n 'For (b), price the measured tokens against the substrate table (estimateCost) instead of leaving $0.',\n ].join(' ')\n }\n return `${r.totalRecords} records with real LLM activity (in=${r.totalInputTokens}, out=${r.totalOutputTokens} tokens, $${r.totalCostUsd.toFixed(4)}).`\n}\n\n/**\n * Throw BackendIntegrityError if the verdict is 'stub' — i.e. every record\n * shows zero LLM activity. Non-strict callers can pass `{ allowMixed: false }`\n * to also reject mixed verdicts (recommended for CI gates).\n *\n * Real backends pass through silently.\n */\nexport function assertRealBackend(\n records: ReadonlyArray<RunRecord>,\n opts: { allowMixed?: boolean } = {},\n): BackendIntegrityReport {\n const report = summarizeBackendIntegrity(records)\n return assertBackendReport(report, opts)\n}\n\n/** Reject a cost ledger with no real agent call or a partial stub run. */\nexport function assertRealAgentReceipts(\n receipts: ReadonlyArray<CostReceipt>,\n opts: { allowMixed?: boolean } = {},\n): BackendIntegrityReport {\n const report = summarizeAgentReceiptIntegrity(receipts)\n return assertBackendReport(report, opts)\n}\n\nfunction assertBackendReport(\n report: BackendIntegrityReport,\n opts: { allowMixed?: boolean },\n): BackendIntegrityReport {\n const allowMixed = opts.allowMixed ?? true\n if (report.verdict === 'stub') {\n throw new BackendIntegrityError(\n `backend-integrity: ran against a stub or unconfigured backend — ${report.diagnosis}`,\n report,\n )\n }\n if (!allowMixed && report.verdict === 'mixed') {\n throw new BackendIntegrityError(\n `backend-integrity: partial backend failure rejected — ${report.diagnosis}`,\n report,\n )\n }\n return report\n}\n","/**\n * Content-addressed judge-verdict caching.\n *\n * LAW: cache JUDGE VERDICTS only — judging the same artifact with the same\n * judge+rubric is pure. NEVER cache agent rollouts. (A router that cached\n * identical fanout prompts silently destroyed best-of-N diversity; rollout\n * caching reintroduces that failure class. Judging has no diversity to\n * destroy — same artifact + same rubric ⇒ same verdict is the desired\n * property, not a bug.)\n *\n * The cache key is a sha-256 over the canonical JSON of everything that can\n * change a verdict: the artifact content, the scenario id, the judge name,\n * the full dimension list (key + description — the description IS the rubric\n * text shown to the judge), and a caller-supplied `judgeVersion`.\n * `judgeVersion` is REQUIRED: a judge whose prompt/model/ensemble changes\n * without a version bump would otherwise silently serve stale verdicts.\n *\n * Strict canonicalization (`canonicalJson`) throws on undefined / function /\n * symbol / non-finite numbers — an artifact that cannot be unambiguously\n * serialized cannot be content-addressed, and coercing it would let two\n * different artifacts collide on one key.\n */\n\nimport { createHash } from 'node:crypto'\nimport { appendFileSync, existsSync, readFileSync } from 'node:fs'\nimport type { JudgeConfig, JudgeScore, Scenario } from './campaign/types'\n\n// ── canonical JSON + content hash ─────────────────────────────────────────\n\nfunction canonicalizeAt(value: unknown, path: string): string {\n if (value === null) return 'null'\n switch (typeof value) {\n case 'boolean':\n return value ? 'true' : 'false'\n case 'number':\n if (!Number.isFinite(value)) {\n throw new Error(\n `canonicalJson: non-finite number (${value}) at ${path} — ambiguity is an error, not a coercion`,\n )\n }\n return JSON.stringify(value)\n case 'string':\n return JSON.stringify(value)\n case 'undefined':\n case 'function':\n case 'symbol':\n throw new Error(\n `canonicalJson: ${typeof value} at ${path} — ambiguity is an error, not a coercion`,\n )\n case 'bigint':\n throw new Error(`canonicalJson: bigint at ${path} — not representable in JSON`)\n case 'object':\n break\n }\n const obj = value as Record<string, unknown>\n // Honor toJSON (Date → ISO string) before structural checks — without it a\n // Date would canonicalize to '{}' and every timestamp would collide.\n if (typeof obj.toJSON === 'function') {\n return canonicalizeAt((obj as { toJSON(): unknown }).toJSON(), path)\n }\n if (Array.isArray(obj)) {\n return `[${obj.map((item, i) => canonicalizeAt(item, `${path}[${i}]`)).join(',')}]`\n }\n if (obj instanceof Map || obj instanceof Set) {\n throw new Error(\n `canonicalJson: ${obj instanceof Map ? 'Map' : 'Set'} at ${path} — would serialize as '{}'; convert to a plain object/array first`,\n )\n }\n const keys = Object.keys(obj).sort()\n const parts = keys.map((k) => `${JSON.stringify(k)}:${canonicalizeAt(obj[k], `${path}.${k}`)}`)\n return `{${parts.join(',')}}`\n}\n\n/**\n * Stable JSON stringify: object keys sorted recursively, so two semantically\n * equal values produce byte-identical output regardless of key insertion\n * order. Throws on undefined / function / symbol / NaN / ±Infinity / bigint /\n * Map / Set — anything JSON.stringify would coerce or drop silently.\n *\n * Distinct from `pre-registration.ts`'s `canonicalize`/`hashJson`, which are\n * permissive (coercion allowed) and async (web-crypto). Use THIS pair when a\n * hash collision or silent coercion would corrupt a cache key or attestation.\n */\nexport function canonicalJson(value: unknown): string {\n return canonicalizeAt(value, '$')\n}\n\n/** Hex sha-256 over `canonicalJson(value)`. The content address used by the\n * verdict cache and report attestation. */\nexport function contentHash(value: unknown): string {\n return createHash('sha256').update(canonicalJson(value)).digest('hex')\n}\n\n// ── store contract ─────────────────────────────────────────────────────────\n\n/** Pluggable verdict store. Sync or async on both legs — `cachedJudge`\n * awaits the results either way. */\nexport interface VerdictCacheStore {\n get(key: string): Promise<JudgeScore | undefined> | JudgeScore | undefined\n set(key: string, score: JudgeScore): Promise<void> | void\n}\n\n/** Process-local Map-backed store. */\nexport function inMemoryVerdictCache(): VerdictCacheStore {\n const entries = new Map<string, JudgeScore>()\n return {\n get: (key) => entries.get(key),\n set: (key, score) => {\n entries.set(key, score)\n },\n }\n}\n\ninterface VerdictCacheLine {\n key: string\n score: JudgeScore\n}\n\nfunction parseCacheLine(line: string, path: string, lineNo: number): VerdictCacheLine {\n let parsed: unknown\n try {\n parsed = JSON.parse(line)\n } catch (err) {\n throw new Error(\n `fileVerdictCache: corrupt JSONL at ${path}:${lineNo} — ${err instanceof Error ? err.message : String(err)}`,\n )\n }\n const rec = parsed as Partial<VerdictCacheLine>\n if (\n typeof rec !== 'object' ||\n rec === null ||\n typeof rec.key !== 'string' ||\n typeof rec.score !== 'object' ||\n rec.score === null ||\n typeof rec.score.composite !== 'number' ||\n typeof rec.score.dimensions !== 'object'\n ) {\n throw new Error(\n `fileVerdictCache: invalid record shape at ${path}:${lineNo} — expected {key, score:{dimensions, composite, notes}}`,\n )\n }\n return rec as VerdictCacheLine\n}\n\n/**\n * JSONL-file-backed store: the full file is loaded into an in-memory index at\n * construction; every `set` appends one line synchronously (durable before\n * the verdict is returned). A corrupt or malformed line throws at load with\n * file:line — a skipped line would silently re-judge (cost) or, worse, mask\n * a half-written file that needs operator attention.\n */\nexport function fileVerdictCache(path: string): VerdictCacheStore {\n const entries = new Map<string, JudgeScore>()\n if (existsSync(path)) {\n const lines = readFileSync(path, 'utf8').split('\\n')\n for (let i = 0; i < lines.length; i++) {\n const line = lines[i]\n if (line === undefined || line.trim() === '') continue\n const rec = parseCacheLine(line, path, i + 1)\n entries.set(rec.key, rec.score)\n }\n }\n return {\n get: (key) => entries.get(key),\n set: (key, score) => {\n appendFileSync(path, `${JSON.stringify({ key, score })}\\n`, 'utf8')\n entries.set(key, score)\n },\n }\n}\n\n// ── cached judge wrapper ───────────────────────────────────────────────────\n\nexport interface VerdictCacheStats {\n hits: number\n misses: number\n}\n\nexport interface CachedJudgeOptions {\n /** REQUIRED — part of the cache key. Bump on any change to the judge's\n * prompt, model, ensemble, or scoring logic; silent judge upgrades must\n * never serve stale verdicts. */\n judgeVersion: string\n}\n\n/** The wrapped judge: same `JudgeConfig` seam, plus hit/miss observability. */\nexport type CachedJudge<TArtifact, TScenario extends Scenario = Scenario> = JudgeConfig<\n TArtifact,\n TScenario\n> & {\n stats(): VerdictCacheStats\n}\n\n/**\n * Wrap a `JudgeConfig` so repeat judgments of the same artifact are served\n * from the store instead of re-invoking `score()`. The wrapper is generic\n * over the judge's own type parameters and preserves `appliesTo` — it is a\n * drop-in replacement anywhere a `JudgeConfig` is accepted.\n *\n * A judge that throws is NOT cached: the error propagates and the next\n * attempt re-judges (caching a failure would pin a transient outage forever).\n */\nexport function cachedJudge<TArtifact, TScenario extends Scenario = Scenario>(\n judge: JudgeConfig<TArtifact, TScenario>,\n store: VerdictCacheStore,\n options: CachedJudgeOptions,\n): CachedJudge<TArtifact, TScenario> {\n if (typeof options.judgeVersion !== 'string' || options.judgeVersion.trim() === '') {\n throw new Error('cachedJudge: judgeVersion is required and must be a non-empty string')\n }\n const stats: VerdictCacheStats = { hits: 0, misses: 0 }\n const wrapped: CachedJudge<TArtifact, TScenario> = {\n name: judge.name,\n dimensions: judge.dimensions,\n judgeVersion: options.judgeVersion,\n async score(input) {\n const key = contentHash({\n artifact: canonicalJson(input.artifact),\n scenarioId: input.scenario.id,\n judgeName: judge.name,\n dimensions: judge.dimensions,\n judgeVersion: options.judgeVersion,\n })\n const cached = await store.get(key)\n if (cached !== undefined) {\n stats.hits += 1\n return cached\n }\n const score = await judge.score(input)\n await store.set(key, score)\n stats.misses += 1\n return score\n },\n stats: () => ({ ...stats }),\n }\n if (judge.appliesTo) wrapped.appliesTo = judge.appliesTo\n return wrapped\n}\n","import { contentHash } from '../verdict-cache'\nimport type { CampaignCellResult, CampaignScenarioIdentity, Scenario } from './types'\n\nexport interface CampaignCoverage {\n complete: boolean\n expectedCellIds: string[]\n scorableCellIds: string[]\n unscorableCells: Array<{ cellId: string; reason: string }>\n}\n\n/** Reject campaign designs whose denominator cannot be identified exactly. */\nexport function assertCampaignDesign<TScenario extends Scenario>(\n scenarios: readonly TScenario[],\n reps: number,\n): void {\n if (!Number.isSafeInteger(reps) || reps < 1) {\n throw new Error('campaign design requires reps to be a positive safe integer')\n }\n const scenarioIds = new Set<string>()\n for (const scenario of scenarios) {\n if (typeof scenario.id !== 'string' || scenario.id.trim().length === 0) {\n throw new Error('campaign design requires every scenario to have a non-empty id')\n }\n if (scenarioIds.has(scenario.id)) {\n throw new Error(`campaign design contains duplicate scenario id '${scenario.id}'`)\n }\n if (typeof scenario.kind !== 'string' || scenario.kind.trim().length === 0) {\n throw new Error('campaign design requires every scenario to have a non-empty kind')\n }\n scenarioIds.add(scenario.id)\n }\n}\n\n/** Redacted but independently verifiable identity of one complete scenario. */\nexport function campaignScenarioIdentity<TScenario extends Scenario>(\n scenario: TScenario,\n): CampaignScenarioIdentity & Pick<TScenario, 'id' | 'kind'> {\n assertCampaignDesign([scenario], 1)\n return {\n id: scenario.id,\n kind: scenario.kind,\n scenarioDigest: `sha256:${contentHash(scenario)}`,\n }\n}\n\n/** Canonical split identity reconstructed from redacted scenario identities. */\nexport function campaignSplitDigestFromIdentities(\n scenarios: readonly CampaignScenarioIdentity[],\n reps: number,\n): `sha256:${string}` {\n assertCampaignDesign(scenarios, reps)\n for (const scenario of scenarios) {\n if (!/^sha256:[a-f0-9]{64}$/.test(scenario.scenarioDigest)) {\n throw new Error(`campaign scenario '${scenario.id}' has an invalid digest`)\n }\n }\n return `sha256:${contentHash({\n schema: 'tangle.campaign-split',\n scenarios: scenarios.map(({ id, kind, scenarioDigest }) => ({ id, kind, scenarioDigest })),\n reps,\n })}`\n}\n\n/** Canonical identity of the exact scenario payloads and replicate count. */\nexport function campaignSplitDigest<TScenario extends Scenario>(\n scenarios: readonly TScenario[],\n reps: number,\n): `sha256:${string}` {\n assertCampaignDesign(scenarios, reps)\n return campaignSplitDigestFromIdentities(scenarios.map(campaignScenarioIdentity), reps)\n}\n\n/** Refuse a campaign whose retained task identities contradict its split digest. */\nexport function assertCampaignSplitIdentity(\n scenarios: readonly CampaignScenarioIdentity[],\n reps: number,\n splitDigest: string,\n): void {\n if (campaignSplitDigestFromIdentities(scenarios, reps) !== splitDigest) {\n throw new Error('campaign split digest does not match its retained scenario identities')\n }\n}\n\n/** Exact designed-denominator receipt for one campaign. */\nexport function campaignCoverage<TArtifact, TScenario extends Scenario>(\n cells: readonly CampaignCellResult<TArtifact>[],\n scenarios: readonly TScenario[],\n reps: number,\n requireJudgeScore: boolean,\n): CampaignCoverage {\n assertCampaignDesign(scenarios, reps)\n const expectedCellIds = designedCellIds(scenarios, reps)\n const cellsById = new Map<string, CampaignCellResult<TArtifact>[]>()\n for (const cell of cells) {\n const matches = cellsById.get(cell.cellId) ?? []\n matches.push(cell)\n cellsById.set(cell.cellId, matches)\n }\n\n const scorableCellIds: string[] = []\n const unscorableCells: Array<{ cellId: string; reason: string }> = []\n for (const cellId of expectedCellIds) {\n const matches = cellsById.get(cellId) ?? []\n if (matches.length === 0) {\n unscorableCells.push({ cellId, reason: 'missing campaign cell' })\n continue\n }\n if (matches.length > 1) {\n unscorableCells.push({ cellId, reason: `duplicate campaign cell (${matches.length})` })\n continue\n }\n\n const cell = matches[0]!\n const scoreEntries = Object.entries(cell.judgeScores)\n const successfulScores = scoreEntries\n .map(([, score]) => score)\n .filter((score) => score.failed !== true && Number.isFinite(score.composite))\n const nonFiniteScores = scoreEntries.filter(\n ([, score]) =>\n score.failed !== true &&\n (!Number.isFinite(score.composite) ||\n Object.values(score.dimensions).some((value) => !Number.isFinite(value))),\n )\n const reasons: string[] = []\n if (cell.error) reasons.push(cell.error)\n if (cell.artifact === null || cell.artifact === undefined) reasons.push('missing artifact')\n if (!cell.error && requireJudgeScore && successfulScores.length === 0) {\n reasons.push('no successful finite judge score')\n }\n if (scoreEntries.some(([, score]) => score.failed === true)) {\n reasons.push('judge score marked failed')\n }\n if (nonFiniteScores.length > 0) {\n reasons.push(\n `non-finite judge score: ${nonFiniteScores\n .map(([name]) => name)\n .sort()\n .join(', ')}`,\n )\n }\n\n if (reasons.length > 0) {\n unscorableCells.push({ cellId, reason: reasons.join('; ') })\n } else {\n scorableCellIds.push(cellId)\n }\n }\n\n const expected = new Set(expectedCellIds)\n for (const cell of cells) {\n if (cell.cellId !== `${cell.scenarioId}:${cell.rep}`) {\n unscorableCells.push({\n cellId: cell.cellId,\n reason: 'campaign cell id does not match scenario id and rep',\n })\n continue\n }\n if (!expected.has(cell.cellId)) {\n unscorableCells.push({ cellId: cell.cellId, reason: 'unexpected campaign cell' })\n }\n }\n\n return {\n complete: unscorableCells.length === 0 && scorableCellIds.length === expectedCellIds.length,\n expectedCellIds,\n scorableCellIds,\n unscorableCells,\n }\n}\n\nexport function formatCoverageFailures(coverage: CampaignCoverage): string {\n const shown = coverage.unscorableCells\n .slice(0, 3)\n .map((cell) => `${cell.cellId}: ${cell.reason}`)\n .join('; ')\n const remainder = coverage.unscorableCells.length - Math.min(3, coverage.unscorableCells.length)\n return remainder > 0 ? `${shown}; +${remainder} more` : shown || 'unknown coverage failure'\n}\n\nfunction designedCellIds<TScenario extends Scenario>(\n scenarios: readonly TScenario[],\n reps: number,\n): string[] {\n const ids: string[] = []\n for (const scenario of scenarios) {\n for (let rep = 0; rep < reps; rep++) ids.push(`${scenario.id}:${rep}`)\n }\n return ids\n}\n","import { homedir } from 'node:os'\nimport { basename, isAbsolute, join } from 'node:path'\n\n/** The shared, out-of-repo root for campaign/benchmark run bundles. Keeping run\n * outputs here means they never land in a repo working tree (no per-repo\n * gitignore, no clutter, no accidental commits). Layout:\n * ~/.tangle/traces/<repo>/runs/<runName>/\n * where <repo> disambiguates runs across repos in one place. */\nexport function tangleTracesRoot(): string {\n return join(homedir(), '.tangle', 'traces')\n}\n\n/** Resolve a campaign `runDir`. An absolute path is honored as-is (the caller\n * chose an explicit location). A bare name is placed under the shared home root\n * so bundles never pollute a repo working tree — the default the harness should\n * compute so callers pass a *name*, not a path. */\nexport function resolveRunDir(runDir: string, repo?: string): string {\n if (isAbsolute(runDir) || runDir.startsWith('mem://')) return runDir\n const r = repo && repo.trim().length > 0 ? repo : basename(process.cwd())\n return join(tangleTracesRoot(), r, 'runs', runDir)\n}\n","import { createRequire } from 'node:module'\nimport { join } from 'node:path'\nimport { CostLedger } from '../cost-ledger'\nimport { appendSearchLedgerLine, tryWithSearchLedgerFileLock } from './search-ledger-file'\n\n/**\n * `CampaignStorage` — the filesystem seam `runCampaign` writes through\n * (run/cell dirs, the resumability cache, per-cell artifacts, trace spans).\n *\n * The default (`fsCampaignStorage`) is the Node filesystem — identical\n * behavior to the inline `node:fs` calls it replaces, so existing CLI\n * consumers are unaffected. `inMemoryCampaignStorage` keeps everything in a\n * `Map`, so the substrate runs in environments WITHOUT a filesystem\n * (Cloudflare Workers, Deno Deploy, other edge runtimes) — the campaign\n * still produces its `CampaignResult` (cells + aggregates) in memory;\n * artifacts/traces simply aren't persisted to disk.\n *\n * Paths are opaque keys to the in-memory adapter — it does not parse them,\n * so the same `join(...)`-built paths work unchanged across both adapters.\n */\nexport interface CampaignStorage {\n /** Ensure a directory exists (recursive). No-op for in-memory. */\n ensureDir(dir: string): void\n /** Does this path exist (as a written file or an ensured dir)? */\n exists(path: string): boolean\n /** Read a UTF-8 file; `undefined` when missing or unreadable. */\n read(path: string): string | undefined\n /** Write a file (string or bytes). Parent dir is assumed ensured. */\n write(path: string, content: string | Uint8Array): void\n /** Append only when the current UTF-8 byte length matches `expectedBytes`.\n * Returns the new length, or undefined when another writer won. */\n append?(path: string, content: string, expectedBytes: number): number | undefined\n}\n\n/** Node-filesystem storage — the default. Lazily requires `node:fs` so the\n * module imports cleanly in non-Node runtimes (where the caller passes\n * `inMemoryCampaignStorage` instead and never constructs this).\n *\n * `createRequire(import.meta.url)` is the ESM-native lazy require — a bare\n * `require` is a ReferenceError under `\"type\": \"module\"`, which is exactly\n * the shape this package publishes. */\nexport function fsCampaignStorage(): CampaignStorage {\n const nodeRequire = createRequire(import.meta.url)\n const { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } = nodeRequire(\n 'node:fs',\n ) as typeof import('node:fs')\n return {\n ensureDir(dir) {\n if (!existsSync(dir)) mkdirSync(dir, { recursive: true })\n },\n exists(path) {\n return existsSync(path)\n },\n read(path) {\n try {\n return readFileSync(path, 'utf8')\n } catch {\n return undefined\n }\n },\n write(path, content) {\n writeFileSync(path, content as Uint8Array)\n },\n append(path, content, expectedBytes) {\n const result = tryWithSearchLedgerFileLock(path, () => {\n let actualBytes = 0\n try {\n actualBytes = statSync(path).size\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error\n }\n if (actualBytes !== expectedBytes) return undefined\n appendSearchLedgerLine(path, content)\n return expectedBytes + Buffer.byteLength(content)\n })\n return result.acquired ? result.value : undefined\n },\n }\n}\n\n/** In-memory storage for filesystem-less runtimes. Artifacts + trace spans\n * live in a `Map` for the duration of the run; the `CampaignResult` is\n * fully populated, but nothing is persisted to disk. */\nexport function inMemoryCampaignStorage(): CampaignStorage {\n const files = new Map<string, string | Uint8Array>()\n const dirs = new Set<string>()\n return {\n ensureDir(dir) {\n dirs.add(dir)\n },\n exists(path) {\n return files.has(path) || dirs.has(path)\n },\n read(path) {\n const value = files.get(path)\n if (value === undefined) return undefined\n return typeof value === 'string' ? value : new TextDecoder().decode(value)\n },\n write(path, content) {\n files.set(path, content)\n },\n append(path, content, expectedBytes) {\n const current = files.get(path)\n const currentText =\n current === undefined\n ? ''\n : typeof current === 'string'\n ? current\n : new TextDecoder().decode(current)\n const currentBytes = new TextEncoder().encode(currentText).byteLength\n if (currentBytes !== expectedBytes) return undefined\n files.set(path, `${currentText}${content}`)\n return currentBytes + new TextEncoder().encode(content).byteLength\n },\n }\n}\n\n/** Open the durable spend account stored beside a logical run. */\nexport function createRunCostLedger(input: {\n storage: CampaignStorage\n runDir: string\n costCeilingUsd?: number\n}): CostLedger {\n const path = join(input.runDir, 'cost-ledger.jsonl')\n input.storage.ensureDir(input.runDir)\n return new CostLedger({\n costCeilingUsd: input.costCeilingUsd,\n persistence: {\n read: () => {\n const stored = input.storage.read(path)\n if (stored === undefined && input.storage.exists(path)) {\n throw new Error(`CostLedger: cannot read existing event log '${path}'`)\n }\n const events = stored ?? ''\n return {\n revision: String(new TextEncoder().encode(events).byteLength),\n events,\n }\n },\n append: (expectedRevision, event) => {\n const expectedBytes = Number(expectedRevision)\n if (!Number.isSafeInteger(expectedBytes) || expectedBytes < 0) {\n throw new Error(`CostLedger: invalid storage revision '${expectedRevision}'`)\n }\n if (!input.storage.append) {\n throw new Error('CostLedger: CampaignStorage.append is required for paid calls')\n }\n const next = input.storage.append(path, event, expectedBytes)\n return next === undefined ? undefined : String(next)\n },\n },\n })\n}\n","/** Filesystem durability and cross-process exclusion for the search ledger. */\n\nimport { randomUUID } from 'node:crypto'\nimport {\n closeSync,\n constants,\n existsSync,\n fsyncSync,\n linkSync,\n mkdirSync,\n openSync,\n readFileSync,\n renameSync,\n unlinkSync,\n writeSync,\n} from 'node:fs'\nimport { hostname } from 'node:os'\nimport { dirname } from 'node:path'\nimport { z } from 'zod'\nimport { SearchLedgerIntegrityError } from './search-ledger-errors'\n\nexport function appendSearchLedgerLine(path: string, line: string): void {\n mkdirSync(dirname(path), { recursive: true })\n const fd = openSync(path, constants.O_CREAT | constants.O_WRONLY | constants.O_APPEND, 0o600)\n try {\n writeAll(fd, Buffer.from(line, 'utf8'))\n fsyncSync(fd)\n } finally {\n closeSync(fd)\n }\n fsyncDirectory(dirname(path))\n}\n\nexport function withSearchLedgerFileLock<T>(ledgerPath: string, run: () => T): T {\n const result = tryWithSearchLedgerFileLock(ledgerPath, run)\n if (!result.acquired) {\n throw new SearchLedgerIntegrityError(`search ledger lock is held (${ledgerPath})`)\n }\n return result.value\n}\n\nexport type FileLockResult<T> = { acquired: true; value: T } | { acquired: false }\n\nexport function tryWithSearchLedgerFileLock<T>(\n ledgerPath: string,\n run: () => T,\n): FileLockResult<T> {\n mkdirSync(dirname(ledgerPath), { recursive: true })\n const lockPath = `${ledgerPath}.lock`\n const owner = acquireLock(lockPath)\n if (!owner) return { acquired: false }\n try {\n return { acquired: true, value: run() }\n } finally {\n releaseLock(lockPath, owner)\n }\n}\n\nfunction writeAll(fd: number, bytes: Buffer): void {\n let offset = 0\n while (offset < bytes.byteLength) {\n const written = writeSync(fd, bytes, offset, bytes.byteLength - offset)\n if (written <= 0) throw new SearchLedgerIntegrityError('filesystem wrote zero bytes')\n offset += written\n }\n}\n\nfunction fsyncDirectory(path: string): void {\n const fd = openSync(path, constants.O_RDONLY)\n try {\n fsyncSync(fd)\n } finally {\n closeSync(fd)\n }\n}\n\ninterface LockOwner {\n pid: number\n host: string\n nonce: string\n}\n\n/** Create a complete owner inode first, then hard-link it into the fixed lock\n * path. `link` is the atomic compare-and-set; a crash can never leave an empty\n * or partially-written lock owner. */\nfunction acquireLock(lockPath: string): LockOwner | undefined {\n const owner: LockOwner = { pid: process.pid, host: hostname(), nonce: randomUUID() }\n const ownerBytes = `${canonicalOwner(owner)}\\n`\n for (let attempt = 0; attempt < 8; attempt += 1) {\n const ownerPath = `${lockPath}.${owner.pid}.${owner.nonce}.${attempt}.owner`\n const fd = openSync(ownerPath, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY, 0o600)\n try {\n writeAll(fd, Buffer.from(ownerBytes, 'utf8'))\n fsyncSync(fd)\n } finally {\n closeSync(fd)\n }\n\n try {\n linkSync(ownerPath, lockPath)\n unlinkSync(ownerPath)\n return owner\n } catch (error) {\n unlinkIfExists(ownerPath)\n if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error\n }\n\n const holder = readOwner(lockPath)\n if (holder.host !== owner.host || isProcessAlive(holder.pid)) {\n return undefined\n }\n\n const tombstone = `${lockPath}.stale.${owner.nonce}.${attempt}`\n try {\n renameSync(lockPath, tombstone)\n unlinkSync(tombstone)\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error\n }\n }\n throw new SearchLedgerIntegrityError(`could not acquire search ledger lock ${lockPath}`)\n}\n\nfunction releaseLock(lockPath: string, owner: LockOwner): void {\n if (!existsSync(lockPath)) return\n const holder = readOwner(lockPath)\n if (canonicalOwner(holder) !== canonicalOwner(owner)) {\n throw new SearchLedgerIntegrityError(\n `search ledger lock owner changed before release (${lockPath})`,\n )\n }\n unlinkSync(lockPath)\n}\n\nfunction readOwner(lockPath: string): LockOwner {\n let raw: unknown\n try {\n raw = JSON.parse(readFileSync(lockPath, 'utf8'))\n } catch (error) {\n throw new SearchLedgerIntegrityError(`search ledger lock ${lockPath} is malformed`, {\n cause: error,\n })\n }\n const parsed = z\n .object({\n pid: z.number().int().positive().safe(),\n host: z.string().trim().min(1),\n nonce: z.string().trim().min(1),\n })\n .strict()\n .safeParse(raw)\n if (!parsed.success) {\n throw new SearchLedgerIntegrityError(\n `search ledger lock ${lockPath} is malformed: ${parsed.error.issues\n .map((issue) => `${issue.path.join('.') || '<root>'}: ${issue.message}`)\n .join('; ')}`,\n )\n }\n return parsed.data\n}\n\nfunction canonicalOwner(owner: LockOwner): string {\n return JSON.stringify({ host: owner.host, nonce: owner.nonce, pid: owner.pid })\n}\n\nfunction isProcessAlive(pid: number): boolean {\n try {\n process.kill(pid, 0)\n return true\n } catch (error) {\n return (error as NodeJS.ErrnoException).code !== 'ESRCH'\n }\n}\n\nfunction unlinkIfExists(path: string): void {\n try {\n unlinkSync(path)\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error\n }\n}\n","import { ValidationError } from '../errors'\n\nexport class SearchLedgerError extends ValidationError {}\n\nexport class SearchLedgerIntegrityError extends SearchLedgerError {}\n\nexport class SearchLedgerConflictError extends SearchLedgerError {}\n"],"mappings":";;;;;;;;;;;;;AAWA,SAAS,QAAAA,aAAY;;;AC2Cd,IAAM,wBAAN,cAAoC,eAAe;AAAA,EACxD,YACE,SACgB,QAChB;AACA,UAAM,qBAAqB,OAAO;AAFlB;AAAA,EAGlB;AAAA,EAHkB;AAIpB;AAOO,SAAS,0BACd,SACwB;AACxB,SAAO;AAAA,IACL,QAAQ,IAAI,CAAC,YAAY;AAAA,MACvB,aAAa,OAAO,WAAW;AAAA,MAC/B,cAAc,OAAO,WAAW;AAAA,MAChC,SAAS,OAAO;AAAA,IAClB,EAAE;AAAA,EACJ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W,IAAK,CAAC;AACtE,SAAO,EAAE,MAAM,OAAO,MAAM,CAAC,GAAG,OAAO,GAAG,KAAK,GAAG,EAAE;AACtD;","names":["join","join","existsSync","readFileSync","existsSync","readFileSync","existsSync","mkdirSync","readFileSync","join","join"]}
|
package/dist/contract/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AgentImprovementMeasuredComparison, Sha256Digest } from '@tangle-network/agent-interface';
|
|
1
2
|
import { AxFunction, AxAIService } from '@ax-llm/ax';
|
|
2
3
|
import { z } from 'zod';
|
|
3
4
|
|
|
@@ -1122,6 +1123,8 @@ interface AnalystCost {
|
|
|
1122
1123
|
est_usd_per_run?: number;
|
|
1123
1124
|
/** Models the analyst expects to use (informational). */
|
|
1124
1125
|
models?: string[];
|
|
1126
|
+
/** Maximum post-cancellation wait for provider usage. Model analysts default to 5 seconds. */
|
|
1127
|
+
settlement_timeout_ms?: number;
|
|
1125
1128
|
}
|
|
1126
1129
|
interface AnalystRequirements {
|
|
1127
1130
|
/** Min number of shots / samples the analyst needs to produce signal. */
|
|
@@ -1145,7 +1148,7 @@ interface AnalystContext {
|
|
|
1145
1148
|
runId: string;
|
|
1146
1149
|
/** Stable correlation id so logs from a single registry.run() share a tag. */
|
|
1147
1150
|
correlationId: string;
|
|
1148
|
-
/**
|
|
1151
|
+
/** Enforced wall-clock deadline (epoch ms). */
|
|
1149
1152
|
deadlineMs?: number;
|
|
1150
1153
|
/** Per-analyst USD budget. Analysts MAY check before issuing LLM calls. */
|
|
1151
1154
|
budgetUsd?: number;
|
|
@@ -1384,6 +1387,10 @@ interface Scenario {
|
|
|
1384
1387
|
kind: string;
|
|
1385
1388
|
tags?: string[];
|
|
1386
1389
|
}
|
|
1390
|
+
/** Redacted identity of a complete scenario payload retained in campaign results. */
|
|
1391
|
+
interface CampaignScenarioIdentity extends Pick<Scenario, 'id' | 'kind'> {
|
|
1392
|
+
scenarioDigest: `sha256:${string}`;
|
|
1393
|
+
}
|
|
1387
1394
|
/** Context handed to every dispatch invocation. Scoped — every
|
|
1388
1395
|
* trace/span carries the cellId, every artifact write lands under the cell's
|
|
1389
1396
|
* artifact root, the cost meter accumulates per cell. */
|
|
@@ -1961,7 +1968,11 @@ interface CampaignAggregates {
|
|
|
1961
1968
|
interface CampaignResult<TArtifact = unknown, TScenario extends Scenario = Scenario> {
|
|
1962
1969
|
/** sha256(scenarios, judges, dispatch source ref, optimizer config, seed). Stable identity for reruns. */
|
|
1963
1970
|
manifestHash: string;
|
|
1971
|
+
/** Canonical identity of the exact scenario payloads and replicate count. */
|
|
1972
|
+
splitDigest: `sha256:${string}`;
|
|
1964
1973
|
seed: number;
|
|
1974
|
+
/** Replicates designed for every scenario in this campaign. */
|
|
1975
|
+
reps: number;
|
|
1965
1976
|
startedAt: string;
|
|
1966
1977
|
endedAt: string;
|
|
1967
1978
|
durationMs: number;
|
|
@@ -1975,13 +1986,14 @@ interface CampaignResult<TArtifact = unknown, TScenario extends Scenario = Scena
|
|
|
1975
1986
|
prUrl?: string;
|
|
1976
1987
|
runDir: string;
|
|
1977
1988
|
artifactsByPath: Record<string, string>;
|
|
1978
|
-
/**
|
|
1979
|
-
*
|
|
1980
|
-
|
|
1981
|
-
* want narrowed types when extending `CampaignResult`. */
|
|
1982
|
-
scenarios: Array<Pick<TScenario, 'id' | 'kind'>>;
|
|
1989
|
+
/** Redacted identities that let consumers verify the exact scenario payloads
|
|
1990
|
+
* without retaining customer task content in the result. */
|
|
1991
|
+
scenarios: Array<CampaignScenarioIdentity & Pick<TScenario, 'id' | 'kind'>>;
|
|
1983
1992
|
}
|
|
1984
1993
|
|
|
1994
|
+
/** Canonical identity of the exact scenario payloads and replicate count. */
|
|
1995
|
+
declare function campaignSplitDigest<TScenario extends Scenario>(scenarios: readonly TScenario[], reps: number): `sha256:${string}`;
|
|
1996
|
+
|
|
1985
1997
|
/**
|
|
1986
1998
|
* `CampaignStorage` — the filesystem seam `runCampaign` writes through
|
|
1987
1999
|
* (run/cell dirs, the resumability cache, per-cell artifacts, trace spans).
|
|
@@ -2316,8 +2328,8 @@ interface RunOptimizationResult<TArtifact, TScenario extends Scenario> {
|
|
|
2316
2328
|
*
|
|
2317
2329
|
* Hard-refuses unsafe configurations:
|
|
2318
2330
|
* - `tracing: 'off'` when a proposer is wired (improvement is unattributable)
|
|
2319
|
-
* - `autoOnPromote: 'config'` —
|
|
2320
|
-
*
|
|
2331
|
+
* - `autoOnPromote: 'config'` — live mutation is unsupported without
|
|
2332
|
+
* isolated deployment, rollback, and independent validation.
|
|
2321
2333
|
*/
|
|
2322
2334
|
|
|
2323
2335
|
type RunImprovementLoopOptions<TScenario extends Scenario, TArtifact> = RunOptimizationOptions<TScenario, TArtifact> & {
|
|
@@ -2331,15 +2343,11 @@ type RunImprovementLoopOptions<TScenario extends Scenario, TArtifact> = RunOptim
|
|
|
2331
2343
|
/** What to do when the gate ships:
|
|
2332
2344
|
* - `'pr'`: open a PR via `openAutoPr`
|
|
2333
2345
|
* - `'none'`: just report — caller decides what to do with the winner
|
|
2334
|
-
*
|
|
2335
|
-
* deferred to Pass B behind safety stack. */
|
|
2346
|
+
* Live-runtime self-mutation is intentionally unsupported. */
|
|
2336
2347
|
autoOnPromote: 'pr' | 'none';
|
|
2337
2348
|
/** GH owner / repo for the auto-PR. Required when autoOnPromote === 'pr'. */
|
|
2338
2349
|
ghOwner?: string;
|
|
2339
2350
|
ghRepo?: string;
|
|
2340
|
-
/** Optional render override — substrate writes a diff-shaped surface; pass
|
|
2341
|
-
* a function to format the promoted surface differently. */
|
|
2342
|
-
renderPromotedDiff?: (winnerSurface: MutableSurface, baselineSurface: MutableSurface) => string;
|
|
2343
2351
|
/** Placebo control. When supplied AND the winner differs from baseline, the
|
|
2344
2352
|
* loop scores a THIRD holdout arm: the winner surface with its content
|
|
2345
2353
|
* footprint-matched-blanked by this function (typically via `neutralizeText`).
|
|
@@ -2352,6 +2360,8 @@ type RunImprovementLoopOptions<TScenario extends Scenario, TArtifact> = RunOptim
|
|
|
2352
2360
|
interface RunImprovementLoopResult<TArtifact, TScenario extends Scenario> extends RunOptimizationResult<TArtifact, TScenario> {
|
|
2353
2361
|
baselineOnHoldout: CampaignResult<TArtifact, TScenario>;
|
|
2354
2362
|
winnerOnHoldout: CampaignResult<TArtifact, TScenario>;
|
|
2363
|
+
neutralizedOnHoldout?: CampaignResult<TArtifact, TScenario>;
|
|
2364
|
+
neutralizedSurface?: MutableSurface;
|
|
2355
2365
|
gateResult: Awaited<ReturnType<Gate<TArtifact, TScenario>['decide']>>;
|
|
2356
2366
|
/** Unified baseline→winner surface diff. Computed UNCONDITIONALLY (not only
|
|
2357
2367
|
* when `autoOnPromote === 'pr'`) so the diff that the gate decided on is
|
|
@@ -3554,9 +3564,8 @@ interface PowerPreflight {
|
|
|
3554
3564
|
* reads). The hosted `/v1/ingest/traces` endpoint receives the FULL loop,
|
|
3555
3565
|
* not just the `cost.*` spans `runCampaign` already emits per cell.
|
|
3556
3566
|
*
|
|
3557
|
-
* The record is built from the
|
|
3558
|
-
*
|
|
3559
|
-
* could drift from what the gate actually saw.
|
|
3567
|
+
* The record is built from the loop result and its settled cost receipts — no
|
|
3568
|
+
* second usage collector can contradict what the measured cells recorded.
|
|
3560
3569
|
*/
|
|
3561
3570
|
|
|
3562
3571
|
interface LoopProvenanceCandidate {
|
|
@@ -3566,6 +3575,8 @@ interface LoopProvenanceCandidate {
|
|
|
3566
3575
|
surfaceHash: string;
|
|
3567
3576
|
/** Full sha256 content hash — byte-identical-verifiable. */
|
|
3568
3577
|
contentHash: string;
|
|
3578
|
+
/** Exact scored rows that produced this candidate's search result. */
|
|
3579
|
+
campaignDigest: `sha256:${string}`;
|
|
3569
3580
|
/** Proposer label, when the proposer returned a `ProposedCandidate`. */
|
|
3570
3581
|
label?: string;
|
|
3571
3582
|
/** Proposer rationale — the "because Z". When the proposer returned a bare
|
|
@@ -3599,13 +3610,33 @@ interface LoopProvenanceBackend {
|
|
|
3599
3610
|
totalOutputTokens: number;
|
|
3600
3611
|
totalCostUsd: number;
|
|
3601
3612
|
}
|
|
3613
|
+
interface LoopProvenanceEvidence {
|
|
3614
|
+
search: {
|
|
3615
|
+
splitDigest: `sha256:${string}`;
|
|
3616
|
+
baselineCampaignDigest: `sha256:${string}`;
|
|
3617
|
+
};
|
|
3618
|
+
holdout: {
|
|
3619
|
+
splitDigest: `sha256:${string}`;
|
|
3620
|
+
baselineCampaignDigest: `sha256:${string}`;
|
|
3621
|
+
winnerCampaignDigest: `sha256:${string}`;
|
|
3622
|
+
neutralized?: {
|
|
3623
|
+
contentHash: `sha256:${string}`;
|
|
3624
|
+
campaignDigest: `sha256:${string}`;
|
|
3625
|
+
composite: number;
|
|
3626
|
+
lift: number;
|
|
3627
|
+
};
|
|
3628
|
+
};
|
|
3629
|
+
costReceiptsDigest: `sha256:${string}`;
|
|
3630
|
+
}
|
|
3602
3631
|
/**
|
|
3603
3632
|
* The durable provenance record. Aligns to the hosted `EvalRunEvent` path but
|
|
3604
3633
|
* ADDS the rationale + the explicit baseline→candidate diff (both omitted from
|
|
3605
3634
|
* the bare hosted event) + backend provenance.
|
|
3606
3635
|
*/
|
|
3607
3636
|
interface LoopProvenanceRecord {
|
|
3608
|
-
schema: 'tangle.loop-provenance
|
|
3637
|
+
schema: 'tangle.loop-provenance';
|
|
3638
|
+
/** SHA-256 over the canonical record with this field omitted. */
|
|
3639
|
+
recordDigest: `sha256:${string}`;
|
|
3609
3640
|
runId: string;
|
|
3610
3641
|
runDir: string;
|
|
3611
3642
|
timestamp: string;
|
|
@@ -3619,6 +3650,8 @@ interface LoopProvenanceRecord {
|
|
|
3619
3650
|
diff: string;
|
|
3620
3651
|
/** Every candidate across every generation, with its rationale and structured cause. */
|
|
3621
3652
|
candidates: LoopProvenanceCandidate[];
|
|
3653
|
+
/** Exact campaign, split, surface, and receipt identities behind every summary. */
|
|
3654
|
+
evidence: LoopProvenanceEvidence;
|
|
3622
3655
|
/** Baseline composite on the search split that generated the candidates. */
|
|
3623
3656
|
baselineSearchComposite: number;
|
|
3624
3657
|
/** The gate verdict — decision + reasons + contributing gates + delta. */
|
|
@@ -3629,6 +3662,7 @@ interface LoopProvenanceRecord {
|
|
|
3629
3662
|
contributingGates: Array<{
|
|
3630
3663
|
name: string;
|
|
3631
3664
|
passed: boolean;
|
|
3665
|
+
detail: unknown;
|
|
3632
3666
|
}>;
|
|
3633
3667
|
};
|
|
3634
3668
|
/** baseline-on-holdout composite mean. */
|
|
@@ -3781,17 +3815,6 @@ interface SelfImproveOptions<TScenario extends Scenario, TArtifact> {
|
|
|
3781
3815
|
* Default `mem://selfImprove-<timestamp>` (in-memory, non-durable). Pass a
|
|
3782
3816
|
* real path to persist the provenance record + spans. */
|
|
3783
3817
|
runDir?: string;
|
|
3784
|
-
/**
|
|
3785
|
-
* Worker call records for backend provenance. The agent is opaque to the
|
|
3786
|
-
* substrate (it returns an artifact, not token usage), so to capture an
|
|
3787
|
-
* `assertRealBackend`-grade verdict + worker call count + model in the
|
|
3788
|
-
* provenance record, the agent reports its per-call `RunRecord`s here.
|
|
3789
|
-
* Called once after the loop; return the records the agent accumulated.
|
|
3790
|
-
* When unset, backend provenance is derived from campaign cells (cost only;
|
|
3791
|
-
* verdict will read `stub` without token usage — the honest signal that no
|
|
3792
|
-
* token channel was wired).
|
|
3793
|
-
*/
|
|
3794
|
-
collectWorkerRecords?: () => RunRecord[];
|
|
3795
3818
|
/** Fires once the durable provenance record + OTel spans are emitted.
|
|
3796
3819
|
* Receives the structured record for inline assertions / custom routing. */
|
|
3797
3820
|
onProvenance?: (record: LoopProvenanceRecord) => void;
|
|
@@ -4000,6 +4023,19 @@ interface DefinedAgentEval<TScenario extends Scenario, TArtifact> {
|
|
|
4000
4023
|
*/
|
|
4001
4024
|
declare function defineAgentEval<TScenario extends Scenario, TArtifact>(defaults: DefineAgentEvalOptions<TScenario, TArtifact>): DefinedAgentEval<TScenario, TArtifact>;
|
|
4002
4025
|
|
|
4026
|
+
interface MeasuredComparisonFromSelfImproveResultOptions<TScenario extends Scenario, TArtifact> {
|
|
4027
|
+
result: SelfImproveResult<TScenario, TArtifact>;
|
|
4028
|
+
benchmark: AgentImprovementMeasuredComparison['benchmark'];
|
|
4029
|
+
/** Canonical digest verified by the runtime that owns the baseline profile. */
|
|
4030
|
+
baselineProfileDigest: Sha256Digest;
|
|
4031
|
+
/** Canonical digest verified by the runtime that owns candidate sealing. */
|
|
4032
|
+
candidateBundleDigest: Sha256Digest;
|
|
4033
|
+
/** Exact baseline surface; contradictory recorded provenance is rejected. */
|
|
4034
|
+
baselineSurface: MutableSurface;
|
|
4035
|
+
}
|
|
4036
|
+
/** Convert one paired self-improvement result into the portable Interface evidence record. */
|
|
4037
|
+
declare function measuredComparisonFromSelfImproveResult<TScenario extends Scenario, TArtifact>(options: MeasuredComparisonFromSelfImproveResultOptions<TScenario, TArtifact>): AgentImprovementMeasuredComparison;
|
|
4038
|
+
|
|
4003
4039
|
/**
|
|
4004
4040
|
* Typed Ax output for analyst findings.
|
|
4005
4041
|
*
|
|
@@ -4065,8 +4101,8 @@ type CanonicalRawAnalystFinding = z.infer<typeof CanonicalRawAnalystFindingSchem
|
|
|
4065
4101
|
* Analyst-kind factory — the typed way to define trace analysts.
|
|
4066
4102
|
*
|
|
4067
4103
|
* A "kind" is a specialized analyst whose actor prompt, tool subset,
|
|
4068
|
-
* and Ax
|
|
4069
|
-
* classification, knowledge gap discovery, knowledge poisoning,
|
|
4104
|
+
* and bounded Ax subqueries target one failure-mode lens (failure-mode
|
|
4105
|
+
* classification, knowledge gap discovery, knowledge poisoning,
|
|
4070
4106
|
* self-improvement, ...). Kinds emit findings in the typed
|
|
4071
4107
|
* `CanonicalRawAnalystFinding` shape via a JSON-array Ax output; the factory
|
|
4072
4108
|
* validates each row with Zod and lifts it into `AnalystFinding[]`.
|
|
@@ -4077,12 +4113,11 @@ type CanonicalRawAnalystFinding = z.infer<typeof CanonicalRawAnalystFindingSchem
|
|
|
4077
4113
|
* - Each kind picks a narrow tool subset from `ANALYST_TOOL_GROUPS`.
|
|
4078
4114
|
* A kind that never needs full-trace dumps can drop `viewTrace` /
|
|
4079
4115
|
* `viewSpans` and stay cheap.
|
|
4080
|
-
* - Each kind declares its
|
|
4081
|
-
*
|
|
4082
|
-
* (poisoning) usually stay at 0 since they have a tighter brief.
|
|
4116
|
+
* - Each kind declares its subquery + parallelism budget. Discovery-heavy
|
|
4117
|
+
* kinds can fan out more bounded semantic questions than narrow lenses.
|
|
4083
4118
|
*
|
|
4084
4119
|
* Optimizer hook: kinds may declare `goldens` — labeled examples used
|
|
4085
|
-
* by `
|
|
4120
|
+
* by `AxBootstrapFewShot` / `AxGEPA` to fit the actor
|
|
4086
4121
|
* description programmatically. Stored on the kind, not the registry,
|
|
4087
4122
|
* because the right metric is kind-specific.
|
|
4088
4123
|
*/
|
|
@@ -4102,20 +4137,18 @@ interface TraceAnalystKindSpec {
|
|
|
4102
4137
|
version: string;
|
|
4103
4138
|
/** Actor system prompt. Must instruct the LLM to emit `findings` per the schema. */
|
|
4104
4139
|
actorDescription: string;
|
|
4105
|
-
/** Responder system prompt; falls back to a minimal "format the findings" instruction. */
|
|
4106
|
-
responderDescription?: string;
|
|
4107
4140
|
/** Tool functions the actor may call. Pick narrow subsets via `ANALYST_TOOL_GROUPS`. */
|
|
4108
4141
|
buildTools: (store: TraceAnalysisStore) => AxFunction[];
|
|
4109
|
-
/**
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4142
|
+
/** Bounded semantic subqueries. `maxCalls: 0` disables model fan-out. */
|
|
4143
|
+
subqueries?: {
|
|
4144
|
+
maxCalls: number;
|
|
4145
|
+
maxParallel?: number;
|
|
4113
4146
|
};
|
|
4114
4147
|
/** Actor turn cap. Default 12. */
|
|
4115
4148
|
maxTurns?: number;
|
|
4116
4149
|
/** Runtime char cap. Default 6000. */
|
|
4117
4150
|
maxRuntimeChars?: number;
|
|
4118
|
-
/** Maximum output tokens for every actor
|
|
4151
|
+
/** Maximum output tokens for every actor and subquery model call. Default 4096. */
|
|
4119
4152
|
maxOutputTokens?: number;
|
|
4120
4153
|
/** Cost classification surfaced in `registry.list()` and budget enforcement. */
|
|
4121
4154
|
cost: AnalystCost;
|
|
@@ -4219,7 +4252,7 @@ interface RegistryRunOpts {
|
|
|
4219
4252
|
skip?: string[];
|
|
4220
4253
|
/** Budget policy — totalUsd + optional weights/allocator. Falls back to options.defaultBudget. */
|
|
4221
4254
|
budget?: BudgetPolicy;
|
|
4222
|
-
/**
|
|
4255
|
+
/** Active-work cap for the complete registry run. Model receipt settlement may follow. */
|
|
4223
4256
|
timeoutMs?: number;
|
|
4224
4257
|
/** Abort signal — forwarded into every analyst's context. */
|
|
4225
4258
|
signal?: AbortSignal;
|
|
@@ -4933,4 +4966,4 @@ interface FromOtelSpansOptions {
|
|
|
4933
4966
|
}
|
|
4934
4967
|
declare function fromOtelSpans(opts: FromOtelSpansOptions): RunRecord[];
|
|
4935
4968
|
|
|
4936
|
-
export { type AgentEvalAgent, type AgentEvalEvaluateOptions, type AgentEvalImproveOptions, type AgentTraceContributor, type AgentTraceContributorType, type AgentTraceConversation, type AgentTraceFile, type AgentTraceIndex, type AgentTraceRange, type AgentTraceRecord, type AnalystFinding, type AnalyzeRunsOptions, type AuthoringProvenance, type AxisEvidence, type AxisVerdict, type BuildEvidenceVectorOptions, type CampaignAggregates, type CampaignArtifactWriter, type CampaignCellResult, type CampaignCostMeter, type CampaignResult, type CampaignStorage, type CampaignTraceWriter, type ChatClient, type CodeAgentSessionDiagnostic, type CodeAgentSessionIntakeOptions, type CodeAgentSessionIntakeResult, type CodeAgentSessionMetrics, type CodeAgentSessionSource, type CodeSurface, type CostLedgerHandle, type CostProvenanceSummary, type CreateChatClientOpts, type DefaultAnalystRegistryOptions, type DefaultProductionGateOptions, type DefineAgentEvalOptions, type DefinedAgentEval, type DeploymentOutcome, type DispatchFn as Dispatch, type DispatchContext, type EvalCellScoreDelta, type EvalDimensionDelta, type EvalGenerationDiff, type EvalReportingSuiteInput, type EvalReportingSuiteOptions, type EvalReportingSuiteResult, type EvalRunDiff, type EvidenceVector, type EvolutionaryProposerOptions, type ExecutionInsight, type ExecutionReport, type FailureClusterInsight, type FeedbackTableMeta, type FeedbackTableRow, FileSystemOutcomeStore, type FileSystemOutcomeStoreOptions, type FromFeedbackTableOptions, type FromFeedbackTableResult, type FromOtelSpansOptions, type FromRunRecordDirOptions, type FromRunRecordDirResult, type Gate, type GateContext, type GateDecision, type GateResult, type GenerationCandidate, type GenerationRecord, type GepaProposerOptions, type HeldOutGateOptions, type HostedTenant, InMemoryOutcomeStore, type InsightReport, type InterRaterInsight, type JudgeConfig, type JudgeDimension, type JudgeInsight, type JudgeScore, type LiftInsight, type MutableSurface, type Mutator, type ObjectiveSource, type OptimizationProposer, type OptimizerConfig, type OutcomeCorrelationInsight, type OutcomeStore, type ParetoSignificanceGateOptions, type ParsedCodeAgentJsonl, type PartitionByAuthoringModelResult, type PromotionObjective, type PromotionPolicy, REFERENCE_EQUIVALENCE_INPUT_LIMITS, REFERENCE_EQUIVALENCE_JUDGE_VERSION, type Recommendation, type ReferenceEquivalenceJudgeInput, type ReferenceEquivalenceJudgeOptions, type ReferenceEquivalenceJudgeResult, type ReferenceEquivalenceScenario, type ReleaseSummary, type RunCampaignOptions, type RunEvalOptions, type RunImprovementLoopOptions, type RunImprovementLoopResult, type RunRecordRejection, type ScalarDistribution, type Scenario, type SelfImproveBudget, type SelfImproveLlm, type SelfImproveOptions, type SelfImproveProgressEvent, type SelfImproveResult, SelfImproveRunError, type SessionScript, type SummarizeExecutionOptions, type SurfaceProposer, type TokenUsageInsight, analyzeRuns, buildDefaultAnalystRegistry, buildEvidenceVector, composeGate, createChatClient, createReferenceEquivalenceJudge, defaultProductionGate, defineAgentEval, diffGenerations, diffRunBaselineToWinner, diffRuns, evalReportingSuite, evolutionaryProposer, fromClaudeCodeSession, fromCodexSession, fromFeedbackTable, fromKimiCodeSession, fromOpenCodeSession, fromOtelSpans, fromPiSession, fromPigraphSession, fromRunRecordDir, fsCampaignStorage, gepaProposer, heldOutGate, inMemoryCampaignStorage, paretoPolicy, paretoSignificanceGate, parseAgentTrace, parseCodeAgentJsonl, partitionRunsByAuthoringModel, runCampaign, runEval, runImprovementLoop, runReferenceEquivalenceJudge, selfImprove, summarizeExecution };
|
|
4969
|
+
export { type AgentEvalAgent, type AgentEvalEvaluateOptions, type AgentEvalImproveOptions, type AgentTraceContributor, type AgentTraceContributorType, type AgentTraceConversation, type AgentTraceFile, type AgentTraceIndex, type AgentTraceRange, type AgentTraceRecord, type AnalystFinding, type AnalyzeRunsOptions, type AuthoringProvenance, type AxisEvidence, type AxisVerdict, type BuildEvidenceVectorOptions, type CampaignAggregates, type CampaignArtifactWriter, type CampaignCellResult, type CampaignCostMeter, type CampaignResult, type CampaignStorage, type CampaignTraceWriter, type ChatClient, type CodeAgentSessionDiagnostic, type CodeAgentSessionIntakeOptions, type CodeAgentSessionIntakeResult, type CodeAgentSessionMetrics, type CodeAgentSessionSource, type CodeSurface, type CostLedgerHandle, type CostProvenanceSummary, type CreateChatClientOpts, type DefaultAnalystRegistryOptions, type DefaultProductionGateOptions, type DefineAgentEvalOptions, type DefinedAgentEval, type DeploymentOutcome, type DispatchFn as Dispatch, type DispatchContext, type EvalCellScoreDelta, type EvalDimensionDelta, type EvalGenerationDiff, type EvalReportingSuiteInput, type EvalReportingSuiteOptions, type EvalReportingSuiteResult, type EvalRunDiff, type EvidenceVector, type EvolutionaryProposerOptions, type ExecutionInsight, type ExecutionReport, type FailureClusterInsight, type FeedbackTableMeta, type FeedbackTableRow, FileSystemOutcomeStore, type FileSystemOutcomeStoreOptions, type FromFeedbackTableOptions, type FromFeedbackTableResult, type FromOtelSpansOptions, type FromRunRecordDirOptions, type FromRunRecordDirResult, type Gate, type GateContext, type GateDecision, type GateResult, type GenerationCandidate, type GenerationRecord, type GepaProposerOptions, type HeldOutGateOptions, type HostedTenant, InMemoryOutcomeStore, type InsightReport, type InterRaterInsight, type JudgeConfig, type JudgeDimension, type JudgeInsight, type JudgeScore, type LiftInsight, type MeasuredComparisonFromSelfImproveResultOptions, type MutableSurface, type Mutator, type ObjectiveSource, type OptimizationProposer, type OptimizerConfig, type OutcomeCorrelationInsight, type OutcomeStore, type ParetoSignificanceGateOptions, type ParsedCodeAgentJsonl, type PartitionByAuthoringModelResult, type PromotionObjective, type PromotionPolicy, REFERENCE_EQUIVALENCE_INPUT_LIMITS, REFERENCE_EQUIVALENCE_JUDGE_VERSION, type Recommendation, type ReferenceEquivalenceJudgeInput, type ReferenceEquivalenceJudgeOptions, type ReferenceEquivalenceJudgeResult, type ReferenceEquivalenceScenario, type ReleaseSummary, type RunCampaignOptions, type RunEvalOptions, type RunImprovementLoopOptions, type RunImprovementLoopResult, type RunRecordRejection, type ScalarDistribution, type Scenario, type SelfImproveBudget, type SelfImproveLlm, type SelfImproveOptions, type SelfImproveProgressEvent, type SelfImproveResult, SelfImproveRunError, type SessionScript, type SummarizeExecutionOptions, type SurfaceProposer, type TokenUsageInsight, analyzeRuns, buildDefaultAnalystRegistry, buildEvidenceVector, campaignSplitDigest, composeGate, createChatClient, createReferenceEquivalenceJudge, defaultProductionGate, defineAgentEval, diffGenerations, diffRunBaselineToWinner, diffRuns, evalReportingSuite, evolutionaryProposer, fromClaudeCodeSession, fromCodexSession, fromFeedbackTable, fromKimiCodeSession, fromOpenCodeSession, fromOtelSpans, fromPiSession, fromPigraphSession, fromRunRecordDir, fsCampaignStorage, gepaProposer, heldOutGate, inMemoryCampaignStorage, measuredComparisonFromSelfImproveResult, paretoPolicy, paretoSignificanceGate, parseAgentTrace, parseCodeAgentJsonl, partitionRunsByAuthoringModel, runCampaign, runEval, runImprovementLoop, runReferenceEquivalenceJudge, selfImprove, summarizeExecution };
|