@warmdrift/kgauto-compiler 2.0.0-alpha.19 → 2.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -215,7 +215,11 @@ function passApplyCliffs(ir, profile, estimatedInputTokens) {
215
215
  const mutations = [];
216
216
  const hints = { qualityWarning: [] };
217
217
  let nextIR = ir;
218
+ const sequentialMode = nextIR.constraints?.toolOrchestration === "sequential";
218
219
  for (const cliff of profile.cliffs) {
220
+ if (sequentialMode && cliff.reason.includes("L-040")) {
221
+ continue;
222
+ }
219
223
  let triggered = false;
220
224
  switch (cliff.metric) {
221
225
  case "input_tokens":
@@ -891,8 +895,19 @@ function compile(ir, opts = {}) {
891
895
  cacheableTokens: lowered.diagnostics.cacheableTokens,
892
896
  estimatedCacheSavingsUsd: lowered.diagnostics.estimatedCacheSavingsUsd,
893
897
  historyCacheableTokens: lowered.diagnostics.historyCacheableTokens,
894
- historyTokensTotal: compressed.historyTokensTotal
898
+ historyTokensTotal: compressed.historyTokensTotal,
899
+ // alpha.20 E3: mirror the consumer's declared mode for Glass-Box +
900
+ // brain observability. Undefined when not declared (pre-alpha.20).
901
+ toolOrchestration: ir.constraints?.toolOrchestration
895
902
  };
903
+ if (ir.intent.archetype === "hunt" && ir.constraints?.toolOrchestration === "sequential") {
904
+ accumulatedMutations.push({
905
+ id: "sequential-mode-chain-selected",
906
+ source: "tool_orchestration",
907
+ passName: "compile",
908
+ description: "ir.constraints.toolOrchestration='sequential' selected the DeepSeek-tier-0 hunt chain overlay (L-040 parallel-tool cliff doesn't apply at single-step granularity)."
909
+ });
910
+ }
896
911
  const advisories = runAdvisor(
897
912
  ir,
898
913
  {
@@ -1156,7 +1171,9 @@ function registerCompile(appId, archetype, ir, result) {
1156
1171
  mutationsApplied: result.mutationsApplied.map((m) => m.id),
1157
1172
  startedAt: Date.now(),
1158
1173
  historyCacheableTokens: result.diagnostics.historyCacheableTokens,
1159
- historyTokensTotal: result.diagnostics.historyTokensTotal
1174
+ historyTokensTotal: result.diagnostics.historyTokensTotal,
1175
+ // alpha.20 E3: capture consumer's declared mode for the brain payload.
1176
+ toolOrchestration: result.diagnostics.toolOrchestration
1160
1177
  });
1161
1178
  }
1162
1179
  async function record(input) {
@@ -1169,11 +1186,22 @@ async function record(input) {
1169
1186
  const config = activeConfig;
1170
1187
  const fetchFn = config.fetchImpl ?? fetch;
1171
1188
  const send = async () => {
1189
+ let outcomeId;
1172
1190
  try {
1173
1191
  const res = await fetchFn(`${config.endpoint}/outcomes`, {
1174
1192
  method: "POST",
1175
1193
  headers: {
1176
1194
  "Content-Type": "application/json",
1195
+ // alpha.20: request the inserted row back so we can JOIN advisories
1196
+ // to it via outcome_id. PostgREST returns the row when
1197
+ // `Prefer: return=representation` is set; proxies that pass the
1198
+ // header through (the recommended `const row = { ...body }` shape
1199
+ // from OutcomePayload's forward-compat rule) will surface
1200
+ // the row id. Proxies that don't (legacy / hand-rolled shapes)
1201
+ // simply produce no parseable id → secondary advisory POST is
1202
+ // skipped silently. Best-effort — primary outcome row is the
1203
+ // load-bearing write.
1204
+ Prefer: "return=representation",
1177
1205
  ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
1178
1206
  },
1179
1207
  body: JSON.stringify(payload)
@@ -1182,6 +1210,29 @@ async function record(input) {
1182
1210
  const text = await res.text().catch(() => "<no body>");
1183
1211
  throw new Error(`brain ${res.status}: ${text}`);
1184
1212
  }
1213
+ outcomeId = await tryExtractOutcomeId(res);
1214
+ } catch (err) {
1215
+ (config.onError ?? defaultOnError2)(err);
1216
+ return;
1217
+ }
1218
+ const advisories = input.advisories;
1219
+ if (!advisories || advisories.length === 0) return;
1220
+ if (outcomeId === void 0) return;
1221
+ try {
1222
+ const advisoryPayload = advisories.map((a) => buildAdvisoryRow(outcomeId, a));
1223
+ const res = await fetchFn(`${config.endpoint}/compile_outcome_advisories`, {
1224
+ method: "POST",
1225
+ headers: {
1226
+ "Content-Type": "application/json",
1227
+ Prefer: "return=minimal",
1228
+ ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
1229
+ },
1230
+ body: JSON.stringify(advisoryPayload)
1231
+ });
1232
+ if (!res.ok) {
1233
+ const text = await res.text().catch(() => "<no body>");
1234
+ throw new Error(`brain advisories ${res.status}: ${text}`);
1235
+ }
1185
1236
  } catch (err) {
1186
1237
  (config.onError ?? defaultOnError2)(err);
1187
1238
  }
@@ -1231,7 +1282,12 @@ function buildPayload(input, reg) {
1231
1282
  cost_usd_actual: costUsdActual,
1232
1283
  ttft_ms: input.ttftMs,
1233
1284
  history_cacheable_tokens: reg?.historyCacheableTokens,
1234
- history_tokens_at_compile: reg?.historyTokensTotal
1285
+ history_tokens_at_compile: reg?.historyTokensTotal,
1286
+ // alpha.20 E3: mirror consumer's declared tool-orchestration mode so
1287
+ // the brain can measure per-mode model perf separately (DeepSeek in
1288
+ // sequential vs parallel mode is two different stories — L-040).
1289
+ // Null when consumer hadn't adopted the constraint yet.
1290
+ tool_orchestration: reg?.toolOrchestration ?? null
1235
1291
  };
1236
1292
  }
1237
1293
  function computeCostUsd(modelId, tokensIn, tokensOut) {
@@ -1248,6 +1304,77 @@ function computeCostUsd(modelId, tokensIn, tokensOut) {
1248
1304
  const outUsd = tokensOut / 1e6 * profile.costOutputPer1m;
1249
1305
  return Math.round((inUsd + outUsd) * 1e6) / 1e6;
1250
1306
  }
1307
+ async function tryExtractOutcomeId(res) {
1308
+ try {
1309
+ const ct = res.headers?.get?.("content-type") ?? "";
1310
+ if (ct && !ct.includes("application/json")) return void 0;
1311
+ if (typeof res.json !== "function") return void 0;
1312
+ const body = await res.json();
1313
+ if (Array.isArray(body) && body.length > 0) {
1314
+ const first = body[0];
1315
+ const id = first?.id;
1316
+ if (typeof id === "number") return id;
1317
+ } else if (body && typeof body === "object") {
1318
+ const id = body.id;
1319
+ if (typeof id === "number") return id;
1320
+ }
1321
+ return void 0;
1322
+ } catch {
1323
+ return void 0;
1324
+ }
1325
+ }
1326
+ function buildAdvisoryRow(outcomeId, a) {
1327
+ return {
1328
+ outcome_id: outcomeId,
1329
+ code: a.code,
1330
+ level: a.level,
1331
+ message: a.message,
1332
+ ...a.recommendationType ? { recommendation_type: a.recommendationType } : {},
1333
+ ...a.suggestion ? { suggestion: a.suggestion } : {},
1334
+ ...a.docsUrl ? { docs_url: a.docsUrl } : {}
1335
+ };
1336
+ }
1337
+ async function recordOutcome(input) {
1338
+ if (!activeConfig) {
1339
+ return { ok: false, reason: "brain_not_configured" };
1340
+ }
1341
+ const config = activeConfig;
1342
+ const fetchFn = config.fetchImpl ?? fetch;
1343
+ const payload = {
1344
+ outcome_id: input.outcomeId,
1345
+ outcome: input.outcome,
1346
+ rating: input.rating ?? null,
1347
+ reason: input.reason ?? null,
1348
+ observed_confidence: input.observedConfidence ?? null
1349
+ };
1350
+ const send = async () => {
1351
+ try {
1352
+ const res = await fetchFn(`${config.endpoint}/compile_outcome_quality`, {
1353
+ method: "POST",
1354
+ headers: {
1355
+ "Content-Type": "application/json",
1356
+ ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
1357
+ },
1358
+ body: JSON.stringify(payload)
1359
+ });
1360
+ if (!res.ok) {
1361
+ const text = await res.text().catch(() => "<no body>");
1362
+ const err = new Error(`brain ${res.status}: ${text}`);
1363
+ (config.onError ?? defaultOnError2)(err);
1364
+ return { ok: false, reason: "persistence_failed" };
1365
+ }
1366
+ return { ok: true };
1367
+ } catch (err) {
1368
+ (config.onError ?? defaultOnError2)(err);
1369
+ return { ok: false, reason: "persistence_failed" };
1370
+ }
1371
+ };
1372
+ if (config.sync) {
1373
+ return send();
1374
+ }
1375
+ void send();
1376
+ return { ok: true };
1377
+ }
1251
1378
 
1252
1379
  // src/ir.ts
1253
1380
  var CallError = class extends Error {
@@ -1574,92 +1701,135 @@ var loadChainsFromBrain = createBrainQueryCache({
1574
1701
  });
1575
1702
 
1576
1703
  // src/fallback.ts
1577
- var STARTER_CHAINS = {
1704
+ var STARTER_CHAINS_GROUNDED = {
1578
1705
  // Reasoning floor — never degrade. Walk UP on 429 to Opus → cross-provider.
1579
- // alpha.16: gpt-5.5 appended as third-provider critique floor (frontier-tier,
1580
- // archetypePerf=9). Cross-provider-tail invariant has somewhere to land when
1581
- // both Anthropic + Google are unreachable (consumer adds only OpenAI key).
1582
1706
  critique: [
1583
- "claude-opus-4-7",
1584
- "claude-sonnet-4-6",
1585
- "gemini-2.5-pro",
1586
- "gpt-5.5"
1707
+ { id: "claude-opus-4-7", grounding: "judgment", reason: "Highest reasoning bar, no degradation tier \u2014 engineer pick, awaiting measured backing" },
1708
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Same-provider walk-down from Opus on 429" },
1709
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor in similar quality bracket" },
1710
+ { id: "gpt-5.5", grounding: "judgment", reason: "alpha.16: third-provider frontier-tier floor (archetypePerf=9)" }
1587
1711
  ],
1588
- // Reasoning matters — Sonnet primary; walk UP to Opus on 429 (rare exception
1589
- // to "always cheaper"); cross-provider via Pro; DeepSeek Pro as tier 3 floor.
1712
+ // Reasoning matters — Sonnet primary; walk UP to Opus on 429.
1590
1713
  plan: [
1591
- "claude-sonnet-4-6",
1592
- "claude-opus-4-7",
1593
- "gemini-2.5-pro",
1594
- "deepseek-v4-pro"
1714
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Reasoning + cost balance \u2014 engineer pick" },
1715
+ { id: "claude-opus-4-7", grounding: "judgment", reason: 'Same-provider walk-UP on 429 (rare exception to "always cheaper")' },
1716
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
1717
+ { id: "deepseek-v4-pro", grounding: "judgment", reason: "Tier 3 cost floor \u2014 no brain evidence yet" }
1595
1718
  ],
1596
- // Quality + cost match. Walk Sonnet → Haiku same-provider, Pro cross,
1597
- // gpt-5.4-mini as third-provider tail (alpha.16 — closes the mono-Anthropic
1598
- // gap when consumer has only ANTHROPIC + OPENAI keys; archetypePerf=7).
1719
+ // Quality + cost match.
1599
1720
  generate: [
1600
- "claude-sonnet-4-6",
1601
- "claude-haiku-4-5",
1602
- "gemini-2.5-pro",
1603
- "gpt-5.4-mini"
1721
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality + cost match \u2014 engineer pick" },
1722
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
1723
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
1724
+ { id: "gpt-5.4-mini", grounding: "judgment", reason: "alpha.16: third-provider tail (archetypePerf=7) \u2014 closes mono-Anthropic gap" }
1604
1725
  ],
1726
+ // ask::sonnet — STARTER_CHAINS calls this "Quality + cost match" but
1727
+ // tt-intel s78 prod data showed 27% empty rate. Labeled 'judgment' until
1728
+ // evidence either validates or refutes the placement.
1605
1729
  ask: [
1606
- "claude-sonnet-4-6",
1607
- "claude-haiku-4-5",
1608
- "gemini-2.5-pro",
1609
- "gpt-5.4-mini"
1730
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality + cost match \u2014 engineer pick. NOTE: tt-intel s78 prod showed 27% empty rate; placement awaits measurement validation" },
1731
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
1732
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
1733
+ { id: "gpt-5.4-mini", grounding: "judgment", reason: "alpha.16: third-provider tail (archetypePerf=7)" }
1610
1734
  ],
1611
- // Structured-output archetype — Flash skipped (alpha.8 MAX_TOKENS cliff),
1612
- // DeepSeek skipped (no brain evidence). Floor at Haiku. alpha.16: gpt-5.4
1613
- // appended as third-provider extract floor (archetypePerf=8, native
1614
- // structured-output support).
1735
+ // Structured-output archetype — Flash skipped (alpha.8 MAX_TOKENS cliff,
1736
+ // capability-fact); DeepSeek skipped (no brain evidence).
1615
1737
  extract: [
1616
- "claude-sonnet-4-6",
1617
- "claude-haiku-4-5",
1618
- "gemini-2.5-pro",
1619
- "gpt-5.4"
1738
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Reliable structured-output anchor \u2014 engineer pick" },
1739
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down with native structured output" },
1740
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor with structured-output support" },
1741
+ { id: "gpt-5.4", grounding: "capability-fact", reason: "alpha.16: third-provider floor \u2014 native structured-output capability (archetypePerf=8)" }
1620
1742
  ],
1621
1743
  // Forgiving archetype — Sonnet primary but Flash safely floors it.
1622
1744
  transform: [
1623
- "claude-sonnet-4-6",
1624
- "claude-haiku-4-5",
1625
- "gemini-2.5-pro",
1626
- "gemini-2.5-flash"
1745
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality anchor \u2014 engineer pick" },
1746
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
1747
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
1748
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost floor \u2014 forgiving archetype tolerates Flash" }
1627
1749
  ],
1628
- // Parallel-tool throughput champion (Flash, L-040). Tier 1 cross-provider
1629
- // Pro; tier 2 Sonnet (quality safety net for blocked-Flash case); tier 3
1630
- // Haiku (reduced tool budget — cliff at 16 fires).
1750
+ // Parallel-tool throughput champion Flash leads on the L-040 cliff
1751
+ // (capability-fact: Flash 15-75 parallel calls/step vs DeepSeek 7-8).
1631
1752
  hunt: [
1632
- "gemini-2.5-flash",
1633
- "gemini-2.5-pro",
1634
- "claude-sonnet-4-6",
1635
- "claude-haiku-4-5"
1753
+ { id: "gemini-2.5-flash", grounding: "capability-fact", reason: "L-040 parallel-tool throughput champion (15-75 calls/step)" },
1754
+ { id: "gemini-2.5-pro", grounding: "capability-fact", reason: "Cross-provider tier 1 with strong parallel-tool support" },
1755
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality safety net for blocked-Flash case" },
1756
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Reduced tool budget \u2014 cliff at 16 fires" }
1636
1757
  ],
1637
- // Cost-sensitive + tolerant. DeepSeek brain-evidence tier 1; Haiku tier 2
1638
- // for quality safety; Flash-Lite emergency floor (onboarded s22).
1758
+ // Cost-sensitive + tolerant. DeepSeek brain-evidence tier 1.
1639
1759
  summarize: [
1640
- "gemini-2.5-flash",
1641
- "deepseek-v4-flash",
1642
- "claude-haiku-4-5",
1643
- "gemini-2.5-flash-lite"
1760
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost-sensitive primary \u2014 engineer pick" },
1761
+ { id: "deepseek-v4-flash", grounding: "measured", reason: "Brain-validated tier 1 for cost-sensitive summarize workloads", n: 169 },
1762
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Quality safety net" },
1763
+ { id: "gemini-2.5-flash-lite", grounding: "judgment", reason: "Emergency floor \u2014 onboarded s22, no brain evidence yet" }
1644
1764
  ],
1645
- // Brain-validated DeepSeek tier 1 (169 rows, 0% empty); Haiku tier 2;
1646
- // Flash-Lite floor for repeat-prompt workloads (cache-discount 10×).
1765
+ // Brain-validated DeepSeek tier 1 (169 rows, 0% empty rate).
1647
1766
  classify: [
1648
- "gemini-2.5-flash",
1649
- "deepseek-v4-flash",
1650
- "claude-haiku-4-5",
1651
- "gemini-2.5-flash-lite"
1767
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost-sensitive primary \u2014 engineer pick" },
1768
+ { id: "deepseek-v4-flash", grounding: "measured", reason: "Brain-validated tier 1 (169 rows, 0% empty rate)", n: 169 },
1769
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Quality safety net" },
1770
+ { id: "gemini-2.5-flash-lite", grounding: "judgment", reason: "Cache-discount 10\xD7 floor for repeat-prompt workloads" }
1652
1771
  ]
1653
1772
  };
1773
+ var STARTER_CHAINS = (() => {
1774
+ const out = {};
1775
+ for (const [archetype, entries] of Object.entries(STARTER_CHAINS_GROUNDED)) {
1776
+ out[archetype] = entries.map((e) => e.id);
1777
+ }
1778
+ return out;
1779
+ })();
1780
+ var STARTER_CHAINS_BY_MODE_GROUNDED = {
1781
+ hunt: {
1782
+ sequential: [
1783
+ {
1784
+ id: "deepseek-v4-pro",
1785
+ grounding: "judgment",
1786
+ reason: "alpha.20 E3: cheap + good reasoning at single-step granularity; L-040 cliff silenced when sequential \u2014 hypothesis not yet measured"
1787
+ },
1788
+ {
1789
+ id: "deepseek-v4-flash",
1790
+ grounding: "judgment",
1791
+ reason: "Cheapest viable; sibling-provider fallback"
1792
+ },
1793
+ {
1794
+ id: "claude-sonnet-4-6",
1795
+ grounding: "judgment",
1796
+ reason: "Cross-provider safety net \u2014 Sonnet handles sequential agentic loops cleanly"
1797
+ },
1798
+ {
1799
+ id: "gemini-2.5-pro",
1800
+ grounding: "judgment",
1801
+ reason: "Third-provider tail when no DeepSeek key reachable"
1802
+ }
1803
+ ]
1804
+ }
1805
+ };
1806
+ var STARTER_CHAINS_BY_MODE = (() => {
1807
+ const out = {};
1808
+ for (const [archetype, modes] of Object.entries(STARTER_CHAINS_BY_MODE_GROUNDED)) {
1809
+ if (modes?.sequential) {
1810
+ out[archetype] = {
1811
+ sequential: modes.sequential.map((e) => e.id)
1812
+ };
1813
+ }
1814
+ }
1815
+ return out;
1816
+ })();
1817
+ function resolveStarterForMode(archetype, toolOrchestration, allChains) {
1818
+ if (toolOrchestration === "sequential") {
1819
+ const overlay = STARTER_CHAINS_BY_MODE[archetype]?.sequential;
1820
+ if (overlay) return [...overlay];
1821
+ }
1822
+ return allChains[archetype];
1823
+ }
1654
1824
  function getDefaultFallbackChain(opts) {
1655
- const { archetype, primary, maxDepth = 3, policy, reachability } = opts;
1825
+ const { archetype, primary, maxDepth = 3, policy, reachability, toolOrchestration } = opts;
1656
1826
  if (maxDepth < 1) {
1657
1827
  throw new Error(
1658
1828
  `getDefaultFallbackChain: maxDepth must be >= 1, got ${maxDepth}`
1659
1829
  );
1660
1830
  }
1661
1831
  const allChains = loadChainsFromBrain();
1662
- const starter = allChains[archetype];
1832
+ const starter = resolveStarterForMode(archetype, toolOrchestration, allChains);
1663
1833
  if (!starter) {
1664
1834
  throw new Error(
1665
1835
  `getDefaultFallbackChain: unknown archetype "${archetype}". Known: ${Object.keys(allChains).join(", ")}`
@@ -1705,6 +1875,114 @@ function getAllStarterChains() {
1705
1875
  }
1706
1876
  return out;
1707
1877
  }
1878
+ function getSequentialStarterChain(archetype) {
1879
+ const overlay = STARTER_CHAINS_BY_MODE[archetype]?.sequential;
1880
+ return overlay ? [...overlay] : void 0;
1881
+ }
1882
+ function copyEntry(e) {
1883
+ const out = { id: e.id, grounding: e.grounding };
1884
+ if (e.reason !== void 0) out.reason = e.reason;
1885
+ if (e.n !== void 0) out.n = e.n;
1886
+ return out;
1887
+ }
1888
+ function lookupStaticEntry(id, archetype) {
1889
+ const archetypeEntries = STARTER_CHAINS_GROUNDED[archetype];
1890
+ if (archetypeEntries) {
1891
+ const hit = archetypeEntries.find((e) => e.id === id);
1892
+ if (hit) return hit;
1893
+ }
1894
+ const seqOverlay = STARTER_CHAINS_BY_MODE_GROUNDED[archetype]?.sequential;
1895
+ if (seqOverlay) {
1896
+ const hit = seqOverlay.find((e) => e.id === id);
1897
+ if (hit) return hit;
1898
+ }
1899
+ return void 0;
1900
+ }
1901
+ function resolveGroundedChainForArchetype(archetype, toolOrchestration) {
1902
+ if (toolOrchestration === "sequential") {
1903
+ const overlay = STARTER_CHAINS_BY_MODE_GROUNDED[archetype]?.sequential;
1904
+ if (overlay) return overlay.map(copyEntry);
1905
+ }
1906
+ const allChains = loadChainsFromBrain();
1907
+ const ids = allChains[archetype];
1908
+ if (!ids) return void 0;
1909
+ return ids.map((id) => {
1910
+ const known = lookupStaticEntry(id, archetype);
1911
+ if (known) return copyEntry(known);
1912
+ return { id, grounding: "judgment" };
1913
+ });
1914
+ }
1915
+ function getDefaultFallbackChainWithGrounding(opts) {
1916
+ const {
1917
+ archetype,
1918
+ primary,
1919
+ maxDepth = 3,
1920
+ policy,
1921
+ reachability,
1922
+ toolOrchestration
1923
+ } = opts;
1924
+ if (maxDepth < 1) {
1925
+ throw new Error(
1926
+ `getDefaultFallbackChainWithGrounding: maxDepth must be >= 1, got ${maxDepth}`
1927
+ );
1928
+ }
1929
+ const starter = resolveGroundedChainForArchetype(archetype, toolOrchestration);
1930
+ if (!starter) {
1931
+ throw new Error(
1932
+ `getDefaultFallbackChainWithGrounding: unknown archetype "${archetype}". Known: ${Object.keys(STARTER_CHAINS_GROUNDED).join(", ")}`
1933
+ );
1934
+ }
1935
+ let chain;
1936
+ if (primary) {
1937
+ const primaryEntry = (() => {
1938
+ const inStarter = starter.find((e) => e.id === primary);
1939
+ if (inStarter) return copyEntry(inStarter);
1940
+ const knownAnywhere = lookupStaticEntry(primary, archetype);
1941
+ if (knownAnywhere) return { ...copyEntry(knownAnywhere), id: primary };
1942
+ return { id: primary, grounding: "judgment" };
1943
+ })();
1944
+ chain = [primaryEntry, ...starter.filter((e) => e.id !== primary)];
1945
+ } else {
1946
+ chain = [...starter];
1947
+ }
1948
+ if (policy?.blockedModels && policy.blockedModels.length > 0) {
1949
+ const blocked = new Set(policy.blockedModels);
1950
+ chain = chain.filter((e) => !blocked.has(e.id));
1951
+ }
1952
+ const seen = /* @__PURE__ */ new Set();
1953
+ const deduped = [];
1954
+ for (const e of chain) {
1955
+ if (!seen.has(e.id)) {
1956
+ seen.add(e.id);
1957
+ deduped.push(e);
1958
+ }
1959
+ }
1960
+ let filtered = deduped;
1961
+ if (reachability) {
1962
+ filtered = deduped.filter((e) => isModelReachable(e.id, reachability));
1963
+ }
1964
+ return filtered.slice(0, maxDepth);
1965
+ }
1966
+ function getStarterChainWithGrounding(archetype) {
1967
+ const entries = STARTER_CHAINS_GROUNDED[archetype];
1968
+ if (!entries) {
1969
+ throw new Error(
1970
+ `getStarterChainWithGrounding: unknown archetype "${archetype}"`
1971
+ );
1972
+ }
1973
+ return entries.map(copyEntry);
1974
+ }
1975
+ function getAllStarterChainsWithGrounding() {
1976
+ const out = {};
1977
+ for (const [archetype, entries] of Object.entries(STARTER_CHAINS_GROUNDED)) {
1978
+ out[archetype] = entries.map(copyEntry);
1979
+ }
1980
+ return out;
1981
+ }
1982
+ function getSequentialStarterChainWithGrounding(archetype) {
1983
+ const overlay = STARTER_CHAINS_BY_MODE_GROUNDED[archetype]?.sequential;
1984
+ return overlay ? overlay.map(copyEntry) : void 0;
1985
+ }
1708
1986
  function ensureCrossProviderTail(opts) {
1709
1987
  const { chain, archetype, apiKeys, envSource } = opts;
1710
1988
  if (chain.length < 1) return { chain };
@@ -2169,6 +2447,17 @@ function mapRowsToPerfMap(rows) {
2169
2447
  }
2170
2448
  return out;
2171
2449
  }
2450
+ function mapRowsToNMap(rows) {
2451
+ const out = /* @__PURE__ */ new Map();
2452
+ for (const row of rows) {
2453
+ if (!isPerfRow(row)) continue;
2454
+ if (typeof row.n !== "number") continue;
2455
+ const existing = out.get(row.model_id) ?? {};
2456
+ existing[row.archetype] = row.n;
2457
+ out.set(row.model_id, existing);
2458
+ }
2459
+ return out;
2460
+ }
2172
2461
  function bundledArchetypePerf() {
2173
2462
  const out = /* @__PURE__ */ new Map();
2174
2463
  for (const profile of allProfiles()) {
@@ -2176,13 +2465,27 @@ function bundledArchetypePerf() {
2176
2465
  }
2177
2466
  return out;
2178
2467
  }
2468
+ function bundledArchetypePerfN() {
2469
+ return /* @__PURE__ */ new Map();
2470
+ }
2179
2471
  var loadArchetypePerfFromBrain = createBrainQueryCache({
2180
2472
  table: "kgauto_archetype_perf",
2181
2473
  mapRows: mapRowsToPerfMap,
2182
2474
  bundledFallback: bundledArchetypePerf
2183
2475
  });
2476
+ var loadArchetypePerfNFromBrain = createBrainQueryCache(
2477
+ {
2478
+ table: "kgauto_archetype_perf",
2479
+ mapRows: mapRowsToNMap,
2480
+ bundledFallback: bundledArchetypePerfN
2481
+ }
2482
+ );
2483
+ var MEASURED_GROUNDING_MIN_N = 10;
2184
2484
  function getArchetypePerfScore(modelId, archetype) {
2185
- return loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
2485
+ const score = loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
2486
+ const n = loadArchetypePerfNFromBrain().get(modelId)?.[archetype] ?? 0;
2487
+ const grounding = n >= MEASURED_GROUNDING_MIN_N ? "measured" : "judgment";
2488
+ return { score, n, grounding };
2186
2489
  }
2187
2490
 
2188
2491
  // src/models-brain.ts
@@ -2316,6 +2619,7 @@ export {
2316
2619
  CallError,
2317
2620
  DIALECT_VERSION,
2318
2621
  INTENT_ARCHETYPES,
2622
+ MEASURED_GROUNDING_MIN_N,
2319
2623
  PROVIDER_ENV_KEYS,
2320
2624
  allProfiles,
2321
2625
  bucketContext,
@@ -2329,11 +2633,16 @@ export {
2329
2633
  countTokens,
2330
2634
  execute,
2331
2635
  getAllStarterChains,
2636
+ getAllStarterChainsWithGrounding,
2332
2637
  getArchetypePerfScore,
2333
2638
  getDefaultFallbackChain,
2639
+ getDefaultFallbackChainWithGrounding,
2334
2640
  getProfile,
2335
2641
  getReachabilityDiagnostic,
2642
+ getSequentialStarterChain,
2643
+ getSequentialStarterChainWithGrounding,
2336
2644
  getStarterChain,
2645
+ getStarterChainWithGrounding,
2337
2646
  hashShape,
2338
2647
  isArchetype,
2339
2648
  isModelReachable,
@@ -2341,12 +2650,14 @@ export {
2341
2650
  learningKey,
2342
2651
  loadAliasesFromBrain,
2343
2652
  loadArchetypePerfFromBrain,
2653
+ loadArchetypePerfNFromBrain,
2344
2654
  loadChainsFromBrain,
2345
2655
  loadModelsFromBrain,
2346
2656
  loadPricingFromBrain,
2347
2657
  profileToRow,
2348
2658
  profilesByProvider,
2349
2659
  record,
2660
+ recordOutcome,
2350
2661
  resetTokenizer,
2351
2662
  resolvePricingAt,
2352
2663
  resolveProviderKey,