@warmdrift/kgauto-compiler 2.0.0-alpha.20 → 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,6 +56,7 @@ __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,
@@ -2898,101 +2905,119 @@ var loadChainsFromBrain = createBrainQueryCache({
2898
2905
  });
2899
2906
 
2900
2907
  // src/fallback.ts
2901
- var STARTER_CHAINS = {
2908
+ var STARTER_CHAINS_GROUNDED = {
2902
2909
  // Reasoning floor — never degrade. Walk UP on 429 to Opus → cross-provider.
2903
- // alpha.16: gpt-5.5 appended as third-provider critique floor (frontier-tier,
2904
- // archetypePerf=9). Cross-provider-tail invariant has somewhere to land when
2905
- // both Anthropic + Google are unreachable (consumer adds only OpenAI key).
2906
2910
  critique: [
2907
- "claude-opus-4-7",
2908
- "claude-sonnet-4-6",
2909
- "gemini-2.5-pro",
2910
- "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)" }
2911
2915
  ],
2912
- // Reasoning matters — Sonnet primary; walk UP to Opus on 429 (rare exception
2913
- // to "always cheaper"); cross-provider via Pro; DeepSeek Pro as tier 3 floor.
2916
+ // Reasoning matters — Sonnet primary; walk UP to Opus on 429.
2914
2917
  plan: [
2915
- "claude-sonnet-4-6",
2916
- "claude-opus-4-7",
2917
- "gemini-2.5-pro",
2918
- "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" }
2919
2922
  ],
2920
- // Quality + cost match. Walk Sonnet → Haiku same-provider, Pro cross,
2921
- // gpt-5.4-mini as third-provider tail (alpha.16 — closes the mono-Anthropic
2922
- // gap when consumer has only ANTHROPIC + OPENAI keys; archetypePerf=7).
2923
+ // Quality + cost match.
2923
2924
  generate: [
2924
- "claude-sonnet-4-6",
2925
- "claude-haiku-4-5",
2926
- "gemini-2.5-pro",
2927
- "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" }
2928
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.
2929
2933
  ask: [
2930
- "claude-sonnet-4-6",
2931
- "claude-haiku-4-5",
2932
- "gemini-2.5-pro",
2933
- "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)" }
2934
2938
  ],
2935
- // Structured-output archetype — Flash skipped (alpha.8 MAX_TOKENS cliff),
2936
- // DeepSeek skipped (no brain evidence). Floor at Haiku. alpha.16: gpt-5.4
2937
- // appended as third-provider extract floor (archetypePerf=8, native
2938
- // structured-output support).
2939
+ // Structured-output archetype — Flash skipped (alpha.8 MAX_TOKENS cliff,
2940
+ // capability-fact); DeepSeek skipped (no brain evidence).
2939
2941
  extract: [
2940
- "claude-sonnet-4-6",
2941
- "claude-haiku-4-5",
2942
- "gemini-2.5-pro",
2943
- "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)" }
2944
2946
  ],
2945
2947
  // Forgiving archetype — Sonnet primary but Flash safely floors it.
2946
2948
  transform: [
2947
- "claude-sonnet-4-6",
2948
- "claude-haiku-4-5",
2949
- "gemini-2.5-pro",
2950
- "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" }
2951
2953
  ],
2952
- // Parallel-tool throughput champion (Flash, L-040). Tier 1 cross-provider
2953
- // Pro; tier 2 Sonnet (quality safety net for blocked-Flash case); tier 3
2954
- // Haiku (reduced tool budget — cliff at 16 fires). This is the
2955
- // `toolOrchestration: 'parallel'` (default) hunt chain. The sequential
2956
- // variant lives in STARTER_CHAINS_BY_MODE.hunt.sequential below — see
2957
- // alpha.20 E3 / interfaces/kgauto.md `sequential-agentic-hunt-mode`.
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).
2958
2956
  hunt: [
2959
- "gemini-2.5-flash",
2960
- "gemini-2.5-pro",
2961
- "claude-sonnet-4-6",
2962
- "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" }
2963
2961
  ],
2964
- // Cost-sensitive + tolerant. DeepSeek brain-evidence tier 1; Haiku tier 2
2965
- // for quality safety; Flash-Lite emergency floor (onboarded s22).
2962
+ // Cost-sensitive + tolerant. DeepSeek brain-evidence tier 1.
2966
2963
  summarize: [
2967
- "gemini-2.5-flash",
2968
- "deepseek-v4-flash",
2969
- "claude-haiku-4-5",
2970
- "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" }
2971
2968
  ],
