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

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,1062 @@ 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.29.
378
+ *
379
+ * Pure function. Walks `IR.sections`, matches each section's `kind` against
380
+ * the model + archetype + profile, and applies a model-aware rewrite when
381
+ * a rule fires. Returns the rewritten IR + the list of rewrites for
382
+ * `CompileResult.sectionRewritesApplied` and brain persistence.
383
+ *
384
+ * This is the s37 translator-framing eureka in code: kgauto graduates from
385
+ * "gate" (alpha.28's cliff advisor: "consumer must accept adapter") to
386
+ * "translator" (alpha.29: "consumer declared the section kind, kgauto
387
+ * applies the adapter at compile time without consumer-side branching").
388
+ *
389
+ * alpha.29 ships ONE rule:
390
+ *
391
+ * tool_call_contract + profile.archetypePerf[archetype] < ARCHETYPE_FLOOR_DEFAULT
392
+ * → prepend sequential-tool-pattern guidance
393
+ * → emit wireOverrides: { parallelToolCalls: false }
394
+ *
395
+ * alpha.30+ will extend the rule table to `narration_contract`, `role_intro`,
396
+ * etc. Each new rule lands here as an explicit branch.
397
+ *
398
+ * **Interaction with the cliff advisor (alpha.28):** when this translator
399
+ * fires for a `tool_call_contract` section, the advisor's
400
+ * `archetype-perf-floor-breach` rule MUST suppress for the same call — the
401
+ * cliff was structurally cleared by the rewrite, not unaddressed. The
402
+ * suppression check lives in `advisor.ts` and consults the
403
+ * `CompileResult.sectionRewritesApplied` list.
404
+ *
405
+ * Design contract:
406
+ * command-center/advisory/kgauto/2026-05-21_alpha-29-translator-and-advisories-api.md
407
+ */
408
+
409
+ /**
410
+ * Re-export of `ARCHETYPE_FLOOR_DEFAULT` as the canonical "translator fires
411
+ * below this score" threshold. Same constant as the cliff advisor — the
412
+ * single threshold is shared (alpha.28's "below this, advisor warns"; alpha.29's
413
+ * "below this, translator AUTO-APPLIES the adapter").
414
+ */
415
+ declare const TRANSLATOR_FLOOR = 6;
416
+ /**
417
+ * Stable identifier of the alpha.29 sequential-tool rule. Surfaces on
418
+ * `SectionRewrite.rule` and in brain aggregates. Future rules extend this
419
+ * list; the brain learns per-rule effectiveness over time.
420
+ */
421
+ declare const RULE_SEQUENTIAL_TOOL_CLIFF = "sequential-tool-cliff-below-floor";
422
+ interface ApplySectionRewritesArgs {
423
+ ir: PromptIR;
424
+ profile: ModelProfile;
425
+ archetype: IntentArchetypeName;
426
+ }
427
+ interface ApplySectionRewritesResult {
428
+ /**
429
+ * IR with section.text fields possibly rewritten. When no rewrites fired,
430
+ * this is identical to the input IR (referentially distinct array but
431
+ * same section payloads).
432
+ */
433
+ rewrittenIR: PromptIR;
434
+ /**
435
+ * One entry per section the translator rewrote. Empty array when no
436
+ * rules fired. Order matches the corresponding section in
437
+ * `rewrittenIR.sections`.
438
+ */
439
+ rewrites: SectionRewrite[];
440
+ }
441
+ /**
442
+ * Pure function. Apply model-aware section rewrites to the IR at compile time.
443
+ *
444
+ * Discipline:
445
+ * - Never mutates the input IR; returns a new IR with new sections array.
446
+ * - Sections without a `kind` (or `kind === 'arbitrary'`) pass through
447
+ * unchanged.
448
+ * - Empty `sections` array → returns `{ rewrittenIR: ir, rewrites: [] }`.
449
+ * - Missing `profile.archetypePerf` → no rewrite (defensive — treat the
450
+ * model as un-classified rather than below-floor).
451
+ * - Sections of the same `kind` are processed in array order; the rule
452
+ * fires once per matching section (today every tool_call_contract
453
+ * section gets the same prepend — multiple sections of the same kind
454
+ * ARE supported but is an unusual consumer shape).
455
+ *
456
+ * @example
457
+ * ```ts
458
+ * import { applySectionRewrites } from '@warmdrift/kgauto-compiler';
459
+ * import { getProfile } from '@warmdrift/kgauto-compiler';
460
+ *
461
+ * const { rewrittenIR, rewrites } = applySectionRewrites({
462
+ * ir,
463
+ * profile: getProfile('deepseek-v4-pro'),
464
+ * archetype: 'hunt',
465
+ * });
466
+ * if (rewrites.length > 0) console.log('translator fired:', rewrites);
467
+ * ```
468
+ */
469
+ declare function applySectionRewrites(args: ApplySectionRewritesArgs): ApplySectionRewritesResult;
470
+
471
+ /**
472
+ * advisories-api — structured advisories API (alpha.29 Workstream B).
473
+ *
474
+ * Closes the L-117 family bottleneck: kgauto's `result.advisories[]` (the
475
+ * compile-time warnings about caching-off, tool-bloat, archetype-perf-floor
476
+ * breaches, etc.) used to disappear after the consumer read the compile
477
+ * result. The s34 caching-off advisory pattern is the canonical failure: an
478
+ * advisory fired 100+ times in 24h on `generate::sonnet` at 99.4% empty
479
+ * rate, and nobody knew because there was no structured channel to surface
480
+ * "what's open right now?"
481
+ *
482
+ * Migration 020 ships the substrate: `compile_outcome_advisories` gains
483
+ * lifecycle columns (`resolved_at`, `resolution_source`, `resolution_note`)
484
+ * and the `actionable_advisories_v` view rolls per-firing rows into
485
+ * per-(app_id, code) tuples with deterministic stable ids + server-side
486
+ * auto-resolution.
487
+ *
488
+ * Public surface:
489
+ * getActionableAdvisories({ appId, severity?, status?, brainEndpoint, brainJwt, brainAnonKey, fetch? })
490
+ * → Promise<ActionableAdvisory[]>
491
+ *
492
+ * markAdvisoryResolved({ id, resolutionNote?, brainEndpoint, brainJwt, brainAnonKey, fetch? })
493
+ * → Promise<{ ok: true } | { ok: false; reason: string }>
494
+ *
495
+ * Both functions are pure I/O — no module-level state. The fetch wiring
496
+ * mirrors `createProxyHandler` in `glassbox-routes/proxy.ts`: scoped JWT in
497
+ * `Authorization: Bearer`, `apikey` header carries the anon key.
498
+ *
499
+ * The auto-resolution rule is enforced server-side in `actionable_advisories_v`:
500
+ * • Latest firing > 14 days ago → status='resolved' (auto-pruned)
501
+ * • All firings have resolved_at set → status='resolved' (consumer-marked)
502
+ * • Otherwise → status='open'
503
+ *
504
+ * `markAdvisoryResolved` is the consumer override: "I fixed it; the next
505
+ * firing will reopen the rule, but until then it stays resolved."
506
+ */
507
+
508
+ /**
509
+ * Severity of an advisory. Maps to the `level` column on
510
+ * `compile_outcome_advisories` (info | warn | critical).
511
+ */
512
+ type AdvisorySeverity = 'info' | 'warn' | 'critical';
513
+ /**
514
+ * Status of an advisory rollup.
515
+ *
516
+ * - `open` — at least one unresolved firing in the last 14 days
517
+ * - `resolved` — either auto-pruned (no firings >14d) or consumer-marked
518
+ * - `snoozed` — reserved for alpha.30+ (snooze-until-date); type-accepted
519
+ * today but the view will never emit this value
520
+ */
521
+ type AdvisoryStatus = 'open' | 'snoozed' | 'resolved';
522
+ /**
523
+ * Source of resolution when status='resolved'.
524
+ *
525
+ * - `auto` — server-side rule auto-pruned (>14d since last firing)
526
+ * - `consumer-marked` — consumer called `markAdvisoryResolved`
527
+ * - `declined` — reserved (alpha.30+: consumer marked the suggestion
528
+ * inapplicable; the advisor will still fire but UIs can hide it)
529
+ */
530
+ type AdvisoryResolutionSource = 'auto' | 'consumer-marked' | 'declined';
531
+ /**
532
+ * Suggested-fix metadata. `null` when the advisor has no actionable fix
533
+ * (info-level rules can be observational). When set, the consumer's
534
+ * Admin UI can render a one-click apply (alpha.30+) or surface the
535
+ * before/after diff inline.
536
+ */
537
+ interface AdvisorySuggestedFix {
538
+ type: 'config-change' | 'one-liner' | 'env-var' | 'manual';
539
+ /** File:line hint if kgauto can infer (reserved — alpha.30+). */
540
+ siteHint?: string;
541
+ before?: string;
542
+ after?: string;
543
+ docsLink?: string;
544
+ }
545
+ /**
546
+ * Per-(app_id, code) advisory rollup. The `id` is stable across polls
547
+ * until the rule auto-resolves and re-opens (a new firing after the 14d
548
+ * boundary advances `opened_at` → new id). Treat id-changes as
549
+ * intentional "fresh re-open" signals.
550
+ *
551
+ * Wire-shape from `actionable_advisories_v`. The transformer maps the
552
+ * view's snake_case columns to camelCase. See `feedback_typed_boundary_transformers.md`
553
+ * (L-118) for the rationale.
554
+ */
555
+ interface ActionableAdvisory {
556
+ id: string;
557
+ rule: string;
558
+ severity: AdvisorySeverity;
559
+ openedAt: string;
560
+ lastObservedAt: string;
561
+ observationCount: number;
562
+ appliesTo: {
563
+ archetype?: IntentArchetypeName;
564
+ model?: string;
565
+ callSiteHint?: string;
566
+ };
567
+ message: string;
568
+ suggestedFix: AdvisorySuggestedFix | null;
569
+ /** Reserved — always `false` in alpha.29. alpha.30+ ships actual auto-apply. */
570
+ autoApplicable: boolean;
571
+ status: AdvisoryStatus;
572
+ resolvedAt?: string;
573
+ resolutionSource?: AdvisoryResolutionSource;
574
+ resolutionNote?: string;
575
+ }
576
+ /**
577
+ * Filter + transport for `getActionableAdvisories`. The brain JWT must
578
+ * carry an `app_id` claim matching `opts.appId` — RLS enforces tenant
579
+ * isolation on the underlying table, so a mismatch silently returns [].
580
+ */
581
+ interface GetActionableAdvisoriesOptions {
582
+ appId: string;
583
+ /** Severity filter; if omitted, all severities are returned. */
584
+ severity?: AdvisorySeverity;
585
+ /** Status filter; defaults to 'open'. Pass 'all' for the full set. */
586
+ status?: 'open' | 'snoozed' | 'resolved' | 'all';
587
+ brainEndpoint: string;
588
+ brainJwt: string;
589
+ brainAnonKey: string;
590
+ fetch?: typeof fetch;
591
+ }
592
+ interface MarkAdvisoryResolvedOptions {
593
+ /** Stable id from a prior `getActionableAdvisories` call. */
594
+ id: string;
595
+ resolutionNote?: string;
596
+ brainEndpoint: string;
597
+ brainJwt: string;
598
+ brainAnonKey: string;
599
+ fetch?: typeof fetch;
600
+ }
601
+ /**
602
+ * Query the open advisory set for an app. Pulls from
603
+ * `actionable_advisories_v` (per-(app_id, code) rollup with stable ids
604
+ * + server-side auto-resolution).
605
+ *
606
+ * Default behavior (no status filter): returns only `status='open'`.
607
+ * Pass `status: 'all'` to see resolved + open together — useful for
608
+ * Admin UIs that show "recently fixed" badges.
609
+ *
610
+ * Fetch failures bubble out as thrown Errors. (`markAdvisoryResolved`
611
+ * uses the ok/reason envelope; this read path throws to match
612
+ * existing kgauto query semantics where the consumer can decide to
613
+ * retry or render an error state.)
614
+ */
615
+ declare function getActionableAdvisories(opts: GetActionableAdvisoriesOptions): Promise<ActionableAdvisory[]>;
616
+ /**
617
+ * Mark an advisory as consumer-resolved.
618
+ *
619
+ * Lookup strategy:
620
+ * 1. Query `actionable_advisories_v?id=eq.<id>` to find the (app_id, code)
621
+ * tuple for this advisory.
622
+ * 2. PATCH the latest unresolved firing in `compile_outcome_advisories`
623
+ * matching that tuple via the underlying outcome's app_id.
624
+ *
625
+ * The (app_id, code) lookup is necessary because `compile_outcome_advisories`
626
+ * has no `app_id` column — it inherits scope via the FK to compile_outcomes.
627
+ * PostgREST cannot PATCH with a JOIN predicate, so the markAdvisoryResolved
628
+ * path is a two-step round-trip. The cost is one extra GET per resolve call;
629
+ * acceptable because marks are low-frequency operator actions, not per-call
630
+ * hot path.
631
+ *
632
+ * Idempotent re-marks: if the advisory is already resolved (no unresolved
633
+ * firings match), the PATCH affects zero rows and the call still returns
634
+ * `ok: true`. The function returns `ok: false` only on transport / auth /
635
+ * lookup failures.
636
+ *
637
+ * Returns ok/reason envelope (vs throwing) because consumer Admin UIs
638
+ * typically want to render the failure inline rather than crash.
639
+ */
640
+ declare function markAdvisoryResolved(opts: MarkAdvisoryResolvedOptions): Promise<{
641
+ ok: true;
642
+ } | {
643
+ ok: false;
644
+ reason: string;
645
+ }>;
646
+
647
+ /**
648
+ * Archetype-cliff compatibility — alpha.28 (tt-intel-Cairn ratified).
649
+ *
650
+ * One question, one answer: *given this model and this intent, can it work
651
+ * — and if not, what adapter (if any) would make it work?*
652
+ *
653
+ * Replaces the silent-archetype-cliff failure mode where a consumer picks a
654
+ * model that's structurally wrong for the intent and kgauto compiles cleanly
655
+ * without surfacing the gap. The triggering incident: tt-intel shipped
656
+ * `deepseek-v4-pro` as the hunt default per a local spec; the kgauto coord
657
+ * doc said L-040 — V4 is structurally wrong for hunt (sequential tools).
658
+ * `archetypePerf.hunt = 4` was already in the profile data. The compiler
659
+ * stayed silent. This API + the matching advisor rule surfaces it.
660
+ *
661
+ * Pure function. No network. No brain query. No side effects. ~1ms.
662
+ *
663
+ * Consultation doc:
664
+ * command-center/advisory/kgauto/2026-05-21_archetype-cliff-advisor.md
665
+ *
666
+ * Refinements applied per tt-intel-Cairn ratification (2026-05-21):
667
+ * R1: every variant carries `archetypePerf: number` (raw score) — and
668
+ * `requires-adapter` adds `archetypePerfWithAdapter: number` so
669
+ * consumer policy can be expressed as "accept adapter only when score
670
+ * crosses some threshold WITH the adapter on."
671
+ * R2: every variant carries a plain-English `reason: string`. No internal
672
+ * jargon ("L-040", "archetypePerf=4") — the consumer chooses whether
673
+ * to render it as UI hint or operator-tooling tooltip.
674
+ * R3: `Adapter` is a CLOSED discriminated union, not `| string`. alpha.28
675
+ * ships ONE variant (`toolOrchestration: 'sequential'`). Future
676
+ * adapter parameters extend the union explicitly in named releases.
677
+ * NO escape hatch — the whole point is catching "I added a new
678
+ * adapter and forgot to update consumer policy" at compile time.
679
+ */
680
+
681
+ /**
682
+ * Minimum `archetypePerf[archetype]` score to count as `compatible` under
683
+ * Option A (default policy). Below this, a documented adapter is needed
684
+ * to lift the model above the floor; if no adapter exists, the model is
685
+ * rejected.
686
+ *
687
+ * Matches `QUALITY_FLOOR_FOR_RECOMMENDATION` in `advisor.ts` — kgauto's
688
+ * library-wide convention for "below this score, swap recommendations stop."
689
+ */
690
+ declare const ARCHETYPE_FLOOR_DEFAULT = 6;
691
+ /**
692
+ * Absolute floor — below this, the cliff is too steep for ANY adapter to
693
+ * lift cleanly. Reserved under Option A (unused today; every below-floor
694
+ * case is gated by adapter availability). Would gate `reject` vs
695
+ * `requires-adapter` under a future Option B per consultation doc Q1.
696
+ *
697
+ * Exported so consumer-side policy can read it (e.g. "accept adapter only
698
+ * when archetypePerf >= ABSOLUTE_FLOOR + 1"). Not used internally by
699
+ * `getModelCompatibility` today — the gate is "does an adapter exist for
700
+ * this cliff?", not score-based.
701
+ */
702
+ declare const ABSOLUTE_FLOOR = 4;
703
+ /**
704
+ * The intent the call is expressing — archetype + optional orchestration
705
+ * mode. Same shape as `ir.intent.archetype` + `ir.constraints.toolOrchestration`
706
+ * so a consumer can pass `{ archetype: ir.intent.archetype,
707
+ * toolOrchestration: ir.constraints?.toolOrchestration }` directly.
708
+ */
709
+ interface CompatibilityIntent {
710
+ archetype: IntentArchetypeName;
711
+ toolOrchestration?: 'parallel' | 'sequential' | 'either';
712
+ }
713
+ /**
714
+ * `Adapter` — re-exported above. Canonical definition lives in `ir.ts` to
715
+ * avoid an import cycle (compatibility.ts → profiles.ts → ir.ts).
716
+ *
717
+ * **CLOSED discriminated union per R3.** Future adapter parameters extend
718
+ * the union explicitly in named alpha releases. No `| string` escape hatch
719
+ * — consumer policy code SHOULD write exhaustive `switch (adapter.parameter)`
720
+ * and rely on the compiler to flag "I added a new adapter parameter and
721
+ * forgot to update the consumer's policy."
722
+ *
723
+ * alpha.28 variants:
724
+ * - `{ parameter: 'toolOrchestration'; value: 'sequential'; consequence }`
725
+ * Lifts DeepSeek V4-family on `hunt` from sequential-tool cliff (L-040).
726
+ * The "consequence" plain-English-ifies the trade-off:
727
+ * "Tool calls run one at a time — slower but reliable."
728
+ *
729
+ * alpha.29+ likely additions (per tt-intel-Cairn priority list):
730
+ * - `{ parameter: 'parallelToolCalls'; value: false; consequence }`
731
+ * - `{ parameter: 'maxTools'; value: number; consequence }`
732
+ * - `{ parameter: 'thinkingBudget'; value: 0; consequence }`
733
+ *
734
+ * Each new variant lands in its own named release with the union extended
735
+ * in `ir.ts`. Consumers see the change at compile time.
736
+ */
737
+ /**
738
+ * The compatibility verdict for a (model, intent) pair. Discriminated union
739
+ * on `status` — `compatible` | `requires-adapter` | `reject`.
740
+ *
741
+ * **Every variant carries `archetypePerf` (R1) + `reason` (R2):**
742
+ * - `archetypePerf` — the raw 0-10 score for (model, archetype). Lets
743
+ * consumers build their own thresholds without re-importing the profile
744
+ * registry.
745
+ * - `reason` — plain-English, consumer-renderable. NOT internal jargon
746
+ * like "L-040 cliff" or "archetypePerf=4". Examples in R2 ratification:
747
+ * - compatible: "Suited for hunt-style parallel discovery."
748
+ * - requires-adapter: "Best with sequential tool calls for hunt — slower but works."
749
+ * - reject: "Not suited for hunt — would underperform significantly."
750
+ *
751
+ * `requires-adapter` additionally carries:
752
+ * - `archetypePerfWithAdapter` — estimated post-adapter score. May be an
753
+ * estimate (we don't measure post-adapter scores yet); kgauto's prior is
754
+ * "adapter lifts to ARCHETYPE_FLOOR_DEFAULT + 1" unless brain-evidenced.
755
+ * - `adapter` — the closed-union variant describing the structural change.
756
+ *
757
+ * Backward-compat: an unknown model returns `reject` with a "model not
758
+ * registered" reason; callers never throw. Unknown archetype is impossible
759
+ * at the type level (`IntentArchetypeName` is a closed union).
760
+ */
761
+ type ModelCompatibility = {
762
+ status: 'compatible';
763
+ reason: string;
764
+ archetypePerf: number;
765
+ } | {
766
+ status: 'requires-adapter';
767
+ reason: string;
768
+ archetypePerf: number;
769
+ archetypePerfWithAdapter: number;
770
+ adapter: Adapter;
771
+ } | {
772
+ status: 'reject';
773
+ reason: string;
774
+ archetypePerf: number;
775
+ };
776
+ /**
777
+ * Compatibility query — *does this model fit this intent, and if not,
778
+ * what would?*
779
+ *
780
+ * **Rules (Option A from consultation doc Q1):**
781
+ * 1. If model is unregistered → `reject` with "model not registered" reason.
782
+ * 2. If intent provides `toolOrchestration: 'sequential'` AND that adapter
783
+ * silences the cliff (because the cliff IS the sequential-tool one)
784
+ * → return `compatible` with raw score (NOT the post-adapter estimate
785
+ * — caller already paid the adapter, score reflects reality).
786
+ * 3. If raw `archetypePerf[archetype] >= ARCHETYPE_FLOOR_DEFAULT`
787
+ * → `compatible`.
788
+ * 4. If below floor BUT a documented adapter exists that lifts to floor
789
+ * → `requires-adapter` with adapter + estimated post-adapter score.
790
+ * 5. If below floor AND no adapter exists → `reject`.
791
+ *
792
+ * **Pure function.** Deterministic for `(modelId, intent)`. No I/O.
793
+ *
794
+ * @example
795
+ * ```ts
796
+ * import { getModelCompatibility } from '@warmdrift/kgauto-compiler';
797
+ *
798
+ * const c = getModelCompatibility('deepseek-v4-pro', { archetype: 'hunt' });
799
+ * // → { status: 'requires-adapter',
800
+ * // reason: 'Best with sequential tool calls for hunt — slower but works.',
801
+ * // archetypePerf: 4,
802
+ * // archetypePerfWithAdapter: 7,
803
+ * // adapter: {
804
+ * // parameter: 'toolOrchestration',
805
+ * // value: 'sequential',
806
+ * // consequence: 'Tool calls run one at a time...'
807
+ * // } }
808
+ *
809
+ * // With the adapter already declared:
810
+ * const c2 = getModelCompatibility('deepseek-v4-pro', {
811
+ * archetype: 'hunt',
812
+ * toolOrchestration: 'sequential',
813
+ * });
814
+ * // → { status: 'compatible',
815
+ * // reason: 'Suited for hunt with sequential tool calls.',
816
+ * // archetypePerf: 4 }
817
+ * ```
818
+ */
819
+ declare function getModelCompatibility(modelId: string, intent: CompatibilityIntent): ModelCompatibility;
820
+
821
+ /**
822
+ * alpha.22 — sync introspection: is brain-query mode active for a given
823
+ * table? Used by the advisor (`model-stale-evidence` rule) to decide
824
+ * whether a `judgment`-grounded chosen model is a measurement gap worth
825
+ * surfacing. Returns false on cold start, when configureBrain() was never
826
+ * called, or when the consumer explicitly opted the table out via
827
+ * `BrainConfig.brainQuery.<table> = false`.
828
+ */
829
+ declare function isBrainQueryActiveFor(table: string): boolean;
830
+ interface GetPerAxisMetricsOpts {
831
+ /** App id to filter on. Required. */
832
+ appId: string;
833
+ /** Intent archetype to filter on. Required. */
834
+ archetype: string;
835
+ /** Canonical model id to filter on. Required. */
836
+ model: string;
837
+ /**
838
+ * Window in days. Default 30. Only rows with
839
+ * `created_at > now() - windowDays` are counted.
840
+ */
841
+ windowDays?: number;
842
+ /**
843
+ * Consumer-declared quality floor (0..1 oracle/approve-rate scale).
844
+ * When set, the response's `qualityFloorMet` is true/false; when omitted,
845
+ * `qualityFloorMet` is null (no floor declared, no judgment).
846
+ */
847
+ qualityFloor?: number;
848
+ /** Pluggable fetch (tests inject mock). Defaults to global fetch. */
849
+ fetch?: typeof fetch;
850
+ /**
851
+ * PostgREST base endpoint (e.g. `https://kgauto-brain.vercel.app/api`).
852
+ * When omitted, falls back to the active configureBrainQuery runtime's
853
+ * endpoint. Returns null when neither is set.
854
+ */
855
+ endpoint?: string;
856
+ /** Bearer token. Forwarded as `Authorization: Bearer ${apiKey}`. */
857
+ apiKey?: string;
858
+ }
859
+ /**
860
+ * Call the `get_per_axis_metrics` RPC and return the typed result.
861
+ *
862
+ * Returns null when:
863
+ * - no endpoint provided AND no configureBrainQuery runtime active
864
+ * - RPC returns empty / null / unexpected shape
865
+ * - brain unreachable / fetch throws / HTTP error
866
+ *
867
+ * Never throws — operator-facing query, must not blow up the caller.
868
+ */
869
+ declare function getPerAxisMetrics(opts: GetPerAxisMetricsOpts): Promise<PerAxisMetrics | null>;
870
+
871
+ /**
872
+ * env.ts — provider env-key resolution + reachability predicates.
873
+ *
874
+ * Centralizes the per-provider env var names that kgauto checks for
875
+ * reachability. Used by:
876
+ *
877
+ * - execute.ts — to find an API key when one isn't passed via apiKeys
878
+ * - call.ts — to auto-filter unreachable models from the fallback walk
879
+ * - getDefaultFallbackChain — opt-in chain filter when consumer passes `reachability`
880
+ * - operator scripts — getReachabilityDiagnostic() prints what's wired up
881
+ *
882
+ * Keeping the map in ONE place means execute() and the reachability check
883
+ * always agree. Without this, kgauto could declare a model "reachable" because
884
+ * env.ts found GOOGLE_GENERATIVE_AI_API_KEY, while execute() looks at
885
+ * GOOGLE_API_KEY only and 401s — shipping the bug we're trying to fix.
886
+ *
887
+ * Resolution order (apiKeys takes precedence):
888
+ * 1. opts.apiKeys?.[provider]
889
+ * 2. process.env[name] for each name in PROVIDER_ENV_KEYS[provider] (first-present wins)
890
+ *
891
+ * alpha.10 (2026-05-14). Resolves the auto-filter-unreachable-models-silently
892
+ * request from PB after first-deploy 401 on the alpha.9 summarize chain.
893
+ */
894
+
895
+ /**
896
+ * Providers kgauto can resolve keys for today. Subset of `Provider` — `mistral`
897
+ * and `xai` are declared in the type union but not yet executable (no profiles,
898
+ * no execute() handler, no env-var convention). Narrowing here keeps the
899
+ * reachability check structurally honest.
900
+ */
901
+ type SupportedProvider = 'anthropic' | 'google' | 'openai' | 'deepseek';
902
+ /**
903
+ * Per-provider env var names kgauto recognizes. Order doesn't matter —
904
+ * first-present wins. Multiple names per provider supported because Google
905
+ * has historical drift (`GOOGLE_API_KEY` from older Google Cloud SDKs,
906
+ * `GEMINI_API_KEY` in many examples, `GOOGLE_GENERATIVE_AI_API_KEY` is the
907
+ * Vercel AI SDK convention used by IC + tt-intel adapters).
908
+ *
909
+ * Frozen so consumers/tests can't mutate (would break the cache invariant
910
+ * that execute() and reachability checks agree).
911
+ */
912
+ declare const PROVIDER_ENV_KEYS: Readonly<Record<SupportedProvider, readonly string[]>>;
913
+ interface ReachabilityOpts {
914
+ /** Explicit keys (alpha.3 ApiKeys). Checked first; takes precedence over env. */
915
+ apiKeys?: ApiKeys;
916
+ /**
917
+ * Override env source. Defaults to `process.env` in Node-shaped runtimes,
918
+ * `{}` everywhere else. Pass `{}` explicitly in tests for hermetic runs.
919
+ */
920
+ envSource?: Record<string, string | undefined>;
921
+ }
922
+ /**
923
+ * Resolve a usable API key for the provider. Returns the key string, or
924
+ * undefined if neither apiKeys nor any of the env names are set.
925
+ *
926
+ * Used internally by execute.ts so the reachability check and the actual
927
+ * call check stay in sync.
928
+ */
929
+ declare function resolveProviderKey(provider: Provider, opts?: ReachabilityOpts): string | undefined;
930
+ /**
931
+ * True iff the provider has a usable key — either via explicit `apiKeys`
932
+ * or one of the `PROVIDER_ENV_KEYS[provider]` names is set in envSource.
933
+ */
934
+ declare function isProviderReachable(provider: Provider, opts?: ReachabilityOpts): boolean;
935
+ /**
936
+ * True iff the model's profile exists AND its provider is reachable.
937
+ * Unknown model id returns false (treat as unreachable; the chain walker
938
+ * will surface "no reachable models" if everything filters out).
939
+ */
940
+ declare function isModelReachable(modelId: string, opts?: ReachabilityOpts): boolean;
941
+ interface ProviderReachability {
942
+ reachable: boolean;
943
+ /** How the key was found. `null` when unreachable. */
944
+ via: 'apiKeys' | 'env' | null;
945
+ /** Which env var name supplied the key (only when via === 'env'). */
946
+ envKeyFound?: string;
947
+ }
948
+ /**
949
+ * Snapshot of which providers are reachable from the current env / apiKeys.
950
+ * Useful for operator scripts ("kgauto diagnose"), startup-time logging,
951
+ * and the cost-watcher's "which consumer is missing what" report.
952
+ *
953
+ * Does NOT log the key value itself — only the env var name that supplied it.
954
+ */
955
+ declare function getReachabilityDiagnostic(opts?: ReachabilityOpts): Record<SupportedProvider, ProviderReachability>;
956
+
957
+ /**
958
+ * getDefaultFallbackChain — the alpha.9 cascading ship.
959
+ *
960
+ * Returns a per-archetype fallback chain that walks the cost/performance
961
+ * Pareto frontier (master plan §1.3 + §3). Three customer postures:
962
+ *
963
+ * locked — caller passes [theOneModel]; never call this function
964
+ * preferred — caller passes `primary`; chain returned is [primary, ...fallbacks]
965
+ * open — caller passes no `primary`; chain returned is [best, ...fallbacks]
966
+ *
967
+ * The chain at each step:
968
+ * 1. Costs strictly less than the previous (no expensive sideways moves)
969
+ * 2. Comes from a different provider than the previous step where possible
970
+ * (correlated outages don't kill consecutive attempts)
971
+ * 3. Stays above the archetype's perf floor (skip models scored <baseline
972
+ * for archetypes where degradation would be unacceptable)
973
+ *
974
+ * In alpha.9 the chain is **hand-curated** per archetype (§3.3 starter
975
+ * table). Brain-query mode lands in alpha.10. Policy.blockedModels filters
976
+ * the result; policy.maxCostPerCallUsd is NOT applied here because the
977
+ * function doesn't see the IR's token counts — that filtering happens at
978
+ * `passScoreTargets()` time inside compile().
979
+ *
980
+ * The function is **pure** — no brain query, no I/O, no randomness. Same
981
+ * inputs always produce the same chain.
982
+ */
983
+
984
+ /**
985
+ * Posture passed into `getDefaultFallbackChain`. The chain function only
986
+ * sees `'open'` and `'preferred'` — callers in `'locked'` posture should
987
+ * pass `models: [theOneModel]` directly and skip this function entirely.
988
+ *
989
+ * Equivalent to `CompilePolicy.posture` minus `'locked'`. Kept distinct so
990
+ * the type system enforces "don't ask for a chain when you don't want one."
991
+ */
992
+ type FallbackPosture = 'open' | 'preferred';
993
+ interface GetDefaultFallbackChainOpts {
994
+ /** The archetype the call is performing. Drives chain shape. */
995
+ archetype: IntentArchetypeName;
996
+ /**
997
+ * The user-selected or caller-anchored primary model. When provided, it
998
+ * appears at position 0 of the returned chain and fallbacks follow.
999
+ * When omitted, the function picks the best-perf model for the archetype
1000
+ * as position 0 (open posture).
1001
+ */
1002
+ primary?: string;
1003
+ /**
1004
+ * Informational. `'preferred'` and `'open'` produce the same chain shape
1005
+ * given the same `primary`/no-primary input — posture is a tag the brain
1006
+ * uses to distinguish "user-anchored" from "library-anchored" telemetry.
1007
+ */
1008
+ posture?: FallbackPosture;
1009
+ /**
1010
+ * Cap on chain length. Default 3. Min 1. Useful when the consumer wants
1011
+ * to keep the worst-case latency low (each fallback adds a round-trip).
1012
+ */
1013
+ maxDepth?: number;
1014
+ /**
1015
+ * Consumer-side gating. `blockedModels` are filtered from the chain.
1016
+ * `preferredModels` is informational (no boost applied at this layer —
1017
+ * compile()'s `passScoreTargets` handles preference ranking).
1018
+ * `maxCostPerCallUsd` is NOT applied here — needs IR-level token
1019
+ * estimation. Use compile()'s policy plumbing instead.
1020
+ */
1021
+ policy?: CompilePolicy;
1022
+ /**
1023
+ * alpha.10. When provided, the chain is filtered to models whose provider
1024
+ * has a reachable API key (via `apiKeys` or one of `PROVIDER_ENV_KEYS[provider]`).
1025
+ * Models whose provider can't be reached are silently dropped. If filtering
1026
+ * leaves the chain empty, returns `[]` — caller decides what to do (call()
1027
+ * throws CallError; this function stays pure).
1028
+ *
1029
+ * Pass `{}` to opt in with `process.env` as the env source. Pass `{ apiKeys, envSource }`
1030
+ * for explicit control (tests, non-Node runtimes). Omit entirely for the
1031
+ * legacy unfiltered behavior — preserves alpha.9 callers byte-for-byte.
1032
+ */
1033
+ reachability?: ReachabilityOpts;
1034
+ /**
1035
+ * alpha.20 E3: consumer-declared tool-orchestration shape. Currently
1036
+ * only affects `archetype: 'hunt'`, where 'sequential' swaps the
1037
+ * parallel-tool-tier-0 chain (Flash → Pro → Sonnet → Haiku) for a
1038
+ * DeepSeek-tier-0 chain (V4-Pro → Flash → Sonnet) — DeepSeek's L-040
1039
+ * parallel-tool cliff doesn't apply when the consumer commits to
1040
+ * single-step orchestration.
1041
+ *
1042
+ * Other archetypes are NOT mode-aware in this release — they ship the
1043
+ * same chain regardless of toolOrchestration. Future versions may
1044
+ * extend mode-awareness to ask/generate/etc. when brain evidence
1045
+ * supports it.
1046
+ *
1047
+ * Default (omitted or 'either'): parallel chain. Back-compat with all
1048
+ * pre-alpha.20 callers.
1049
+ */
1050
+ toolOrchestration?: 'parallel' | 'sequential' | 'either';
1051
+ }
1052
+ /**
1053
+ * Returns the fallback chain for an archetype as a plain `string[]` of
1054
+ * model ids.
1055
+ *
1056
+ * @deprecated since alpha.21 — prefer
1057
+ * {@link getDefaultFallbackChainWithGrounding}, which returns the same chain
1058
+ * shape with a `grounding` label on every entry (measured / capability-fact /
1059
+ * judgment). The string[] return is preserved indefinitely for back-compat —
1060
+ * no functional change in alpha.21. Existing callers don't need to migrate
1061
+ * unless they want to surface the grounding gap to users.
1062
+ */
1063
+ declare function getDefaultFallbackChain(opts: GetDefaultFallbackChainOpts): string[];
1064
+ /**
1065
+ * Returns a shallow copy of the hand-curated starter chain for an archetype.
1066
+ * Useful for tests + the `scripts/digest.mjs` operator readout.
1067
+ */
1068
+ declare function getStarterChain(archetype: IntentArchetypeName): string[];
1069
+ /**
1070
+ * Returns a shallow copy of all starter chains keyed by archetype.
1071
+ * Useful for the `digest.mjs` readout and consumer audits.
1072
+ */
1073
+ declare function getAllStarterChains(): Record<IntentArchetypeName, string[]>;
1074
+ /**
1075
+ * alpha.20 E3 introspection — returns the sequential-mode overlay for an
1076
+ * archetype, or `undefined` when no overlay is registered (the archetype
1077
+ * is mode-agnostic and reuses `STARTER_CHAINS[archetype]`).
1078
+ *
1079
+ * Useful for tests + the `scripts/digest.mjs` operator readout to surface
1080
+ * the mode-aware chains.
1081
+ */
1082
+ declare function getSequentialStarterChain(archetype: IntentArchetypeName): string[] | undefined;
1083
+ /**
1084
+ * alpha.21 (s78 Entry 1) — returns the fallback chain as `ChainEntry[]`,
1085
+ * with a `grounding` label on every position.
1086
+ *
1087
+ * Same selection logic as {@link getDefaultFallbackChain} (primary anchoring,
1088
+ * blockedModels filter, dedupe, reachability filter, maxDepth cap) — the
1089
+ * only difference is the return shape: each position is a `ChainEntry`
1090
+ * carrying `{ id, grounding, reason?, n? }` instead of a bare string.
1091
+ *
1092
+ * Use this when surfacing the chain to consumers who care WHY each entry
1093
+ * sits where it sits — Glass-Box panels, operator dashboards, eval
1094
+ * scaffolding deciding which entries deserve measurement priority.
1095
+ *
1096
+ * Returns `[]` when filtering empties the chain (same semantics as the
1097
+ * string variant) — consumer decides what to do.
1098
+ */
1099
+ declare function getDefaultFallbackChainWithGrounding(opts: GetDefaultFallbackChainOpts): ChainEntry[];
1100
+ /**
1101
+ * alpha.21 introspection — returns the grounded starter chain for an
1102
+ * archetype (no primary anchoring, no policy filtering, no maxDepth cap).
1103
+ * Use this when you want the raw, hand-curated grounded chain — every
1104
+ * entry carries a `grounding` label and optional reason/n.
1105
+ */
1106
+ declare function getStarterChainWithGrounding(archetype: IntentArchetypeName): ChainEntry[];
1107
+ /**
1108
+ * alpha.21 introspection — all grounded starter chains keyed by archetype.
1109
+ * Useful for the `digest.mjs` readout and consumer audits that want to
1110
+ * surface the grounding gap across the entire chain table.
1111
+ */
1112
+ declare function getAllStarterChainsWithGrounding(): Record<IntentArchetypeName, ChainEntry[]>;
1113
+ /**
1114
+ * alpha.21 introspection — sequential-mode overlay with grounding labels,
1115
+ * or `undefined` when no overlay is registered for the archetype.
1116
+ */
1117
+ declare function getSequentialStarterChainWithGrounding(archetype: IntentArchetypeName): ChainEntry[] | undefined;
1118
+
1119
+ /**
1120
+ * chains-brain — alpha.11 KG-11 adapter.
1121
+ *
1122
+ * Brain-driven STARTER_CHAINS for `getDefaultFallbackChain`. Reads
1123
+ * `kgauto_chains` table via the shared brain-query SWR cache (D6 + D8);
1124
+ * falls back to bundled STARTER_CHAINS on cold-start, brain-down, or
1125
+ * empty/missing table (D2 + D4).
1126
+ *
1127
+ * Behavioral note (locked via D2): the sync API surface returns bundled on
1128
+ * cold-start, with a background refresh fired. Subsequent calls within the
1129
+ * 5-min TTL return brain data. Vercel cold-start consumers see the seed
1130
+ * snapshot (functionally identical to pre-alpha.11); warm-start consumers
1131
+ * see live brain mutations within 5 min.
1132
+ */
1133
+ /**
1134
+ * Sync reader for the brain-driven chains map. Returns bundled
1135
+ * STARTER_CHAINS when brain-query is disabled, cold, or unreachable.
1136
+ */
1137
+ declare const loadChainsFromBrain: () => Record<"ask" | "hunt" | "classify" | "summarize" | "generate" | "extract" | "plan" | "critique" | "transform", string[]>;
1138
+
1139
+ /**
1140
+ * archetype-perf-brain — alpha.11 KG-12 adapter.
1141
+ *
1142
+ * Brain-driven archetypePerf scores. Substrate for the future closed-loop
1143
+ * tuning engine (KG-12.5): brain telemetry → human or automated bumps →
1144
+ * brain UPDATE → consumers see new scores within 5-min cache TTL with
1145
+ * zero refresh.
1146
+ *
1147
+ * Today: data migrates to brain. No runtime call site reads archetypePerf
1148
+ * (it's metadata for the master plan §2.5 anti-hallucination guardrail +
1149
+ * future auto-tuning). The adapter exists so future readers — auto-tuning
1150
+ * + operator scripts + KG-12.5 — have a shipped substrate to consume.
1151
+ */
1152
+
1153
+ type ArchetypePerfMap = Map<string, Partial<Record<IntentArchetypeName, number>>>;
1154
+ /**
1155
+ * alpha.21: per-(model, archetype) row count map. Same shape as
1156
+ * ArchetypePerfMap but stores brain row counts when the brain backs a
1157
+ * placement. Undefined entries → no row count seen → score is the
1158
+ * hand-curated cold-start prior (grounding='judgment').
1159
+ */
1160
+ type ArchetypePerfNMap = Map<string, Partial<Record<IntentArchetypeName, number>>>;
1161
+ /**
1162
+ * Sync reader for the brain-driven archetypePerf map. Returns bundled
1163
+ * profile.archetypePerf data when brain-query is disabled, cold, or
1164
+ * unreachable. Identical shape pre/post-alpha.11 by design (D2).
1165
+ */
1166
+ declare const loadArchetypePerfFromBrain: () => ArchetypePerfMap;
1167
+ /**
1168
+ * alpha.21 — sync reader for the brain row-count map paired with
1169
+ * archetype-perf. Returns empty map (no measured backing) on cold start /
1170
+ * brain-down / unreachable — all `getArchetypePerfScore` calls then
1171
+ * resolve to `grounding: 'judgment'`. When the brain table includes an
1172
+ * `n` column on each row, this map mirrors those counts so consumers can
1173
+ * see how many measurements back each score.
1174
+ */
1175
+ declare const loadArchetypePerfNFromBrain: () => ArchetypePerfNMap;
1176
+ /**
1177
+ * Threshold above which a brain row count counts as 'measured' grounding.
1178
+ * Below this, the score is treated as 'judgment' (cold-start prior or
1179
+ * not-yet-enough-evidence). Mirrors the alpha.20 `getCleanPerfScore`
1180
+ * `minRows` default — same rule for consistency.
1181
+ */
1182
+ declare const MEASURED_GROUNDING_MIN_N = 10;
1183
+ /**
1184
+ * alpha.21 — return shape for the extended {@link getArchetypePerfScore}.
1185
+ * Wraps the existing 0..10 score with:
1186
+ *
1187
+ * - `n`: brain row count backing this score (0 when no measurement).
1188
+ * - `grounding`: derived label — 'measured' when n >= 10, else 'judgment'.
1189
+ *
1190
+ * The score itself is unchanged from pre-alpha.21 (numeric, 5 = neutral
1191
+ * default). 'capability-fact' is NOT a perf-score grounding — capability
1192
+ * decisions live on chain entries, not on perf scores.
1193
+ */
1194
+ interface ArchetypePerfScoreResult {
1195
+ /** 0..10 perf score. 5 = neutral default when no entry exists. */
1196
+ score: number;
1197
+ /**
1198
+ * Brain row count backing this score. 0 when bundled (cold-start prior)
1199
+ * or when the brain row didn't carry an `n` column.
1200
+ */
1201
+ n: number;
1202
+ /**
1203
+ * Provenance — 'measured' when `n >= 10`, else 'judgment'.
1204
+ * Never 'capability-fact' (that label is reserved for chain-entry
1205
+ * inclusion/exclusion decisions).
1206
+ */
1207
+ grounding: Grounding;
1208
+ }
1209
+ /**
1210
+ * Per-model accessor with grounding (alpha.21). Returns 5 (neutral) when no
1211
+ * entry is found — consistent with the master plan §3.3 "missing archetypes
1212
+ * default to 5" convention documented in profiles.ts
1213
+ * ModelProfile.archetypePerf.
1214
+ *
1215
+ * Backwards-compat note: pre-alpha.21 callers expected `number` here. The
1216
+ * new return shape `{ score, n, grounding }` is a breaking shape change at
1217
+ * the type level, but `.score` carries the legacy value. Callers reading
1218
+ * `.score` field-by-field continue working; callers using the bare number
1219
+ * arithmetically need to switch to `.score`. The migration is single-line
1220
+ * (`const x = getArchetypePerfScore(...)` → `const x = getArchetypePerfScore(...).score`).
1221
+ */
1222
+ declare function getArchetypePerfScore(modelId: string, archetype: IntentArchetypeName): ArchetypePerfScoreResult;
1223
+
1224
+ /**
1225
+ * pricing-brain — alpha.11 KG-13 adapter.
1226
+ *
1227
+ * Brain-driven pricing data with **time-bounded resolution** (`valid_from`
1228
+ * / `valid_until` columns). The V4-Pro 75%-off promo through 2026-05-31
1229
+ * gets modeled as two rows; `at` parameter resolution picks the correct
1230
+ * row per call timestamp. Promo flips happen automatically at the
1231
+ * boundary without alpha cuts.
1232
+ *
1233
+ * SWR cache holds the FULL pricing snapshot (all active rows for all
1234
+ * models). Per-call `at` filtering is in-memory — keeps the SWR semantics
1235
+ * uniform with other adapters (one cache, one snapshot, sync reads).
1236
+ */
1237
+ interface PricingRow {
1238
+ modelId: string;
1239
+ costInputPer1m: number;
1240
+ costOutputPer1m: number;
1241
+ cacheInputPer1m?: number;
1242
+ cacheCreationPer1m?: number;
1243
+ validFrom: number;
1244
+ validUntil?: number;
1245
+ source?: string;
1246
+ }
1247
+ /**
1248
+ * Sync reader for the brain-driven pricing snapshot. Returns bundled
1249
+ * profile pricing when brain-query is disabled, cold, or unreachable.
1250
+ * Caller filters by `at` via {@link resolvePricingAt}.
1251
+ */
1252
+ declare const loadPricingFromBrain: () => PricingRow[];
1253
+ /**
1254
+ * Resolve the active pricing row for a model at a given timestamp.
1255
+ * Picks the row with the latest `valid_from <= at` whose
1256
+ * `valid_until > at` (or NULL — open-ended).
1257
+ *
1258
+ * Returns `undefined` when no row matches. Callers should fall back to
1259
+ * profile static pricing in that case.
1260
+ */
1261
+ declare function resolvePricingAt(modelId: string, at?: Date): PricingRow | undefined;
1262
+
1263
+ /**
1264
+ * models-brain — alpha.11 KG-14 adapter (the largest of the four).
1265
+ *
1266
+ * Brain-driven model registry + aliases. Two adapters share the same
1267
+ * SWR snapshot:
1268
+ *
1269
+ * - `loadModelsFromBrain()` — `Map<modelId, ModelProfile>` from
1270
+ * `kgauto_models` table (cliffs/lowering/recovery as JSONB columns).
1271
+ * - `loadAliasesFromBrain()` — `Record<aliasId, canonicalId>` from
1272
+ * `kgauto_aliases` table.
1273
+ *
1274
+ * After alpha.11, new-model onboarding becomes brain INSERT (vs PR + alpha
1275
+ * cut + 3 consumer refreshes). The auto-onboard pipeline
1276
+ * (`scripts/check-model-releases.mjs`) shifts from emitting profile.ts
1277
+ * edits to writing brain rows directly.
1278
+ *
1279
+ * **D5 — alias resolution stable regardless of canonical's active state.**
1280
+ * `canonicalId('deepseek-chat') → 'deepseek-v4-flash'` even when the
1281
+ * canonical row is `active=false`. Aliases are wire-format contracts;
1282
+ * legacy callers' resolution promise outlives the canonical's active
1283
+ * status. Deprecation (`active=false`) affects chain composition only.
1284
+ */
1285
+
1286
+ /**
1287
+ * Exported brain-row shape for `kgauto_models`. Mirrors the SQL table
1288
+ * (`v2/brain/migrations/010_kgauto_models_and_aliases.sql`) — column names
1289
+ * are snake_case to match PostgREST. Used by operator scripts that write
1290
+ * to brain (auto-onboard, promote-model) — see `profileToRow()`.
1291
+ *
1292
+ * Read path stays internal via `RawModelRow` to keep `rowToProfile()`'s
1293
+ * tolerance contract from leaking into write callers.
1294
+ */
1295
+ interface ModelBrainRow {
1296
+ model_id: string;
1297
+ provider: string;
1298
+ status?: string;
1299
+ max_context_tokens?: number;
1300
+ max_output_tokens?: number;
1301
+ max_tools?: number;
1302
+ parallel_tool_calls?: boolean;
1303
+ structured_output?: string;
1304
+ system_prompt_mode?: string;
1305
+ streaming?: boolean;
1306
+ cliffs?: unknown;
1307
+ lowering?: unknown;
1308
+ recovery?: unknown;
1309
+ strengths?: string[] | null;
1310
+ weaknesses?: string[] | null;
1311
+ cost_input_per_1m?: number;
1312
+ cost_output_per_1m?: number;
1313
+ notes?: string | null;
1314
+ verified_against_docs?: string | null;
1315
+ archetype_perf?: Record<string, number> | null;
1316
+ version_added?: string;
1317
+ version_removed?: string | null;
1318
+ active?: boolean;
1319
+ }
1320
+ interface ProfileToRowOptions {
1321
+ /** e.g. `'2.0.0-alpha.12'` — leave undefined to omit from row. */
1322
+ versionAdded?: string;
1323
+ /** Pass `null` to clear; omit to leave field unset. */
1324
+ versionRemoved?: string | null;
1325
+ /** Defaults to true if omitted. */
1326
+ active?: boolean;
1327
+ /**
1328
+ * Override verifiedAgainstDocs (e.g. set to `null` for auto-onboard).
1329
+ * When omitted, the profile's own value is used. Pass `null` explicitly
1330
+ * to write a SQL NULL (e.g. unverified auto-onboard rows).
1331
+ */
1332
+ verifiedAgainstDocs?: string | null;
1333
+ }
1334
+ /**
1335
+ * Inverse of `rowToProfile` — serialize a `ModelProfile` to a `kgauto_models`
1336
+ * row payload for INSERT/UPSERT. Used by operator scripts that write to
1337
+ * brain (auto-onboard pipeline, promote-model verification CLI).
1338
+ *
1339
+ * Row-level fields (`version_added`, `version_removed`, `active`) are
1340
+ * operator-controlled and pass through `opts`. `verifiedAgainstDocs` can
1341
+ * also be overridden via `opts` — auto-onboard explicitly passes `null` to
1342
+ * mark unverified rows (the column is DATE, doesn't accept the
1343
+ * `'UNVERIFIED-AUTO-ONBOARD'` sentinel used in PROFILES_RAW).
1344
+ */
1345
+ declare function profileToRow(profile: ModelProfile, opts?: ProfileToRowOptions): ModelBrainRow;
1346
+ /**
1347
+ * Sync reader for the brain-driven model registry. Returns bundled
1348
+ * PROFILES_RAW when brain-query is disabled, cold, or unreachable.
1349
+ */
1350
+ declare const loadModelsFromBrain: () => Map<string, ModelProfile>;
1351
+ /**
1352
+ * Sync reader for the brain-driven aliases map. Returns bundled ALIASES
1353
+ * when brain-query is disabled, cold, or unreachable.
1354
+ *
1355
+ * D5: this map carries both active and inactive canonical mappings —
1356
+ * alias resolution is stable regardless of canonical's `active` state.
1357
+ */
1358
+ declare const loadAliasesFromBrain: () => Record<string, string>;
1359
+
192
1360
  /**
193
1361
  * @warmdrift/kgauto v2 — prompt compiler + central learning brain.
194
1362
  *
@@ -235,4 +1403,4 @@ declare function countTokens(text: string): number;
235
1403
  */
236
1404
  declare function compile(ir: PromptIR, opts?: CompileOptions): CompileResult;
237
1405
 
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 };
1406
+ 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 };