@warmdrift/kgauto-compiler 2.0.0-alpha.7 → 2.0.0-alpha.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -46
- package/dist/brain-proxy.d.mts +113 -0
- package/dist/brain-proxy.d.ts +113 -0
- package/dist/brain-proxy.js +193 -0
- package/dist/brain-proxy.mjs +6 -0
- package/dist/chunk-4UO4CCSP.mjs +1620 -0
- package/dist/chunk-65ZMX5OT.mjs +169 -0
- package/dist/{chunk-5TI6PNSK.mjs → chunk-BVEXV5KC.mjs} +11 -0
- package/dist/chunk-NBO4R5PC.mjs +313 -0
- package/dist/chunk-P3TOAEG4.mjs +56 -0
- package/dist/chunk-RO22VFIF.mjs +29 -0
- package/dist/chunk-SBFSYCQG.mjs +719 -0
- package/dist/chunk-URFQR3SB.mjs +203 -0
- package/dist/dialect.d.mts +41 -3
- package/dist/dialect.d.ts +41 -3
- package/dist/dialect.js +14 -2
- package/dist/dialect.mjs +5 -3
- package/dist/glassbox/index.d.mts +59 -0
- package/dist/glassbox/index.d.ts +59 -0
- package/dist/glassbox/index.js +312 -0
- package/dist/glassbox/index.mjs +12 -0
- package/dist/glassbox-routes/format.d.mts +24 -0
- package/dist/glassbox-routes/format.d.ts +24 -0
- package/dist/glassbox-routes/format.js +86 -0
- package/dist/glassbox-routes/format.mjs +18 -0
- package/dist/glassbox-routes/index.d.mts +191 -0
- package/dist/glassbox-routes/index.d.ts +191 -0
- package/dist/glassbox-routes/index.js +2888 -0
- package/dist/glassbox-routes/index.mjs +667 -0
- package/dist/glassbox-routes/react/index.d.mts +74 -0
- package/dist/glassbox-routes/react/index.d.ts +74 -0
- package/dist/glassbox-routes/react/index.js +819 -0
- package/dist/glassbox-routes/react/index.mjs +754 -0
- package/dist/index.d.mts +2745 -17
- package/dist/index.d.ts +2745 -17
- package/dist/index.js +9006 -1651
- package/dist/index.mjs +5103 -367
- package/dist/ir-BEQ28muo.d.ts +1608 -0
- package/dist/ir-CnnJST_N.d.mts +1608 -0
- package/dist/key-health.d.mts +131 -0
- package/dist/key-health.d.ts +131 -0
- package/dist/key-health.js +228 -0
- package/dist/key-health.mjs +6 -0
- package/dist/profiles.d.mts +292 -2
- package/dist/profiles.d.ts +292 -2
- package/dist/profiles.js +1231 -16
- package/dist/profiles.mjs +9 -1
- package/dist/types-B--CYzMo.d.ts +131 -0
- package/dist/types-BDFrJkma.d.mts +131 -0
- package/dist/types-DSeJJ6tt.d.ts +149 -0
- package/dist/types-DeGRCTlJ.d.mts +149 -0
- package/package.json +54 -8
- package/dist/chunk-MBEI5UOM.mjs +0 -409
- package/dist/profiles-B3eNQ2py.d.ts +0 -619
- package/dist/profiles-Py8c7zjJ.d.mts +0 -619
|
@@ -0,0 +1,1608 @@
|
|
|
1
|
+
import { IntentArchetypeName, OutputMode } from './dialect.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Golden-set capture (alpha.62, eval spine — design brief 2026-07-17).
|
|
5
|
+
*
|
|
6
|
+
* The eval-driven self-improvement contract needs a replay corpus: 20–50 real
|
|
7
|
+
* historical IRs per (app, archetype), stored WITH the incumbent's output.
|
|
8
|
+
* This module is the capture side — a sampling gate on call()'s success path
|
|
9
|
+
* that stores the full PromptIR + served response to `kgauto_golden_irs`.
|
|
10
|
+
*
|
|
11
|
+
* ## Consent posture (s51 amendment, ratified 2026-07-17)
|
|
12
|
+
*
|
|
13
|
+
* Golden rows carry RAW prompts — a deliberate, documented departure from the
|
|
14
|
+
* s51 "brain holds no raw consumer prompts" rule. Capture is therefore
|
|
15
|
+
* strictly opt-in:
|
|
16
|
+
*
|
|
17
|
+
* - `KGAUTO_GOLDEN_CAPTURE=0.1` in the consumer's environment (the env var
|
|
18
|
+
* IS the consent act — the consumer's operator sets it knowingly), or
|
|
19
|
+
* - `CallOptions.goldenCapture.sampleRate` in consumer code.
|
|
20
|
+
*
|
|
21
|
+
* Default is 0 (off): absent flag, empty string, non-finite, or ≤0 all mean
|
|
22
|
+
* "never capture". Every stored row records its consent provenance in the
|
|
23
|
+
* `consent` column. Reads are strictly app-scoped (RLS) — a consumer can only
|
|
24
|
+
* ever see its own raw prompts.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** Consumer-facing capture options (CallOptions.goldenCapture). */
|
|
28
|
+
interface GoldenCaptureOptions {
|
|
29
|
+
/**
|
|
30
|
+
* Probability [0..1] that a successful call's IR + output is stored as a
|
|
31
|
+
* golden row. Overrides the KGAUTO_GOLDEN_CAPTURE env var when set.
|
|
32
|
+
*/
|
|
33
|
+
sampleRate?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Optional free-text appended to the stored consent provenance (e.g. a
|
|
36
|
+
* ticket ref or the operator who approved capture for this surface).
|
|
37
|
+
*/
|
|
38
|
+
consentNote?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Parse a capture-rate env value. Mirrors the consumer-side
|
|
42
|
+
* `parseShadowProbeRate` semantics playbacksam established: undefined, empty,
|
|
43
|
+
* or non-finite → 0 (default-OFF); result clamped to [0, 1].
|
|
44
|
+
*/
|
|
45
|
+
declare function parseGoldenCaptureRate(raw: string | undefined): number;
|
|
46
|
+
/**
|
|
47
|
+
* Resolve the effective capture rate: explicit option wins; otherwise the
|
|
48
|
+
* KGAUTO_GOLDEN_CAPTURE env var (when a process env exists — Edge-safe).
|
|
49
|
+
*/
|
|
50
|
+
declare function resolveGoldenCaptureRate(optRate?: number): number;
|
|
51
|
+
/** Sampling gate. Injectable rng for tests (same shape as shouldSampleProbe). */
|
|
52
|
+
declare function shouldCaptureGolden(rate: number, rng?: () => number): boolean;
|
|
53
|
+
/** Everything call()'s success path has in scope when capture fires. */
|
|
54
|
+
interface GoldenCaptureContext {
|
|
55
|
+
ir: PromptIR;
|
|
56
|
+
servedModel: string;
|
|
57
|
+
response: NormalizedResponse;
|
|
58
|
+
latencyMs: number;
|
|
59
|
+
handle: string;
|
|
60
|
+
/** Compile-time shape key (peeked before record() popped the registry). */
|
|
61
|
+
shapeKey?: string;
|
|
62
|
+
/** How the rate was resolved — recorded as consent provenance. */
|
|
63
|
+
consent: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Store one golden row from a live served call. Fire-and-forget discipline is
|
|
67
|
+
* recordGoldenIr()'s (dead-letter, ledger, honors BrainConfig.sync); this
|
|
68
|
+
* wrapper only shapes the row. Never throws.
|
|
69
|
+
*/
|
|
70
|
+
declare function captureGoldenIr(ctx: GoldenCaptureContext): Promise<void>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A semantically-named section of the system prompt. Sections enable
|
|
74
|
+
* intent-aware slicing (drop sections not tagged for this intent), dedupe
|
|
75
|
+
* (collapse identical sections across files), and cache marking (identify
|
|
76
|
+
* the stable prefix).
|
|
77
|
+
*/
|
|
78
|
+
interface PromptSection {
|
|
79
|
+
/** Stable identifier — used for slicing, dedupe, and cache markers. */
|
|
80
|
+
id: string;
|
|
81
|
+
/** Section text. */
|
|
82
|
+
text: string;
|
|
83
|
+
/**
|
|
84
|
+
* Which intents this section applies to. Empty = applies to all intents.
|
|
85
|
+
* Pass `compile()` will drop sections whose intents array doesn't include
|
|
86
|
+
* the current intent.
|
|
87
|
+
*/
|
|
88
|
+
intents?: IntentArchetypeName[];
|
|
89
|
+
/**
|
|
90
|
+
* If true, this section is part of the stable cacheable prefix. The lower
|
|
91
|
+
* pass uses this to place cache markers correctly per target.
|
|
92
|
+
*/
|
|
93
|
+
cacheable?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Section weight when ordering — lower = earlier in the assembled prompt.
|
|
96
|
+
* Defaults to insertion order.
|
|
97
|
+
*/
|
|
98
|
+
weight?: number;
|
|
99
|
+
/**
|
|
100
|
+
* alpha.29+ — declares the section's semantic kind so kgauto can apply
|
|
101
|
+
* model-aware rewrites at compile time. Default `'arbitrary'` (when
|
|
102
|
+
* unset) for full back-compat — pre-alpha.29 sections continue working
|
|
103
|
+
* unchanged.
|
|
104
|
+
*
|
|
105
|
+
* alpha.29 ships rewrites for `tool_call_contract` only. Other kinds are
|
|
106
|
+
* type-accepted but pass through. alpha.30+ will add rewrites for
|
|
107
|
+
* `narration_contract`, `role_intro`, etc.
|
|
108
|
+
*
|
|
109
|
+
* See `translator.ts` for the rewrite engine that consumes this field.
|
|
110
|
+
*/
|
|
111
|
+
kind?: SectionKind;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* alpha.29+ — semantic kind tag for a `PromptSection`. The translator
|
|
115
|
+
* (`v2/src/translator.ts`) consumes this to apply model-aware rewrites at
|
|
116
|
+
* compile time. CLOSED union; future kinds extend it explicitly in named
|
|
117
|
+
* alpha releases.
|
|
118
|
+
*
|
|
119
|
+
* alpha.29 ships rewrites for `tool_call_contract` only. Other kinds are
|
|
120
|
+
* type-accepted but pass through.
|
|
121
|
+
*
|
|
122
|
+
* - `role_intro` — "You are a helpful assistant", persona blocks
|
|
123
|
+
* - `tool_call_contract` — tool-use rules ("call X then Y"); the alpha.29
|
|
124
|
+
* translator rewrites this for models with a
|
|
125
|
+
* sequential-tool cliff on the active archetype
|
|
126
|
+
* - `narration_contract` — output-format rules ("don't narrate your steps");
|
|
127
|
+
* alpha.30+ candidate
|
|
128
|
+
* - `discipline_contract`— alpha.68 / Release A (delegation-fanout-accelerator
|
|
129
|
+
* §5.D): surface-scaffolding gate block. When declared
|
|
130
|
+
* on a role/system section, the translator prepends the
|
|
131
|
+
* frozen `discipline-gates-v1` preamble subject to a
|
|
132
|
+
* two-factor eligibility screen — Factor A (archetype:
|
|
133
|
+
* hunt/summarize/plan/critique/judge) AND Factor C
|
|
134
|
+
* (output-shape: text only, never json/tool_call).
|
|
135
|
+
* Clears no cliff, emits no wireOverrides ⇒ no advisor
|
|
136
|
+
* suppression. See `translator.ts`.
|
|
137
|
+
* - `user_turn` — when sections carry user content rather than
|
|
138
|
+
* system context (rare)
|
|
139
|
+
* - `reference` — supporting reference data the model may consult
|
|
140
|
+
* - `arbitrary` — explicit pass-through (default when unset)
|
|
141
|
+
*/
|
|
142
|
+
type SectionKind = 'role_intro' | 'tool_call_contract' | 'narration_contract' | 'discipline_contract' | 'user_turn' | 'reference' | 'arbitrary';
|
|
143
|
+
interface ToolDefinition {
|
|
144
|
+
name: string;
|
|
145
|
+
description?: string;
|
|
146
|
+
parameters?: Record<string, unknown>;
|
|
147
|
+
/**
|
|
148
|
+
* Per-intent relevance scores. Compile uses these to drop irrelevant tools.
|
|
149
|
+
* Missing intents default to 0.5 (neutral).
|
|
150
|
+
*/
|
|
151
|
+
relevanceByIntent?: Partial<Record<IntentArchetypeName, number>>;
|
|
152
|
+
/** Pass-through for provider-specific fields (Anthropic input_schema, etc.). */
|
|
153
|
+
[key: string]: unknown;
|
|
154
|
+
}
|
|
155
|
+
interface Message {
|
|
156
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
157
|
+
content: string;
|
|
158
|
+
/** Optional structured parts (tool calls, results) — passed through to lowering. */
|
|
159
|
+
parts?: unknown[];
|
|
160
|
+
/** For tool messages — which tool this corresponds to. */
|
|
161
|
+
toolName?: string;
|
|
162
|
+
/** For tool messages — the call id. */
|
|
163
|
+
toolCallId?: string;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The compile-time intent declaration. `name` is the app's local label;
|
|
167
|
+
* `archetype` is the canonical dialect-v1 archetype the app maps it to.
|
|
168
|
+
*
|
|
169
|
+
* Apps with their own intent vocabulary (tt-intelligence's "ask"/"hunt"/
|
|
170
|
+
* "dashboard") declare the mapping here. The brain learns by archetype, not
|
|
171
|
+
* by app-local name.
|
|
172
|
+
*/
|
|
173
|
+
interface IntentDeclaration {
|
|
174
|
+
/** App-local intent name (free-form, for app's own debugging). */
|
|
175
|
+
name: string;
|
|
176
|
+
/** Canonical dialect-v1 archetype. Required for cross-app learning. */
|
|
177
|
+
archetype: IntentArchetypeName;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* alpha.57 — dialect-level reasoning-effort tier (data-first).
|
|
181
|
+
*
|
|
182
|
+
* Vocabulary is a portfolio-level tier ladder (low → max), NOT any provider's
|
|
183
|
+
* wire value — mapping to `reasoning_effort` (OpenAI) / thinking budgets
|
|
184
|
+
* (Anthropic) / `thinkingConfig` (Gemini) stays consumer-side until
|
|
185
|
+
* routing-on-effort ships. Declaring it records the tier on the brain row
|
|
186
|
+
* (`compile_outcomes.effort`, migration 033) so effort×archetype quality
|
|
187
|
+
* evidence accumulates BEFORE any routing logic exists — same data-first
|
|
188
|
+
* sequencing as `tool_orchestration` (alpha.20).
|
|
189
|
+
*/
|
|
190
|
+
type EffortLevel = 'low' | 'medium' | 'high' | 'xhigh' | 'max';
|
|
191
|
+
interface Constraints {
|
|
192
|
+
/** Hard latency ceiling — compiler will down-rank slow models. Advisory. */
|
|
193
|
+
maxLatencyMs?: number;
|
|
194
|
+
/** Hard cost ceiling per call (USD). Advisory. */
|
|
195
|
+
maxCostUsd?: number;
|
|
196
|
+
/**
|
|
197
|
+
* alpha.57 (data-first): the reasoning-effort tier the consumer ran (or
|
|
198
|
+
* intends to run) this call at. RECORDED, NOT APPLIED — the compiler does
|
|
199
|
+
* not emit provider thinking/effort params from this field and does not
|
|
200
|
+
* route on it yet; it flows to `compile_outcomes.effort` via record()'s
|
|
201
|
+
* registry auto-enrich (same pattern as `mutationsApplied`/advisories).
|
|
202
|
+
* Declare it truthfully: this is consumer-reported ground truth, like
|
|
203
|
+
* `tokensIn`/`latencyMs`. Does NOT enter the shape key (no learning-key
|
|
204
|
+
* fragmentation).
|
|
205
|
+
*/
|
|
206
|
+
effort?: EffortLevel;
|
|
207
|
+
/** Caller wants structured (JSON) output. */
|
|
208
|
+
structuredOutput?: boolean;
|
|
209
|
+
/**
|
|
210
|
+
* alpha.69 — consumer-declared output shape. Overrides the inference in
|
|
211
|
+
* `resolveOutputMode` (structuredOutput ⇒ json · tools ⇒ tool_call · else
|
|
212
|
+
* text), which is deliberately conservative: it cannot see that an agentic
|
|
213
|
+
* surface carrying tools ultimately emits prose to the user.
|
|
214
|
+
*
|
|
215
|
+
* Declaring `'text'` on a tool-carrying surface is what re-opens the
|
|
216
|
+
* alpha.68 discipline gates for it (Factor C is a hard screen on inferred
|
|
217
|
+
* `tool_call`, because wrongly firing breaks a parser while wrongly
|
|
218
|
+
* withholding only forgoes a lift). Declare it truthfully — it is
|
|
219
|
+
* consumer-reported ground truth about what the model emits, and it feeds
|
|
220
|
+
* `shape_key`, so a false declaration fragments the learning key AND lets
|
|
221
|
+
* shape-altering rules fire on a structured surface.
|
|
222
|
+
*/
|
|
223
|
+
outputMode?: OutputMode;
|
|
224
|
+
/** Hint: caller expects a short response (used to disable thinking on Gemini). */
|
|
225
|
+
expectedShortOutput?: boolean;
|
|
226
|
+
/** Hint: max response words. */
|
|
227
|
+
maxResponseWords?: number;
|
|
228
|
+
/** Override target model selection — if set, compiler uses this instead of routing. */
|
|
229
|
+
forceModel?: string;
|
|
230
|
+
/**
|
|
231
|
+
* alpha.20: consumer-declared tool-orchestration shape for this call.
|
|
232
|
+
* - 'parallel': model may fire multiple tool calls per step (current
|
|
233
|
+
* default behavior; the L-040 cliff applies — DeepSeek's
|
|
234
|
+
* `tool_count >= 1` cliff trims tools because parallel-tool throughput
|
|
235
|
+
* collapses to sequential semantics).
|
|
236
|
+
* - 'sequential': consumer commits to one tool call per step (the agentic
|
|
237
|
+
* loop pattern). DeepSeek V4-Flash + V4-Pro can compete cleanly in
|
|
238
|
+
* this mode — the L-040 cliff is silenced and the hunt chain shifts
|
|
239
|
+
* to a DeepSeek-tier-1 ordering.
|
|
240
|
+
* - 'either': consumer doesn't care; library picks the parallel chain
|
|
241
|
+
* (status-quo default) and may upgrade to brain-driven per-mode perf
|
|
242
|
+
* selection in a future release.
|
|
243
|
+
*
|
|
244
|
+
* Affects:
|
|
245
|
+
* - Chain composition for `archetype: 'hunt'` (see
|
|
246
|
+
* `getDefaultFallbackChain` and `STARTER_CHAINS_BY_MODE`).
|
|
247
|
+
* - L-040 cliff in `passApplyCliffs` (silent when 'sequential').
|
|
248
|
+
*
|
|
249
|
+
* Default (when undefined): equivalent to 'parallel' for back-compat
|
|
250
|
+
* with every pre-alpha.20 caller.
|
|
251
|
+
*/
|
|
252
|
+
toolOrchestration?: 'parallel' | 'sequential' | 'either';
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Cache marker policy for the messages array (history + currentTurn).
|
|
256
|
+
*
|
|
257
|
+
* Anthropic positional caching: a `cache_control` marker on a content block
|
|
258
|
+
* tells the API "remember the prefix up through this block." On a subsequent
|
|
259
|
+
* request whose first N tokens match, those N billed at the cached rate
|
|
260
|
+
* (10% of the input price). Without a marker, every call re-pays for the
|
|
261
|
+
* entire history.
|
|
262
|
+
*
|
|
263
|
+
* - `'none'` (default when omitted): no history cache marker. System-level
|
|
264
|
+
* cache markers from `PromptSection.cacheable=true` still apply.
|
|
265
|
+
* - `'all-but-latest'`: marks the message immediately preceding `currentTurn`
|
|
266
|
+
* (the last history entry). On the next call, that entire history prefix
|
|
267
|
+
* is cacheable. Good fit for chat/agent loops where every prior turn is
|
|
268
|
+
* stable.
|
|
269
|
+
* - `'fixed-suffix'`: marks the message `suffix` positions from the end of
|
|
270
|
+
* `history`. Use when the last few turns are volatile (e.g., scratchpad,
|
|
271
|
+
* draft revisions) but the earlier prefix is stable.
|
|
272
|
+
*
|
|
273
|
+
* For non-Anthropic providers, no wire-format marker is emitted (Gemini /
|
|
274
|
+
* OpenAI / DeepSeek implicit caching takes effect automatically when a
|
|
275
|
+
* stable prefix is reused). The compiler still computes
|
|
276
|
+
* `diagnostics.historyCacheableTokens` for telemetry on every provider.
|
|
277
|
+
*
|
|
278
|
+
* alpha.5.
|
|
279
|
+
*/
|
|
280
|
+
type HistoryCachePolicy = {
|
|
281
|
+
strategy: 'none';
|
|
282
|
+
} | {
|
|
283
|
+
strategy: 'all-but-latest';
|
|
284
|
+
} | {
|
|
285
|
+
strategy: 'fixed-suffix';
|
|
286
|
+
suffix: number;
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Consumer-declared policy for model selection. Lives outside the IR
|
|
290
|
+
* (passed via CompileOptions) because it's a SESSION/APP-level constraint,
|
|
291
|
+
* not a per-call shape.
|
|
292
|
+
*
|
|
293
|
+
* The original tt-intelligence scenario (s11): user capped Anthropic
|
|
294
|
+
* spending on Sonnet for cost reasons. v2 compile() kept picking Sonnet
|
|
295
|
+
* as the best target, Hunter's preflight hit the cap and fell back to
|
|
296
|
+
* Flash — every single call. CompilePolicy.blockedModels lets the
|
|
297
|
+
* consumer tell kgauto "don't pick Sonnet right now" and the compiler
|
|
298
|
+
* routes to the next-best option directly. No wasted preflight tax.
|
|
299
|
+
*
|
|
300
|
+
* This is the "coach knows the constraints" feature — kgauto stops
|
|
301
|
+
* recommending things the consumer has already ruled out.
|
|
302
|
+
*/
|
|
303
|
+
interface CompilePolicy {
|
|
304
|
+
/**
|
|
305
|
+
* Model IDs the consumer has gated. Compile() will never select these.
|
|
306
|
+
* Use for: cost caps, account-level rate limits, "this model is broken
|
|
307
|
+
* for our workload" decisions.
|
|
308
|
+
*/
|
|
309
|
+
blockedModels?: string[];
|
|
310
|
+
/**
|
|
311
|
+
* Hard ceiling on estimated input cost per call (USD). Models whose
|
|
312
|
+
* estimated cost exceeds this are rejected. Use for: budget enforcement
|
|
313
|
+
* on high-volume routes.
|
|
314
|
+
*/
|
|
315
|
+
maxCostPerCallUsd?: number;
|
|
316
|
+
/**
|
|
317
|
+
* alpha.68 / Release A (delegation-fanout-accelerator §5.1) — trace-level
|
|
318
|
+
* cost ceiling (USD) across a fan-out `trace_id`'s cumulative spend.
|
|
319
|
+
*
|
|
320
|
+
* RECORDED / INERT in Release A: nothing fans out yet, so this field is
|
|
321
|
+
* accepted and carried but the compiler enforces NOTHING from it. It is
|
|
322
|
+
* enforced by the `delegate` primitive against the running trace spend in
|
|
323
|
+
* Release D. Kept distinct from `maxCostPerCallUsd` because that per-call
|
|
324
|
+
* ceiling is trivially evadable N times under fan-out — a fan-out budget
|
|
325
|
+
* has to be trace-scoped, not per-call.
|
|
326
|
+
*/
|
|
327
|
+
maxCostPerTraceUsd?: number;
|
|
328
|
+
/**
|
|
329
|
+
* Model IDs the consumer prefers. When multiple models fit, preferred
|
|
330
|
+
* models get a rank boost (large enough to overcome small quality
|
|
331
|
+
* differences but not large enough to override hard rejects).
|
|
332
|
+
*/
|
|
333
|
+
preferredModels?: string[];
|
|
334
|
+
/**
|
|
335
|
+
* Customer-posture tag (master plan §1.2, alpha.9).
|
|
336
|
+
*
|
|
337
|
+
* - `'locked'` — compliance/contract/brand-promise. Caller passes
|
|
338
|
+
* exactly one model; no fallback is desired. kgauto
|
|
339
|
+
* never walks the chain.
|
|
340
|
+
* - `'preferred'` — user-selected primary, fallback chain as safety
|
|
341
|
+
* net. On 429/5xx, walk the chain and surface
|
|
342
|
+
* `fellOverFrom` so the consumer can show "Claude
|
|
343
|
+
* was busy; we used Pro for this answer."
|
|
344
|
+
* - `'open'` — library picks the chain. Model identity is
|
|
345
|
+
* irrelevant; output is the contract.
|
|
346
|
+
*
|
|
347
|
+
* The field is **informational** — kgauto's execution path is already
|
|
348
|
+
* determined by the shape of `ir.models`. Posture surfaces in
|
|
349
|
+
* telemetry so the cost-watcher can distinguish "locked failed, no
|
|
350
|
+
* fallback was tried" from "open chain exhausted." Default: when
|
|
351
|
+
* `ir.models.length === 1` posture is treated as `'locked'` by the
|
|
352
|
+
* advisor; otherwise unspecified.
|
|
353
|
+
*/
|
|
354
|
+
posture?: 'locked' | 'preferred' | 'open';
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* alpha.41 — entry in `PromptIR.models[]`. Either a literal model id (the
|
|
358
|
+
* pre-alpha.41 shape, fully preserved) or a `{ family: string }` alias that
|
|
359
|
+
* resolves at compile() time to the latest-current model in that family.
|
|
360
|
+
*
|
|
361
|
+
* Family entries resolve via `resolveFamilyEntry` (internal twin of the
|
|
362
|
+
* public `getRecommendedPrimary`). Resolution fails CLOSED — a family that
|
|
363
|
+
* matches no current+active candidate throws `FamilyResolutionError` at
|
|
364
|
+
* compile time. Consumers who want a literal fallback should call
|
|
365
|
+
* `getRecommendedPrimary({ family, fallback, ... })` at IR-construction time
|
|
366
|
+
* and inline the resolved id, rather than passing `{ family }` into the IR.
|
|
367
|
+
*
|
|
368
|
+
* Family taxonomy is defined in `family-resolution.ts`; see also migration
|
|
369
|
+
* 024 (`kgauto_models.family` column + index).
|
|
370
|
+
*/
|
|
371
|
+
type ChainModelEntry = string | {
|
|
372
|
+
family: string;
|
|
373
|
+
};
|
|
374
|
+
/**
|
|
375
|
+
* The IR — the input to compile().
|
|
376
|
+
*/
|
|
377
|
+
interface PromptIR {
|
|
378
|
+
/** App identifier — required for multi-tenant brain. */
|
|
379
|
+
appId: string;
|
|
380
|
+
/** Intent declaration — what is this call doing? */
|
|
381
|
+
intent: IntentDeclaration;
|
|
382
|
+
/** Structured system prompt sections. */
|
|
383
|
+
sections: PromptSection[];
|
|
384
|
+
/** Available tools (compiler may drop based on intent relevance + budget). */
|
|
385
|
+
tools?: ToolDefinition[];
|
|
386
|
+
/** Conversation history (compiler may compress old turns). */
|
|
387
|
+
history?: Message[];
|
|
388
|
+
/** The user's current turn — never dropped. */
|
|
389
|
+
currentTurn?: Message;
|
|
390
|
+
/**
|
|
391
|
+
* Allowed model IDs (or family aliases — alpha.41+), in caller-preference
|
|
392
|
+
* order. Compiler resolves family entries to literal ids before scoring
|
|
393
|
+
* and picks among them. See {@link ChainModelEntry}.
|
|
394
|
+
*
|
|
395
|
+
* Internal passes downstream of compile()'s family-resolution step
|
|
396
|
+
* receive a narrowed `string[]` shape — `string` is a subtype of
|
|
397
|
+
* `ChainModelEntry`, so a `string[]` IS a `ChainModelEntry[]` at the
|
|
398
|
+
* type level. Code that needs to access string ids (passes.ts) reads
|
|
399
|
+
* via `getModelIds(ir)` from `models-runtime.ts` rather than narrowing
|
|
400
|
+
* inline.
|
|
401
|
+
*/
|
|
402
|
+
models: ChainModelEntry[];
|
|
403
|
+
/** Compile constraints. */
|
|
404
|
+
constraints?: Constraints;
|
|
405
|
+
/**
|
|
406
|
+
* Cache marker placement policy for the messages array. Default = no
|
|
407
|
+
* history cache markers. See `HistoryCachePolicy` for semantics.
|
|
408
|
+
* alpha.5.
|
|
409
|
+
*/
|
|
410
|
+
historyCachePolicy?: HistoryCachePolicy;
|
|
411
|
+
}
|
|
412
|
+
type Provider = 'anthropic' | 'google' | 'openai' | 'deepseek' | 'zai' | 'moonshot' | 'mistral' | 'xai';
|
|
413
|
+
/**
|
|
414
|
+
* Mutation IDs that fired during compile. Empty in v1 (no mutation engine
|
|
415
|
+
* yet). Populated when the brain is online and pushing mutations.
|
|
416
|
+
*/
|
|
417
|
+
type MutationApplied = {
|
|
418
|
+
id: string;
|
|
419
|
+
source: string;
|
|
420
|
+
passName: string;
|
|
421
|
+
description: string;
|
|
422
|
+
};
|
|
423
|
+
/**
|
|
424
|
+
* Target-specific wire request. Shape varies by provider — caller passes the
|
|
425
|
+
* right field to the right SDK.
|
|
426
|
+
*/
|
|
427
|
+
type CompiledRequest = {
|
|
428
|
+
provider: 'anthropic';
|
|
429
|
+
model: string;
|
|
430
|
+
system: Array<{
|
|
431
|
+
type: 'text';
|
|
432
|
+
text: string;
|
|
433
|
+
cache_control?: {
|
|
434
|
+
type: 'ephemeral';
|
|
435
|
+
};
|
|
436
|
+
}>;
|
|
437
|
+
messages: Array<{
|
|
438
|
+
role: string;
|
|
439
|
+
content: unknown;
|
|
440
|
+
}>;
|
|
441
|
+
tools?: unknown[];
|
|
442
|
+
max_tokens?: number;
|
|
443
|
+
/**
|
|
444
|
+
* alpha.29 — emitted only when the translator's wire-overrides set
|
|
445
|
+
* `parallelToolCalls = false`. Shape per Anthropic Messages API docs:
|
|
446
|
+
* `{ type: 'auto', disable_parallel_tool_use: true }`. kgauto defaults
|
|
447
|
+
* to omitting `tool_choice` entirely (Anthropic defaults to auto + parallel),
|
|
448
|
+
* so this field's presence signals an explicit override.
|
|
449
|
+
*/
|
|
450
|
+
tool_choice?: {
|
|
451
|
+
type: 'auto' | 'any' | 'tool' | 'none';
|
|
452
|
+
disable_parallel_tool_use?: boolean;
|
|
453
|
+
name?: string;
|
|
454
|
+
};
|
|
455
|
+
} | {
|
|
456
|
+
provider: 'google';
|
|
457
|
+
model: string;
|
|
458
|
+
systemInstruction?: {
|
|
459
|
+
role: 'system';
|
|
460
|
+
parts: Array<{
|
|
461
|
+
text: string;
|
|
462
|
+
}>;
|
|
463
|
+
};
|
|
464
|
+
contents: Array<{
|
|
465
|
+
role: string;
|
|
466
|
+
parts: unknown[];
|
|
467
|
+
}>;
|
|
468
|
+
tools?: unknown[];
|
|
469
|
+
generationConfig?: Record<string, unknown>;
|
|
470
|
+
cachedContent?: string;
|
|
471
|
+
} | {
|
|
472
|
+
provider: 'openai';
|
|
473
|
+
model: string;
|
|
474
|
+
messages: Array<{
|
|
475
|
+
role: string;
|
|
476
|
+
content: unknown;
|
|
477
|
+
}>;
|
|
478
|
+
tools?: unknown[];
|
|
479
|
+
response_format?: unknown;
|
|
480
|
+
reasoning_effort?: string;
|
|
481
|
+
/**
|
|
482
|
+
* alpha.29 — emitted only when the translator's wire-overrides set
|
|
483
|
+
* `parallelToolCalls = false`. OpenAI defaults parallel_tool_calls=true
|
|
484
|
+
* server-side; we explicit-set to false only when overriding.
|
|
485
|
+
*/
|
|
486
|
+
parallel_tool_calls?: boolean;
|
|
487
|
+
} | {
|
|
488
|
+
provider: 'deepseek';
|
|
489
|
+
model: string;
|
|
490
|
+
messages: Array<{
|
|
491
|
+
role: string;
|
|
492
|
+
content: unknown;
|
|
493
|
+
}>;
|
|
494
|
+
tools?: unknown[];
|
|
495
|
+
} | {
|
|
496
|
+
provider: 'zai';
|
|
497
|
+
model: string;
|
|
498
|
+
messages: Array<{
|
|
499
|
+
role: string;
|
|
500
|
+
content: unknown;
|
|
501
|
+
}>;
|
|
502
|
+
tools?: unknown[];
|
|
503
|
+
response_format?: unknown;
|
|
504
|
+
/**
|
|
505
|
+
* alpha.65 — Z.ai thinking knob (docs.z.ai chat-completion reference):
|
|
506
|
+
* `thinking.type` is 'enabled' (default) or 'disabled'. Emitted only
|
|
507
|
+
* when a cliff forces thinking off (`force_thinking_budget_zero`);
|
|
508
|
+
* omitted otherwise so the provider default applies.
|
|
509
|
+
*/
|
|
510
|
+
thinking?: {
|
|
511
|
+
type: 'enabled' | 'disabled';
|
|
512
|
+
};
|
|
513
|
+
} | {
|
|
514
|
+
provider: 'moonshot';
|
|
515
|
+
model: string;
|
|
516
|
+
messages: Array<{
|
|
517
|
+
role: string;
|
|
518
|
+
content: unknown;
|
|
519
|
+
}>;
|
|
520
|
+
tools?: unknown[];
|
|
521
|
+
response_format?: unknown;
|
|
522
|
+
};
|
|
523
|
+
/**
|
|
524
|
+
* Best-practice advisory emitted by the compiler at compile time. Non-fatal —
|
|
525
|
+
* consumers log, surface in dev tools, gate on `level === 'critical'` in CI,
|
|
526
|
+
* or ignore. The advisor inspects the IR + selected profile + diagnostics
|
|
527
|
+
* and emits one entry per detected gap.
|
|
528
|
+
*
|
|
529
|
+
* Codes are stable across releases. `suggestion` and `docsUrl` are optional
|
|
530
|
+
* but encouraged: suggestion = the actionable diff; docsUrl = the
|
|
531
|
+
* interfaces/kgauto.md anchor for context.
|
|
532
|
+
*
|
|
533
|
+
* alpha.6 Phase 1 starter rules:
|
|
534
|
+
* - `caching-off-on-claude` (warn) system >2000 chars on Anthropic, no cacheable=true
|
|
535
|
+
* - `single-chunk-system` (info) Anthropic, only one PromptSection >1000 chars
|
|
536
|
+
* - `tool-bloat` (warn) >10 tools on a short-output archetype
|
|
537
|
+
* - `history-uncached-on-claude` (warn) Anthropic, ≥2 history messages, no historyCachePolicy
|
|
538
|
+
*
|
|
539
|
+
* Phase 2 (catalog as `bestPractices` block in profiles) and Phase 3 (brain
|
|
540
|
+
* telemetry on `advisories_fired`) are alpha.7+ territory.
|
|
541
|
+
*/
|
|
542
|
+
interface BestPracticeAdvisory {
|
|
543
|
+
/**
|
|
544
|
+
* Severity. `info` = informational; `warn` = behavioral pattern that's
|
|
545
|
+
* usually expensive or wrong; `critical` = likely bug or production-grade
|
|
546
|
+
* misuse. Phase 1 ships info + warn only.
|
|
547
|
+
*/
|
|
548
|
+
level: 'info' | 'warn' | 'critical';
|
|
549
|
+
/** Stable kebab-case code. Consumers filter / gate by this. */
|
|
550
|
+
code: string;
|
|
551
|
+
/** Human-readable explanation of what was detected. */
|
|
552
|
+
message: string;
|
|
553
|
+
/** Optional: how to fix — actionable diff or pattern. */
|
|
554
|
+
suggestion?: string;
|
|
555
|
+
/** Optional: link to docs anchor for more context. */
|
|
556
|
+
docsUrl?: string;
|
|
557
|
+
/**
|
|
558
|
+
* alpha.20 — actionable category for routing/dashboard surfacing. When set,
|
|
559
|
+
* the brain persists this as `recommendation_type` on
|
|
560
|
+
* `compile_outcome_advisories` so consumers can filter "show me all
|
|
561
|
+
* client-side issues that are caching-fix recommendations." Optional;
|
|
562
|
+
* absent on legacy or uncategorized rules.
|
|
563
|
+
*
|
|
564
|
+
* - `'model-swap'` — swap to a different model fixes this
|
|
565
|
+
* - `'prompt-fix'` — restructure prompt (sections, tools, format)
|
|
566
|
+
* - `'caching-fix'` — add cache markers (system or history)
|
|
567
|
+
* - `'no-ai-needed'` — the call shouldn't be using an AI model
|
|
568
|
+
* - `'tier-down'` — current model is overkill for this archetype
|
|
569
|
+
* - `'architecture-change'` — the issue isn't fixable at the kgauto layer
|
|
570
|
+
*/
|
|
571
|
+
recommendationType?: 'model-swap' | 'prompt-fix' | 'caching-fix' | 'no-ai-needed' | 'tier-down' | 'architecture-change';
|
|
572
|
+
/**
|
|
573
|
+
* alpha.36 — architectural recommendation when the call shouldn't be an
|
|
574
|
+
* AI call at all, or should be a cheaper non-AI substitute. Optional and
|
|
575
|
+
* orthogonal to {@link recommendationType}: when set, narrows the
|
|
576
|
+
* `no-ai-needed` / `architecture-change` rec-type into an actionable
|
|
577
|
+
* pattern the consumer can implement.
|
|
578
|
+
*
|
|
579
|
+
* - `'lookup-table'` — deterministic input → output mapping (≥95% agreement at high N). Build a domain table; fall back to AI for novel inputs.
|
|
580
|
+
* - `'memoization-cache'` — same canonical input recurs across sessions with equivalent output. Add an edge/KV cache keyed on canonical input.
|
|
581
|
+
* - `'tier-down'` — cheaper model tier delivers equal-or-better oracle score on this shape. Move primary to the cheaper tier.
|
|
582
|
+
* - `'deterministic-parser'` — `constraints.structuredOutput: true` calls where the schema is regex/JSON-extractable from the input.
|
|
583
|
+
* - `'precompute'` — input is derivable at build / cron / batch time; eliminate the per-request call entirely.
|
|
584
|
+
*
|
|
585
|
+
* Operator-side detection lives in `v2/scripts/no-ai-needed-detector.mjs`
|
|
586
|
+
* (alpha.36); a compile-time advisor rule that consumes the brain-side
|
|
587
|
+
* findings cache follows in a later alpha. Today populated only by the
|
|
588
|
+
* operator-side detector + the brain RPC `get_no_ai_needed_candidates`
|
|
589
|
+
* (migration 021).
|
|
590
|
+
*/
|
|
591
|
+
recommendedArchitecture?: 'lookup-table' | 'memoization-cache' | 'tier-down' | 'deterministic-parser' | 'precompute';
|
|
592
|
+
/**
|
|
593
|
+
* alpha.28 — when a rule wants to surface a specific structural adaptation
|
|
594
|
+
* (not just a swap or a prompt fix), it attaches the adapter shape here.
|
|
595
|
+
* Shape is the canonical {@link Adapter} discriminated union defined in
|
|
596
|
+
* this module; `compatibility.ts` re-exports it so
|
|
597
|
+
* `getModelCompatibility()` and `BestPracticeAdvisory.suggestedAdaptation`
|
|
598
|
+
* share one source of truth.
|
|
599
|
+
*
|
|
600
|
+
* Today fired by `archetype-perf-floor-breach` (alpha.28) when a
|
|
601
|
+
* documented adapter exists for the chosen model's archetype cliff.
|
|
602
|
+
* Absent on rules without a structural adapter (caching-off-on-claude,
|
|
603
|
+
* tool-bloat, etc.) and on the `reject` branch of
|
|
604
|
+
* `archetype-perf-floor-breach` where no adapter would help.
|
|
605
|
+
*
|
|
606
|
+
* CLOSED discriminated union (R3 from consultation doc) — future adapter
|
|
607
|
+
* parameters extend the union in `compatibility.ts` in named alpha
|
|
608
|
+
* releases. No `| string` escape hatch; consumer code can write
|
|
609
|
+
* exhaustive `switch (suggestedAdaptation.parameter)`.
|
|
610
|
+
*
|
|
611
|
+
* Phase 2 cross-builder coherence: Builder A's
|
|
612
|
+
* `AdvisoryRecord.suggestedAdaptation` (in `glassbox-routes/types.ts`)
|
|
613
|
+
* MUST type to the same union. Phase 2 integration verifies.
|
|
614
|
+
*/
|
|
615
|
+
suggestedAdaptation?: Adapter;
|
|
616
|
+
/**
|
|
617
|
+
* alpha.42 — the kgauto request handle that produced this advisory. Stamped
|
|
618
|
+
* by `compile()` after `runAdvisor()` returns, sourced from
|
|
619
|
+
* `CompileResult.handle`. Lets a consumer's admin UI pivot from advisory
|
|
620
|
+
* row → original trace (Glass-Box card, brain `compile_outcomes` lookup).
|
|
621
|
+
*
|
|
622
|
+
* Absent when the advisory was constructed outside the full `compile()`
|
|
623
|
+
* pipeline (e.g., a direct `runAdvisor()` call in tests). Always present
|
|
624
|
+
* on advisories returned from `compile().advisories`.
|
|
625
|
+
*/
|
|
626
|
+
kgautoRequestId?: string;
|
|
627
|
+
/**
|
|
628
|
+
* alpha.42 — who is positioned to act on this advisory.
|
|
629
|
+
*
|
|
630
|
+
* - `'consumer-actionable'` (Class A): the consumer can fix this by
|
|
631
|
+
* changing their wire-up — add `cacheable: true` markers, swap their
|
|
632
|
+
* model literal, declare a section, etc. The admin UI should surface
|
|
633
|
+
* a one-click apply or a "fix it" CTA.
|
|
634
|
+
* - `'producer-owned'` (Class B): kgauto needs to ship a code or data
|
|
635
|
+
* change — update a profile, promote a model in the chain, fix a
|
|
636
|
+
* wire contract. The admin UI should surface this as informational
|
|
637
|
+
* ("kgauto is on it") and not present a consumer-side action.
|
|
638
|
+
*
|
|
639
|
+
* Stamped by `compile()` after `runAdvisor()` returns. Rules can self-
|
|
640
|
+
* declare via the matching field on their return; otherwise the stamp
|
|
641
|
+
* consults the static `PRODUCER_OWNED_RULE_CODES` set in `advisor.ts`,
|
|
642
|
+
* defaulting to `'consumer-actionable'` when the code isn't listed.
|
|
643
|
+
*
|
|
644
|
+
* Closes the s47 dogfood gap where the tt-intel admin UI conflated the
|
|
645
|
+
* two classes — consumers couldn't tell whether they should act or wait.
|
|
646
|
+
*/
|
|
647
|
+
ownership?: 'consumer-actionable' | 'producer-owned';
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* alpha.28 — adapter shape attached to advisories and returned by
|
|
651
|
+
* `getModelCompatibility()`. A CLOSED discriminated union: future adapter
|
|
652
|
+
* parameters extend it explicitly in named alpha releases. NO `| string`
|
|
653
|
+
* escape hatch — consumer policy code SHOULD write exhaustive
|
|
654
|
+
* `switch (adapter.parameter)` and rely on the compiler to flag
|
|
655
|
+
* "I added a new adapter parameter and forgot to update consumer policy."
|
|
656
|
+
*
|
|
657
|
+
* Defined here (in `ir.ts`, the foundational types module) and re-exported
|
|
658
|
+
* from `compatibility.ts` for ergonomic consumer imports. Anchoring it
|
|
659
|
+
* here avoids the import cycle that would form if both files tried to be
|
|
660
|
+
* the source of truth (ir.ts → compatibility.ts → profiles.ts → ir.ts).
|
|
661
|
+
*
|
|
662
|
+
* alpha.28 variants:
|
|
663
|
+
* - `{ parameter: 'toolOrchestration'; value: 'sequential'; consequence }`
|
|
664
|
+
* Lifts DeepSeek V4-family on `hunt` from the sequential-tool cliff
|
|
665
|
+
* (L-040). `consequence` is consumer-renderable plain English.
|
|
666
|
+
*
|
|
667
|
+
* Future alpha releases will add e.g. `parallelToolCalls`, `maxTools`,
|
|
668
|
+
* `thinkingBudget` (per tt-intel-Cairn priority list).
|
|
669
|
+
*/
|
|
670
|
+
type Adapter = {
|
|
671
|
+
parameter: 'toolOrchestration';
|
|
672
|
+
value: 'sequential';
|
|
673
|
+
consequence: string;
|
|
674
|
+
};
|
|
675
|
+
/**
|
|
676
|
+
* alpha.29+ — record of a single section rewrite fired by the translator at
|
|
677
|
+
* compile time. Surfaces on `CompileResult.sectionRewritesApplied` and (in
|
|
678
|
+
* scrubbed wire form, without original/transformed text) on
|
|
679
|
+
* `TraceDetail.sectionRewritesApplied` for Glass-Box Coaching-card rendering.
|
|
680
|
+
*
|
|
681
|
+
* `originalText` / `transformedText` stay package-internal — they may carry
|
|
682
|
+
* consumer PII. The wire-shape variant (`TraceSectionRewrite` in
|
|
683
|
+
* `glassbox-routes/types.ts`) carries only `summary` for renderer use.
|
|
684
|
+
*/
|
|
685
|
+
interface SectionRewrite {
|
|
686
|
+
/** Stable id of the `PromptSection` that was rewritten. */
|
|
687
|
+
sectionId: string;
|
|
688
|
+
/** The `kind` discriminator that matched the rewrite rule. */
|
|
689
|
+
kind: SectionKind;
|
|
690
|
+
/**
|
|
691
|
+
* Stable identifier of the rule that fired (e.g.
|
|
692
|
+
* `'sequential-tool-cliff-below-floor'`). Future rules add named ids; the
|
|
693
|
+
* brain aggregates by this value for cross-app learning.
|
|
694
|
+
*/
|
|
695
|
+
rule: string;
|
|
696
|
+
/** The section's text BEFORE the rewrite fired. */
|
|
697
|
+
originalText: string;
|
|
698
|
+
/** The text the translator emitted into the IR for this section. */
|
|
699
|
+
transformedText: string;
|
|
700
|
+
/**
|
|
701
|
+
* Wire-level overrides emitted alongside the text rewrite. Merged into
|
|
702
|
+
* `CompileResult.wireOverrides` by `applySectionRewrites`. alpha.29 ships
|
|
703
|
+
* `parallelToolCalls`; the union extends as more wire-overrides surface.
|
|
704
|
+
*/
|
|
705
|
+
wireOverrides?: {
|
|
706
|
+
parallelToolCalls?: boolean;
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
interface CompileResult {
|
|
710
|
+
/** Unique handle for this call — pass to record() to correlate the outcome. */
|
|
711
|
+
handle: string;
|
|
712
|
+
/** Selected target model id. */
|
|
713
|
+
target: string;
|
|
714
|
+
/** Selected provider. */
|
|
715
|
+
provider: Provider;
|
|
716
|
+
/** The wire request — pass the appropriate fields to your SDK. */
|
|
717
|
+
request: CompiledRequest;
|
|
718
|
+
/** Estimated tokens (input). */
|
|
719
|
+
tokensIn: number;
|
|
720
|
+
/** Estimated cost in USD (input portion). */
|
|
721
|
+
estimatedCostUsd: number;
|
|
722
|
+
/** Mutations that fired during compile (informational). */
|
|
723
|
+
mutationsApplied: MutationApplied[];
|
|
724
|
+
/** Fallback chain — try these in order if target fails. */
|
|
725
|
+
fallbackChain: string[];
|
|
726
|
+
/**
|
|
727
|
+
* Best-practice advisories emitted by the compiler. Non-fatal. Empty
|
|
728
|
+
* array when no rules fired. alpha.6 Phase 1.
|
|
729
|
+
*/
|
|
730
|
+
advisories: BestPracticeAdvisory[];
|
|
731
|
+
/**
|
|
732
|
+
* alpha.29+ — per-section rewrites applied by the translator at compile
|
|
733
|
+
* time. Empty array means no rewrites fired (or pre-alpha.29 behavior —
|
|
734
|
+
* all sections default `kind: 'arbitrary'`, which is pass-through).
|
|
735
|
+
*
|
|
736
|
+
* Surfaces to:
|
|
737
|
+
* - Glass-Box Coaching card (via `TraceDetail.sectionRewritesApplied`,
|
|
738
|
+
* scrubbed of original/transformed text)
|
|
739
|
+
* - brain `compile_outcomes.section_rewrites_applied` (migration 019)
|
|
740
|
+
* for cross-app learning
|
|
741
|
+
*/
|
|
742
|
+
sectionRewritesApplied: SectionRewrite[];
|
|
743
|
+
/**
|
|
744
|
+
* alpha.29+ — wire-level overrides emitted by translator rewrites. The
|
|
745
|
+
* provider lowering pass threads these through to the wire request before
|
|
746
|
+
* emit. Today only `parallelToolCalls: boolean`; the type extends as more
|
|
747
|
+
* wire-overrides surface.
|
|
748
|
+
*
|
|
749
|
+
* Undefined when no rewrite emitted overrides — the common case.
|
|
750
|
+
*/
|
|
751
|
+
wireOverrides?: {
|
|
752
|
+
parallelToolCalls?: boolean;
|
|
753
|
+
};
|
|
754
|
+
/** Diagnostics for caller-side logging. */
|
|
755
|
+
diagnostics: {
|
|
756
|
+
sectionsKept: number;
|
|
757
|
+
sectionsDropped: number;
|
|
758
|
+
toolsKept: number;
|
|
759
|
+
toolsDropped: number;
|
|
760
|
+
historyKept: number;
|
|
761
|
+
historyDropped: number;
|
|
762
|
+
cacheableTokens: number;
|
|
763
|
+
estimatedCacheSavingsUsd: number;
|
|
764
|
+
/**
|
|
765
|
+
* Tokens in `history` (and `currentTurn` when before the marker) that
|
|
766
|
+
* fall within the cacheable prefix per `historyCachePolicy`. Always
|
|
767
|
+
* computed; only Anthropic actually emits a wire-format marker. For
|
|
768
|
+
* Gemini / OpenAI / DeepSeek, this represents the theoretical cacheable
|
|
769
|
+
* prefix that implicit caching may pick up — useful telemetry for the
|
|
770
|
+
* brain to learn which (app, model, archetype) tuples benefit most
|
|
771
|
+
* from history caching. alpha.5.
|
|
772
|
+
*/
|
|
773
|
+
historyCacheableTokens: number;
|
|
774
|
+
/**
|
|
775
|
+
* Total tokens in input `history` (pre-compression). Computed regardless
|
|
776
|
+
* of whether `passCompressHistory` fired — surfaces how close a tuple is
|
|
777
|
+
* to its `compressHistoryAboveTokens` threshold so dashboards / cost-
|
|
778
|
+
* watchers can see the bloat axis the count-based threshold misses.
|
|
779
|
+
* 0 when history is empty. alpha.7.
|
|
780
|
+
*/
|
|
781
|
+
historyTokensTotal: number;
|
|
782
|
+
/**
|
|
783
|
+
* alpha.20 E3. Consumer-declared tool-orchestration mode for this call,
|
|
784
|
+
* mirrored from `ir.constraints.toolOrchestration` for downstream
|
|
785
|
+
* observability (Glass-Box panel, brain telemetry, advisor logs).
|
|
786
|
+
* Undefined when the consumer hadn't adopted the constraint yet —
|
|
787
|
+
* treat as 'parallel' equivalent for back-compat.
|
|
788
|
+
*/
|
|
789
|
+
toolOrchestration?: 'parallel' | 'sequential' | 'either';
|
|
790
|
+
/**
|
|
791
|
+
* alpha.33. Zero-based index into the resolved (post-compression) history
|
|
792
|
+
* array at which Anthropic prompt-cache marker should land — i.e., the
|
|
793
|
+
* last message that belongs to the stable cacheable prefix. Consumers
|
|
794
|
+
* using AI-SDK's `streamText({ messages: convertToModelMessages(...) })`
|
|
795
|
+
* lose the per-message `providerOptions.anthropic.cacheControl` markers
|
|
796
|
+
* the compiler emits on `result.request.messages`, because
|
|
797
|
+
* `convertToModelMessages` reads raw input not the lowered output.
|
|
798
|
+
* This index gives the consumer a single deterministic position to
|
|
799
|
+
* attach `cacheControl: { type: 'ephemeral' }` after their own conversion.
|
|
800
|
+
*
|
|
801
|
+
* Computation:
|
|
802
|
+
* - `historyCachePolicy.strategy === 'all-but-latest'`: history.length - 1
|
|
803
|
+
* (or undefined if history is empty)
|
|
804
|
+
* - `historyCachePolicy.strategy === 'fixed-suffix'` with `suffix: N`:
|
|
805
|
+
* history.length - 1 - N (undefined if N exceeds history length)
|
|
806
|
+
* - `historyCachePolicy.strategy === 'none'` or omitted: undefined
|
|
807
|
+
*
|
|
808
|
+
* The companion helper `attachCacheControlToStreamTextInput()` reads
|
|
809
|
+
* this field + the consumer's converted messages to perform the per-
|
|
810
|
+
* attempt mutation correctly. Filed by IC + tt-intel cross-consumer
|
|
811
|
+
* pattern 2026-05-20 (`streamText-cache-marker-propagation-gap`).
|
|
812
|
+
*/
|
|
813
|
+
historyCacheMarkIndex?: number;
|
|
814
|
+
/**
|
|
815
|
+
* alpha.33. Zero-based index into the structured `systemMessages` array
|
|
816
|
+
* (see top-level `systemMessages` field) at which the cacheable system
|
|
817
|
+
* prefix ends. Useful when the consumer is building a multi-block
|
|
818
|
+
* system parameter for Anthropic streamText — they can attach
|
|
819
|
+
* `providerOptions.anthropic.cacheControl` to `systemMessages[index]`.
|
|
820
|
+
*
|
|
821
|
+
* Undefined when no section had `cacheable: true` OR `systemMessages` is
|
|
822
|
+
* empty. Matches `historyCacheMarkIndex` semantics on the history axis.
|
|
823
|
+
*/
|
|
824
|
+
systemCacheMarkIndex?: number;
|
|
825
|
+
/**
|
|
826
|
+
* alpha.43. Cliff-style warnings surfaced by the convention pass
|
|
827
|
+
* (`passApplyConventions`) when the selected profile + archetype carry
|
|
828
|
+
* a `cliffWarning` whose preconditions are met. These are informational
|
|
829
|
+
* — they describe a structural mismatch between the call's shape and
|
|
830
|
+
* the chosen family (e.g. "reasoner family is wrong for parallel-tool
|
|
831
|
+
* hunt") so consumers can route differently next time.
|
|
832
|
+
*
|
|
833
|
+
* Empty array when no convention fired or no warnings surfaced.
|
|
834
|
+
* Separate from `cliff_guard` mutations in `mutationsApplied` — those
|
|
835
|
+
* are profile.cliffs[] runtime triggers; these are convention-level
|
|
836
|
+
* advisory text. Both can fire on the same call.
|
|
837
|
+
*/
|
|
838
|
+
cliffWarnings: string[];
|
|
839
|
+
/**
|
|
840
|
+
* alpha.68 / Release A (delegation-fanout-accelerator §5.D) — tokens added
|
|
841
|
+
* by the `discipline_contract` gate block on this call; 0 when the gate
|
|
842
|
+
* didn't fire (no eligible `discipline_contract` section, or Factor A /
|
|
843
|
+
* Factor C screened it out). Measured, never assumed: counted from the
|
|
844
|
+
* exact frozen preamble the translator prepended. Persisted to
|
|
845
|
+
* `compile_outcomes.discipline_gate_tokens` (migration 042) so the tax is a
|
|
846
|
+
* number the cost-watcher can weigh against the lift.
|
|
847
|
+
*/
|
|
848
|
+
disciplineGateTokens: number;
|
|
849
|
+
};
|
|
850
|
+
/**
|
|
851
|
+
* alpha.33. Structured `system` for AI-SDK `streamText({ system })`
|
|
852
|
+
* consumers. When the consumer's lowered request has cacheable section
|
|
853
|
+
* markers, the flat `system: string` form loses the marker assignment
|
|
854
|
+
* (every `streamText({ system: '<string>' })` call silently strips
|
|
855
|
+
* providerOptions). Pass `systemMessages` instead to `streamText({
|
|
856
|
+
* system: result.systemMessages })` so the cacheable prefix is structurally
|
|
857
|
+
* preserved.
|
|
858
|
+
*
|
|
859
|
+
* Each entry is a `SystemModelMessage`-shaped object:
|
|
860
|
+
* { role: 'system', content: string, providerOptions?: { anthropic?:
|
|
861
|
+
* { cacheControl: { type: 'ephemeral' } } } }
|
|
862
|
+
*
|
|
863
|
+
* Provider-agnostic on emit: only Anthropic actually consumes the
|
|
864
|
+
* cacheControl block; Gemini / OpenAI / DeepSeek receive the same
|
|
865
|
+
* shape but ignore the marker (matching their implicit-caching semantics).
|
|
866
|
+
*
|
|
867
|
+
* Empty array when the IR carried zero sections (rare; usually means the
|
|
868
|
+
* compiler dropped everything for the chosen intent). Consumers can fall
|
|
869
|
+
* back to `result.systemMessages.length === 0 ? '' : result.systemMessages`
|
|
870
|
+
* or use the helper `attachCacheControlToStreamTextInput()` which handles
|
|
871
|
+
* both shapes.
|
|
872
|
+
*/
|
|
873
|
+
systemMessages: SystemModelMessage[];
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* alpha.33. AI-SDK-compatible system-message shape carried on
|
|
877
|
+
* `CompileResult.systemMessages`. Matches the AI-SDK `streamText({ system })`
|
|
878
|
+
* structured form — consumers can pass these directly without conversion.
|
|
879
|
+
*
|
|
880
|
+
* The `providerOptions` field is set ONLY for entries that came from a
|
|
881
|
+
* section with `cacheable: true` AND the chosen provider is Anthropic AND
|
|
882
|
+
* the compiler computed a non-zero `cacheableTokens`. Other providers see
|
|
883
|
+
* `providerOptions: undefined` (or omitted entirely).
|
|
884
|
+
*/
|
|
885
|
+
interface SystemModelMessage {
|
|
886
|
+
role: 'system';
|
|
887
|
+
content: string;
|
|
888
|
+
providerOptions?: {
|
|
889
|
+
anthropic?: {
|
|
890
|
+
cacheControl: {
|
|
891
|
+
type: 'ephemeral';
|
|
892
|
+
};
|
|
893
|
+
};
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Token usage normalized across providers. `cached` and `cacheCreated` are
|
|
898
|
+
* Anthropic prompt-cache reads/writes (Gemini implicit caching populates
|
|
899
|
+
* `cached` from `usageMetadata.cachedContentTokenCount`; OpenAI populates
|
|
900
|
+
* from `prompt_tokens_details.cached_tokens`).
|
|
901
|
+
*/
|
|
902
|
+
interface NormalizedTokens {
|
|
903
|
+
input: number;
|
|
904
|
+
output: number;
|
|
905
|
+
total: number;
|
|
906
|
+
cached?: number;
|
|
907
|
+
cacheCreated?: number;
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Tool call in a provider-agnostic shape. Anthropic `tool_use` blocks,
|
|
911
|
+
* Google `functionCall` parts, and OpenAI/DeepSeek `tool_calls[]` all
|
|
912
|
+
* collapse to this.
|
|
913
|
+
*/
|
|
914
|
+
interface ToolCall {
|
|
915
|
+
id: string;
|
|
916
|
+
name: string;
|
|
917
|
+
args: Record<string, unknown>;
|
|
918
|
+
}
|
|
919
|
+
interface NormalizedResponse {
|
|
920
|
+
/** Main text body. Empty string if response had no text content. */
|
|
921
|
+
text: string;
|
|
922
|
+
/**
|
|
923
|
+
* Parsed structured output. Populated when ir.constraints.structuredOutput
|
|
924
|
+
* is true and JSON.parse(text) succeeds. Null otherwise.
|
|
925
|
+
*/
|
|
926
|
+
structuredOutput: unknown | null;
|
|
927
|
+
/** Tool calls in normalized shape. Empty array if none. */
|
|
928
|
+
toolCalls: ToolCall[];
|
|
929
|
+
tokens: NormalizedTokens;
|
|
930
|
+
/** Provider-specific finish reason, passed through unchanged. */
|
|
931
|
+
finishReason?: string;
|
|
932
|
+
/** Untouched provider response — escape hatch for consumers needing fields not yet normalized. */
|
|
933
|
+
raw: unknown;
|
|
934
|
+
/** Set when structuredOutput parsing was attempted and failed. */
|
|
935
|
+
parseError?: string;
|
|
936
|
+
}
|
|
937
|
+
interface ApiKeys {
|
|
938
|
+
anthropic?: string;
|
|
939
|
+
google?: string;
|
|
940
|
+
openai?: string;
|
|
941
|
+
deepseek?: string;
|
|
942
|
+
zai?: string;
|
|
943
|
+
moonshot?: string;
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* Per-provider override fields shallow-merged into the lowered request before
|
|
947
|
+
* execution. Lets consumers reach Gemini `safetySettings`, Anthropic
|
|
948
|
+
* `tool_choice`, OpenAI `seed` etc. without bypassing kgauto.
|
|
949
|
+
*/
|
|
950
|
+
interface ProviderOverrides {
|
|
951
|
+
anthropic?: Record<string, unknown>;
|
|
952
|
+
google?: Record<string, unknown>;
|
|
953
|
+
openai?: Record<string, unknown>;
|
|
954
|
+
deepseek?: Record<string, unknown>;
|
|
955
|
+
zai?: Record<string, unknown>;
|
|
956
|
+
moonshot?: Record<string, unknown>;
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Full-IR inline shadow-probe config (Shape B, Phase 1 — 2026-05-29 s51).
|
|
960
|
+
*
|
|
961
|
+
* When set on `call()`, after the primary response is served kgauto re-lowers
|
|
962
|
+
* the SAME in-memory PromptIR to each candidate and runs it, persisting a
|
|
963
|
+
* `probe_outcomes` row with `replay_source='inline-full-ir'` and
|
|
964
|
+
* `prompt_fidelity=1.0` — a fair, full-prompt measurement (vs the watchers'
|
|
965
|
+
* lossy `prompt_preview` replay). The probe NEVER blocks the user response and
|
|
966
|
+
* NEVER persists the raw prompt (system/context/payload) — only response
|
|
967
|
+
* previews + metadata, same policy the brain already holds.
|
|
968
|
+
*
|
|
969
|
+
* This is the trustworthy path that earns the right to a quality verdict;
|
|
970
|
+
* `prompt_preview` replay is retired for verdicts (see CLAUDE.md).
|
|
971
|
+
*
|
|
972
|
+
* Phase 1 enforces `sampleRate` and runs the candidate with `judge: 'off'`
|
|
973
|
+
* (responses stored for an offline batch judge). `judge: 'opus'` (inline
|
|
974
|
+
* verdict) and `maxPerDay` (per-tuple daily cap, needs a brain-count read)
|
|
975
|
+
* are accepted by the type but ENFORCED IN PHASE 2.
|
|
976
|
+
*/
|
|
977
|
+
interface ShadowProbeConfig {
|
|
978
|
+
/** Model id(s) or family alias(es) to shadow-test against the served model, on the same IR. */
|
|
979
|
+
candidates: string | string[];
|
|
980
|
+
/** Probability [0,1] that any given call fires a probe. Default 0.05. */
|
|
981
|
+
sampleRate?: number;
|
|
982
|
+
/**
|
|
983
|
+
* 'off' (Phase 1 default): run candidate + store both responses for an
|
|
984
|
+
* offline batch judge. 'opus': judge candidate-vs-served inline (Phase 2).
|
|
985
|
+
*/
|
|
986
|
+
judge?: 'opus' | 'off';
|
|
987
|
+
/** Per-(appId, archetype, candidate) daily cap. Phase 2 (needs brain-count read). Default 20. */
|
|
988
|
+
maxPerDay?: number;
|
|
989
|
+
/**
|
|
990
|
+
* alpha — latency budget (ms) for the probe leg. **Only enforced in sync mode**
|
|
991
|
+
* (`BrainConfig.sync === true`), where `call()` awaits the probe before returning
|
|
992
|
+
* the served response (PB-class Edge consumers, L-086) and the candidate's
|
|
993
|
+
* latency therefore lands on the user-facing critical path.
|
|
994
|
+
*
|
|
995
|
+
* The probe path races against this budget: if the candidate(s) don't finish
|
|
996
|
+
* within `maxLatencyMs`, the probe aborts, `call()` returns the already-served
|
|
997
|
+
* response immediately, and an `outcome='aborted_latency_budget'` row is
|
|
998
|
+
* recorded (no verdict, no false quality data). **The user is never delayed
|
|
999
|
+
* past the budget.** Default `15000`.
|
|
1000
|
+
*
|
|
1001
|
+
* No-op for fire-and-forget consumers (`sync` unset/false, and every
|
|
1002
|
+
* `probeShadow()` caller fired from `after()`/`waitUntil()`) — they don't block
|
|
1003
|
+
* the user, so there's no latency to bound. The probe runs to completion there.
|
|
1004
|
+
*
|
|
1005
|
+
* Root cause this closes (PB s57 dogfood incident, 2026-06-05): PB arms the
|
|
1006
|
+
* probe inline with `sync:true` on a `maxDuration:120` analyze route; arming a
|
|
1007
|
+
* `latency_tier='slow'` candidate (deepseek-v4-pro, ~78–84s) pushed total
|
|
1008
|
+
* handler time past the frontend's patience and the served analysis came back
|
|
1009
|
+
* empty. A sync probe had no latency budget — this field is the budget.
|
|
1010
|
+
*/
|
|
1011
|
+
maxLatencyMs?: number;
|
|
1012
|
+
/**
|
|
1013
|
+
* alpha — when true (the default) **in sync mode**, skip a candidate whose
|
|
1014
|
+
* registry `latency_tier === 'slow'` *before starting it*: a slow-tier model
|
|
1015
|
+
* (e.g. deepseek-v4-pro / deepseek-v4-flash, both `slow` = ~24s+ p50) cannot
|
|
1016
|
+
* fit a sane inline budget, so starting it just to abort it wastes a provider
|
|
1017
|
+
* call. An `outcome='skipped_slow_tier_sync'` row is recorded so the offline
|
|
1018
|
+
* rollup can see the skip (not a silent drop, not a false verdict). Fail-safe.
|
|
1019
|
+
*
|
|
1020
|
+
* **The deeper rule (slow-reasoner → offline-mode):** inline shadow-probing is
|
|
1021
|
+
* for FAST candidates only. To evaluate a slow reasoner you need the offline /
|
|
1022
|
+
* async probe path (the Phase-2 batch direction) which runs off the response
|
|
1023
|
+
* entirely — never the inline sync probe. Set this `false` only if you've moved
|
|
1024
|
+
* the budget high enough that a slow candidate genuinely fits (rare), or you're
|
|
1025
|
+
* in a non-sync consumer where this flag is a no-op anyway.
|
|
1026
|
+
*
|
|
1027
|
+
* No-op for fire-and-forget consumers (`sync` unset/false; `probeShadow()`):
|
|
1028
|
+
* there's no user latency to protect, so slow candidates run normally. Default
|
|
1029
|
+
* `true`.
|
|
1030
|
+
*/
|
|
1031
|
+
skipSlowTierInSync?: boolean;
|
|
1032
|
+
}
|
|
1033
|
+
interface CallOptions {
|
|
1034
|
+
/** Forwarded to compile(). */
|
|
1035
|
+
policy?: CompilePolicy;
|
|
1036
|
+
/**
|
|
1037
|
+
* alpha.68 / Release A (delegation-fanout-accelerator §5.0, R0 linkage).
|
|
1038
|
+
* When set, this call is a fan-out BRANCH of the parent trace: the outcome
|
|
1039
|
+
* row records `fanout_role='branch'`, `parent_handle=<this value>`, and
|
|
1040
|
+
* `trace_id` inherits the parent handle (the trace root). Absent ⇒ this is a
|
|
1041
|
+
* root (user-facing) call: `fanout_role='root'`, `trace_id=<own handle>`.
|
|
1042
|
+
* Pass the parent's `CallResult.handle` / `CompileResult.handle`.
|
|
1043
|
+
*
|
|
1044
|
+
* Release A supports 1-level linkage (root → branch); deeper trees await the
|
|
1045
|
+
* `delegate` primitive in Release D. Labelling only — NOT a routing change.
|
|
1046
|
+
*/
|
|
1047
|
+
parentHandle?: string;
|
|
1048
|
+
/**
|
|
1049
|
+
* alpha (s51 Phase 1) — full-IR inline shadow-probe. When set, kgauto
|
|
1050
|
+
* measures the candidate(s) on the same IR after serving the primary,
|
|
1051
|
+
* fire-and-forget. See {@link ShadowProbeConfig}.
|
|
1052
|
+
*/
|
|
1053
|
+
shadowProbe?: ShadowProbeConfig;
|
|
1054
|
+
toolRelevanceThreshold?: number;
|
|
1055
|
+
compressHistoryAfter?: number;
|
|
1056
|
+
/** Override API keys (defaults: process.env). */
|
|
1057
|
+
apiKeys?: ApiKeys;
|
|
1058
|
+
/** Provider-specific request fields shallow-merged into the lowered request. */
|
|
1059
|
+
providerOverrides?: ProviderOverrides;
|
|
1060
|
+
/** Override fetch (for tests). */
|
|
1061
|
+
fetchImpl?: typeof fetch;
|
|
1062
|
+
/** Disable retry/fallback walk on retryable errors. Default: enabled. */
|
|
1063
|
+
noFallback?: boolean;
|
|
1064
|
+
/**
|
|
1065
|
+
* alpha.10. Disable the silent auto-filter of unreachable models from the
|
|
1066
|
+
* fallback walk. Default: false (filter ON). Opt-out exists for tests +
|
|
1067
|
+
* the rare consumer that wants the legacy "fail at execute() with auth
|
|
1068
|
+
* error" behavior. When ON (default), models whose provider has no
|
|
1069
|
+
* resolvable API key are dropped from `targetsToTry` before the first
|
|
1070
|
+
* network call; if the chain empties entirely, throws CallError with
|
|
1071
|
+
* `lastErrorCode = 'no_reachable_models'`.
|
|
1072
|
+
*
|
|
1073
|
+
* Reachability source: `apiKeys` (this CallOptions) + `process.env` (via
|
|
1074
|
+
* `PROVIDER_ENV_KEYS`). Override env via env.ts's `ReachabilityOpts.envSource`
|
|
1075
|
+
* is not exposed here — `call()` always uses process.env. Use
|
|
1076
|
+
* `getDefaultFallbackChain({ reachability: { envSource } })` upstream
|
|
1077
|
+
* for hermetic test runs.
|
|
1078
|
+
*/
|
|
1079
|
+
noAutoFilter?: boolean;
|
|
1080
|
+
/**
|
|
1081
|
+
* alpha.34. When provided AND the chosen provider supports streaming
|
|
1082
|
+
* (`profile.streaming === true`) AND `noStream` is not set, kgauto
|
|
1083
|
+
* enables provider-native SSE streaming at the wire layer and invokes
|
|
1084
|
+
* `onChunk(delta)` once per provider stream event. `delta` is the text
|
|
1085
|
+
* since the previous `onChunk` call (NOT cumulative) — provider
|
|
1086
|
+
* stream-shape headache normalized in kgauto's lowering layer.
|
|
1087
|
+
*
|
|
1088
|
+
* `CallResult.response.text` is still populated with the full assembled
|
|
1089
|
+
* response — kgauto buffers internally regardless of the callback.
|
|
1090
|
+
* Consumers can use either the streaming side-effect OR the final
|
|
1091
|
+
* `response.text`, doesn't matter.
|
|
1092
|
+
*
|
|
1093
|
+
* Tool calls + finish reason + usage are collected from stream events
|
|
1094
|
+
* and returned in the final `CallResult` exactly as the non-streaming
|
|
1095
|
+
* path would shape them. Brain telemetry latency = time-to-stream-end.
|
|
1096
|
+
*
|
|
1097
|
+
* Chain-walk semantics: if the streaming target fails mid-stream
|
|
1098
|
+
* (network error, retryable provider error), kgauto walks to the next
|
|
1099
|
+
* fallback target and restarts streaming from its first chunk —
|
|
1100
|
+
* `onChunk` fires fresh from the new target. Consumer detects via the
|
|
1101
|
+
* post-call `CallResult.fellOverFrom`. To opt out of fallback for
|
|
1102
|
+
* streaming, set `noFallback: true` alongside `onChunk`.
|
|
1103
|
+
*
|
|
1104
|
+
* Filed by playbacksam s42 (2026-05-22) as `streaming-output-callback-
|
|
1105
|
+
* on-callresult` for ComposeDrawer SSE; perceived-latency UX win
|
|
1106
|
+
* during 10-30s draft assembly.
|
|
1107
|
+
*/
|
|
1108
|
+
onChunk?: (chunk: string) => void;
|
|
1109
|
+
/**
|
|
1110
|
+
* alpha.34. Explicit opt-out of streaming even when `onChunk` is
|
|
1111
|
+
* provided. Default: false (streaming enabled when `onChunk` is set).
|
|
1112
|
+
* Use when the consumer wants to pass `onChunk` conditionally without
|
|
1113
|
+
* branching the call site (e.g., capture chunks for instrumentation
|
|
1114
|
+
* without engaging streaming wire format).
|
|
1115
|
+
*/
|
|
1116
|
+
noStream?: boolean;
|
|
1117
|
+
/**
|
|
1118
|
+
* alpha.62 (eval spine) — golden-set capture. When the resolved sample rate
|
|
1119
|
+
* is > 0, a successful non-empty call's FULL PromptIR + served output is
|
|
1120
|
+
* stored to the brain's `kgauto_golden_irs` replay corpus (raw prompt —
|
|
1121
|
+
* explicit opt-in; see golden.ts consent posture). `sampleRate` here
|
|
1122
|
+
* overrides the `KGAUTO_GOLDEN_CAPTURE` env var; when neither is set,
|
|
1123
|
+
* capture never fires.
|
|
1124
|
+
*/
|
|
1125
|
+
goldenCapture?: GoldenCaptureOptions;
|
|
1126
|
+
/**
|
|
1127
|
+
* alpha.66 — one same-model retry on a structured-output contract violation
|
|
1128
|
+
* (`max_tokens_on_structured_output` / `structured_output_parse_failed`)
|
|
1129
|
+
* BEFORE chain-walking to a lower-ranked tier. Filed by playbacksam
|
|
1130
|
+
* 2026-07-18 (`structured-output-truncation-retry-same-model`): on PB's
|
|
1131
|
+
* summarize surface the walk destination is measured quality-inferior
|
|
1132
|
+
* (golden eval run #3), so one more attempt at the leader beats walking.
|
|
1133
|
+
*
|
|
1134
|
+
* Consent posture (matches KGAUTO_AUTO_PROMOTE / KGAUTO_GOLDEN_CAPTURE):
|
|
1135
|
+
* default OFF; `true` wins, explicit `false` beats the env, undefined
|
|
1136
|
+
* falls through to the `KGAUTO_SAME_MODEL_RETRY` env var ('1'/'true').
|
|
1137
|
+
* Default-off because the retry adds one full model round-trip of latency
|
|
1138
|
+
* in the failure class — the consumer decides whether their fallback tier
|
|
1139
|
+
* is bad enough to pay that.
|
|
1140
|
+
*
|
|
1141
|
+
* Bounds: at most ONE retry per call(), on whichever target hits the
|
|
1142
|
+
* contract violation first. Truncation-class retries additionally raise
|
|
1143
|
+
* any explicit wire output cap sitting below the profile's
|
|
1144
|
+
* `maxOutputTokens` (e.g. a terse-clamp or providerOverrides cap);
|
|
1145
|
+
* requests already at the profile ceiling re-roll unchanged.
|
|
1146
|
+
*/
|
|
1147
|
+
sameModelRetry?: boolean;
|
|
1148
|
+
}
|
|
1149
|
+
interface CallAttempt {
|
|
1150
|
+
model: string;
|
|
1151
|
+
status: 'success' | 'retryable' | 'terminal';
|
|
1152
|
+
errorCode?: string;
|
|
1153
|
+
message?: string;
|
|
1154
|
+
/**
|
|
1155
|
+
* alpha.66 — true on the attempt row that was the one-shot same-model
|
|
1156
|
+
* retry (CallOptions.sameModelRetry). The preceding row for the same model
|
|
1157
|
+
* carries the contract violation that triggered it.
|
|
1158
|
+
*/
|
|
1159
|
+
sameModelRetry?: boolean;
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Why fallback fired. Normalized for `CallResult.fallbackReason` (alpha.9).
|
|
1163
|
+
*
|
|
1164
|
+
* - `rate_limit` provider returned 429
|
|
1165
|
+
* - `provider_error` 5xx, network, or other retryable upstream issue
|
|
1166
|
+
* - `cost_cap` preflight policy.maxCostPerCallUsd rejected target
|
|
1167
|
+
* - `cliff` alpha.8 contract violation (MAX_TOKENS on
|
|
1168
|
+
* structured output, parse-failed JSON)
|
|
1169
|
+
* - `contract_violation` other compile-time-contract failures (reserved
|
|
1170
|
+
* for alpha.10+ — e.g. mid-stream policy rejects)
|
|
1171
|
+
* - `provider_auth_failed` alpha.14 — initial provider returned 401/403
|
|
1172
|
+
* (upstream key revocation, malformed-but-truthy
|
|
1173
|
+
* key, billing lapse). The chain walks to the
|
|
1174
|
+
* next non-same-provider target instead of
|
|
1175
|
+
* short-circuiting; same-provider remaining
|
|
1176
|
+
* entries skip with errorCode='auth_inferred'.
|
|
1177
|
+
*/
|
|
1178
|
+
type FallbackReason = 'rate_limit' | 'provider_error' | 'cost_cap' | 'cliff' | 'contract_violation' | 'provider_auth_failed';
|
|
1179
|
+
interface CallResult {
|
|
1180
|
+
/** Compile handle (still valid for record() if consumer wants to add oracle scores later). */
|
|
1181
|
+
handle: string;
|
|
1182
|
+
/** The model that ACTUALLY served the response (post-fallback). */
|
|
1183
|
+
actualModel: string;
|
|
1184
|
+
/** What compile() originally targeted. */
|
|
1185
|
+
requestedModel: string;
|
|
1186
|
+
provider: Provider;
|
|
1187
|
+
response: NormalizedResponse;
|
|
1188
|
+
latencyMs: number;
|
|
1189
|
+
/** Mutations that fired during compile (informational, mirrors CompileResult.mutationsApplied). */
|
|
1190
|
+
mutationsApplied: MutationApplied[];
|
|
1191
|
+
/** One entry per provider attempt — observability for retry/fallback walks. */
|
|
1192
|
+
attempts: CallAttempt[];
|
|
1193
|
+
/**
|
|
1194
|
+
* Alpha.9 normalization of fallback-walk telemetry. When the chain
|
|
1195
|
+
* succeeded on the first attempt, these collapse to:
|
|
1196
|
+
* - `servedBy === requestedModel`
|
|
1197
|
+
* - `fellOverFrom` undefined
|
|
1198
|
+
* - `fallbackReason` undefined
|
|
1199
|
+
*
|
|
1200
|
+
* When fallback fired:
|
|
1201
|
+
* - `servedBy` = `actualModel` (the model that produced the response)
|
|
1202
|
+
* - `fellOverFrom` = `requestedModel` (what the caller / compile() asked for)
|
|
1203
|
+
* - `fallbackReason` = normalized cause derived from the first
|
|
1204
|
+
* non-success attempt's `errorCode`
|
|
1205
|
+
*
|
|
1206
|
+
* Consumer UX use: show "Claude was busy; we used Pro for this answer"
|
|
1207
|
+
* when `fellOverFrom` is set (master plan §3.6).
|
|
1208
|
+
*/
|
|
1209
|
+
/** Model that actually answered. Equal to `actualModel`; kept distinct for clarity. */
|
|
1210
|
+
servedBy: string;
|
|
1211
|
+
/** Set only when fallback fired. Equal to `requestedModel` in that case. */
|
|
1212
|
+
fellOverFrom?: string;
|
|
1213
|
+
/** Set only when fallback fired. Normalized cause. */
|
|
1214
|
+
fallbackReason?: FallbackReason;
|
|
1215
|
+
/**
|
|
1216
|
+
* alpha.71 — the PRECISE `errorCode` of the attempt that drove the chain,
|
|
1217
|
+
* unnormalized. `fallbackReason` is a six-value summary and one of its
|
|
1218
|
+
* values, `'cliff'`, absorbs two failures with OPPOSITE remedies:
|
|
1219
|
+
*
|
|
1220
|
+
* - `max_tokens_on_structured_output` — the output budget was too small
|
|
1221
|
+
* for the requested shape ⇒ raise the cap / add an output-budget cliff.
|
|
1222
|
+
* - `structured_output_parse_failed` — the model emitted non-JSON ⇒ its
|
|
1223
|
+
* declared `structuredOutput` capability is wrong for this shape.
|
|
1224
|
+
*
|
|
1225
|
+
* The code was already computed in `attempts[]` and thrown away at record
|
|
1226
|
+
* time, so a surface could accumulate a dozen identical `'cliff'` rows that
|
|
1227
|
+
* still could not tell an operator which fix applies. (Found diagnosing
|
|
1228
|
+
* claude-haiku-4-5's 12-of-12 failures on playbacksam/summarize, where the
|
|
1229
|
+
* measured-failure gate correctly stopped the bleeding but the evidence
|
|
1230
|
+
* could not name the cause.)
|
|
1231
|
+
*/
|
|
1232
|
+
fellOverErrorCode?: string;
|
|
1233
|
+
/**
|
|
1234
|
+
* alpha.66 — true when the one-shot same-model retry
|
|
1235
|
+
* (CallOptions.sameModelRetry) fired during this call, regardless of
|
|
1236
|
+
* whether the retry rescued the call or the chain walked afterwards.
|
|
1237
|
+
* Cross-check `attempts[]` (the `sameModelRetry: true` row) and
|
|
1238
|
+
* `fellOverFrom` to distinguish rescued-by-retry from retried-then-walked.
|
|
1239
|
+
*/
|
|
1240
|
+
retriedSameModel?: boolean;
|
|
1241
|
+
/**
|
|
1242
|
+
* alpha.10. Models that auto-filter dropped from the fallback walk because
|
|
1243
|
+
* their provider had no reachable API key. Empty when nothing was filtered
|
|
1244
|
+
* (the common case once consumers have all the keys they need). Surfaces
|
|
1245
|
+
* silent self-heal so consumers can log/audit what happened without
|
|
1246
|
+
* defeating the "kgauto just gets" UX.
|
|
1247
|
+
*
|
|
1248
|
+
* Empty array (not undefined) when filter ran but dropped nothing —
|
|
1249
|
+
* distinguishes "filter ran cleanly" from "filter was disabled" (`undefined`
|
|
1250
|
+
* when `noAutoFilter: true`).
|
|
1251
|
+
*/
|
|
1252
|
+
unreachableFiltered?: string[];
|
|
1253
|
+
/**
|
|
1254
|
+
* alpha.16. Models that policy.blockedModels filtering dropped from the
|
|
1255
|
+
* fallback walk. Defense-in-depth at the call() boundary — compile()'s
|
|
1256
|
+
* passScoreTargets already excludes blocked entries from the initial
|
|
1257
|
+
* target + fallbackChain, but if a consumer re-shapes the chain and
|
|
1258
|
+
* threads policy through only partially, this filter catches the gap.
|
|
1259
|
+
*
|
|
1260
|
+
* Resolves TT-40 follow-on `policy-block-not-enforced-on-fallback-chain`
|
|
1261
|
+
* (2026-05-15) where mutations_applied recorded the block intent but
|
|
1262
|
+
* the call walker landed on the blocked model anyway.
|
|
1263
|
+
*
|
|
1264
|
+
* Undefined when no filter ran (no blockedModels set). Populated only
|
|
1265
|
+
* when filter ran AND dropped at least one entry — empty drops are
|
|
1266
|
+
* stored as `undefined` to keep brain telemetry quiet on the common
|
|
1267
|
+
* case.
|
|
1268
|
+
*/
|
|
1269
|
+
policyBlockedFiltered?: string[];
|
|
1270
|
+
/**
|
|
1271
|
+
* alpha.17. Unique identifier for this call() invocation, generated at
|
|
1272
|
+
* call() entry via crypto.randomUUID(). Returned on success and emitted
|
|
1273
|
+
* as the routing key for Glass-Box observability events
|
|
1274
|
+
* (compile.start, compile.done, execute.attempt, execute.success,
|
|
1275
|
+
* fallback.walked, advisory.fired). Pass the same id to
|
|
1276
|
+
* `subscribe(traceId)` from `@warmdrift/kgauto-compiler/glassbox` to
|
|
1277
|
+
* tap the in-flight event stream.
|
|
1278
|
+
*
|
|
1279
|
+
* Always present on success. Additive, non-breaking.
|
|
1280
|
+
*/
|
|
1281
|
+
traceId: string;
|
|
1282
|
+
/**
|
|
1283
|
+
* alpha.44. Best-practice advisories emitted by the compile that actually
|
|
1284
|
+
* served the response (mirrors `CompileResult.advisories`; uses the SERVED
|
|
1285
|
+
* compile on fallback, same as `mutationsApplied`). Empty array when no
|
|
1286
|
+
* rules fired.
|
|
1287
|
+
*
|
|
1288
|
+
* Closes IC's `callForAgent` side-finding (2026-05-26): call() consumers
|
|
1289
|
+
* (agent paths — sweep / dyad / session-title) previously had no way to
|
|
1290
|
+
* `logAdvisories('[kgauto-v2 ...]', advisories)` like compile() consumers
|
|
1291
|
+
* (chat / intake) do, because the advisories were consumed internally
|
|
1292
|
+
* (trace events, brain record) but never returned. Additive, non-breaking.
|
|
1293
|
+
*/
|
|
1294
|
+
advisories: BestPracticeAdvisory[];
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Thrown when call() exhausts the fallback chain without success.
|
|
1298
|
+
* `attempts` carries every model tried + classification.
|
|
1299
|
+
*/
|
|
1300
|
+
declare class CallError extends Error {
|
|
1301
|
+
readonly attempts: CallAttempt[];
|
|
1302
|
+
readonly lastErrorCode?: string;
|
|
1303
|
+
readonly lastStatus?: number;
|
|
1304
|
+
constructor(message: string, attempts: CallAttempt[], lastStatus?: number, lastErrorCode?: string);
|
|
1305
|
+
}
|
|
1306
|
+
interface OracleScore {
|
|
1307
|
+
/** 0..1 overall quality. */
|
|
1308
|
+
score: number;
|
|
1309
|
+
/** Optional per-dimension breakdown. */
|
|
1310
|
+
dimensions?: Record<string, number>;
|
|
1311
|
+
/** Free-form explanation for debugging. */
|
|
1312
|
+
rationale?: string;
|
|
1313
|
+
}
|
|
1314
|
+
interface RecordInput {
|
|
1315
|
+
/** Handle from CompileResult. */
|
|
1316
|
+
handle: string;
|
|
1317
|
+
/** Actual tokens consumed (post-call). */
|
|
1318
|
+
tokensIn: number;
|
|
1319
|
+
tokensOut: number;
|
|
1320
|
+
/** Wall-clock latency in ms. */
|
|
1321
|
+
latencyMs: number;
|
|
1322
|
+
/** True iff the call returned a usable response. */
|
|
1323
|
+
success: boolean;
|
|
1324
|
+
/** True iff the call returned 0 output tokens despite success. */
|
|
1325
|
+
emptyResponse?: boolean;
|
|
1326
|
+
/** Provider error code if any. */
|
|
1327
|
+
errorType?: string;
|
|
1328
|
+
/** Tools actually invoked by the model. */
|
|
1329
|
+
toolsCalled?: string[];
|
|
1330
|
+
/** Oracle quality score — required for learning to fire. */
|
|
1331
|
+
oracleScore?: OracleScore;
|
|
1332
|
+
/** Optional: scrubbed prompt/response previews for debugging. */
|
|
1333
|
+
promptPreview?: string;
|
|
1334
|
+
responsePreview?: string;
|
|
1335
|
+
/**
|
|
1336
|
+
* The model that ACTUALLY RAN. Set this when consumer-side fallback ran
|
|
1337
|
+
* a different model than v2 compile() targeted. Brain stores this as
|
|
1338
|
+
* `model` (the truth) and the original target as `requested_model`.
|
|
1339
|
+
*
|
|
1340
|
+
* Omit when no fallback occurred — brain stores compile target as `model`
|
|
1341
|
+
* (still the truth in that case) and `requested_model` stays NULL.
|
|
1342
|
+
*
|
|
1343
|
+
* s11 fix: prevents the brain from misattributing fallback traffic to
|
|
1344
|
+
* the originally-requested model.
|
|
1345
|
+
*/
|
|
1346
|
+
actualModel?: string;
|
|
1347
|
+
/**
|
|
1348
|
+
* Override `mutations_applied` for this outcome. Set by `call()` when
|
|
1349
|
+
* fallback fires — the served compile's mutations (which actually shaped
|
|
1350
|
+
* the request that went on the wire) replace the initial compile's
|
|
1351
|
+
* mutations (registered against the handle). Without this override, fallback
|
|
1352
|
+
* traffic is attributed to the initial compile's mutations and the brain's
|
|
1353
|
+
* mutation effectiveness stats become misleading.
|
|
1354
|
+
*
|
|
1355
|
+
* alpha.4: extends s11 truth-in-logging to mutations.
|
|
1356
|
+
*/
|
|
1357
|
+
mutationsApplied?: string[];
|
|
1358
|
+
/**
|
|
1359
|
+
* Cache read input tokens, when supported by the provider.
|
|
1360
|
+
* - Anthropic: `usage.cache_read_input_tokens`
|
|
1361
|
+
* - Google (implicit caching): `usageMetadata.cachedContentTokenCount`
|
|
1362
|
+
* - OpenAI: `usage.prompt_tokens_details.cached_tokens`
|
|
1363
|
+
*
|
|
1364
|
+
* Powers the cost-and-efficiency-watcher (interfaces/kgauto.md, alpha.4):
|
|
1365
|
+
* `tokens_in - cache_read_input_tokens` is the un-cached new context per call.
|
|
1366
|
+
*/
|
|
1367
|
+
cacheReadInputTokens?: number;
|
|
1368
|
+
/**
|
|
1369
|
+
* Cache creation input tokens (Anthropic-specific).
|
|
1370
|
+
* `usage.cache_creation_input_tokens`. The first call that pays the 25%
|
|
1371
|
+
* upcharge to write a cache marker; subsequent calls hit `cacheRead`.
|
|
1372
|
+
*/
|
|
1373
|
+
cacheCreationInputTokens?: number;
|
|
1374
|
+
/**
|
|
1375
|
+
* Time to first token (ms). Optional; populated when the provider/SDK
|
|
1376
|
+
* surfaces it. Distinct from `latencyMs` (end-to-end wall clock).
|
|
1377
|
+
*/
|
|
1378
|
+
ttftMs?: number;
|
|
1379
|
+
/**
|
|
1380
|
+
* alpha.57 (data-first) — the reasoning-effort tier this call actually ran
|
|
1381
|
+
* at. Overrides the compile-declared `constraints.effort` when both are
|
|
1382
|
+
* present (input wins — same precedence as `mutationsApplied`/`advisories`).
|
|
1383
|
+
* When omitted, record() auto-enriches from the registry-cached compile
|
|
1384
|
+
* declaration. Undefined end-to-end → the `effort` key is absent from the
|
|
1385
|
+
* outcome payload entirely (safe against pre-migration-033 brains).
|
|
1386
|
+
*/
|
|
1387
|
+
effort?: EffortLevel;
|
|
1388
|
+
/**
|
|
1389
|
+
* alpha.20 — advisories fired at compile() time. Persisted to the brain's
|
|
1390
|
+
* `compile_outcome_advisories` sibling table via a second POST that fires
|
|
1391
|
+
* AFTER the primary outcome insert succeeds. Best-effort: a failed
|
|
1392
|
+
* advisory POST is logged via onError but does NOT throw or roll back the
|
|
1393
|
+
* primary outcome row.
|
|
1394
|
+
*
|
|
1395
|
+
* Pass `result.advisories` from the CompileResult directly. The brain
|
|
1396
|
+
* uses these to compute the `empty_rate_clean` comparator (rows with
|
|
1397
|
+
* zero advisories fired) so consumers can distinguish "model is bad"
|
|
1398
|
+
* from "client sent a bloated/uncached/malformed request."
|
|
1399
|
+
*
|
|
1400
|
+
* Empty array / undefined → no second POST fires.
|
|
1401
|
+
*/
|
|
1402
|
+
advisories?: BestPracticeAdvisory[];
|
|
1403
|
+
/**
|
|
1404
|
+
* alpha.28 — Glass-Box renderer substrate fields (migration 018).
|
|
1405
|
+
*
|
|
1406
|
+
* All optional. When omitted, brain stores NULL and the renderer falls
|
|
1407
|
+
* back to "—" / hidden rows. Library callers (`call.ts`) populate what
|
|
1408
|
+
* they observe; adapter / SDK consumers can populate the rest from their
|
|
1409
|
+
* own provider response surface.
|
|
1410
|
+
*/
|
|
1411
|
+
/**
|
|
1412
|
+
* Provider finish reason. Captured from NormalizedResponse.finishReason
|
|
1413
|
+
* (Anthropic `stop_reason`, Google `finishReason`, OpenAI `finish_reason`).
|
|
1414
|
+
* Lower-case canonicalization is the brain's job; consumers can pass
|
|
1415
|
+
* raw provider strings.
|
|
1416
|
+
*/
|
|
1417
|
+
finishReason?: string;
|
|
1418
|
+
/**
|
|
1419
|
+
* End-to-end wall-clock latency in ms. Distinct from `latencyMs` only
|
|
1420
|
+
* insofar as `latencyMs` was the historical name for the same metric;
|
|
1421
|
+
* `totalMs` is the new column on `compile_outcomes` (migration 018).
|
|
1422
|
+
* When omitted, brain mirrors `latency_ms`.
|
|
1423
|
+
*/
|
|
1424
|
+
totalMs?: number;
|
|
1425
|
+
/** Tools kept after the tool-relevance pass. */
|
|
1426
|
+
toolsCount?: number;
|
|
1427
|
+
/** Number of history messages at compile time. */
|
|
1428
|
+
historyDepth?: number;
|
|
1429
|
+
/** Rendered system prompt size in characters. */
|
|
1430
|
+
systemPromptChars?: number;
|
|
1431
|
+
/** Model originally targeted when a fallback fired. */
|
|
1432
|
+
fellOverFrom?: string;
|
|
1433
|
+
/**
|
|
1434
|
+
* Why the fallback fired. Closed set mirroring CallResult.fallbackReason —
|
|
1435
|
+
* keep in sync with the wire-contract enum (TraceDetail.fallbackReason).
|
|
1436
|
+
*/
|
|
1437
|
+
fallbackReason?: 'rate_limit' | 'provider_auth_failed' | 'provider_error' | 'cliff' | 'cost_cap' | 'contract_violation';
|
|
1438
|
+
/**
|
|
1439
|
+
* alpha.66 — true when the one-shot same-model retry fired during this
|
|
1440
|
+
* call (migration 039 `retried_same_model`). Powers the offline rollup:
|
|
1441
|
+
* retry rate on the cliff class, and — joined with `fell_over_from` —
|
|
1442
|
+
* how often the retry rescued the call vs the chain walking anyway.
|
|
1443
|
+
*/
|
|
1444
|
+
retriedSameModel?: boolean;
|
|
1445
|
+
/**
|
|
1446
|
+
* alpha.71 — the precise `errorCode` of the attempt that drove the chain
|
|
1447
|
+
* (migration 044). Set automatically by `call()`; a consumer driving
|
|
1448
|
+
* `record()` by hand may pass it. See `CallResult.fellOverErrorCode` for
|
|
1449
|
+
* why the normalized `fallbackReason` is insufficient on its own.
|
|
1450
|
+
*/
|
|
1451
|
+
fellOverErrorCode?: string;
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* alpha.20 Entry 4: kinds of consumer-declared outcomes feeding the quality
|
|
1455
|
+
* loop. Surfaces in `recordOutcome()` as the verdict the consumer's UX is
|
|
1456
|
+
* forwarding to the brain.
|
|
1457
|
+
*
|
|
1458
|
+
* - `approved` user explicitly approved (thumbs up, "looks good", accepted)
|
|
1459
|
+
* - `rejected` user explicitly rejected (thumbs down, "redo", discarded)
|
|
1460
|
+
* - `partial` accepted with edits or partial use (mixed signal)
|
|
1461
|
+
* - `engaged` user engaged with the output (copy/scroll/dwell)
|
|
1462
|
+
* - `abandoned` user abandoned the response (closed, navigated away)
|
|
1463
|
+
* - `unknown` verdict could not be inferred — recorded for completeness
|
|
1464
|
+
*/
|
|
1465
|
+
type OutcomeKind = 'approved' | 'rejected' | 'partial' | 'engaged' | 'abandoned' | 'unknown';
|
|
1466
|
+
/**
|
|
1467
|
+
* Input to `recordOutcome()` — consumer's verdict on a previously-compiled
|
|
1468
|
+
* call. Joins to the original `compile_outcomes` row via outcomeId,
|
|
1469
|
+
* enabling per-(model, archetype) approve-rate measurement once N ≥ 10
|
|
1470
|
+
* outcomes accumulate.
|
|
1471
|
+
*/
|
|
1472
|
+
interface RecordOutcomeInput {
|
|
1473
|
+
/**
|
|
1474
|
+
* Joins to compile_outcomes.id. Pass the compile `handle` string
|
|
1475
|
+
* (CompileResult.handle / CallResult.handle) — consumer proxies resolve
|
|
1476
|
+
* handle → compile_outcomes.id via the `compile_outcomes.handle` column
|
|
1477
|
+
* (reference implementations: PB `api/kgauto-v2/compile_outcome_quality.js`,
|
|
1478
|
+
* tt-intel/IC `app/api/kgauto/v2/compile_outcome_quality/route.ts`). A
|
|
1479
|
+
* numeric compile_outcomes.id also works if the caller already has it.
|
|
1480
|
+
* (There is no `CompileResult.outcomeId` field — the brain assigns the row
|
|
1481
|
+
* id at insert; `handle` is the consumer-visible correlator.)
|
|
1482
|
+
*/
|
|
1483
|
+
outcomeId: number | string;
|
|
1484
|
+
/** What did the user / system do with this output? */
|
|
1485
|
+
outcome: OutcomeKind;
|
|
1486
|
+
/** Optional 1-5 user rating (e.g., thumbs up/down with intensity, NPS-style). */
|
|
1487
|
+
rating?: 1 | 2 | 3 | 4 | 5;
|
|
1488
|
+
/** Optional free-text reason (e.g., user-typed feedback, system-inferred cause). */
|
|
1489
|
+
reason?: string;
|
|
1490
|
+
/**
|
|
1491
|
+
* Optional model-reported confidence at compile time (0..1). Used for
|
|
1492
|
+
* Brier-score calibration in later phases (alpha.21+) — pair this with
|
|
1493
|
+
* the actual `outcome` to compute calibration error.
|
|
1494
|
+
*/
|
|
1495
|
+
observedConfidence?: number;
|
|
1496
|
+
}
|
|
1497
|
+
/**
|
|
1498
|
+
* Return shape of `recordOutcome()`. Never throws — persistence failures
|
|
1499
|
+
* surface as `ok: false` with a stable `reason` string.
|
|
1500
|
+
*/
|
|
1501
|
+
interface OutcomeResult {
|
|
1502
|
+
/**
|
|
1503
|
+
* `true` — the POST was acknowledged 2xx (sync mode only).
|
|
1504
|
+
* `'queued'` (alpha.61) — fire-and-forget mode: the write was handed to the
|
|
1505
|
+
* runtime but NOT acknowledged; delivery is unknown at return time. Read
|
|
1506
|
+
* `brainHealth()` or set `BrainConfig.sync` for delivery evidence.
|
|
1507
|
+
* `false` — brain not configured, or the POST failed (sync mode).
|
|
1508
|
+
* Both truthy values pass `if (result.ok)`; pre-alpha.61 code that treated
|
|
1509
|
+
* the fire-and-forget `true` as an ack was reading a false signal.
|
|
1510
|
+
*/
|
|
1511
|
+
ok: boolean | 'queued';
|
|
1512
|
+
/** Stable reason code when ok=false. One of: 'brain_not_configured' | 'persistence_failed'. */
|
|
1513
|
+
reason?: string;
|
|
1514
|
+
}
|
|
1515
|
+
/**
|
|
1516
|
+
* alpha.21 (s78 Entry 1): provenance label on a chain entry. Surfaces WHY
|
|
1517
|
+
* an entry sits where it sits so consumers can distinguish:
|
|
1518
|
+
*
|
|
1519
|
+
* - 'measured' brain has N>=10 rows with a measurable quality
|
|
1520
|
+
* outcome backing this placement. The number lives on
|
|
1521
|
+
* `ChainEntry.n`.
|
|
1522
|
+
* - 'capability-fact' inclusion or exclusion driven by a published or
|
|
1523
|
+
* measured CAPABILITY (L-040 cliff, ctx window cap,
|
|
1524
|
+
* structured-output support). Not an opinion — a
|
|
1525
|
+
* fact about what the model can/can't do.
|
|
1526
|
+
* - 'judgment' engineer's pick, no measured backing yet. Cold-start
|
|
1527
|
+
* prior; entirely valid until evidence accumulates.
|
|
1528
|
+
*
|
|
1529
|
+
* "Judgment" is HONEST, not a downgrade. Most of `STARTER_CHAINS` lands here
|
|
1530
|
+
* in alpha.21 — that's the point: consumers can SEE the grounding gap and
|
|
1531
|
+
* prioritize the measurement work that would graduate them to 'measured'.
|
|
1532
|
+
*/
|
|
1533
|
+
type Grounding = 'measured' | 'capability-fact' | 'judgment';
|
|
1534
|
+
/**
|
|
1535
|
+
* alpha.21 (s78 Entry 1): a single position in a fallback chain, carrying its
|
|
1536
|
+
* provenance label and an optional human-readable reason. The shape replaces
|
|
1537
|
+
* the old `string[]` representation everywhere chains are surfaced externally.
|
|
1538
|
+
*
|
|
1539
|
+
* `n` is REQUIRED when `grounding === 'measured'` — the runtime helper
|
|
1540
|
+
* `makeMeasuredEntry()` enforces this. For 'capability-fact' and 'judgment'
|
|
1541
|
+
* entries, `n` is undefined.
|
|
1542
|
+
*/
|
|
1543
|
+
interface ChainEntry {
|
|
1544
|
+
/** Canonical model id (post-alias). */
|
|
1545
|
+
id: string;
|
|
1546
|
+
/** Why this entry sits in this position. */
|
|
1547
|
+
grounding: Grounding;
|
|
1548
|
+
/**
|
|
1549
|
+
* Optional one-liner explaining the grounding decision. The inline comments
|
|
1550
|
+
* that historically lived next to STARTER_CHAINS entries are now expressed
|
|
1551
|
+
* here as machine-readable text.
|
|
1552
|
+
*/
|
|
1553
|
+
reason?: string;
|
|
1554
|
+
/**
|
|
1555
|
+
* When `grounding === 'measured'`, the brain row count that backs this
|
|
1556
|
+
* placement. Undefined for 'capability-fact' and 'judgment' entries.
|
|
1557
|
+
*/
|
|
1558
|
+
n?: number;
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* alpha.21 introspection shape — a per-archetype chain with grounding on
|
|
1562
|
+
* every position. Consumers reading this never see naked string ids;
|
|
1563
|
+
* everything carries provenance.
|
|
1564
|
+
*/
|
|
1565
|
+
interface ChainWithGrounding {
|
|
1566
|
+
archetype: IntentArchetypeName;
|
|
1567
|
+
/** Ordered: position 0 = primary, rising index = fallback positions. */
|
|
1568
|
+
entries: ChainEntry[];
|
|
1569
|
+
}
|
|
1570
|
+
/** alpha.23 (s78 Phase 3): per-axis metrics returned by the brain RPC. */
|
|
1571
|
+
interface PerAxisMetrics {
|
|
1572
|
+
appId: string;
|
|
1573
|
+
archetype: string;
|
|
1574
|
+
model: string;
|
|
1575
|
+
windowDays: number;
|
|
1576
|
+
/** Total brain rows for this tuple in the window. */
|
|
1577
|
+
nRows: number;
|
|
1578
|
+
/** Subset of nRows with zero advisories fired — the "clean signal" comparator. */
|
|
1579
|
+
nRowsClean: number;
|
|
1580
|
+
/** Count of compile_outcome_quality entries joining to this tuple's outcomes. */
|
|
1581
|
+
nQualityOutcomes: number;
|
|
1582
|
+
/** Approve rate from quality outcomes. null when nQualityOutcomes === 0. */
|
|
1583
|
+
magicRate: number | null;
|
|
1584
|
+
/** Whether magicRate >= consumer-declared qualityFloor. null when no floor declared OR no outcomes. */
|
|
1585
|
+
qualityFloorMet: boolean | null;
|
|
1586
|
+
costEfficiency: {
|
|
1587
|
+
avgCostUsd: number | null;
|
|
1588
|
+
avgCostUsdClean: number | null;
|
|
1589
|
+
avgInputTokens: number | null;
|
|
1590
|
+
avgOutputTokens: number | null;
|
|
1591
|
+
inputTokenRatio: number | null;
|
|
1592
|
+
};
|
|
1593
|
+
timeEfficiency: {
|
|
1594
|
+
avgLatencyMs: number | null;
|
|
1595
|
+
avgTtftMs: number | null;
|
|
1596
|
+
};
|
|
1597
|
+
reliability: {
|
|
1598
|
+
successRate: number | null;
|
|
1599
|
+
successRateClean: number | null;
|
|
1600
|
+
emptyRate: number | null;
|
|
1601
|
+
emptyRateClean: number | null;
|
|
1602
|
+
};
|
|
1603
|
+
evidenceFreshnessDays: number | null;
|
|
1604
|
+
}
|
|
1605
|
+
/** Per-axis metrics keyed by model — used for chain-comparison views. */
|
|
1606
|
+
type PerAxisMetricsByModel = Record<string, PerAxisMetrics>;
|
|
1607
|
+
|
|
1608
|
+
export { type ApiKeys as A, type BestPracticeAdvisory as B, type CompilePolicy as C, captureGoldenIr as D, type EffortLevel as E, type FallbackReason as F, type Grounding as G, type HistoryCachePolicy as H, type IntentDeclaration as I, parseGoldenCaptureRate as J, resolveGoldenCaptureRate as K, shouldCaptureGolden as L, type Message as M, type NormalizedResponse as N, type OutcomeResult as O, type ProviderOverrides as P, type RecordInput as R, type SystemModelMessage as S, type ToolCall as T, type CompiledRequest as a, type PromptIR as b, type CallOptions as c, type CallResult as d, type CompileResult as e, type SectionRewrite as f, type RecordOutcomeInput as g, type OracleScore as h, type Adapter as i, type PerAxisMetrics as j, type Provider as k, type ChainEntry as l, type CallAttempt as m, CallError as n, type ChainModelEntry as o, type ChainWithGrounding as p, type Constraints as q, type GoldenCaptureOptions as r, type MutationApplied as s, type NormalizedTokens as t, type OutcomeKind as u, type PerAxisMetricsByModel as v, type PromptSection as w, type SectionKind as x, type ShadowProbeConfig as y, type ToolDefinition as z };
|