@warmdrift/kgauto-compiler 2.0.0-alpha.47 → 2.0.0-alpha.49

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/README.md CHANGED
@@ -89,6 +89,37 @@ await record({
89
89
  });
90
90
  ```
91
91
 
92
+ ### `probeShadow()` — measure a swap candidate from an AI-SDK route (alpha.48)
93
+
94
+ `call()` runs a full-IR shadow probe internally (`shadowProbe` option). AI-SDK consumers who drive `streamText()` themselves never call `call()`, so they reach the same probe via the standalone `probeShadow()` export — fire it from `onFinish`, passing the served leg you already have, wrapped in `waitUntil()`/`after()` so it never blocks the response.
95
+
96
+ ```ts
97
+ import { probeShadow, configureBrain } from '@warmdrift/kgauto-compiler';
98
+ import { after } from 'next/server'; // or the platform's waitUntil()
99
+
100
+ configureBrain({ endpoint: 'https://your-app.com/api/kgauto/v2', apiKey: '...' });
101
+
102
+ const t0 = Date.now();
103
+ const result = streamText({
104
+ model, system, messages,
105
+ onFinish: ({ text, usage }) => {
106
+ after(() => probeShadow(ir, {
107
+ served: {
108
+ model: servedModelId,
109
+ responseText: text,
110
+ tokensIn: usage.promptTokens,
111
+ tokensOut: usage.completionTokens,
112
+ latencyMs: Date.now() - t0,
113
+ },
114
+ candidates: ['deepseek-v4-pro'], // what you'd consider swapping to
115
+ sampleRate: 0.05, // pin 1 for a dogfood window
116
+ }));
117
+ },
118
+ });
119
+ ```
120
+
121
+ kgauto runs each candidate on the **same IR** and persists a `probe_outcomes` row (`replay_source='inline-full-ir'`, `prompt_fidelity=1.0`, both latency legs). Phase 1 is `judge:'off'` — rows accumulate for an offline verdict rollup; inline Opus judging is Phase 2. Fire-and-forget: never throws into your `onFinish`.
122
+
92
123
  ## Structured advisories API (alpha.29+)
93
124
 
94
125
  kgauto's compile-time advisories (`caching-off-on-claude`, `tool-bloat`,
package/dist/index.d.mts CHANGED
@@ -102,6 +102,70 @@ declare function execute(request: CompiledRequest, opts?: ExecuteOptions): Promi
102
102
  * exhausted without success.
103
103
  */
104
104
  declare function call(ir: PromptIR, opts?: CallOptions): Promise<CallResult>;
105
+ /** Served-side result the AI-SDK consumer already holds from streamText. */
106
+ interface ProbeShadowServed {
107
+ /** Model id that actually served the user (becomes `current_model` on the row). */
108
+ model: string;
109
+ /** The served response text (stored as a preview; never the prompt). */
110
+ responseText: string;
111
+ /** Served prompt tokens (AI SDK `usage.promptTokens`). */
112
+ tokensIn: number;
113
+ /** Served completion tokens (AI SDK `usage.completionTokens`). */
114
+ tokensOut: number;
115
+ /** The user's actual wait in ms (alpha.46 served-latency axis). */
116
+ latencyMs: number;
117
+ }
118
+ /** Options for the standalone {@link probeShadow} export. */
119
+ interface ProbeShadowOptions {
120
+ /** The served leg the consumer supplies; kgauto runs the candidate leg. */
121
+ served: ProbeShadowServed;
122
+ /** Model id(s)/family alias(es) to shadow-test against the served model. */
123
+ candidates: string | string[];
124
+ /** Probability [0,1] a given call fires a probe. Default 0.05; pin 1 for dogfood. */
125
+ sampleRate?: number;
126
+ /** Phase 1 = 'off' (offline rollup). 'opus' inline judge is Phase 2. Default 'off'. */
127
+ judge?: 'off' | 'opus';
128
+ /** Override API keys (defaults: process.env). Gates candidate reachability. */
129
+ apiKeys?: ApiKeys;
130
+ /** Override fetch (for tests / custom transport). */
131
+ fetchImpl?: typeof fetch;
132
+ /** Provider-specific request fields shallow-merged into the candidate request. */
133
+ providerOverrides?: ProviderOverrides;
134
+ /** Forwarded to compile() for the candidate run (match the served-side compile). */
135
+ policy?: CompilePolicy;
136
+ toolRelevanceThreshold?: number;
137
+ compressHistoryAfter?: number;
138
+ }
139
+ /**
140
+ * s54 — standalone full-IR shadow probe for AI-SDK consumers (tt-intel hunt, IC
141
+ * chat) that own their own streamText round-trip and never call call(). Fire it
142
+ * from the streamText `onFinish` callback, wrapped in the platform's
143
+ * `waitUntil()` / `after()`, so it runs after the response is flushed — no
144
+ * blocking, no `sync:true`, no teardown race (cleaner than call()'s in-process
145
+ * fire-and-forget for Edge runtimes; L-086).
146
+ *
147
+ * Runs the SAME candidate loop as the call()-inline probe ({@link
148
+ * runProbeCandidates}): same gates (sample, self-skip, G4 judge-self,
149
+ * reachability, G6), same fidelity=1.0 full-IR rows, same alpha.46 latency
150
+ * capture. The consumer supplies the served leg it already has; kgauto runs the
151
+ * candidate(s) and writes the `probe_outcomes` row.
152
+ *
153
+ * Requires `configureBrain()` to have been called (same as call()).
154
+ * Fire-and-forget: never throws into the caller.
155
+ *
156
+ * @example
157
+ * const result = streamText({ model, system, messages,
158
+ * onFinish: ({ text, usage }) => {
159
+ * after(() => probeShadow(ir, {
160
+ * served: { model: servedModelId, responseText: text,
161
+ * tokensIn: usage.promptTokens, tokensOut: usage.completionTokens,
162
+ * latencyMs: Date.now() - t0 },
163
+ * candidates: ['deepseek-v4-pro'], sampleRate: 0.05,
164
+ * }));
165
+ * },
166
+ * });
167
+ */
168
+ declare function probeShadow(ir: PromptIR, opts: ProbeShadowOptions): Promise<void>;
105
169
 
106
170
  /**
107
171
  * alpha.33 — AI-SDK streamText helpers.
@@ -1780,6 +1844,10 @@ interface ModelBrainRow {
1780
1844
  active?: boolean;
1781
1845
  /** alpha.41 — model-family tag (migration 024 column). */
1782
1846
  family?: string | null;
1847
+ /** alpha.49 — explicit latency bucket (migration 028 column). */
1848
+ latency_tier?: string | null;
1849
+ /** alpha.49 — per-archetype prompt-shape conventions (migration 028 column). */
1850
+ archetype_conventions?: ArchetypeConvention[] | null;
1783
1851
  }
1784
1852
  interface ProfileToRowOptions {
1785
1853
  /** e.g. `'2.0.0-alpha.12'` — leave undefined to omit from row. */
@@ -2195,4 +2263,4 @@ declare function markPromoteReadyHandled(opts: MarkPromoteReadyHandledOptions):
2195
2263
  */
2196
2264
  declare function compile(ir: PromptIR, opts?: CompileOptions): CompileResult;
2197
2265
 
2198
- export { ABSOLUTE_FLOOR, type AISDKConvertedMessage, ARCHETYPE_FAMILY_FITS, ARCHETYPE_FLOOR_DEFAULT, type ActionableAdvisory, Adapter, type AdvisoryResolutionSource, type AdvisorySeverity, type AdvisoryStatus, type AdvisorySuggestedFix, ApiKeys, type AppOracle, type ApplySectionRewritesArgs, type ApplySectionRewritesResult, ArchetypeConvention, type ArchetypeFamilyFit, type ArchetypePerfMap, type ArchetypePerfNMap, type ArchetypePerfScoreResult, type AttachCacheControlResult, BestPracticeAdvisory, type BrainConfig, type BrainQueryConfig, type BrainReadEnv, CallOptions, CallResult, ChainEntry, type CompatibilityIntent, type CompileOptions, CompilePolicy, CompileResult, CompiledRequest, DEFAULT_FINDINGS_ENDPOINT, type ExclusionFindingRow, type ExclusionResolutionSource, type ExecuteErr, type ExecuteOk, type ExecuteOptions, type ExecuteResult, type FallbackPosture, FamilyResolutionError, type GetActionableAdvisoriesOptions, type GetDefaultFallbackChainOpts, type GetPerAxisMetricsOpts, type GetRecommendedPrimaryOptions, Grounding, IntentArchetypeName, type LLMJudgeOptions, MEASURED_GROUNDING_MIN_N, type MarkAdvisoryResolvedOptions, type MarkExclusionFindingHandledOptions, type MarkPromoteReadyHandledOptions, type ModelBrainRow, type ModelCompatibility, ModelProfile, NormalizedResponse, type OracleContext, OracleScore, type OutcomePayload, OutcomeResult, PRODUCER_OWNED_RULE_CODES, PROVIDER_ENV_KEYS, PerAxisMetrics, type PricingRow, type ProfileToRowOptions, type PromoteReadyFindingRow, type PromoteReadyResolution, PromptIR, Provider, ProviderOverrides, type ProviderReachability, RULE_SEQUENTIAL_TOOL_CLIFF, type ReachabilityOpts, RecordInput, RecordOutcomeInput, type RunAdvisorPhase2Context, SectionRewrite, type ShadowProbeRecordInput, type SupportedProvider, SystemModelMessage, TRANSLATOR_FLOOR, applyArchetypeConvention, applySectionRewrites, attachCacheControlToStreamTextInput, buildLLMJudge, buildShadowProbeRow, call, clearBrain, compile, configureBrain, countTokens, deriveFamilyFromModelId, deriveOwnership, execute, findBetterFit, getActionableAdvisories, getAllStarterChains, getAllStarterChainsWithGrounding, getArchetypePerfScore, getDefaultFallbackChain, getDefaultFallbackChainWithGrounding, getModelCompatibility, getPerAxisMetrics, getReachabilityDiagnostic, getRecommendedPrimary, getSequentialStarterChain, getSequentialStarterChainWithGrounding, getStaleExclusionFindings, getStarterChain, getStarterChainWithGrounding, isBrainQueryActiveFor, isBrainSync, isExclusionFindingsBrainActive, isModelReachable, isProviderReachable, loadAliasesFromBrain, loadArchetypePerfFromBrain, loadArchetypePerfNFromBrain, loadChainsFromBrain, loadModelsFromBrain, loadPricingFromBrain, markAdvisoryResolved, markExclusionFindingHandled, markPromoteReadyHandled, profileToRow, readBrainReadEnv, record, recordOutcome, recordShadowProbe, resetTokenizer, resolveConventionsForProfile, resolvePricingAt, resolveProviderKey, runAdvisor, setTokenizer };
2266
+ export { ABSOLUTE_FLOOR, type AISDKConvertedMessage, ARCHETYPE_FAMILY_FITS, ARCHETYPE_FLOOR_DEFAULT, type ActionableAdvisory, Adapter, type AdvisoryResolutionSource, type AdvisorySeverity, type AdvisoryStatus, type AdvisorySuggestedFix, ApiKeys, type AppOracle, type ApplySectionRewritesArgs, type ApplySectionRewritesResult, ArchetypeConvention, type ArchetypeFamilyFit, type ArchetypePerfMap, type ArchetypePerfNMap, type ArchetypePerfScoreResult, type AttachCacheControlResult, BestPracticeAdvisory, type BrainConfig, type BrainQueryConfig, type BrainReadEnv, CallOptions, CallResult, ChainEntry, type CompatibilityIntent, type CompileOptions, CompilePolicy, CompileResult, CompiledRequest, DEFAULT_FINDINGS_ENDPOINT, type ExclusionFindingRow, type ExclusionResolutionSource, type ExecuteErr, type ExecuteOk, type ExecuteOptions, type ExecuteResult, type FallbackPosture, FamilyResolutionError, type GetActionableAdvisoriesOptions, type GetDefaultFallbackChainOpts, type GetPerAxisMetricsOpts, type GetRecommendedPrimaryOptions, Grounding, IntentArchetypeName, type LLMJudgeOptions, MEASURED_GROUNDING_MIN_N, type MarkAdvisoryResolvedOptions, type MarkExclusionFindingHandledOptions, type MarkPromoteReadyHandledOptions, type ModelBrainRow, type ModelCompatibility, ModelProfile, NormalizedResponse, type OracleContext, OracleScore, type OutcomePayload, OutcomeResult, PRODUCER_OWNED_RULE_CODES, PROVIDER_ENV_KEYS, PerAxisMetrics, type PricingRow, type ProbeShadowOptions, type ProbeShadowServed, type ProfileToRowOptions, type PromoteReadyFindingRow, type PromoteReadyResolution, PromptIR, Provider, ProviderOverrides, type ProviderReachability, RULE_SEQUENTIAL_TOOL_CLIFF, type ReachabilityOpts, RecordInput, RecordOutcomeInput, type RunAdvisorPhase2Context, SectionRewrite, type ShadowProbeRecordInput, type SupportedProvider, SystemModelMessage, TRANSLATOR_FLOOR, applyArchetypeConvention, applySectionRewrites, attachCacheControlToStreamTextInput, buildLLMJudge, buildShadowProbeRow, call, clearBrain, compile, configureBrain, countTokens, deriveFamilyFromModelId, deriveOwnership, execute, findBetterFit, getActionableAdvisories, getAllStarterChains, getAllStarterChainsWithGrounding, getArchetypePerfScore, getDefaultFallbackChain, getDefaultFallbackChainWithGrounding, getModelCompatibility, getPerAxisMetrics, getReachabilityDiagnostic, getRecommendedPrimary, getSequentialStarterChain, getSequentialStarterChainWithGrounding, getStaleExclusionFindings, getStarterChain, getStarterChainWithGrounding, isBrainQueryActiveFor, isBrainSync, isExclusionFindingsBrainActive, isModelReachable, isProviderReachable, loadAliasesFromBrain, loadArchetypePerfFromBrain, loadArchetypePerfNFromBrain, loadChainsFromBrain, loadModelsFromBrain, loadPricingFromBrain, markAdvisoryResolved, markExclusionFindingHandled, markPromoteReadyHandled, probeShadow, profileToRow, readBrainReadEnv, record, recordOutcome, recordShadowProbe, resetTokenizer, resolveConventionsForProfile, resolvePricingAt, resolveProviderKey, runAdvisor, setTokenizer };
package/dist/index.d.ts CHANGED
@@ -102,6 +102,70 @@ declare function execute(request: CompiledRequest, opts?: ExecuteOptions): Promi
102
102
  * exhausted without success.
103
103
  */
104
104
  declare function call(ir: PromptIR, opts?: CallOptions): Promise<CallResult>;
105
+ /** Served-side result the AI-SDK consumer already holds from streamText. */
106
+ interface ProbeShadowServed {
107
+ /** Model id that actually served the user (becomes `current_model` on the row). */
108
+ model: string;
109
+ /** The served response text (stored as a preview; never the prompt). */
110
+ responseText: string;
111
+ /** Served prompt tokens (AI SDK `usage.promptTokens`). */
112
+ tokensIn: number;
113
+ /** Served completion tokens (AI SDK `usage.completionTokens`). */
114
+ tokensOut: number;
115
+ /** The user's actual wait in ms (alpha.46 served-latency axis). */
116
+ latencyMs: number;
117
+ }
118
+ /** Options for the standalone {@link probeShadow} export. */
119
+ interface ProbeShadowOptions {
120
+ /** The served leg the consumer supplies; kgauto runs the candidate leg. */
121
+ served: ProbeShadowServed;
122
+ /** Model id(s)/family alias(es) to shadow-test against the served model. */
123
+ candidates: string | string[];
124
+ /** Probability [0,1] a given call fires a probe. Default 0.05; pin 1 for dogfood. */
125
+ sampleRate?: number;
126
+ /** Phase 1 = 'off' (offline rollup). 'opus' inline judge is Phase 2. Default 'off'. */
127
+ judge?: 'off' | 'opus';
128
+ /** Override API keys (defaults: process.env). Gates candidate reachability. */
129
+ apiKeys?: ApiKeys;
130
+ /** Override fetch (for tests / custom transport). */
131
+ fetchImpl?: typeof fetch;
132
+ /** Provider-specific request fields shallow-merged into the candidate request. */
133
+ providerOverrides?: ProviderOverrides;
134
+ /** Forwarded to compile() for the candidate run (match the served-side compile). */
135
+ policy?: CompilePolicy;
136
+ toolRelevanceThreshold?: number;
137
+ compressHistoryAfter?: number;
138
+ }
139
+ /**
140
+ * s54 — standalone full-IR shadow probe for AI-SDK consumers (tt-intel hunt, IC
141
+ * chat) that own their own streamText round-trip and never call call(). Fire it
142
+ * from the streamText `onFinish` callback, wrapped in the platform's
143
+ * `waitUntil()` / `after()`, so it runs after the response is flushed — no
144
+ * blocking, no `sync:true`, no teardown race (cleaner than call()'s in-process
145
+ * fire-and-forget for Edge runtimes; L-086).
146
+ *
147
+ * Runs the SAME candidate loop as the call()-inline probe ({@link
148
+ * runProbeCandidates}): same gates (sample, self-skip, G4 judge-self,
149
+ * reachability, G6), same fidelity=1.0 full-IR rows, same alpha.46 latency
150
+ * capture. The consumer supplies the served leg it already has; kgauto runs the
151
+ * candidate(s) and writes the `probe_outcomes` row.
152
+ *
153
+ * Requires `configureBrain()` to have been called (same as call()).
154
+ * Fire-and-forget: never throws into the caller.
155
+ *
156
+ * @example
157
+ * const result = streamText({ model, system, messages,
158
+ * onFinish: ({ text, usage }) => {
159
+ * after(() => probeShadow(ir, {
160
+ * served: { model: servedModelId, responseText: text,
161
+ * tokensIn: usage.promptTokens, tokensOut: usage.completionTokens,
162
+ * latencyMs: Date.now() - t0 },
163
+ * candidates: ['deepseek-v4-pro'], sampleRate: 0.05,
164
+ * }));
165
+ * },
166
+ * });
167
+ */
168
+ declare function probeShadow(ir: PromptIR, opts: ProbeShadowOptions): Promise<void>;
105
169
 
106
170
  /**
107
171
  * alpha.33 — AI-SDK streamText helpers.
@@ -1780,6 +1844,10 @@ interface ModelBrainRow {
1780
1844
  active?: boolean;
1781
1845
  /** alpha.41 — model-family tag (migration 024 column). */
1782
1846
  family?: string | null;
1847
+ /** alpha.49 — explicit latency bucket (migration 028 column). */
1848
+ latency_tier?: string | null;
1849
+ /** alpha.49 — per-archetype prompt-shape conventions (migration 028 column). */
1850
+ archetype_conventions?: ArchetypeConvention[] | null;
1783
1851
  }
1784
1852
  interface ProfileToRowOptions {
1785
1853
  /** e.g. `'2.0.0-alpha.12'` — leave undefined to omit from row. */
@@ -2195,4 +2263,4 @@ declare function markPromoteReadyHandled(opts: MarkPromoteReadyHandledOptions):
2195
2263
  */
2196
2264
  declare function compile(ir: PromptIR, opts?: CompileOptions): CompileResult;
2197
2265
 
2198
- export { ABSOLUTE_FLOOR, type AISDKConvertedMessage, ARCHETYPE_FAMILY_FITS, ARCHETYPE_FLOOR_DEFAULT, type ActionableAdvisory, Adapter, type AdvisoryResolutionSource, type AdvisorySeverity, type AdvisoryStatus, type AdvisorySuggestedFix, ApiKeys, type AppOracle, type ApplySectionRewritesArgs, type ApplySectionRewritesResult, ArchetypeConvention, type ArchetypeFamilyFit, type ArchetypePerfMap, type ArchetypePerfNMap, type ArchetypePerfScoreResult, type AttachCacheControlResult, BestPracticeAdvisory, type BrainConfig, type BrainQueryConfig, type BrainReadEnv, CallOptions, CallResult, ChainEntry, type CompatibilityIntent, type CompileOptions, CompilePolicy, CompileResult, CompiledRequest, DEFAULT_FINDINGS_ENDPOINT, type ExclusionFindingRow, type ExclusionResolutionSource, type ExecuteErr, type ExecuteOk, type ExecuteOptions, type ExecuteResult, type FallbackPosture, FamilyResolutionError, type GetActionableAdvisoriesOptions, type GetDefaultFallbackChainOpts, type GetPerAxisMetricsOpts, type GetRecommendedPrimaryOptions, Grounding, IntentArchetypeName, type LLMJudgeOptions, MEASURED_GROUNDING_MIN_N, type MarkAdvisoryResolvedOptions, type MarkExclusionFindingHandledOptions, type MarkPromoteReadyHandledOptions, type ModelBrainRow, type ModelCompatibility, ModelProfile, NormalizedResponse, type OracleContext, OracleScore, type OutcomePayload, OutcomeResult, PRODUCER_OWNED_RULE_CODES, PROVIDER_ENV_KEYS, PerAxisMetrics, type PricingRow, type ProfileToRowOptions, type PromoteReadyFindingRow, type PromoteReadyResolution, PromptIR, Provider, ProviderOverrides, type ProviderReachability, RULE_SEQUENTIAL_TOOL_CLIFF, type ReachabilityOpts, RecordInput, RecordOutcomeInput, type RunAdvisorPhase2Context, SectionRewrite, type ShadowProbeRecordInput, type SupportedProvider, SystemModelMessage, TRANSLATOR_FLOOR, applyArchetypeConvention, applySectionRewrites, attachCacheControlToStreamTextInput, buildLLMJudge, buildShadowProbeRow, call, clearBrain, compile, configureBrain, countTokens, deriveFamilyFromModelId, deriveOwnership, execute, findBetterFit, getActionableAdvisories, getAllStarterChains, getAllStarterChainsWithGrounding, getArchetypePerfScore, getDefaultFallbackChain, getDefaultFallbackChainWithGrounding, getModelCompatibility, getPerAxisMetrics, getReachabilityDiagnostic, getRecommendedPrimary, getSequentialStarterChain, getSequentialStarterChainWithGrounding, getStaleExclusionFindings, getStarterChain, getStarterChainWithGrounding, isBrainQueryActiveFor, isBrainSync, isExclusionFindingsBrainActive, isModelReachable, isProviderReachable, loadAliasesFromBrain, loadArchetypePerfFromBrain, loadArchetypePerfNFromBrain, loadChainsFromBrain, loadModelsFromBrain, loadPricingFromBrain, markAdvisoryResolved, markExclusionFindingHandled, markPromoteReadyHandled, profileToRow, readBrainReadEnv, record, recordOutcome, recordShadowProbe, resetTokenizer, resolveConventionsForProfile, resolvePricingAt, resolveProviderKey, runAdvisor, setTokenizer };
2266
+ export { ABSOLUTE_FLOOR, type AISDKConvertedMessage, ARCHETYPE_FAMILY_FITS, ARCHETYPE_FLOOR_DEFAULT, type ActionableAdvisory, Adapter, type AdvisoryResolutionSource, type AdvisorySeverity, type AdvisoryStatus, type AdvisorySuggestedFix, ApiKeys, type AppOracle, type ApplySectionRewritesArgs, type ApplySectionRewritesResult, ArchetypeConvention, type ArchetypeFamilyFit, type ArchetypePerfMap, type ArchetypePerfNMap, type ArchetypePerfScoreResult, type AttachCacheControlResult, BestPracticeAdvisory, type BrainConfig, type BrainQueryConfig, type BrainReadEnv, CallOptions, CallResult, ChainEntry, type CompatibilityIntent, type CompileOptions, CompilePolicy, CompileResult, CompiledRequest, DEFAULT_FINDINGS_ENDPOINT, type ExclusionFindingRow, type ExclusionResolutionSource, type ExecuteErr, type ExecuteOk, type ExecuteOptions, type ExecuteResult, type FallbackPosture, FamilyResolutionError, type GetActionableAdvisoriesOptions, type GetDefaultFallbackChainOpts, type GetPerAxisMetricsOpts, type GetRecommendedPrimaryOptions, Grounding, IntentArchetypeName, type LLMJudgeOptions, MEASURED_GROUNDING_MIN_N, type MarkAdvisoryResolvedOptions, type MarkExclusionFindingHandledOptions, type MarkPromoteReadyHandledOptions, type ModelBrainRow, type ModelCompatibility, ModelProfile, NormalizedResponse, type OracleContext, OracleScore, type OutcomePayload, OutcomeResult, PRODUCER_OWNED_RULE_CODES, PROVIDER_ENV_KEYS, PerAxisMetrics, type PricingRow, type ProbeShadowOptions, type ProbeShadowServed, type ProfileToRowOptions, type PromoteReadyFindingRow, type PromoteReadyResolution, PromptIR, Provider, ProviderOverrides, type ProviderReachability, RULE_SEQUENTIAL_TOOL_CLIFF, type ReachabilityOpts, RecordInput, RecordOutcomeInput, type RunAdvisorPhase2Context, SectionRewrite, type ShadowProbeRecordInput, type SupportedProvider, SystemModelMessage, TRANSLATOR_FLOOR, applyArchetypeConvention, applySectionRewrites, attachCacheControlToStreamTextInput, buildLLMJudge, buildShadowProbeRow, call, clearBrain, compile, configureBrain, countTokens, deriveFamilyFromModelId, deriveOwnership, execute, findBetterFit, getActionableAdvisories, getAllStarterChains, getAllStarterChainsWithGrounding, getArchetypePerfScore, getDefaultFallbackChain, getDefaultFallbackChainWithGrounding, getModelCompatibility, getPerAxisMetrics, getReachabilityDiagnostic, getRecommendedPrimary, getSequentialStarterChain, getSequentialStarterChainWithGrounding, getStaleExclusionFindings, getStarterChain, getStarterChainWithGrounding, isBrainQueryActiveFor, isBrainSync, isExclusionFindingsBrainActive, isModelReachable, isProviderReachable, loadAliasesFromBrain, loadArchetypePerfFromBrain, loadArchetypePerfNFromBrain, loadChainsFromBrain, loadModelsFromBrain, loadPricingFromBrain, markAdvisoryResolved, markExclusionFindingHandled, markPromoteReadyHandled, probeShadow, profileToRow, readBrainReadEnv, record, recordOutcome, recordShadowProbe, resetTokenizer, resolveConventionsForProfile, resolvePricingAt, resolveProviderKey, runAdvisor, setTokenizer };
package/dist/index.js CHANGED
@@ -88,6 +88,7 @@ __export(index_exports, {
88
88
  markAdvisoryResolved: () => markAdvisoryResolved,
89
89
  markExclusionFindingHandled: () => markExclusionFindingHandled,
90
90
  markPromoteReadyHandled: () => markPromoteReadyHandled,
91
+ probeShadow: () => probeShadow,
91
92
  profileToRow: () => profileToRow,
92
93
  profilesByProvider: () => profilesByProvider,
93
94
  readBrainReadEnv: () => readBrainReadEnv,
@@ -1586,6 +1587,9 @@ function rowToProfile(row) {
1586
1587
  if (row.lowering !== void 0 && row.lowering !== null && (typeof row.lowering !== "object" || Array.isArray(row.lowering))) {
1587
1588
  return null;
1588
1589
  }
1590
+ if (row.archetype_conventions !== void 0 && row.archetype_conventions !== null && !Array.isArray(row.archetype_conventions)) {
1591
+ return null;
1592
+ }
1589
1593
  return {
1590
1594
  id: row.model_id,
1591
1595
  provider: row.provider,
@@ -1612,12 +1616,21 @@ function rowToProfile(row) {
1612
1616
  // resolution time (see family-resolution.ts, G1).
1613
1617
  family: row.family ?? void 0,
1614
1618
  versionAdded: row.version_added ?? void 0,
1615
- active: row.active ?? void 0
1619
+ active: row.active ?? void 0,
1620
+ // alpha.49 — executable-knowledge fields (migration 028). Invalid
1621
+ // latency_tier → undefined (latencyTierOf derives from tags; not
1622
+ // safety-critical). archetype_conventions already array-validated above.
1623
+ latencyTier: normalizeLatencyTier(row.latency_tier),
1624
+ archetypeConventions: row.archetype_conventions ?? void 0
1616
1625
  };
1617
1626
  } catch {
1618
1627
  return null;
1619
1628
  }
1620
1629
  }
1630
+ var VALID_LATENCY_TIERS = /* @__PURE__ */ new Set(["fast", "medium", "slow"]);
1631
+ function normalizeLatencyTier(v) {
1632
+ return typeof v === "string" && VALID_LATENCY_TIERS.has(v) ? v : void 0;
1633
+ }
1621
1634
  function profileToRow(profile, opts = {}) {
1622
1635
  const row = {
1623
1636
  model_id: profile.id,
@@ -1643,7 +1656,12 @@ function profileToRow(profile, opts = {}) {
1643
1656
  // alpha.41 — round-trip family + version_added when present on profile.
1644
1657
  // version_added is operator-controlled via opts; profile-side value is
1645
1658
  // used only when opts didn't override.
1646
- family: profile.family ?? null
1659
+ family: profile.family ?? null,
1660
+ // alpha.49 — round-trip the executable-knowledge fields (migration 028)
1661
+ // so a reseed from bundled profiles makes the latency lever + schema
1662
+ // conventions live warm. Closes the silently-dropped-field gap.
1663
+ latency_tier: profile.latencyTier ?? null,
1664
+ archetype_conventions: profile.archetypeConventions ?? null
1647
1665
  };
1648
1666
  if (opts.verifiedAgainstDocs !== void 0) {
1649
1667
  row.verified_against_docs = opts.verifiedAgainstDocs;
@@ -2388,6 +2406,17 @@ function passApplyCliffs(ir, profile, estimatedInputTokens) {
2388
2406
  }
2389
2407
  var LATENCY_OVERAGE_WEIGHT = 0.6;
2390
2408
  var LATENCY_PENALTY_CAP = 1;
2409
+ var QUALITY_GATE_PENALTY = 4;
2410
+ function effectiveConventions(profile) {
2411
+ const own = profile.archetypeConventions ?? [];
2412
+ if (own.length > 0) return own;
2413
+ const family = profile.family ?? deriveFamilyFromModelId(profile.id);
2414
+ if (!family) return [];
2415
+ const repId = familyRepId(family);
2416
+ if (!repId || repId === profile.id) return [];
2417
+ const rep = tryGetProfile(repId);
2418
+ return rep?.archetypeConventions ?? [];
2419
+ }
2391
2420
  function passScoreTargets(ir, opts) {
2392
2421
  const constraints = ir.constraints ?? {};
2393
2422
  const policy = opts.policy ?? {};
@@ -2452,7 +2481,14 @@ function passScoreTargets(ir, opts) {
2452
2481
  latencyPenalty = Math.min(LATENCY_OVERAGE_WEIGHT * overage, LATENCY_PENALTY_CAP);
2453
2482
  }
2454
2483
  }
2455
- const rank = qualityScore + callerOrderBoost - costPenalty - reasons.length * 10 + preferredBoost - latencyPenalty;
2484
+ let qualityGatePenalty = 0;
2485
+ if (constraints.structuredOutput) {
2486
+ const schemaWeak = effectiveConventions(profile).some(
2487
+ (c) => c.archetype === ir.intent.archetype && c.structuredOutputHint === "avoid"
2488
+ );
2489
+ if (schemaWeak) qualityGatePenalty = QUALITY_GATE_PENALTY;
2490
+ }
2491
+ const rank = qualityScore + callerOrderBoost - costPenalty - reasons.length * 10 + preferredBoost - latencyPenalty - qualityGatePenalty;
2456
2492
  scores.push({
2457
2493
  modelId,
2458
2494
  estimatedCostUsd,
@@ -2494,6 +2530,14 @@ function passScoreTargets(ir, opts) {
2494
2530
  description: `Model ${modelId} rank down ${latencyPenalty.toFixed(2)} \u2014 latency tier '${tier}' (~${LATENCY_TIER_MS[tier]}ms) exceeds constraints.maxLatencyMs (${maxLatencyMs}ms)`
2495
2531
  });
2496
2532
  }
2533
+ if (qualityGatePenalty > 0) {
2534
+ policyMutations.push({
2535
+ id: `quality-gate-structured-${modelId}`,
2536
+ source: "quality_gate",
2537
+ passName: "score_targets",
2538
+ description: `Model ${modelId} gated below the quality floor for archetype '${ir.intent.archetype}' \u2014 declared structuredOutput + kgauto convention flags it schema-weak for this contract (structuredOutputHint:'avoid'). Down-ranked out of leadership; retained as graceful fallback only.`
2539
+ });
2540
+ }
2497
2541
  }
2498
2542
  return { value: scores, mutations: policyMutations };
2499
2543
  }
@@ -6157,16 +6201,15 @@ function hashForProbe(s) {
6157
6201
  for (let i = 0; i < s.length; i++) h = (h << 5) - h + s.charCodeAt(i) | 0;
6158
6202
  return `h${(h >>> 0).toString(16)}`;
6159
6203
  }
6160
- async function runShadowProbe(args) {
6204
+ async function runProbeCandidates(args) {
6161
6205
  try {
6162
- const cfg = args.opts.shadowProbe;
6163
- if (!shouldSampleProbe(cfg.sampleRate ?? 0.05)) return;
6164
- const candidates = normalizeProbeCandidates(cfg.candidates);
6206
+ if (!shouldSampleProbe(args.cfg.sampleRate ?? 0.05)) return;
6207
+ const candidates = normalizeProbeCandidates(args.cfg.candidates);
6165
6208
  const promptHash = hashForProbe(args.ir.currentTurn?.content ?? "");
6166
6209
  for (const candidate of candidates) {
6167
6210
  if (candidate === args.servedModel) continue;
6168
6211
  if (deriveFamilyFromModelId(candidate) === "claude-opus") continue;
6169
- if (!isModelReachable(candidate, { apiKeys: args.opts.apiKeys })) continue;
6212
+ if (!isModelReachable(candidate, { apiKeys: args.exec.apiKeys })) continue;
6170
6213
  try {
6171
6214
  const candCompile = compileAndRegister(
6172
6215
  {
@@ -6174,13 +6217,13 @@ async function runShadowProbe(args) {
6174
6217
  models: args.ir.models.includes(candidate) ? args.ir.models : [candidate, ...args.ir.models],
6175
6218
  constraints: { ...args.ir.constraints ?? {}, forceModel: candidate }
6176
6219
  },
6177
- args.opts
6220
+ args.exec
6178
6221
  );
6179
6222
  const candStart = Date.now();
6180
6223
  const exec = await execute(candCompile.request, {
6181
- apiKeys: args.opts.apiKeys,
6182
- fetchImpl: args.opts.fetchImpl,
6183
- providerOverrides: args.opts.providerOverrides
6224
+ apiKeys: args.exec.apiKeys,
6225
+ fetchImpl: args.exec.fetchImpl,
6226
+ providerOverrides: args.exec.providerOverrides
6184
6227
  });
6185
6228
  const candidateLatencyMs = Date.now() - candStart;
6186
6229
  if (!exec.ok) continue;
@@ -6209,6 +6252,54 @@ async function runShadowProbe(args) {
6209
6252
  } catch {
6210
6253
  }
6211
6254
  }
6255
+ async function runShadowProbe(args) {
6256
+ const cfg = args.opts.shadowProbe;
6257
+ if (!cfg) return;
6258
+ await runProbeCandidates({
6259
+ ir: args.ir,
6260
+ cfg,
6261
+ servedModel: args.servedModel,
6262
+ servedResponse: args.servedResponse,
6263
+ servedLatencyMs: args.servedLatencyMs,
6264
+ // CallOptions is a structural superset of ProbeExecOpts.
6265
+ exec: args.opts
6266
+ });
6267
+ }
6268
+ async function probeShadow(ir, opts) {
6269
+ try {
6270
+ const servedResponse = {
6271
+ text: opts.served.responseText,
6272
+ structuredOutput: null,
6273
+ toolCalls: [],
6274
+ tokens: {
6275
+ input: opts.served.tokensIn,
6276
+ output: opts.served.tokensOut,
6277
+ total: opts.served.tokensIn + opts.served.tokensOut
6278
+ },
6279
+ raw: null
6280
+ };
6281
+ await runProbeCandidates({
6282
+ ir,
6283
+ cfg: {
6284
+ candidates: opts.candidates,
6285
+ sampleRate: opts.sampleRate,
6286
+ judge: opts.judge ?? "off"
6287
+ },
6288
+ servedModel: opts.served.model,
6289
+ servedResponse,
6290
+ servedLatencyMs: opts.served.latencyMs,
6291
+ exec: {
6292
+ policy: opts.policy,
6293
+ toolRelevanceThreshold: opts.toolRelevanceThreshold,
6294
+ compressHistoryAfter: opts.compressHistoryAfter,
6295
+ apiKeys: opts.apiKeys,
6296
+ fetchImpl: opts.fetchImpl,
6297
+ providerOverrides: opts.providerOverrides
6298
+ }
6299
+ });
6300
+ } catch {
6301
+ }
6302
+ }
6212
6303
  function validateStructuredContract(exec, ir) {
6213
6304
  if (!ir.constraints?.structuredOutput) {
6214
6305
  return { ok: true, response: exec.response };
@@ -6795,6 +6886,7 @@ function compile2(ir, opts) {
6795
6886
  markAdvisoryResolved,
6796
6887
  markExclusionFindingHandled,
6797
6888
  markPromoteReadyHandled,
6889
+ probeShadow,
6798
6890
  profileToRow,
6799
6891
  profilesByProvider,
6800
6892
  readBrainReadEnv,
package/dist/index.mjs CHANGED
@@ -76,6 +76,9 @@ function rowToProfile(row) {
76
76
  if (row.lowering !== void 0 && row.lowering !== null && (typeof row.lowering !== "object" || Array.isArray(row.lowering))) {
77
77
  return null;
78
78
  }
79
+ if (row.archetype_conventions !== void 0 && row.archetype_conventions !== null && !Array.isArray(row.archetype_conventions)) {
80
+ return null;
81
+ }
79
82
  return {
80
83
  id: row.model_id,
81
84
  provider: row.provider,
@@ -102,12 +105,21 @@ function rowToProfile(row) {
102
105
  // resolution time (see family-resolution.ts, G1).
103
106
  family: row.family ?? void 0,
104
107
  versionAdded: row.version_added ?? void 0,
105
- active: row.active ?? void 0
108
+ active: row.active ?? void 0,
109
+ // alpha.49 — executable-knowledge fields (migration 028). Invalid
110
+ // latency_tier → undefined (latencyTierOf derives from tags; not
111
+ // safety-critical). archetype_conventions already array-validated above.
112
+ latencyTier: normalizeLatencyTier(row.latency_tier),
113
+ archetypeConventions: row.archetype_conventions ?? void 0
106
114
  };
107
115
  } catch {
108
116
  return null;
109
117
  }
110
118
  }
119
+ var VALID_LATENCY_TIERS = /* @__PURE__ */ new Set(["fast", "medium", "slow"]);
120
+ function normalizeLatencyTier(v) {
121
+ return typeof v === "string" && VALID_LATENCY_TIERS.has(v) ? v : void 0;
122
+ }
111
123
  function profileToRow(profile, opts = {}) {
112
124
  const row = {
113
125
  model_id: profile.id,
@@ -133,7 +145,12 @@ function profileToRow(profile, opts = {}) {
133
145
  // alpha.41 — round-trip family + version_added when present on profile.
134
146
  // version_added is operator-controlled via opts; profile-side value is
135
147
  // used only when opts didn't override.
136
- family: profile.family ?? null
148
+ family: profile.family ?? null,
149
+ // alpha.49 — round-trip the executable-knowledge fields (migration 028)
150
+ // so a reseed from bundled profiles makes the latency lever + schema
151
+ // conventions live warm. Closes the silently-dropped-field gap.
152
+ latency_tier: profile.latencyTier ?? null,
153
+ archetype_conventions: profile.archetypeConventions ?? null
137
154
  };
138
155
  if (opts.verifiedAgainstDocs !== void 0) {
139
156
  row.verified_against_docs = opts.verifiedAgainstDocs;
@@ -714,6 +731,17 @@ function passApplyCliffs(ir, profile, estimatedInputTokens) {
714
731
  }
715
732
  var LATENCY_OVERAGE_WEIGHT = 0.6;
716
733
  var LATENCY_PENALTY_CAP = 1;
734
+ var QUALITY_GATE_PENALTY = 4;
735
+ function effectiveConventions(profile) {
736
+ const own = profile.archetypeConventions ?? [];
737
+ if (own.length > 0) return own;
738
+ const family = profile.family ?? deriveFamilyFromModelId(profile.id);
739
+ if (!family) return [];
740
+ const repId = familyRepId(family);
741
+ if (!repId || repId === profile.id) return [];
742
+ const rep = tryGetProfile(repId);
743
+ return rep?.archetypeConventions ?? [];
744
+ }
717
745
  function passScoreTargets(ir, opts) {
718
746
  const constraints = ir.constraints ?? {};
719
747
  const policy = opts.policy ?? {};
@@ -778,7 +806,14 @@ function passScoreTargets(ir, opts) {
778
806
  latencyPenalty = Math.min(LATENCY_OVERAGE_WEIGHT * overage, LATENCY_PENALTY_CAP);
779
807
  }
780
808
  }
781
- const rank = qualityScore + callerOrderBoost - costPenalty - reasons.length * 10 + preferredBoost - latencyPenalty;
809
+ let qualityGatePenalty = 0;
810
+ if (constraints.structuredOutput) {
811
+ const schemaWeak = effectiveConventions(profile).some(
812
+ (c) => c.archetype === ir.intent.archetype && c.structuredOutputHint === "avoid"
813
+ );
814
+ if (schemaWeak) qualityGatePenalty = QUALITY_GATE_PENALTY;
815
+ }
816
+ const rank = qualityScore + callerOrderBoost - costPenalty - reasons.length * 10 + preferredBoost - latencyPenalty - qualityGatePenalty;
782
817
  scores.push({
783
818
  modelId,
784
819
  estimatedCostUsd,
@@ -820,6 +855,14 @@ function passScoreTargets(ir, opts) {
820
855
  description: `Model ${modelId} rank down ${latencyPenalty.toFixed(2)} \u2014 latency tier '${tier}' (~${LATENCY_TIER_MS[tier]}ms) exceeds constraints.maxLatencyMs (${maxLatencyMs}ms)`
821
856
  });
822
857
  }
858
+ if (qualityGatePenalty > 0) {
859
+ policyMutations.push({
860
+ id: `quality-gate-structured-${modelId}`,
861
+ source: "quality_gate",
862
+ passName: "score_targets",
863
+ description: `Model ${modelId} gated below the quality floor for archetype '${ir.intent.archetype}' \u2014 declared structuredOutput + kgauto convention flags it schema-weak for this contract (structuredOutputHint:'avoid'). Down-ranked out of leadership; retained as graceful fallback only.`
864
+ });
865
+ }
823
866
  }
824
867
  return { value: scores, mutations: policyMutations };
825
868
  }
@@ -3768,16 +3811,15 @@ function hashForProbe(s) {
3768
3811
  for (let i = 0; i < s.length; i++) h = (h << 5) - h + s.charCodeAt(i) | 0;
3769
3812
  return `h${(h >>> 0).toString(16)}`;
3770
3813
  }
3771
- async function runShadowProbe(args) {
3814
+ async function runProbeCandidates(args) {
3772
3815
  try {
3773
- const cfg = args.opts.shadowProbe;
3774
- if (!shouldSampleProbe(cfg.sampleRate ?? 0.05)) return;
3775
- const candidates = normalizeProbeCandidates(cfg.candidates);
3816
+ if (!shouldSampleProbe(args.cfg.sampleRate ?? 0.05)) return;
3817
+ const candidates = normalizeProbeCandidates(args.cfg.candidates);
3776
3818
  const promptHash = hashForProbe(args.ir.currentTurn?.content ?? "");
3777
3819
  for (const candidate of candidates) {
3778
3820
  if (candidate === args.servedModel) continue;
3779
3821
  if (deriveFamilyFromModelId(candidate) === "claude-opus") continue;
3780
- if (!isModelReachable(candidate, { apiKeys: args.opts.apiKeys })) continue;
3822
+ if (!isModelReachable(candidate, { apiKeys: args.exec.apiKeys })) continue;
3781
3823
  try {
3782
3824
  const candCompile = compileAndRegister(
3783
3825
  {
@@ -3785,13 +3827,13 @@ async function runShadowProbe(args) {
3785
3827
  models: args.ir.models.includes(candidate) ? args.ir.models : [candidate, ...args.ir.models],
3786
3828
  constraints: { ...args.ir.constraints ?? {}, forceModel: candidate }
3787
3829
  },
3788
- args.opts
3830
+ args.exec
3789
3831
  );
3790
3832
  const candStart = Date.now();
3791
3833
  const exec = await execute(candCompile.request, {
3792
- apiKeys: args.opts.apiKeys,
3793
- fetchImpl: args.opts.fetchImpl,
3794
- providerOverrides: args.opts.providerOverrides
3834
+ apiKeys: args.exec.apiKeys,
3835
+ fetchImpl: args.exec.fetchImpl,
3836
+ providerOverrides: args.exec.providerOverrides
3795
3837
  });
3796
3838
  const candidateLatencyMs = Date.now() - candStart;
3797
3839
  if (!exec.ok) continue;
@@ -3820,6 +3862,54 @@ async function runShadowProbe(args) {
3820
3862
  } catch {
3821
3863
  }
3822
3864
  }
3865
+ async function runShadowProbe(args) {
3866
+ const cfg = args.opts.shadowProbe;
3867
+ if (!cfg) return;
3868
+ await runProbeCandidates({
3869
+ ir: args.ir,
3870
+ cfg,
3871
+ servedModel: args.servedModel,
3872
+ servedResponse: args.servedResponse,
3873
+ servedLatencyMs: args.servedLatencyMs,
3874
+ // CallOptions is a structural superset of ProbeExecOpts.
3875
+ exec: args.opts
3876
+ });
3877
+ }
3878
+ async function probeShadow(ir, opts) {
3879
+ try {
3880
+ const servedResponse = {
3881
+ text: opts.served.responseText,
3882
+ structuredOutput: null,
3883
+ toolCalls: [],
3884
+ tokens: {
3885
+ input: opts.served.tokensIn,
3886
+ output: opts.served.tokensOut,
3887
+ total: opts.served.tokensIn + opts.served.tokensOut
3888
+ },
3889
+ raw: null
3890
+ };
3891
+ await runProbeCandidates({
3892
+ ir,
3893
+ cfg: {
3894
+ candidates: opts.candidates,
3895
+ sampleRate: opts.sampleRate,
3896
+ judge: opts.judge ?? "off"
3897
+ },
3898
+ servedModel: opts.served.model,
3899
+ servedResponse,
3900
+ servedLatencyMs: opts.served.latencyMs,
3901
+ exec: {
3902
+ policy: opts.policy,
3903
+ toolRelevanceThreshold: opts.toolRelevanceThreshold,
3904
+ compressHistoryAfter: opts.compressHistoryAfter,
3905
+ apiKeys: opts.apiKeys,
3906
+ fetchImpl: opts.fetchImpl,
3907
+ providerOverrides: opts.providerOverrides
3908
+ }
3909
+ });
3910
+ } catch {
3911
+ }
3912
+ }
3823
3913
  function validateStructuredContract(exec, ir) {
3824
3914
  if (!ir.constraints?.structuredOutput) {
3825
3915
  return { ok: true, response: exec.response };
@@ -4405,6 +4495,7 @@ export {
4405
4495
  markAdvisoryResolved,
4406
4496
  markExclusionFindingHandled,
4407
4497
  markPromoteReadyHandled,
4498
+ probeShadow,
4408
4499
  profileToRow,
4409
4500
  profilesByProvider,
4410
4501
  readBrainReadEnv,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warmdrift/kgauto-compiler",
3
- "version": "2.0.0-alpha.47",
3
+ "version": "2.0.0-alpha.49",
4
4
  "description": "Prompt compiler + central learning brain for multi-model AI apps. Swap models without rewriting prompts.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",