@warmdrift/kgauto-compiler 2.0.0-alpha.3 → 2.0.0-alpha.31

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.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- import { M as ModelProfile, C as CompilePolicy, N as NormalizedResponse, A as ApiKeys, P as ProviderOverrides, a as CompiledRequest, b as PromptIR, c as CallOptions, d as CallResult, R as RecordInput, O as OracleScore, e as CompileResult } from './profiles-C5lVqF8_.js';
2
- export { f as ALIASES, g as CacheStrategy, h as CallAttempt, i as CallError, j as CliffRule, k as Constraints, I as IntentDeclaration, L as LoweringSpec, l as Message, m as MutationApplied, n as NormalizedTokens, o as PromptSection, p as Provider, q as RecoveryRule, S as StructuredOutputCapability, r as SystemPromptMode, T as ToolCall, s as ToolDefinition, t as allProfiles, u as getProfile, v as profilesByProvider, w as tryGetProfile } from './profiles-C5lVqF8_.js';
3
- export { ALL_ARCHETYPES, ContextBucket, DIALECT_VERSION, HistoryDepth, INTENT_ARCHETYPES, IntentArchetypeName, OutputMode, ShapeSignature, ToolCountBucket, bucketContext, bucketHistory, bucketToolCount, hashShape, isArchetype, learningKey } from './dialect.js';
1
+ import { C as CompilePolicy, N as NormalizedResponse, A as ApiKeys, P as ProviderOverrides, a as CompiledRequest, b as PromptIR, c as CallOptions, d as CallResult, S as SectionRewrite, R as RecordInput, e as RecordOutcomeInput, O as OutcomeResult, f as OracleScore, g as CompileResult, B as BestPracticeAdvisory, h as Adapter, i as PerAxisMetrics, j as Provider, k as ChainEntry, G as Grounding } from './ir-BIAT9gJk.js';
2
+ export { l as CallAttempt, m as CallError, n as ChainWithGrounding, o as Constraints, F as FallbackReason, H as HistoryCachePolicy, I as IntentDeclaration, M as Message, p as MutationApplied, q as NormalizedTokens, r as OutcomeKind, s as PerAxisMetricsByModel, t as PromptSection, u as SectionKind, T as ToolCall, v as ToolDefinition } from './ir-BIAT9gJk.js';
3
+ import { ModelProfile } from './profiles.js';
4
+ export { ALIASES, CacheStrategy, CliffRule, LoweringSpec, RecoveryRule, StructuredOutputCapability, SystemPromptMode, allProfiles, getProfile, profilesByProvider, tryGetProfile } from './profiles.js';
5
+ import { IntentArchetypeName } from './dialect.js';
6
+ export { ALL_ARCHETYPES, ContextBucket, DIALECT_VERSION, HistoryDepth, INTENT_ARCHETYPES, OutputMode, ShapeSignature, ToolCountBucket, bucketContext, bucketHistory, bucketToolCount, hashShape, isArchetype, learningKey } from './dialect.js';
4
7
 
5
8
  /**
6
9
  * compile() — the main orchestrator.
@@ -19,6 +22,15 @@ interface CompileOptions {
19
22
  toolRelevanceThreshold?: number;
20
23
  /** History compression — turns count threshold (default 8). */
21
24
  compressHistoryAfter?: number;
25
+ /**
26
+ * History compression — token threshold (alpha.7). When total history
27
+ * tokens exceed this AND there are more recent turns to keep, compress
28
+ * even when count threshold is below `compressHistoryAfter`. Catches
29
+ * fat-message bloat (tool-using agents pack many tool-call/result pairs
30
+ * into single assistant messages — count stays low, tokens explode).
31
+ * Default undefined (disabled — backward-compatible).
32
+ */
33
+ compressHistoryAboveTokens?: number;
22
34
  /**
23
35
  * Consumer-declared policy. Filters blocked models, enforces cost
24
36
  * ceiling, boosts preferred. See CompilePolicy in ir.ts.
@@ -83,15 +95,28 @@ declare function execute(request: CompiledRequest, opts?: ExecuteOptions): Promi
83
95
  declare function call(ir: PromptIR, opts?: CallOptions): Promise<CallResult>;
84
96
 
85
97
  /**
86
- * Brain client fire-and-forget telemetry to the central kgauto Supabase.
87
- *
88
- * The brain is the centralized learning store. Apps POST outcomes here;
89
- * mutations flow back through a separate pull (in v2.1).
98
+ * alpha.11opt-in nested config for brain-query mode (chains / archetype
99
+ * perf / pricing / models registry). Enabled by default when endpoint is
100
+ * set; per-table opt-out via explicit `false`.
90
101
  *
91
- * Design: never blocks the caller. Failures are silent (logged via optional
92
- * onError hook). Uses fetch() works in Node 18+, Edge runtimes, and browsers.
102
+ * Locked via /plan-eng-review 2026-05-15 (decision D3). Structured group
103
+ * keeps the BrainConfig surface clean as more brain-driven tables ship.
93
104
  */
