@tangle-network/agent-eval 0.94.0 → 0.95.1

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 (140) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +44 -30
  3. package/dist/adapters/http.d.ts +8 -7
  4. package/dist/adapters/http.js.map +1 -1
  5. package/dist/adapters/langchain.d.ts +3 -2
  6. package/dist/adapters/otel.d.ts +5 -4
  7. package/dist/analyst/index.d.ts +11 -31
  8. package/dist/analyst/index.js +5 -65
  9. package/dist/analyst/index.js.map +1 -1
  10. package/dist/{analyze-runs-B6Ljo_dI.d.ts → analyze-runs-DtT6F_6T.d.ts} +3 -3
  11. package/dist/belief-state/index.d.ts +4 -3
  12. package/dist/benchmarks/index.d.ts +3 -2
  13. package/dist/campaign/index.d.ts +727 -616
  14. package/dist/campaign/index.js +1863 -1316
  15. package/dist/campaign/index.js.map +1 -1
  16. package/dist/{chunk-2K6UUZ7P.js → chunk-2T4EZACH.js} +1 -1
  17. package/dist/chunk-2T4EZACH.js.map +1 -0
  18. package/dist/{chunk-CTBHKLEU.js → chunk-77T4STFI.js} +59 -86
  19. package/dist/chunk-77T4STFI.js.map +1 -0
  20. package/dist/{chunk-EGPMSBEZ.js → chunk-7QTQKIDD.js} +178 -177
  21. package/dist/chunk-7QTQKIDD.js.map +1 -0
  22. package/dist/{chunk-MIFZUPEK.js → chunk-AQ5WQAIV.js} +21 -6
  23. package/dist/chunk-AQ5WQAIV.js.map +1 -0
  24. package/dist/chunk-DJWX3GVS.js +81 -0
  25. package/dist/chunk-DJWX3GVS.js.map +1 -0
  26. package/dist/{chunk-S6OZEZQK.js → chunk-HMA63UEO.js} +37 -9
  27. package/dist/{chunk-S6OZEZQK.js.map → chunk-HMA63UEO.js.map} +1 -1
  28. package/dist/{chunk-TBDR6PAI.js → chunk-IZCEK2HR.js} +2 -2
  29. package/dist/{chunk-SD2YFWQQ.js → chunk-KKWJD5E6.js} +20 -20
  30. package/dist/chunk-KKWJD5E6.js.map +1 -0
  31. package/dist/{chunk-KWRRMR3J.js → chunk-LO6IOIJ2.js} +10 -10
  32. package/dist/chunk-LO6IOIJ2.js.map +1 -0
  33. package/dist/{chunk-E4GH6USR.js → chunk-NZEQVRH5.js} +2 -2
  34. package/dist/chunk-NZEQVRH5.js.map +1 -0
  35. package/dist/{chunk-MPQWFX6Y.js → chunk-PSWWQXHF.js} +13 -88
  36. package/dist/chunk-PSWWQXHF.js.map +1 -0
  37. package/dist/{chunk-Q5LIB7BC.js → chunk-S4SYLDFX.js} +2 -2
  38. package/dist/chunk-S4SYLDFX.js.map +1 -0
  39. package/dist/{chunk-KW53MSA5.js → chunk-X74V6ESX.js} +2 -2
  40. package/dist/{chunk-QMUEXQJS.js → chunk-YBIGNSCZ.js} +81 -4
  41. package/dist/chunk-YBIGNSCZ.js.map +1 -0
  42. package/dist/{chunk-2KNZHH3P.js → chunk-Z6L6YSU6.js} +2 -2
  43. package/dist/{code-agent-session-BO8nCnv3.d.ts → code-agent-session-CPHRCb4-.d.ts} +1 -1
  44. package/dist/contract/index.d.ts +91 -43
  45. package/dist/contract/index.js +127 -17
  46. package/dist/contract/index.js.map +1 -1
  47. package/dist/{control-D6qwHXIR.d.ts → control-Doncu-B_.d.ts} +2 -2
  48. package/dist/control.d.ts +3 -2
  49. package/dist/control.js +2 -2
  50. package/dist/{corpus-B8A4BDR3.d.ts → corpus-D4YW9UoJ.d.ts} +1 -1
  51. package/dist/{default-registry-6dhErQbs.d.ts → default-registry-GyE8X5SP.d.ts} +3 -3
  52. package/dist/diagnose.d.ts +4 -3
  53. package/dist/diagnose.js +1 -1
  54. package/dist/{run-improvement-loop-DBahB8Ax.d.ts → gepa-C1NCIZ9o.d.ts} +117 -130
  55. package/dist/hosted/index.d.ts +5 -4
  56. package/dist/{index-Bx3gZ8xl.d.ts → index-_Y4oNOOb.d.ts} +1 -1
  57. package/dist/index.d.ts +76 -81
  58. package/dist/index.js +66 -31
  59. package/dist/index.js.map +1 -1
  60. package/dist/{insight-report-DWl3z9tl.d.ts → insight-report-BnRjTibG.d.ts} +1 -1
  61. package/dist/{kind-factory-0BhLSI27.d.ts → kind-factory-X3eDYbKn.d.ts} +2 -3
  62. package/dist/matrix/index.d.ts +1 -1
  63. package/dist/meta-eval/index.d.ts +3 -2
  64. package/dist/multishot/index.d.ts +4 -4
  65. package/dist/multishot/index.js.map +1 -1
  66. package/dist/openapi.json +1 -1
  67. package/dist/{pre-registration-mAnCugl9.d.ts → pre-registration-nfUdc9EQ.d.ts} +2 -42
  68. package/dist/{provenance-P-bCL2Fo.d.ts → provenance-CncDq9qE.d.ts} +26 -41
  69. package/dist/{release-report-BEbWmVYj.d.ts → release-report-pidWUMZ2.d.ts} +2 -2
  70. package/dist/reporting.d.ts +5 -4
  71. package/dist/{researcher-B0C2_fVO.d.ts → researcher-Jr8ME1dZ.d.ts} +2 -2
  72. package/dist/rl.d.ts +516 -515
  73. package/dist/rl.js +612 -612
  74. package/dist/rl.js.map +1 -1
  75. package/dist/{rubric-predictive-validity-Cy_W-hWZ.d.ts → rubric-predictive-validity-C2hDKM8Z.d.ts} +1 -1
  76. package/dist/{run-campaign-7WNXMDSN.js → run-campaign-WXY7KI67.js} +2 -2
  77. package/dist/{run-record-e7vj1uZQ.d.ts → run-record-CP2ObebC.d.ts} +14 -18
  78. package/dist/{runtime-trajectory-BDgfGZSr.d.ts → runtime-trajectory-BOUUjI0y.d.ts} +1 -1
  79. package/dist/{semantic-concept-judge-B9MgmBnM.d.ts → semantic-concept-judge-DSBB2Cfp.d.ts} +2 -2
  80. package/dist/{summary-report-BDOFevaT.d.ts → summary-report-CInXwsza.d.ts} +1 -1
  81. package/dist/testing-C21CHsq2.d.ts +20 -0
  82. package/dist/testing.d.ts +1 -0
  83. package/dist/testing.js +8 -0
  84. package/dist/testing.js.map +1 -0
  85. package/dist/traces.d.ts +26 -10
  86. package/dist/traces.js +41 -11
  87. package/dist/{types-Ce17tDlG.d.ts → types-B5x54y6n.d.ts} +1 -1
  88. package/dist/{types-mn5Aqk7x.d.ts → types-BUxNaJ8c.d.ts} +2 -4
  89. package/dist/{types-BU-7W85F.d.ts → types-DQRY8ZT-.d.ts} +60 -58
  90. package/dist/workflow/index.d.ts +5 -4
  91. package/dist/workflow/index.js +1 -1
  92. package/docs/campaign-proposers.md +170 -0
  93. package/docs/concepts.md +8 -4
  94. package/docs/customer-journeys.md +15 -13
  95. package/docs/design/loop-taxonomy.md +34 -66
  96. package/docs/distributed-driver.md +14 -14
  97. package/docs/feature-guide.md +1 -1
  98. package/docs/hosted-ingest-spec.md +2 -3
  99. package/docs/multi-shot-optimization.md +8 -8
  100. package/docs/product-eval-adoption.md +1 -1
  101. package/docs/self-improvement-map.md +33 -29
  102. package/package.json +8 -14
  103. package/dist/chunk-2K6UUZ7P.js.map +0 -1
  104. package/dist/chunk-CTBHKLEU.js.map +0 -1
  105. package/dist/chunk-E4GH6USR.js.map +0 -1
  106. package/dist/chunk-EGPMSBEZ.js.map +0 -1
  107. package/dist/chunk-KWRRMR3J.js.map +0 -1
  108. package/dist/chunk-MIFZUPEK.js.map +0 -1
  109. package/dist/chunk-MPQWFX6Y.js.map +0 -1
  110. package/dist/chunk-Q5LIB7BC.js.map +0 -1
  111. package/dist/chunk-QMUEXQJS.js.map +0 -1
  112. package/dist/chunk-SD2YFWQQ.js.map +0 -1
  113. package/docs/design/external-agent-wedge.md +0 -89
  114. package/docs/design/phase-d-rfc.md +0 -125
  115. package/docs/design/phase4-consumer-migration.md +0 -70
  116. package/docs/design/primitives-integration-spec.md +0 -393
  117. package/docs/design/product-self-improvement-loop.md +0 -146
  118. package/docs/design/self-improvement-engine.md +0 -140
  119. package/docs/design/self-improvement-protocol.md +0 -223
  120. package/docs/design/self-improvement-roadmap.md +0 -106
  121. package/docs/design/substrate-gaps.md +0 -118
  122. package/docs/phase-b-pairing-kit.md +0 -188
  123. package/docs/phase-b-runbook.md +0 -176
  124. package/docs/pilot/README.md +0 -62
  125. package/docs/pilot/customer-checklist.md +0 -90
  126. package/docs/pilot/integration-foreign-stack.md +0 -296
  127. package/docs/pilot/integration-tangle-stack.md +0 -248
  128. package/docs/pilot/one-pager.md +0 -161
  129. package/docs/pilot/sample-insight-report.json +0 -172
  130. package/docs/quickstart-external.md +0 -229
  131. package/docs/research/belief-state-agent-eval-roadmap.md +0 -593
  132. package/docs/research/research-roadmap.md +0 -205
  133. package/docs/specs/driver-honest-spec.md +0 -251
  134. package/docs/specs/hermes-self-improvement-audit.md +0 -93
  135. package/docs/specs/profile-versioning.md +0 -291
  136. package/docs/three-package-architecture.md +0 -168
  137. /package/dist/{chunk-TBDR6PAI.js.map → chunk-IZCEK2HR.js.map} +0 -0
  138. /package/dist/{chunk-KW53MSA5.js.map → chunk-X74V6ESX.js.map} +0 -0
  139. /package/dist/{chunk-2KNZHH3P.js.map → chunk-Z6L6YSU6.js.map} +0 -0
  140. /package/dist/{run-campaign-7WNXMDSN.js.map → run-campaign-WXY7KI67.js.map} +0 -0
@@ -14,31 +14,31 @@ import {
14
14
  buildEvidenceVector,
15
15
  composeGate,
16
16
  defaultProductionGate,
17
- evolutionaryDriver,
17
+ evolutionaryProposer,
18
18
  paretoPolicy,
19
19
  paretoSignificanceGate,
20
20
  runEval
21
- } from "../chunk-SD2YFWQQ.js";
21
+ } from "../chunk-KKWJD5E6.js";
22
22
  import {
23
23
  analyzeRuns
24
- } from "../chunk-2KNZHH3P.js";
24
+ } from "../chunk-Z6L6YSU6.js";
25
25
  import {
26
26
  emitLoopProvenance,
27
- gepaDriver,
27
+ gepaProposer,
28
28
  heldOutGate,
29
29
  runImprovementLoop,
30
30
  surfaceContentHash
31
- } from "../chunk-EGPMSBEZ.js";
31
+ } from "../chunk-7QTQKIDD.js";
32
32
  import {
33
33
  fsCampaignStorage,
34
34
  inMemoryCampaignStorage,
35
35
  runCampaign
36
- } from "../chunk-2K6UUZ7P.js";
36
+ } from "../chunk-2T4EZACH.js";
37
37
  import "../chunk-VI2UW6B6.js";
38
38
  import {
39
39
  buildDefaultAnalystRegistry
40
- } from "../chunk-MIFZUPEK.js";
41
- import "../chunk-Q5LIB7BC.js";
40
+ } from "../chunk-AQ5WQAIV.js";
41
+ import "../chunk-S4SYLDFX.js";
42
42
  import "../chunk-5HRORJQY.js";