2972
- // Brain-validated DeepSeek tier 1 (169 rows, 0% empty); Haiku tier 2;
2973
- // Flash-Lite floor for repeat-prompt workloads (cache-discount 10×).
2969
+ // Brain-validated DeepSeek tier 1 (169 rows, 0% empty rate).
2974
2970
  classify: [
2975
- "gemini-2.5-flash",
2976
- "deepseek-v4-flash",
2977
- "claude-haiku-4-5",
2978
- "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" }
2979
2975
  ]
2980
2976
  };
2981
- var STARTER_CHAINS_BY_MODE = {
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 = {
2982
2985
  hunt: {
2983
2986
  sequential: [
2984
- // V4-Pro: cheap + good reasoning at single-step granularity; no
2985
- // L-040 cliff applies when consumer commits to sequential.
2986
- "deepseek-v4-pro",
2987
- // V4-Flash: cheapest viable; sibling-provider fallback.
2988
- "deepseek-v4-flash",
2989
- // Cross-provider safety net — Sonnet handles sequential agentic loops
2990
- // cleanly; Pro as third-provider tail when no DeepSeek key reachable.
2991
- "claude-sonnet-4-6",
2992
- "gemini-2.5-pro"
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
+ }
2993
3007
  ]
2994
3008
  }
2995
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
+ })();
2996
3021
  function resolveStarterForMode(archetype, toolOrchestration, allChains) {
2997
3022
  if (toolOrchestration === "sequential") {
2998
3023
  const overlay = STARTER_CHAINS_BY_MODE[archetype]?.sequential;
@@ -3054,6 +3079,114 @@ function getAllStarterChains() {
3054
3079
  }
3055
3080
  return out;
3056
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
+ }
3057
3190
  function ensureCrossProviderTail(opts) {
3058
3191
  const { chain, archetype, apiKeys, envSource } = opts;
3059
3192
  if (chain.length < 1) return { chain };
@@ -3806,6 +3939,17 @@ function mapRowsToPerfMap(rows) {
3806
3939
  }
3807
3940
  return out;
3808
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
+ }
3809
3953
  function bundledArchetypePerf() {
3810
3954
  const out = /* @__PURE__ */ new Map();
3811
3955
  for (const profile of allProfiles()) {
@@ -3813,13 +3957,27 @@ function bundledArchetypePerf() {
3813
3957
  }
3814
3958
  return out;
3815
3959
  }
3960
+ function bundledArchetypePerfN() {
3961
+ return /* @__PURE__ */ new Map();
3962
+ }
3816
3963
  var loadArchetypePerfFromBrain = createBrainQueryCache({
3817
3964
  table: "kgauto_archetype_perf",
3818
3965
  mapRows: mapRowsToPerfMap,
3819
3966
  bundledFallback: bundledArchetypePerf
3820
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;
3821
3976
  function getArchetypePerfScore(modelId, archetype) {
3822
- 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 };
3823
3981
  }
3824
3982
 
3825
3983
  // src/models-brain.ts
@@ -3954,6 +4112,7 @@ function compile2(ir, opts) {
3954
4112
  CallError,
3955
4113
  DIALECT_VERSION,
3956
4114
  INTENT_ARCHETYPES,
4115
+ MEASURED_GROUNDING_MIN_N,
3957
4116
  PROVIDER_ENV_KEYS,
3958
4117
  allProfiles,
3959
4118
  bucketContext,
@@ -3967,11 +4126,16 @@ function compile2(ir, opts) {
3967
4126
  countTokens,
3968
4127
  execute,
3969
4128
  getAllStarterChains,
4129
+ getAllStarterChainsWithGrounding,
3970
4130
  getArchetypePerfScore,
3971
4131
  getDefaultFallbackChain,
4132
+ getDefaultFallbackChainWithGrounding,
3972
4133
  getProfile,
3973
4134
  getReachabilityDiagnostic,
4135
+ getSequentialStarterChain,
4136
+ getSequentialStarterChainWithGrounding,
3974
4137
  getStarterChain,
4138
+ getStarterChainWithGrounding,
3975
4139
  hashShape,
3976
4140
  isArchetype,
3977
4141
  isModelReachable,
@@ -3979,6 +4143,7 @@ function compile2(ir, opts) {
3979
4143
  learningKey,
3980
4144
  loadAliasesFromBrain,
3981
4145
  loadArchetypePerfFromBrain,
4146
+ loadArchetypePerfNFromBrain,
3982
4147
  loadChainsFromBrain,
3983
4148
  loadModelsFromBrain,
3984
4149
  loadPricingFromBrain,