@tangle-network/agent-eval 0.120.2 → 0.120.4

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.
Files changed (172) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/analyst/index.d.ts +3111 -0
  3. package/dist/analyst/index.js +403 -0
  4. package/dist/analyst/index.js.map +1 -0
  5. package/dist/authenticity/index.d.ts +161 -0
  6. package/dist/authenticity/index.js +215 -0
  7. package/dist/authenticity/index.js.map +1 -0
  8. package/dist/belief-state/index.d.ts +1301 -0
  9. package/dist/belief-state/index.js +2152 -0
  10. package/dist/belief-state/index.js.map +1 -0
  11. package/dist/benchmarks/index.d.ts +974 -0
  12. package/dist/benchmarks/index.js +60 -0
  13. package/dist/benchmarks/index.js.map +1 -0
  14. package/dist/builder-eval/index.d.ts +695 -0
  15. package/dist/builder-eval/index.js +366 -0
  16. package/dist/builder-eval/index.js.map +1 -0
  17. package/dist/campaign/index.d.ts +7469 -0
  18. package/dist/campaign/index.js +272 -0
  19. package/dist/campaign/index.js.map +1 -0
  20. package/dist/chunk-3RF76KTD.js +84 -0
  21. package/dist/chunk-3RF76KTD.js.map +1 -0
  22. package/dist/chunk-3XH4Y2SS.js +750 -0
  23. package/dist/chunk-3XH4Y2SS.js.map +1 -0
  24. package/dist/chunk-3YYRZDON.js +45 -0
  25. package/dist/chunk-3YYRZDON.js.map +1 -0
  26. package/dist/chunk-5BYTIDZ7.js +550 -0
  27. package/dist/chunk-5BYTIDZ7.js.map +1 -0
  28. package/dist/chunk-5CVUPHJ4.js +2668 -0
  29. package/dist/chunk-5CVUPHJ4.js.map +1 -0
  30. package/dist/chunk-7QBFOJ3E.js +3920 -0
  31. package/dist/chunk-7QBFOJ3E.js.map +1 -0
  32. package/dist/chunk-ARU2PZFM.js +312 -0
  33. package/dist/chunk-ARU2PZFM.js.map +1 -0
  34. package/dist/chunk-BOD4O7OF.js +40 -0
  35. package/dist/chunk-BOD4O7OF.js.map +1 -0
  36. package/dist/chunk-CVJP5TMD.js +766 -0
  37. package/dist/chunk-CVJP5TMD.js.map +1 -0
  38. package/dist/chunk-DPZAEKA6.js +880 -0
  39. package/dist/chunk-DPZAEKA6.js.map +1 -0
  40. package/dist/chunk-DTJ6QUQB.js +131 -0
  41. package/dist/chunk-DTJ6QUQB.js.map +1 -0
  42. package/dist/chunk-GGE4NNQT.js +65 -0
  43. package/dist/chunk-GGE4NNQT.js.map +1 -0
  44. package/dist/chunk-H5UD2323.js +286 -0
  45. package/dist/chunk-H5UD2323.js.map +1 -0
  46. package/dist/chunk-HHWE3POT.js +94 -0
  47. package/dist/chunk-HHWE3POT.js.map +1 -0
  48. package/dist/chunk-HKUCJ437.js +787 -0
  49. package/dist/chunk-HKUCJ437.js.map +1 -0
  50. package/dist/chunk-JHCHEVET.js +274 -0
  51. package/dist/chunk-JHCHEVET.js.map +1 -0
  52. package/dist/chunk-K4DBDHLK.js +158 -0
  53. package/dist/chunk-K4DBDHLK.js.map +1 -0
  54. package/dist/chunk-K6N6XJJX.js +306 -0
  55. package/dist/chunk-K6N6XJJX.js.map +1 -0
  56. package/dist/chunk-MA6HLL3S.js +65 -0
  57. package/dist/chunk-MA6HLL3S.js.map +1 -0
  58. package/dist/chunk-MAZ26DC7.js +99 -0
  59. package/dist/chunk-MAZ26DC7.js.map +1 -0
  60. package/dist/chunk-MOXWMGPC.js +577 -0
  61. package/dist/chunk-MOXWMGPC.js.map +1 -0
  62. package/dist/chunk-NJC7U437.js +626 -0
  63. package/dist/chunk-NJC7U437.js.map +1 -0
  64. package/dist/chunk-NMN4WGSJ.js +1030 -0
  65. package/dist/chunk-NMN4WGSJ.js.map +1 -0
  66. package/dist/chunk-NPCTHQIO.js +91 -0
  67. package/dist/chunk-NPCTHQIO.js.map +1 -0
  68. package/dist/chunk-ONWEPEDO.js +57 -0
  69. package/dist/chunk-ONWEPEDO.js.map +1 -0
  70. package/dist/chunk-OYZAPX5G.js +1526 -0
  71. package/dist/chunk-OYZAPX5G.js.map +1 -0
  72. package/dist/chunk-PC4UYEBM.js +166 -0
  73. package/dist/chunk-PC4UYEBM.js.map +1 -0
  74. package/dist/chunk-PJQFMIOX.js +1182 -0
  75. package/dist/chunk-PJQFMIOX.js.map +1 -0
  76. package/dist/chunk-PXD6ZFNY.js +1107 -0
  77. package/dist/chunk-PXD6ZFNY.js.map +1 -0
  78. package/dist/chunk-PXE2VKMX.js +140 -0
  79. package/dist/chunk-PXE2VKMX.js.map +1 -0
  80. package/dist/chunk-PZ5AY32C.js +10 -0
  81. package/dist/chunk-PZ5AY32C.js.map +1 -0
  82. package/dist/chunk-QBRSJK47.js +622 -0
  83. package/dist/chunk-QBRSJK47.js.map +1 -0
  84. package/dist/chunk-S3UZOQ5Y.js +328 -0
  85. package/dist/chunk-S3UZOQ5Y.js.map +1 -0
  86. package/dist/chunk-SQQED7ZH.js +998 -0
  87. package/dist/chunk-SQQED7ZH.js.map +1 -0
  88. package/dist/chunk-SYV364BL.js +1266 -0
  89. package/dist/chunk-SYV364BL.js.map +1 -0
  90. package/dist/chunk-T4SQEITX.js +95 -0
  91. package/dist/chunk-T4SQEITX.js.map +1 -0
  92. package/dist/chunk-TT4KNT67.js +124 -0
  93. package/dist/chunk-TT4KNT67.js.map +1 -0
  94. package/dist/chunk-U5CHZ5M3.js +357 -0
  95. package/dist/chunk-U5CHZ5M3.js.map +1 -0
  96. package/dist/chunk-ULOKLHIQ.js +1937 -0
  97. package/dist/chunk-ULOKLHIQ.js.map +1 -0
  98. package/dist/chunk-VI2UW6B6.js +162 -0
  99. package/dist/chunk-VI2UW6B6.js.map +1 -0
  100. package/dist/chunk-VQMK5FMP.js +247 -0
  101. package/dist/chunk-VQMK5FMP.js.map +1 -0
  102. package/dist/chunk-VSMTAMNK.js +53 -0
  103. package/dist/chunk-VSMTAMNK.js.map +1 -0
  104. package/dist/chunk-VZSRQ272.js +149 -0
  105. package/dist/chunk-VZSRQ272.js.map +1 -0
  106. package/dist/chunk-WW2A73HW.js +159 -0
  107. package/dist/chunk-WW2A73HW.js.map +1 -0
  108. package/dist/chunk-X4UCIOTZ.js +136 -0
  109. package/dist/chunk-X4UCIOTZ.js.map +1 -0
  110. package/dist/chunk-XJYR7XFV.js +317 -0
  111. package/dist/chunk-XJYR7XFV.js.map +1 -0
  112. package/dist/chunk-ZET2UAYW.js +89 -0
  113. package/dist/chunk-ZET2UAYW.js.map +1 -0
  114. package/dist/chunk-ZMXDQ4K7.js +870 -0
  115. package/dist/chunk-ZMXDQ4K7.js.map +1 -0
  116. package/dist/chunk-ZTJPIIVI.js +7958 -0
  117. package/dist/chunk-ZTJPIIVI.js.map +1 -0
  118. package/dist/chunk-ZZUXHH3R.js +99 -0
  119. package/dist/chunk-ZZUXHH3R.js.map +1 -0
  120. package/dist/cli.d.ts +1 -0
  121. package/dist/cli.js +112 -0
  122. package/dist/cli.js.map +1 -0
  123. package/dist/contract/index.d.ts +4987 -0
  124. package/dist/contract/index.js +1654 -0
  125. package/dist/contract/index.js.map +1 -0
  126. package/dist/control.d.ts +1013 -0
  127. package/dist/control.js +34 -0
  128. package/dist/control.js.map +1 -0
  129. package/dist/fuzz.d.ts +759 -0
  130. package/dist/fuzz.js +714 -0
  131. package/dist/fuzz.js.map +1 -0
  132. package/dist/hosted/index.d.ts +730 -0
  133. package/dist/hosted/index.js +14 -0
  134. package/dist/hosted/index.js.map +1 -0
  135. package/dist/index.d.ts +16780 -0
  136. package/dist/index.js +12168 -0
  137. package/dist/index.js.map +1 -0
  138. package/dist/matrix/index.d.ts +155 -0
  139. package/dist/matrix/index.js +8 -0
  140. package/dist/matrix/index.js.map +1 -0
  141. package/dist/meta-eval/index.d.ts +1030 -0
  142. package/dist/meta-eval/index.js +417 -0
  143. package/dist/meta-eval/index.js.map +1 -0
  144. package/dist/multishot/index.d.ts +579 -0
  145. package/dist/multishot/index.js +589 -0
  146. package/dist/multishot/index.js.map +1 -0
  147. package/dist/openapi.json +992 -0
  148. package/dist/pipelines/index.d.ts +567 -0
  149. package/dist/pipelines/index.js +515 -0
  150. package/dist/pipelines/index.js.map +1 -0
  151. package/dist/reporting.d.ts +1277 -0
  152. package/dist/reporting.js +48 -0
  153. package/dist/reporting.js.map +1 -0
  154. package/dist/rl.d.ts +4092 -0
  155. package/dist/rl.js +1724 -0
  156. package/dist/rl.js.map +1 -0
  157. package/dist/run-campaign-75RTPVV5.js +14 -0
  158. package/dist/run-campaign-75RTPVV5.js.map +1 -0
  159. package/dist/storyboard/index.d.ts +279 -0
  160. package/dist/storyboard/index.js +767 -0
  161. package/dist/storyboard/index.js.map +1 -0
  162. package/dist/trace-attributes.d.ts +52 -0
  163. package/dist/trace-attributes.js +62 -0
  164. package/dist/trace-attributes.js.map +1 -0
  165. package/dist/traces.d.ts +2343 -0
  166. package/dist/traces.js +249 -0
  167. package/dist/traces.js.map +1 -0
  168. package/dist/wire/index.d.ts +1252 -0
  169. package/dist/wire/index.js +81 -0
  170. package/dist/wire/index.js.map +1 -0
  171. package/docs/eval-surface-map.md +1 -1
  172. package/package.json +1 -1