43
43
  import {
44
44
  FileSystemOutcomeStore,
@@ -51,7 +51,11 @@ import "../chunk-56GC6VYO.js";
51
51
  import "../chunk-GGE4NNQT.js";
52
52
  import "../chunk-UFSG7ACU.js";
53
53
  import "../chunk-5BKGXME7.js";
54
- import "../chunk-QMUEXQJS.js";
54
+ import {
55
+ LLM_INPUT_TOKEN_ATTR_KEYS,
56
+ LLM_MODEL_ATTR_KEYS,
57
+ LLM_OUTPUT_TOKEN_ATTR_KEYS
58
+ } from "../chunk-YBIGNSCZ.js";
55
59
  import "../chunk-PC4UYEBM.js";
56
60
  import "../chunk-VSMTAMNK.js";
57
61
  import "../chunk-3BFEG2F6.js";
@@ -115,13 +119,13 @@ async function selfImprove(opts) {
115
119
  if (holdout.length === 0) {
116
120
  throw new Error("selfImprove: holdout split is empty. Pass more scenarios.");
117
121
  }
118
- const driver = opts.driver ?? gepaDriver({
122
+ const proposer = opts.proposer ?? gepaProposer({
119
123
  llm: {
120
124
  baseUrl: opts.llm?.baseUrl ?? "https://router.tangle.tools/v1",
121
125
  apiKey: opts.llm?.apiKey ?? process.env.OPENAI_API_KEY ?? ""
122
126
  },
123
127
  model: opts.llm?.model ?? "anthropic/claude-sonnet-4.6",
124
- target: opts.driverTarget ?? "agent surface (system prompt or config) being optimized by selfImprove",
128
+ target: opts.proposerTarget ?? "agent surface (system prompt or config) being optimized by selfImprove",
125
129
  mutationPrimitives: opts.mutationPrimitives ?? DEFAULT_MUTATION_PRIMITIVES
126
130
  });
127
131
  const gate = opts.gate ?? defaultProductionGate({
@@ -138,7 +142,7 @@ async function selfImprove(opts) {
138
142
  scenarios: train,
139
143
  baselineSurface: opts.baselineSurface,
140
144
  dispatchWithSurface: opts.agent,
141
- driver,
145
+ proposer,
142
146
  judges: [opts.judge],
143
147
  populationSize,
144
148
  maxGenerations: generations,
@@ -365,6 +369,111 @@ function cellsToRunRecords(cells, candidateId, runId, surface) {
365
369
  });
366
370
  }
367
371
 
372
+ // src/contract/define-agent-eval.ts
373
+ function defineAgentEval(defaults) {
374
+ const defaultEvaluateOptions = evaluateDefaults(defaults);
375
+ return {
376
+ scenarios: defaults.scenarios,
377
+ baselineSurface: defaults.baselineSurface,
378
+ async evaluate(opts = {}) {
379
+ const { agent, judge, judges, runDir, scenarios, surface, ...campaignOpts } = opts;
380
+ const selectedAgent = agent ?? defaults.agent;
381
+ const selectedSurface = surface ?? defaults.baselineSurface;
382
+ const selectedRunDir = runDir ?? defaults.runDir ?? `mem://defineAgentEval-${Date.now()}`;
383
+ const selectedStorage = campaignOpts.storage ?? defaultEvaluateOptions.storage ?? (selectedRunDir.startsWith("mem://") ? inMemoryCampaignStorage() : void 0);
384
+ const evalOptions = {
385
+ ...defaultEvaluateOptions,
386
+ ...campaignOpts,
387
+ ...selectedStorage ? { storage: selectedStorage } : {},
388
+ runDir: selectedRunDir,
389
+ scenarios: scenarios ?? defaults.scenarios,
390
+ dispatch: (scenario, ctx) => selectedAgent(selectedSurface, scenario, ctx),
391
+ judges: evaluateJudges(judges, judge ?? defaults.judge)
392
+ };
393
+ if (evalOptions.reps !== void 0)
394
+ evalOptions.reps = requirePositiveInteger(evalOptions.reps, "reps");
395
+ return runEval(evalOptions);
396
+ },
397
+ async improve(opts = {}) {
398
+ const {
399
+ budget: budgetOverride,
400
+ hostedTenant: hostedTenantOverride,
401
+ llm: llmOverride,
402
+ ...topLevelOverrides
403
+ } = opts;
404
+ const merged = mergeDefined(defaults, topLevelOverrides);
405
+ const budget = mergeBudget(defaults.budget, budgetOverride);
406
+ const llm = mergeLlm(defaults.llm, llmOverride);
407
+ const hostedTenant = mergeHostedTenant(defaults.hostedTenant, hostedTenantOverride);
408
+ return selfImprove({
409
+ ...merged,
410
+ ...budget ? { budget } : {},
411
+ ...llm ? { llm } : {},
412
+ ...hostedTenant ? { hostedTenant } : {}
413
+ });
414
+ }
415
+ };
416
+ }
417
+ function evaluateDefaults(defaults) {
418
+ const out = {};
419
+ if (defaults.storage) out.storage = defaults.storage;
420
+ if (defaults.labeledStore) out.labeledStore = defaults.labeledStore;
421
+ if (defaults.captureSource) out.captureSource = defaults.captureSource;
422
+ if (defaults.cellPlacement) out.cellPlacement = defaults.cellPlacement;
423
+ if (defaults.expectUsage) out.expectUsage = defaults.expectUsage;
424
+ if (defaults.budget?.dollars !== void 0) out.costCeiling = defaults.budget.dollars;
425
+ if (defaults.budget?.maxConcurrency !== void 0)
426
+ out.maxConcurrency = defaults.budget.maxConcurrency;
427
+ if (defaults.budget?.reps !== void 0)
428
+ out.reps = requirePositiveInteger(defaults.budget.reps, "budget.reps");
429
+ return out;
430
+ }
431
+ function mergeBudget(defaults, overrides) {
432
+ const merged = mergeOptionalObject(defaults, overrides);
433
+ if (merged?.reps !== void 0) merged.reps = requirePositiveInteger(merged.reps, "budget.reps");
434
+ return merged;
435
+ }
436
+ function mergeLlm(defaults, overrides) {
437
+ return mergeOptionalObject(defaults, overrides);
438
+ }
439
+ function mergeHostedTenant(defaults, overrides) {
440
+ const merged = mergeOptionalObject(defaults, overrides);
441
+ if (!merged) return void 0;
442
+ if (!merged.endpoint?.trim() || !merged.apiKey?.trim() || !merged.tenantId?.trim()) {
443
+ throw new Error(
444
+ "defineAgentEval.improve: hostedTenant requires endpoint, apiKey, and tenantId after merging defaults and overrides"
445
+ );
446
+ }
447
+ return merged;
448
+ }
449
+ function mergeDefined(defaults, overrides) {
450
+ if (!overrides) return defaults;
451
+ const merged = { ...defaults };
452
+ for (const [key, value] of Object.entries(overrides)) {
453
+ if (value !== void 0) merged[key] = value;
454
+ }
455
+ return merged;
456
+ }
457
+ function mergeOptionalObject(defaults, overrides) {
458
+ if (!defaults && !overrides) return void 0;
459
+ return mergeDefined(defaults ?? {}, overrides);
460
+ }
461
+ function evaluateJudges(judges, defaultJudge) {
462
+ if (judges !== void 0) {
463
+ if (judges.length === 0) {
464
+ throw new Error("defineAgentEval.evaluate: judges must not be empty");
465
+ }
466
+ return judges;
467
+ }
468
+ return [defaultJudge];
469
+ }
470
+ function requirePositiveInteger(value, field) {
471
+ if (!Number.isInteger(value) || value < 1) {
472
+ throw new Error(`defineAgentEval: ${field} must be a positive integer`);
473
+ }
474
+ return value;
475
+ }
476
+
368
477
  // src/contract/diff.ts
369
478
  function keyForCell(cell) {
370
479
  return JSON.stringify([cell.scenarioId, cell.rep]);
@@ -612,10 +721,10 @@ function fromFeedbackTable(opts) {
612
721
 
613
722
  // src/contract/intake/otel-spans.ts
614
723
  var SCORE_KEYS = ["tangle.score", "eval.score", "score"];
615
- var MODEL_KEYS = ["tangle.model", "gen_ai.request.model", "llm.model", "model"];
724
+ var MODEL_KEYS = ["tangle.model", ...LLM_MODEL_ATTR_KEYS, "model"];
616
725
  var COST_KEYS = ["tangle.cost.usd", "gen_ai.usage.cost_usd", "cost.usd", "cost"];
617
- var INPUT_TOKEN_KEYS = ["gen_ai.usage.input_tokens", "tangle.tokens.in", "tokens.in"];
618
- var OUTPUT_TOKEN_KEYS = ["gen_ai.usage.output_tokens", "tangle.tokens.out", "tokens.out"];
726
+ var INPUT_TOKEN_KEYS = [...LLM_INPUT_TOKEN_ATTR_KEYS, "tangle.tokens.in", "tokens.in"];
727
+ var OUTPUT_TOKEN_KEYS = [...LLM_OUTPUT_TOKEN_ATTR_KEYS, "tangle.tokens.out", "tokens.out"];
619
728
  var PROMPT_HASH_KEYS = ["tangle.prompt_hash", "prompt.hash"];
620
729
  var CONFIG_HASH_KEYS = ["tangle.config_hash", "config.hash"];
621
730
  function fromOtelSpans(opts) {
@@ -720,10 +829,11 @@ export {
720
829
  buildEvidenceVector,
721
830
  composeGate,
722
831
  defaultProductionGate,
832
+ defineAgentEval,
723
833
  diffGenerations,
724
834
  diffRunBaselineToWinner,
725
835
  diffRuns,
726
- evolutionaryDriver,
836
+ evolutionaryProposer,
727
837
  fromClaudeCodeSession,
728
838
  fromCodexSession,
729
839
  fromFeedbackTable,
@@ -733,7 +843,7 @@ export {
733
843
  fromPiSession,
734
844
  fromPigraphSession,
735
845
  fsCampaignStorage,
736
- gepaDriver,
846
+ gepaProposer,
737
847
  heldOutGate,
738
848
  inMemoryCampaignStorage,
739
849
  paretoPolicy,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/contract/self-improve.ts","../../src/contract/diff.ts","../../src/contract/intake/agent-trace.ts","../../src/contract/intake/feedback-table.ts","../../src/contract/intake/otel-spans.ts"],"sourcesContent":["/**\n * # `selfImprove()` — the LAND-tier one-shot.\n *\n * The cheapest possible call site to run a real closed-loop self-\n * improvement over your agent. Wraps `runImprovementLoop` with smart\n * defaults and a budget-shaped options API; every escape hatch the\n * substrate exposes is reachable from here without losing the\n * one-function feel.\n *\n * Defaults picked to match the LAND-tier story:\n * - In-memory storage (no filesystem touch).\n * - `gepaDriver` reflective mutation with copywriting-flavored primitives\n * (override `driver` or `mutationPrimitives` for any domain).\n * - `defaultProductionGate` with `deltaThreshold: 0.05`.\n * - Held-out split = 25% of scenarios, deterministic by id hash.\n * - 3 generations × population 2 (raise via `budget` for more search).\n * - `autoOnPromote: 'none'` (we don't open PRs unless you ask).\n *\n * Want one-click? Provide `agent` + `scenarios` + `judge`. Done.\n * Want distributed? Pass `cellPlacement` + an `httpDispatch`-backed\n * agent. Want a code-tier surface? Pass a `MutableSurface` + your own\n * `driver`. Same function.\n */\n\nimport { createHash } from 'node:crypto'\nimport { gepaDriver } from '../campaign/drivers/gepa'\nimport { defaultProductionGate } from '../campaign/gates/default-production-gate'\nimport {\n type RunImprovementLoopResult,\n runImprovementLoop,\n} from '../campaign/presets/run-improvement-loop'\nimport type { RunOptimizationOptions } from '../campaign/presets/run-optimization'\nimport {\n emitLoopProvenance,\n type LoopProvenanceRecord,\n surfaceContentHash,\n} from '../campaign/provenance'\nimport {\n type CampaignStorage,\n fsCampaignStorage,\n inMemoryCampaignStorage,\n} from '../campaign/storage'\nimport type {\n CampaignCellResult,\n DispatchContext,\n Gate,\n ImprovementDriver,\n JudgeConfig,\n LabeledScenarioStore,\n MutableSurface,\n Scenario,\n} from '../campaign/types'\nimport { createHostedClient, type HostedTenant } from '../hosted/client'\nimport type { EvalRunCellScore, EvalRunEvent, EvalRunGenerationSnapshot } from '../hosted/types'\nimport type { JudgeScoresRecord, RunRecord } from '../run-record'\nimport { analyzeRuns } from './analyze-runs'\nimport type { InsightReport } from './insight-report'\n\nexport interface SelfImproveBudget {\n /** Hard $ ceiling across all cells in baseline + every generation. Cells\n * beyond the ceiling are skipped (cost-aware, not aborted). */\n dollars?: number\n /** How many improvement generations to explore. Default 3. Set 0 to\n * skip improvement entirely (selfImprove becomes a baseline-only run). */\n generations?: number\n /** Candidates the driver proposes per generation. Default 2. */\n populationSize?: number\n /** Max concurrent cells across the loop. Default 2. */\n maxConcurrency?: number\n /** Fraction of `scenarios` held out from training, used for the gate.\n * Default 0.25. Ignored when `holdoutScenarios` is set explicitly. */\n holdoutFraction?: number\n /** Explicit held-out scenarios; overrides `holdoutFraction`. */\n holdoutScenarios?: Scenario[]\n /** Per-scenario replicates per cell — raises bootstrap-CI tightness. Default 1. */\n reps?: number\n /** Top-scoring candidates carried into the next generation. Default 2. */\n promoteTopK?: number\n}\n\nexport interface SelfImproveLlm {\n /** Endpoint base URL. Default Tangle Router. */\n baseUrl?: string\n /** Bearer token. Default `process.env.OPENAI_API_KEY`. */\n apiKey?: string\n /** Model id used by `gepaDriver` reflection. Default\n * `anthropic/claude-sonnet-4.6`. */\n model?: string\n}\n\nexport type SelfImproveProgressEvent =\n | { kind: 'baseline.started'; scenarios: number }\n | { kind: 'baseline.completed'; compositeMean: number; durationMs: number }\n | { kind: 'generation.started'; index: number; populationSize: number }\n | { kind: 'generation.completed'; index: number; bestComposite: number; durationMs: number }\n | { kind: 'gate.decided'; decision: string; lift: number }\n\nexport interface SelfImproveOptions<TScenario extends Scenario, TArtifact> {\n /**\n * Your agent — a function that takes the current `MutableSurface`\n * (typically a system prompt the loop is optimizing) plus the\n * scenario + cell ctx, and returns the artifact your judge scores.\n *\n * Same shape as `RunOptimizationOptions.dispatchWithSurface`. Wrap a\n * plain `Dispatch` if you don't have a surface seam:\n *\n * agent: (_surface, scenario, ctx) => yourPlainDispatch(scenario, ctx)\n *\n * That mode evaluates without mutating any surface — useful as a\n * baseline-only run (set `budget.generations = 0`).\n */\n agent: (surface: MutableSurface, scenario: TScenario, ctx: DispatchContext) => Promise<TArtifact>\n\n /** Scenarios to evaluate against. Train/holdout split is computed from\n * these unless `budget.holdoutScenarios` is set explicitly. */\n scenarios: TScenario[]\n\n /** Judge that scores artifacts. Bring your own; use `langchainJudge`\n * from `/adapters/langchain` for a Runnable-shaped one. */\n judge: JudgeConfig<TArtifact, TScenario>\n\n /** Starting surface — system prompt, JSON config, anything `MutableSurface`\n * accepts. The driver mutates this each generation. */\n baselineSurface: MutableSurface\n\n /** Budget + loop shape. All fields optional; defaults pick the LAND-tier\n * story. */\n budget?: SelfImproveBudget\n\n /** Custom driver. Default is `gepaDriver` configured from `llm` +\n * `mutationPrimitives`. */\n driver?: ImprovementDriver\n\n /** Default-driver overrides — used when `driver` is unset. */\n mutationPrimitives?: string[]\n driverTarget?: string\n\n /** Custom gate. Default is `defaultProductionGate` with\n * `deltaThreshold: 0.05` on the held-out split. */\n gate?: Gate<TArtifact, TScenario>\n\n /** LLM config consumed by the default `gepaDriver`. Ignored if you pass\n * your own `driver`. */\n llm?: SelfImproveLlm\n\n /** Storage backend. Default is DURABLE: when a real (non-`mem://`) `runDir`\n * is available, the substrate defaults to `fsCampaignStorage()` so the\n * provenance record + OTel spans survive the call. Pass\n * `inMemoryCampaignStorage()` explicitly to opt OUT (tests, edge runtimes).\n * Default when `runDir` is `mem://...` (or unset): in-memory. */\n storage?: CampaignStorage\n\n /** Run directory (logical for in-memory storage, real path for fs).\n * Default `mem://selfImprove-<timestamp>` (in-memory, non-durable). Pass a\n * real path to persist the provenance record + spans. */\n runDir?: string\n\n /**\n * Worker call records for backend provenance. The agent is opaque to the\n * substrate (it returns an artifact, not token usage), so to capture an\n * `assertRealBackend`-grade verdict + worker call count + model in the\n * provenance record, the agent reports its per-call `RunRecord`s here.\n * Called once after the loop; return the records the agent accumulated.\n * When unset, backend provenance is derived from campaign cells (cost only;\n * verdict will read `stub` without token usage — the honest signal that no\n * token channel was wired).\n */\n collectWorkerRecords?: () => RunRecord[]\n\n /** Fires once the durable provenance record + OTel spans are emitted.\n * Receives the structured record for inline assertions / custom routing. */\n onProvenance?: (record: LoopProvenanceRecord) => void\n\n /** Distributed-driver seam — same as `RunCampaignOptions.cellPlacement`.\n * Returns an opaque placement key the substrate forwards to your agent\n * as `ctx.placement`. Combined with `httpDispatch` from\n * `/adapters/http`, fans cells across regions. */\n cellPlacement?: (input: {\n scenario: TScenario\n rep: number\n generation?: number\n }) => string | undefined\n\n /** Streaming hook — fires on baseline + each generation + gate decision.\n * Consumer routes events wherever (UI, dashboard, logs). */\n onProgress?: (event: SelfImproveProgressEvent) => void\n\n /** Auto-promotion behavior on a ship decision. Default `'none'` — we\n * return the winner; you ship it however you ship. `'pr'` opens a\n * GitHub PR via `openAutoPr`; requires `ghOwner` + `ghRepo`. */\n autoOnPromote?: 'pr' | 'none'\n ghOwner?: string\n ghRepo?: string\n\n /**\n * Opt-in: ship eval-run events to a hosted orchestrator (ours, your\n * self-hosted one, or any compatible implementation of the\n * `docs/hosted-ingest-spec.md` wire format). When set, the substrate\n * POSTs the final `EvalRunEvent` to `${endpoint}/v1/ingest/eval-runs`\n * after the loop completes. Failures are logged but do not fail the\n * loop — local result is always returned.\n *\n * For our orchestrator: `{ endpoint: 'https://orchestrator.tangle.tools/v1', apiKey, tenantId }`.\n *\n * For your self-hosted: any URL serving the wire format. See\n * `examples/hosted-ingest-server/` for the reference receiver.\n */\n hostedTenant?: HostedTenant\n\n /** Free-form labels attached to the hosted event (env, branch, model id,\n * etc.). Ignored when `hostedTenant` is unset. */\n hostedLabels?: Record<string, string>\n\n /** Capture every artifact + judge score to this store (labeled-example\n * corpus the driver may read for few-shot, and the dataset you ship). Pass\n * `'off'` to disable. Default: off. */\n labeledStore?: LabeledScenarioStore | 'off'\n\n /** Capture-source tag for `labeledStore`. Default `'eval-run'`. */\n captureSource?: 'production-trace' | 'eval-run' | 'manual' | 'red-team' | 'synthetic'\n\n /**\n * Per-cell backend-integrity expectation — the fail-loud guard. A cell that\n * produced an artifact but reported `costUsd === 0` AND zero tokens is a\n * stub. Modes: `'assert'` throws on the first such cell, `'warn'` logs it,\n * `'off'` skips the check (offline/replay). Default `'assert'` — `selfImprove`\n * is the real-run path, so a stub fails loud rather than scoring a clean 0.\n */\n expectUsage?: 'assert' | 'warn' | 'off'\n\n /**\n * Per-generation findings producer (the EYES→HANDS closure). After each\n * generation is scored, this is called; whatever it returns REPLACES the\n * driver's `findings` for the next generation's `propose()`. Plug a\n * trace-analyst registry / HALO here. When absent, findings stay\n * `opts.findings`.\n */\n analyzeGeneration?: RunOptimizationOptions<TScenario, TArtifact>['analyzeGeneration']\n\n /** Static findings forwarded to the driver's `propose()` as `ctx.findings`\n * (a findings-grounded driver consumes them). Default: none. */\n findings?: unknown[]\n}\n\nexport interface SelfImproveResult<TScenario extends Scenario, TArtifact> {\n /** Composite mean across all scenarios, baseline run. */\n baseline: {\n compositeMean: number\n perScenario: Record<string, number>\n }\n /** Composite mean on the held-out set, winner run. */\n winner: {\n compositeMean: number\n perScenario: Record<string, number>\n surface: MutableSurface\n /** Driver label for the promoted change. Absent ⇒ winner == baseline or\n * a bare-surface mutator. */\n label?: string\n /** Driver rationale — the \"because Z\" that motivated the promoted change.\n * Threaded from the driver's `ProposedCandidate` through the loop.\n * Absent ⇒ winner == baseline. */\n rationale?: string\n }\n /** `winner.compositeMean - baselineOnHoldout.compositeMean`. Positive\n * means the gate observed improvement. */\n lift: number\n /** The explicit baseline→winner unified diff. Always present (empty string\n * when winner == baseline). */\n diff: string\n /** Durable, queryable provenance record: candidate→cell→gate→promote chain +\n * rationale + diff + backend provenance. The artifact the hosted ingest\n * path stores; the +lift RECOMPUTES from `record.heldOutLift`. */\n provenance: LoopProvenanceRecord\n /** `defaultProductionGate.decide()` result. */\n gateDecision: 'ship' | 'hold' | 'need_more_work' | 'model_ceiling' | 'arch_ceiling'\n /** Number of generations actually explored (may be less than the\n * budget if the driver gave up early). */\n generationsExplored: number\n /** Wall-clock total. */\n durationMs: number\n /** Total cost across baseline + every generation. */\n totalCostUsd: number\n /**\n * Rigor packet: distributional summary, paired-bootstrap lift CI,\n * judge stats, contamination check, recommendations. Wired through\n * `analyzeRuns()` on the baseline + winner cells of the campaign.\n * Hosted-tier dashboards render this as the v3-vs-v4 decision view.\n */\n insight: InsightReport\n /**\n * Raw substrate result for advanced inspection — full per-generation\n * candidates, full campaign artifacts, all judge scores. Useful for\n * debugging or reporting beyond the summary.\n */\n raw: RunImprovementLoopResult<TArtifact, TScenario>\n}\n\n/**\n * Deterministic train/holdout split by a stable hash of `scenario.id`,\n * so the same scenario set always splits the same way across runs.\n */\nfunction splitTrainHoldout<TScenario extends Scenario>(\n scenarios: TScenario[],\n fraction: number,\n): { train: TScenario[]; holdout: TScenario[] } {\n // Stable fnv-1a-ish hash of the id for ordering.\n function hash(s: string): number {\n let h = 2166136261 >>> 0\n for (let i = 0; i < s.length; i++) {\n h ^= s.charCodeAt(i)\n h = Math.imul(h, 16777619) >>> 0\n }\n return h\n }\n const sorted = [...scenarios].sort((a, b) => hash(a.id) - hash(b.id))\n const nHoldout = Math.max(1, Math.min(sorted.length - 1, Math.round(sorted.length * fraction)))\n return {\n holdout: sorted.slice(0, nHoldout),\n train: sorted.slice(nHoldout),\n }\n}\n\nfunction meanComposite(byScenario: Record<string, { meanComposite: number }>): {\n compositeMean: number\n perScenario: Record<string, number>\n} {\n const perScenario: Record<string, number> = {}\n const values: number[] = []\n for (const [id, agg] of Object.entries(byScenario)) {\n perScenario[id] = agg.meanComposite\n values.push(agg.meanComposite)\n }\n return {\n compositeMean: values.length === 0 ? 0 : values.reduce((s, v) => s + v, 0) / values.length,\n perScenario,\n }\n}\n\nconst DEFAULT_MUTATION_PRIMITIVES = [\n 'Tighten the hook: lead with the specific user outcome.',\n 'Replace generic adjectives with specific verbs or proof numbers.',\n \"Anchor every claim in something the scenario's brief literally supports.\",\n 'Honor the surface-shape constraint (length, register, audience vocabulary).',\n]\n\n/**\n * One-shot self-improvement loop. See module docstring for defaults +\n * extension points.\n *\n * @example Minimum (LAND tier):\n *\n * const result = await selfImprove({\n * agent: (surface, scenario, ctx) => myAgent(surface, scenario, ctx.signal),\n * scenarios,\n * judge,\n * baselineSurface: DEFAULT_PROMPT,\n * })\n * console.log(`lift: ${result.lift.toFixed(3)} (${result.gateDecision})`)\n *\n * @example Distributed (workers in three regions):\n *\n * await selfImprove({\n * agent: httpDispatch({ resolveUrl: ({ placement }) => REGION_URLS[placement!] }),\n * scenarios,\n * judge,\n * baselineSurface: DEFAULT_PROMPT,\n * cellPlacement: ({ scenario }) => scenario.region,\n * budget: { maxConcurrency: 12 },\n * })\n */\nexport async function selfImprove<TScenario extends Scenario, TArtifact>(\n opts: SelfImproveOptions<TScenario, TArtifact>,\n): Promise<SelfImproveResult<TScenario, TArtifact>> {\n const startedAt = Date.now()\n\n const budget = opts.budget ?? {}\n const generations = budget.generations ?? 3\n const populationSize = budget.populationSize ?? 2\n const maxConcurrency = budget.maxConcurrency ?? 2\n const holdoutFraction = budget.holdoutFraction ?? 0.25\n const costCeiling = budget.dollars\n const expectUsage = opts.expectUsage ?? 'assert'\n\n const explicitHoldout = budget.holdoutScenarios\n const { train, holdout } = explicitHoldout\n ? {\n train: opts.scenarios.filter((s) => !explicitHoldout.some((h) => h.id === s.id)),\n holdout: explicitHoldout as TScenario[],\n }\n : splitTrainHoldout(opts.scenarios, holdoutFraction)\n\n if (train.length === 0) {\n throw new Error(\n 'selfImprove: train split is empty. Reduce holdoutFraction or pass more scenarios.',\n )\n }\n if (holdout.length === 0) {\n throw new Error('selfImprove: holdout split is empty. Pass more scenarios.')\n }\n\n const driver: ImprovementDriver =\n opts.driver ??\n gepaDriver({\n llm: {\n baseUrl: opts.llm?.baseUrl ?? 'https://router.tangle.tools/v1',\n apiKey: opts.llm?.apiKey ?? process.env.OPENAI_API_KEY ?? '',\n },\n model: opts.llm?.model ?? 'anthropic/claude-sonnet-4.6',\n target:\n opts.driverTarget ??\n 'agent surface (system prompt or config) being optimized by selfImprove',\n mutationPrimitives: opts.mutationPrimitives ?? DEFAULT_MUTATION_PRIMITIVES,\n })\n\n const gate: Gate<TArtifact, TScenario> =\n opts.gate ??\n defaultProductionGate<TArtifact, TScenario>({\n holdoutScenarios: holdout,\n deltaThreshold: 0.05,\n })\n\n // Durable by default: a real (non-`mem://`) runDir means the caller wants\n // persistence, so default to fs storage — the provenance record + spans\n // survive the call. A `mem://` runDir (or none) stays in-memory. An explicit\n // `storage` always wins (the opt-out path for tests / edge runtimes).\n const runDir = opts.runDir ?? `mem://selfImprove-${startedAt}`\n const isMemRunDir = runDir.startsWith('mem://')\n const storage = opts.storage ?? (isMemRunDir ? inMemoryCampaignStorage() : fsCampaignStorage())\n\n if (opts.onProgress) {\n opts.onProgress({ kind: 'baseline.started', scenarios: opts.scenarios.length })\n }\n\n const result = await runImprovementLoop<TScenario, TArtifact>({\n scenarios: train,\n baselineSurface: opts.baselineSurface,\n dispatchWithSurface: opts.agent,\n driver,\n judges: [opts.judge],\n populationSize,\n maxGenerations: generations,\n promoteTopK: budget.promoteTopK,\n reps: budget.reps,\n holdoutScenarios: holdout,\n gate,\n autoOnPromote: opts.autoOnPromote ?? 'none',\n ghOwner: opts.ghOwner,\n ghRepo: opts.ghRepo,\n storage,\n runDir,\n maxConcurrency,\n cellPlacement: opts.cellPlacement,\n costCeiling,\n expectUsage,\n labeledStore: opts.labeledStore,\n captureSource: opts.captureSource,\n analyzeGeneration: opts.analyzeGeneration,\n findings: opts.findings,\n })\n\n const baseline = meanComposite(result.baselineOnHoldout.aggregates.byScenario)\n const winnerStats = meanComposite(result.winnerOnHoldout.aggregates.byScenario)\n\n if (opts.onProgress) {\n opts.onProgress({\n kind: 'baseline.completed',\n compositeMean: baseline.compositeMean,\n durationMs: Date.now() - startedAt,\n })\n opts.onProgress({\n kind: 'gate.decided',\n decision: result.gateResult.decision,\n lift: winnerStats.compositeMean - baseline.compositeMean,\n })\n }\n\n const totalCost =\n result.baselineCampaign.aggregates.totalCostUsd +\n result.generations.reduce(\n (sum, gen) =>\n sum + gen.surfaces.reduce((s, sf) => s + sf.campaign.aggregates.totalCostUsd, 0),\n 0,\n )\n\n // Rigor packet: feed baseline + winner cells through analyzeRuns().\n // The two candidates (`baseline` / `winner`) give the lift section a\n // clean paired comparison; per-judge / per-dimension / cost-quality\n // sections populate from the cells' judgeScores.\n const insight = await analyzeRuns({\n runs: [\n ...cellsToRunRecords(result.baselineCampaign.cells, 'baseline', runDir, opts.baselineSurface),\n ...cellsToRunRecords(result.winnerOnHoldout.cells, 'winner', runDir, result.winnerSurface),\n ],\n baselineCandidateId: 'baseline',\n candidateCandidateId: 'winner',\n })\n\n // ── Durable provenance: candidate→cell→gate→promote chain + rationale +\n // diff + backend provenance. Always emitted; the +lift recomputes from it.\n const durationMs = Date.now() - startedAt\n const workerRecords =\n opts.collectWorkerRecords?.() ??\n cellsToRunRecords(result.winnerOnHoldout.cells, 'winner', runDir, result.winnerSurface)\n const { record: provenance } = await emitLoopProvenance<TArtifact, TScenario>({\n runId: `${runDir}#${startedAt}`,\n runDir,\n timestamp: new Date(startedAt).toISOString(),\n baselineSurface: opts.baselineSurface,\n winnerSurface: result.winnerSurface,\n winnerLabel: result.winnerLabel,\n winnerRationale: result.winnerRationale,\n diff: result.promotedDiff,\n generations: result.generations.map((g) => ({\n generationIndex: g.record.generationIndex,\n candidates: g.record.candidates,\n promoted: g.record.promoted,\n surfaces: g.surfaces.map((s) => ({ surfaceHash: s.surfaceHash, surface: s.surface })),\n })),\n gate: result.gateResult,\n baselineOnHoldout: result.baselineOnHoldout,\n winnerOnHoldout: result.winnerOnHoldout,\n workerRecords,\n totalCostUsd: totalCost,\n totalDurationMs: durationMs,\n storage,\n hostedClient: opts.hostedTenant ? createHostedClient(opts.hostedTenant) : undefined,\n })\n if (opts.onProvenance) opts.onProvenance(provenance)\n\n const summary: SelfImproveResult<TScenario, TArtifact> = {\n baseline,\n winner: {\n ...winnerStats,\n surface: result.winnerSurface,\n ...(result.winnerLabel ? { label: result.winnerLabel } : {}),\n ...(result.winnerRationale ? { rationale: result.winnerRationale } : {}),\n },\n lift: winnerStats.compositeMean - baseline.compositeMean,\n diff: result.promotedDiff,\n provenance,\n gateDecision: result.gateResult.decision,\n generationsExplored: result.generations.length,\n durationMs,\n totalCostUsd: totalCost,\n insight,\n raw: result,\n }\n\n // Opt-in hosted ingest. Failures logged but never fail the loop — the\n // local result is always returned. This matches the wedge-doc invariant\n // that LAND-tier never blocks on EXPAND-tier infra.\n if (opts.hostedTenant) {\n try {\n await shipEvalRunToHosted(opts.hostedTenant, opts, summary, result, runDir)\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err)\n // eslint-disable-next-line no-console -- intentional: hosted-ingest is best-effort\n console.warn(`[agent-eval] hosted ingest failed (continuing): ${msg}`)\n }\n }\n\n return summary\n}\n\nasync function shipEvalRunToHosted<TScenario extends Scenario, TArtifact>(\n tenant: HostedTenant,\n opts: SelfImproveOptions<TScenario, TArtifact>,\n summary: SelfImproveResult<TScenario, TArtifact>,\n raw: RunImprovementLoopResult<TArtifact, TScenario>,\n runDir: string,\n): Promise<void> {\n const client = createHostedClient(tenant)\n\n function snapshotFromCampaign(\n index: number,\n surface: MutableSurface | undefined,\n campaign: RunImprovementLoopResult<TArtifact, TScenario>['baselineCampaign'],\n durationMs: number,\n ): EvalRunGenerationSnapshot {\n const cells: EvalRunCellScore[] = campaign.cells.map((cell) => {\n const judgeScores = Object.values(cell.judgeScores)\n const composite =\n judgeScores.length === 0\n ? 0\n : judgeScores.reduce((s, j) => s + j.composite, 0) / judgeScores.length\n return {\n scenarioId: cell.scenarioId,\n rep: cell.rep,\n compositeMean: composite,\n dimensions: Object.fromEntries(\n Object.entries(cell.judgeScores).map(([name, score]) => [name, score.dimensions]),\n ),\n errorMessage: cell.error ?? undefined,\n }\n })\n const compositeMean =\n cells.length === 0 ? 0 : cells.reduce((s, c) => s + c.compositeMean, 0) / cells.length\n return {\n index,\n surfaceHash:\n typeof surface === 'string'\n ? hashString(surface)\n : hashString(JSON.stringify(surface ?? '')),\n surface,\n cells,\n compositeMean,\n costUsd: campaign.aggregates.totalCostUsd,\n durationMs,\n }\n }\n\n const generations: EvalRunGenerationSnapshot[] = []\n // Baseline as generation 0.\n generations.push(snapshotFromCampaign(0, opts.baselineSurface, raw.baselineCampaign, 0))\n // Improvement generations as 1..N. Substrate stores per-surface campaigns\n // per generation — we summarize the WINNING surface per generation here.\n for (const gen of raw.generations) {\n const winner = gen.surfaces.reduce(\n (best, s) =>\n s.campaign.aggregates.cellsExecuted > 0 &&\n (best === undefined || averageComposite(s.campaign) > averageComposite(best.campaign))\n ? s\n : best,\n gen.surfaces[0],\n )\n if (!winner) continue\n generations.push(\n snapshotFromCampaign(gen.record.generationIndex + 1, winner.surface, winner.campaign, 0),\n )\n }\n\n const event: EvalRunEvent = {\n runId: `${runDir}#${Date.now()}`,\n runDir,\n timestamp: new Date().toISOString(),\n status: 'finished',\n labels: opts.hostedLabels ?? {},\n baseline: generations[0],\n generations,\n gateDecision: summary.gateDecision,\n holdoutLift: summary.lift,\n totalCostUsd: summary.totalCostUsd,\n totalDurationMs: summary.durationMs,\n insightReport: summary.insight,\n }\n\n await client.ingestEvalRun(event)\n}\n\nfunction averageComposite(\n campaign: RunImprovementLoopResult<unknown, Scenario>['baselineCampaign'],\n): number {\n const aggs = Object.values(campaign.aggregates.byScenario)\n return aggs.length === 0 ? 0 : aggs.reduce((s, a) => s + a.meanComposite, 0) / aggs.length\n}\n\nfunction hashString(s: string): string {\n let h = 2166136261 >>> 0\n for (let i = 0; i < s.length; i++) {\n h ^= s.charCodeAt(i)\n h = Math.imul(h, 16777619) >>> 0\n }\n return h.toString(16).padStart(8, '0')\n}\n\n/**\n * Adapt campaign cells into the `RunRecord` shape `analyzeRuns()` consumes.\n * Each cell becomes one run; `candidateId` is the caller-supplied label so\n * baseline + winner pair cleanly on `(experimentId, seed)`.\n *\n * `promptHash` is the REAL sha256 content hash of the surface this cell ran\n * (baseline vs winner are byte-distinguishable + byte-identical-verifiable);\n * `configHash` is the sha256 of the candidate label so the two candidates'\n * config rows differ. Both were previously the literal `'sha256:cell'`, which\n * made baseline and winner indistinguishable in every downstream record.\n */\nfunction cellsToRunRecords<TArtifact>(\n cells: ReadonlyArray<CampaignCellResult<TArtifact>>,\n candidateId: 'baseline' | 'winner',\n runId: string,\n surface: MutableSurface,\n): RunRecord[] {\n const promptHash = surfaceContentHash(surface)\n const configHash = `sha256:${createHash('sha256').update(candidateId).digest('hex')}`\n return cells.map((cell) => {\n const perJudge: Record<string, Record<string, number>> = {}\n const perDimMeanAccum: Record<string, { sum: number; n: number }> = {}\n let compositeSum = 0\n let compositeCount = 0\n for (const [judgeId, score] of Object.entries(cell.judgeScores)) {\n perJudge[judgeId] = { ...score.dimensions }\n for (const [dim, value] of Object.entries(score.dimensions)) {\n if (!Number.isFinite(value)) continue\n const accum = perDimMeanAccum[dim] ?? { sum: 0, n: 0 }\n accum.sum += value\n accum.n += 1\n perDimMeanAccum[dim] = accum\n }\n if (Number.isFinite(score.composite)) {\n compositeSum += score.composite\n compositeCount += 1\n }\n }\n const perDimMean: Record<string, number> = {}\n for (const [dim, { sum, n }] of Object.entries(perDimMeanAccum)) {\n perDimMean[dim] = n === 0 ? 0 : sum / n\n }\n const composite = compositeCount === 0 ? 0 : compositeSum / compositeCount\n const judgeScores: JudgeScoresRecord = {\n perJudge,\n perDimMean,\n composite,\n }\n return {\n runId: `${runId}::${candidateId}::${cell.cellId}`,\n experimentId: runId,\n candidateId,\n // Pair on (scenarioId, rep) — analyzeRuns pairs on (experimentId, seed).\n // Synthesize a stable seed for that pairing.\n seed:\n cell.rep * 1_000_000 +\n hashString(cell.scenarioId)\n .slice(0, 6)\n .split('')\n .reduce((a, c) => (a * 31 + c.charCodeAt(0)) >>> 0, 0),\n model: 'campaign-cell',\n promptHash,\n configHash,\n commitSha: 'cell',\n wallMs: cell.durationMs,\n costUsd: cell.costUsd,\n tokenUsage: { input: 0, output: 0 },\n outcome: {\n holdoutScore: composite,\n raw: {},\n judgeScores,\n },\n splitTag: 'holdout',\n ...(cell.error ? { failureMode: cell.error } : {}),\n } satisfies RunRecord\n })\n}\n","/**\n * # `@tangle-network/agent-eval/contract` — eval-run diff primitive.\n *\n * The substrate side of the v-N-versus-v-N+1 dashboard view. Given two\n * `EvalRunEvent`s (or two `EvalRunGenerationSnapshot`s from one run), this\n * returns a normalised diff: per-cell composite + per-judge/per-dimension\n * deltas, surface-hash change, aggregate cost + duration shifts.\n *\n * Consumed by:\n * - The hosted-tier dashboard (intelligence-web) — renders v3 vs v4\n * comparisons of cells × judges × dimensions.\n * - CI reporting — emits a \"shipped: composite +0.07, cost +$1.20\" line\n * in PR review for autonomous-improvement runs.\n * - Any downstream consumer that needs \"what actually changed\" without\n * reimplementing the matching + arithmetic.\n *\n * Cells are matched on the natural composite key `(scenarioId, rep)`.\n * Unmatched cells surface as `removed` / `added` so callers can tell\n * \"this cell got worse\" from \"this cell wasn't run.\"\n */\n\nimport type { GateDecision } from '../campaign/types'\nimport type { EvalRunCellScore, EvalRunEvent, EvalRunGenerationSnapshot } from '../hosted/types'\n\n// ── Types ────────────────────────────────────────────────────────────\n\n/** Per-dimension delta. `before` / `after` are null when the judge did not\n * emit a value for that side. `delta` is `after - before`; null when\n * either side is null. */\nexport interface EvalDimensionDelta {\n before: number | null\n after: number | null\n delta: number | null\n}\n\n/** Per-cell delta, keyed on `(scenarioId, rep)`. */\nexport interface EvalCellScoreDelta {\n scenarioId: string\n rep: number\n compositeBefore: number\n compositeAfter: number\n compositeDelta: number\n /** Per-judge → per-dimension deltas. Outer key = judge name from\n * `EvalRunCellScore.dimensions`; inner key = dimension name. */\n dimensions: Record<string, Record<string, EvalDimensionDelta>>\n}\n\n/** Diff between two generation snapshots — the unit the dashboard renders\n * for a single \"v3 vs v4\" comparison. */\nexport interface EvalGenerationDiff {\n beforeIndex: number\n afterIndex: number\n beforeSurfaceHash: string\n afterSurfaceHash: string\n surfaceChanged: boolean\n /** Cells present in both snapshots, matched on `(scenarioId, rep)`. */\n matched: EvalCellScoreDelta[]\n /** Cells present in `before` but missing from `after`. */\n removed: EvalRunCellScore[]\n /** Cells present in `after` but missing from `before`. */\n added: EvalRunCellScore[]\n /** Aggregate composite mean across all cells in the snapshot. */\n compositeBefore: number\n compositeAfter: number\n compositeDelta: number\n costUsdBefore: number\n costUsdAfter: number\n costUsdDelta: number\n durationMsBefore: number\n durationMsAfter: number\n durationMsDelta: number\n}\n\n/** Diff between two full eval-runs. Includes both baseline-vs-baseline and\n * winner-vs-winner generation diffs when both sides expose them, plus\n * run-level metadata. */\nexport interface EvalRunDiff {\n beforeRunId: string\n afterRunId: string\n beforeTimestamp: string\n afterTimestamp: string\n beforeGateDecision: GateDecision | null\n afterGateDecision: GateDecision | null\n beforeHoldoutLift: number | null\n afterHoldoutLift: number | null\n holdoutLiftDelta: number | null\n beforeTotalCostUsd: number\n afterTotalCostUsd: number\n totalCostUsdDelta: number\n beforeTotalDurationMs: number\n afterTotalDurationMs: number\n totalDurationMsDelta: number\n /** Baseline-vs-baseline diff. Null when either run has no baseline. */\n baselineDiff: EvalGenerationDiff | null\n /** Highest-index-generation comparison. Null when either run has no\n * recorded generations (e.g. baseline-only or errored before any\n * generation completed). */\n winnersDiff: EvalGenerationDiff | null\n}\n\n// ── Implementation ───────────────────────────────────────────────────\n\nfunction keyForCell(cell: EvalRunCellScore): string {\n // JSON-tuple key — `scenarioId` may legitimately contain `::` or any other\n // delimiter, so we use JSON.stringify on a 2-tuple to get an unambiguous,\n // collision-free composite key.\n return JSON.stringify([cell.scenarioId, cell.rep])\n}\n\n/** Build the per-dimension delta map for a matched cell. Each judge name +\n * dimension name encountered on EITHER side appears in the result. */\nfunction diffDimensions(\n before: EvalRunCellScore['dimensions'],\n after: EvalRunCellScore['dimensions'],\n): EvalCellScoreDelta['dimensions'] {\n const out: EvalCellScoreDelta['dimensions'] = {}\n const judges = new Set<string>([...Object.keys(before), ...Object.keys(after)])\n for (const judge of judges) {\n const beforeDims = before[judge] ?? {}\n const afterDims = after[judge] ?? {}\n const dims = new Set<string>([...Object.keys(beforeDims), ...Object.keys(afterDims)])\n const judgeOut: Record<string, EvalDimensionDelta> = {}\n for (const dim of dims) {\n // Coerce non-finite values (NaN, ±Infinity) to null so the diff never\n // surfaces NaN/Infinity to the dashboard. A NaN score is a substrate\n // bug from upstream; the diff treats it as \"no value\" rather than\n // propagating the corruption.\n const rawBefore = beforeDims[dim]\n const rawAfter = afterDims[dim]\n const b = typeof rawBefore === 'number' && Number.isFinite(rawBefore) ? rawBefore : null\n const a = typeof rawAfter === 'number' && Number.isFinite(rawAfter) ? rawAfter : null\n judgeOut[dim] = {\n before: b,\n after: a,\n delta: b !== null && a !== null ? a - b : null,\n }\n }\n out[judge] = judgeOut\n }\n return out\n}\n\n/**\n * Diff two generation snapshots. Cells are matched on `(scenarioId, rep)`;\n * unmatched cells surface in `added` / `removed`. Aggregate fields are\n * recomputed from the snapshot's stored fields, not re-derived from cells —\n * this keeps the diff consistent with whatever aggregation the substrate\n * actually reported.\n */\nexport function diffGenerations(\n before: EvalRunGenerationSnapshot,\n after: EvalRunGenerationSnapshot,\n): EvalGenerationDiff {\n const beforeMap = new Map(before.cells.map((c) => [keyForCell(c), c]))\n const afterMap = new Map(after.cells.map((c) => [keyForCell(c), c]))\n\n const matched: EvalCellScoreDelta[] = []\n const removed: EvalRunCellScore[] = []\n const added: EvalRunCellScore[] = []\n\n for (const [key, beforeCell] of beforeMap) {\n const afterCell = afterMap.get(key)\n if (!afterCell) {\n removed.push(beforeCell)\n continue\n }\n matched.push({\n scenarioId: beforeCell.scenarioId,\n rep: beforeCell.rep,\n compositeBefore: beforeCell.compositeMean,\n compositeAfter: afterCell.compositeMean,\n compositeDelta: afterCell.compositeMean - beforeCell.compositeMean,\n dimensions: diffDimensions(beforeCell.dimensions, afterCell.dimensions),\n })\n }\n for (const [key, afterCell] of afterMap) {\n if (!beforeMap.has(key)) added.push(afterCell)\n }\n\n return {\n beforeIndex: before.index,\n afterIndex: after.index,\n beforeSurfaceHash: before.surfaceHash,\n afterSurfaceHash: after.surfaceHash,\n surfaceChanged: before.surfaceHash !== after.surfaceHash,\n matched,\n removed,\n added,\n compositeBefore: before.compositeMean,\n compositeAfter: after.compositeMean,\n compositeDelta: after.compositeMean - before.compositeMean,\n costUsdBefore: before.costUsd,\n costUsdAfter: after.costUsd,\n costUsdDelta: after.costUsd - before.costUsd,\n durationMsBefore: before.durationMs,\n durationMsAfter: after.durationMs,\n durationMsDelta: after.durationMs - before.durationMs,\n }\n}\n\n/** Highest-index generation, or null if the run recorded none. */\nfunction winnerOf(run: EvalRunEvent): EvalRunGenerationSnapshot | null {\n if (run.generations.length === 0) return null\n let winner = run.generations[0] as EvalRunGenerationSnapshot\n for (const gen of run.generations) {\n if (gen.index > winner.index) winner = gen\n }\n return winner\n}\n\n/**\n * Diff two full eval-runs. Produces baseline-vs-baseline and\n * winner-vs-winner generation diffs when both sides expose them, plus\n * run-level cost / lift / gate-decision deltas.\n */\nexport function diffRuns(before: EvalRunEvent, after: EvalRunEvent): EvalRunDiff {\n const beforeWinner = winnerOf(before)\n const afterWinner = winnerOf(after)\n const baselineDiff =\n before.baseline && after.baseline ? diffGenerations(before.baseline, after.baseline) : null\n const winnersDiff =\n beforeWinner && afterWinner ? diffGenerations(beforeWinner, afterWinner) : null\n\n const beforeLift = before.holdoutLift ?? null\n const afterLift = after.holdoutLift ?? null\n\n return {\n beforeRunId: before.runId,\n afterRunId: after.runId,\n beforeTimestamp: before.timestamp,\n afterTimestamp: after.timestamp,\n beforeGateDecision: before.gateDecision ?? null,\n afterGateDecision: after.gateDecision ?? null,\n beforeHoldoutLift: beforeLift,\n afterHoldoutLift: afterLift,\n holdoutLiftDelta: beforeLift !== null && afterLift !== null ? afterLift - beforeLift : null,\n beforeTotalCostUsd: before.totalCostUsd,\n afterTotalCostUsd: after.totalCostUsd,\n totalCostUsdDelta: after.totalCostUsd - before.totalCostUsd,\n beforeTotalDurationMs: before.totalDurationMs,\n afterTotalDurationMs: after.totalDurationMs,\n totalDurationMsDelta: after.totalDurationMs - before.totalDurationMs,\n baselineDiff,\n winnersDiff,\n }\n}\n\n/**\n * Within-run baseline → winning-generation diff. The natural \"what did the\n * improvement loop produce\" view for a single run. Returns null when the\n * run never reached a generation past baseline (errored early, or the gate\n * shipped the baseline as-is).\n */\nexport function diffRunBaselineToWinner(run: EvalRunEvent): EvalGenerationDiff | null {\n if (!run.baseline) return null\n const winner = winnerOf(run)\n if (!winner || winner.index === run.baseline.index) return null\n return diffGenerations(run.baseline, winner)\n}\n","/**\n * `fromAgentTrace` — provenance correlation from Cursor's Agent Trace spec\n * (https://github.com/cursor/agent-trace, RFC v0.1.0).\n *\n * Agent Trace is NOT a run/quality trace — it carries no outcome, score, or\n * cost. It records *code authorship*: for a VCS revision, which AI model /\n * conversation authored which file ranges. It explicitly disclaims quality\n * assessment — which is exactly what `analyzeRuns` adds.\n *\n * The two layers join on a key the substrate already has: a `RunRecord`\n * carries `commitSha`, and an Agent Trace record is keyed by\n * `vcs.revision`. So this adapter does not produce `RunRecord`s — it builds a\n * provenance index by commit and partitions existing runs by their authoring\n * model. Feed each cohort to `analyzeRuns` (or pass one as `baselineRuns`) to\n * answer the question no run-only trace can: *which authoring agent's code\n * fails / regresses / costs more.*\n *\n * Granularity is commit-level (the SHA join). Per-file/per-line correlation\n * would require runs to record which files they exercised — out of scope.\n */\n\nimport type { RunRecord } from '../../run-record'\n\n// ── Agent Trace record schema (the subset we read) ──────────────────────────\n\nexport type AgentTraceContributorType = 'human' | 'ai' | 'mixed' | 'unknown'\n\nexport interface AgentTraceContributor {\n type: AgentTraceContributorType\n /** models.dev id, e.g. `anthropic/claude-opus-4-5-20251101`. */\n model_id?: string\n}\n\nexport interface AgentTraceRange {\n start_line: number\n end_line: number\n content_hash?: string\n /** Per-range contributor override (agent handoffs). Wins over the\n * conversation-level contributor for these lines. */\n contributor?: AgentTraceContributor\n}\n\nexport interface AgentTraceConversation {\n url?: string\n contributor?: AgentTraceContributor\n ranges: AgentTraceRange[]\n}\n\nexport interface AgentTraceFile {\n path: string\n conversations: AgentTraceConversation[]\n}\n\nexport interface AgentTraceRecord {\n version: string\n id: string\n timestamp: string\n vcs?: { type: string; revision: string }\n tool?: { name?: string; version?: string }\n files: AgentTraceFile[]\n}\n\n// ── Provenance index ─────────────────────────────────────────────────────────\n\n/** Authorship provenance for one VCS revision, aggregated across the record's\n * files/conversations/ranges. */\nexport interface AuthoringProvenance {\n commitSha: string\n /** Unique AI model ids that authored code in this commit (type ai|mixed). */\n aiModels: string[]\n /** Tools that produced the records (e.g. `cursor`). */\n tools: string[]\n conversationCount: number\n fileCount: number\n /** Total attributed lines (sum of range spans). */\n lineCount: number\n /** True if any range was authored (in whole or part) by a human. */\n humanInvolved: boolean\n}\n\nexport type AgentTraceIndex = Map<string, AuthoringProvenance>\n\nfunction rangeLines(r: AgentTraceRange): number {\n return Math.max(0, r.end_line - r.start_line + 1)\n}\n\n/**\n * Build a commit → provenance index from Agent Trace records. Multiple records\n * for the same revision are merged. Records without `vcs.revision` are skipped\n * (the SHA is the join key — without it there is nothing to correlate against).\n */\nexport function parseAgentTrace(records: AgentTraceRecord[]): AgentTraceIndex {\n interface Acc {\n models: Set<string>\n tools: Set<string>\n files: Set<string>\n conversationCount: number\n lineCount: number\n humanInvolved: boolean\n }\n const acc = new Map<string, Acc>()\n\n for (const record of records) {\n const sha = record.vcs?.revision\n if (!sha) continue // the SHA is the join key — nothing to correlate without it\n\n let a = acc.get(sha)\n if (!a) {\n a = {\n models: new Set(),\n tools: new Set(),\n files: new Set(),\n conversationCount: 0,\n lineCount: 0,\n humanInvolved: false,\n }\n acc.set(sha, a)\n }\n\n if (record.tool?.name) a.tools.add(record.tool.name)\n\n for (const file of record.files ?? []) {\n a.files.add(file.path)\n for (const conv of file.conversations ?? []) {\n a.conversationCount += 1\n for (const range of conv.ranges ?? []) {\n // Per-range contributor wins, else the conversation contributor.\n const contributor = range.contributor ?? conv.contributor\n a.lineCount += rangeLines(range)\n if (!contributor) continue\n if (contributor.type === 'human' || contributor.type === 'mixed') {\n a.humanInvolved = true\n }\n if ((contributor.type === 'ai' || contributor.type === 'mixed') && contributor.model_id) {\n a.models.add(contributor.model_id)\n }\n }\n }\n }\n }\n\n const index: AgentTraceIndex = new Map()\n for (const [sha, a] of acc) {\n index.set(sha, {\n commitSha: sha,\n aiModels: [...a.models].sort(),\n tools: [...a.tools].sort(),\n conversationCount: a.conversationCount,\n fileCount: a.files.size,\n lineCount: a.lineCount,\n humanInvolved: a.humanInvolved,\n })\n }\n return index\n}\n\n// ── Run ↔ provenance join ──────────────────────────────────────────────────\n\nexport interface PartitionByAuthoringModelResult {\n /** Runs grouped by each AI model that authored code in the run's commit. A\n * run whose commit had multiple authoring models appears under EACH — the\n * cohorts overlap by construction at commit granularity. */\n byModel: Map<string, RunRecord[]>\n /** Runs whose `commitSha` had no Agent Trace provenance (no record, or no\n * AI authorship). Kept separate — never silently folded into a cohort. */\n unattributed: RunRecord[]\n}\n\n/**\n * Partition runs by the AI model(s) that authored the code at each run's\n * `commitSha`. Feed `byModel.get(modelId)` to `analyzeRuns`, or compare two\n * model cohorts via `analyzeRuns({ runs: a, baselineRuns: b })` for a lift CI\n * on \"model A's code vs model B's code\".\n */\nexport function partitionRunsByAuthoringModel(\n runs: RunRecord[],\n index: AgentTraceIndex,\n): PartitionByAuthoringModelResult {\n const byModel = new Map<string, RunRecord[]>()\n const unattributed: RunRecord[] = []\n\n for (const run of runs) {\n const provenance = index.get(run.commitSha)\n if (!provenance || provenance.aiModels.length === 0) {\n unattributed.push(run)\n continue\n }\n for (const model of provenance.aiModels) {\n const cohort = byModel.get(model) ?? []\n cohort.push(run)\n byModel.set(model, cohort)\n }\n }\n\n return { byModel, unattributed }\n}\n","/**\n * # `intake/feedback-table` — multi-rater approve/reject corpus → `RunRecord[]`.\n *\n * The generic shape behind Obsidian's `#approved` / `#rejected` tags, a\n * Google Sheet, a Postgres `feedback` table, or any CSV with ratings.\n *\n * Caller supplies one row per (run, rater) tuple plus per-run metadata; the\n * adapter rolls them up into the substrate-canonical `RunRecord` shape so\n * `analyzeRuns({ runs, raterScores })` can produce inter-rater agreement,\n * disagreement triage, and downstream recommendations.\n *\n * Per-run `RunRecord.outcome.searchScore` is the rater-mean rating\n * (normalised to 0..1 when scale is supplied); `outcome.raw` carries the\n * per-rater scores keyed by rater id for downstream attribution.\n */\n\nimport type { JudgeScoresRecord, RunOutcome, RunRecord, RunSplitTag } from '../../run-record'\n\nexport interface FeedbackTableRow {\n /** Stable id for this run — the unit a rater scored. Drives pairing\n * across analysis primitives. */\n runId: string\n /** Identifier of the rater that produced this rating. */\n rater: string\n /** The rating itself. Accepts boolean (approve/reject), 0..1 scalar,\n * or any numeric scale — see `scale`. */\n rating: number | boolean\n /** Optional metadata carried through to `RunRecord.outcome.raw` and the\n * custom-shape metadata bag. */\n metadata?: Record<string, unknown>\n}\n\nexport interface FeedbackTableMeta {\n runId: string\n /** When omitted, defaults to `'feedback-corpus'`. Used to group related\n * runs in `analyzeRuns()` lift analysis. */\n experimentId?: string\n /** When omitted, defaults to `runId` — each run is its own candidate. */\n candidateId?: string\n /** Cost in USD, when available. Set to 0 when unknown — the consumer's\n * cost analysis sections will collapse gracefully. */\n costUsd?: number\n /** Wall-clock ms, when available. Defaults to 0. */\n wallMs?: number\n /** Model identifier including snapshot. Default `unknown@unknown`. */\n model?: string\n /** Optional sha256 of the prompt; default `'sha256:unknown'`. */\n promptHash?: string\n /** Default `'sha256:unknown'`. */\n configHash?: string\n /** Default `'unknown'`. */\n commitSha?: string\n /** Default `'holdout'` — feedback corpora are by nature the holdout\n * signal a closed-loop improvement aims at. */\n splitTag?: RunSplitTag\n /** Free-form metadata available to consumers via the cast-out path on\n * the resulting RunRecord. */\n extras?: Record<string, unknown>\n}\n\nexport interface FromFeedbackTableOptions {\n /** Per-(run, rater) ratings. */\n ratings: FeedbackTableRow[]\n /** Per-run metadata. When a runId appears in `ratings` but not here, the\n * adapter synthesises minimal metadata with defaults documented above. */\n meta?: FeedbackTableMeta[]\n /** Rating scale. Provide `{ min, max }` for non-0..1 numeric scales.\n * Booleans are normalised: true → 1, false → 0. Default: assumes\n * ratings are already 0..1. */\n scale?: { min: number; max: number }\n /** When true, the rater scores are emitted into `raterScores` (a sibling\n * array `analyzeRuns()` accepts) instead of being averaged into the\n * run's `outcome.searchScore`. Default `true` — preserves rater-level\n * signal for inter-rater analysis. */\n emitRaterScores?: boolean\n}\n\nexport interface FromFeedbackTableResult {\n runs: RunRecord[]\n /** Rater-level scores ready to pass into `analyzeRuns({ raterScores })`\n * for inter-rater agreement + disagreement triage. */\n raterScores: Array<{ runId: string; rater: string; score: number }>\n}\n\nexport function fromFeedbackTable(opts: FromFeedbackTableOptions): FromFeedbackTableResult {\n const { ratings, meta = [], scale, emitRaterScores = true } = opts\n const metaByRun = new Map(meta.map((m) => [m.runId, m]))\n\n // Normalise per-rating to a 0..1 score.\n const normalise = (rating: number | boolean): number => {\n if (typeof rating === 'boolean') return rating ? 1 : 0\n if (!Number.isFinite(rating)) return Number.NaN\n if (!scale) return rating\n const { min, max } = scale\n if (max === min) return rating\n return (rating - min) / (max - min)\n }\n\n // Group ratings by runId.\n const byRun = new Map<string, FeedbackTableRow[]>()\n for (const row of ratings) {\n const list = byRun.get(row.runId) ?? []\n list.push(row)\n byRun.set(row.runId, list)\n }\n\n const runs: RunRecord[] = []\n const raterScores: FromFeedbackTableResult['raterScores'] = []\n\n for (const [runId, rowsForRun] of byRun) {\n const normalised = rowsForRun\n .map((r) => ({ rater: r.rater, score: normalise(r.rating) }))\n .filter((r) => Number.isFinite(r.score))\n if (normalised.length === 0) continue\n\n const meanScore = normalised.reduce((s, r) => s + r.score, 0) / normalised.length\n\n const runMeta = metaByRun.get(runId) ?? ({ runId } as FeedbackTableMeta)\n\n const judgeScores: JudgeScoresRecord = {\n perJudge: Object.fromEntries(normalised.map((r) => [r.rater, { rating: r.score }])),\n perDimMean: { rating: meanScore },\n composite: meanScore,\n }\n\n const outcome: RunOutcome = {\n // Feedback corpora ARE the holdout signal — score lands on\n // `holdoutScore` so downstream substrate primitives (`paretoChart`,\n // promotion gates) read it correctly by default.\n holdoutScore: meanScore,\n raw: Object.fromEntries(normalised.map((r) => [`rater:${r.rater}`, r.score])),\n judgeScores,\n }\n\n runs.push({\n runId,\n experimentId: runMeta.experimentId ?? 'feedback-corpus',\n candidateId: runMeta.candidateId ?? runId,\n seed: 0,\n model: runMeta.model ?? 'unknown@unknown',\n promptHash: runMeta.promptHash ?? 'sha256:unknown',\n configHash: runMeta.configHash ?? 'sha256:unknown',\n commitSha: runMeta.commitSha ?? 'unknown',\n wallMs: runMeta.wallMs ?? 0,\n costUsd: runMeta.costUsd ?? 0,\n tokenUsage: { input: 0, output: 0 },\n outcome,\n splitTag: runMeta.splitTag ?? 'holdout',\n } as RunRecord)\n\n if (emitRaterScores) {\n for (const r of normalised) raterScores.push({ runId, rater: r.rater, score: r.score })\n }\n }\n\n return { runs, raterScores }\n}\n","/**\n * # `intake/otel-spans` — OTel `TraceSpanEvent[]` → `RunRecord[]`.\n *\n * Turns an existing observability stream into the substrate-canonical\n * `RunRecord` shape so consumers with logs but no eval discipline can\n * call `analyzeRuns()` against their production traffic immediately.\n *\n * Pivot rule: spans are grouped by `tangle.runId` (the same attribute the\n * hosted-tier wire format uses) or, when absent, by `traceId`. One group\n * becomes one `RunRecord`. The root span (no `parentSpanId`) supplies:\n *\n * - `runId` (the group key)\n * - `wallMs` from `endTimeUnixNano - startTimeUnixNano`\n * - `model` from `gen_ai.request.model` / `llm.model` / `tangle.model`\n * - cost from `cost.usd` / `gen_ai.usage.cost_usd` / `tangle.cost.usd`\n * - token usage from `gen_ai.usage.{input,output}_tokens`\n * - `outcome.searchScore` from `tangle.score` / `eval.score` when\n * present; `outcome.raw` collects every numeric attribute.\n *\n * Spans that ERRORed (`status.code === 'ERROR'`) populate `failureMode`\n * with their `name` so `analyzeRuns()`'s failure clustering sees them.\n */\n\nimport type { TraceSpanEvent } from '../../hosted/types'\nimport type {\n JudgeScoresRecord,\n RunOutcome,\n RunRecord,\n RunSplitTag,\n RunTokenUsage,\n} from '../../run-record'\n\nconst SCORE_KEYS = ['tangle.score', 'eval.score', 'score']\nconst MODEL_KEYS = ['tangle.model', 'gen_ai.request.model', 'llm.model', 'model']\nconst COST_KEYS = ['tangle.cost.usd', 'gen_ai.usage.cost_usd', 'cost.usd', 'cost']\nconst INPUT_TOKEN_KEYS = ['gen_ai.usage.input_tokens', 'tangle.tokens.in', 'tokens.in']\nconst OUTPUT_TOKEN_KEYS = ['gen_ai.usage.output_tokens', 'tangle.tokens.out', 'tokens.out']\nconst PROMPT_HASH_KEYS = ['tangle.prompt_hash', 'prompt.hash']\nconst CONFIG_HASH_KEYS = ['tangle.config_hash', 'config.hash']\n\nexport interface FromOtelSpansOptions {\n spans: TraceSpanEvent[]\n /** Default split tag for synthesized records. Defaults to `'holdout'`. */\n defaultSplit?: RunSplitTag\n /** Default `experimentId` when not present on any span. */\n experimentId?: string\n}\n\nexport function fromOtelSpans(opts: FromOtelSpansOptions): RunRecord[] {\n const { spans, defaultSplit = 'holdout', experimentId = 'otel-corpus' } = opts\n const grouped = groupSpans(spans)\n\n const runs: RunRecord[] = []\n for (const [groupKey, groupSpans] of grouped) {\n const root = findRoot(groupSpans)\n if (!root) continue\n\n const wallMs = Math.max(0, (root.endTimeUnixNano - root.startTimeUnixNano) / 1_000_000)\n const model = readAttrString(groupSpans, MODEL_KEYS) ?? 'unknown@unknown'\n const costUsd = readAttrNumber(groupSpans, COST_KEYS) ?? 0\n const inputTokens = readAttrNumber(groupSpans, INPUT_TOKEN_KEYS) ?? 0\n const outputTokens = readAttrNumber(groupSpans, OUTPUT_TOKEN_KEYS) ?? 0\n const promptHash = readAttrString(groupSpans, PROMPT_HASH_KEYS) ?? 'sha256:unknown'\n const configHash = readAttrString(groupSpans, CONFIG_HASH_KEYS) ?? 'sha256:unknown'\n const score = readAttrNumber(groupSpans, SCORE_KEYS)\n\n const rawNumeric = collectNumericAttrs(groupSpans)\n const tokenUsage: RunTokenUsage = {\n input: inputTokens,\n output: outputTokens,\n }\n\n const judgeScores: JudgeScoresRecord | undefined =\n score !== undefined\n ? {\n perJudge: { 'otel-derived': { score } },\n perDimMean: { score },\n composite: score,\n }\n : undefined\n\n const errorSpan = groupSpans.find((s) => s.status?.code === 'ERROR')\n const outcome: RunOutcome = {\n ...(opts.defaultSplit === 'search' ? { searchScore: score } : { holdoutScore: score }),\n raw: rawNumeric,\n ...(judgeScores ? { judgeScores } : {}),\n }\n\n runs.push({\n runId: groupKey,\n experimentId,\n candidateId: (root.attributes['tangle.candidateId'] as string | undefined) ?? 'otel-default',\n seed: 0,\n model,\n promptHash,\n configHash,\n commitSha: (root.attributes['tangle.commit_sha'] as string | undefined) ?? 'unknown',\n wallMs,\n costUsd,\n tokenUsage,\n outcome,\n splitTag: defaultSplit,\n ...(errorSpan ? { failureMode: errorSpan.name } : {}),\n } as RunRecord)\n }\n return runs\n}\n\n// ── Internal helpers ────────────────────────────────────────────────\n\nfunction groupSpans(spans: TraceSpanEvent[]): Map<string, TraceSpanEvent[]> {\n const m = new Map<string, TraceSpanEvent[]>()\n for (const span of spans) {\n const key = (span['tangle.runId'] as string | undefined) ?? span.traceId\n const list = m.get(key) ?? []\n list.push(span)\n m.set(key, list)\n }\n return m\n}\n\nfunction findRoot(group: TraceSpanEvent[]): TraceSpanEvent | undefined {\n return group.find((s) => !s.parentSpanId) ?? group[0]\n}\n\nfunction readAttrString(spans: TraceSpanEvent[], keys: string[]): string | undefined {\n for (const span of spans) {\n for (const key of keys) {\n const v = span.attributes[key]\n if (typeof v === 'string' && v.length > 0) return v\n }\n }\n return undefined\n}\n\nfunction readAttrNumber(spans: TraceSpanEvent[], keys: string[]): number | undefined {\n for (const span of spans) {\n for (const key of keys) {\n const v = span.attributes[key]\n if (typeof v === 'number' && Number.isFinite(v)) return v\n if (typeof v === 'string') {\n const parsed = Number(v)\n if (Number.isFinite(parsed)) return parsed\n }\n }\n }\n return undefined\n}\n\nfunction collectNumericAttrs(spans: TraceSpanEvent[]): Record<string, number> {\n const raw: Record<string, number> = {}\n for (const span of spans) {\n for (const [k, v] of Object.entries(span.attributes)) {\n if (typeof v === 'number' && Number.isFinite(v)) raw[k] = v\n }\n }\n return raw\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAS,kBAAkB;AAqR3B,SAAS,kBACP,WACA,UAC8C;AAE9C,WAAS,KAAK,GAAmB;AAC/B,QAAI,IAAI,eAAe;AACvB,aAAS,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;AACjC,WAAK,EAAE,WAAW,CAAC;AACnB,UAAI,KAAK,KAAK,GAAG,QAAQ,MAAM;AAAA,IACjC;AACA,WAAO;AAAA,EACT;AACA,QAAM,SAAS,CAAC,GAAG,SAAS,EAAE,KAAK,CAAC,GAAG,MAAM,KAAK,EAAE,EAAE,IAAI,KAAK,EAAE,EAAE,CAAC;AACpE,QAAM,WAAW,KAAK,IAAI,GAAG,KAAK,IAAI,OAAO,SAAS,GAAG,KAAK,MAAM,OAAO,SAAS,QAAQ,CAAC,CAAC;AAC9F,SAAO;AAAA,IACL,SAAS,OAAO,MAAM,GAAG,QAAQ;AAAA,IACjC,OAAO,OAAO,MAAM,QAAQ;AAAA,EAC9B;AACF;AAEA,SAAS,cAAc,YAGrB;AACA,QAAM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,cAAM,SAAS,OAAO,CAAC;AACvB,YAAI,OAAO,SAAS,MAAM,EAAG,QAAO;AAAA,MACtC;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,oBAAoB,OAAiD;AAC5E,QAAM,MAA8B,CAAC;AACrC,aAAW,QAAQ,OAAO;AACxB,eAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,KAAK,UAAU,GAAG;AACpD,UAAI,OAAO,MAAM,YAAY,OAAO,SAAS,CAAC,EAAG,KAAI,CAAC,IAAI;AAAA,IAC5D;AAAA,EACF;AACA,SAAO;AACT;","names":["groupSpans"]}
1
+ {"version":3,"sources":["../../src/contract/self-improve.ts","../../src/contract/define-agent-eval.ts","../../src/contract/diff.ts","../../src/contract/intake/agent-trace.ts","../../src/contract/intake/feedback-table.ts","../../src/contract/intake/otel-spans.ts"],"sourcesContent":["/**\n * # `selfImprove()` - the one-call improvement loop.\n *\n * The cheapest possible call site to run a real closed-loop self-\n * improvement over your agent. Wraps `runImprovementLoop` with smart\n * defaults and a budget-shaped options API; every escape hatch the\n * substrate exposes is reachable from here without losing the\n * one-function feel.\n *\n * Defaults:\n * - In-memory storage (no filesystem touch).\n * - `gepaProposer` reflective mutation with copywriting-flavored primitives\n * (override `proposer` or `mutationPrimitives` for any domain).\n * - `defaultProductionGate` with `deltaThreshold: 0.05`.\n * - Held-out split = 25% of scenarios, deterministic by id hash.\n * - 3 generations × population 2 (raise via `budget` for more search).\n * - `autoOnPromote: 'none'` (we don't open PRs unless you ask).\n *\n * Want one-click? Provide `agent` + `scenarios` + `judge`. Done.\n * Want distributed? Pass `cellPlacement` + an `httpDispatch`-backed\n * agent. Want a code-tier surface? Pass a `MutableSurface` + your own\n * `proposer`. Same function.\n */\n\nimport { createHash } from 'node:crypto'\nimport { defaultProductionGate } from '../campaign/gates/default-production-gate'\nimport {\n type RunImprovementLoopResult,\n runImprovementLoop,\n} from '../campaign/presets/run-improvement-loop'\nimport type { RunOptimizationOptions } from '../campaign/presets/run-optimization'\nimport { gepaProposer } from '../campaign/proposers/gepa'\nimport {\n emitLoopProvenance,\n type LoopProvenanceRecord,\n surfaceContentHash,\n} from '../campaign/provenance'\nimport {\n type CampaignStorage,\n fsCampaignStorage,\n inMemoryCampaignStorage,\n} from '../campaign/storage'\nimport type {\n CampaignCellResult,\n DispatchContext,\n Gate,\n JudgeConfig,\n LabeledScenarioStore,\n MutableSurface,\n Scenario,\n SurfaceProposer,\n} from '../campaign/types'\nimport { createHostedClient, type HostedTenant } from '../hosted/client'\nimport type { EvalRunCellScore, EvalRunEvent, EvalRunGenerationSnapshot } from '../hosted/types'\nimport type { JudgeScoresRecord, RunRecord } from '../run-record'\nimport { analyzeRuns } from './analyze-runs'\nimport type { InsightReport } from './insight-report'\n\nexport interface SelfImproveBudget {\n /** Hard $ ceiling across all cells in baseline + every generation. Cells\n * beyond the ceiling are skipped (cost-aware, not aborted). */\n dollars?: number\n /** How many improvement generations to explore. Default 3. Set 0 to\n * skip improvement entirely (selfImprove becomes a baseline-only run). */\n generations?: number\n /** Candidates the proposer emits per generation. Default 2. */\n populationSize?: number\n /** Max concurrent cells across the loop. Default 2. */\n maxConcurrency?: number\n /** Fraction of `scenarios` held out from training, used for the gate.\n * Default 0.25. Ignored when `holdoutScenarios` is set explicitly. */\n holdoutFraction?: number\n /** Explicit held-out scenarios; overrides `holdoutFraction`. */\n holdoutScenarios?: Scenario[]\n /** Per-scenario replicates per cell — raises bootstrap-CI tightness. Default 1. */\n reps?: number\n /** Top-scoring candidates carried into the next generation. Default 2. */\n promoteTopK?: number\n}\n\nexport interface SelfImproveLlm {\n /** Endpoint base URL. Default Tangle Router. */\n baseUrl?: string\n /** Bearer token. Default `process.env.OPENAI_API_KEY`. */\n apiKey?: string\n /** Model id used by `gepaProposer` reflection. Default\n * `anthropic/claude-sonnet-4.6`. */\n model?: string\n}\n\nexport type SelfImproveProgressEvent =\n | { kind: 'baseline.started'; scenarios: number }\n | { kind: 'baseline.completed'; compositeMean: number; durationMs: number }\n | { kind: 'generation.started'; index: number; populationSize: number }\n | { kind: 'generation.completed'; index: number; bestComposite: number; durationMs: number }\n | { kind: 'gate.decided'; decision: string; lift: number }\n\nexport interface SelfImproveOptions<TScenario extends Scenario, TArtifact> {\n /**\n * Your agent — a function that takes the current `MutableSurface`\n * (typically a system prompt the loop is optimizing) plus the\n * scenario + cell ctx, and returns the artifact your judge scores.\n *\n * Same shape as `RunOptimizationOptions.dispatchWithSurface`. Wrap a\n * plain `Dispatch` if you don't have a surface seam:\n *\n * agent: (_surface, scenario, ctx) => yourPlainDispatch(scenario, ctx)\n *\n * That mode evaluates without mutating any surface — useful as a\n * baseline-only run (set `budget.generations = 0`).\n */\n agent: (surface: MutableSurface, scenario: TScenario, ctx: DispatchContext) => Promise<TArtifact>\n\n /** Scenarios to evaluate against. Train/holdout split is computed from\n * these unless `budget.holdoutScenarios` is set explicitly. */\n scenarios: TScenario[]\n\n /** Judge that scores artifacts. Bring your own; use `langchainJudge`\n * from `/adapters/langchain` for a Runnable-shaped one. */\n judge: JudgeConfig<TArtifact, TScenario>\n\n /** Starting surface — system prompt, JSON config, anything `MutableSurface`\n * accepts. The proposer mutates this each generation. */\n baselineSurface: MutableSurface\n\n /** Budget + loop shape. All fields optional. */\n budget?: SelfImproveBudget\n\n /** Custom surface proposer. Default is `gepaProposer` configured from `llm` +\n * `mutationPrimitives`. */\n proposer?: SurfaceProposer\n\n /** Default-proposer overrides — used when `proposer` is unset. */\n mutationPrimitives?: string[]\n proposerTarget?: string\n\n /** Custom gate. Default is `defaultProductionGate` with\n * `deltaThreshold: 0.05` on the held-out split. */\n gate?: Gate<TArtifact, TScenario>\n\n /** LLM config consumed by the default `gepaProposer`. Ignored if you pass\n * your own `proposer`. */\n llm?: SelfImproveLlm\n\n /** Storage backend. Default is DURABLE: when a real (non-`mem://`) `runDir`\n * is available, the substrate defaults to `fsCampaignStorage()` so the\n * provenance record + OTel spans survive the call. Pass\n * `inMemoryCampaignStorage()` explicitly to opt OUT (tests, edge runtimes).\n * Default when `runDir` is `mem://...` (or unset): in-memory. */\n storage?: CampaignStorage\n\n /** Run directory (logical for in-memory storage, real path for fs).\n * Default `mem://selfImprove-<timestamp>` (in-memory, non-durable). Pass a\n * real path to persist the provenance record + spans. */\n runDir?: string\n\n /**\n * Worker call records for backend provenance. The agent is opaque to the\n * substrate (it returns an artifact, not token usage), so to capture an\n * `assertRealBackend`-grade verdict + worker call count + model in the\n * provenance record, the agent reports its per-call `RunRecord`s here.\n * Called once after the loop; return the records the agent accumulated.\n * When unset, backend provenance is derived from campaign cells (cost only;\n * verdict will read `stub` without token usage — the honest signal that no\n * token channel was wired).\n */\n collectWorkerRecords?: () => RunRecord[]\n\n /** Fires once the durable provenance record + OTel spans are emitted.\n * Receives the structured record for inline assertions / custom routing. */\n onProvenance?: (record: LoopProvenanceRecord) => void\n\n /** Distributed execution seam — same as `RunCampaignOptions.cellPlacement`.\n * Returns an opaque placement key the substrate forwards to your agent\n * as `ctx.placement`. Combined with `httpDispatch` from\n * `/adapters/http`, fans cells across regions. */\n cellPlacement?: (input: {\n scenario: TScenario\n rep: number\n generation?: number\n }) => string | undefined\n\n /** Streaming hook — fires on baseline + each generation + gate decision.\n * Consumer routes events wherever (UI, dashboard, logs). */\n onProgress?: (event: SelfImproveProgressEvent) => void\n\n /** Auto-promotion behavior on a ship decision. Default `'none'` — we\n * return the winner; you ship it however you ship. `'pr'` opens a\n * GitHub PR via `openAutoPr`; requires `ghOwner` + `ghRepo`. */\n autoOnPromote?: 'pr' | 'none'\n ghOwner?: string\n ghRepo?: string\n\n /**\n * Opt-in: ship eval-run events to a hosted orchestrator (ours, your\n * self-hosted one, or any compatible implementation of the\n * `docs/hosted-ingest-spec.md` wire format). When set, the substrate\n * POSTs the final `EvalRunEvent` to `${endpoint}/v1/ingest/eval-runs`\n * after the loop completes. Failures are logged but do not fail the\n * loop — local result is always returned.\n *\n * For our orchestrator: `{ endpoint: 'https://orchestrator.tangle.tools/v1', apiKey, tenantId }`.\n *\n * For your self-hosted: any URL serving the wire format. See\n * `examples/hosted-ingest-server/` for the reference receiver.\n */\n hostedTenant?: HostedTenant\n\n /** Free-form labels attached to the hosted event (env, branch, model id,\n * etc.). Ignored when `hostedTenant` is unset. */\n hostedLabels?: Record<string, string>\n\n /** Capture every artifact + judge score to this store (labeled-example\n * corpus the proposer may read for few-shot, and the dataset you ship). Pass\n * `'off'` to disable. Default: off. */\n labeledStore?: LabeledScenarioStore | 'off'\n\n /** Capture-source tag for `labeledStore`. Default `'eval-run'`. */\n captureSource?: 'production-trace' | 'eval-run' | 'manual' | 'red-team' | 'synthetic'\n\n /**\n * Per-cell backend-integrity expectation — the fail-loud guard. A cell that\n * produced an artifact but reported `costUsd === 0` AND zero tokens is a\n * stub. Modes: `'assert'` throws on the first such cell, `'warn'` logs it,\n * `'off'` skips the check (offline/replay). Default `'assert'` — `selfImprove`\n * is the real-run path, so a stub fails loud rather than scoring a clean 0.\n */\n expectUsage?: 'assert' | 'warn' | 'off'\n\n /**\n * Per-generation findings producer. After each\n * generation is scored, this is called; whatever it returns REPLACES the\n * proposer's `findings` for the next generation's `propose()`. Plug a\n * trace-analyst registry / HALO here. When absent, findings stay\n * `opts.findings`.\n */\n analyzeGeneration?: RunOptimizationOptions<TScenario, TArtifact>['analyzeGeneration']\n\n /** Static findings forwarded to the proposer's `propose()` as `ctx.findings`\n * (a findings-grounded proposer consumes them). Default: none. */\n findings?: unknown[]\n}\n\nexport interface SelfImproveResult<TScenario extends Scenario, TArtifact> {\n /** Composite mean across all scenarios, baseline run. */\n baseline: {\n compositeMean: number\n perScenario: Record<string, number>\n }\n /** Composite mean on the held-out set, winner run. */\n winner: {\n compositeMean: number\n perScenario: Record<string, number>\n surface: MutableSurface\n /** Proposer label for the promoted change. Absent ⇒ winner == baseline or\n * a bare-surface mutator. */\n label?: string\n /** Proposer rationale — the \"because Z\" that motivated the promoted change.\n * Threaded from the proposer's `ProposedCandidate` through the loop.\n * Absent ⇒ winner == baseline. */\n rationale?: string\n }\n /** `winner.compositeMean - baselineOnHoldout.compositeMean`. Positive\n * means the gate observed improvement. */\n lift: number\n /** The explicit baseline→winner unified diff. Always present (empty string\n * when winner == baseline). */\n diff: string\n /** Durable, queryable provenance record: candidate→cell→gate→promote chain +\n * rationale + diff + backend provenance. The artifact the hosted ingest\n * path stores; the +lift RECOMPUTES from `record.heldOutLift`. */\n provenance: LoopProvenanceRecord\n /** `defaultProductionGate.decide()` result. */\n gateDecision: 'ship' | 'hold' | 'need_more_work' | 'model_ceiling' | 'arch_ceiling'\n /** Number of generations actually explored (may be less than the\n * budget if the proposer gave up early). */\n generationsExplored: number\n /** Wall-clock total. */\n durationMs: number\n /** Total cost across baseline + every generation. */\n totalCostUsd: number\n /**\n * Rigor packet: distributional summary, paired-bootstrap lift CI,\n * judge stats, contamination check, recommendations. Wired through\n * `analyzeRuns()` on the baseline + winner cells of the campaign.\n * Hosted-tier dashboards render this as the v3-vs-v4 decision view.\n */\n insight: InsightReport\n /**\n * Raw substrate result for advanced inspection — full per-generation\n * candidates, full campaign artifacts, all judge scores. Useful for\n * debugging or reporting beyond the summary.\n */\n raw: RunImprovementLoopResult<TArtifact, TScenario>\n}\n\n/**\n * Deterministic train/holdout split by a stable hash of `scenario.id`,\n * so the same scenario set always splits the same way across runs.\n */\nfunction splitTrainHoldout<TScenario extends Scenario>(\n scenarios: TScenario[],\n fraction: number,\n): { train: TScenario[]; holdout: TScenario[] } {\n // Stable fnv-1a-ish hash of the id for ordering.\n function hash(s: string): number {\n let h = 2166136261 >>> 0\n for (let i = 0; i < s.length; i++) {\n h ^= s.charCodeAt(i)\n h = Math.imul(h, 16777619) >>> 0\n }\n return h\n }\n const sorted = [...scenarios].sort((a, b) => hash(a.id) - hash(b.id))\n const nHoldout = Math.max(1, Math.min(sorted.length - 1, Math.round(sorted.length * fraction)))\n return {\n holdout: sorted.slice(0, nHoldout),\n train: sorted.slice(nHoldout),\n }\n}\n\nfunction meanComposite(byScenario: Record<string, { meanComposite: number }>): {\n compositeMean: number\n perScenario: Record<string, number>\n} {\n const perScenario: Record<string, number> = {}\n const values: number[] = []\n for (const [id, agg] of Object.entries(byScenario)) {\n perScenario[id] = agg.meanComposite\n values.push(agg.meanComposite)\n }\n return {\n compositeMean: values.length === 0 ? 0 : values.reduce((s, v) => s + v, 0) / values.length,\n perScenario,\n }\n}\n\nconst DEFAULT_MUTATION_PRIMITIVES = [\n 'Tighten the hook: lead with the specific user outcome.',\n 'Replace generic adjectives with specific verbs or proof numbers.',\n \"Anchor every claim in something the scenario's brief literally supports.\",\n 'Honor the surface-shape constraint (length, register, audience vocabulary).',\n]\n\n/**\n * One-shot self-improvement loop. See module docstring for defaults +\n * extension points.\n *\n * @example Minimum:\n *\n * const result = await selfImprove({\n * agent: (surface, scenario, ctx) => myAgent(surface, scenario, ctx.signal),\n * scenarios,\n * judge,\n * baselineSurface: DEFAULT_PROMPT,\n * })\n * console.log(`lift: ${result.lift.toFixed(3)} (${result.gateDecision})`)\n *\n * @example Distributed (workers in three regions):\n *\n * await selfImprove({\n * agent: httpDispatch({ resolveUrl: ({ placement }) => REGION_URLS[placement!] }),\n * scenarios,\n * judge,\n * baselineSurface: DEFAULT_PROMPT,\n * cellPlacement: ({ scenario }) => scenario.region,\n * budget: { maxConcurrency: 12 },\n * })\n */\nexport async function selfImprove<TScenario extends Scenario, TArtifact>(\n opts: SelfImproveOptions<TScenario, TArtifact>,\n): Promise<SelfImproveResult<TScenario, TArtifact>> {\n const startedAt = Date.now()\n\n const budget = opts.budget ?? {}\n const generations = budget.generations ?? 3\n const populationSize = budget.populationSize ?? 2\n const maxConcurrency = budget.maxConcurrency ?? 2\n const holdoutFraction = budget.holdoutFraction ?? 0.25\n const costCeiling = budget.dollars\n const expectUsage = opts.expectUsage ?? 'assert'\n\n const explicitHoldout = budget.holdoutScenarios\n const { train, holdout } = explicitHoldout\n ? {\n train: opts.scenarios.filter((s) => !explicitHoldout.some((h) => h.id === s.id)),\n holdout: explicitHoldout as TScenario[],\n }\n : splitTrainHoldout(opts.scenarios, holdoutFraction)\n\n if (train.length === 0) {\n throw new Error(\n 'selfImprove: train split is empty. Reduce holdoutFraction or pass more scenarios.',\n )\n }\n if (holdout.length === 0) {\n throw new Error('selfImprove: holdout split is empty. Pass more scenarios.')\n }\n\n const proposer: SurfaceProposer =\n opts.proposer ??\n gepaProposer({\n llm: {\n baseUrl: opts.llm?.baseUrl ?? 'https://router.tangle.tools/v1',\n apiKey: opts.llm?.apiKey ?? process.env.OPENAI_API_KEY ?? '',\n },\n model: opts.llm?.model ?? 'anthropic/claude-sonnet-4.6',\n target:\n opts.proposerTarget ??\n 'agent surface (system prompt or config) being optimized by selfImprove',\n mutationPrimitives: opts.mutationPrimitives ?? DEFAULT_MUTATION_PRIMITIVES,\n })\n\n const gate: Gate<TArtifact, TScenario> =\n opts.gate ??\n defaultProductionGate<TArtifact, TScenario>({\n holdoutScenarios: holdout,\n deltaThreshold: 0.05,\n })\n\n // Durable by default: a real (non-`mem://`) runDir means the caller wants\n // persistence, so default to fs storage — the provenance record + spans\n // survive the call. A `mem://` runDir (or none) stays in-memory. An explicit\n // `storage` always wins (the opt-out path for tests / edge runtimes).\n const runDir = opts.runDir ?? `mem://selfImprove-${startedAt}`\n const isMemRunDir = runDir.startsWith('mem://')\n const storage = opts.storage ?? (isMemRunDir ? inMemoryCampaignStorage() : fsCampaignStorage())\n\n if (opts.onProgress) {\n opts.onProgress({ kind: 'baseline.started', scenarios: opts.scenarios.length })\n }\n\n const result = await runImprovementLoop<TScenario, TArtifact>({\n scenarios: train,\n baselineSurface: opts.baselineSurface,\n dispatchWithSurface: opts.agent,\n proposer,\n judges: [opts.judge],\n populationSize,\n maxGenerations: generations,\n promoteTopK: budget.promoteTopK,\n reps: budget.reps,\n holdoutScenarios: holdout,\n gate,\n autoOnPromote: opts.autoOnPromote ?? 'none',\n ghOwner: opts.ghOwner,\n ghRepo: opts.ghRepo,\n storage,\n runDir,\n maxConcurrency,\n cellPlacement: opts.cellPlacement,\n costCeiling,\n expectUsage,\n labeledStore: opts.labeledStore,\n captureSource: opts.captureSource,\n analyzeGeneration: opts.analyzeGeneration,\n findings: opts.findings,\n })\n\n const baseline = meanComposite(result.baselineOnHoldout.aggregates.byScenario)\n const winnerStats = meanComposite(result.winnerOnHoldout.aggregates.byScenario)\n\n if (opts.onProgress) {\n opts.onProgress({\n kind: 'baseline.completed',\n compositeMean: baseline.compositeMean,\n durationMs: Date.now() - startedAt,\n })\n opts.onProgress({\n kind: 'gate.decided',\n decision: result.gateResult.decision,\n lift: winnerStats.compositeMean - baseline.compositeMean,\n })\n }\n\n const totalCost =\n result.baselineCampaign.aggregates.totalCostUsd +\n result.generations.reduce(\n (sum, gen) =>\n sum + gen.surfaces.reduce((s, sf) => s + sf.campaign.aggregates.totalCostUsd, 0),\n 0,\n )\n\n // Rigor packet: feed baseline + winner cells through analyzeRuns().\n // The two candidates (`baseline` / `winner`) give the lift section a\n // clean paired comparison; per-judge / per-dimension / cost-quality\n // sections populate from the cells' judgeScores.\n const insight = await analyzeRuns({\n runs: [\n ...cellsToRunRecords(result.baselineCampaign.cells, 'baseline', runDir, opts.baselineSurface),\n ...cellsToRunRecords(result.winnerOnHoldout.cells, 'winner', runDir, result.winnerSurface),\n ],\n baselineCandidateId: 'baseline',\n candidateCandidateId: 'winner',\n })\n\n // ── Durable provenance: candidate→cell→gate→promote chain + rationale +\n // diff + backend provenance. Always emitted; the +lift recomputes from it.\n const durationMs = Date.now() - startedAt\n const workerRecords =\n opts.collectWorkerRecords?.() ??\n cellsToRunRecords(result.winnerOnHoldout.cells, 'winner', runDir, result.winnerSurface)\n const { record: provenance } = await emitLoopProvenance<TArtifact, TScenario>({\n runId: `${runDir}#${startedAt}`,\n runDir,\n timestamp: new Date(startedAt).toISOString(),\n baselineSurface: opts.baselineSurface,\n winnerSurface: result.winnerSurface,\n winnerLabel: result.winnerLabel,\n winnerRationale: result.winnerRationale,\n diff: result.promotedDiff,\n generations: result.generations.map((g) => ({\n generationIndex: g.record.generationIndex,\n candidates: g.record.candidates,\n promoted: g.record.promoted,\n surfaces: g.surfaces.map((s) => ({ surfaceHash: s.surfaceHash, surface: s.surface })),\n })),\n gate: result.gateResult,\n baselineOnHoldout: result.baselineOnHoldout,\n winnerOnHoldout: result.winnerOnHoldout,\n workerRecords,\n totalCostUsd: totalCost,\n totalDurationMs: durationMs,\n storage,\n hostedClient: opts.hostedTenant ? createHostedClient(opts.hostedTenant) : undefined,\n })\n if (opts.onProvenance) opts.onProvenance(provenance)\n\n const summary: SelfImproveResult<TScenario, TArtifact> = {\n baseline,\n winner: {\n ...winnerStats,\n surface: result.winnerSurface,\n ...(result.winnerLabel ? { label: result.winnerLabel } : {}),\n ...(result.winnerRationale ? { rationale: result.winnerRationale } : {}),\n },\n lift: winnerStats.compositeMean - baseline.compositeMean,\n diff: result.promotedDiff,\n provenance,\n gateDecision: result.gateResult.decision,\n generationsExplored: result.generations.length,\n durationMs,\n totalCostUsd: totalCost,\n insight,\n raw: result,\n }\n\n // Opt-in hosted ingest. Failures are logged but never fail the loop: the\n // local result is always returned.\n if (opts.hostedTenant) {\n try {\n await shipEvalRunToHosted(opts.hostedTenant, opts, summary, result, runDir)\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err)\n // eslint-disable-next-line no-console -- intentional: hosted-ingest is best-effort\n console.warn(`[agent-eval] hosted ingest failed (continuing): ${msg}`)\n }\n }\n\n return summary\n}\n\nasync function shipEvalRunToHosted<TScenario extends Scenario, TArtifact>(\n tenant: HostedTenant,\n opts: SelfImproveOptions<TScenario, TArtifact>,\n summary: SelfImproveResult<TScenario, TArtifact>,\n raw: RunImprovementLoopResult<TArtifact, TScenario>,\n runDir: string,\n): Promise<void> {\n const client = createHostedClient(tenant)\n\n function snapshotFromCampaign(\n index: number,\n surface: MutableSurface | undefined,\n campaign: RunImprovementLoopResult<TArtifact, TScenario>['baselineCampaign'],\n durationMs: number,\n ): EvalRunGenerationSnapshot {\n const cells: EvalRunCellScore[] = campaign.cells.map((cell) => {\n const judgeScores = Object.values(cell.judgeScores)\n const composite =\n judgeScores.length === 0\n ? 0\n : judgeScores.reduce((s, j) => s + j.composite, 0) / judgeScores.length\n return {\n scenarioId: cell.scenarioId,\n rep: cell.rep,\n compositeMean: composite,\n dimensions: Object.fromEntries(\n Object.entries(cell.judgeScores).map(([name, score]) => [name, score.dimensions]),\n ),\n errorMessage: cell.error ?? undefined,\n }\n })\n const compositeMean =\n cells.length === 0 ? 0 : cells.reduce((s, c) => s + c.compositeMean, 0) / cells.length\n return {\n index,\n surfaceHash:\n typeof surface === 'string'\n ? hashString(surface)\n : hashString(JSON.stringify(surface ?? '')),\n surface,\n cells,\n compositeMean,\n costUsd: campaign.aggregates.totalCostUsd,\n durationMs,\n }\n }\n\n const generations: EvalRunGenerationSnapshot[] = []\n // Baseline as generation 0.\n generations.push(snapshotFromCampaign(0, opts.baselineSurface, raw.baselineCampaign, 0))\n // Improvement generations as 1..N. Substrate stores per-surface campaigns\n // per generation — we summarize the WINNING surface per generation here.\n for (const gen of raw.generations) {\n const winner = gen.surfaces.reduce(\n (best, s) =>\n s.campaign.aggregates.cellsExecuted > 0 &&\n (best === undefined || averageComposite(s.campaign) > averageComposite(best.campaign))\n ? s\n : best,\n gen.surfaces[0],\n )\n if (!winner) continue\n generations.push(\n snapshotFromCampaign(gen.record.generationIndex + 1, winner.surface, winner.campaign, 0),\n )\n }\n\n const event: EvalRunEvent = {\n runId: `${runDir}#${Date.now()}`,\n runDir,\n timestamp: new Date().toISOString(),\n status: 'finished',\n labels: opts.hostedLabels ?? {},\n baseline: generations[0],\n generations,\n gateDecision: summary.gateDecision,\n holdoutLift: summary.lift,\n totalCostUsd: summary.totalCostUsd,\n totalDurationMs: summary.durationMs,\n insightReport: summary.insight,\n }\n\n await client.ingestEvalRun(event)\n}\n\nfunction averageComposite(\n campaign: RunImprovementLoopResult<unknown, Scenario>['baselineCampaign'],\n): number {\n const aggs = Object.values(campaign.aggregates.byScenario)\n return aggs.length === 0 ? 0 : aggs.reduce((s, a) => s + a.meanComposite, 0) / aggs.length\n}\n\nfunction hashString(s: string): string {\n let h = 2166136261 >>> 0\n for (let i = 0; i < s.length; i++) {\n h ^= s.charCodeAt(i)\n h = Math.imul(h, 16777619) >>> 0\n }\n return h.toString(16).padStart(8, '0')\n}\n\n/**\n * Adapt campaign cells into the `RunRecord` shape `analyzeRuns()` consumes.\n * Each cell becomes one run; `candidateId` is the caller-supplied label so\n * baseline + winner pair cleanly on `(experimentId, seed)`.\n *\n * `promptHash` is the REAL sha256 content hash of the surface this cell ran\n * (baseline vs winner are byte-distinguishable + byte-identical-verifiable);\n * `configHash` is the sha256 of the candidate label so the two candidates'\n * config rows differ. Both were previously the literal `'sha256:cell'`, which\n * made baseline and winner indistinguishable in every downstream record.\n */\nfunction cellsToRunRecords<TArtifact>(\n cells: ReadonlyArray<CampaignCellResult<TArtifact>>,\n candidateId: 'baseline' | 'winner',\n runId: string,\n surface: MutableSurface,\n): RunRecord[] {\n const promptHash = surfaceContentHash(surface)\n const configHash = `sha256:${createHash('sha256').update(candidateId).digest('hex')}`\n return cells.map((cell) => {\n const perJudge: Record<string, Record<string, number>> = {}\n const perDimMeanAccum: Record<string, { sum: number; n: number }> = {}\n let compositeSum = 0\n let compositeCount = 0\n for (const [judgeId, score] of Object.entries(cell.judgeScores)) {\n perJudge[judgeId] = { ...score.dimensions }\n for (const [dim, value] of Object.entries(score.dimensions)) {\n if (!Number.isFinite(value)) continue\n const accum = perDimMeanAccum[dim] ?? { sum: 0, n: 0 }\n accum.sum += value\n accum.n += 1\n perDimMeanAccum[dim] = accum\n }\n if (Number.isFinite(score.composite)) {\n compositeSum += score.composite\n compositeCount += 1\n }\n }\n const perDimMean: Record<string, number> = {}\n for (const [dim, { sum, n }] of Object.entries(perDimMeanAccum)) {\n perDimMean[dim] = n === 0 ? 0 : sum / n\n }\n const composite = compositeCount === 0 ? 0 : compositeSum / compositeCount\n const judgeScores: JudgeScoresRecord = {\n perJudge,\n perDimMean,\n composite,\n }\n return {\n runId: `${runId}::${candidateId}::${cell.cellId}`,\n experimentId: runId,\n candidateId,\n // Pair on (scenarioId, rep) — analyzeRuns pairs on (experimentId, seed).\n // Synthesize a stable seed for that pairing.\n seed:\n cell.rep * 1_000_000 +\n hashString(cell.scenarioId)\n .slice(0, 6)\n .split('')\n .reduce((a, c) => (a * 31 + c.charCodeAt(0)) >>> 0, 0),\n model: 'campaign-cell',\n promptHash,\n configHash,\n commitSha: 'cell',\n wallMs: cell.durationMs,\n costUsd: cell.costUsd,\n tokenUsage: { input: 0, output: 0 },\n outcome: {\n holdoutScore: composite,\n raw: {},\n judgeScores,\n },\n splitTag: 'holdout',\n ...(cell.error ? { failureMode: cell.error } : {}),\n } satisfies RunRecord\n })\n}\n","import type { RunEvalOptions } from '../campaign/presets/run-eval'\nimport { runEval } from '../campaign/presets/run-eval'\nimport { inMemoryCampaignStorage } from '../campaign/storage'\nimport type {\n CampaignResult,\n DispatchContext,\n JudgeConfig,\n MutableSurface,\n Scenario,\n} from '../campaign/types'\nimport type { HostedTenant } from '../hosted/client'\nimport {\n type SelfImproveBudget,\n type SelfImproveLlm,\n type SelfImproveOptions,\n type SelfImproveResult,\n selfImprove,\n} from './self-improve'\n\nexport type AgentEvalAgent<TScenario extends Scenario, TArtifact> = (\n surface: MutableSurface,\n scenario: TScenario,\n ctx: DispatchContext,\n) => Promise<TArtifact>\n\nexport type DefineAgentEvalOptions<TScenario extends Scenario, TArtifact> = SelfImproveOptions<\n TScenario,\n TArtifact\n>\n\nexport interface AgentEvalEvaluateOptions<TScenario extends Scenario, TArtifact>\n extends Omit<\n RunEvalOptions<TScenario, TArtifact>,\n 'dispatch' | 'judges' | 'runDir' | 'scenarios'\n > {\n /** Scenario set to evaluate. Defaults to the scenarios passed to `defineAgentEval`. */\n scenarios?: TScenario[]\n /** Surface to evaluate. Defaults to the baseline surface passed to `defineAgentEval`. */\n surface?: MutableSurface\n /** Agent to evaluate. Defaults to the agent passed to `defineAgentEval`. */\n agent?: AgentEvalAgent<TScenario, TArtifact>\n /** Single judge override. Ignored when `judges` is set. */\n judge?: JudgeConfig<TArtifact, TScenario>\n /** Full judge list override. Defaults to the single judge passed to `defineAgentEval`. */\n judges?: JudgeConfig<TArtifact, TScenario>[]\n /** Logical or filesystem run directory. Defaults to an in-memory run. */\n runDir?: string\n}\n\nexport type AgentEvalImproveOptions<TScenario extends Scenario, TArtifact> = Omit<\n Partial<SelfImproveOptions<TScenario, TArtifact>>,\n 'budget' | 'hostedTenant' | 'llm'\n> & {\n budget?: Partial<SelfImproveBudget>\n hostedTenant?: Partial<HostedTenant>\n llm?: Partial<SelfImproveLlm>\n}\n\nexport interface DefinedAgentEval<TScenario extends Scenario, TArtifact> {\n /** The default scenarios used by `evaluate()` and `improve()`. */\n readonly scenarios: readonly TScenario[]\n /** The default baseline surface used by `evaluate()` and `improve()`. */\n readonly baselineSurface: MutableSurface\n /**\n * Run one scored evaluation. Use this for a baseline score or to score one\n * candidate surface without running an improvement loop.\n */\n evaluate(\n opts?: AgentEvalEvaluateOptions<TScenario, TArtifact>,\n ): Promise<CampaignResult<TArtifact, TScenario>>\n /**\n * Run the closed improvement loop. Per-call overrides replace the definition\n * except for nested config objects (`budget`, `llm`, `hostedTenant`), which\n * are merged field-by-field so callers can override one knob without\n * repeating secrets or budget defaults.\n */\n improve(\n opts?: AgentEvalImproveOptions<TScenario, TArtifact>,\n ): Promise<SelfImproveResult<TScenario, TArtifact>>\n}\n\n/**\n * Define an agent eval once, then either score a surface with `evaluate()` or\n * run the closed loop with `improve()`.\n *\n * This is a DX wrapper only: it delegates to `runEval()` and `selfImprove()` and\n * returns their native result shapes.\n */\nexport function defineAgentEval<TScenario extends Scenario, TArtifact>(\n defaults: DefineAgentEvalOptions<TScenario, TArtifact>,\n): DefinedAgentEval<TScenario, TArtifact> {\n const defaultEvaluateOptions = evaluateDefaults(defaults)\n\n return {\n scenarios: defaults.scenarios,\n baselineSurface: defaults.baselineSurface,\n\n async evaluate(opts = {}) {\n const { agent, judge, judges, runDir, scenarios, surface, ...campaignOpts } = opts\n const selectedAgent = agent ?? defaults.agent\n const selectedSurface = surface ?? defaults.baselineSurface\n const selectedRunDir = runDir ?? defaults.runDir ?? `mem://defineAgentEval-${Date.now()}`\n const selectedStorage =\n campaignOpts.storage ??\n defaultEvaluateOptions.storage ??\n (selectedRunDir.startsWith('mem://') ? inMemoryCampaignStorage() : undefined)\n const evalOptions: RunEvalOptions<TScenario, TArtifact> = {\n ...defaultEvaluateOptions,\n ...campaignOpts,\n ...(selectedStorage ? { storage: selectedStorage } : {}),\n runDir: selectedRunDir,\n scenarios: scenarios ?? defaults.scenarios,\n dispatch: (scenario, ctx) => selectedAgent(selectedSurface, scenario, ctx),\n judges: evaluateJudges(judges, judge ?? defaults.judge),\n }\n if (evalOptions.reps !== undefined)\n evalOptions.reps = requirePositiveInteger(evalOptions.reps, 'reps')\n return runEval<TScenario, TArtifact>(evalOptions)\n },\n\n async improve(opts = {}) {\n const {\n budget: budgetOverride,\n hostedTenant: hostedTenantOverride,\n llm: llmOverride,\n ...topLevelOverrides\n } = opts\n const merged = mergeDefined(defaults, topLevelOverrides)\n const budget = mergeBudget(defaults.budget, budgetOverride)\n const llm = mergeLlm(defaults.llm, llmOverride)\n const hostedTenant = mergeHostedTenant(defaults.hostedTenant, hostedTenantOverride)\n return selfImprove<TScenario, TArtifact>({\n ...merged,\n ...(budget ? { budget } : {}),\n ...(llm ? { llm } : {}),\n ...(hostedTenant ? { hostedTenant } : {}),\n })\n },\n }\n}\n\ntype SharedEvaluateDefaults<TScenario extends Scenario, TArtifact> = Omit<\n RunEvalOptions<TScenario, TArtifact>,\n 'dispatch' | 'judges' | 'runDir' | 'scenarios'\n>\n\nfunction evaluateDefaults<TScenario extends Scenario, TArtifact>(\n defaults: DefineAgentEvalOptions<TScenario, TArtifact>,\n): SharedEvaluateDefaults<TScenario, TArtifact> {\n const out: SharedEvaluateDefaults<TScenario, TArtifact> = {}\n if (defaults.storage) out.storage = defaults.storage\n if (defaults.labeledStore) out.labeledStore = defaults.labeledStore\n if (defaults.captureSource) out.captureSource = defaults.captureSource\n if (defaults.cellPlacement) out.cellPlacement = defaults.cellPlacement\n if (defaults.expectUsage) out.expectUsage = defaults.expectUsage\n if (defaults.budget?.dollars !== undefined) out.costCeiling = defaults.budget.dollars\n if (defaults.budget?.maxConcurrency !== undefined)\n out.maxConcurrency = defaults.budget.maxConcurrency\n if (defaults.budget?.reps !== undefined)\n out.reps = requirePositiveInteger(defaults.budget.reps, 'budget.reps')\n return out\n}\n\nfunction mergeBudget(\n defaults: SelfImproveBudget | undefined,\n overrides: Partial<SelfImproveBudget> | undefined,\n): SelfImproveBudget | undefined {\n const merged = mergeOptionalObject(defaults, overrides)\n if (merged?.reps !== undefined) merged.reps = requirePositiveInteger(merged.reps, 'budget.reps')\n return merged\n}\n\nfunction mergeLlm(\n defaults: SelfImproveLlm | undefined,\n overrides: Partial<SelfImproveLlm> | undefined,\n): SelfImproveLlm | undefined {\n return mergeOptionalObject(defaults, overrides)\n}\n\nfunction mergeHostedTenant(\n defaults: HostedTenant | undefined,\n overrides: Partial<HostedTenant> | undefined,\n): HostedTenant | undefined {\n const merged = mergeOptionalObject(defaults, overrides)\n if (!merged) return undefined\n if (!merged.endpoint?.trim() || !merged.apiKey?.trim() || !merged.tenantId?.trim()) {\n throw new Error(\n 'defineAgentEval.improve: hostedTenant requires endpoint, apiKey, and tenantId after merging defaults and overrides',\n )\n }\n return merged\n}\n\nfunction mergeDefined<T extends object>(defaults: T, overrides: Partial<T> | undefined): T {\n if (!overrides) return defaults\n const merged = { ...defaults } as Record<string, unknown>\n for (const [key, value] of Object.entries(overrides)) {\n if (value !== undefined) merged[key] = value\n }\n return merged as T\n}\n\nfunction mergeOptionalObject<T extends object>(\n defaults: T | undefined,\n overrides: Partial<T> | undefined,\n): T | undefined {\n if (!defaults && !overrides) return undefined\n return mergeDefined(defaults ?? ({} as T), overrides)\n}\n\nfunction evaluateJudges<TArtifact, TScenario extends Scenario>(\n judges: JudgeConfig<TArtifact, TScenario>[] | undefined,\n defaultJudge: JudgeConfig<TArtifact, TScenario>,\n): JudgeConfig<TArtifact, TScenario>[] {\n if (judges !== undefined) {\n if (judges.length === 0) {\n throw new Error('defineAgentEval.evaluate: judges must not be empty')\n }\n return judges\n }\n return [defaultJudge]\n}\n\nfunction requirePositiveInteger(value: number, field: string): number {\n if (!Number.isInteger(value) || value < 1) {\n throw new Error(`defineAgentEval: ${field} must be a positive integer`)\n }\n return value\n}\n","/**\n * # `@tangle-network/agent-eval/contract` — eval-run diff primitive.\n *\n * The substrate side of the v-N-versus-v-N+1 dashboard view. Given two\n * `EvalRunEvent`s (or two `EvalRunGenerationSnapshot`s from one run), this\n * returns a normalised diff: per-cell composite + per-judge/per-dimension\n * deltas, surface-hash change, aggregate cost + duration shifts.\n *\n * Consumed by:\n * - The hosted-tier dashboard (intelligence-web) — renders v3 vs v4\n * comparisons of cells × judges × dimensions.\n * - CI reporting — emits a \"shipped: composite +0.07, cost +$1.20\" line\n * in PR review for autonomous-improvement runs.\n * - Any downstream consumer that needs \"what actually changed\" without\n * reimplementing the matching + arithmetic.\n *\n * Cells are matched on the natural composite key `(scenarioId, rep)`.\n * Unmatched cells surface as `removed` / `added` so callers can tell\n * \"this cell got worse\" from \"this cell wasn't run.\"\n */\n\nimport type { GateDecision } from '../campaign/types'\nimport type { EvalRunCellScore, EvalRunEvent, EvalRunGenerationSnapshot } from '../hosted/types'\n\n// ── Types ────────────────────────────────────────────────────────────\n\n/** Per-dimension delta. `before` / `after` are null when the judge did not\n * emit a value for that side. `delta` is `after - before`; null when\n * either side is null. */\nexport interface EvalDimensionDelta {\n before: number | null\n after: number | null\n delta: number | null\n}\n\n/** Per-cell delta, keyed on `(scenarioId, rep)`. */\nexport interface EvalCellScoreDelta {\n scenarioId: string\n rep: number\n compositeBefore: number\n compositeAfter: number\n compositeDelta: number\n /** Per-judge → per-dimension deltas. Outer key = judge name from\n * `EvalRunCellScore.dimensions`; inner key = dimension name. */\n dimensions: Record<string, Record<string, EvalDimensionDelta>>\n}\n\n/** Diff between two generation snapshots — the unit the dashboard renders\n * for a single \"v3 vs v4\" comparison. */\nexport interface EvalGenerationDiff {\n beforeIndex: number\n afterIndex: number\n beforeSurfaceHash: string\n afterSurfaceHash: string\n surfaceChanged: boolean\n /** Cells present in both snapshots, matched on `(scenarioId, rep)`. */\n matched: EvalCellScoreDelta[]\n /** Cells present in `before` but missing from `after`. */\n removed: EvalRunCellScore[]\n /** Cells present in `after` but missing from `before`. */\n added: EvalRunCellScore[]\n /** Aggregate composite mean across all cells in the snapshot. */\n compositeBefore: number\n compositeAfter: number\n compositeDelta: number\n costUsdBefore: number\n costUsdAfter: number\n costUsdDelta: number\n durationMsBefore: number\n durationMsAfter: number\n durationMsDelta: number\n}\n\n/** Diff between two full eval-runs. Includes both baseline-vs-baseline and\n * winner-vs-winner generation diffs when both sides expose them, plus\n * run-level metadata. */\nexport interface EvalRunDiff {\n beforeRunId: string\n afterRunId: string\n beforeTimestamp: string\n afterTimestamp: string\n beforeGateDecision: GateDecision | null\n afterGateDecision: GateDecision | null\n beforeHoldoutLift: number | null\n afterHoldoutLift: number | null\n holdoutLiftDelta: number | null\n beforeTotalCostUsd: number\n afterTotalCostUsd: number\n totalCostUsdDelta: number\n beforeTotalDurationMs: number\n afterTotalDurationMs: number\n totalDurationMsDelta: number\n /** Baseline-vs-baseline diff. Null when either run has no baseline. */\n baselineDiff: EvalGenerationDiff | null\n /** Highest-index-generation comparison. Null when either run has no\n * recorded generations (e.g. baseline-only or errored before any\n * generation completed). */\n winnersDiff: EvalGenerationDiff | null\n}\n\n// ── Implementation ───────────────────────────────────────────────────\n\nfunction keyForCell(cell: EvalRunCellScore): string {\n // JSON-tuple key — `scenarioId` may legitimately contain `::` or any other\n // delimiter, so we use JSON.stringify on a 2-tuple to get an unambiguous,\n // collision-free composite key.\n return JSON.stringify([cell.scenarioId, cell.rep])\n}\n\n/** Build the per-dimension delta map for a matched cell. Each judge name +\n * dimension name encountered on EITHER side appears in the result. */\nfunction diffDimensions(\n before: EvalRunCellScore['dimensions'],\n after: EvalRunCellScore['dimensions'],\n): EvalCellScoreDelta['dimensions'] {\n const out: EvalCellScoreDelta['dimensions'] = {}\n const judges = new Set<string>([...Object.keys(before), ...Object.keys(after)])\n for (const judge of judges) {\n const beforeDims = before[judge] ?? {}\n const afterDims = after[judge] ?? {}\n const dims = new Set<string>([...Object.keys(beforeDims), ...Object.keys(afterDims)])\n const judgeOut: Record<string, EvalDimensionDelta> = {}\n for (const dim of dims) {\n // Coerce non-finite values (NaN, ±Infinity) to null so the diff never\n // surfaces NaN/Infinity to the dashboard. A NaN score is a substrate\n // bug from upstream; the diff treats it as \"no value\" rather than\n // propagating the corruption.\n const rawBefore = beforeDims[dim]\n const rawAfter = afterDims[dim]\n const b = typeof rawBefore === 'number' && Number.isFinite(rawBefore) ? rawBefore : null\n const a = typeof rawAfter === 'number' && Number.isFinite(rawAfter) ? rawAfter : null\n judgeOut[dim] = {\n before: b,\n after: a,\n delta: b !== null && a !== null ? a - b : null,\n }\n }\n out[judge] = judgeOut\n }\n return out\n}\n\n/**\n * Diff two generation snapshots. Cells are matched on `(scenarioId, rep)`;\n * unmatched cells surface in `added` / `removed`. Aggregate fields are\n * recomputed from the snapshot's stored fields, not re-derived from cells —\n * this keeps the diff consistent with whatever aggregation the substrate\n * actually reported.\n */\nexport function diffGenerations(\n before: EvalRunGenerationSnapshot,\n after: EvalRunGenerationSnapshot,\n): EvalGenerationDiff {\n const beforeMap = new Map(before.cells.map((c) => [keyForCell(c), c]))\n const afterMap = new Map(after.cells.map((c) => [keyForCell(c), c]))\n\n const matched: EvalCellScoreDelta[] = []\n const removed: EvalRunCellScore[] = []\n const added: EvalRunCellScore[] = []\n\n for (const [key, beforeCell] of beforeMap) {\n const afterCell = afterMap.get(key)\n if (!afterCell) {\n removed.push(beforeCell)\n continue\n }\n matched.push({\n scenarioId: beforeCell.scenarioId,\n rep: beforeCell.rep,\n compositeBefore: beforeCell.compositeMean,\n compositeAfter: afterCell.compositeMean,\n compositeDelta: afterCell.compositeMean - beforeCell.compositeMean,\n dimensions: diffDimensions(beforeCell.dimensions, afterCell.dimensions),\n })\n }\n for (const [key, afterCell] of afterMap) {\n if (!beforeMap.has(key)) added.push(afterCell)\n }\n\n return {\n beforeIndex: before.index,\n afterIndex: after.index,\n beforeSurfaceHash: before.surfaceHash,\n afterSurfaceHash: after.surfaceHash,\n surfaceChanged: before.surfaceHash !== after.surfaceHash,\n matched,\n removed,\n added,\n compositeBefore: before.compositeMean,\n compositeAfter: after.compositeMean,\n compositeDelta: after.compositeMean - before.compositeMean,\n costUsdBefore: before.costUsd,\n costUsdAfter: after.costUsd,\n costUsdDelta: after.costUsd - before.costUsd,\n durationMsBefore: before.durationMs,\n durationMsAfter: after.durationMs,\n durationMsDelta: after.durationMs - before.durationMs,\n }\n}\n\n/** Highest-index generation, or null if the run recorded none. */\nfunction winnerOf(run: EvalRunEvent): EvalRunGenerationSnapshot | null {\n if (run.generations.length === 0) return null\n let winner = run.generations[0] as EvalRunGenerationSnapshot\n for (const gen of run.generations) {\n if (gen.index > winner.index) winner = gen\n }\n return winner\n}\n\n/**\n * Diff two full eval-runs. Produces baseline-vs-baseline and\n * winner-vs-winner generation diffs when both sides expose them, plus\n * run-level cost / lift / gate-decision deltas.\n */\nexport function diffRuns(before: EvalRunEvent, after: EvalRunEvent): EvalRunDiff {\n const beforeWinner = winnerOf(before)\n const afterWinner = winnerOf(after)\n const baselineDiff =\n before.baseline && after.baseline ? diffGenerations(before.baseline, after.baseline) : null\n const winnersDiff =\n beforeWinner && afterWinner ? diffGenerations(beforeWinner, afterWinner) : null\n\n const beforeLift = before.holdoutLift ?? null\n const afterLift = after.holdoutLift ?? null\n\n return {\n beforeRunId: before.runId,\n afterRunId: after.runId,\n beforeTimestamp: before.timestamp,\n afterTimestamp: after.timestamp,\n beforeGateDecision: before.gateDecision ?? null,\n afterGateDecision: after.gateDecision ?? null,\n beforeHoldoutLift: beforeLift,\n afterHoldoutLift: afterLift,\n holdoutLiftDelta: beforeLift !== null && afterLift !== null ? afterLift - beforeLift : null,\n beforeTotalCostUsd: before.totalCostUsd,\n afterTotalCostUsd: after.totalCostUsd,\n totalCostUsdDelta: after.totalCostUsd - before.totalCostUsd,\n beforeTotalDurationMs: before.totalDurationMs,\n afterTotalDurationMs: after.totalDurationMs,\n totalDurationMsDelta: after.totalDurationMs - before.totalDurationMs,\n baselineDiff,\n winnersDiff,\n }\n}\n\n/**\n * Within-run baseline → winning-generation diff. The natural \"what did the\n * improvement loop produce\" view for a single run. Returns null when the\n * run never reached a generation past baseline (errored early, or the gate\n * shipped the baseline as-is).\n */\nexport function diffRunBaselineToWinner(run: EvalRunEvent): EvalGenerationDiff | null {\n if (!run.baseline) return null\n const winner = winnerOf(run)\n if (!winner || winner.index === run.baseline.index) return null\n return diffGenerations(run.baseline, winner)\n}\n","/**\n * `fromAgentTrace` — provenance correlation from Cursor's Agent Trace spec\n * (https://github.com/cursor/agent-trace, RFC v0.1.0).\n *\n * Agent Trace is NOT a run/quality trace — it carries no outcome, score, or\n * cost. It records *code authorship*: for a VCS revision, which AI model /\n * conversation authored which file ranges. It explicitly disclaims quality\n * assessment — which is exactly what `analyzeRuns` adds.\n *\n * The two layers join on a key the substrate already has: a `RunRecord`\n * carries `commitSha`, and an Agent Trace record is keyed by\n * `vcs.revision`. So this adapter does not produce `RunRecord`s — it builds a\n * provenance index by commit and partitions existing runs by their authoring\n * model. Feed each cohort to `analyzeRuns` (or pass one as `baselineRuns`) to\n * answer the question no run-only trace can: *which authoring agent's code\n * fails / regresses / costs more.*\n *\n * Granularity is commit-level (the SHA join). Per-file/per-line correlation\n * would require runs to record which files they exercised — out of scope.\n */\n\nimport type { RunRecord } from '../../run-record'\n\n// ── Agent Trace record schema (the subset we read) ──────────────────────────\n\nexport type AgentTraceContributorType = 'human' | 'ai' | 'mixed' | 'unknown'\n\nexport interface AgentTraceContributor {\n type: AgentTraceContributorType\n /** models.dev id, e.g. `anthropic/claude-opus-4-5-20251101`. */\n model_id?: string\n}\n\nexport interface AgentTraceRange {\n start_line: number\n end_line: number\n content_hash?: string\n /** Per-range contributor override (agent handoffs). Wins over the\n * conversation-level contributor for these lines. */\n contributor?: AgentTraceContributor\n}\n\nexport interface AgentTraceConversation {\n url?: string\n contributor?: AgentTraceContributor\n ranges: AgentTraceRange[]\n}\n\nexport interface AgentTraceFile {\n path: string\n conversations: AgentTraceConversation[]\n}\n\nexport interface AgentTraceRecord {\n version: string\n id: string\n timestamp: string\n vcs?: { type: string; revision: string }\n tool?: { name?: string; version?: string }\n files: AgentTraceFile[]\n}\n\n// ── Provenance index ─────────────────────────────────────────────────────────\n\n/** Authorship provenance for one VCS revision, aggregated across the record's\n * files/conversations/ranges. */\nexport interface AuthoringProvenance {\n commitSha: string\n /** Unique AI model ids that authored code in this commit (type ai|mixed). */\n aiModels: string[]\n /** Tools that produced the records (e.g. `cursor`). */\n tools: string[]\n conversationCount: number\n fileCount: number\n /** Total attributed lines (sum of range spans). */\n lineCount: number\n /** True if any range was authored (in whole or part) by a human. */\n humanInvolved: boolean\n}\n\nexport type AgentTraceIndex = Map<string, AuthoringProvenance>\n\nfunction rangeLines(r: AgentTraceRange): number {\n return Math.max(0, r.end_line - r.start_line + 1)\n}\n\n/**\n * Build a commit → provenance index from Agent Trace records. Multiple records\n * for the same revision are merged. Records without `vcs.revision` are skipped\n * (the SHA is the join key — without it there is nothing to correlate against).\n */\nexport function parseAgentTrace(records: AgentTraceRecord[]): AgentTraceIndex {\n interface Acc {\n models: Set<string>\n tools: Set<string>\n files: Set<string>\n conversationCount: number\n lineCount: number\n humanInvolved: boolean\n }\n const acc = new Map<string, Acc>()\n\n for (const record of records) {\n const sha = record.vcs?.revision\n if (!sha) continue // the SHA is the join key — nothing to correlate without it\n\n let a = acc.get(sha)\n if (!a) {\n a = {\n models: new Set(),\n tools: new Set(),\n files: new Set(),\n conversationCount: 0,\n lineCount: 0,\n humanInvolved: false,\n }\n acc.set(sha, a)\n }\n\n if (record.tool?.name) a.tools.add(record.tool.name)\n\n for (const file of record.files ?? []) {\n a.files.add(file.path)\n for (const conv of file.conversations ?? []) {\n a.conversationCount += 1\n for (const range of conv.ranges ?? []) {\n // Per-range contributor wins, else the conversation contributor.\n const contributor = range.contributor ?? conv.contributor\n a.lineCount += rangeLines(range)\n if (!contributor) continue\n if (contributor.type === 'human' || contributor.type === 'mixed') {\n a.humanInvolved = true\n }\n if ((contributor.type === 'ai' || contributor.type === 'mixed') && contributor.model_id) {\n a.models.add(contributor.model_id)\n }\n }\n }\n }\n }\n\n const index: AgentTraceIndex = new Map()\n for (const [sha, a] of acc) {\n index.set(sha, {\n commitSha: sha,\n aiModels: [...a.models].sort(),\n tools: [...a.tools].sort(),\n conversationCount: a.conversationCount,\n fileCount: a.files.size,\n lineCount: a.lineCount,\n humanInvolved: a.humanInvolved,\n })\n }\n return index\n}\n\n// ── Run ↔ provenance join ──────────────────────────────────────────────────\n\nexport interface PartitionByAuthoringModelResult {\n /** Runs grouped by each AI model that authored code in the run's commit. A\n * run whose commit had multiple authoring models appears under EACH — the\n * cohorts overlap by construction at commit granularity. */\n byModel: Map<string, RunRecord[]>\n /** Runs whose `commitSha` had no Agent Trace provenance (no record, or no\n * AI authorship). Kept separate — never silently folded into a cohort. */\n unattributed: RunRecord[]\n}\n\n/**\n * Partition runs by the AI model(s) that authored the code at each run's\n * `commitSha`. Feed `byModel.get(modelId)` to `analyzeRuns`, or compare two\n * model cohorts via `analyzeRuns({ runs: a, baselineRuns: b })` for a lift CI\n * on \"model A's code vs model B's code\".\n */\nexport function partitionRunsByAuthoringModel(\n runs: RunRecord[],\n index: AgentTraceIndex,\n): PartitionByAuthoringModelResult {\n const byModel = new Map<string, RunRecord[]>()\n const unattributed: RunRecord[] = []\n\n for (const run of runs) {\n const provenance = index.get(run.commitSha)\n if (!provenance || provenance.aiModels.length === 0) {\n unattributed.push(run)\n continue\n }\n for (const model of provenance.aiModels) {\n const cohort = byModel.get(model) ?? []\n cohort.push(run)\n byModel.set(model, cohort)\n }\n }\n\n return { byModel, unattributed }\n}\n","/**\n * # `intake/feedback-table` — multi-rater approve/reject corpus → `RunRecord[]`.\n *\n * The generic shape behind Obsidian's `#approved` / `#rejected` tags, a\n * Google Sheet, a Postgres `feedback` table, or any CSV with ratings.\n *\n * Caller supplies one row per (run, rater) tuple plus per-run metadata; the\n * adapter rolls them up into the substrate-canonical `RunRecord` shape so\n * `analyzeRuns({ runs, raterScores })` can produce inter-rater agreement,\n * disagreement triage, and downstream recommendations.\n *\n * Per-run `RunRecord.outcome.searchScore` is the rater-mean rating\n * (normalised to 0..1 when scale is supplied); `outcome.raw` carries the\n * per-rater scores keyed by rater id for downstream attribution.\n */\n\nimport type { JudgeScoresRecord, RunOutcome, RunRecord, RunSplitTag } from '../../run-record'\n\nexport interface FeedbackTableRow {\n /** Stable id for this run — the unit a rater scored. Drives pairing\n * across analysis primitives. */\n runId: string\n /** Identifier of the rater that produced this rating. */\n rater: string\n /** The rating itself. Accepts boolean (approve/reject), 0..1 scalar,\n * or any numeric scale — see `scale`. */\n rating: number | boolean\n /** Optional metadata carried through to `RunRecord.outcome.raw` and the\n * custom-shape metadata bag. */\n metadata?: Record<string, unknown>\n}\n\nexport interface FeedbackTableMeta {\n runId: string\n /** When omitted, defaults to `'feedback-corpus'`. Used to group related\n * runs in `analyzeRuns()` lift analysis. */\n experimentId?: string\n /** When omitted, defaults to `runId` — each run is its own candidate. */\n candidateId?: string\n /** Cost in USD, when available. Set to 0 when unknown — the consumer's\n * cost analysis sections will collapse gracefully. */\n costUsd?: number\n /** Wall-clock ms, when available. Defaults to 0. */\n wallMs?: number\n /** Model identifier including snapshot. Default `unknown@unknown`. */\n model?: string\n /** Optional sha256 of the prompt; default `'sha256:unknown'`. */\n promptHash?: string\n /** Default `'sha256:unknown'`. */\n configHash?: string\n /** Default `'unknown'`. */\n commitSha?: string\n /** Default `'holdout'` — feedback corpora are by nature the holdout\n * signal a closed-loop improvement aims at. */\n splitTag?: RunSplitTag\n /** Free-form metadata available to consumers via the cast-out path on\n * the resulting RunRecord. */\n extras?: Record<string, unknown>\n}\n\nexport interface FromFeedbackTableOptions {\n /** Per-(run, rater) ratings. */\n ratings: FeedbackTableRow[]\n /** Per-run metadata. When a runId appears in `ratings` but not here, the\n * adapter synthesises minimal metadata with defaults documented above. */\n meta?: FeedbackTableMeta[]\n /** Rating scale. Provide `{ min, max }` for non-0..1 numeric scales.\n * Booleans are normalised: true → 1, false → 0. Default: assumes\n * ratings are already 0..1. */\n scale?: { min: number; max: number }\n /** When true, the rater scores are emitted into `raterScores` (a sibling\n * array `analyzeRuns()` accepts) instead of being averaged into the\n * run's `outcome.searchScore`. Default `true` — preserves rater-level\n * signal for inter-rater analysis. */\n emitRaterScores?: boolean\n}\n\nexport interface FromFeedbackTableResult {\n runs: RunRecord[]\n /** Rater-level scores ready to pass into `analyzeRuns({ raterScores })`\n * for inter-rater agreement + disagreement triage. */\n raterScores: Array<{ runId: string; rater: string; score: number }>\n}\n\nexport function fromFeedbackTable(opts: FromFeedbackTableOptions): FromFeedbackTableResult {\n const { ratings, meta = [], scale, emitRaterScores = true } = opts\n const metaByRun = new Map(meta.map((m) => [m.runId, m]))\n\n // Normalise per-rating to a 0..1 score.\n const normalise = (rating: number | boolean): number => {\n if (typeof rating === 'boolean') return rating ? 1 : 0\n if (!Number.isFinite(rating)) return Number.NaN\n if (!scale) return rating\n const { min, max } = scale\n if (max === min) return rating\n return (rating - min) / (max - min)\n }\n\n // Group ratings by runId.\n const byRun = new Map<string, FeedbackTableRow[]>()\n for (const row of ratings) {\n const list = byRun.get(row.runId) ?? []\n list.push(row)\n byRun.set(row.runId, list)\n }\n\n const runs: RunRecord[] = []\n const raterScores: FromFeedbackTableResult['raterScores'] = []\n\n for (const [runId, rowsForRun] of byRun) {\n const normalised = rowsForRun\n .map((r) => ({ rater: r.rater, score: normalise(r.rating) }))\n .filter((r) => Number.isFinite(r.score))\n if (normalised.length === 0) continue\n\n const meanScore = normalised.reduce((s, r) => s + r.score, 0) / normalised.length\n\n const runMeta = metaByRun.get(runId) ?? ({ runId } as FeedbackTableMeta)\n\n const judgeScores: JudgeScoresRecord = {\n perJudge: Object.fromEntries(normalised.map((r) => [r.rater, { rating: r.score }])),\n perDimMean: { rating: meanScore },\n composite: meanScore,\n }\n\n const outcome: RunOutcome = {\n // Feedback corpora ARE the holdout signal — score lands on\n // `holdoutScore` so downstream substrate primitives (`paretoChart`,\n // promotion gates) read it correctly by default.\n holdoutScore: meanScore,\n raw: Object.fromEntries(normalised.map((r) => [`rater:${r.rater}`, r.score])),\n judgeScores,\n }\n\n runs.push({\n runId,\n experimentId: runMeta.experimentId ?? 'feedback-corpus',\n candidateId: runMeta.candidateId ?? runId,\n seed: 0,\n model: runMeta.model ?? 'unknown@unknown',\n promptHash: runMeta.promptHash ?? 'sha256:unknown',\n configHash: runMeta.configHash ?? 'sha256:unknown',\n commitSha: runMeta.commitSha ?? 'unknown',\n wallMs: runMeta.wallMs ?? 0,\n costUsd: runMeta.costUsd ?? 0,\n tokenUsage: { input: 0, output: 0 },\n outcome,\n splitTag: runMeta.splitTag ?? 'holdout',\n } as RunRecord)\n\n if (emitRaterScores) {\n for (const r of normalised) raterScores.push({ runId, rater: r.rater, score: r.score })\n }\n }\n\n return { runs, raterScores }\n}\n","/**\n * # `intake/otel-spans` — OTel `TraceSpanEvent[]` → `RunRecord[]`.\n *\n * Turns an existing observability stream into the substrate-canonical\n * `RunRecord` shape so consumers with logs but no eval discipline can\n * call `analyzeRuns()` against their production traffic immediately.\n *\n * Pivot rule: spans are grouped by `tangle.runId` (the same attribute the\n * hosted-tier wire format uses) or, when absent, by `traceId`. One group\n * becomes one `RunRecord`. The root span (no `parentSpanId`) supplies:\n *\n * - `runId` (the group key)\n * - `wallMs` from `endTimeUnixNano - startTimeUnixNano`\n * - `model` from `gen_ai.request.model` / `llm.model` / `tangle.model`\n * - cost from `cost.usd` / `gen_ai.usage.cost_usd` / `tangle.cost.usd`\n * - token usage from `gen_ai.usage.{input,output}_tokens`\n * - `outcome.searchScore` from `tangle.score` / `eval.score` when\n * present; `outcome.raw` collects every numeric attribute.\n *\n * Spans that ERRORed (`status.code === 'ERROR'`) populate `failureMode`\n * with their `name` so `analyzeRuns()`'s failure clustering sees them.\n */\n\nimport type { TraceSpanEvent } from '../../hosted/types'\nimport type {\n JudgeScoresRecord,\n RunOutcome,\n RunRecord,\n RunSplitTag,\n RunTokenUsage,\n} from '../../run-record'\nimport {\n LLM_INPUT_TOKEN_ATTR_KEYS,\n LLM_MODEL_ATTR_KEYS,\n LLM_OUTPUT_TOKEN_ATTR_KEYS,\n} from '../../trace/otlp-attributes'\n\nconst SCORE_KEYS = ['tangle.score', 'eval.score', 'score']\nconst MODEL_KEYS = ['tangle.model', ...LLM_MODEL_ATTR_KEYS, 'model']\nconst COST_KEYS = ['tangle.cost.usd', 'gen_ai.usage.cost_usd', 'cost.usd', 'cost']\nconst INPUT_TOKEN_KEYS = [...LLM_INPUT_TOKEN_ATTR_KEYS, 'tangle.tokens.in', 'tokens.in']\nconst OUTPUT_TOKEN_KEYS = [...LLM_OUTPUT_TOKEN_ATTR_KEYS, 'tangle.tokens.out', 'tokens.out']\nconst PROMPT_HASH_KEYS = ['tangle.prompt_hash', 'prompt.hash']\nconst CONFIG_HASH_KEYS = ['tangle.config_hash', 'config.hash']\n\nexport interface FromOtelSpansOptions {\n spans: TraceSpanEvent[]\n /** Default split tag for synthesized records. Defaults to `'holdout'`. */\n defaultSplit?: RunSplitTag\n /** Default `experimentId` when not present on any span. */\n experimentId?: string\n}\n\nexport function fromOtelSpans(opts: FromOtelSpansOptions): RunRecord[] {\n const { spans, defaultSplit = 'holdout', experimentId = 'otel-corpus' } = opts\n const grouped = groupSpans(spans)\n\n const runs: RunRecord[] = []\n for (const [groupKey, groupSpans] of grouped) {\n const root = findRoot(groupSpans)\n if (!root) continue\n\n const wallMs = Math.max(0, (root.endTimeUnixNano - root.startTimeUnixNano) / 1_000_000)\n const model = readAttrString(groupSpans, MODEL_KEYS) ?? 'unknown@unknown'\n const costUsd = readAttrNumber(groupSpans, COST_KEYS) ?? 0\n const inputTokens = readAttrNumber(groupSpans, INPUT_TOKEN_KEYS) ?? 0\n const outputTokens = readAttrNumber(groupSpans, OUTPUT_TOKEN_KEYS) ?? 0\n const promptHash = readAttrString(groupSpans, PROMPT_HASH_KEYS) ?? 'sha256:unknown'\n const configHash = readAttrString(groupSpans, CONFIG_HASH_KEYS) ?? 'sha256:unknown'\n const score = readAttrNumber(groupSpans, SCORE_KEYS)\n\n const rawNumeric = collectNumericAttrs(groupSpans)\n const tokenUsage: RunTokenUsage = {\n input: inputTokens,\n output: outputTokens,\n }\n\n const judgeScores: JudgeScoresRecord | undefined =\n score !== undefined\n ? {\n perJudge: { 'otel-derived': { score } },\n perDimMean: { score },\n composite: score,\n }\n : undefined\n\n const errorSpan = groupSpans.find((s) => s.status?.code === 'ERROR')\n const outcome: RunOutcome = {\n ...(opts.defaultSplit === 'search' ? { searchScore: score } : { holdoutScore: score }),\n raw: rawNumeric,\n ...(judgeScores ? { judgeScores } : {}),\n }\n\n runs.push({\n runId: groupKey,\n experimentId,\n candidateId: (root.attributes['tangle.candidateId'] as string | undefined) ?? 'otel-default',\n seed: 0,\n model,\n promptHash,\n configHash,\n commitSha: (root.attributes['tangle.commit_sha'] as string | undefined) ?? 'unknown',\n wallMs,\n costUsd,\n tokenUsage,\n outcome,\n splitTag: defaultSplit,\n ...(errorSpan ? { failureMode: errorSpan.name } : {}),\n } as RunRecord)\n }\n return runs\n}\n\n// ── Internal helpers ────────────────────────────────────────────────\n\nfunction groupSpans(spans: TraceSpanEvent[]): Map<string, TraceSpanEvent[]> {\n const m = new Map<string, TraceSpanEvent[]>()\n for (const span of spans) {\n const key = (span['tangle.runId'] as string | undefined) ?? span.traceId\n const list = m.get(key) ?? []\n list.push(span)\n m.set(key, list)\n }\n return m\n}\n\nfunction findRoot(group: TraceSpanEvent[]): TraceSpanEvent | undefined {\n return group.find((s) => !s.parentSpanId) ?? group[0]\n}\n\nfunction readAttrString(spans: TraceSpanEvent[], keys: string[]): string | undefined {\n for (const span of spans) {\n for (const key of keys) {\n const v = span.attributes[key]\n if (typeof v === 'string' && v.length > 0) return v\n }\n }\n return undefined\n}\n\nfunction readAttrNumber(spans: TraceSpanEvent[], keys: string[]): number | undefined {\n for (const span of spans) {\n for (const key of keys) {\n const v = span.attributes[key]\n if (typeof v === 'number' && Number.isFinite(v)) return v\n if (typeof v === 'string') {\n const parsed = Number(v)\n if (Number.isFinite(parsed)) return parsed\n }\n }\n }\n return undefined\n}\n\nfunction collectNumericAttrs(spans: TraceSpanEvent[]): Record<string, number> {\n const raw: Record<string, number> = {}\n for (const span of spans) {\n for (const [k, v] of Object.entries(span.attributes)) {\n if (typeof v === 'number' && Number.isFinite(v)) raw[k] = v\n }\n }\n return raw\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAS,kBAAkB;AAoR3B,SAAS,kBACP,WACA,UAC8C;AAE9C,WAAS,KAAK,GAAmB;AAC/B,QAAI,IAAI,eAAe;AACvB,aAAS,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;AACjC,WAAK,EAAE,WAAW,CAAC;AACnB,UAAI,KAAK,KAAK,GAAG,QAAQ,MAAM;AAAA,IACjC;AACA,WAAO;AAAA,EACT;AACA,QAAM,SAAS,CAAC,GAAG,SAAS,EAAE,KAAK,CAAC,GAAG,MAAM,KAAK,EAAE,EAAE,IAAI,KAAK,EAAE,EAAE,CAAC;AACpE,QAAM,WAAW,KAAK,IAAI,GAAG,KAAK,IAAI,OAAO,SAAS,GAAG,KAAK,MAAM,OAAO,SAAS,QAAQ,CAAC,CAAC;AAC9F,SAAO;AAAA,IACL,SAAS,OAAO,MAAM,GAAG,QAAQ;AAAA,IACjC,OAA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