@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.js CHANGED
@@ -25,6 +25,7 @@ __export(index_exports, {
25
25
  CallError: () => CallError,
26
26
  DIALECT_VERSION: () => DIALECT_VERSION,
27
27
  INTENT_ARCHETYPES: () => INTENT_ARCHETYPES,
28
+ MEASURED_GROUNDING_MIN_N: () => MEASURED_GROUNDING_MIN_N,
28
29
  PROVIDER_ENV_KEYS: () => PROVIDER_ENV_KEYS,
29
30
  allProfiles: () => allProfiles,
30
31
  bucketContext: () => bucketContext,
@@ -38,11 +39,16 @@ __export(index_exports, {
38
39
  countTokens: () => countTokens,
39
40
  execute: () => execute,
40
41
  getAllStarterChains: () => getAllStarterChains,
42
+ getAllStarterChainsWithGrounding: () => getAllStarterChainsWithGrounding,
41
43
  getArchetypePerfScore: () => getArchetypePerfScore,
42
44
  getDefaultFallbackChain: () => getDefaultFallbackChain,
45
+ getDefaultFallbackChainWithGrounding: () => getDefaultFallbackChainWithGrounding,
43
46
  getProfile: () => getProfile,
44
47
  getReachabilityDiagnostic: () => getReachabilityDiagnostic,
48
+ getSequentialStarterChain: () => getSequentialStarterChain,
49
+ getSequentialStarterChainWithGrounding: () => getSequentialStarterChainWithGrounding,
45
50
  getStarterChain: () => getStarterChain,
51
+ getStarterChainWithGrounding: () => getStarterChainWithGrounding,
46
52
  hashShape: () => hashShape,
47
53
  isArchetype: () => isArchetype,
48
54
  isModelReachable: () => isModelReachable,
@@ -50,12 +56,14 @@ __export(index_exports, {
50
56
  learningKey: () => learningKey,
51
57
  loadAliasesFromBrain: () => loadAliasesFromBrain,
52
58
  loadArchetypePerfFromBrain: () => loadArchetypePerfFromBrain,
59
+ loadArchetypePerfNFromBrain: () => loadArchetypePerfNFromBrain,
53
60
  loadChainsFromBrain: () => loadChainsFromBrain,
54
61
  loadModelsFromBrain: () => loadModelsFromBrain,
55
62
  loadPricingFromBrain: () => loadPricingFromBrain,
56
63
  profileToRow: () => profileToRow,
57
64
  profilesByProvider: () => profilesByProvider,
58
65
  record: () => record,
66
+ recordOutcome: () => recordOutcome,
59
67
  resetTokenizer: () => resetTokenizer,
60
68
  resolvePricingAt: () => resolvePricingAt,
61
69
  resolveProviderKey: () => resolveProviderKey,
@@ -337,7 +345,11 @@ function passApplyCliffs(ir, profile, estimatedInputTokens) {
337
345
  const mutations = [];
338
346
  const hints = { qualityWarning: [] };
339
347
  let nextIR = ir;
348
+ const sequentialMode = nextIR.constraints?.toolOrchestration === "sequential";
340
349
  for (const cliff of profile.cliffs) {
350
+ if (sequentialMode && cliff.reason.includes("L-040")) {
351
+ continue;
352
+ }
341
353
  let triggered = false;
342
354
  switch (cliff.metric) {
343
355
  case "input_tokens":
@@ -2087,8 +2099,19 @@ function compile(ir, opts = {}) {
2087
2099
  cacheableTokens: lowered.diagnostics.cacheableTokens,
2088
2100
  estimatedCacheSavingsUsd: lowered.diagnostics.estimatedCacheSavingsUsd,
2089
2101
  historyCacheableTokens: lowered.diagnostics.historyCacheableTokens,
2090
- historyTokensTotal: compressed.historyTokensTotal
2102
+ historyTokensTotal: compressed.historyTokensTotal,
2103
+ // alpha.20 E3: mirror the consumer's declared mode for Glass-Box +
2104
+ // brain observability. Undefined when not declared (pre-alpha.20).
2105
+ toolOrchestration: ir.constraints?.toolOrchestration
2091
2106
  };
2107
+ if (ir.intent.archetype === "hunt" && ir.constraints?.toolOrchestration === "sequential") {
2108
+ accumulatedMutations.push({
2109
+ id: "sequential-mode-chain-selected",
2110
+ source: "tool_orchestration",
2111
+ passName: "compile",
2112
+ 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)."
2113
+ });
2114
+ }
2092
2115
  const advisories = runAdvisor(
2093
2116
  ir,
2094
2117
  {
@@ -2352,7 +2375,9 @@ function registerCompile(appId, archetype, ir, result) {
2352
2375
  mutationsApplied: result.mutationsApplied.map((m) => m.id),
2353
2376
  startedAt: Date.now(),
2354
2377
  historyCacheableTokens: result.diagnostics.historyCacheableTokens,
2355
- historyTokensTotal: result.diagnostics.historyTokensTotal
2378
+ historyTokensTotal: result.diagnostics.historyTokensTotal,
2379
+ // alpha.20 E3: capture consumer's declared mode for the brain payload.
2380
+ toolOrchestration: result.diagnostics.toolOrchestration
2356
2381
  });
2357
2382
  }
2358
2383
  async function record(input) {
@@ -2365,11 +2390,22 @@ async function record(input) {
2365
2390
  const config = activeConfig;
2366
2391
  const fetchFn = config.fetchImpl ?? fetch;
2367
2392
  const send = async () => {
2393
+ let outcomeId;
2368
2394
  try {
2369
2395
  const res = await fetchFn(`${config.endpoint}/outcomes`, {
2370
2396
  method: "POST",
2371
2397
  headers: {
2372
2398
  "Content-Type": "application/json",
2399
+ // alpha.20: request the inserted row back so we can JOIN advisories
2400
+ // to it via outcome_id. PostgREST returns the row when
2401
+ // `Prefer: return=representation` is set; proxies that pass the
2402
+ // header through (the recommended `const row = { ...body }` shape
2403
+ // from OutcomePayload's forward-compat rule) will surface
2404
+ // the row id. Proxies that don't (legacy / hand-rolled shapes)
2405
+ // simply produce no parseable id → secondary advisory POST is
2406
+ // skipped silently. Best-effort — primary outcome row is the
2407
+ // load-bearing write.
2408
+ Prefer: "return=representation",
2373
2409
  ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
2374
2410
  },
2375
2411
  body: JSON.stringify(payload)
@@ -2378,6 +2414,29 @@ async function record(input) {
2378
2414
  const text = await res.text().catch(() => "<no body>");
2379
2415
  throw new Error(`brain ${res.status}: ${text}`);
2380
2416
  }
2417
+ outcomeId = await tryExtractOutcomeId(res);
2418
+ } catch (err) {
2419
+ (config.onError ?? defaultOnError2)(err);
2420
+ return;
2421
+ }
2422
+ const advisories = input.advisories;
2423
+ if (!advisories || advisories.length === 0) return;
2424
+ if (outcomeId === void 0) return;
2425
+ try {
2426
+ const advisoryPayload = advisories.map((a) => buildAdvisoryRow(outcomeId, a));
2427
+ const res = await fetchFn(`${config.endpoint}/compile_outcome_advisories`, {
2428
+ method: "POST",
2429
+ headers: {
2430
+ "Content-Type": "application/json",
2431
+ Prefer: "return=minimal",
2432
+ ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
2433
+ },
2434
+ body: JSON.stringify(advisoryPayload)
2435
+ });
2436
+ if (!res.ok) {
2437
+ const text = await res.text().catch(() => "<no body>");
2438
+ throw new Error(`brain advisories ${res.status}: ${text}`);
2439
+ }
2381
2440
  } catch (err) {
2382
2441
  (config.onError ?? defaultOnError2)(err);
2383
2442
  }
@@ -2427,7 +2486,12 @@ function buildPayload(input, reg) {
2427
2486
  cost_usd_actual: costUsdActual,
2428
2487
  ttft_ms: input.ttftMs,
2429
2488
  history_cacheable_tokens: reg?.historyCacheableTokens,
2430
- history_tokens_at_compile: reg?.historyTokensTotal
2489
+ history_tokens_at_compile: reg?.historyTokensTotal,
2490
+ // alpha.20 E3: mirror consumer's declared tool-orchestration mode so
2491
+ // the brain can measure per-mode model perf separately (DeepSeek in
2492
+ // sequential vs parallel mode is two different stories — L-040).
2493
+ // Null when consumer hadn't adopted the constraint yet.
2494
+ tool_orchestration: reg?.toolOrchestration ?? null
2431
2495
  };
2432
2496
  }
2433
2497
  function computeCostUsd(modelId, tokensIn, tokensOut) {
@@ -2444,6 +2508,77 @@ function computeCostUsd(modelId, tokensIn, tokensOut) {
2444
2508
  const outUsd = tokensOut / 1e6 * profile.costOutputPer1m;
2445
2509
  return Math.round((inUsd + outUsd) * 1e6) / 1e6;
2446
2510
  }
2511
+ async function tryExtractOutcomeId(res) {
2512
+ try {
2513
+ const ct = res.headers?.get?.("content-type") ?? "";
2514
+ if (ct && !ct.includes("application/json")) return void 0;
2515
+ if (typeof res.json !== "function") return void 0;
2516
+ const body = await res.json();
2517
+ if (Array.isArray(body) && body.length > 0) {
2518
+ const first = body[0];
2519
+ const id = first?.id;
2520
+ if (typeof id === "number") return id;
2521
+ } else if (body && typeof body === "object") {
2522
+ const id = body.id;
2523
+ if (typeof id === "number") return id;
2524
+ }
2525
+ return void 0;
2526
+ } catch {
2527
+ return void 0;
2528
+ }
2529
+ }
2530
+ function buildAdvisoryRow(outcomeId, a) {
2531
+ return {
2532
+ outcome_id: outcomeId,
2533
+ code: a.code,
2534
+ level: a.level,
2535
+ message: a.message,
2536
+ ...a.recommendationType ? { recommendation_type: a.recommendationType } : {},
2537
+ ...a.suggestion ? { suggestion: a.suggestion } : {},
2538
+ ...a.docsUrl ? { docs_url: a.docsUrl } : {}
2539
+ };
2540
+ }
2541
+ async function recordOutcome(input) {
2542
+ if (!activeConfig) {
2543
+ return { ok: false, reason: "brain_not_configured" };
2544
+ }
2545
+ const config = activeConfig;
2546
+ const fetchFn = config.fetchImpl ?? fetch;
2547
+ const payload = {
2548
+ outcome_id: input.outcomeId,
2549
+ outcome: input.outcome,
2550
+ rating: input.rating ?? null,
2551
+ reason: input.reason ?? null,
2552
+ observed_confidence: input.observedConfidence ?? null
2553
+ };
2554
+ const send = async () => {
2555
+ try {
2556
+ const res = await fetchFn(`${config.endpoint}/compile_outcome_quality`, {
2557
+ method: "POST",
2558
+ headers: {
2559
+ "Content-Type": "application/json",
2560
+ ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
2561
+ },
2562
+ body: JSON.stringify(payload)
2563
+ });
2564
+ if (!res.ok) {
2565
+ const text = await res.text().catch(() => "<no body>");
2566
+ const err = new Error(`brain ${res.status}: ${text}`);
2567
+ (config.onError ?? defaultOnError2)(err);
2568
+ return { ok: false, reason: "persistence_failed" };
2569
+ }
2570
+ return { ok: true };
2571
+ } catch (err) {
2572
+ (config.onError ?? defaultOnError2)(err);
2573
+ return { ok: false, reason: "persistence_failed" };
2574
+ }
2575
+ };
2576
+ if (config.sync) {
2577
+ return send();
2578
+ }
2579
+ void send();
2580
+ return { ok: true };
2581
+ }
2447
2582
 
2448
2583
  // src/ir.ts
2449
2584
  var CallError = class extends Error {
@@ -2770,92 +2905,135 @@ var loadChainsFromBrain = createBrainQueryCache({
2770
2905
  });
2771
2906
 
2772
2907
  // src/fallback.ts
2773
- var STARTER_CHAINS = {
2908
+ var STARTER_CHAINS_GROUNDED = {
2774
2909
  // Reasoning floor — never degrade. Walk UP on 429 to Opus → cross-provider.
2775
- // alpha.16: gpt-5.5 appended as third-provider critique floor (frontier-tier,
2776
- // archetypePerf=9). Cross-provider-tail invariant has somewhere to land when
2777
- // both Anthropic + Google are unreachable (consumer adds only OpenAI key).
2778
2910
  critique: [
2779
- "claude-opus-4-7",
2780
- "claude-sonnet-4-6",
2781
- "gemini-2.5-pro",
2782
- "gpt-5.5"
2911
+ { id: "claude-opus-4-7", grounding: "judgment", reason: "Highest reasoning bar, no degradation tier \u2014 engineer pick, awaiting measured backing" },
2912
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Same-provider walk-down from Opus on 429" },
2913
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor in similar quality bracket" },
2914
+ { id: "gpt-5.5", grounding: "judgment", reason: "alpha.16: third-provider frontier-tier floor (archetypePerf=9)" }
2783
2915
  ],
2784
- // Reasoning matters — Sonnet primary; walk UP to Opus on 429 (rare exception
2785
- // to "always cheaper"); cross-provider via Pro; DeepSeek Pro as tier 3 floor.
2916
+ // Reasoning matters — Sonnet primary; walk UP to Opus on 429.
2786
2917
  plan: [
2787
- "claude-sonnet-4-6",
2788
- "claude-opus-4-7",
2789
- "gemini-2.5-pro",
2790
- "deepseek-v4-pro"
2918
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Reasoning + cost balance \u2014 engineer pick" },
2919
+ { id: "claude-opus-4-7", grounding: "judgment", reason: 'Same-provider walk-UP on 429 (rare exception to "always cheaper")' },
2920
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
2921
+ { id: "deepseek-v4-pro", grounding: "judgment", reason: "Tier 3 cost floor \u2014 no brain evidence yet" }
2791
2922
  ],
2792
- // Quality + cost match. Walk Sonnet → Haiku same-provider, Pro cross,
2793
- // gpt-5.4-mini as third-provider tail (alpha.16 — closes the mono-Anthropic
2794
- // gap when consumer has only ANTHROPIC + OPENAI keys; archetypePerf=7).
2923
+ // Quality + cost match.
2795
2924
  generate: [
2796
- "claude-sonnet-4-6",
2797
- "claude-haiku-4-5",
2798
- "gemini-2.5-pro",
2799
- "gpt-5.4-mini"
2925
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality + cost match \u2014 engineer pick" },
2926
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
2927
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
2928
+ { id: "gpt-5.4-mini", grounding: "judgment", reason: "alpha.16: third-provider tail (archetypePerf=7) \u2014 closes mono-Anthropic gap" }
2800
2929
  ],
2930
+ // ask::sonnet — STARTER_CHAINS calls this "Quality + cost match" but
2931
+ // tt-intel s78 prod data showed 27% empty rate. Labeled 'judgment' until
2932
+ // evidence either validates or refutes the placement.
2801
2933
  ask: [
2802
- "claude-sonnet-4-6",
2803
- "claude-haiku-4-5",
2804
- "gemini-2.5-pro",
2805
- "gpt-5.4-mini"
2934
+ { 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" },
2935
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
2936
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
2937
+ { id: "gpt-5.4-mini", grounding: "judgment", reason: "alpha.16: third-provider tail (archetypePerf=7)" }
2806
2938
  ],
2807
- // Structured-output archetype — Flash skipped (alpha.8 MAX_TOKENS cliff),
2808
- // DeepSeek skipped (no brain evidence). Floor at Haiku. alpha.16: gpt-5.4
2809
- // appended as third-provider extract floor (archetypePerf=8, native
2810
- // structured-output support).
2939
+ // Structured-output archetype — Flash skipped (alpha.8 MAX_TOKENS cliff,
2940
+ // capability-fact); DeepSeek skipped (no brain evidence).
2811
2941
  extract: [
2812
- "claude-sonnet-4-6",
2813
- "claude-haiku-4-5",
2814
- "gemini-2.5-pro",
2815
- "gpt-5.4"
2942
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Reliable structured-output anchor \u2014 engineer pick" },
2943
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down with native structured output" },
2944
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor with structured-output support" },
2945
+ { id: "gpt-5.4", grounding: "capability-fact", reason: "alpha.16: third-provider floor \u2014 native structured-output capability (archetypePerf=8)" }
2816
2946
  ],
2817
2947
  // Forgiving archetype — Sonnet primary but Flash safely floors it.
2818
2948
  transform: [
2819
- "claude-sonnet-4-6",
2820
- "claude-haiku-4-5",
2821
- "gemini-2.5-pro",
2822
- "gemini-2.5-flash"
2949
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality anchor \u2014 engineer pick" },
2950
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
2951
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
2952
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost floor \u2014 forgiving archetype tolerates Flash" }
2823
2953
  ],
2824
- // Parallel-tool throughput champion (Flash, L-040). Tier 1 cross-provider
2825
- // Pro; tier 2 Sonnet (quality safety net for blocked-Flash case); tier 3
2826
- // Haiku (reduced tool budget — cliff at 16 fires).
2954
+ // Parallel-tool throughput champion Flash leads on the L-040 cliff
2955
+ // (capability-fact: Flash 15-75 parallel calls/step vs DeepSeek 7-8).
2827
2956
  hunt: [
2828
- "gemini-2.5-flash",
2829
- "gemini-2.5-pro",
2830
- "claude-sonnet-4-6",
2831
- "claude-haiku-4-5"
2957
+ { id: "gemini-2.5-flash", grounding: "capability-fact", reason: "L-040 parallel-tool throughput champion (15-75 calls/step)" },
2958
+ { id: "gemini-2.5-pro", grounding: "capability-fact", reason: "Cross-provider tier 1 with strong parallel-tool support" },
2959
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality safety net for blocked-Flash case" },
2960
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Reduced tool budget \u2014 cliff at 16 fires" }
2832
2961
  ],
2833
- // Cost-sensitive + tolerant. DeepSeek brain-evidence tier 1; Haiku tier 2
2834
- // for quality safety; Flash-Lite emergency floor (onboarded s22).
2962
+ // Cost-sensitive + tolerant. DeepSeek brain-evidence tier 1.
2835
2963
  summarize: [
2836
- "gemini-2.5-flash",
2837
- "deepseek-v4-flash",
2838
- "claude-haiku-4-5",
2839
- "gemini-2.5-flash-lite"
2964
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost-sensitive primary \u2014 engineer pick" },
2965
+ { id: "deepseek-v4-flash", grounding: "measured", reason: "Brain-validated tier 1 for cost-sensitive summarize workloads", n: 169 },
2966
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Quality safety net" },
2967
+ { id: "gemini-2.5-flash-lite", grounding: "judgment", reason: "Emergency floor \u2014 onboarded s22, no brain evidence yet" }
2840
2968
  ],
2841
- // Brain-validated DeepSeek tier 1 (169 rows, 0% empty); Haiku tier 2;
2842
- // Flash-Lite floor for repeat-prompt workloads (cache-discount 10×).
2969
+ // Brain-validated DeepSeek tier 1 (169 rows, 0% empty rate).
2843
2970
  classify: [
2844
- "gemini-2.5-flash",
2845
- "deepseek-v4-flash",
2846
- "claude-haiku-4-5",
2847
- "gemini-2.5-flash-lite"
2971
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost-sensitive primary \u2014 engineer pick" },
2972
+ { id: "deepseek-v4-flash", grounding: "measured", reason: "Brain-validated tier 1 (169 rows, 0% empty rate)", n: 169 },
2973
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Quality safety net" },
2974
+ { id: "gemini-2.5-flash-lite", grounding: "judgment", reason: "Cache-discount 10\xD7 floor for repeat-prompt workloads" }
2848
2975
  ]
2849
2976
  };
2977
+ var STARTER_CHAINS = (() => {
2978
+ const out = {};
2979
+ for (const [archetype, entries] of Object.entries(STARTER_CHAINS_GROUNDED)) {
2980
+ out[archetype] = entries.map((e) => e.id);
2981
+ }
2982
+ return out;
2983
+ })();
2984
+ var STARTER_CHAINS_BY_MODE_GROUNDED = {
2985
+ hunt: {
2986
+ sequential: [
2987
+ {
2988
+ id: "deepseek-v4-pro",
2989
+ grounding: "judgment",
2990
+ reason: "alpha.20 E3: cheap + good reasoning at single-step granularity; L-040 cliff silenced when sequential \u2014 hypothesis not yet measured"
2991
+ },
2992
+ {
2993
+ id: "deepseek-v4-flash",
2994
+ grounding: "judgment",
2995
+ reason: "Cheapest viable; sibling-provider fallback"
2996
+ },
2997
+ {
2998
+ id: "claude-sonnet-4-6",
2999
+ grounding: "judgment",
3000
+ reason: "Cross-provider safety net \u2014 Sonnet handles sequential agentic loops cleanly"
3001
+ },
3002
+ {
3003
+ id: "gemini-2.5-pro",
3004
+ grounding: "judgment",
3005
+ reason: "Third-provider tail when no DeepSeek key reachable"
3006
+ }
3007
+ ]
3008
+ }
3009
+ };
3010
+ var STARTER_CHAINS_BY_MODE = (() => {
3011
+ const out = {};
3012
+ for (const [archetype, modes] of Object.entries(STARTER_CHAINS_BY_MODE_GROUNDED)) {
3013
+ if (modes?.sequential) {
3014
+ out[archetype] = {
3015
+ sequential: modes.sequential.map((e) => e.id)
3016
+ };
3017
+ }
3018
+ }
3019
+ return out;
3020
+ })();
3021
+ function resolveStarterForMode(archetype, toolOrchestration, allChains) {
3022
+ if (toolOrchestration === "sequential") {
3023
+ const overlay = STARTER_CHAINS_BY_MODE[archetype]?.sequential;
3024
+ if (overlay) return [...overlay];
3025
+ }
3026
+ return allChains[archetype];
3027
+ }
2850
3028
  function getDefaultFallbackChain(opts) {
2851
- const { archetype, primary, maxDepth = 3, policy, reachability } = opts;
3029
+ const { archetype, primary, maxDepth = 3, policy, reachability, toolOrchestration } = opts;
2852
3030
  if (maxDepth < 1) {
2853
3031
  throw new Error(
2854
3032
  `getDefaultFallbackChain: maxDepth must be >= 1, got ${maxDepth}`
2855
3033
  );
2856
3034
  }
2857
3035
  const allChains = loadChainsFromBrain();
2858
- const starter = allChains[archetype];
3036
+ const starter = resolveStarterForMode(archetype, toolOrchestration, allChains);
2859
3037
  if (!starter) {
2860
3038
  throw new Error(
2861
3039
  `getDefaultFallbackChain: unknown archetype "${archetype}". Known: ${Object.keys(allChains).join(", ")}`
@@ -2901,6 +3079,114 @@ function getAllStarterChains() {
2901
3079
  }
2902
3080
  return out;
2903
3081
  }
3082
+ function getSequentialStarterChain(archetype) {
3083
+ const overlay = STARTER_CHAINS_BY_MODE[archetype]?.sequential;
3084
+ return overlay ? [...overlay] : void 0;
3085
+ }
3086
+ function copyEntry(e) {
3087
+ const out = { id: e.id, grounding: e.grounding };
3088
+ if (e.reason !== void 0) out.reason = e.reason;
3089
+ if (e.n !== void 0) out.n = e.n;
3090
+ return out;
3091
+ }
3092
+ function lookupStaticEntry(id, archetype) {
3093
+ const archetypeEntries = STARTER_CHAINS_GROUNDED[archetype];
3094
+ if (archetypeEntries) {
3095
+ const hit = archetypeEntries.find((e) => e.id === id);
3096
+ if (hit) return hit;
3097
+ }
3098
+ const seqOverlay = STARTER_CHAINS_BY_MODE_GROUNDED[archetype]?.sequential;
3099
+ if (seqOverlay) {
3100
+ const hit = seqOverlay.find((e) => e.id === id);
3101
+ if (hit) return hit;
3102
+ }
3103
+ return void 0;
3104
+ }
3105
+ function resolveGroundedChainForArchetype(archetype, toolOrchestration) {
3106
+ if (toolOrchestration === "sequential") {
3107
+ const overlay = STARTER_CHAINS_BY_MODE_GROUNDED[archetype]?.sequential;
3108
+ if (overlay) return overlay.map(copyEntry);
3109
+ }
3110
+ const allChains = loadChainsFromBrain();
3111
+ const ids = allChains[archetype];
3112
+ if (!ids) return void 0;
3113
+ return ids.map((id) => {
3114
+ const known = lookupStaticEntry(id, archetype);
3115
+ if (known) return copyEntry(known);
3116
+ return { id, grounding: "judgment" };
3117
+ });
3118
+ }
3119
+ function getDefaultFallbackChainWithGrounding(opts) {
3120
+ const {
3121
+ archetype,
3122
+ primary,
3123
+ maxDepth = 3,
3124
+ policy,
3125
+ reachability,
3126
+ toolOrchestration
3127
+ } = opts;
3128
+ if (maxDepth < 1) {
3129
+ throw new Error(
3130
+ `getDefaultFallbackChainWithGrounding: maxDepth must be >= 1, got ${maxDepth}`
3131
+ );
3132
+ }
3133
+ const starter = resolveGroundedChainForArchetype(archetype, toolOrchestration);
3134
+ if (!starter) {
3135
+ throw new Error(
3136
+ `getDefaultFallbackChainWithGrounding: unknown archetype "${archetype}". Known: ${Object.keys(STARTER_CHAINS_GROUNDED).join(", ")}`
3137
+ );
3138
+ }
3139
+ let chain;
3140
+ if (primary) {
3141
+ const primaryEntry = (() => {
3142
+ const inStarter = starter.find((e) => e.id === primary);
3143
+ if (inStarter) return copyEntry(inStarter);
3144
+ const knownAnywhere = lookupStaticEntry(primary, archetype);
3145
+ if (knownAnywhere) return { ...copyEntry(knownAnywhere), id: primary };
3146
+ return { id: primary, grounding: "judgment" };
3147
+ })();
3148
+ chain = [primaryEntry, ...starter.filter((e) => e.id !== primary)];
3149
+ } else {
3150
+ chain = [...starter];
3151
+ }
3152
+ if (policy?.blockedModels && policy.blockedModels.length > 0) {
3153
+ const blocked = new Set(policy.blockedModels);
3154
+ chain = chain.filter((e) => !blocked.has(e.id));
3155
+ }
3156
+ const seen = /* @__PURE__ */ new Set();
3157
+ const deduped = [];
3158
+ for (const e of chain) {
3159
+ if (!seen.has(e.id)) {
3160
+ seen.add(e.id);
3161
+ deduped.push(e);
3162
+ }
3163
+ }
3164
+ let filtered = deduped;
3165
+ if (reachability) {
3166
+ filtered = deduped.filter((e) => isModelReachable(e.id, reachability));
3167
+ }
3168
+ return filtered.slice(0, maxDepth);
3169
+ }
3170
+ function getStarterChainWithGrounding(archetype) {
3171
+ const entries = STARTER_CHAINS_GROUNDED[archetype];
3172
+ if (!entries) {
3173
+ throw new Error(
3174
+ `getStarterChainWithGrounding: unknown archetype "${archetype}"`
3175
+ );
3176
+ }
3177
+ return entries.map(copyEntry);
3178
+ }
3179
+ function getAllStarterChainsWithGrounding() {
3180
+ const out = {};
3181
+ for (const [archetype, entries] of Object.entries(STARTER_CHAINS_GROUNDED)) {
3182
+ out[archetype] = entries.map(copyEntry);
3183
+ }
3184
+ return out;
3185
+ }
3186
+ function getSequentialStarterChainWithGrounding(archetype) {
3187
+ const overlay = STARTER_CHAINS_BY_MODE_GROUNDED[archetype]?.sequential;
3188
+ return overlay ? overlay.map(copyEntry) : void 0;
3189
+ }
2904
3190
  function ensureCrossProviderTail(opts) {
2905
3191
  const { chain, archetype, apiKeys, envSource } = opts;
2906
3192
  if (chain.length < 1) return { chain };
@@ -3653,6 +3939,17 @@ function mapRowsToPerfMap(rows) {
3653
3939
  }
3654
3940
  return out;
3655
3941
  }
3942
+ function mapRowsToNMap(rows) {
3943
+ const out = /* @__PURE__ */ new Map();
3944
+ for (const row of rows) {
3945
+ if (!isPerfRow(row)) continue;
3946
+ if (typeof row.n !== "number") continue;
3947
+ const existing = out.get(row.model_id) ?? {};
3948
+ existing[row.archetype] = row.n;
3949
+ out.set(row.model_id, existing);
3950
+ }
3951
+ return out;
3952
+ }
3656
3953
  function bundledArchetypePerf() {
3657
3954
  const out = /* @__PURE__ */ new Map();
3658
3955
  for (const profile of allProfiles()) {
@@ -3660,13 +3957,27 @@ function bundledArchetypePerf() {
3660
3957
  }
3661
3958
  return out;
3662
3959
  }
3960
+ function bundledArchetypePerfN() {
3961
+ return /* @__PURE__ */ new Map();
3962
+ }
3663
3963
  var loadArchetypePerfFromBrain = createBrainQueryCache({
3664
3964
  table: "kgauto_archetype_perf",
3665
3965
  mapRows: mapRowsToPerfMap,
3666
3966
  bundledFallback: bundledArchetypePerf
3667
3967
  });
3968
+ var loadArchetypePerfNFromBrain = createBrainQueryCache(
3969
+ {
3970
+ table: "kgauto_archetype_perf",
3971
+ mapRows: mapRowsToNMap,
3972
+ bundledFallback: bundledArchetypePerfN
3973
+ }
3974
+ );
3975
+ var MEASURED_GROUNDING_MIN_N = 10;
3668
3976
  function getArchetypePerfScore(modelId, archetype) {
3669
- return loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
3977
+ const score = loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
3978
+ const n = loadArchetypePerfNFromBrain().get(modelId)?.[archetype] ?? 0;
3979
+ const grounding = n >= MEASURED_GROUNDING_MIN_N ? "measured" : "judgment";
3980
+ return { score, n, grounding };
3670
3981
  }
3671
3982
 
3672
3983
  // src/models-brain.ts
@@ -3801,6 +4112,7 @@ function compile2(ir, opts) {
3801
4112
  CallError,
3802
4113
  DIALECT_VERSION,
3803
4114
  INTENT_ARCHETYPES,
4115
+ MEASURED_GROUNDING_MIN_N,
3804
4116
  PROVIDER_ENV_KEYS,
3805
4117
  allProfiles,
3806
4118
  bucketContext,
@@ -3814,11 +4126,16 @@ function compile2(ir, opts) {
3814
4126
  countTokens,
3815
4127
  execute,
3816
4128
  getAllStarterChains,
4129
+ getAllStarterChainsWithGrounding,
3817
4130
  getArchetypePerfScore,
3818
4131
  getDefaultFallbackChain,
4132
+ getDefaultFallbackChainWithGrounding,
3819
4133
  getProfile,
3820
4134
  getReachabilityDiagnostic,
4135
+ getSequentialStarterChain,
4136
+ getSequentialStarterChainWithGrounding,
3821
4137
  getStarterChain,
4138
+ getStarterChainWithGrounding,
3822
4139
  hashShape,
3823
4140
  isArchetype,
3824
4141
  isModelReachable,
@@ -3826,12 +4143,14 @@ function compile2(ir, opts) {
3826
4143
  learningKey,
3827
4144
  loadAliasesFromBrain,
3828
4145
  loadArchetypePerfFromBrain,
4146
+ loadArchetypePerfNFromBrain,
3829
4147
  loadChainsFromBrain,
3830
4148
  loadModelsFromBrain,
3831
4149
  loadPricingFromBrain,
3832
4150
  profileToRow,
3833
4151
  profilesByProvider,
3834
4152
  record,
4153
+ recordOutcome,
3835
4154
  resetTokenizer,
3836
4155
  resolvePricingAt,
3837
4156
  resolveProviderKey,