@@ -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;AACF;AAGO,SAAS,+BACd,UACwB;AACxB,SAAO;AAAA,IACL,SACG,OAAO,CAAC,YAAY,QAAQ,YAAY,OAAO,EAC/C,IAAI,CAAC,aAAa;AAAA,MACjB,aAAa,QAAQ;AAAA,MACrB,cAAc,QAAQ;AAAA,MACtB,SAAS,QAAQ;A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AK,YAAY,GAAG;AAAA,EACzC;AACA,aAAW,CAAC,YAAY,OAAO,KAAK,gBAAgB;AAClD,UAAM,KAAK,UAAU,SAAS,IAAI;AAClC,eAAW,UAAU,IAAI,EAAE,eAAe,GAAG,MAAM,MAAM,GAAG,MAAM,GAAG,GAAG,EAAE;AAAA,EAC5E;AACA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,cAAc,KAAK;AAAA,IACnB,eAAe,MAAM,OAAO,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE;AAAA,IAC7C,cAAc,MAAM,OAAO,CAAC,MAAM,EAAE,OAAO,WAAW,UAAU,CAAC,EAAE;AAAA,IACnE,aAAa,MAAM,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE;AAAA,IAC3C,aAAa,MAAM,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,WAAW,UAAU,CAAC,EAAE;AAAA,EAC/E;AACF;AAKA,SAAS,UAAU,SAAmB,MAA8B;AAClE,QAAM,IAAI,QAAQ;AAClB,MAAI,MAAM,EAAG,QAAO,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,EAAE;AAC5D,QAAM,OAAO,QAAQ,OAAO,CAAC,GAAG,MAAM,IAAI,GAAG,CAAC,IAAI;AAClD,QAAM,WAAW,QAAQ,OAAO,CAAC,GAAG,MAAM,KAAK,IAAI,SAAS,GAAG,CAAC,IAAI,KAAK,IAAI,GAAG,IAAI,CAAC;AACrF,QAAM,QAAQ,KAAK,KAAK,QAAQ;AAChC,QAAM,KAAK,mBAAmB,SAAS,MAAM,EAAE,MAAM,WAAW,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"]}
@@ -0,0 +1,95 @@
1
+ // src/runtime-trajectory.ts
2
+ var DEFAULT_SPLIT_TAG = "search";
3
+ function projectRuntimeTrajectoryEvidence(options) {
4
+ const diagnostics = [];
5
+ const runsById = /* @__PURE__ */ new Map();
6
+ const events = [];
7
+ let recordWithRuntimeEventsCount = 0;
8
+ let defaultedSplitCount = 0;
9
+ for (let recordIndex = 0; recordIndex < options.records.length; recordIndex += 1) {
10
+ const record = options.records[recordIndex];
11
+ const key = runtimeTrajectoryRecordKey(record, recordIndex, options.recordIdOf);
12
+ const splitTag = record.splitTag ?? options.defaultSplitTag ?? DEFAULT_SPLIT_TAG;
13
+ if (record.splitTag === void 0) defaultedSplitCount += 1;
14
+ const rawEvents = record.runtimeEvents;
15
+ if (!Array.isArray(rawEvents)) {
16
+ diagnostics.push(
17
+ `${key}: runtimeEvents is not an array; no runtime run join can be extracted`
18
+ );
19
+ continue;
20
+ }
21
+ if (rawEvents.length === 0) {
22
+ diagnostics.push(`${key}: no runtimeEvents; no runtime run join can be extracted`);
23
+ continue;
24
+ }
25
+ recordWithRuntimeEventsCount += 1;
26
+ for (let index = 0; index < rawEvents.length; index += 1) {
27
+ const event = parseRuntimeTrajectoryHookEvent(rawEvents[index]);
28
+ if (!event) {
29
+ diagnostics.push(`${key}: runtimeEvents[${index}] is not a RuntimeHookEvent`);
30
+ continue;
31
+ }
32
+ events.push(event);
33
+ const scenarioId = event.scenarioId ?? stringOrUndefined(options.scenarioIdOf?.(record, recordIndex)) ?? stringOrUndefined(record.scenarioId);
34
+ const prior = runsById.get(event.runId);
35
+ if (!prior) {
36
+ runsById.set(event.runId, { runId: event.runId, scenarioId, splitTag });
37
+ continue;
38
+ }
39
+ if (prior.scenarioId !== scenarioId || prior.splitTag !== splitTag) {
40
+ diagnostics.push(`${key}: runId ${event.runId} has conflicting scenario/split metadata`);
41
+ }
42
+ }
43
+ }
44
+ const runs = [...runsById.values()];
45
+ return {
46
+ runs,
47
+ events,
48
+ summary: {
49
+ recordCount: options.records.length,
50
+ recordWithRuntimeEventsCount,
51
+ runtimeRunCount: runs.length,
52
+ lifecycleEventCount: events.length,
53
+ defaultedSplitCount
54
+ },
55
+ diagnostics
56
+ };
57
+ }
58
+ function parseRuntimeTrajectoryHookEvent(input) {
59
+ if (!isRecord(input)) return null;
60
+ if (typeof input.id !== "string" || input.id.length === 0) return null;
61
+ if (typeof input.runId !== "string" || input.runId.length === 0) return null;
62
+ if (typeof input.target !== "string" || input.target.length === 0) return null;
63
+ if (typeof input.phase !== "string" || input.phase.length === 0) return null;
64
+ if (typeof input.timestamp !== "number" || !Number.isFinite(input.timestamp)) return null;
65
+ return {
66
+ id: input.id,
67
+ runId: input.runId,
68
+ scenarioId: stringOrUndefined(input.scenarioId),
69
+ target: input.target,
70
+ phase: input.phase,
71
+ timestamp: input.timestamp,
72
+ stepIndex: finiteNumberOrUndefined(input.stepIndex),
73
+ parentId: stringOrUndefined(input.parentId),
74
+ payload: input.payload,
75
+ metadata: isRecord(input.metadata) ? { ...input.metadata } : void 0
76
+ };
77
+ }
78
+ function runtimeTrajectoryRecordKey(record, index, recordIdOf) {
79
+ return stringOrUndefined(recordIdOf?.(record, index)) ?? stringOrUndefined(record.id) ?? `record[${index}]`;
80
+ }
81
+ function isRecord(value) {
82
+ return typeof value === "object" && value !== null && !Array.isArray(value);
83
+ }
84
+ function stringOrUndefined(value) {
85
+ return typeof value === "string" && value.length > 0 ? value : void 0;
86
+ }
87
+ function finiteNumberOrUndefined(value) {
88
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
89
+ }
90
+
91
+ export {
92
+ projectRuntimeTrajectoryEvidence,
93
+ parseRuntimeTrajectoryHookEvent
94
+ };
95
+ //# sourceMappingURL=chunk-T4SQEITX.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/runtime-trajectory.ts"],"sourcesContent":["import type { RunSplitTag } from './run-record'\n\nexport interface RuntimeTrajectoryHookEvent {\n id: string\n runId: string\n scenarioId?: string\n target: string\n phase: string\n timestamp: number\n stepIndex?: number\n parentId?: string\n payload?: unknown\n metadata?: Record<string, unknown>\n}\n\nexport interface RuntimeTrajectoryRecord {\n id?: string\n scenarioId?: string\n splitTag?: RunSplitTag\n runtimeEvents?: unknown\n [key: string]: unknown\n}\n\nexport interface RuntimeTrajectoryRunRecord {\n runId: string\n scenarioId?: string\n splitTag: RunSplitTag\n}\n\nexport interface RuntimeTrajectoryEvidenceSummary {\n recordCount: number\n recordWithRuntimeEventsCount: number\n runtimeRunCount: number\n lifecycleEventCount: number\n defaultedSplitCount: number\n}\n\nexport interface RuntimeTrajectoryEvidenceProjection {\n runs: RuntimeTrajectoryRunRecord[]\n events: RuntimeTrajectoryHookEvent[]\n summary: RuntimeTrajectoryEvidenceSummary\n diagnostics: string[]\n}\n\nexport interface ProjectRuntimeTrajectoryEvidenceOptions<\n TRecord extends RuntimeTrajectoryRecord = RuntimeTrajectoryRecord,\n> {\n records: TRecord[]\n defaultSplitTag?: RunSplitTag\n recordIdOf?: (record: TRecord, index: number) => string | undefined\n scenarioIdOf?: (record: TRecord, index: number) => string | undefined\n}\n\nconst DEFAULT_SPLIT_TAG: RunSplitTag = 'search'\n\nexport function projectRuntimeTrajectoryEvidence<TRecord extends RuntimeTrajectoryRecord>(\n options: ProjectRuntimeTrajectoryEvidenceOptions<TRecord>,\n): RuntimeTrajectoryEvidenceProjection {\n const diagnostics: string[] = []\n const runsById = new Map<string, RuntimeTrajectoryRunRecord>()\n const events: RuntimeTrajectoryHookEvent[] = []\n let recordWithRuntimeEventsCount = 0\n let defaultedSplitCount = 0\n\n for (let recordIndex = 0; recordIndex < options.records.length; recordIndex += 1) {\n const record = options.records[recordIndex]!\n const key = runtimeTrajectoryRecordKey(record, recordIndex, options.recordIdOf)\n const splitTag = record.splitTag ?? options.defaultSplitTag ?? DEFAULT_SPLIT_TAG\n if (record.splitTag === undefined) defaultedSplitCount += 1\n\n const rawEvents = record.runtimeEvents\n if (!Array.isArray(rawEvents)) {\n diagnostics.push(\n `${key}: runtimeEvents is not an array; no runtime run join can be extracted`,\n )\n continue\n }\n if (rawEvents.length === 0) {\n diagnostics.push(`${key}: no runtimeEvents; no runtime run join can be extracted`)\n continue\n }\n recordWithRuntimeEventsCount += 1\n\n for (let index = 0; index < rawEvents.length; index += 1) {\n const event = parseRuntimeTrajectoryHookEvent(rawEvents[index])\n if (!event) {\n diagnostics.push(`${key}: runtimeEvents[${index}] is not a RuntimeHookEvent`)\n continue\n }\n events.push(event)\n\n const scenarioId =\n event.scenarioId ??\n stringOrUndefined(options.scenarioIdOf?.(record, recordIndex)) ??\n stringOrUndefined(record.scenarioId)\n const prior = runsById.get(event.runId)\n if (!prior) {\n runsById.set(event.runId, { runId: event.runId, scenarioId, splitTag })\n continue\n }\n if (prior.scenarioId !== scenarioId || prior.splitTag !== splitTag) {\n diagnostics.push(`${key}: runId ${event.runId} has conflicting scenario/split metadata`)\n }\n }\n }\n\n const runs = [...runsById.values()]\n return {\n runs,\n events,\n summary: {\n recordCount: options.records.length,\n recordWithRuntimeEventsCount,\n runtimeRunCount: runs.length,\n lifecycleEventCount: events.length,\n defaultedSplitCount,\n },\n diagnostics,\n }\n}\n\nexport function parseRuntimeTrajectoryHookEvent(input: unknown): RuntimeTrajectoryHookEvent | null {\n if (!isRecord(input)) return null\n if (typeof input.id !== 'string' || input.id.length === 0) return null\n if (typeof input.runId !== 'string' || input.runId.length === 0) return null\n if (typeof input.target !== 'string' || input.target.length === 0) return null\n if (typeof input.phase !== 'string' || input.phase.length === 0) return null\n if (typeof input.timestamp !== 'number' || !Number.isFinite(input.timestamp)) return null\n\n return {\n id: input.id,\n runId: input.runId,\n scenarioId: stringOrUndefined(input.scenarioId),\n target: input.target,\n phase: input.phase,\n timestamp: input.timestamp,\n stepIndex: finiteNumberOrUndefined(input.stepIndex),\n parentId: stringOrUndefined(input.parentId),\n payload: input.payload,\n metadata: isRecord(input.metadata) ? { ...input.metadata } : undefined,\n }\n}\n\nfunction runtimeTrajectoryRecordKey<TRecord extends RuntimeTrajectoryRecord>(\n record: TRecord,\n index: number,\n recordIdOf?: (record: TRecord, index: number) => string | undefined,\n): string {\n return (\n stringOrUndefined(recordIdOf?.(record, index)) ??\n stringOrUndefined(record.id) ??\n `record[${index}]`\n )\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n\nfunction stringOrUndefined(value: unknown): string | undefined {\n return typeof value === 'string' && value.length > 0 ? value : undefined\n}\n\nfunction finiteNumberOrUndefined(value: unknown): number | undefined {\n return typeof value === 'number' && Number.isFinite(value) ? value : undefined\n}\n"],"mappings":";AAqDA,IAAM,oBAAiC;AAEhC,SAAS,iCACd,SACqC;AACrC,QAAM,cAAwB,CAAC;AAC/B,QAAM,WAAW,oBAAI,IAAwC;AAC7D,QAAM,SAAuC,CAAC;AAC9C,MAAI,+BAA+B;AACnC,MAAI,sBAAsB;AAE1B,WAAS,cAAc,GAAG,cAAc,QAAQ,QAAQ,QAAQ,eAAe,GAAG;AAChF,UAAM,SAAS,QAAQ,QAAQ,WAAW;AAC1C,UAAM,MAAM,2BAA2B,QAAQ,aAAa,QAAQ,UAAU;AAC9E,UAAM,WAAW,OAAO,YAAY,QAAQ,mBAAmB;AAC/D,QAAI,OAAO,aAAa,OAAW,wBAAuB;AAE1D,UAAM,YAAY,OAAO;AACzB,QAAI,CAAC,MAAM,QAAQ,SAAS,GAAG;AAC7B,kBAAY;AAAA,QACV,GAAG,GAAG;AAAA,MACR;AACA;AAAA,IACF;AACA,QAAI,UAAU,WAAW,GAAG;AAC1B,kBAAY,KAAK,GAAG,GAAG,0DAA0D;AACjF;AAAA,IACF;AACA,oCAAgC;AAEhC,aAAS,QAAQ,GAAG,QAAQ,UAAU,QAAQ,SAAS,GAAG;AACxD,YAAM,QAAQ,gCAAgC,UAAU,KAAK,CAAC;AAC9D,UAAI,CAAC,OAAO;AACV,oBAAY,KAAK,GAAG,GAAG,mBAAmB,KAAK,6BAA6B;AAC5E;AAAA,MACF;AACA,aAAO,KAAK,KAA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jB,WAAW,wBAAwB,MAAM,SAAS;AAAA,IAClD,UAAU,kBAAkB,MAAM,QAAQ;AAAA,IAC1C,SAAS,MAAM;AAAA,IACf,UAAU,SAAS,MAAM,QAAQ,IAAI,EAAE,GAAG,MAAM,SAAS,IAAI;AAAA,EAC/D;AACF;AAEA,SAAS,2BACP,QACA,OACA,YACQ;AACR,SACE,kBAAkB,aAAa,QAAQ,KAAK,CAAC,KAC7C,kBAAkB,OAAO,EAAE,KAC3B,UAAU,KAAK;AAEnB;AAEA,SAAS,SAAS,OAAkD;AAClE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC5E;AAEA,SAAS,kBAAkB,OAAoC;AAC7D,SAAO,OAAO,UAAU,YAAY,MAAM,SAAS,IAAI,QAAQ;AACjE;AAEA,SAAS,wBAAwB,OAAoC;AACnE,SAAO,OAAO,UAAU,YAAY,OAAO,SAAS,KAAK,IAAI,QAAQ;AACvE;","names":[]}
@@ -0,0 +1,124 @@
1
+ import {
2
+ CaptureIntegrityError
3
+ } from "./chunk-ONWEPEDO.js";
4
+
5
+ // src/trace/integrity.ts
6
+ var RunIntegrityError = class extends CaptureIntegrityError {
7
+ constructor(report) {
8
+ super(
9
+ `Run ${report.runId} failed integrity check: ${report.issues.map((i) => i.code).join(", ")}`
10
+ );
11
+ this.report = report;
12
+ }
13
+ report;
14
+ };
15
+ async function assertRunCaptured(store, runId, expectations = {}) {
16
+ const issues = [];
17
+ const run = await store.getRun(runId);
18
+ if (!run) {
19
+ return {
20
+ ok: false,
21
+ runId,
22
+ llmSpanCount: 0,
23
+ judgeSpanCount: 0,
24
+ toolSpanCount: 0,
25
+ rawProviderEventCount: 0,
26
+ rawSpanCoverage: { covered: 0, total: 0 },
27
+ issues: [{ code: "no_run", message: `Run ${runId} not found in store.` }]
28
+ };
29
+ }
30
+ const spans = await store.spans({ runId });
31
+ const llmSpans = spans.filter((s) => s.kind === "llm");
32
+ const judgeSpans = spans.filter((s) => s.kind === "judge");
33
+ const toolSpans = spans.filter((s) => s.kind === "tool");
34
+ const llmMin = expectations.llmSpansMin ?? 0;
35
+ const judgeMin = expectations.judgeSpansMin ?? 0;
36
+ const toolMin = expectations.toolSpansMin ?? 0;
37
+ if (llmSpans.length < llmMin) {
38
+ issues.push({
39
+ code: "missing_llm_spans",
40
+ message: `Expected \u2265 ${llmMin} LLM spans, found ${llmSpans.length}.`,
41
+ detail: { expected: llmMin, found: llmSpans.length }
42
+ });
43
+ }
44
+ if (judgeSpans.length < judgeMin) {
45
+ issues.push({
46
+ code: "missing_judge_spans",
47
+ message: `Expected \u2265 ${judgeMin} judge spans, found ${judgeSpans.length}.`,
48
+ detail: { expected: judgeMin, found: judgeSpans.length }
49
+ });
50
+ }
51
+ if (toolSpans.length < toolMin) {
52
+ issues.push({
53
+ code: "missing_tool_spans",
54
+ message: `Expected \u2265 ${toolMin} tool spans, found ${toolSpans.length}.`,
55
+ detail: { expected: toolMin, found: toolSpans.length }
56
+ });
57
+ }
58
+ let rawEventCount = 0;
59
+ let coverage = { covered: 0, total: llmSpans.length };
60
+ if (expectations.rawSink) {
61
+ if (!expectations.rawSink.list) {
62
+ issues.push({
63
+ code: "no_raw_sink",
64
+ message: "Provided rawSink does not implement list(); cannot verify capture."
65
+ });
66
+ } else {
67
+ const events = await expectations.rawSink.list({ runId });
68
+ rawEventCount = events.length;
69
+ const rawMin = expectations.rawProviderEventsMin ?? 1;
70
+ if (rawEventCount < rawMin) {
71
+ issues.push({
72
+ code: "missing_raw_events",
73
+ message: `Expected \u2265 ${rawMin} raw provider events, found ${rawEventCount}.`,
74
+ detail: { expected: rawMin, found: rawEventCount }
75
+ });
76
+ }
77
+ if (expectations.requireRawCoverageOfLlmSpans) {
78
+ const requestEventsBySpan = new Set(
79
+ events.filter((e) => e.direction === "request" && e.spanId).map((e) => e.spanId)
80
+ );
81
+ const orphaned = llmSpans.filter((s) => !requestEventsBySpan.has(s.spanId));
82
+ coverage = { covered: llmSpans.length - orphaned.length, total: llmSpans.length };
83
+ if (orphaned.length > 0) {
84
+ issues.push({
85
+ code: "orphan_llm_span",
86
+ message: `${orphaned.length} LLM span(s) have no matching raw provider request event.`,
87
+ detail: { orphanedSpanIds: orphaned.map((s) => s.spanId) }
88
+ });
89
+ }
90
+ }
91
+ }
92
+ } else if (expectations.requireRawCoverageOfLlmSpans || expectations.rawProviderEventsMin) {
93
+ issues.push({
94
+ code: "no_raw_sink",
95
+ message: "Raw coverage required but no rawSink supplied to the integrity check."
96
+ });
97
+ }
98
+ if (expectations.requireOutcome && (run.outcome === void 0 || run.outcome === null)) {
99
+ issues.push({
100
+ code: "missing_outcome",
101
+ message: `Run ${runId} has no outcome recorded.`
102
+ });
103
+ }
104
+ return {
105
+ ok: issues.length === 0,
106
+ runId,
107
+ llmSpanCount: llmSpans.length,
108
+ judgeSpanCount: judgeSpans.length,
109
+ toolSpanCount: toolSpans.length,
110
+ rawProviderEventCount: rawEventCount,
111
+ rawSpanCoverage: coverage,
112
+ issues
113
+ };
114
+ }
115
+ function throwIfRunIncomplete(report) {
116
+ if (!report.ok) throw new RunIntegrityError(report);
117
+ }
118
+
119
+ export {
120
+ RunIntegrityError,
121
+ assertRunCaptured,
122
+ throwIfRunIncomplete
123
+ };
124
+ //# sourceMappingURL=chunk-TT4KNT67.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/trace/integrity.ts"],"sourcesContent":["/**\n * Run-completion integrity check — at end of run, verify the expected event\n * types were actually captured. The point is the launch-review failure mode:\n * a run *appears* successful but the raw provider events were never written,\n * so a downstream reviewer can't reconstruct what happened.\n *\n * Pattern:\n *\n * const report = await assertRunCaptured(store, runId, {\n * llmSpansMin: 1,\n * judgeSpansMin: 1,\n * rawSink: providerSink, // must have ≥ 1 event for this run\n * requireRawCoverageOfLlmSpans: true, // every llm span has matching raw events\n * })\n * if (!report.ok) throwIfRunIncomplete(report) // or mark run failed and continue\n *\n * The function is read-only on the store and returns a structured report;\n * the caller chooses the failure mode (throw, mark run failed, log warning).\n * `throwIfRunIncomplete` is the convenient strict mode.\n */\n\nimport { CaptureIntegrityError } from '../errors'\nimport type { RawProviderSink } from './raw-provider-sink'\nimport type { TraceStore } from './store'\n\nexport interface RunIntegrityExpectations {\n /** Minimum LLM span count. Default 0 (no requirement). */\n llmSpansMin?: number\n /** Minimum judge span count. Default 0. */\n judgeSpansMin?: number\n /** Minimum tool span count. Default 0. */\n toolSpansMin?: number\n /**\n * Raw provider sink to consult for capture verification. When present,\n * the check requires at least one raw event for the run.\n */\n rawSink?: RawProviderSink\n /** Minimum raw provider event count. Default 0; ignored when `rawSink` absent. */\n rawProviderEventsMin?: number\n /**\n * Every LLM span must have at least one matching raw `request` event\n * (matched by spanId). Catches the common bug where the structured span\n * was emitted but the raw HTTP capture was wired to a different sink.\n */\n requireRawCoverageOfLlmSpans?: boolean\n /** Run outcome must be set (not null/undefined). Default false. */\n requireOutcome?: boolean\n}\n\nexport type RunIntegrityIssueCode =\n | 'no_run'\n | 'missing_llm_spans'\n | 'missing_judge_spans'\n | 'missing_tool_spans'\n | 'missing_raw_events'\n | 'no_raw_sink'\n | 'orphan_llm_span'\n | 'missing_outcome'\n\nexport interface RunIntegrityIssue {\n code: RunIntegrityIssueCode\n message: string\n detail?: Record<string, unknown>\n}\n\nexport interface RunIntegrityReport {\n ok: boolean\n runId: string\n llmSpanCount: number\n judgeSpanCount: number\n toolSpanCount: number\n rawProviderEventCount: number\n /**\n * Coverage of LLM spans by raw provider events keyed on spanId.\n * `total` is the number of LLM spans; `covered` is the count with at\n * least one matching `request` raw event.\n */\n rawSpanCoverage: { covered: number; total: number }\n issues: RunIntegrityIssue[]\n}\n\nexport class RunIntegrityError extends CaptureIntegrityError {\n constructor(public readonly report: RunIntegrityReport) {\n super(\n `Run ${report.runId} failed integrity check: ${report.issues.map((i) => i.code).join(', ')}`,\n )\n }\n}\n\nexport async function assertRunCaptured(\n store: TraceStore,\n runId: string,\n expectations: RunIntegrityExpectations = {},\n): Promise<RunIntegrityReport> {\n const issues: RunIntegrityIssue[] = []\n const run = await store.getRun(runId)\n if (!run) {\n return {\n ok: false,\n runId,\n llmSpanCount: 0,\n judgeSpanCount: 0,\n toolSpanCount: 0,\n rawProviderEventCount: 0,\n rawSpanCoverage: { covered: 0, total: 0 },\n issues: [{ code: 'no_run', message: `Run ${runId} not found in store.` }],\n }\n }\n\n const spans = await store.spans({ runId })\n const llmSpans = spans.filter((s) => s.kind === 'llm')\n const judgeSpans = spans.filter((s) => s.kind === 'judge')\n const toolSpans = spans.filter((s) => s.kind === 'tool')\n\n const llmMin = expectations.llmSpansMin ?? 0\n const judgeMin = expectations.judgeSpansMin ?? 0\n const toolMin = expectations.toolSpansMin ?? 0\n\n if (llmSpans.length < llmMin) {\n issues.push({\n code: 'missing_llm_spans',\n message: `Expected ≥ ${llmMin} LLM spans, found ${llmSpans.length}.`,\n detail: { expected: llmMin, found: llmSpans.length },\n })\n }\n if (judgeSpans.length < judgeMin) {\n issues.push({\n code: 'missing_judge_spans',\n message: `Expected ≥ ${judgeMin} judge spans, found ${judgeSpans.length}.`,\n detail: { expected: judgeMin, found: judgeSpans.length },\n })\n }\n if (toolSpans.length < toolMin) {\n issues.push({\n code: 'missing_tool_spans',\n message: `Expected ≥ ${toolMin} tool spans, found ${toolSpans.length}.`,\n detail: { expected: toolMin, found: toolSpans.length },\n })\n }\n\n let rawEventCount = 0\n let coverage = { covered: 0, total: llmSpans.length }\n\n if (expectations.rawSink) {\n if (!expectations.rawSink.list) {\n issues.push({\n code: 'no_raw_sink',\n message: 'Provided rawSink does not implement list(); cannot verify capture.',\n })\n } else {\n const events = await expectations.rawSink.list({ runId })\n rawEventCount = events.length\n const rawMin = expectations.rawProviderEventsMin ?? 1\n if (rawEventCount < rawMin) {\n issues.push({\n code: 'missing_raw_events',\n message: `Expected ≥ ${rawMin} raw provider events, found ${rawEventCount}.`,\n detail: { expected: rawMin, found: rawEventCount },\n })\n }\n if (expectations.requireRawCoverageOfLlmSpans) {\n const requestEventsBySpan = new Set(\n events.filter((e) => e.direction === 'request' && e.spanId).map((e) => e.spanId!),\n )\n const orphaned = llmSpans.filter((s) => !requestEventsBySpan.has(s.spanId))\n coverage = { covered: llmSpans.length - orphaned.length, total: llmSpans.length }\n if (orphaned.length > 0) {\n issues.push({\n code: 'orphan_llm_span',\n message: `${orphaned.length} LLM span(s) have no matching raw provider request event.`,\n detail: { orphanedSpanIds: orphaned.map((s) => s.spanId) },\n })\n }\n }\n }\n } else if (expectations.requireRawCoverageOfLlmSpans || expectations.rawProviderEventsMin) {\n issues.push({\n code: 'no_raw_sink',\n message: 'Raw coverage required but no rawSink supplied to the integrity check.',\n })\n }\n\n if (expectations.requireOutcome && (run.outcome === undefined || run.outcome === null)) {\n issues.push({\n code: 'missing_outcome',\n message: `Run ${runId} has no outcome recorded.`,\n })\n }\n\n return {\n ok: issues.length === 0,\n runId,\n llmSpanCount: llmSpans.length,\n judgeSpanCount: judgeSpans.length,\n toolSpanCount: toolSpans.length,\n rawProviderEventCount: rawEventCount,\n rawSpanCoverage: coverage,\n issues,\n }\n}\n\n/** Strict mode: throws `RunIntegrityError` when the report isn't ok. */\nexport function throwIfRunIncomplete(report: RunIntegrityReport): void {\n if (!report.ok) throw new RunIntegrityError(report)\n}\n"],"mappings":";;;;;AAiFO,IAAM,oBAAN,cAAgC,sBAAsB;AAAA,EAC3D,YAA4B,QAA4B;AACtD;AAAA,MACE,OAAO,OAAO,KAAK,4BAA4B,OAAO,OAAO,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,IAAI,CAAC;AAAA,IAC5F;AAH0B;AAAA,EAI5B;AAAA,EAJ4B;AAK9B;AAEA,eAAsB,kBACpB,OACA,OACA,eAAyC,CAAC,GACb;AAC7B,QAAM,SAA8B,CAAC;AACrC,QAAM,MAAM,MAAM,MAAM,OAAO,KAAK;AACpC,MAAI,CAAC,KAAK;AACR,WAAO;AAAA,MACL,IAAI;AAAA,MACJ;AAAA,MACA,cAAc;AAA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