94
-
105
+ interface BrainQueryConfig {
106
+ /** Default true when endpoint set. Brain-driven fallback chains. */
107
+ chains?: boolean;
108
+ /** Default true when endpoint set. Brain-driven archetype perf scores. */
109
+ perf?: boolean;
110
+ /** Default true when endpoint set. Brain-driven pricing with at-time resolution. */
111
+ pricing?: boolean;
112
+ /** Default true when endpoint set. Brain-driven model registry + aliases. */
113
+ models?: boolean;
114
+ /** SWR window in ms. Default 300_000 (5 min). */
115
+ cacheTtlMs?: number;
116
+ /** Override the GET URL when the read endpoint differs from the write one.
117
+ * Defaults to `${endpoint}/v2/config` when omitted. */
118
+ configEndpoint?: string;
119
+ }
95
120
  interface BrainConfig {
96
121
  /** Brain HTTP endpoint base URL (e.g., https://kgauto-brain.vercel.app/api). */
97
122
  endpoint: string;
@@ -103,6 +128,9 @@ interface BrainConfig {
103
128
  sync?: boolean;
104
129
  /** Optional fetch override (for tests). */
105
130
  fetchImpl?: typeof fetch;
131
+ /** alpha.11 — brain-query mode for config tables. Default-on per table
132
+ * when endpoint is set; opt-out via `false`. See BrainQueryConfig. */
133
+ brainQuery?: BrainQueryConfig;
106
134
  }
107
135
  declare function configureBrain(config: BrainConfig): void;
108
136
  declare function clearBrain(): void;
@@ -114,6 +142,90 @@ declare function clearBrain(): void;
114
142
  * network error is swallowed/forwarded to onError.
115
143
  */
116
144
  declare function record(input: RecordInput): Promise<void>;
145
+ /**
146
+ * Wire shape POSTed by `record()` to the brain proxy's `/outcomes` endpoint.
147
+ *
148
+ * Exported so consumer proxies can `import { OutcomePayload } from
149
+ * '@warmdrift/kgauto-compiler'` instead of redefining the shape — that way
150
+ * TypeScript catches future schema additions (cache fields, advisory
151
+ * telemetry, etc.) at consumer build time, not silently at runtime.
152
+ *
153
+ * **Forward-compat rule:** consumer proxies should pass the body through to
154
+ * Supabase rather than reconstructing field-by-field. The recommended shape
155
+ * is `const row = { ...body }` (or `await supabase.from('compile_outcomes')
156
+ * .insert(body)` directly). Filtering proxies break schema evolution
157
+ * silently — see s17 root-cause investigation 2026-05-10.
158
+ */
159
+ interface OutcomePayload {
160
+ handle: string;
161
+ app_id?: string;
162
+ intent_archetype?: string;
163
+ /** The model that ACTUALLY RAN (post-fallback). */
164
+ model?: string;
165
+ /** The model v2 compile() originally targeted. NULL when no fallback. */
166
+ requested_model?: string;
167
+ provider?: string;
168
+ shape_key?: string;
169
+ learning_key?: string;
170
+ mutations_applied: string[];
171
+ tokens_in: number;
172
+ tokens_out: number;
173
+ estimated_tokens_in?: number;
174
+ latency_ms: number;
175
+ success: boolean;
176
+ empty_response: boolean;
177
+ error_type?: string;
178
+ tools_called?: string[];
179
+ oracle_score?: number;
180
+ oracle_dimensions?: Record<string, number>;
181
+ oracle_rationale?: string;
182
+ prompt_preview?: string;
183
+ response_preview?: string;
184
+ dialect_version: string;
185
+ cache_read_input_tokens?: number;
186
+ cache_creation_input_tokens?: number;
187
+ cost_usd_actual?: number;
188
+ ttft_ms?: number;
189
+ history_cacheable_tokens?: number;
190
+ history_tokens_at_compile?: number;
191
+ /**
192
+ * Mirrors `ir.constraints.toolOrchestration` from compile time. NULL when
193
+ * the consumer hadn't adopted the constraint (pre-alpha.20). Powers
194
+ * per-mode model-perf queries on the brain (the L-040 parallel-tool
195
+ * cliff lumps DeepSeek sequential perf with parallel without this).
196
+ */
197
+ tool_orchestration?: 'parallel' | 'sequential' | 'either' | null;
198
+ finish_reason?: string;
199
+ total_ms?: number;
200
+ tools_count?: number;
201
+ history_depth?: number;
202
+ system_prompt_chars?: number;
203
+ fell_over_from?: string;
204
+ fallback_reason?: 'rate_limit' | 'provider_auth_failed' | 'provider_error' | 'cliff' | 'cost_cap' | 'contract_violation';
205
+ /**
206
+ * Per-call SectionRewrite[] captured at compile time. Omitted (sent as
207
+ * undefined → stored NULL) when no rewrites fired. Powers cross-app
208
+ * learning aggregates ("rule X fired N times on (app, model, archetype),
209
+ * downstream outcome quality lifted by M points").
210
+ */
211
+ section_rewrites_applied?: SectionRewrite[] | null;
212
+ }
213
+ /**
214
+ * alpha.20 Entry 4: record a quality outcome for a previously-compiled call.
215
+ *
216
+ * Fires after the consumer's UX surfaces an approve/reject event (e.g., user
217
+ * clicks Approve on a hunt result). Joins to the original `compile_outcomes`
218
+ * row via outcomeId — enables per-(model, archetype) approve-rate measurement
219
+ * once N ≥ 10 outcomes accumulate.
220
+ *
221
+ * Fire-and-forget by default (matches record() semantics). Set BrainConfig.sync
222
+ * = true for runtime contexts that can't tolerate fire-and-forget teardown
223
+ * (Vercel Edge, Cloudflare Workers, AWS Lambda) — see L-086.
224
+ *
225
+ * Returns OutcomeResult with ok: false + stable reason on persistence
226
+ * failure. Never throws.
227
+ */
228
+ declare function recordOutcome(input: RecordOutcomeInput): Promise<OutcomeResult>;
117
229
 
118
230
  /**
119
231
  * Oracle contract — how an app tells the brain whether a response was good.
@@ -189,6 +301,1078 @@ declare function resetTokenizer(): void;
189
301
  */
190
302
  declare function countTokens(text: string): number;
191
303
 
304
+ /**
305
+ * Best-practice advisor — alpha.6 Phase 1.
306
+ *
307
+ * Inspects an IR + the selected profile + compile diagnostics and emits a
308
+ * list of `BestPracticeAdvisory` entries describing detected gaps. Runs
309
+ * after `lower()` in the compile pipeline; the result lands on
310
+ * `CompileResult.advisories` for the consumer to log, surface, or filter.
311
+ *
312
+ * Driven by interfaces/kgauto.md `best-practice-advisories` (IC, 2026-05-07).
313
+ * Phase 1 ships 4 starter rules sourced from the s14 kgauto comment +
314
+ * s15 empirical seed of brain anti-patterns:
315
+ *
316
+ * 1. `caching-off-on-claude` system >2000 chars on Anthropic, no cacheable=true
317
+ * 2. `single-chunk-system` Anthropic, only one PromptSection >1000 chars
318
+ * 3. `tool-bloat` >10 tools on a short-output archetype
319
+ * 4. `history-uncached-on-claude` Anthropic, ≥2 history messages, no historyCachePolicy
320
+ *
321
+ * Each rule is a pure function: (ir, result, profile) → BestPracticeAdvisory[].
322
+ * No side effects. No randomness. Deterministic for a given IR.
323
+ *
324
+ * The thresholds (2000 chars, 1000 chars, 10 tools, 2 history) are chosen
325
+ * to balance noise vs. signal — too low fires on innocuous calls, too high
326
+ * misses real waste. They may tune with brain evidence over time; for now
327
+ * they're literals in the rule bodies. Make them configurable when the
328
+ * cost-watcher's R-rules graduate to here.
329
+ */
330
+
331
+ /** Subset of CompileResult fields the advisor needs. */
332
+ type AdvisorContext = Pick<CompileResult, 'target' | 'provider' | 'tokensIn' | 'diagnostics'>;
333
+ /**
334
+ * Optional Phase 2 (alpha.22) context — fallback chain + a profile resolver
335
+ * for cross-model comparison. Three new rules
336
+ * (`cost-mismatched-archetype`, `model-stale-evidence`, `tier-down`) consume
337
+ * this to surface measurement-substrate signals (alpha.20 clean-attribution +
338
+ * alpha.21 grounding labels) as actionable consumer guidance.
339
+ *
340
+ * When `fallbackChain` is empty, rules 1 + 3 stay silent (nothing to
341
+ * compare against). When `profileResolver` is omitted, the rules degrade
342
+ * gracefully — they can still inspect the chosen profile but not chain
343
+ * alternatives. Rule 2 (`model-stale-evidence`) is independent of chain
344
+ * shape and works on the chosen model alone.
345
+ */
346
+ interface RunAdvisorPhase2Context {
347
+ fallbackChain: string[];
348
+ profileResolver?: (id: string) => ModelProfile | undefined;
349
+ /**
350
+ * alpha.29 — translator rewrites that fired this compile. When a rewrite
351
+ * cleared the cliff (e.g. `tool_call_contract` → sequential-tool preamble
352
+ * + `parallelToolCalls: false`), the matching cliff advisor
353
+ * (`archetype-perf-floor-breach`) MUST suppress for the same call. Without
354
+ * suppression both fire — the advisory contradicts the rewrite ("cliff is
355
+ * unaddressed" vs "we just addressed it").
356
+ *
357
+ * Empty array / undefined → no suppression (alpha.28 behavior preserved).
358
+ */
359
+ sectionRewritesApplied?: SectionRewrite[];
360
+ }
361
+ /**
362
+ * Run all phased rules and return collected advisories. Order is fixed so
363
+ * output is stable across runs. The `policy` argument is alpha.9 — the
364
+ * `single-model-array` rule needs to know whether the consumer explicitly
365
+ * declared `posture: 'locked'` (in which case single-model is intentional
366
+ * and shouldn't warn).
367
+ *
368
+ * `phase2` is alpha.22 — gives the advisor access to the fallback chain +
369
+ * a profile resolver so the three new compile-time recommendation rules
370
+ * (`cost-mismatched-archetype`, `model-stale-evidence`, `tier-down`) can
371
+ * compare the chosen model against in-chain alternatives. Optional for
372
+ * backward compatibility with consumers calling `runAdvisor()` directly.
373
+ */
374
+ declare function runAdvisor(ir: PromptIR, result: AdvisorContext, profile: ModelProfile, policy?: CompilePolicy, phase2?: RunAdvisorPhase2Context): BestPracticeAdvisory[];
375
+
376
+ /**
377
+ * Translator primitive — alpha.31.
378
+ *
379
+ * Pure function. Walks `IR.sections`, matches each section's `kind` against
380
+ * a per-rule dispatch table keyed on (kind, profile, archetype), and applies
381
+ * a model-aware rewrite when a rule fires. Returns the rewritten IR + the
382
+ * list of rewrites for `CompileResult.sectionRewritesApplied` and brain
383
+ * persistence.
384
+ *
385
+ * This is the s37 translator-framing eureka in code: kgauto graduates from
386
+ * "gate" (alpha.28's cliff advisor: "consumer must accept adapter") to
387
+ * "translator" (alpha.29: "consumer declared the section kind, kgauto
388
+ * applies the adapter at compile time without consumer-side branching").
389
+ *
390
+ * Rules shipped:
391
+ *
392
+ * alpha.29 — tool_call_contract + archetypePerf[archetype] < TRANSLATOR_FLOOR
393
+ * → prepend sequential-tool-pattern guidance
394
+ * → emit wireOverrides: { parallelToolCalls: false }
395
+ * → rule_id: 'sequential-tool-cliff-below-floor'
396
+ *
397
+ * alpha.31 — narration_contract + profile.provider === 'anthropic'
398
+ * → prepend terse-log narration guidance
399
+ * → no wireOverrides
400
+ * → rule_id: 'narration-drift-anthropic'
401
+ *
402
+ * alpha.31 — narration_contract + profile.provider === 'deepseek'
403
+ * → prepend <thinking>-suppression guidance
404
+ * → no wireOverrides
405
+ * → rule_id: 'narration-thinking-leak-deepseek'
406
+ *
407
+ * Per-rule walk (alpha.31 refactor): the alpha.30 short-circuit
408
+ * `if (!cliffFires) return passthrough` was correct when only the
409
+ * cliff-gated tool_call_contract rule existed; alpha.31's narration rules
410
+ * fire on every call regardless of cliff (narration drift is steady-state,
411
+ * not a cliff condition). Each section now consults the dispatch table
412
+ * independently — first-match wins per (section.kind, profile, archetype).
413
+ *
414
+ * **Interaction with the cliff advisor (alpha.28):** when this translator
415
+ * fires for a `tool_call_contract` section, the advisor's
416
+ * `archetype-perf-floor-breach` rule MUST suppress for the same call — the
417
+ * cliff was structurally cleared by the rewrite, not unaddressed. The
418
+ * suppression check lives in `advisor.ts` and consults the
419
+ * `CompileResult.sectionRewritesApplied` list.
420
+ *
421
+ * Design contracts:
422
+ * command-center/advisory/kgauto/2026-05-21_alpha-29-translator-and-advisories-api.md
423
+ * command-center/advisory/kgauto/2026-05-22_alpha-31-narration-contract.md
424
+ */
425
+
426
+ /**
427
+ * Re-export of `ARCHETYPE_FLOOR_DEFAULT` as the canonical "translator fires
428
+ * below this score" threshold. Same constant as the cliff advisor — the
429
+ * single threshold is shared (alpha.28's "below this, advisor warns"; alpha.29's
430
+ * "below this, translator AUTO-APPLIES the adapter").
431
+ */
432
+ declare const TRANSLATOR_FLOOR = 6;
433
+ /**
434
+ * Stable identifier of the alpha.29 sequential-tool rule. Surfaces on
435
+ * `SectionRewrite.rule` and in brain aggregates. The brain treats this
436
+ * identifier together with the preamble string as the rule's wire
437
+ * fingerprint — both stay byte-stable across releases.
438
+ */
439
+ declare const RULE_SEQUENTIAL_TOOL_CLIFF = "sequential-tool-cliff-below-floor";
440
+ interface ApplySectionRewritesArgs {
441
+ ir: PromptIR;
442
+ profile: ModelProfile;
443
+ archetype: IntentArchetypeName;
444
+ }
445
+ interface ApplySectionRewritesResult {
446
+ /**
447
+ * IR with section.text fields possibly rewritten. When no rewrites fired,
448
+ * this is identical to the input IR (referentially distinct array but
449
+ * same section payloads).
450
+ */
451
+ rewrittenIR: PromptIR;
452
+ /**
453
+ * One entry per section the translator rewrote. Empty array when no
454
+ * rules fired. Order matches the corresponding section in
455
+ * `rewrittenIR.sections`.
456
+ */
457
+ rewrites: SectionRewrite[];
458
+ }
459
+ /**
460
+ * Pure function. Apply model-aware section rewrites to the IR at compile time.
461
+ *
462
+ * Discipline:
463
+ * - Never mutates the input IR; returns a new IR with new sections array
464
+ * when at least one rewrite fired; otherwise returns the input IR by
465
+ * reference (referential identity preserved on no-op).
466
+ * - Sections without a `kind` (or `kind === 'arbitrary'`) pass through
467
+ * unchanged.
468
+ * - Empty `sections` array → returns `{ rewrittenIR: ir, rewrites: [] }`.
469
+ * - Sections of the same `kind` are processed in array order; first-match
470
+ * wins per section. (Today every rule is a single-match rule.)
471
+ *
472
+ * @example
473
+ * ```ts
474
+ * import { applySectionRewrites } from '@warmdrift/kgauto-compiler';
475
+ * import { getProfile } from '@warmdrift/kgauto-compiler';
476
+ *
477
+ * const { rewrittenIR, rewrites } = applySectionRewrites({
478
+ * ir,
479
+ * profile: getProfile('deepseek-v4-pro'),
480
+ * archetype: 'hunt',
481
+ * });
482
+ * if (rewrites.length > 0) console.log('translator fired:', rewrites);
483
+ * ```
484
+ */
485
+ declare function applySectionRewrites(args: ApplySectionRewritesArgs): ApplySectionRewritesResult;
486
+
487
+ /**
488
+ * advisories-api — structured advisories API (alpha.29 Workstream B).
489
+ *
490
+ * Closes the L-117 family bottleneck: kgauto's `result.advisories[]` (the
491
+ * compile-time warnings about caching-off, tool-bloat, archetype-perf-floor
492
+ * breaches, etc.) used to disappear after the consumer read the compile
493
+ * result. The s34 caching-off advisory pattern is the canonical failure: an
494
+ * advisory fired 100+ times in 24h on `generate::sonnet` at 99.4% empty
495
+ * rate, and nobody knew because there was no structured channel to surface
496
+ * "what's open right now?"
497
+ *
498
+ * Migration 020 ships the substrate: `compile_outcome_advisories` gains
499
+ * lifecycle columns (`resolved_at`, `resolution_source`, `resolution_note`)
500
+ * and the `actionable_advisories_v` view rolls per-firing rows into
501
+ * per-(app_id, code) tuples with deterministic stable ids + server-side
502
+ * auto-resolution.
503
+ *
504
+ * Public surface:
505
+ * getActionableAdvisories({ appId, severity?, status?, brainEndpoint, brainJwt, brainAnonKey, fetch? })
506
+ * → Promise<ActionableAdvisory[]>
507
+ *
508
+ * markAdvisoryResolved({ id, resolutionNote?, brainEndpoint, brainJwt, brainAnonKey, fetch? })
509
+ * → Promise<{ ok: true } | { ok: false; reason: string }>
510
+ *
511
+ * Both functions are pure I/O — no module-level state. The fetch wiring
512
+ * mirrors `createProxyHandler` in `glassbox-routes/proxy.ts`: scoped JWT in
513
+ * `Authorization: Bearer`, `apikey` header carries the anon key.
514
+ *
515
+ * The auto-resolution rule is enforced server-side in `actionable_advisories_v`:
516
+ * • Latest firing > 14 days ago → status='resolved' (auto-pruned)
517
+ * • All firings have resolved_at set → status='resolved' (consumer-marked)
518
+ * • Otherwise → status='open'
519
+ *
520
+ * `markAdvisoryResolved` is the consumer override: "I fixed it; the next
521
+ * firing will reopen the rule, but until then it stays resolved."
522
+ */
523
+
524
+ /**
525
+ * Severity of an advisory. Maps to the `level` column on
526
+ * `compile_outcome_advisories` (info | warn | critical).
527
+ */
528
+ type AdvisorySeverity = 'info' | 'warn' | 'critical';
529
+ /**
530
+ * Status of an advisory rollup.
531
+ *
532
+ * - `open` — at least one unresolved firing in the last 14 days
533
+ * - `resolved` — either auto-pruned (no firings >14d) or consumer-marked
534
+ * - `snoozed` — reserved for alpha.30+ (snooze-until-date); type-accepted
535
+ * today but the view will never emit this value
536
+ */
537
+ type AdvisoryStatus = 'open' | 'snoozed' | 'resolved';
538
+ /**
539
+ * Source of resolution when status='resolved'.
540
+ *
541
+ * - `auto` — server-side rule auto-pruned (>14d since last firing)
542
+ * - `consumer-marked` — consumer called `markAdvisoryResolved`
543
+ * - `declined` — reserved (alpha.30+: consumer marked the suggestion
544
+ * inapplicable; the advisor will still fire but UIs can hide it)
545
+ */
546
+ type AdvisoryResolutionSource = 'auto' | 'consumer-marked' | 'declined';
547
+ /**
548
+ * Suggested-fix metadata. `null` when the advisor has no actionable fix
549
+ * (info-level rules can be observational). When set, the consumer's
550
+ * Admin UI can render a one-click apply (alpha.30+) or surface the
551
+ * before/after diff inline.
552
+ */
553
+ interface AdvisorySuggestedFix {
554
+ type: 'config-change' | 'one-liner' | 'env-var' | 'manual';
555
+ /** File:line hint if kgauto can infer (reserved — alpha.30+). */
556
+ siteHint?: string;
557
+ before?: string;
558
+ after?: string;
559
+ docsLink?: string;
560
+ }
561
+ /**
562
+ * Per-(app_id, code) advisory rollup. The `id` is stable across polls
563
+ * until the rule auto-resolves and re-opens (a new firing after the 14d
564
+ * boundary advances `opened_at` → new id). Treat id-changes as
565
+ * intentional "fresh re-open" signals.
566
+ *
567
+ * Wire-shape from `actionable_advisories_v`. The transformer maps the
568
+ * view's snake_case columns to camelCase. See `feedback_typed_boundary_transformers.md`
569
+ * (L-118) for the rationale.
570
+ */
571
+ interface ActionableAdvisory {
572
+ id: string;
573
+ rule: string;
574
+ severity: AdvisorySeverity;
575
+ openedAt: string;
576
+ lastObservedAt: string;
577
+ observationCount: number;
578
+ appliesTo: {
579
+ archetype?: IntentArchetypeName;
580
+ model?: string;
581
+ callSiteHint?: string;
582
+ };
583
+ message: string;
584
+ suggestedFix: AdvisorySuggestedFix | null;
585
+ /** Reserved — always `false` in alpha.29. alpha.30+ ships actual auto-apply. */
586
+ autoApplicable: boolean;
587
+ status: AdvisoryStatus;
588
+ resolvedAt?: string;
589
+ resolutionSource?: AdvisoryResolutionSource;
590
+ resolutionNote?: string;
591
+ }
592
+ /**
593
+ * Filter + transport for `getActionableAdvisories`. The brain JWT must
594
+ * carry an `app_id` claim matching `opts.appId` — RLS enforces tenant
595
+ * isolation on the underlying table, so a mismatch silently returns [].
596
+ */
597
+ interface GetActionableAdvisoriesOptions {
598
+ appId: string;
599
+ /** Severity filter; if omitted, all severities are returned. */
600
+ severity?: AdvisorySeverity;
601
+ /** Status filter; defaults to 'open'. Pass 'all' for the full set. */
602
+ status?: 'open' | 'snoozed' | 'resolved' | 'all';
603
+ brainEndpoint: string;
604
+ brainJwt: string;
605
+ brainAnonKey: string;
606
+ fetch?: typeof fetch;
607
+ }
608
+ interface MarkAdvisoryResolvedOptions {
609
+ /** Stable id from a prior `getActionableAdvisories` call. */
610
+ id: string;
611
+ resolutionNote?: string;
612
+ brainEndpoint: string;
613
+ brainJwt: string;
614
+ brainAnonKey: string;
615
+ fetch?: typeof fetch;
616
+ }
617
+ /**
618
+ * Query the open advisory set for an app. Pulls from
619
+ * `actionable_advisories_v` (per-(app_id, code) rollup with stable ids
620
+ * + server-side auto-resolution).
621
+ *
622
+ * Default behavior (no status filter): returns only `status='open'`.
623
+ * Pass `status: 'all'` to see resolved + open together — useful for
624
+ * Admin UIs that show "recently fixed" badges.
625
+ *
626
+ * Fetch failures bubble out as thrown Errors. (`markAdvisoryResolved`
627
+ * uses the ok/reason envelope; this read path throws to match
628
+ * existing kgauto query semantics where the consumer can decide to
629
+ * retry or render an error state.)
630
+ */
631
+ declare function getActionableAdvisories(opts: GetActionableAdvisoriesOptions): Promise<ActionableAdvisory[]>;
632
+ /**
633
+ * Mark an advisory as consumer-resolved.
634
+ *
635
+ * Lookup strategy:
636
+ * 1. Query `actionable_advisories_v?id=eq.<id>` to find the (app_id, code)
637
+ * tuple for this advisory.
638
+ * 2. PATCH the latest unresolved firing in `compile_outcome_advisories`
639
+ * matching that tuple via the underlying outcome's app_id.
640
+ *
641
+ * The (app_id, code) lookup is necessary because `compile_outcome_advisories`
642
+ * has no `app_id` column — it inherits scope via the FK to compile_outcomes.
643
+ * PostgREST cannot PATCH with a JOIN predicate, so the markAdvisoryResolved
644
+ * path is a two-step round-trip. The cost is one extra GET per resolve call;
645
+ * acceptable because marks are low-frequency operator actions, not per-call
646
+ * hot path.
647
+ *
648
+ * Idempotent re-marks: if the advisory is already resolved (no unresolved
649
+ * firings match), the PATCH affects zero rows and the call still returns
650
+ * `ok: true`. The function returns `ok: false` only on transport / auth /
651
+ * lookup failures.
652
+ *
653
+ * Returns ok/reason envelope (vs throwing) because consumer Admin UIs
654
+ * typically want to render the failure inline rather than crash.
655
+ */
656
+ declare function markAdvisoryResolved(opts: MarkAdvisoryResolvedOptions): Promise<{
657
+ ok: true;
658
+ } | {
659
+ ok: false;
660
+ reason: string;
661
+ }>;
662
+
663
+ /**
664
+ * Archetype-cliff compatibility — alpha.28 (tt-intel-Cairn ratified).
665
+ *
666
+ * One question, one answer: *given this model and this intent, can it work
667
+ * — and if not, what adapter (if any) would make it work?*
668
+ *
669
+ * Replaces the silent-archetype-cliff failure mode where a consumer picks a
670
+ * model that's structurally wrong for the intent and kgauto compiles cleanly
671
+ * without surfacing the gap. The triggering incident: tt-intel shipped
672
+ * `deepseek-v4-pro` as the hunt default per a local spec; the kgauto coord
673
+ * doc said L-040 — V4 is structurally wrong for hunt (sequential tools).
674
+ * `archetypePerf.hunt = 4` was already in the profile data. The compiler
675
+ * stayed silent. This API + the matching advisor rule surfaces it.
676
+ *
677
+ * Pure function. No network. No brain query. No side effects. ~1ms.
678
+ *
679
+ * Consultation doc:
680
+ * command-center/advisory/kgauto/2026-05-21_archetype-cliff-advisor.md
681
+ *
682
+ * Refinements applied per tt-intel-Cairn ratification (2026-05-21):
683
+ * R1: every variant carries `archetypePerf: number` (raw score) — and
684
+ * `requires-adapter` adds `archetypePerfWithAdapter: number` so
685
+ * consumer policy can be expressed as "accept adapter only when score
686
+ * crosses some threshold WITH the adapter on."
687
+ * R2: every variant carries a plain-English `reason: string`. No internal
688
+ * jargon ("L-040", "archetypePerf=4") — the consumer chooses whether
689
+ * to render it as UI hint or operator-tooling tooltip.
690
+ * R3: `Adapter` is a CLOSED discriminated union, not `| string`. alpha.28
691
+ * ships ONE variant (`toolOrchestration: 'sequential'`). Future
692
+ * adapter parameters extend the union explicitly in named releases.
693
+ * NO escape hatch — the whole point is catching "I added a new
694
+ * adapter and forgot to update consumer policy" at compile time.
695
+ */
696
+
697
+ /**
698
+ * Minimum `archetypePerf[archetype]` score to count as `compatible` under
699
+ * Option A (default policy). Below this, a documented adapter is needed
700
+ * to lift the model above the floor; if no adapter exists, the model is
701
+ * rejected.
702
+ *
703
+ * Matches `QUALITY_FLOOR_FOR_RECOMMENDATION` in `advisor.ts` — kgauto's
704
+ * library-wide convention for "below this score, swap recommendations stop."
705
+ */
706
+ declare const ARCHETYPE_FLOOR_DEFAULT = 6;
707
+ /**
708
+ * Absolute floor — below this, the cliff is too steep for ANY adapter to
709
+ * lift cleanly. Reserved under Option A (unused today; every below-floor
710
+ * case is gated by adapter availability). Would gate `reject` vs
711
+ * `requires-adapter` under a future Option B per consultation doc Q1.
712
+ *
713
+ * Exported so consumer-side policy can read it (e.g. "accept adapter only
714
+ * when archetypePerf >= ABSOLUTE_FLOOR + 1"). Not used internally by
715
+ * `getModelCompatibility` today — the gate is "does an adapter exist for
716
+ * this cliff?", not score-based.
717
+ */
718
+ declare const ABSOLUTE_FLOOR = 4;
719
+ /**
720
+ * The intent the call is expressing — archetype + optional orchestration
721
+ * mode. Same shape as `ir.intent.archetype` + `ir.constraints.toolOrchestration`
722
+ * so a consumer can pass `{ archetype: ir.intent.archetype,
723
+ * toolOrchestration: ir.constraints?.toolOrchestration }` directly.
724
+ */
725
+ interface CompatibilityIntent {
726
+ archetype: IntentArchetypeName;
727
+ toolOrchestration?: 'parallel' | 'sequential' | 'either';
728
+ }
729
+ /**
730
+ * `Adapter` — re-exported above. Canonical definition lives in `ir.ts` to
731
+ * avoid an import cycle (compatibility.ts → profiles.ts → ir.ts).
732
+ *
733
+ * **CLOSED discriminated union per R3.** Future adapter parameters extend
734
+ * the union explicitly in named alpha releases. No `| string` escape hatch
735
+ * — consumer policy code SHOULD write exhaustive `switch (adapter.parameter)`
736
+ * and rely on the compiler to flag "I added a new adapter parameter and
737
+ * forgot to update the consumer's policy."
738
+ *
739
+ * alpha.28 variants:
740
+ * - `{ parameter: 'toolOrchestration'; value: 'sequential'; consequence }`
741
+ * Lifts DeepSeek V4-family on `hunt` from sequential-tool cliff (L-040).
742
+ * The "consequence" plain-English-ifies the trade-off:
743
+ * "Tool calls run one at a time — slower but reliable."
744
+ *
745
+ * alpha.29+ likely additions (per tt-intel-Cairn priority list):
746
+ * - `{ parameter: 'parallelToolCalls'; value: false; consequence }`
747
+ * - `{ parameter: 'maxTools'; value: number; consequence }`
748
+ * - `{ parameter: 'thinkingBudget'; value: 0; consequence }`
749
+ *
750
+ * Each new variant lands in its own named release with the union extended
751
+ * in `ir.ts`. Consumers see the change at compile time.
752
+ */
753
+ /**
754
+ * The compatibility verdict for a (model, intent) pair. Discriminated union
755
+ * on `status` — `compatible` | `requires-adapter` | `reject`.
756
+ *
757
+ * **Every variant carries `archetypePerf` (R1) + `reason` (R2):**
758
+ * - `archetypePerf` — the raw 0-10 score for (model, archetype). Lets
759
+ * consumers build their own thresholds without re-importing the profile
760
+ * registry.
761
+ * - `reason` — plain-English, consumer-renderable. NOT internal jargon
762
+ * like "L-040 cliff" or "archetypePerf=4". Examples in R2 ratification:
763
+ * - compatible: "Suited for hunt-style parallel discovery."
764
+ * - requires-adapter: "Best with sequential tool calls for hunt — slower but works."
765
+ * - reject: "Not suited for hunt — would underperform significantly."
766
+ *
767
+ * `requires-adapter` additionally carries:
768
+ * - `archetypePerfWithAdapter` — estimated post-adapter score. May be an
769
+ * estimate (we don't measure post-adapter scores yet); kgauto's prior is
770
+ * "adapter lifts to ARCHETYPE_FLOOR_DEFAULT + 1" unless brain-evidenced.
771
+ * - `adapter` — the closed-union variant describing the structural change.
772
+ *
773
+ * Backward-compat: an unknown model returns `reject` with a "model not
774
+ * registered" reason; callers never throw. Unknown archetype is impossible
775
+ * at the type level (`IntentArchetypeName` is a closed union).
776
+ */
777
+ type ModelCompatibility = {
778
+ status: 'compatible';
779
+ reason: string;
780
+ archetypePerf: number;
781
+ } | {
782
+ status: 'requires-adapter';
783
+ reason: string;
784
+ archetypePerf: number;
785
+ archetypePerfWithAdapter: number;
786
+ adapter: Adapter;
787
+ } | {
788
+ status: 'reject';
789
+ reason: string;
790
+ archetypePerf: number;
791
+ };
792
+ /**
793
+ * Compatibility query — *does this model fit this intent, and if not,
794
+ * what would?*
795
+ *
796
+ * **Rules (Option A from consultation doc Q1):**
797
+ * 1. If model is unregistered → `reject` with "model not registered" reason.
798
+ * 2. If intent provides `toolOrchestration: 'sequential'` AND that adapter
799
+ * silences the cliff (because the cliff IS the sequential-tool one)
800
+ * → return `compatible` with raw score (NOT the post-adapter estimate
801
+ * — caller already paid the adapter, score reflects reality).
802
+ * 3. If raw `archetypePerf[archetype] >= ARCHETYPE_FLOOR_DEFAULT`
803
+ * → `compatible`.
804
+ * 4. If below floor BUT a documented adapter exists that lifts to floor
805
+ * → `requires-adapter` with adapter + estimated post-adapter score.
806
+ * 5. If below floor AND no adapter exists → `reject`.
807
+ *
808
+ * **Pure function.** Deterministic for `(modelId, intent)`. No I/O.
809
+ *
810
+ * @example
811
+ * ```ts
812
+ * import { getModelCompatibility } from '@warmdrift/kgauto-compiler';
813
+ *
814
+ * const c = getModelCompatibility('deepseek-v4-pro', { archetype: 'hunt' });
815
+ * // → { status: 'requires-adapter',
816
+ * // reason: 'Best with sequential tool calls for hunt — slower but works.',
817
+ * // archetypePerf: 4,
818
+ * // archetypePerfWithAdapter: 7,
819
+ * // adapter: {
820
+ * // parameter: 'toolOrchestration',
821
+ * // value: 'sequential',
822
+ * // consequence: 'Tool calls run one at a time...'
823
+ * // } }
824
+ *
825
+ * // With the adapter already declared:
826
+ * const c2 = getModelCompatibility('deepseek-v4-pro', {
827
+ * archetype: 'hunt',
828
+ * toolOrchestration: 'sequential',
829
+ * });
830
+ * // → { status: 'compatible',
831
+ * // reason: 'Suited for hunt with sequential tool calls.',
832
+ * // archetypePerf: 4 }
833
+ * ```
834
+ */
835
+ declare function getModelCompatibility(modelId: string, intent: CompatibilityIntent): ModelCompatibility;
836
+
837
+ /**
838
+ * alpha.22 — sync introspection: is brain-query mode active for a given
839
+ * table? Used by the advisor (`model-stale-evidence` rule) to decide
840
+ * whether a `judgment`-grounded chosen model is a measurement gap worth
841
+ * surfacing. Returns false on cold start, when configureBrain() was never
842
+ * called, or when the consumer explicitly opted the table out via
843
+ * `BrainConfig.brainQuery.<table> = false`.
844
+ */
845
+ declare function isBrainQueryActiveFor(table: string): boolean;
846
+ interface GetPerAxisMetricsOpts {
847
+ /** App id to filter on. Required. */
848
+ appId: string;
849
+ /** Intent archetype to filter on. Required. */
850
+ archetype: string;
851
+ /** Canonical model id to filter on. Required. */
852
+ model: string;
853
+ /**
854
+ * Window in days. Default 30. Only rows with
855
+ * `created_at > now() - windowDays` are counted.
856
+ */
857
+ windowDays?: number;
858
+ /**
859
+ * Consumer-declared quality floor (0..1 oracle/approve-rate scale).
860
+ * When set, the response's `qualityFloorMet` is true/false; when omitted,
861
+ * `qualityFloorMet` is null (no floor declared, no judgment).
862
+ */
863
+ qualityFloor?: number;
864
+ /** Pluggable fetch (tests inject mock). Defaults to global fetch. */
865
+ fetch?: typeof fetch;
866
+ /**
867
+ * PostgREST base endpoint (e.g. `https://kgauto-brain.vercel.app/api`).
868
+ * When omitted, falls back to the active configureBrainQuery runtime's
869
+ * endpoint. Returns null when neither is set.
870
+ */
871
+ endpoint?: string;
872
+ /** Bearer token. Forwarded as `Authorization: Bearer ${apiKey}`. */
873
+ apiKey?: string;
874
+ }
875
+ /**
876
+ * Call the `get_per_axis_metrics` RPC and return the typed result.
877
+ *
878
+ * Returns null when:
879
+ * - no endpoint provided AND no configureBrainQuery runtime active
880
+ * - RPC returns empty / null / unexpected shape
881
+ * - brain unreachable / fetch throws / HTTP error
882
+ *
883
+ * Never throws — operator-facing query, must not blow up the caller.
884
+ */
885
+ declare function getPerAxisMetrics(opts: GetPerAxisMetricsOpts): Promise<PerAxisMetrics | null>;
886
+
887
+ /**
888
+ * env.ts — provider env-key resolution + reachability predicates.
889
+ *
890
+ * Centralizes the per-provider env var names that kgauto checks for
891
+ * reachability. Used by:
892
+ *
893
+ * - execute.ts — to find an API key when one isn't passed via apiKeys
894
+ * - call.ts — to auto-filter unreachable models from the fallback walk
895
+ * - getDefaultFallbackChain — opt-in chain filter when consumer passes `reachability`
896
+ * - operator scripts — getReachabilityDiagnostic() prints what's wired up
897
+ *
898
+ * Keeping the map in ONE place means execute() and the reachability check
899
+ * always agree. Without this, kgauto could declare a model "reachable" because
900
+ * env.ts found GOOGLE_GENERATIVE_AI_API_KEY, while execute() looks at
901
+ * GOOGLE_API_KEY only and 401s — shipping the bug we're trying to fix.
902
+ *
903
+ * Resolution order (apiKeys takes precedence):
904
+ * 1. opts.apiKeys?.[provider]
905
+ * 2. process.env[name] for each name in PROVIDER_ENV_KEYS[provider] (first-present wins)
906
+ *
907
+ * alpha.10 (2026-05-14). Resolves the auto-filter-unreachable-models-silently
908
+ * request from PB after first-deploy 401 on the alpha.9 summarize chain.
909
+ */
910
+
911
+ /**
912
+ * Providers kgauto can resolve keys for today. Subset of `Provider` — `mistral`
913
+ * and `xai` are declared in the type union but not yet executable (no profiles,
914
+ * no execute() handler, no env-var convention). Narrowing here keeps the
915
+ * reachability check structurally honest.
916
+ */
917
+ type SupportedProvider = 'anthropic' | 'google' | 'openai' | 'deepseek';
918
+ /**
919
+ * Per-provider env var names kgauto recognizes. Order doesn't matter —
920
+ * first-present wins. Multiple names per provider supported because Google
921
+ * has historical drift (`GOOGLE_API_KEY` from older Google Cloud SDKs,
922
+ * `GEMINI_API_KEY` in many examples, `GOOGLE_GENERATIVE_AI_API_KEY` is the
923
+ * Vercel AI SDK convention used by IC + tt-intel adapters).
924
+ *
925
+ * Frozen so consumers/tests can't mutate (would break the cache invariant
926
+ * that execute() and reachability checks agree).
927
+ */
928
+ declare const PROVIDER_ENV_KEYS: Readonly<Record<SupportedProvider, readonly string[]>>;
929
+ interface ReachabilityOpts {
930
+ /** Explicit keys (alpha.3 ApiKeys). Checked first; takes precedence over env. */
931
+ apiKeys?: ApiKeys;
932
+ /**
933
+ * Override env source. Defaults to `process.env` in Node-shaped runtimes,
934
+ * `{}` everywhere else. Pass `{}` explicitly in tests for hermetic runs.
935
+ */
936
+ envSource?: Record<string, string | undefined>;
937
+ }
938
+ /**
939
+ * Resolve a usable API key for the provider. Returns the key string, or
940
+ * undefined if neither apiKeys nor any of the env names are set.
941
+ *
942
+ * Used internally by execute.ts so the reachability check and the actual
943
+ * call check stay in sync.
944
+ */
945
+ declare function resolveProviderKey(provider: Provider, opts?: ReachabilityOpts): string | undefined;
946
+ /**
947
+ * True iff the provider has a usable key — either via explicit `apiKeys`
948
+ * or one of the `PROVIDER_ENV_KEYS[provider]` names is set in envSource.
949
+ */
950
+ declare function isProviderReachable(provider: Provider, opts?: ReachabilityOpts): boolean;
951
+ /**
952
+ * True iff the model's profile exists AND its provider is reachable.
953
+ * Unknown model id returns false (treat as unreachable; the chain walker
954
+ * will surface "no reachable models" if everything filters out).
955
+ */
956
+ declare function isModelReachable(modelId: string, opts?: ReachabilityOpts): boolean;
957
+ interface ProviderReachability {
958
+ reachable: boolean;
959
+ /** How the key was found. `null` when unreachable. */
960
+ via: 'apiKeys' | 'env' | null;
961
+ /** Which env var name supplied the key (only when via === 'env'). */
962
+ envKeyFound?: string;
963
+ }
964
+ /**
965
+ * Snapshot of which providers are reachable from the current env / apiKeys.
966
+ * Useful for operator scripts ("kgauto diagnose"), startup-time logging,
967
+ * and the cost-watcher's "which consumer is missing what" report.
968
+ *
969
+ * Does NOT log the key value itself — only the env var name that supplied it.
970
+ */
971
+ declare function getReachabilityDiagnostic(opts?: ReachabilityOpts): Record<SupportedProvider, ProviderReachability>;
972
+
973
+ /**
974
+ * getDefaultFallbackChain — the alpha.9 cascading ship.
975
+ *
976
+ * Returns a per-archetype fallback chain that walks the cost/performance
977
+ * Pareto frontier (master plan §1.3 + §3). Three customer postures:
978
+ *
979
+ * locked — caller passes [theOneModel]; never call this function
980
+ * preferred — caller passes `primary`; chain returned is [primary, ...fallbacks]
981
+ * open — caller passes no `primary`; chain returned is [best, ...fallbacks]
982
+ *
983
+ * The chain at each step:
984
+ * 1. Costs strictly less than the previous (no expensive sideways moves)
985
+ * 2. Comes from a different provider than the previous step where possible
986
+ * (correlated outages don't kill consecutive attempts)
987
+ * 3. Stays above the archetype's perf floor (skip models scored <baseline
988
+ * for archetypes where degradation would be unacceptable)
989
+ *
990
+ * In alpha.9 the chain is **hand-curated** per archetype (§3.3 starter
991
+ * table). Brain-query mode lands in alpha.10. Policy.blockedModels filters
992
+ * the result; policy.maxCostPerCallUsd is NOT applied here because the
993
+ * function doesn't see the IR's token counts — that filtering happens at
994
+ * `passScoreTargets()` time inside compile().
995
+ *
996
+ * The function is **pure** — no brain query, no I/O, no randomness. Same
997
+ * inputs always produce the same chain.
998
+ */
999
+
1000
+ /**
1001
+ * Posture passed into `getDefaultFallbackChain`. The chain function only
1002
+ * sees `'open'` and `'preferred'` — callers in `'locked'` posture should
1003
+ * pass `models: [theOneModel]` directly and skip this function entirely.
1004
+ *
1005
+ * Equivalent to `CompilePolicy.posture` minus `'locked'`. Kept distinct so
1006
+ * the type system enforces "don't ask for a chain when you don't want one."
1007
+ */
1008
+ type FallbackPosture = 'open' | 'preferred';
1009
+ interface GetDefaultFallbackChainOpts {
1010
+ /** The archetype the call is performing. Drives chain shape. */
1011
+ archetype: IntentArchetypeName;
1012
+ /**
1013
+ * The user-selected or caller-anchored primary model. When provided, it
1014
+ * appears at position 0 of the returned chain and fallbacks follow.
1015
+ * When omitted, the function picks the best-perf model for the archetype
1016
+ * as position 0 (open posture).
1017
+ */
1018
+ primary?: string;
1019
+ /**
1020
+ * Informational. `'preferred'` and `'open'` produce the same chain shape
1021
+ * given the same `primary`/no-primary input — posture is a tag the brain
1022
+ * uses to distinguish "user-anchored" from "library-anchored" telemetry.
1023
+ */
1024
+ posture?: FallbackPosture;
1025
+ /**
1026
+ * Cap on chain length. Default 3. Min 1. Useful when the consumer wants
1027
+ * to keep the worst-case latency low (each fallback adds a round-trip).
1028
+ */
1029
+ maxDepth?: number;
1030
+ /**
1031
+ * Consumer-side gating. `blockedModels` are filtered from the chain.
1032
+ * `preferredModels` is informational (no boost applied at this layer —
1033
+ * compile()'s `passScoreTargets` handles preference ranking).
1034
+ * `maxCostPerCallUsd` is NOT applied here — needs IR-level token
1035
+ * estimation. Use compile()'s policy plumbing instead.
1036
+ */
1037
+ policy?: CompilePolicy;
1038
+ /**
1039
+ * alpha.10. When provided, the chain is filtered to models whose provider
1040
+ * has a reachable API key (via `apiKeys` or one of `PROVIDER_ENV_KEYS[provider]`).
1041
+ * Models whose provider can't be reached are silently dropped. If filtering
1042
+ * leaves the chain empty, returns `[]` — caller decides what to do (call()
1043
+ * throws CallError; this function stays pure).
1044
+ *
1045
+ * Pass `{}` to opt in with `process.env` as the env source. Pass `{ apiKeys, envSource }`
1046
+ * for explicit control (tests, non-Node runtimes). Omit entirely for the
1047
+ * legacy unfiltered behavior — preserves alpha.9 callers byte-for-byte.
1048
+ */
1049
+ reachability?: ReachabilityOpts;
1050
+ /**
1051
+ * alpha.20 E3: consumer-declared tool-orchestration shape. Currently
1052
+ * only affects `archetype: 'hunt'`, where 'sequential' swaps the
1053
+ * parallel-tool-tier-0 chain (Flash → Pro → Sonnet → Haiku) for a
1054
+ * DeepSeek-tier-0 chain (V4-Pro → Flash → Sonnet) — DeepSeek's L-040
1055
+ * parallel-tool cliff doesn't apply when the consumer commits to
1056
+ * single-step orchestration.
1057
+ *
1058
+ * Other archetypes are NOT mode-aware in this release — they ship the
1059
+ * same chain regardless of toolOrchestration. Future versions may
1060
+ * extend mode-awareness to ask/generate/etc. when brain evidence
1061
+ * supports it.
1062
+ *
1063
+ * Default (omitted or 'either'): parallel chain. Back-compat with all
1064
+ * pre-alpha.20 callers.
1065
+ */
1066
+ toolOrchestration?: 'parallel' | 'sequential' | 'either';
1067
+ }
1068
+ /**
1069
+ * Returns the fallback chain for an archetype as a plain `string[]` of
1070
+ * model ids.
1071
+ *
1072
+ * @deprecated since alpha.21 — prefer
1073
+ * {@link getDefaultFallbackChainWithGrounding}, which returns the same chain
1074
+ * shape with a `grounding` label on every entry (measured / capability-fact /
1075
+ * judgment). The string[] return is preserved indefinitely for back-compat —
1076
+ * no functional change in alpha.21. Existing callers don't need to migrate
1077
+ * unless they want to surface the grounding gap to users.
1078
+ */
1079
+ declare function getDefaultFallbackChain(opts: GetDefaultFallbackChainOpts): string[];
1080
+ /**
1081
+ * Returns a shallow copy of the hand-curated starter chain for an archetype.
1082
+ * Useful for tests + the `scripts/digest.mjs` operator readout.
1083
+ */
1084
+ declare function getStarterChain(archetype: IntentArchetypeName): string[];
1085
+ /**
1086
+ * Returns a shallow copy of all starter chains keyed by archetype.
1087
+ * Useful for the `digest.mjs` readout and consumer audits.
1088
+ */
1089
+ declare function getAllStarterChains(): Record<IntentArchetypeName, string[]>;
1090
+ /**
1091
+ * alpha.20 E3 introspection — returns the sequential-mode overlay for an
1092
+ * archetype, or `undefined` when no overlay is registered (the archetype
1093
+ * is mode-agnostic and reuses `STARTER_CHAINS[archetype]`).
1094
+ *
1095
+ * Useful for tests + the `scripts/digest.mjs` operator readout to surface
1096
+ * the mode-aware chains.
1097
+ */
1098
+ declare function getSequentialStarterChain(archetype: IntentArchetypeName): string[] | undefined;
1099
+ /**
1100
+ * alpha.21 (s78 Entry 1) — returns the fallback chain as `ChainEntry[]`,
1101
+ * with a `grounding` label on every position.
1102
+ *
1103
+ * Same selection logic as {@link getDefaultFallbackChain} (primary anchoring,
1104
+ * blockedModels filter, dedupe, reachability filter, maxDepth cap) — the
1105
+ * only difference is the return shape: each position is a `ChainEntry`
1106
+ * carrying `{ id, grounding, reason?, n? }` instead of a bare string.
1107
+ *
1108
+ * Use this when surfacing the chain to consumers who care WHY each entry
1109
+ * sits where it sits — Glass-Box panels, operator dashboards, eval
1110
+ * scaffolding deciding which entries deserve measurement priority.
1111
+ *
1112
+ * Returns `[]` when filtering empties the chain (same semantics as the
1113
+ * string variant) — consumer decides what to do.
1114
+ */
1115
+ declare function getDefaultFallbackChainWithGrounding(opts: GetDefaultFallbackChainOpts): ChainEntry[];
1116
+ /**
1117
+ * alpha.21 introspection — returns the grounded starter chain for an
1118
+ * archetype (no primary anchoring, no policy filtering, no maxDepth cap).
1119
+ * Use this when you want the raw, hand-curated grounded chain — every
1120
+ * entry carries a `grounding` label and optional reason/n.
1121
+ */
1122
+ declare function getStarterChainWithGrounding(archetype: IntentArchetypeName): ChainEntry[];
1123
+ /**
1124
+ * alpha.21 introspection — all grounded starter chains keyed by archetype.
1125
+ * Useful for the `digest.mjs` readout and consumer audits that want to
1126
+ * surface the grounding gap across the entire chain table.
1127
+ */
1128
+ declare function getAllStarterChainsWithGrounding(): Record<IntentArchetypeName, ChainEntry[]>;
1129
+ /**
1130
+ * alpha.21 introspection — sequential-mode overlay with grounding labels,
1131
+ * or `undefined` when no overlay is registered for the archetype.
1132
+ */
1133
+ declare function getSequentialStarterChainWithGrounding(archetype: IntentArchetypeName): ChainEntry[] | undefined;
1134
+
1135
+ /**
1136
+ * chains-brain — alpha.11 KG-11 adapter.
1137
+ *
1138
+ * Brain-driven STARTER_CHAINS for `getDefaultFallbackChain`. Reads
1139
+ * `kgauto_chains` table via the shared brain-query SWR cache (D6 + D8);
1140
+ * falls back to bundled STARTER_CHAINS on cold-start, brain-down, or
1141
+ * empty/missing table (D2 + D4).
1142
+ *
1143
+ * Behavioral note (locked via D2): the sync API surface returns bundled on
1144
+ * cold-start, with a background refresh fired. Subsequent calls within the
1145
+ * 5-min TTL return brain data. Vercel cold-start consumers see the seed
1146
+ * snapshot (functionally identical to pre-alpha.11); warm-start consumers
1147
+ * see live brain mutations within 5 min.
1148
+ */
1149
+ /**
1150
+ * Sync reader for the brain-driven chains map. Returns bundled
1151
+ * STARTER_CHAINS when brain-query is disabled, cold, or unreachable.
1152
+ */
1153
+ declare const loadChainsFromBrain: () => Record<"ask" | "hunt" | "classify" | "summarize" | "generate" | "extract" | "plan" | "critique" | "transform", string[]>;
1154
+
1155
+ /**
1156
+ * archetype-perf-brain — alpha.11 KG-12 adapter.
1157
+ *
1158
+ * Brain-driven archetypePerf scores. Substrate for the future closed-loop
1159
+ * tuning engine (KG-12.5): brain telemetry → human or automated bumps →
1160
+ * brain UPDATE → consumers see new scores within 5-min cache TTL with
1161
+ * zero refresh.
1162
+ *
1163
+ * Today: data migrates to brain. No runtime call site reads archetypePerf
1164
+ * (it's metadata for the master plan §2.5 anti-hallucination guardrail +
1165
+ * future auto-tuning). The adapter exists so future readers — auto-tuning
1166
+ * + operator scripts + KG-12.5 — have a shipped substrate to consume.
1167
+ */
1168
+
1169
+ type ArchetypePerfMap = Map<string, Partial<Record<IntentArchetypeName, number>>>;
1170
+ /**
1171
+ * alpha.21: per-(model, archetype) row count map. Same shape as
1172
+ * ArchetypePerfMap but stores brain row counts when the brain backs a
1173
+ * placement. Undefined entries → no row count seen → score is the
1174
+ * hand-curated cold-start prior (grounding='judgment').
1175
+ */
1176
+ type ArchetypePerfNMap = Map<string, Partial<Record<IntentArchetypeName, number>>>;
1177
+ /**
1178
+ * Sync reader for the brain-driven archetypePerf map. Returns bundled
1179
+ * profile.archetypePerf data when brain-query is disabled, cold, or
1180
+ * unreachable. Identical shape pre/post-alpha.11 by design (D2).
1181
+ */
1182
+ declare const loadArchetypePerfFromBrain: () => ArchetypePerfMap;
1183
+ /**
1184
+ * alpha.21 — sync reader for the brain row-count map paired with
1185
+ * archetype-perf. Returns empty map (no measured backing) on cold start /
1186
+ * brain-down / unreachable — all `getArchetypePerfScore` calls then
1187
+ * resolve to `grounding: 'judgment'`. When the brain table includes an
1188
+ * `n` column on each row, this map mirrors those counts so consumers can
1189
+ * see how many measurements back each score.
1190
+ */
1191
+ declare const loadArchetypePerfNFromBrain: () => ArchetypePerfNMap;
1192
+ /**
1193
+ * Threshold above which a brain row count counts as 'measured' grounding.
1194
+ * Below this, the score is treated as 'judgment' (cold-start prior or
1195
+ * not-yet-enough-evidence). Mirrors the alpha.20 `getCleanPerfScore`
1196
+ * `minRows` default — same rule for consistency.
1197
+ */
1198
+ declare const MEASURED_GROUNDING_MIN_N = 10;
1199
+ /**
1200
+ * alpha.21 — return shape for the extended {@link getArchetypePerfScore}.
1201
+ * Wraps the existing 0..10 score with:
1202
+ *
1203
+ * - `n`: brain row count backing this score (0 when no measurement).
1204
+ * - `grounding`: derived label — 'measured' when n >= 10, else 'judgment'.
1205
+ *
1206
+ * The score itself is unchanged from pre-alpha.21 (numeric, 5 = neutral
1207
+ * default). 'capability-fact' is NOT a perf-score grounding — capability
1208
+ * decisions live on chain entries, not on perf scores.
1209
+ */
1210
+ interface ArchetypePerfScoreResult {
1211
+ /** 0..10 perf score. 5 = neutral default when no entry exists. */
1212
+ score: number;
1213
+ /**
1214
+ * Brain row count backing this score. 0 when bundled (cold-start prior)
1215
+ * or when the brain row didn't carry an `n` column.
1216
+ */
1217
+ n: number;
1218
+ /**
1219
+ * Provenance — 'measured' when `n >= 10`, else 'judgment'.
1220
+ * Never 'capability-fact' (that label is reserved for chain-entry
1221
+ * inclusion/exclusion decisions).
1222
+ */
1223
+ grounding: Grounding;
1224
+ }
1225
+ /**
1226
+ * Per-model accessor with grounding (alpha.21). Returns 5 (neutral) when no
1227
+ * entry is found — consistent with the master plan §3.3 "missing archetypes
1228
+ * default to 5" convention documented in profiles.ts
1229
+ * ModelProfile.archetypePerf.
1230
+ *
1231
+ * Backwards-compat note: pre-alpha.21 callers expected `number` here. The
1232
+ * new return shape `{ score, n, grounding }` is a breaking shape change at
1233
+ * the type level, but `.score` carries the legacy value. Callers reading
1234
+ * `.score` field-by-field continue working; callers using the bare number
1235
+ * arithmetically need to switch to `.score`. The migration is single-line
1236
+ * (`const x = getArchetypePerfScore(...)` → `const x = getArchetypePerfScore(...).score`).
1237
+ */
1238
+ declare function getArchetypePerfScore(modelId: string, archetype: IntentArchetypeName): ArchetypePerfScoreResult;
1239
+
1240
+ /**
1241
+ * pricing-brain — alpha.11 KG-13 adapter.
1242
+ *
1243
+ * Brain-driven pricing data with **time-bounded resolution** (`valid_from`
1244
+ * / `valid_until` columns). The V4-Pro 75%-off promo through 2026-05-31
1245
+ * gets modeled as two rows; `at` parameter resolution picks the correct
1246
+ * row per call timestamp. Promo flips happen automatically at the
1247
+ * boundary without alpha cuts.
1248
+ *
1249
+ * SWR cache holds the FULL pricing snapshot (all active rows for all
1250
+ * models). Per-call `at` filtering is in-memory — keeps the SWR semantics
1251
+ * uniform with other adapters (one cache, one snapshot, sync reads).
1252
+ */
1253
+ interface PricingRow {
1254
+ modelId: string;
1255
+ costInputPer1m: number;
1256
+ costOutputPer1m: number;
1257
+ cacheInputPer1m?: number;
1258
+ cacheCreationPer1m?: number;
1259
+ validFrom: number;
1260
+ validUntil?: number;
1261
+ source?: string;
1262
+ }
1263
+ /**
1264
+ * Sync reader for the brain-driven pricing snapshot. Returns bundled
1265
+ * profile pricing when brain-query is disabled, cold, or unreachable.
1266
+ * Caller filters by `at` via {@link resolvePricingAt}.
1267
+ */
1268
+ declare const loadPricingFromBrain: () => PricingRow[];
1269
+ /**
1270
+ * Resolve the active pricing row for a model at a given timestamp.
1271
+ * Picks the row with the latest `valid_from <= at` whose
1272
+ * `valid_until > at` (or NULL — open-ended).
1273
+ *
1274
+ * Returns `undefined` when no row matches. Callers should fall back to
1275
+ * profile static pricing in that case.
1276
+ */
1277
+ declare function resolvePricingAt(modelId: string, at?: Date): PricingRow | undefined;
1278
+
1279
+ /**
1280
+ * models-brain — alpha.11 KG-14 adapter (the largest of the four).
1281
+ *
1282
+ * Brain-driven model registry + aliases. Two adapters share the same
1283
+ * SWR snapshot:
1284
+ *
1285
+ * - `loadModelsFromBrain()` — `Map<modelId, ModelProfile>` from
1286
+ * `kgauto_models` table (cliffs/lowering/recovery as JSONB columns).
1287
+ * - `loadAliasesFromBrain()` — `Record<aliasId, canonicalId>` from
1288
+ * `kgauto_aliases` table.
1289
+ *
1290
+ * After alpha.11, new-model onboarding becomes brain INSERT (vs PR + alpha
1291
+ * cut + 3 consumer refreshes). The auto-onboard pipeline
1292
+ * (`scripts/check-model-releases.mjs`) shifts from emitting profile.ts
1293
+ * edits to writing brain rows directly.
1294
+ *
1295
+ * **D5 — alias resolution stable regardless of canonical's active state.**
1296
+ * `canonicalId('deepseek-chat') → 'deepseek-v4-flash'` even when the
1297
+ * canonical row is `active=false`. Aliases are wire-format contracts;
1298
+ * legacy callers' resolution promise outlives the canonical's active
1299
+ * status. Deprecation (`active=false`) affects chain composition only.
1300
+ */
1301
+
1302
+ /**
1303
+ * Exported brain-row shape for `kgauto_models`. Mirrors the SQL table
1304
+ * (`v2/brain/migrations/010_kgauto_models_and_aliases.sql`) — column names
1305
+ * are snake_case to match PostgREST. Used by operator scripts that write
1306
+ * to brain (auto-onboard, promote-model) — see `profileToRow()`.
1307
+ *
1308
+ * Read path stays internal via `RawModelRow` to keep `rowToProfile()`'s
1309
+ * tolerance contract from leaking into write callers.
1310
+ */
1311
+ interface ModelBrainRow {
1312
+ model_id: string;
1313
+ provider: string;
1314
+ status?: string;
1315
+ max_context_tokens?: number;
1316
+ max_output_tokens?: number;
1317
+ max_tools?: number;
1318
+ parallel_tool_calls?: boolean;
1319
+ structured_output?: string;
1320
+ system_prompt_mode?: string;
1321
+ streaming?: boolean;
1322
+ cliffs?: unknown;
1323
+ lowering?: unknown;
1324
+ recovery?: unknown;
1325
+ strengths?: string[] | null;
1326
+ weaknesses?: string[] | null;
1327
+ cost_input_per_1m?: number;
1328
+ cost_output_per_1m?: number;
1329
+ notes?: string | null;
1330
+ verified_against_docs?: string | null;
1331
+ archetype_perf?: Record<string, number> | null;
1332
+ version_added?: string;
1333
+ version_removed?: string | null;
1334
+ active?: boolean;
1335
+ }
1336
+ interface ProfileToRowOptions {
1337
+ /** e.g. `'2.0.0-alpha.12'` — leave undefined to omit from row. */
1338
+ versionAdded?: string;
1339
+ /** Pass `null` to clear; omit to leave field unset. */
1340
+ versionRemoved?: string | null;
1341
+ /** Defaults to true if omitted. */
1342
+ active?: boolean;
1343
+ /**
1344
+ * Override verifiedAgainstDocs (e.g. set to `null` for auto-onboard).
1345
+ * When omitted, the profile's own value is used. Pass `null` explicitly
1346
+ * to write a SQL NULL (e.g. unverified auto-onboard rows).
1347
+ */
1348
+ verifiedAgainstDocs?: string | null;
1349
+ }
1350
+ /**
1351
+ * Inverse of `rowToProfile` — serialize a `ModelProfile` to a `kgauto_models`
1352
+ * row payload for INSERT/UPSERT. Used by operator scripts that write to
1353
+ * brain (auto-onboard pipeline, promote-model verification CLI).
1354
+ *
1355
+ * Row-level fields (`version_added`, `version_removed`, `active`) are
1356
+ * operator-controlled and pass through `opts`. `verifiedAgainstDocs` can
1357
+ * also be overridden via `opts` — auto-onboard explicitly passes `null` to
1358
+ * mark unverified rows (the column is DATE, doesn't accept the
1359
+ * `'UNVERIFIED-AUTO-ONBOARD'` sentinel used in PROFILES_RAW).
1360
+ */
1361
+ declare function profileToRow(profile: ModelProfile, opts?: ProfileToRowOptions): ModelBrainRow;
1362
+ /**
1363
+ * Sync reader for the brain-driven model registry. Returns bundled
1364
+ * PROFILES_RAW when brain-query is disabled, cold, or unreachable.
1365
+ */
1366
+ declare const loadModelsFromBrain: () => Map<string, ModelProfile>;
1367
+ /**
1368
+ * Sync reader for the brain-driven aliases map. Returns bundled ALIASES
1369
+ * when brain-query is disabled, cold, or unreachable.
1370
+ *
1371
+ * D5: this map carries both active and inactive canonical mappings —
1372
+ * alias resolution is stable regardless of canonical's `active` state.
1373
+ */
1374
+ declare const loadAliasesFromBrain: () => Record<string, string>;
1375
+
192
1376
  /**
193
1377
  * @warmdrift/kgauto v2 — prompt compiler + central learning brain.
194
1378
  *
@@ -235,4 +1419,4 @@ declare function countTokens(text: string): number;
235
1419
  */
236
1420
  declare function compile(ir: PromptIR, opts?: CompileOptions): CompileResult;
237
1421
 
238
- export { ApiKeys, type AppOracle, type BrainConfig, CallOptions, CallResult, type CompileOptions, CompilePolicy, CompileResult, CompiledRequest, type ExecuteErr, type ExecuteOk, type ExecuteOptions, type ExecuteResult, type LLMJudgeOptions, ModelProfile, NormalizedResponse, type OracleContext, OracleScore, PromptIR, ProviderOverrides, RecordInput, buildLLMJudge, call, clearBrain, compile, configureBrain, countTokens, execute, record, resetTokenizer, setTokenizer };
1422
+ export { ABSOLUTE_FLOOR, ARCHETYPE_FLOOR_DEFAULT, type ActionableAdvisory, Adapter, type AdvisoryResolutionSource, type AdvisorySeverity, type AdvisoryStatus, type AdvisorySuggestedFix, ApiKeys, type AppOracle, type ApplySectionRewritesArgs, type ApplySectionRewritesResult, type ArchetypePerfMap, type ArchetypePerfNMap, type ArchetypePerfScoreResult, BestPracticeAdvisory, type BrainConfig, type BrainQueryConfig, CallOptions, CallResult, ChainEntry, type CompatibilityIntent, type CompileOptions, CompilePolicy, CompileResult, CompiledRequest, type ExecuteErr, type ExecuteOk, type ExecuteOptions, type ExecuteResult, type FallbackPosture, type GetActionableAdvisoriesOptions, type GetDefaultFallbackChainOpts, type GetPerAxisMetricsOpts, Grounding, IntentArchetypeName, type LLMJudgeOptions, MEASURED_GROUNDING_MIN_N, type MarkAdvisoryResolvedOptions, type ModelBrainRow, type ModelCompatibility, ModelProfile, NormalizedResponse, type OracleContext, OracleScore, type OutcomePayload, OutcomeResult, PROVIDER_ENV_KEYS, PerAxisMetrics, type PricingRow, type ProfileToRowOptions, PromptIR, Provider, ProviderOverrides, type ProviderReachability, RULE_SEQUENTIAL_TOOL_CLIFF, type ReachabilityOpts, RecordInput, RecordOutcomeInput, type RunAdvisorPhase2Context, SectionRewrite, type SupportedProvider, TRANSLATOR_FLOOR, applySectionRewrites, buildLLMJudge, call, clearBrain, compile, configureBrain, countTokens, execute, getActionableAdvisories, getAllStarterChains, getAllStarterChainsWithGrounding, getArchetypePerfScore, getDefaultFallbackChain, getDefaultFallbackChainWithGrounding, getModelCompatibility, getPerAxisMetrics, getReachabilityDiagnostic, getSequentialStarterChain, getSequentialStarterChainWithGrounding, getStarterChain, getStarterChainWithGrounding, isBrainQueryActiveFor, isModelReachable, isProviderReachable, loadAliasesFromBrain, loadArchetypePerfFromBrain, loadArchetypePerfNFromBrain, loadChainsFromBrain, loadModelsFromBrain, loadPricingFromBrain, markAdvisoryResolved, profileToRow, record, recordOutcome, resetTokenizer, resolvePricingAt, resolveProviderKey, runAdvisor, setTokenizer };