@warmdrift/kgauto-compiler 2.0.0-alpha.7 → 2.0.0-alpha.70

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.
Files changed (55) hide show
  1. package/README.md +176 -46
  2. package/dist/brain-proxy.d.mts +113 -0
  3. package/dist/brain-proxy.d.ts +113 -0
  4. package/dist/brain-proxy.js +193 -0
  5. package/dist/brain-proxy.mjs +6 -0
  6. package/dist/chunk-4UO4CCSP.mjs +1620 -0
  7. package/dist/chunk-65ZMX5OT.mjs +169 -0
  8. package/dist/{chunk-5TI6PNSK.mjs → chunk-BVEXV5KC.mjs} +11 -0
  9. package/dist/chunk-FR4DNGLW.mjs +203 -0
  10. package/dist/chunk-NBO4R5PC.mjs +313 -0
  11. package/dist/chunk-P3TOAEG4.mjs +56 -0
  12. package/dist/chunk-RO22VFIF.mjs +29 -0
  13. package/dist/chunk-SBFSYCQG.mjs +719 -0
  14. package/dist/dialect.d.mts +41 -3
  15. package/dist/dialect.d.ts +41 -3
  16. package/dist/dialect.js +14 -2
  17. package/dist/dialect.mjs +5 -3
  18. package/dist/glassbox/index.d.mts +59 -0
  19. package/dist/glassbox/index.d.ts +59 -0
  20. package/dist/glassbox/index.js +312 -0
  21. package/dist/glassbox/index.mjs +12 -0
  22. package/dist/glassbox-routes/format.d.mts +24 -0
  23. package/dist/glassbox-routes/format.d.ts +24 -0
  24. package/dist/glassbox-routes/format.js +86 -0
  25. package/dist/glassbox-routes/format.mjs +18 -0
  26. package/dist/glassbox-routes/index.d.mts +191 -0
  27. package/dist/glassbox-routes/index.d.ts +191 -0
  28. package/dist/glassbox-routes/index.js +2888 -0
  29. package/dist/glassbox-routes/index.mjs +667 -0
  30. package/dist/glassbox-routes/react/index.d.mts +74 -0
  31. package/dist/glassbox-routes/react/index.d.ts +74 -0
  32. package/dist/glassbox-routes/react/index.js +819 -0
  33. package/dist/glassbox-routes/react/index.mjs +754 -0
  34. package/dist/index.d.mts +2739 -17
  35. package/dist/index.d.ts +2739 -17
  36. package/dist/index.js +8989 -1659
  37. package/dist/index.mjs +5078 -367
  38. package/dist/ir-Bqn1RVdV.d.mts +1583 -0
  39. package/dist/ir-Bvlkw5ja.d.ts +1583 -0
  40. package/dist/key-health.d.mts +131 -0
  41. package/dist/key-health.d.ts +131 -0
  42. package/dist/key-health.js +228 -0
  43. package/dist/key-health.mjs +6 -0
  44. package/dist/profiles.d.mts +292 -2
  45. package/dist/profiles.d.ts +292 -2
  46. package/dist/profiles.js +1231 -16
  47. package/dist/profiles.mjs +9 -1
  48. package/dist/types-Bon96eyc.d.ts +131 -0
  49. package/dist/types-CwGhacGT.d.mts +149 -0
  50. package/dist/types-DIxRZ3Dj.d.ts +149 -0
  51. package/dist/types-DsEq35WY.d.mts +131 -0
  52. package/package.json +54 -8
  53. package/dist/chunk-MBEI5UOM.mjs +0 -409
  54. package/dist/profiles-B3eNQ2py.d.ts +0 -619
  55. package/dist/profiles-Py8c7zjJ.d.mts +0 -619
@@ -0,0 +1,2888 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/glassbox-routes/index.ts
21
+ var glassbox_routes_exports = {};
22
+ __export(glassbox_routes_exports, {
23
+ COUNTERFACTUAL_MAX_RESULTS: () => COUNTERFACTUAL_MAX_RESULTS,
24
+ COUNTERFACTUAL_MIN_SAVINGS_RATIO: () => COUNTERFACTUAL_MIN_SAVINGS_RATIO,
25
+ computeCounterfactuals: () => computeCounterfactuals,
26
+ createGlassboxRoutes: () => createGlassboxRoutes,
27
+ rowToDetail: () => rowToDetail,
28
+ rowToSummary: () => rowToSummary
29
+ });
30
+ module.exports = __toCommonJS(glassbox_routes_exports);
31
+
32
+ // src/glassbox-routes/auth.ts
33
+ var JSON_HEADERS = { "Content-Type": "application/json" };
34
+ function jsonError(status, code) {
35
+ return new Response(JSON.stringify({ error: code }), {
36
+ status,
37
+ headers: JSON_HEADERS
38
+ });
39
+ }
40
+ function tokensEqual(a, b) {
41
+ if (a.length !== b.length) return false;
42
+ let mismatch = 0;
43
+ for (let i = 0; i < a.length; i++) {
44
+ mismatch |= a.charCodeAt(i) ^ b.charCodeAt(i);
45
+ }
46
+ return mismatch === 0;
47
+ }
48
+ function checkAuth(req, config) {
49
+ const authHeader = req.headers.get("Authorization") ?? "";
50
+ const match = /^Bearer\s+(.+)$/i.exec(authHeader);
51
+ const provided = match?.[1]?.trim() ?? "";
52
+ if (!provided || !tokensEqual(provided, config.installToken)) {
53
+ return jsonError(401, "unauthorized");
54
+ }
55
+ const origin = req.headers.get("Origin") ?? "";
56
+ const xExtId = req.headers.get("X-Glassbox-Extension-Id") ?? "";
57
+ const expectedOrigin = `chrome-extension://${config.extensionId}`;
58
+ const originOk = origin === expectedOrigin;
59
+ const xExtOk = xExtId.length > 0 && tokensEqual(xExtId, config.extensionId);
60
+ if (!originOk && !xExtOk) {
61
+ return jsonError(403, "forbidden_origin");
62
+ }
63
+ return null;
64
+ }
65
+
66
+ // src/profiles.ts
67
+ var ANTHROPIC_LOWERING_BASE = {
68
+ system: { mode: "inline" },
69
+ cache: {
70
+ strategy: "cache_control",
71
+ minTokens: 1024,
72
+ discount: 0.1,
73
+ ttlSeconds: 300
74
+ },
75
+ tools: { format: "anthropic" }
76
+ };
77
+ var GOOGLE_LOWERING_BASE = {
78
+ system: { mode: "separate", field: "systemInstruction" },
79
+ cache: {
80
+ strategy: "cachedContent",
81
+ minTokens: 4096,
82
+ discount: 0.25,
83
+ ttlSeconds: 3600
84
+ },
85
+ tools: { format: "google" }
86
+ };
87
+ var PROFILES_RAW = [
88
+ // ── Anthropic ──
89
+ {
90
+ // VERIFIED 2026-06-21 against the claude-api reference. Claude Fable 5 is
91
+ // Anthropic's most capable model — a NEW tier ABOVE the opus/sonnet/haiku
92
+ // line (the id has no MAJOR-MINOR version, just `-5`) at 2× Opus pricing
93
+ // ($10/$50 vs $5/$25). Hand-authored, not cloned: the onboarder regex can't
94
+ // match this shape (new family + single-segment version), AND a clone would
95
+ // carry the wrong (opus) pricing. status:'preview' — no brain evidence yet;
96
+ // it earns per-archetype placement through the machinery like any model, and
97
+ // at 2× Opus the quality-floor + cost gates do real work before it leads.
98
+ // API quirk (claude-api ref): an explicit `thinking:{type:"disabled"}` 400s
99
+ // on Fable 5 (omit instead) — MOOT here: ANTHROPIC_LOWERING_BASE carries no
100
+ // `thinking` field, so kgauto never sends the param. (L-049/L-081.)
101
+ id: "claude-fable-5",
102
+ verifiedAgainstDocs: "2026-06-21",
103
+ provider: "anthropic",
104
+ status: "preview",
105
+ maxContextTokens: 1e6,
106
+ maxOutputTokens: 128e3,
107
+ maxTools: 64,
108
+ parallelToolCalls: true,
109
+ structuredOutput: "grammar",
110
+ systemPromptMode: "inline",
111
+ streaming: true,
112
+ cliffs: [],
113
+ costInputPer1m: 10,
114
+ costOutputPer1m: 50,
115
+ lowering: ANTHROPIC_LOWERING_BASE,
116
+ recovery: [
117
+ {
118
+ signal: "rate_limit",
119
+ action: "escalate",
120
+ reason: "429 from Anthropic \u2014 escalate to fallback chain"
121
+ },
122
+ {
123
+ signal: "model_not_found",
124
+ action: "escalate",
125
+ reason: "Model deprecated/renamed \u2014 escalate (L-061)"
126
+ }
127
+ ],
128
+ strengths: ["reasoning", "agentic_coding", "long_context", "reliable_tool_use", "structured_output"],
129
+ weaknesses: ["cost", "latency"],
130
+ notes: "Most capable Anthropic model (2026-06); a new tier ABOVE Opus at 2\xD7 pricing ($10/$50). 1M ctx, 128k out. Verified 2026-06-21 against the claude-api reference. status:preview \u2014 no brain evidence yet; earns placement via the machinery (cost + quality-floor gates govern). latencyTier derives `slow` (weaknesses includes latency \u2014 minutes-long turns on hard tasks). The thinking-disabled-400 API quirk is moot for kgauto (Anthropic lowering emits no thinking param).",
131
+ // archetypePerf cloned from claude-opus-4-8 as a conservative estimate
132
+ // (Fable >= Opus in capability, but unmeasured — no fabricated superiority).
133
+ // Re-rank once brain rows exist.
134
+ archetypePerf: {
135
+ critique: 10,
136
+ plan: 10,
137
+ generate: 9,
138
+ ask: 9,
139
+ extract: 9,
140
+ transform: 9,
141
+ hunt: 8,
142
+ summarize: 8,
143
+ classify: 8
144
+ }
145
+ },
146
+ {
147
+ // Auto-onboarded 2026-06-04 from `claude-opus-4-7`; VERIFIED 2026-06-21
148
+ // against the claude-api reference. Opus 4.8 shares 4.7's surface exactly —
149
+ // 1M ctx, 128k out, $5/$25, no new breaking changes — so the clone's
150
+ // capability data was accurate (confirmed field-by-field). archetypePerf is
151
+ // still cloned from 4.7 (a sound estimate: 4.8 >= 4.7) — re-rank once brain
152
+ // rows exist. Promoted to status:'current' 2026-06-21 (Sacha: "include Claude
153
+ // Opus 4.8") — now live-selectable. Ties with the still-current claude-opus-4-7
154
+ // (identical $5/$25 + cloned perf) resolve to 4-8 by array order. (L-049/L-081.)
155
+ id: "claude-opus-4-8",
156
+ verifiedAgainstDocs: "2026-06-21",
157
+ provider: "anthropic",
158
+ status: "current",
159
+ maxContextTokens: 1e6,
160
+ maxOutputTokens: 128e3,
161
+ maxTools: 64,
162
+ parallelToolCalls: true,
163
+ structuredOutput: "grammar",
164
+ systemPromptMode: "inline",
165
+ streaming: true,
166
+ cliffs: [],
167
+ costInputPer1m: 5,
168
+ costOutputPer1m: 25,
169
+ lowering: ANTHROPIC_LOWERING_BASE,
170
+ recovery: [
171
+ {
172
+ signal: "rate_limit",
173
+ action: "escalate",
174
+ reason: "429 from Anthropic \u2014 escalate to fallback chain"
175
+ },
176
+ {
177
+ signal: "model_not_found",
178
+ action: "escalate",
179
+ reason: "Model deprecated/renamed \u2014 escalate (L-061)"
180
+ }
181
+ ],
182
+ strengths: ["reasoning", "agentic_coding", "long_context", "reliable_tool_use", "structured_output"],
183
+ weaknesses: ["cost", "latency"],
184
+ notes: "Frontier Opus (2026-06), the current recommended Opus-tier default. Verified 2026-06-21 against the claude-api reference: 1M ctx, 128k out, $5/$25 \u2014 identical surface to 4.7, no new breaking changes. Promoted to status:current 2026-06-21 (live-selectable); coexists with claude-opus-4-7 (still current).",
185
+ // Cloned archetypePerf from claude-opus-4-7 — re-evaluate once brain rows exist.
186
+ archetypePerf: {
187
+ critique: 10,
188
+ plan: 10,
189
+ generate: 9,
190
+ ask: 9,
191
+ extract: 9,
192
+ transform: 9,
193
+ hunt: 8,
194
+ summarize: 8,
195
+ classify: 8
196
+ }
197
+ },
198
+ {
199
+ id: "claude-opus-4-7",
200
+ verifiedAgainstDocs: "2026-05-08",
201
+ provider: "anthropic",
202
+ status: "current",
203
+ maxContextTokens: 1e6,
204
+ maxOutputTokens: 128e3,
205
+ maxTools: 64,
206
+ parallelToolCalls: true,
207
+ structuredOutput: "grammar",
208
+ systemPromptMode: "inline",
209
+ streaming: true,
210
+ cliffs: [],
211
+ costInputPer1m: 5,
212
+ costOutputPer1m: 25,
213
+ lowering: ANTHROPIC_LOWERING_BASE,
214
+ recovery: [
215
+ {
216
+ signal: "rate_limit",
217
+ action: "escalate",
218
+ reason: "429 from Anthropic \u2014 escalate to fallback chain"
219
+ },
220
+ {
221
+ signal: "model_not_found",
222
+ action: "escalate",
223
+ reason: "Model deprecated/renamed \u2014 escalate (L-061)"
224
+ }
225
+ ],
226
+ strengths: ["reasoning", "agentic_coding", "long_context", "reliable_tool_use", "structured_output"],
227
+ weaknesses: ["cost", "latency"],
228
+ notes: "Frontier (2026-05). Step-change improvement over 4.6 in agentic coding. Adaptive thinking only \u2014 no extended-thinking toggle. 1M context, 128k max output.",
229
+ // Frontier perf. Drops on archetypes where parallel-tool throughput
230
+ // (hunt) or low-budget cost-sensitivity (classify/summarize) matters
231
+ // more than reasoning depth.
232
+ archetypePerf: {
233
+ critique: 10,
234
+ plan: 10,
235
+ generate: 9,
236
+ ask: 9,
237
+ extract: 9,
238
+ transform: 9,
239
+ hunt: 8,
240
+ // strong but Flash dominates parallel tool throughput
241
+ summarize: 8,
242
+ // overkill for tolerant archetype; cost-out of frontier
243
+ classify: 8
244
+ // overkill; brain-validated cheaper models cover this
245
+ }
246
+ },
247
+ {
248
+ id: "claude-opus-4-6",
249
+ verifiedAgainstDocs: "2026-05-08",
250
+ provider: "anthropic",
251
+ status: "legacy",
252
+ maxContextTokens: 1e6,
253
+ maxOutputTokens: 128e3,
254
+ maxTools: 64,
255
+ parallelToolCalls: true,
256
+ structuredOutput: "grammar",
257
+ systemPromptMode: "inline",
258
+ streaming: true,
259
+ cliffs: [],
260
+ costInputPer1m: 5,
261
+ costOutputPer1m: 25,
262
+ lowering: ANTHROPIC_LOWERING_BASE,
263
+ recovery: [
264
+ {
265
+ signal: "rate_limit",
266
+ action: "escalate",
267
+ reason: "429 from Anthropic \u2014 escalate to fallback chain"
268
+ },
269
+ {
270
+ signal: "model_not_found",
271
+ action: "escalate",
272
+ reason: "Model deprecated/renamed \u2014 escalate (L-061)"
273
+ }
274
+ ],
275
+ strengths: ["reasoning", "long_context", "reliable_tool_use", "structured_output", "extended_thinking"],
276
+ weaknesses: ["cost", "latency"],
277
+ notes: "Predecessor to 4.7. Still current in Anthropic legacy table. Same pricing as 4.7 \u2014 choose 4.7 unless you need extended-thinking budget control (4.7 is adaptive-only).",
278
+ // One notch below 4.7 across the board — extended-thinking edge does
279
+ // not flip any archetype ranking. Legacy: chains should prefer 4.7.
280
+ archetypePerf: {
281
+ critique: 9,
282
+ plan: 9,
283
+ generate: 9,
284
+ ask: 9,
285
+ extract: 9,
286
+ transform: 9,
287
+ hunt: 7,
288
+ summarize: 8,
289
+ classify: 8
290
+ }
291
+ },
292
+ {
293
+ id: "claude-sonnet-4-6",
294
+ verifiedAgainstDocs: "2026-05-08",
295
+ provider: "anthropic",
296
+ status: "current",
297
+ maxContextTokens: 1e6,
298
+ maxOutputTokens: 64e3,
299
+ maxTools: 64,
300
+ parallelToolCalls: true,
301
+ structuredOutput: "grammar",
302
+ systemPromptMode: "inline",
303
+ streaming: true,
304
+ cliffs: [],
305
+ costInputPer1m: 3,
306
+ costOutputPer1m: 15,
307
+ lowering: ANTHROPIC_LOWERING_BASE,
308
+ recovery: [
309
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate" },
310
+ { signal: "model_not_found", action: "escalate", reason: "Deprecated \u2014 escalate (L-061)" }
311
+ ],
312
+ strengths: ["quality", "tool_use", "long_context", "cache_friendly", "extended_thinking"],
313
+ weaknesses: [],
314
+ notes: "Workhorse. Best price/quality for most multi-turn agentic work. 1M context, 64k max output.",
315
+ // Master plan §6.2 anchor. Tier 0 for plan/generate/ask/extract/transform
316
+ // in starter chains; tier 1 cross-provider for hunt/summarize/classify.
317
+ archetypePerf: {
318
+ ask: 9,
319
+ generate: 9,
320
+ plan: 9,
321
+ critique: 9,
322
+ extract: 9,
323
+ transform: 9,
324
+ hunt: 7,
325
+ // strong but Flash beats on parallel tool throughput
326
+ summarize: 8,
327
+ // overkill for tolerant archetype
328
+ classify: 8
329
+ // overkill
330
+ }
331
+ },
332
+ {
333
+ id: "claude-haiku-4-5",
334
+ verifiedAgainstDocs: "2026-05-08",
335
+ provider: "anthropic",
336
+ status: "current",
337
+ maxContextTokens: 2e5,
338
+ maxOutputTokens: 64e3,
339
+ maxTools: 32,
340
+ parallelToolCalls: true,
341
+ structuredOutput: "grammar",
342
+ systemPromptMode: "inline",
343
+ streaming: true,
344
+ cliffs: [
345
+ {
346
+ metric: "tool_count",
347
+ threshold: 16,
348
+ action: "drop_to_top_relevant",
349
+ reason: "Haiku reliability degrades above ~16 tools"
350
+ }
351
+ ],
352
+ costInputPer1m: 1,
353
+ costOutputPer1m: 5,
354
+ lowering: ANTHROPIC_LOWERING_BASE,
355
+ recovery: [
356
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate to Sonnet" }
357
+ ],
358
+ strengths: ["speed", "cost", "classification", "cache_friendly", "extended_thinking"],
359
+ weaknesses: ["complex_reasoning", "large_tool_sets"],
360
+ notes: "Cheapest Anthropic. Great for classify, summarize, ask shapes. 200k context, 64k max output. API alias `claude-haiku-4-5` resolves to dated snapshot `claude-haiku-4-5-20251001`.",
361
+ // Tier 1 cross-provider anchor for short-output chains (classify/
362
+ // summarize/extract/transform). Falls off on plan/critique where
363
+ // reasoning depth matters; competes with Pro on cost+latency.
364
+ archetypePerf: {
365
+ classify: 8,
366
+ summarize: 8,
367
+ ask: 7,
368
+ transform: 7,
369
+ extract: 7,
370
+ hunt: 6,
371
+ // tool reliability drops at 16 — cliff guard fires
372
+ generate: 6,
373
+ plan: 5,
374
+ critique: 4
375
+ // reasoning depth gap vs Sonnet/Opus
376
+ }
377
+ },
378
+ // ── Google ──
379
+ {
380
+ id: "gemini-2.5-flash",
381
+ verifiedAgainstDocs: "2026-05-08",
382
+ provider: "google",
383
+ status: "current",
384
+ maxContextTokens: 1048576,
385
+ maxOutputTokens: 65535,
386
+ maxTools: 128,
387
+ parallelToolCalls: true,
388
+ structuredOutput: "native",
389
+ systemPromptMode: "separate",
390
+ streaming: true,
391
+ cliffs: [
392
+ {
393
+ metric: "input_tokens",
394
+ threshold: 8e3,
395
+ action: "downgrade_quality_warning",
396
+ reason: "Quality degrades significantly above ~8K context tokens"
397
+ },
398
+ {
399
+ metric: "tool_count",
400
+ threshold: 20,
401
+ action: "drop_to_top_relevant",
402
+ reason: "Tool reliability drops above ~20 tools (despite 128 hard limit)"
403
+ },
404
+ {
405
+ metric: "thinking_with_short_output",
406
+ threshold: 1,
407
+ action: "force_thinking_budget_zero",
408
+ reason: "Thinking tokens consume maxOutputTokens \u2014 empty response if drained"
409
+ },
410
+ {
411
+ // s11 trust artifact (2026-05-02): brain showed 5/5 empty rate on
412
+ // tt-intelligence/summarize/gemini-2.5-flash with tools offered.
413
+ // v1's disable_thinking_for_short_output already fired and didn't
414
+ // help — disabling thinking is necessary but not sufficient. Tools
415
+ // present + summarize intent confuses Flash into a no-output state
416
+ // (likely tool-decision purgatory). Strip tools entirely for this
417
+ // archetype on this model.
418
+ metric: "tool_count",
419
+ threshold: 1,
420
+ whenIntent: "summarize",
421
+ action: "strip_tools",
422
+ reason: "Gemini Flash returns empty when summarize intent has tools offered (5/5 empty rate observed in v1 prod 2026-04-19, replayed into v2 brain 2026-04-29)"
423
+ }
424
+ ],
425
+ costInputPer1m: 0.3,
426
+ costOutputPer1m: 2.5,
427
+ lowering: {
428
+ ...GOOGLE_LOWERING_BASE,
429
+ thinking: { field: "generationConfig.thinkingConfig.thinkingBudget", default: "auto" }
430
+ },
431
+ recovery: [
432
+ {
433
+ signal: "empty_response_after_tool",
434
+ action: "retry_with_params",
435
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
436
+ maxRetries: 1,
437
+ reason: "Known: empty after tool result \u2014 retry with thinking off"
438
+ },
439
+ {
440
+ signal: "empty_response",
441
+ action: "retry_with_params",
442
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
443
+ maxRetries: 1,
444
+ reason: "Empty response \u2014 try with thinking off"
445
+ },
446
+ {
447
+ signal: "malformed_function_call",
448
+ action: "escalate",
449
+ reason: "MALFORMED_FUNCTION_CALL maps to stop \u2014 escalate to next target"
450
+ }
451
+ ],
452
+ strengths: ["speed", "volume", "classification", "1m_context", "cost"],
453
+ weaknesses: ["complex_schemas", "large_tool_sets", "high_context_quality"],
454
+ notes: "Fast and cheap with 1M context. Quality cliffs at 8K context and 20 tools \u2014 guard with cliffs.",
455
+ // Master plan §6.2 anchor. Tier 0 for hunt (parallel tool throughput
456
+ // 15-75 calls/step beats Sonnet — L-040), summarize, classify.
457
+ archetypePerf: {
458
+ hunt: 9,
459
+ // L-040: parallel tool throughput 15-75/step
460
+ classify: 7,
461
+ // brain-validated, 218 rows
462
+ summarize: 7,
463
+ // brain-validated; cliff strips tools when present
464
+ transform: 7,
465
+ ask: 7,
466
+ generate: 6,
467
+ plan: 5,
468
+ extract: 6,
469
+ // alpha.8 MAX_TOKENS history on structured output
470
+ critique: 4
471
+ // reasoning shallower than Sonnet/Opus
472
+ }
473
+ },
474
+ {
475
+ // ── Gemini 2.5 Flash-Lite ──
476
+ // Onboarded 2026-05-13 (s22) after the model-release watcher surfaced
477
+ // it as a UNREGISTERED + NEW candidate. Released by Google July 2025,
478
+ // stable. Positioned BELOW Flash on the cost/perf frontier:
479
+ // input $0.10/M (Flash $0.30/M) — 3× cheaper
480
+ // output $0.40/M (Flash $2.50/M) — 6× cheaper
481
+ // cache $0.01/M — 1/10 of input (vs Flash 0.25 discount)
482
+ // Cliffs are HYPOTHESIZED from Flash's known failure modes — Flash-Lite
483
+ // is a smaller sibling, so we inherit Flash's cliffs at equal-or-tighter
484
+ // thresholds. The brain will validate/relax these as evidence accumulates
485
+ // per (archetype, model) tuple. Currently ZERO brain rows for this model.
486
+ id: "gemini-2.5-flash-lite",
487
+ verifiedAgainstDocs: "2026-05-13",
488
+ provider: "google",
489
+ status: "current",
490
+ maxContextTokens: 1048576,
491
+ maxOutputTokens: 65536,
492
+ maxTools: 128,
493
+ parallelToolCalls: true,
494
+ structuredOutput: "native",
495
+ systemPromptMode: "separate",
496
+ streaming: true,
497
+ cliffs: [
498
+ {
499
+ metric: "input_tokens",
500
+ threshold: 8e3,
501
+ action: "downgrade_quality_warning",
502
+ reason: "Inherited from Flash: quality degrades above ~8K. Smaller model \u2014 likely degrades faster. Re-tune from brain after n\u226520."
503
+ },
504
+ {
505
+ metric: "tool_count",
506
+ threshold: 10,
507
+ action: "drop_to_top_relevant",
508
+ reason: "Conservative: Flash drops at 20, Flash-Lite is smaller \u2014 assume tighter ceiling until brain proves otherwise."
509
+ },
510
+ {
511
+ metric: "thinking_with_short_output",
512
+ threshold: 1,
513
+ action: "force_thinking_budget_zero",
514
+ reason: "Thinking enabled per Google API (thinking: true). Same drain risk as Flash \u2014 thinking tokens consume maxOutputTokens."
515
+ },
516
+ {
517
+ // Strong prior: Flash hit 5/5 empty rate on summarize+tools (s11
518
+ // trust artifact, kgauto commit 3872832). Flash-Lite shares the
519
+ // same architectural family — almost certainly inherits this cliff.
520
+ // Ship the guard preemptively; brain telemetry confirms or relaxes.
521
+ metric: "tool_count",
522
+ threshold: 1,
523
+ whenIntent: "summarize",
524
+ action: "strip_tools",
525
+ reason: "Inherited from Flash s11 cliff: summarize+tools \u2192 empty response. Preemptive guard until brain evidence on Flash-Lite specifically."
526
+ }
527
+ ],
528
+ costInputPer1m: 0.1,
529
+ costOutputPer1m: 0.4,
530
+ lowering: {
531
+ ...GOOGLE_LOWERING_BASE,
532
+ // Cache discount 10× (vs Flash 4×) — Google's spec is $0.01/M cache vs
533
+ // $0.10/M input. Material for repeat-prompt workloads (classify shape).
534
+ cache: { ...GOOGLE_LOWERING_BASE.cache, discount: 0.1 },
535
+ thinking: { field: "generationConfig.thinkingConfig.thinkingBudget", default: "auto" }
536
+ },
537
+ recovery: [
538
+ {
539
+ signal: "empty_response_after_tool",
540
+ action: "retry_with_params",
541
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
542
+ maxRetries: 1,
543
+ reason: "Known on Flash family: empty after tool result \u2014 retry with thinking off."
544
+ },
545
+ {
546
+ signal: "empty_response",
547
+ action: "retry_with_params",
548
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
549
+ maxRetries: 1,
550
+ reason: "Empty response \u2014 try with thinking off."
551
+ },
552
+ {
553
+ signal: "malformed_function_call",
554
+ action: "escalate",
555
+ reason: "MALFORMED_FUNCTION_CALL maps to stop \u2014 escalate to next target."
556
+ }
557
+ ],
558
+ strengths: ["lowest_cost", "speed", "volume", "classification", "summarize", "1m_context", "cache_friendly"],
559
+ weaknesses: ["complex_reasoning", "large_tool_sets", "complex_schemas", "structured_output_unproven", "long_context_quality"],
560
+ notes: "Bottom-frontier anchor on cost: $0.10/$0.40 per 1M tokens, 1M context, 65K max output. Released July 2025 (stable). Positioned for classify / summarize / transform archetypes where quality bar is forgiving. Cliffs inherited from Flash at equal-or-tighter thresholds \u2014 re-tune per (archetype) once brain has n\u226520 rows. Alpha.8 contract layer handles MAX_TOKENS-on-structured-output via fallback chain, so structuredOutput=native is safe to declare even though Flash had alpha.8 history. Cache discount in spec: $0.01/M = 1/10 of input (richer than Flash 25%) \u2014 meaningful for repeat-prompt workloads.",
561
+ // Tier 3 emergency floor for summarize/classify chains. ZERO brain
562
+ // rows — all values are starter hypotheses anchored to "smaller
563
+ // sibling of Flash, at-or-below Flash perf on every archetype." The
564
+ // first 50 brain rows per archetype will validate or relax these.
565
+ archetypePerf: {
566
+ classify: 6,
567
+ // starter hypothesis — verify (Flash is 7, lite likely ≤)
568
+ summarize: 6,
569
+ // starter hypothesis — verify; cliff strips tools
570
+ transform: 6,
571
+ // starter hypothesis — verify
572
+ ask: 5,
573
+ hunt: 5,
574
+ generate: 4,
575
+ extract: 4,
576
+ plan: 3,
577
+ critique: 3
578
+ }
579
+ },
580
+ {
581
+ id: "gemini-2.5-pro",
582
+ verifiedAgainstDocs: "2026-05-08",
583
+ provider: "google",
584
+ status: "current",
585
+ maxContextTokens: 1048576,
586
+ maxOutputTokens: 65535,
587
+ maxTools: 128,
588
+ parallelToolCalls: true,
589
+ structuredOutput: "native",
590
+ systemPromptMode: "separate",
591
+ streaming: true,
592
+ cliffs: [
593
+ {
594
+ metric: "input_tokens",
595
+ threshold: 2e5,
596
+ action: "downgrade_quality_warning",
597
+ reason: "Pricing doubles above 200K: input $1.25\u2192$2.50/M, output $10\u2192$15/M"
598
+ }
599
+ ],
600
+ costInputPer1m: 1.25,
601
+ costOutputPer1m: 10,
602
+ lowering: {
603
+ ...GOOGLE_LOWERING_BASE,
604
+ thinking: { field: "generationConfig.thinkingConfig.thinkingBudget", default: "auto" }
605
+ },
606
+ recovery: [
607
+ {
608
+ signal: "malformed_function_call",
609
+ action: "escalate",
610
+ reason: "MALFORMED_FUNCTION_CALL \u2014 escalate"
611
+ }
612
+ ],
613
+ strengths: ["reasoning", "1m_context", "structured_output", "tool_use"],
614
+ weaknesses: ["pricing_above_200k"],
615
+ // Master plan §3.3 anchor: tier-2 cross-provider in almost every chain.
616
+ // Sits on the frontier at perf-9 — close to Sonnet but cheaper input.
617
+ archetypePerf: {
618
+ critique: 9,
619
+ plan: 9,
620
+ ask: 8,
621
+ generate: 8,
622
+ extract: 8,
623
+ transform: 8,
624
+ hunt: 8,
625
+ // tier 1 cross-provider for hunt chain
626
+ summarize: 7,
627
+ classify: 7
628
+ }
629
+ },
630
+ {
631
+ id: "gemini-3.1-pro-preview",
632
+ verifiedAgainstDocs: "2026-05-08",
633
+ provider: "google",
634
+ status: "preview",
635
+ maxContextTokens: 1048576,
636
+ maxOutputTokens: 65535,
637
+ maxTools: 128,
638
+ parallelToolCalls: true,
639
+ structuredOutput: "native",
640
+ systemPromptMode: "separate",
641
+ streaming: true,
642
+ cliffs: [
643
+ {
644
+ metric: "input_tokens",
645
+ threshold: 2e5,
646
+ action: "downgrade_quality_warning",
647
+ reason: "Pricing doubles above 200K: input $2\u2192$4/M, output $12\u2192$18/M"
648
+ }
649
+ ],
650
+ costInputPer1m: 2,
651
+ costOutputPer1m: 12,
652
+ lowering: {
653
+ ...GOOGLE_LOWERING_BASE,
654
+ cache: { ...GOOGLE_LOWERING_BASE.cache, discount: 0.1 },
655
+ thinking: { field: "generationConfig.thinkingConfig.thinkingBudget", default: "auto" }
656
+ },
657
+ recovery: [
658
+ {
659
+ signal: "malformed_function_call",
660
+ action: "escalate",
661
+ reason: "MALFORMED_FUNCTION_CALL \u2014 escalate"
662
+ }
663
+ ],
664
+ strengths: ["reasoning", "1m_context", "agentic_coding", "structured_output", "tool_use"],
665
+ weaknesses: ["cost", "preview_status", "pricing_above_200k"],
666
+ notes: "Frontier Gemini (preview, 2026-Q2). Step-change agentic coding per Google. Cache discount 10\xD7 (vs 4\xD7 for 2.5 Pro). Use status=preview to flag rollback path until GA.",
667
+ // Frontier-Gemini preview — bumped one notch over 2.5 Pro on agentic
668
+ // coding / reasoning per Google's release notes. Preview status:
669
+ // chains should stay on 2.5 Pro until GA. Starter hypothesis.
670
+ archetypePerf: {
671
+ critique: 10,
672
+ // Google claims step-change on reasoning
673
+ plan: 10,
674
+ ask: 9,
675
+ generate: 9,
676
+ extract: 9,
677
+ transform: 8,
678
+ hunt: 9,
679
+ // step-change agentic per Google
680
+ summarize: 8,
681
+ classify: 7
682
+ }
683
+ },
684
+ // ── DeepSeek ──
685
+ // 2026-05-08 audit (L-073): DeepSeek's `deepseek-chat` was silently aliased
686
+ // to `deepseek-v4-flash` non-thinking mode. Old kgauto profile claimed 64k
687
+ // context + $0.27/$1.10 — actual is 1M context + $0.14/$0.28. Now modeled
688
+ // as: V4-Flash + V4-Pro as canonical profiles; deepseek-chat and
689
+ // deepseek-reasoner registered as aliases (see ALIASES below).
690
+ {
691
+ id: "deepseek-v4-flash",
692
+ verifiedAgainstDocs: "2026-05-08",
693
+ provider: "deepseek",
694
+ status: "current",
695
+ maxContextTokens: 1e6,
696
+ maxOutputTokens: 384e3,
697
+ maxTools: 16,
698
+ parallelToolCalls: false,
699
+ structuredOutput: "native",
700
+ systemPromptMode: "inline",
701
+ streaming: true,
702
+ cliffs: [
703
+ {
704
+ metric: "tool_count",
705
+ threshold: 1,
706
+ action: "drop_to_top_relevant",
707
+ reason: "Sequential tool calls only \u2014 L-040"
708
+ }
709
+ ],
710
+ costInputPer1m: 0.14,
711
+ costOutputPer1m: 0.28,
712
+ lowering: {
713
+ system: { mode: "inline" },
714
+ cache: { strategy: "unsupported" },
715
+ tools: { format: "deepseek" }
716
+ },
717
+ recovery: [
718
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate" }
719
+ ],
720
+ strengths: ["cost", "1m_context", "json_output", "code", "reasoning"],
721
+ weaknesses: ["parallel_tools", "large_tool_sets"],
722
+ // alpha.47 — explicit slow override. Tag derivation would say 'medium'
723
+ // (no 'latency' weakness, no 'speed' strength), but the alpha.46 shadow
724
+ // probe MEASURED deepseek-v4-flash at 20485ms served (2026-06-03) — ~2.3×
725
+ // gemini-2.5-flash on PB's synchronous /api/analyze path. The 'flash' name
726
+ // is DeepSeek's, not a speed promise. This is the row that, scoring 0.85
727
+ // baseQuality (it carries 'reasoning') with no latency counterweight,
728
+ // leapfrogged gemini-2.5-flash as PB's summarize leader once reachable.
729
+ latencyTier: "slow",
730
+ notes: "Cheap workhorse. 1M context, 384k max output. Cache-hit input $0.0028/M (1/50\xD7 of miss). Aliased as `deepseek-chat` (non-thinking) and `deepseek-reasoner` (thinking) \u2014 see ALIASES.",
731
+ // Master plan §6.2 anchor. Brain-validated tier 1 cross-provider for
732
+ // classify (169 rows, 0% empty). Tier 0 for summarize-with-no-tools.
733
+ // Falls off on hunt (sequential tools — L-040) and reasoning depth.
734
+ archetypePerf: {
735
+ classify: 7,
736
+ // brain-validated, 169 rows
737
+ summarize: 7,
738
+ // archetype-tolerant, no brain evidence yet
739
+ ask: 6,
740
+ transform: 6,
741
+ generate: 5,
742
+ plan: 5,
743
+ extract: 5,
744
+ critique: 4,
745
+ hunt: 4
746
+ // sequential tool calls only — L-040
747
+ },
748
+ // alpha.43 — family-level conventions for the `deepseek-chat` family
749
+ // (V4-Flash + future non-thinking-mode members). V4-Flash struggles
750
+ // with summarize-archetype + structured-output: 3 of 4 schema-attempted
751
+ // PB calls (2026-05-18 + 2026-05-22) hit `structured_output_parse_failed`.
752
+ // Surface the friction at compile time so consumers either drop the
753
+ // schema or route to a more schema-reliable family.
754
+ archetypeConventions: [
755
+ {
756
+ archetype: "summarize",
757
+ structuredOutputHint: "avoid",
758
+ cliffWarning: "deepseek-chat family schema compliance on summarize is weak (3 of 4 schema-attempted PB calls hit structured_output_parse_failed). Consider routing summarize+structured-output to a different family (claude-sonnet, gemini-pro, openai-gpt-5.4) or dropping the schema for free-text summaries.",
759
+ reason: "Brain evidence: PB compile_outcomes 2026-05-18 + 2026-05-22, error_type=structured_output_parse_failed on deepseek-v4-flash + summarize archetype."
760
+ }
761
+ ]
762
+ },
763
+ {
764
+ id: "deepseek-v4-pro",
765
+ verifiedAgainstDocs: "2026-06-22",
766
+ provider: "deepseek",
767
+ status: "current",
768
+ maxContextTokens: 1e6,
769
+ maxOutputTokens: 384e3,
770
+ maxTools: 16,
771
+ parallelToolCalls: false,
772
+ structuredOutput: "native",
773
+ systemPromptMode: "inline",
774
+ streaming: true,
775
+ cliffs: [
776
+ {
777
+ metric: "tool_count",
778
+ threshold: 1,
779
+ action: "drop_to_top_relevant",
780
+ reason: "Sequential tool calls only \u2014 L-040"
781
+ }
782
+ ],
783
+ // Verified against the live DeepSeek pricing page 2026-06-22: the 75%-off
784
+ // launch "promo" ($0.435/$0.87) did NOT revert on 2026-05-31 — it became the
785
+ // standing price (docs show $0.435 in / $0.87 out cache-miss, $0.003625
786
+ // cache-hit in; no promo caveat). The old $1.74/$3.48 "regular post-promo"
787
+ // was a projected revert that never happened (L-073/L-081) — it 4×-overstated
788
+ // cost and suppressed V4-Pro in cost-aware ranking.
789
+ costInputPer1m: 0.435,
790
+ costOutputPer1m: 0.87,
791
+ lowering: {
792
+ system: { mode: "inline" },
793
+ cache: { strategy: "unsupported" },
794
+ tools: { format: "deepseek" }
795
+ },
796
+ recovery: [
797
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate" }
798
+ ],
799
+ strengths: ["quality", "reasoning", "1m_context", "json_output", "code", "extended_thinking"],
800
+ weaknesses: ["parallel_tools", "large_tool_sets"],
801
+ // alpha.47 — explicit slow override. Measured 47722ms on the alpha.46
802
+ // shadow probe (2026-06-03) — it's an extended-thinking reasoner, slowest
803
+ // of the served set. Tag derivation would say 'medium'; the measurement says
804
+ // otherwise.
805
+ latencyTier: "slow",
806
+ notes: "Pro tier. 1M context, 384k max output. $0.435/$0.87 per 1M (cache-hit in $0.003625) \u2014 verified live 2026-06-22; the launch promo became the standing price (no revert). Default mode = thinking.",
807
+ // Master plan §3.3: tier 3 cross-provider for plan chain. Reasoning
808
+ // bumped one notch over V4-Flash; same parallel-tool ceiling.
809
+ archetypePerf: {
810
+ plan: 7,
811
+ // §3.3 tier 3 for plan
812
+ critique: 6,
813
+ ask: 7,
814
+ generate: 6,
815
+ classify: 7,
816
+ summarize: 7,
817
+ extract: 6,
818
+ transform: 6,
819
+ hunt: 4
820
+ // sequential tools — same as V4-Flash
821
+ },
822
+ // alpha.43 — family-level conventions for the `deepseek-reasoner` family
823
+ // (V4-Pro is the family rep; future thinking-mode members inherit).
824
+ //
825
+ // The decisive-archetype suffix is the load-bearing piece: 2026-05-28
826
+ // first real V4-Pro probe on tt-intel/classify (exclusion-finding ID 20)
827
+ // showed 8/10 judge rationales citing "candidate hedges and fails to
828
+ // commit to a single classification" or "candidate refuses to pick
829
+ // among the labels." Without the forcing-function suffix, every
830
+ // reasoner probe on classify/extract/ask verdicts stay-excluded for
831
+ // model-behavior reasons (epistemic humility is reasoner-shaped),
832
+ // NOT quality reasons. The suffix re-shapes the contract so the model
833
+ // is asked for what it's actually being measured on.
834
+ //
835
+ // The hunt-archetype cliffWarning is the L-040 architectural reading
836
+ // surfaced at compile time. Reasoners produce sequential reasoning
837
+ // chains by design; hunt archetypes with 3+ parallel tools lose
838
+ // substantial throughput (L-040 + L-041). The L-040 cliff in the
839
+ // `cliffs` array already trims to top-relevant tools; this warning
840
+ // tells the consumer the family is wrong for the shape so they can
841
+ // route to gemini-flash (parallel-strong) instead of accepting trimmed
842
+ // throughput silently.
843
+ archetypeConventions: [
844
+ {
845
+ archetype: "classify",
846
+ promptSuffix: "\n\nOutput exactly one classification label. Do not hedge, do not add preamble, do not explain your reasoning. If uncertain, pick the most likely label and commit to it.",
847
+ reason: "Reasoners (deepseek-reasoner family) hedge on decisive-output archetypes by design. Evidence: 2026-05-28 V4-Pro probe on tt-intel/classify (exclusion-finding ID 20, brain probe_outcomes); 8 of 10 judge rationales cited hedging or refusal-to-commit. Forcing-function suffix converts reasoner output to decisive labels."
848
+ },
849
+ {
850
+ archetype: "extract",
851
+ promptSuffix: "\n\nOutput exactly the requested structured fields. Do not hedge, do not add preamble, do not explain your reasoning. If a field is unknown, emit the field with a null/empty value and commit to that \u2014 do not refuse to extract.",
852
+ reason: "Reasoners (deepseek-reasoner family) hedge on decisive-output archetypes by design. Evidence: same family failure shape as classify (exclusion-finding ID 20, 2026-05-28). Forcing-function suffix asks for commitment instead of epistemic-humility hedging."
853
+ },
854
+ {
855
+ archetype: "ask",
856
+ promptSuffix: "\n\nAnswer the question directly. Do not hedge, do not add preamble, do not explain your reasoning unless explicitly asked. If uncertain, give your best answer and commit to it.",
857
+ reason: "Reasoners (deepseek-reasoner family) hedge on decisive-output archetypes by design. Evidence: same family failure shape as classify (exclusion-finding ID 20, 2026-05-28). Forcing-function suffix asks for commitment instead of epistemic-humility hedging."
858
+ },
859
+ {
860
+ archetype: "hunt",
861
+ whenToolCountAtLeast: 3,
862
+ cliffWarning: "Reasoners (deepseek-reasoner family) produce sequential reasoning chains by design; hunt archetypes with 3+ parallel tools lose substantial throughput. Consider gemini-flash family (parallel-strong) instead \u2014 see L-040 / L-041.",
863
+ reason: "L-040 + L-041 architectural reading; 2026-05-28 V4-Pro probe evidence (exclusion-finding ID 20) confirms reasoner-family hunt fit is structurally weak."
864
+ }
865
+ ]
866
+ },
867
+ // ── OpenAI ──
868
+ // alpha.16 (2026-05-17): close the half-supported provider gap. env.ts
869
+ // already registered OPENAI_API_KEY + executeOpenAI + normalizeOpenAILike
870
+ // + lowerOpenAI all existed; profile entries were missing, so the
871
+ // alpha.10 auto-filter would mark openai-keyed models reachable but
872
+ // there were no profiles to filter IN. Half-supported is now fully
873
+ // supported. PB request `openai-provider-profiles` (2026-05-16).
874
+ //
875
+ // Profile data verified against developers.openai.com/api/docs/pricing
876
+ // + per-model pages 2026-05-17. L-049/L-081 step-zero: no AI-trained
877
+ // numbers — fetched live from OpenAI's docs. As of 2026-05, OpenAI's
878
+ // current flagship is gpt-5.5 (2025-12 cutoff); gpt-5.4-{base,mini,nano}
879
+ // are the workhorse family. gpt-4.1 + gpt-4o are legacy.
880
+ //
881
+ // Both 5.5 and 5.4 carry a 272K input-token pricing cliff (2x input,
882
+ // 1.5x output beyond that). Modeled as a `downgrade_quality_warning`
883
+ // cliff because it ranks the model down at large-context shapes — the
884
+ // semantics of "this model is now 2x more expensive" map onto the
885
+ // existing penalty mechanism. Cost-watcher will catch high-context
886
+ // spikes empirically; the cliff prevents naive routing into the doubled
887
+ // pricing zone.
888
+ {
889
+ id: "gpt-5.5",
890
+ verifiedAgainstDocs: "2026-05-17",
891
+ provider: "openai",
892
+ status: "current",
893
+ maxContextTokens: 105e4,
894
+ maxOutputTokens: 128e3,
895
+ maxTools: 64,
896
+ parallelToolCalls: true,
897
+ structuredOutput: "native",
898
+ systemPromptMode: "inline",
899
+ streaming: true,
900
+ cliffs: [
901
+ {
902
+ metric: "input_tokens",
903
+ threshold: 272e3,
904
+ action: "downgrade_quality_warning",
905
+ reason: "OpenAI pricing tier shift: >272K input tokens billed at 2x input + 1.5x output rates"
906
+ }
907
+ ],
908
+ costInputPer1m: 5,
909
+ costOutputPer1m: 30,
910
+ lowering: {
911
+ system: { mode: "inline" },
912
+ // OpenAI caching is implicit (auto-applied to repeated prefixes
913
+ // ≥1024 tokens for prompt_tokens_details.cached_tokens). No
914
+ // wire-format marker. Discount: 10x for cached input ($0.50/$5.00).
915
+ cache: { strategy: "unsupported", minTokens: 1024, discount: 0.1 },
916
+ tools: { format: "openai" }
917
+ },
918
+ recovery: [
919
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate to fallback chain" },
920
+ { signal: "model_not_found", action: "escalate", reason: "Model deprecated/renamed \u2014 escalate (L-061)" }
921
+ ],
922
+ strengths: ["reasoning", "agentic_coding", "long_context", "structured_output", "reliable_tool_use", "reasoning_effort_knob"],
923
+ weaknesses: ["cost", "pricing_cliff_at_272k"],
924
+ notes: "OpenAI frontier (2026-05). 1M context (1.05M total), 128K max output, 2025-12 cutoff. Reasoning effort knob (none/low/medium/high/xhigh). Pricing cliff at 272K input.",
925
+ // Frontier-tier perf hypothesis. Anchored to Opus 4.7 row (similar
926
+ // price/positioning). Brain evidence will refine; no telemetry yet.
927
+ archetypePerf: {
928
+ critique: 9,
929
+ plan: 9,
930
+ generate: 9,
931
+ ask: 9,
932
+ extract: 9,
933
+ transform: 9,
934
+ hunt: 8,
935
+ // parallel tool support good but cliff at 272K hurts deep multi-step
936
+ summarize: 7,
937
+ // overkill for tolerant archetype
938
+ classify: 7
939
+ // overkill; cheaper models cover this
940
+ }
941
+ },
942
+ {
943
+ id: "gpt-5.4",
944
+ verifiedAgainstDocs: "2026-05-17",
945
+ provider: "openai",
946
+ status: "current",
947
+ maxContextTokens: 105e4,
948
+ maxOutputTokens: 128e3,
949
+ maxTools: 64,
950
+ parallelToolCalls: true,
951
+ structuredOutput: "native",
952
+ systemPromptMode: "inline",
953
+ streaming: true,
954
+ cliffs: [
955
+ {
956
+ metric: "input_tokens",
957
+ threshold: 272e3,
958
+ action: "downgrade_quality_warning",
959
+ reason: "OpenAI pricing tier shift: >272K input tokens billed at 2x input + 1.5x output rates"
960
+ }
961
+ ],
962
+ costInputPer1m: 2.5,
963
+ costOutputPer1m: 15,
964
+ lowering: {
965
+ system: { mode: "inline" },
966
+ cache: { strategy: "unsupported", minTokens: 1024, discount: 0.1 },
967
+ tools: { format: "openai" }
968
+ },
969
+ recovery: [
970
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate to fallback chain" },
971
+ { signal: "model_not_found", action: "escalate", reason: "Model deprecated/renamed \u2014 escalate (L-061)" }
972
+ ],
973
+ strengths: ["reasoning", "long_context", "structured_output", "reliable_tool_use"],
974
+ weaknesses: ["pricing_cliff_at_272k"],
975
+ notes: "OpenAI workhorse (2026-05). 1M context (1.05M total), 128K max output, 2025-08 cutoff. Pricing cliff at 272K input. Pairs cleanly with Sonnet 4.6 on cost ($2.50/$15.00 vs $3.00/$15.00).",
976
+ // Anchored to Sonnet 4.6 row (similar price/positioning). Slight
977
+ // anthropic-side edge on agentic coding per master plan vibe.
978
+ archetypePerf: {
979
+ critique: 8,
980
+ plan: 8,
981
+ generate: 8,
982
+ ask: 8,
983
+ extract: 8,
984
+ transform: 8,
985
+ hunt: 7,
986
+ summarize: 7,
987
+ classify: 7
988
+ }
989
+ },
990
+ {
991
+ id: "gpt-5.4-mini",
992
+ verifiedAgainstDocs: "2026-05-17",
993
+ provider: "openai",
994
+ status: "current",
995
+ maxContextTokens: 4e5,
996
+ maxOutputTokens: 128e3,
997
+ maxTools: 64,
998
+ parallelToolCalls: true,
999
+ structuredOutput: "native",
1000
+ systemPromptMode: "inline",
1001
+ streaming: true,
1002
+ cliffs: [],
1003
+ costInputPer1m: 0.75,
1004
+ costOutputPer1m: 4.5,
1005
+ lowering: {
1006
+ system: { mode: "inline" },
1007
+ cache: { strategy: "unsupported", minTokens: 1024, discount: 0.1 },
1008
+ tools: { format: "openai" }
1009
+ },
1010
+ recovery: [
1011
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate to fallback chain" },
1012
+ { signal: "model_not_found", action: "escalate", reason: "Model deprecated/renamed \u2014 escalate (L-061)" }
1013
+ ],
1014
+ strengths: ["cost", "speed", "agentic_coding", "structured_output", "reliable_tool_use"],
1015
+ weaknesses: ["reasoning_depth"],
1016
+ notes: "OpenAI mini-tier (2026-05). 400K context, 128K max output, 2025-08 cutoff. OpenAI describes as 'strongest mini model for coding, computer use, subagents.' Cache discount 10x ($0.075 input).",
1017
+ // Mini-tier hypothesis. Anchored to Haiku 4.5 + Flash row pricing.
1018
+ // Cost is slightly higher than Haiku ($0.75 vs $0.50 input) but
1019
+ // OpenAI claims strong coding/subagent perf.
1020
+ archetypePerf: {
1021
+ ask: 7,
1022
+ generate: 7,
1023
+ extract: 7,
1024
+ transform: 7,
1025
+ classify: 7,
1026
+ summarize: 7,
1027
+ hunt: 7,
1028
+ plan: 6,
1029
+ critique: 5
1030
+ // reasoning depth gap — frontier models handle this
1031
+ }
1032
+ },
1033
+ {
1034
+ id: "gpt-5.4-nano",
1035
+ verifiedAgainstDocs: "2026-05-17",
1036
+ provider: "openai",
1037
+ status: "current",
1038
+ maxContextTokens: 4e5,
1039
+ maxOutputTokens: 128e3,
1040
+ maxTools: 64,
1041
+ parallelToolCalls: true,
1042
+ structuredOutput: "native",
1043
+ systemPromptMode: "inline",
1044
+ streaming: true,
1045
+ cliffs: [],
1046
+ costInputPer1m: 0.2,
1047
+ costOutputPer1m: 1.25,
1048
+ lowering: {
1049
+ system: { mode: "inline" },
1050
+ cache: { strategy: "unsupported", minTokens: 1024, discount: 0.1 },
1051
+ tools: { format: "openai" }
1052
+ },
1053
+ recovery: [
1054
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate to fallback chain" },
1055
+ { signal: "model_not_found", action: "escalate", reason: "Model deprecated/renamed \u2014 escalate (L-061)" }
1056
+ ],
1057
+ strengths: ["cost", "speed", "volume", "structured_output"],
1058
+ weaknesses: ["reasoning_depth", "no_computer_use"],
1059
+ notes: "OpenAI nano-tier (2026-05). 400K context, 128K max output, 2025-08 cutoff. 'Cheapest GPT-5.4-class for simple high-volume tasks.' No fine-tuning, no computer-use tools. Cache discount 10x.",
1060
+ // Nano-tier. Anchored to Flash-Lite row ($0.10/$0.40 vs nano's
1061
+ // $0.20/$1.25). Slightly more expensive than Flash-Lite but with
1062
+ // OpenAI brand reliability. Good fit for classify/summarize floor.
1063
+ archetypePerf: {
1064
+ classify: 7,
1065
+ summarize: 6,
1066
+ ask: 6,
1067
+ transform: 6,
1068
+ extract: 6,
1069
+ generate: 5,
1070
+ hunt: 5,
1071
+ plan: 4,
1072
+ critique: 3
1073
+ // not for reasoning archetypes
1074
+ }
1075
+ },
1076
+ // ── Auto-onboarded (UNVERIFIED) ──
1077
+ // Cloned by scripts/auto-onboard-models.mjs from a same-family template.
1078
+ // Each entry's pricing/context/cliffs/lowering reflects the template, NOT
1079
+ // provider docs. Verify before promoting status to 'current' (L-049/L-081).
1080
+ {
1081
+ // s37 (2026-05-21): UNVERIFIED-AUTO-ONBOARD → verified against
1082
+ // ai.google.dev/gemini-api/docs/models/gemini-3-flash-preview +
1083
+ // ai.google.dev/gemini-api/docs/pricing. L-081 catches:
1084
+ // maxOutputTokens 65_535 → 65_536 (off-by-one)
1085
+ // costInputPer1m 0.30 → 0.50 (template-cloned from 2.5-flash; actual is 1.67× more expensive)
1086
+ // costOutputPer1m 2.50 → 3.00 (template-cloned; actual 1.2× more expensive)
1087
+ // cache discount default 0.25 → 0.10 (10× discount, $0.05/$0.50 per docs)
1088
+ // Cliffs inherited from 2.5-flash conservatively. The 8K-context-quality
1089
+ // cliff was a 2.5-Flash observation — Google positions Gemini 3 as
1090
+ // sustained-frontier-on-long-context; brain evidence will validate/relax.
1091
+ // Kept as guard for now.
1092
+ id: "gemini-3-flash-preview",
1093
+ verifiedAgainstDocs: "2026-05-21",
1094
+ provider: "google",
1095
+ status: "preview",
1096
+ maxContextTokens: 1048576,
1097
+ maxOutputTokens: 65536,
1098
+ maxTools: 128,
1099
+ parallelToolCalls: true,
1100
+ structuredOutput: "native",
1101
+ systemPromptMode: "separate",
1102
+ streaming: true,
1103
+ cliffs: [
1104
+ {
1105
+ metric: "input_tokens",
1106
+ threshold: 8e3,
1107
+ action: "downgrade_quality_warning",
1108
+ reason: "Inherited from 2.5-flash guard; brain evidence on Gemini 3 long-context quality will validate/relax"
1109
+ },
1110
+ {
1111
+ metric: "tool_count",
1112
+ threshold: 20,
1113
+ action: "drop_to_top_relevant",
1114
+ reason: "Tool reliability drops above ~20 tools (despite 128 hard limit) \u2014 inherited from Flash family"
1115
+ },
1116
+ {
1117
+ metric: "thinking_with_short_output",
1118
+ threshold: 1,
1119
+ action: "force_thinking_budget_zero",
1120
+ reason: "Thinking tokens consume maxOutputTokens \u2014 empty response if drained"
1121
+ },
1122
+ {
1123
+ // Inherited from gemini-2.5-flash s11 trust artifact. Family-likely
1124
+ // failure mode for Flash architecture. Keep preemptively until brain
1125
+ // evidence on Gemini 3 specifically.
1126
+ metric: "tool_count",
1127
+ threshold: 1,
1128
+ whenIntent: "summarize",
1129
+ action: "strip_tools",
1130
+ reason: "Inherited from 2.5-flash s11 cliff: summarize+tools \u2192 empty response. Preemptive guard until brain evidence on 3-flash-preview specifically."
1131
+ }
1132
+ ],
1133
+ costInputPer1m: 0.5,
1134
+ costOutputPer1m: 3,
1135
+ lowering: {
1136
+ ...GOOGLE_LOWERING_BASE,
1137
+ // 10× cache discount per Google pricing: $0.05/M cached vs $0.50/M input.
1138
+ cache: { ...GOOGLE_LOWERING_BASE.cache, discount: 0.1 },
1139
+ thinking: { field: "generationConfig.thinkingConfig.thinkingBudget", default: "auto" }
1140
+ },
1141
+ recovery: [
1142
+ {
1143
+ signal: "empty_response_after_tool",
1144
+ action: "retry_with_params",
1145
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
1146
+ maxRetries: 1,
1147
+ reason: "Known: empty after tool result \u2014 retry with thinking off"
1148
+ },
1149
+ {
1150
+ signal: "empty_response",
1151
+ action: "retry_with_params",
1152
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
1153
+ maxRetries: 1,
1154
+ reason: "Empty response \u2014 try with thinking off"
1155
+ },
1156
+ {
1157
+ signal: "malformed_function_call",
1158
+ action: "escalate",
1159
+ reason: "MALFORMED_FUNCTION_CALL maps to stop \u2014 escalate to next target"
1160
+ }
1161
+ ],
1162
+ strengths: ["speed", "volume", "classification", "1m_context", "cost"],
1163
+ weaknesses: ["complex_schemas", "large_tool_sets", "high_context_quality"],
1164
+ notes: "Verified s37 (2026-05-21) against Google docs. Step-change positioning vs 2.5-flash on agentic loops per Google's release notes (Dec 2025). Pricing 1.67\xD7/1.2\xD7 higher than 2.5-flash; cache discount 10\xD7 (vs 4\xD7 for 2.5). Status=preview until brain evidence accumulates.",
1165
+ // Anchored to 2.5-flash archetypePerf as starter, with judgment adjustments
1166
+ // for Google's "step-change on agentic" positioning. Brain evidence (zero
1167
+ // rows today) will replace these starter values.
1168
+ archetypePerf: {
1169
+ hunt: 9,
1170
+ // Inherits 2.5-flash L-040 parallel-tool tier; Google positions 3 as agentic-loop upgrade
1171
+ classify: 7,
1172
+ // Inherits 2.5-flash brain-validated tier (218 rows on 2.5)
1173
+ summarize: 7,
1174
+ // Inherits 2.5-flash; cliff strips tools when present
1175
+ transform: 7,
1176
+ ask: 8,
1177
+ // +1 vs 2.5-flash — sustained-frontier positioning
1178
+ generate: 7,
1179
+ // +1 vs 2.5-flash — agentic coding upgrade per Google
1180
+ plan: 6,
1181
+ // +1 vs 2.5-flash — complex iterations per positioning
1182
+ extract: 6,
1183
+ critique: 5
1184
+ // +1 vs 2.5-flash — but still below Sonnet/Opus reasoning floor
1185
+ }
1186
+ },
1187
+ {
1188
+ // ── Gemini 3.1 Flash-Lite ──
1189
+ // Onboarded 2026-05-16 by auto-onboarder; s37 (2026-05-21) verified
1190
+ // against ai.google.dev/gemini-api/docs/pricing.
1191
+ //
1192
+ // L-081 CATCHES (template clone from 2.5-flash-lite was 2.5-3.75× too cheap):
1193
+ // costInputPer1m 0.10 → 0.25 (template clone undervalued by 2.5×)
1194
+ // costOutputPer1m 0.40 → 1.50 (template clone undervalued by 3.75×)
1195
+ //
1196
+ // Real 3.1-flash-lite is NOT a cost-equivalent successor to 2.5-flash-lite —
1197
+ // it sits between 2.5-flash-lite ($0.10/$0.40) and 2.5-flash ($0.30/$2.50).
1198
+ // Cache discount 10× verified ($0.025/M cached vs $0.25/M input).
1199
+ //
1200
+ // Cliffs are HYPOTHESIZED from 2.5-flash family; brain evidence pending.
1201
+ id: "gemini-3.1-flash-lite",
1202
+ verifiedAgainstDocs: "2026-05-21",
1203
+ provider: "google",
1204
+ status: "preview",
1205
+ maxContextTokens: 1048576,
1206
+ maxOutputTokens: 65536,
1207
+ maxTools: 128,
1208
+ parallelToolCalls: true,
1209
+ structuredOutput: "native",
1210
+ systemPromptMode: "separate",
1211
+ streaming: true,
1212
+ cliffs: [
1213
+ {
1214
+ metric: "input_tokens",
1215
+ threshold: 8e3,
1216
+ action: "downgrade_quality_warning",
1217
+ reason: "Inherited from Flash: quality degrades above ~8K. Smaller model \u2014 likely degrades faster. Re-tune from brain after n\u226520."
1218
+ },
1219
+ {
1220
+ metric: "tool_count",
1221
+ threshold: 10,
1222
+ action: "drop_to_top_relevant",
1223
+ reason: "Conservative: Flash drops at 20, Flash-Lite is smaller \u2014 assume tighter ceiling until brain proves otherwise."
1224
+ },
1225
+ {
1226
+ metric: "thinking_with_short_output",
1227
+ threshold: 1,
1228
+ action: "force_thinking_budget_zero",
1229
+ reason: "Thinking enabled per Google API (thinking: true). Same drain risk as Flash \u2014 thinking tokens consume maxOutputTokens."
1230
+ },
1231
+ {
1232
+ // Strong prior: Flash hit 5/5 empty rate on summarize+tools (s11
1233
+ // trust artifact, kgauto commit 3872832). Flash-Lite shares the
1234
+ // same architectural family — almost certainly inherits this cliff.
1235
+ // Ship the guard preemptively; brain telemetry confirms or relaxes.
1236
+ metric: "tool_count",
1237
+ threshold: 1,
1238
+ whenIntent: "summarize",
1239
+ action: "strip_tools",
1240
+ reason: "Inherited from Flash s11 cliff: summarize+tools \u2192 empty response. Preemptive guard until brain evidence on Flash-Lite specifically."
1241
+ }
1242
+ ],
1243
+ costInputPer1m: 0.25,
1244
+ costOutputPer1m: 1.5,
1245
+ lowering: {
1246
+ ...GOOGLE_LOWERING_BASE,
1247
+ // Cache discount 10× (vs Flash 4×) — Google docs s37: $0.025/M cached vs
1248
+ // $0.25/M input. Material for repeat-prompt workloads (classify shape).
1249
+ cache: { ...GOOGLE_LOWERING_BASE.cache, discount: 0.1 },
1250
+ thinking: { field: "generationConfig.thinkingConfig.thinkingBudget", default: "auto" }
1251
+ },
1252
+ recovery: [
1253
+ {
1254
+ signal: "empty_response_after_tool",
1255
+ action: "retry_with_params",
1256
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
1257
+ maxRetries: 1,
1258
+ reason: "Known on Flash family: empty after tool result \u2014 retry with thinking off."
1259
+ },
1260
+ {
1261
+ signal: "empty_response",
1262
+ action: "retry_with_params",
1263
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
1264
+ maxRetries: 1,
1265
+ reason: "Empty response \u2014 try with thinking off."
1266
+ },
1267
+ {
1268
+ signal: "malformed_function_call",
1269
+ action: "escalate",
1270
+ reason: "MALFORMED_FUNCTION_CALL maps to stop \u2014 escalate to next target."
1271
+ }
1272
+ ],
1273
+ strengths: ["low_cost", "speed", "volume", "classification", "summarize", "1m_context", "cache_friendly"],
1274
+ weaknesses: ["complex_reasoning", "large_tool_sets", "complex_schemas", "structured_output_unproven", "long_context_quality"],
1275
+ notes: "Verified s37 (2026-05-21) against Google docs. Sits between 2.5-flash-lite (cheaper) and 2.5-flash (more expensive) on cost frontier; 2.5\xD7 more expensive than initial template-clone. Cliffs hypothesized from Flash family \u2014 brain evidence pending.",
1276
+ // Tier 2-3 floor for summarize/classify chains at the new (verified) price
1277
+ // point. ZERO brain rows — values are starter hypotheses anchored to
1278
+ // "smaller sibling of Flash at higher cost than 2.5-flash-lite." The first
1279
+ // 50 brain rows per archetype will validate or relax these.
1280
+ archetypePerf: {
1281
+ classify: 6,
1282
+ // starter hypothesis — verify (Flash is 7, lite likely ≤)
1283
+ summarize: 6,
1284
+ // starter hypothesis — verify; cliff strips tools
1285
+ transform: 6,
1286
+ // starter hypothesis — verify
1287
+ ask: 5,
1288
+ hunt: 5,
1289
+ generate: 4,
1290
+ extract: 4,
1291
+ plan: 3,
1292
+ critique: 3
1293
+ }
1294
+ },
1295
+ // ─────────────────────────────────────────────────────────────────────────
1296
+ // Gemini 3.5 Flash — hand-onboarded s37 (2026-05-21)
1297
+ //
1298
+ // Google positioning ("Most intelligent for sustained frontier performance
1299
+ // on agentic and coding tasks" / "particularly effective for rapid agentic
1300
+ // loops involving complex coding cycles and iterations") suggests this is
1301
+ // the Flash-family upgrade specifically aimed at hunt-shape workloads.
1302
+ // Pricing 5× input / 3.6× output vs 2.5-flash — material cost premium.
1303
+ // archetypePerf adjusted +1 vs 2.5-flash on ask/generate/plan/critique
1304
+ // (sustained-frontier positioning); hunt held at 9 inherited from L-040
1305
+ // family parallel-tool tier; brain evidence will validate within 50 rows.
1306
+ //
1307
+ // Cliffs inherited conservatively from 2.5-flash. Google's "sustained
1308
+ // frontier on long-context" positioning suggests the 8K cliff may not
1309
+ // apply to 3.5 — keep as guard until brain evidence shows otherwise.
1310
+ //
1311
+ // Specs verified against:
1312
+ // ai.google.dev/gemini-api/docs/models/gemini-3.5-flash
1313
+ // ai.google.dev/gemini-api/docs/pricing (Standard tier)
1314
+ // ─────────────────────────────────────────────────────────────────────────
1315
+ {
1316
+ id: "gemini-3.5-flash",
1317
+ verifiedAgainstDocs: "2026-05-21",
1318
+ provider: "google",
1319
+ status: "current",
1320
+ maxContextTokens: 1048576,
1321
+ maxOutputTokens: 65536,
1322
+ maxTools: 128,
1323
+ parallelToolCalls: true,
1324
+ structuredOutput: "native",
1325
+ systemPromptMode: "separate",
1326
+ streaming: true,
1327
+ cliffs: [
1328
+ {
1329
+ metric: "input_tokens",
1330
+ threshold: 8e3,
1331
+ action: "downgrade_quality_warning",
1332
+ reason: "Inherited from 2.5-flash guard; Google positions 3.5 as sustained-frontier-on-long-context but brain evidence pending"
1333
+ },
1334
+ {
1335
+ metric: "tool_count",
1336
+ threshold: 20,
1337
+ action: "drop_to_top_relevant",
1338
+ reason: "Inherited from Flash family: tool reliability drops above ~20 (despite 128 hard limit). Validate per (archetype, model) after n\u226520."
1339
+ },
1340
+ {
1341
+ metric: "thinking_with_short_output",
1342
+ threshold: 1,
1343
+ action: "force_thinking_budget_zero",
1344
+ reason: "Thinking mode supported per Google docs; same drain risk as 2.5-flash \u2014 thinking tokens consume maxOutputTokens"
1345
+ },
1346
+ {
1347
+ // Inherited from 2.5-flash s11 trust artifact (5/5 empty rate on
1348
+ // tt-intelligence/summarize/gemini-2.5-flash with tools offered).
1349
+ // Family-likely failure mode for Flash architecture across versions.
1350
+ // Keep preemptively until brain evidence on 3.5-flash specifically.
1351
+ metric: "tool_count",
1352
+ threshold: 1,
1353
+ whenIntent: "summarize",
1354
+ action: "strip_tools",
1355
+ reason: "Inherited from 2.5-flash s11 cliff (kgauto commit 3872832): summarize+tools \u2192 empty response. Preemptive guard until brain evidence on 3.5-flash specifically."
1356
+ }
1357
+ ],
1358
+ costInputPer1m: 1.5,
1359
+ costOutputPer1m: 9,
1360
+ lowering: {
1361
+ ...GOOGLE_LOWERING_BASE,
1362
+ // 10× cache discount per Google pricing: $0.15/M cached vs $1.50/M input.
1363
+ cache: { ...GOOGLE_LOWERING_BASE.cache, discount: 0.1 },
1364
+ thinking: { field: "generationConfig.thinkingConfig.thinkingBudget", default: "auto" }
1365
+ },
1366
+ recovery: [
1367
+ {
1368
+ signal: "empty_response_after_tool",
1369
+ action: "retry_with_params",
1370
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
1371
+ maxRetries: 1,
1372
+ reason: "Inherited Flash-family pattern: empty after tool result \u2014 retry with thinking off"
1373
+ },
1374
+ {
1375
+ signal: "empty_response",
1376
+ action: "retry_with_params",
1377
+ retryParams: { "generationConfig.thinkingConfig.thinkingBudget": 0 },
1378
+ maxRetries: 1,
1379
+ reason: "Empty response \u2014 try with thinking off"
1380
+ },
1381
+ {
1382
+ signal: "malformed_function_call",
1383
+ action: "escalate",
1384
+ reason: "MALFORMED_FUNCTION_CALL maps to stop \u2014 escalate to next target"
1385
+ }
1386
+ ],
1387
+ strengths: ["agentic_loops", "coding", "1m_context", "parallel_tools", "thinking_mode", "sustained_frontier"],
1388
+ weaknesses: ["cost_vs_2_5_flash", "no_brain_evidence_yet"],
1389
+ notes: "Hand-onboarded s37 (2026-05-21) verified against Google docs. Stable status; positioned as Flash-family upgrade for agentic loops and coding. 5\xD7/3.6\xD7 more expensive than 2.5-flash but Google claims step-change on sustained frontier work. archetypePerf adjustments are judgment-grounded starter hypotheses \u2014 brain evidence will validate within ~50 rows per archetype.",
1390
+ // Starter hypothesis: anchored to 2.5-flash archetypePerf with +1
1391
+ // adjustments where Google's positioning explicitly supports
1392
+ // (agentic/coding/sustained). Hunt held at 9 inherited from L-040 family
1393
+ // parallel-tool tier. Brain evidence will replace.
1394
+ archetypePerf: {
1395
+ hunt: 9,
1396
+ // Inherited from 2.5-flash L-040 parallel-tool tier; Google positions 3.5 as agentic-loop champion
1397
+ classify: 7,
1398
+ // Inherited from 2.5-flash brain-validated tier (218 rows on 2.5)
1399
+ summarize: 7,
1400
+ // Inherited from 2.5-flash; cliff strips tools when present
1401
+ transform: 7,
1402
+ ask: 8,
1403
+ // +1 vs 2.5-flash — sustained-frontier positioning
1404
+ generate: 8,
1405
+ // +1 vs 2.5-flash (6→8) — Google: "complex coding cycles and iterations"
1406
+ plan: 7,
1407
+ // +1 vs 2.5-flash (5→7) — "complex iterations" positioning
1408
+ extract: 7,
1409
+ // +1 vs 2.5-flash — sustained-frontier on structured tasks
1410
+ critique: 5
1411
+ // +1 vs 2.5-flash — but still below Sonnet/Opus reasoning floor
1412
+ }
1413
+ },
1414
+ {
1415
+ // Auto-onboarded 2026-07-01 from `claude-sonnet-4-6`; VERIFIED 2026-07-02
1416
+ // against the claude-api reference (cc-portfolio ratification pass). The
1417
+ // clone got context right (1M) and pricing right at sticker ($3/$15 —
1418
+ // NOTE an introductory $2/$10 per MTok runs through 2026-08-31; sticker
1419
+ // encoded here per the time-bounded-pricing convention, intro belongs in
1420
+ // brain kgauto_pricing if worth capturing). The clone got max output WRONG:
1421
+ // Sonnet 5 is 128k, not 4-6's 64k — corrected. New tokenizer (~30% more
1422
+ // tokens for the same text vs 4-6): byte-budget consumers should re-baseline.
1423
+ // API quirks (claude-api ref): (a) NON-DEFAULT temperature/top_p/top_k
1424
+ // return 400 — moot for kgauto's own call() path (ANTHROPIC_LOWERING_BASE
1425
+ // emits no sampling params) but a REAL hazard for compileForAISDKv6
1426
+ // consumers that pass temperature themselves (tt-intel scoring uses temp:0
1427
+ // for determinism — that 400s on this model; noted to consumers via the
1428
+ // contract). (b) Omitting `thinking` runs ADAPTIVE by default (4-6 ran
1429
+ // thinking-off) — output spend shifts. (c) Supports effort xhigh. status:
1430
+ // 'preview' per the Fable precedent — no brain evidence yet; promotion to
1431
+ // 'current' is an explicit call. (L-049/L-081.)
1432
+ id: "claude-sonnet-5",
1433
+ verifiedAgainstDocs: "2026-07-02",
1434
+ provider: "anthropic",
1435
+ status: "preview",
1436
+ maxContextTokens: 1e6,
1437
+ maxOutputTokens: 128e3,
1438
+ maxTools: 64,
1439
+ parallelToolCalls: true,
1440
+ structuredOutput: "grammar",
1441
+ systemPromptMode: "inline",
1442
+ streaming: true,
1443
+ cliffs: [],
1444
+ costInputPer1m: 3,
1445
+ costOutputPer1m: 15,
1446
+ lowering: ANTHROPIC_LOWERING_BASE,
1447
+ recovery: [
1448
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate" },
1449
+ { signal: "model_not_found", action: "escalate", reason: "Deprecated \u2014 escalate (L-061)" }
1450
+ ],
1451
+ strengths: ["quality", "tool_use", "long_context", "cache_friendly", "extended_thinking"],
1452
+ weaknesses: [],
1453
+ notes: "Sonnet 5 (2026-06): near-Opus quality on coding/agentic work at Sonnet cost. Verified 2026-07-02 against the claude-api reference: 1M ctx, 128k out (clone's 64k corrected), $3/$15 sticker (intro $2/$10 through 2026-08-31). New tokenizer ~30% more tokens vs sonnet-4-6. Consumer hazards: non-default temperature/top_p/top_k 400 (temp:0 rejected \u2014 deterministic-scoring consumers must omit); thinking defaults to adaptive when omitted. status:preview \u2014 no brain evidence yet; earns placement via the machinery.",
1454
+ // Master plan §6.2 anchor. Tier 0 for plan/generate/ask/extract/transform
1455
+ // in starter chains; tier 1 cross-provider for hunt/summarize/classify.
1456
+ archetypePerf: {
1457
+ ask: 9,
1458
+ generate: 9,
1459
+ plan: 9,
1460
+ critique: 9,
1461
+ extract: 9,
1462
+ transform: 9,
1463
+ hunt: 7,
1464
+ // strong but Flash beats on parallel tool throughput
1465
+ summarize: 8,
1466
+ // overkill for tolerant archetype
1467
+ classify: 8
1468
+ // overkill
1469
+ }
1470
+ },
1471
+ // ── Z.ai (GLM) ──
1472
+ // alpha.65 (2026-07-18): first Z.ai profile, provider `zai` added same
1473
+ // release. Step-zero sources (L-049/L-081, all fetched 2026-07-18):
1474
+ // docs.z.ai/guides/llm/glm-5.2 (capabilities), docs.z.ai api-reference
1475
+ // chat-completion (wire params: thinking.type enabled|disabled default
1476
+ // enabled, reasoning_effort max..none, tool_stream, max output 131072),
1477
+ // docs.z.ai/guides/overview/pricing ($1.4 in / $4.4 out, cached input
1478
+ // $0.26, cache storage limited-time free), vLLM recipe (native ctx
1479
+ // 1,048,576). NO AI-trained numbers.
1480
+ {
1481
+ id: "glm-5.2",
1482
+ verifiedAgainstDocs: "2026-07-18",
1483
+ provider: "zai",
1484
+ status: "preview",
1485
+ maxContextTokens: 1048576,
1486
+ maxOutputTokens: 131072,
1487
+ // No documented tool-count cap — conservative default pending measured
1488
+ // evidence; matches the deepseek posture, not a doc number.
1489
+ maxTools: 16,
1490
+ // Hosted-API docs document tools + tool_choice + tool_stream but say
1491
+ // NOTHING about multiple tool_calls per response. Conservative false so
1492
+ // hunt-shaped ranking doesn't over-promise; flip on doc or probe
1493
+ // evidence (L-081: undocumented ≠ true).
1494
+ parallelToolCalls: false,
1495
+ structuredOutput: "native",
1496
+ systemPromptMode: "inline",
1497
+ streaming: true,
1498
+ cliffs: [],
1499
+ costInputPer1m: 1.4,
1500
+ costOutputPer1m: 4.4,
1501
+ lowering: {
1502
+ system: { mode: "inline" },
1503
+ // Automatic prefix caching ("intelligent caching mechanism") — no wire
1504
+ // marker to emit, DeepSeek-style. Cache-hit input $0.26/M (~0.19× of
1505
+ // miss) tracked in notes; storage limited-time free as of 2026-07-18.
1506
+ cache: { strategy: "unsupported" },
1507
+ tools: { format: "openai" },
1508
+ // thinking.type: 'enabled' (server default) | 'disabled'. lowerZai
1509
+ // emits an explicit disable only on forceThinkingZero cliffs.
1510
+ thinking: { field: "thinking.type", default: "auto" }
1511
+ },
1512
+ recovery: [
1513
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate" },
1514
+ { signal: "model_not_found", action: "escalate", reason: "Deprecated \u2014 escalate (L-061)" }
1515
+ ],
1516
+ strengths: ["cost", "1m_context", "code", "tool_use", "json_output", "reasoning"],
1517
+ weaknesses: ["parallel_tools"],
1518
+ notes: "GLM-5.2 (Z.ai, 2026-06): agentic-coding flagship. 1,048,576 ctx / 131,072 max out. $1.40/$4.40 per 1M; cached input $0.26/M (automatic caching, no marker; storage limited-time free as of 2026-07-18). Thinking on by default (`thinking.type`), `reasoning_effort` max..none. Parallel tool calls UNDOCUMENTED on the hosted API \u2014 profile says false until doc or probe evidence. status:preview \u2014 no brain evidence yet; earns placement via the machinery.",
1519
+ // Starter hypotheses — verify with telemetry/probes; NO brain evidence
1520
+ // yet. Anchored on the vendor's agentic-coding positioning at a
1521
+ // mid-cost tier, discounted for the unproven parallel-tool story.
1522
+ archetypePerf: {
1523
+ generate: 7,
1524
+ // agentic-coding positioning — starter hypothesis
1525
+ transform: 7,
1526
+ // starter hypothesis
1527
+ plan: 7,
1528
+ // starter hypothesis
1529
+ summarize: 7,
1530
+ // tolerant archetype — starter hypothesis
1531
+ ask: 6,
1532
+ extract: 6,
1533
+ critique: 6,
1534
+ classify: 6,
1535
+ hunt: 5
1536
+ // tools work; parallel unproven → discounted
1537
+ }
1538
+ },
1539
+ // ── Moonshot (Kimi) ──
1540
+ // alpha.65 (2026-07-18): first Moonshot profile, provider `moonshot` added
1541
+ // same release. Step-zero sources (all fetched 2026-07-18):
1542
+ // platform.kimi.ai/docs/pricing/chat-k3 ($3.00 cache-miss in / $0.30
1543
+ // cache-hit in / $15.00 out; 1,048,576 ctx; ToolCalls + JSON Mode +
1544
+ // Partial Mode), kimi-k3-quickstart (max_completion_tokens default
1545
+ // 131072, up to 1,048,576; base https://api.moonshot.ai/v1; vision via
1546
+ // base64 or ms://file-id, NO public image URLs), models-overview
1547
+ // (temperature FIXED 1.0, top_p FIXED 0.95, n FIXED 1, always reasons,
1548
+ // reasoning_effort 'max' only), tool-calls guide ("can choose to call
1549
+ // multiple tools at once … will tend to call them in parallel").
1550
+ {
1551
+ id: "kimi-k3",
1552
+ verifiedAgainstDocs: "2026-07-18",
1553
+ provider: "moonshot",
1554
+ status: "preview",
1555
+ maxContextTokens: 1048576,
1556
+ // Documented ceiling; default is 131,072 when unset.
1557
+ maxOutputTokens: 1048576,
1558
+ // No hard doc cap; docs recommend dynamic tool loading at "dozens or
1559
+ // hundreds" — 64 matches the frontier-agentic posture (sonnet-5 tier).
1560
+ maxTools: 64,
1561
+ parallelToolCalls: true,
1562
+ structuredOutput: "native",
1563
+ systemPromptMode: "inline",
1564
+ streaming: true,
1565
+ cliffs: [],
1566
+ costInputPer1m: 3,
1567
+ costOutputPer1m: 15,
1568
+ lowering: {
1569
+ system: { mode: "inline" },
1570
+ // Automatic context caching — no wire marker. Cache-hit input $0.30/M
1571
+ // (0.1× of miss) tracked in notes.
1572
+ cache: { strategy: "unsupported" },
1573
+ tools: { format: "openai" }
1574
+ // No thinking knob: reasoning is always-on and reasoning_effort
1575
+ // accepts only 'max' (= the server default). Nothing to lower.
1576
+ },
1577
+ recovery: [
1578
+ { signal: "rate_limit", action: "escalate", reason: "429 \u2014 escalate" },
1579
+ { signal: "model_not_found", action: "escalate", reason: "Deprecated \u2014 escalate (L-061)" }
1580
+ ],
1581
+ strengths: ["quality", "reasoning", "1m_context", "parallel_tools", "tool_use", "code", "vision"],
1582
+ // 'latency' → latencyTierOf derives 'slow': always-on max-effort
1583
+ // reasoner with no off switch; no measured p50 yet, the tag is the
1584
+ // honest prior (reasoner family precedent: deepseek-v4-pro ~48s).
1585
+ weaknesses: ["cost", "latency"],
1586
+ notes: "Kimi K3 (Moonshot, API live 2026-07-16): 2.8T-param open-weight flagship, native vision. 1,048,576 ctx; max_completion_tokens default 131,072, ceiling 1,048,576. $3.00/$15.00 per 1M, cache-hit input $0.30/M (automatic caching). Reasoning ALWAYS ON (reasoning_effort max only, cannot disable; responses may carry reasoning_content). Wire quirks: temperature fixed 1.0 / top_p 0.95 / n 1 (kgauto never emits those); vision input must be base64 or ms://file-id \u2014 public image URLs rejected. status:preview \u2014 earns placement via the machinery.",
1587
+ // Starter hypotheses — verify with telemetry/probes; NO brain evidence
1588
+ // yet. Anchored on documented long-horizon-agentic + parallel-tool
1589
+ // strengths; discounted on terse archetypes where always-on reasoning
1590
+ // bills $15/M output for thinking the archetype doesn't need.
1591
+ archetypePerf: {
1592
+ hunt: 8,
1593
+ // parallel tools + long-horizon agentic — starter hypothesis
1594
+ plan: 8,
1595
+ // starter hypothesis
1596
+ generate: 8,
1597
+ // starter hypothesis
1598
+ critique: 7,
1599
+ ask: 7,
1600
+ extract: 7,
1601
+ transform: 7,
1602
+ summarize: 6,
1603
+ // works, but $15/M output reasoning tax on a tolerant archetype
1604
+ classify: 5
1605
+ // always-on reasoning cost + reasoner hedge risk
1606
+ },
1607
+ // Diagnostics-only (no prompt rewrite): reasoner-family hedge risk on
1608
+ // decisive archetypes, carried as a warning until K3-specific probe
1609
+ // evidence exists. The deepseek-reasoner family showed 8/10 judge
1610
+ // rationales citing hedging on classify (exclusion-finding ID 20,
1611
+ // 2026-05-28); K3 is an always-on reasoner and inherits the risk class,
1612
+ // not the conviction.
1613
+ archetypeConventions: [
1614
+ {
1615
+ archetype: "classify",
1616
+ cliffWarning: "kimi-k3 is an always-on reasoner: decisive archetypes (classify) may hedge, and reasoning tokens bill at $15/M output. Family-prior risk (deepseek-reasoner precedent), not K3-measured \u2014 probe before relying on it for classify.",
1617
+ reason: "Reasoner-family prior (exclusion-finding ID 20, 2026-05-28, deepseek-v4-pro on tt-intel/classify). No K3-specific evidence yet \u2014 warning only, no prompt rewrite."
1618
+ }
1619
+ ]
1620
+ }
1621
+ ];
1622
+ var ALIASES = {
1623
+ // DeepSeek's own model routing — both names served by V4-Flash.
1624
+ "deepseek-chat": "deepseek-v4-flash",
1625
+ "deepseek-reasoner": "deepseek-v4-flash",
1626
+ // Legacy kgauto typo — actual API alias is dash-form (alpha.1 had dot).
1627
+ "claude-haiku-4.5": "claude-haiku-4-5"
1628
+ };
1629
+ var brainHook = {};
1630
+ function canonicalId(id) {
1631
+ return brainHook.resolveAlias?.(id) ?? ALIASES[id] ?? id;
1632
+ }
1633
+ var PROFILE_INDEX = new Map(
1634
+ PROFILES_RAW.map((p) => [p.id, p])
1635
+ );
1636
+ function tryGetProfile(id) {
1637
+ const canonical = canonicalId(id);
1638
+ return brainHook.getProfile?.(canonical) ?? PROFILE_INDEX.get(canonical);
1639
+ }
1640
+
1641
+ // src/env.ts
1642
+ var SUPPORTED_PROVIDERS = Object.freeze([
1643
+ "anthropic",
1644
+ "google",
1645
+ "openai",
1646
+ "deepseek",
1647
+ "zai",
1648
+ "moonshot"
1649
+ ]);
1650
+ function isSupportedProvider(p) {
1651
+ return SUPPORTED_PROVIDERS.includes(p);
1652
+ }
1653
+ var PROVIDER_ENV_KEYS = Object.freeze({
1654
+ anthropic: Object.freeze(["ANTHROPIC_API_KEY"]),
1655
+ google: Object.freeze([
1656
+ "GOOGLE_API_KEY",
1657
+ "GEMINI_API_KEY",
1658
+ "GOOGLE_GENERATIVE_AI_API_KEY"
1659
+ ]),
1660
+ openai: Object.freeze(["OPENAI_API_KEY"]),
1661
+ deepseek: Object.freeze(["DEEPSEEK_API_KEY"]),
1662
+ // alpha.65 — Z.ai (GLM family). ZAI_API_KEY is the canonical name; the
1663
+ // Z_AI_API_KEY variant covers the underscore convention some tooling uses
1664
+ // for the brand's "Z.ai" spelling.
1665
+ zai: Object.freeze(["ZAI_API_KEY", "Z_AI_API_KEY"]),
1666
+ // alpha.65 — Moonshot AI (Kimi family). MOONSHOT_API_KEY is canonical
1667
+ // (api.moonshot.ai); KIMI_API_KEY covers the platform.kimi.ai rebrand
1668
+ // surface so consumers keyed under either name resolve.
1669
+ moonshot: Object.freeze(["MOONSHOT_API_KEY", "KIMI_API_KEY"])
1670
+ });
1671
+ function defaultEnv() {
1672
+ return typeof process !== "undefined" && process.env ? process.env : {};
1673
+ }
1674
+ function readKeyValue(raw) {
1675
+ if (raw === void 0) return void 0;
1676
+ const trimmed = raw.trim();
1677
+ return trimmed.length > 0 ? trimmed : void 0;
1678
+ }
1679
+ function resolveProviderKey(provider, opts = {}) {
1680
+ if (!isSupportedProvider(provider)) return void 0;
1681
+ const explicit = readKeyValue(opts.apiKeys?.[provider]);
1682
+ if (explicit) return explicit;
1683
+ const env = opts.envSource ?? defaultEnv();
1684
+ for (const name of PROVIDER_ENV_KEYS[provider]) {
1685
+ const v = readKeyValue(env[name]);
1686
+ if (v) return v;
1687
+ }
1688
+ return void 0;
1689
+ }
1690
+ function isProviderReachable(provider, opts = {}) {
1691
+ return resolveProviderKey(provider, opts) !== void 0;
1692
+ }
1693
+ function isModelReachable(modelId, opts = {}) {
1694
+ const profile = tryGetProfile(modelId);
1695
+ if (!profile) return false;
1696
+ return isProviderReachable(profile.provider, opts);
1697
+ }
1698
+
1699
+ // src/brain-query.ts
1700
+ var FRESH_SNAPSHOT = {
1701
+ data: null,
1702
+ expiresAt: 0,
1703
+ refreshing: false,
1704
+ warned: false
1705
+ };
1706
+ var snapshot = { ...FRESH_SNAPSHOT };
1707
+ var runtime;
1708
+ function createBrainQueryCache(opts) {
1709
+ return () => {
1710
+ const rt = runtime;
1711
+ if (!rt || !rt.enabledTables.has(opts.table)) {
1712
+ return opts.bundledFallback();
1713
+ }
1714
+ const now = Date.now();
1715
+ const stale = snapshot.expiresAt <= now;
1716
+ if (stale && !snapshot.refreshing) {
1717
+ snapshot.refreshing = true;
1718
+ void asyncRefresh(rt);
1719
+ }
1720
+ if (snapshot.data) {
1721
+ const rows = snapshot.data[opts.table];
1722
+ if (Array.isArray(rows) && rows.length > 0) {
1723
+ try {
1724
+ return opts.mapRows(rows);
1725
+ } catch {
1726
+ return opts.bundledFallback();
1727
+ }
1728
+ }
1729
+ }
1730
+ return opts.bundledFallback();
1731
+ };
1732
+ }
1733
+ var pendingRefresh;
1734
+ async function asyncRefresh(rt) {
1735
+ const promise = doRefresh(rt);
1736
+ pendingRefresh = promise;
1737
+ try {
1738
+ await promise;
1739
+ } finally {
1740
+ if (pendingRefresh === promise) pendingRefresh = void 0;
1741
+ }
1742
+ }
1743
+ var DEFAULT_CONFIG_URL = "https://kgauto-dashboard.vercel.app/api/kgauto-v2/config";
1744
+ async function doRefresh(rt) {
1745
+ const url = rt.configEndpoint ?? DEFAULT_CONFIG_URL;
1746
+ try {
1747
+ const res = await rt.fetchImpl(url, { method: "GET" });
1748
+ if (!res.ok) {
1749
+ throw new Error(`brain-query ${res.status}: ${res.statusText}`);
1750
+ }
1751
+ const body = await res.json();
1752
+ if (runtime !== rt) return;
1753
+ snapshot = {
1754
+ data: body,
1755
+ expiresAt: Date.now() + rt.ttlMs,
1756
+ refreshing: false,
1757
+ warned: snapshot.warned
1758
+ };
1759
+ } catch (err) {
1760
+ if (runtime !== rt) return;
1761
+ snapshot.refreshing = false;
1762
+ snapshot.expiresAt = Date.now() + rt.ttlMs;
1763
+ if (!snapshot.warned) {
1764
+ snapshot.warned = true;
1765
+ (rt.onError ?? defaultOnError)(err);
1766
+ }
1767
+ }
1768
+ }
1769
+ function defaultOnError(err) {
1770
+ console.warn("[kgauto] brain-query failed (using bundled fallback):", err);
1771
+ }
1772
+
1773
+ // src/chains-brain.ts
1774
+ function isChainsRow(x) {
1775
+ if (!x || typeof x !== "object") return false;
1776
+ const r = x;
1777
+ return typeof r.archetype === "string" && typeof r.tier === "number" && typeof r.model_id === "string";
1778
+ }
1779
+ function mapRowsToChains(rows) {
1780
+ const grouped = /* @__PURE__ */ new Map();
1781
+ for (const row of rows) {
1782
+ if (!isChainsRow(row)) continue;
1783
+ const list = grouped.get(row.archetype) ?? [];
1784
+ list.push(row);
1785
+ grouped.set(row.archetype, list);
1786
+ }
1787
+ const out = {};
1788
+ for (const [archetype, group] of grouped.entries()) {
1789
+ group.sort((a, b) => a.tier - b.tier);
1790
+ out[archetype] = group.map((r) => r.model_id);
1791
+ }
1792
+ const bundled = getAllStarterChains();
1793
+ for (const archetype of Object.keys(bundled)) {
1794
+ if (!out[archetype]) out[archetype] = bundled[archetype];
1795
+ }
1796
+ return out;
1797
+ }
1798
+ var loadChainsFromBrain = createBrainQueryCache({
1799
+ table: "kgauto_chains",
1800
+ mapRows: mapRowsToChains,
1801
+ bundledFallback: getAllStarterChains
1802
+ });
1803
+
1804
+ // src/fallback.ts
1805
+ var STARTER_CHAINS_GROUNDED = {
1806
+ // Reasoning floor — never degrade. Walk UP on 429 to Opus → cross-provider.
1807
+ critique: [
1808
+ { id: "claude-opus-4-7", grounding: "judgment", reason: "Highest reasoning bar, no degradation tier \u2014 engineer pick, awaiting measured backing" },
1809
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Same-provider walk-down from Opus on 429" },
1810
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor in similar quality bracket" },
1811
+ { id: "gpt-5.5", grounding: "judgment", reason: "alpha.16: third-provider frontier-tier floor (archetypePerf=9)" }
1812
+ ],
1813
+ // alpha.62 (eval spine) — pairwise output comparison. Same reasoning-floor
1814
+ // posture as critique (a verdict from a weak judge is worse than no
1815
+ // verdict); tier 0 anchored on the CURRENT opus (4-8, s64 roster). Strict
1816
+ // JSON output, so every tier declares native structured output.
1817
+ judge: [
1818
+ { id: "claude-opus-4-8", grounding: "judgment", reason: "Highest reasoning bar for pairwise verdicts \u2014 current opus (s64 roster)" },
1819
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Same-provider walk-down on 429" },
1820
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor in similar quality bracket" },
1821
+ { id: "gpt-5.5", grounding: "judgment", reason: "Third-provider frontier-tier floor" }
1822
+ ],
1823
+ // Reasoning matters — Sonnet primary; walk UP to Opus on 429.
1824
+ plan: [
1825
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Reasoning + cost balance \u2014 engineer pick" },
1826
+ { id: "claude-opus-4-7", grounding: "judgment", reason: 'Same-provider walk-UP on 429 (rare exception to "always cheaper")' },
1827
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
1828
+ { id: "deepseek-v4-pro", grounding: "judgment", reason: "Tier 3 cost floor \u2014 no brain evidence yet" },
1829
+ { id: "gpt-5.4", grounding: "judgment", reason: "alpha.33: third-provider tail (archetypePerf=7) \u2014 closes openai-in-default-fallback-chains" }
1830
+ ],
1831
+ // Quality + cost match.
1832
+ generate: [
1833
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality + cost match \u2014 engineer pick" },
1834
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
1835
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
1836
+ { id: "gpt-5.4-mini", grounding: "judgment", reason: "alpha.16: third-provider tail (archetypePerf=7) \u2014 closes mono-Anthropic gap" }
1837
+ ],
1838
+ // ask::sonnet — STARTER_CHAINS calls this "Quality + cost match" but
1839
+ // tt-intel s78 prod data showed 27% empty rate. Labeled 'judgment' until
1840
+ // evidence either validates or refutes the placement.
1841
+ ask: [
1842
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality + cost match \u2014 engineer pick. NOTE: tt-intel s78 prod showed 27% empty rate; placement awaits measurement validation" },
1843
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
1844
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
1845
+ { id: "gpt-5.4-mini", grounding: "judgment", reason: "alpha.16: third-provider tail (archetypePerf=7)" }
1846
+ ],
1847
+ // Structured-output archetype — Flash skipped (alpha.8 MAX_TOKENS cliff,
1848
+ // capability-fact); DeepSeek skipped (no brain evidence).
1849
+ extract: [
1850
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Reliable structured-output anchor \u2014 engineer pick" },
1851
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down with native structured output" },
1852
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor with structured-output support" },
1853
+ { id: "gpt-5.4", grounding: "capability-fact", reason: "alpha.16: third-provider floor \u2014 native structured-output capability (archetypePerf=8)" }
1854
+ ],
1855
+ // Forgiving archetype — Sonnet primary but Flash safely floors it.
1856
+ transform: [
1857
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality anchor \u2014 engineer pick" },
1858
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Same-provider step-down" },
1859
+ { id: "gemini-2.5-pro", grounding: "judgment", reason: "Cross-provider anchor" },
1860
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost floor \u2014 forgiving archetype tolerates Flash" },
1861
+ { id: "gpt-5.4-mini", grounding: "judgment", reason: "alpha.33: third-provider tail (archetypePerf=7) \u2014 closes openai-in-default-fallback-chains" }
1862
+ ],
1863
+ // Parallel-tool throughput champion — Flash leads on the L-040 cliff
1864
+ // (capability-fact: Flash 15-75 parallel calls/step vs DeepSeek 7-8).
1865
+ hunt: [
1866
+ { id: "gemini-2.5-flash", grounding: "capability-fact", reason: "L-040 parallel-tool throughput champion (15-75 calls/step)" },
1867
+ { id: "gemini-2.5-pro", grounding: "capability-fact", reason: "Cross-provider tier 1 with strong parallel-tool support" },
1868
+ { id: "claude-sonnet-4-6", grounding: "judgment", reason: "Quality safety net for blocked-Flash case" },
1869
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Reduced tool budget \u2014 cliff at 16 fires" },
1870
+ { id: "gpt-5.4-mini", grounding: "judgment", reason: "alpha.33: third-provider tail \u2014 OpenAI parallel-tool capable archetype" }
1871
+ ],
1872
+ // Cost-sensitive + tolerant. DeepSeek brain-evidence tier 1.
1873
+ summarize: [
1874
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost-sensitive primary \u2014 engineer pick" },
1875
+ { id: "deepseek-v4-flash", grounding: "measured", reason: "Brain-validated tier 1 for cost-sensitive summarize workloads", n: 169 },
1876
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Quality safety net" },
1877
+ { id: "gemini-2.5-flash-lite", grounding: "judgment", reason: "Emergency floor \u2014 onboarded s22, no brain evidence yet" },
1878
+ { id: "gpt-5.4-nano", grounding: "judgment", reason: "alpha.33: third-provider tail (archetypePerf=6) \u2014 cheapest OpenAI, matches summarize cost-band" }
1879
+ ],
1880
+ // Brain-validated DeepSeek tier 1 (169 rows, 0% empty rate).
1881
+ classify: [
1882
+ { id: "gemini-2.5-flash", grounding: "judgment", reason: "Cost-sensitive primary \u2014 engineer pick" },
1883
+ { id: "deepseek-v4-flash", grounding: "measured", reason: "Brain-validated tier 1 (169 rows, 0% empty rate)", n: 169 },
1884
+ { id: "claude-haiku-4-5", grounding: "judgment", reason: "Quality safety net" },
1885
+ { id: "gemini-2.5-flash-lite", grounding: "judgment", reason: "Cache-discount 10\xD7 floor for repeat-prompt workloads" },
1886
+ { id: "gpt-5.4-nano", grounding: "judgment", reason: "alpha.33: third-provider tail (archetypePerf=6) \u2014 cheapest OpenAI for classify" }
1887
+ ]
1888
+ };
1889
+ var STARTER_CHAINS = (() => {
1890
+ const out = {};
1891
+ for (const [archetype, entries] of Object.entries(STARTER_CHAINS_GROUNDED)) {
1892
+ out[archetype] = entries.map((e) => e.id);
1893
+ }
1894
+ return out;
1895
+ })();
1896
+ var STARTER_CHAINS_BY_MODE_GROUNDED = {
1897
+ hunt: {
1898
+ sequential: [
1899
+ {
1900
+ id: "deepseek-v4-pro",
1901
+ grounding: "judgment",
1902
+ reason: "alpha.20 E3: cheap + good reasoning at single-step granularity; L-040 cliff silenced when sequential \u2014 hypothesis not yet measured"
1903
+ },
1904
+ {
1905
+ id: "deepseek-v4-flash",
1906
+ grounding: "judgment",
1907
+ reason: "Cheapest viable; sibling-provider fallback"
1908
+ },
1909
+ {
1910
+ id: "claude-sonnet-4-6",
1911
+ grounding: "judgment",
1912
+ reason: "Cross-provider safety net \u2014 Sonnet handles sequential agentic loops cleanly"
1913
+ },
1914
+ {
1915
+ id: "gemini-2.5-pro",
1916
+ grounding: "judgment",
1917
+ reason: "Third-provider tail when no DeepSeek key reachable"
1918
+ }
1919
+ ]
1920
+ }
1921
+ };
1922
+ var STARTER_CHAINS_BY_MODE = (() => {
1923
+ const out = {};
1924
+ for (const [archetype, modes] of Object.entries(STARTER_CHAINS_BY_MODE_GROUNDED)) {
1925
+ if (modes?.sequential) {
1926
+ out[archetype] = {
1927
+ sequential: modes.sequential.map((e) => e.id)
1928
+ };
1929
+ }
1930
+ }
1931
+ return out;
1932
+ })();
1933
+ function resolveStarterForMode(archetype, toolOrchestration, allChains) {
1934
+ if (toolOrchestration === "sequential") {
1935
+ const overlay = STARTER_CHAINS_BY_MODE[archetype]?.sequential;
1936
+ if (overlay) return [...overlay];
1937
+ }
1938
+ return allChains[archetype];
1939
+ }
1940
+ function getDefaultFallbackChain(opts) {
1941
+ const { archetype, primary, maxDepth = 4, policy, reachability, toolOrchestration } = opts;
1942
+ if (maxDepth < 1) {
1943
+ throw new Error(
1944
+ `getDefaultFallbackChain: maxDepth must be >= 1, got ${maxDepth}`
1945
+ );
1946
+ }
1947
+ const allChains = loadChainsFromBrain();
1948
+ const starter = resolveStarterForMode(archetype, toolOrchestration, allChains);
1949
+ if (!starter) {
1950
+ throw new Error(
1951
+ `getDefaultFallbackChain: unknown archetype "${archetype}". Known: ${Object.keys(allChains).join(", ")}`
1952
+ );
1953
+ }
1954
+ let chain;
1955
+ if (primary) {
1956
+ chain = [primary, ...starter.filter((id) => id !== primary)];
1957
+ } else {
1958
+ chain = [...starter];
1959
+ }
1960
+ if (policy?.blockedModels && policy.blockedModels.length > 0) {
1961
+ const blocked = new Set(policy.blockedModels);
1962
+ chain = chain.filter((id) => !blocked.has(id));
1963
+ }
1964
+ const seen = /* @__PURE__ */ new Set();
1965
+ const deduped = [];
1966
+ for (const id of chain) {
1967
+ if (!seen.has(id)) {
1968
+ seen.add(id);
1969
+ deduped.push(id);
1970
+ }
1971
+ }
1972
+ let filtered = deduped;
1973
+ if (reachability) {
1974
+ filtered = deduped.filter((id) => isModelReachable(id, reachability));
1975
+ }
1976
+ return filtered.slice(0, maxDepth);
1977
+ }
1978
+ function getAllStarterChains() {
1979
+ const out = {};
1980
+ for (const [archetype, chain] of Object.entries(STARTER_CHAINS)) {
1981
+ out[archetype] = [...chain];
1982
+ }
1983
+ return out;
1984
+ }
1985
+
1986
+ // src/compatibility.ts
1987
+ var ARCHETYPE_FLOOR_DEFAULT = 6;
1988
+
1989
+ // src/glassbox-routes/counterfactuals.ts
1990
+ var COUNTERFACTUAL_MIN_SAVINGS_RATIO = 0.1;
1991
+ var COUNTERFACTUAL_MAX_RESULTS = 2;
1992
+ function computeCounterfactuals(args) {
1993
+ const {
1994
+ servedModel,
1995
+ servedCostUsd,
1996
+ archetype,
1997
+ tokensIn,
1998
+ tokensOut,
1999
+ cacheReadInputTokens = 0,
2000
+ toolOrchestration
2001
+ } = args;
2002
+ if (tokensIn <= 0) return [];
2003
+ if (servedCostUsd <= 0) return [];
2004
+ let chain;
2005
+ try {
2006
+ chain = getDefaultFallbackChain({
2007
+ archetype,
2008
+ posture: "open",
2009
+ maxDepth: 10,
2010
+ toolOrchestration
2011
+ });
2012
+ } catch {
2013
+ return [];
2014
+ }
2015
+ const candidates = [];
2016
+ const minSavings = servedCostUsd * COUNTERFACTUAL_MIN_SAVINGS_RATIO;
2017
+ for (const modelId of chain) {
2018
+ if (modelId === servedModel) continue;
2019
+ const profile = tryGetProfile(modelId);
2020
+ if (!profile) continue;
2021
+ const perf = profile.archetypePerf?.[archetype] ?? 5;
2022
+ if (perf < ARCHETYPE_FLOOR_DEFAULT) continue;
2023
+ const estimated = estimateCostUsd({
2024
+ profile,
2025
+ tokensIn,
2026
+ tokensOut,
2027
+ cacheReadInputTokens
2028
+ });
2029
+ if (estimated === void 0) continue;
2030
+ const savings = servedCostUsd - estimated;
2031
+ if (savings < minSavings) continue;
2032
+ const savingsPercent = Math.round(savings / servedCostUsd * 100);
2033
+ const reason = buildReason({
2034
+ modelId,
2035
+ archetype,
2036
+ perf,
2037
+ profile
2038
+ });
2039
+ candidates.push({
2040
+ modelId,
2041
+ estimatedCostUsd: round6(estimated),
2042
+ savingsUsd: round6(savings),
2043
+ savingsPercent,
2044
+ reason
2045
+ });
2046
+ }
2047
+ candidates.sort((a, b) => a.estimatedCostUsd - b.estimatedCostUsd);
2048
+ return candidates.slice(0, COUNTERFACTUAL_MAX_RESULTS);
2049
+ }
2050
+ function estimateCostUsd(args) {
2051
+ const { profile, tokensIn, tokensOut, cacheReadInputTokens } = args;
2052
+ const cacheableIn = Math.min(cacheReadInputTokens, tokensIn);
2053
+ const nonCachedIn = Math.max(tokensIn - cacheableIn, 0);
2054
+ const discount = profile.lowering.cache.discount ?? 1;
2055
+ const inUsd = nonCachedIn / 1e6 * profile.costInputPer1m + cacheableIn / 1e6 * profile.costInputPer1m * discount;
2056
+ const outUsd = tokensOut / 1e6 * profile.costOutputPer1m;
2057
+ const total = inUsd + outUsd;
2058
+ if (!Number.isFinite(total)) return void 0;
2059
+ if (total < 0) return void 0;
2060
+ return total;
2061
+ }
2062
+ function round6(n) {
2063
+ return Math.round(n * 1e6) / 1e6;
2064
+ }
2065
+ function buildReason(args) {
2066
+ const { modelId, archetype, perf, profile } = args;
2067
+ const hook = profile.strengths?.[0];
2068
+ const suffix = hook ? `, ${hook.replace(/_/g, " ")}` : "";
2069
+ return `${modelId} on ${archetype}: archetypePerf=${perf}${suffix}`;
2070
+ }
2071
+
2072
+ // src/glassbox-routes/projected-cost.ts
2073
+ var INSUFFICIENT_VOLUME_THRESHOLD = 5;
2074
+ var WINDOW_DAYS = 7;
2075
+ async function computeProjectedDailyCost(args) {
2076
+ const {
2077
+ appId,
2078
+ archetype,
2079
+ servedCostUsd,
2080
+ brainEndpoint,
2081
+ brainJwt,
2082
+ brainAnonKey,
2083
+ fetch: fetchImpl
2084
+ } = args;
2085
+ if (!appId || !archetype) return void 0;
2086
+ if (!Number.isFinite(servedCostUsd) || servedCostUsd <= 0) return void 0;
2087
+ const doFetch = fetchImpl ?? ((...a) => globalThis.fetch(...a));
2088
+ const base = brainEndpoint.replace(/\/+$/, "");
2089
+ const cutoffIso = new Date(
2090
+ Date.now() - WINDOW_DAYS * 24 * 60 * 60 * 1e3
2091
+ ).toISOString();
2092
+ const qs = new URLSearchParams();
2093
+ qs.set("app_id", `eq.${appId}`);
2094
+ qs.set("intent_archetype", `eq.${archetype}`);
2095
+ qs.set("created_at", `gte.${cutoffIso}`);
2096
+ qs.set("select", "handle");
2097
+ qs.set("limit", "0");
2098
+ const url = `${base}/rest/v1/compile_outcomes?${qs.toString()}`;
2099
+ let res;
2100
+ try {
2101
+ res = await doFetch(url, {
2102
+ method: "GET",
2103
+ headers: {
2104
+ Authorization: `Bearer ${brainJwt}`,
2105
+ apikey: brainAnonKey,
2106
+ Accept: "application/json",
2107
+ // Triggers PostgREST exact count in Content-Range header.
2108
+ Prefer: "count=exact"
2109
+ }
2110
+ });
2111
+ } catch {
2112
+ return void 0;
2113
+ }
2114
+ if (!res.ok) return void 0;
2115
+ const contentRange = res.headers.get("content-range");
2116
+ const count = parseContentRangeCount(contentRange);
2117
+ if (count === void 0) return void 0;
2118
+ const avgPerDay = count / WINDOW_DAYS;
2119
+ if (avgPerDay < INSUFFICIENT_VOLUME_THRESHOLD) return void 0;
2120
+ const projected = avgPerDay * servedCostUsd;
2121
+ return Math.round(projected * 1e6) / 1e6;
2122
+ }
2123
+ function parseContentRangeCount(header) {
2124
+ if (!header) return void 0;
2125
+ const slash = header.lastIndexOf("/");
2126
+ if (slash < 0) return void 0;
2127
+ const tail = header.slice(slash + 1).trim();
2128
+ if (tail === "*" || tail === "") return void 0;
2129
+ const n = Number.parseInt(tail, 10);
2130
+ if (!Number.isFinite(n) || n < 0) return void 0;
2131
+ return n;
2132
+ }
2133
+
2134
+ // src/glassbox-routes/proxy.ts
2135
+ var JSON_HEADERS2 = {
2136
+ "Content-Type": "application/json",
2137
+ "Cache-Control": "no-store"
2138
+ };
2139
+ var DEFAULT_LIMIT = 20;
2140
+ var MAX_LIMIT = 100;
2141
+ function jsonResponse(status, body) {
2142
+ return new Response(JSON.stringify(body), { status, headers: JSON_HEADERS2 });
2143
+ }
2144
+ function jsonError2(status, code) {
2145
+ return jsonResponse(status, { error: code });
2146
+ }
2147
+ function applyScrub(row, scrub) {
2148
+ if (!scrub || row == null || typeof row !== "object") return row;
2149
+ try {
2150
+ return scrub(row);
2151
+ } catch {
2152
+ return row;
2153
+ }
2154
+ }
2155
+ function parseLimit(raw) {
2156
+ if (!raw) return DEFAULT_LIMIT;
2157
+ const n = Number.parseInt(raw, 10);
2158
+ if (!Number.isFinite(n) || n <= 0) return DEFAULT_LIMIT;
2159
+ return Math.min(n, MAX_LIMIT);
2160
+ }
2161
+ function rowToSummary(row) {
2162
+ return {
2163
+ traceId: typeof row.handle === "string" ? row.handle : "",
2164
+ appId: typeof row.app_id === "string" ? row.app_id : "",
2165
+ archetype: typeof row.intent_archetype === "string" ? row.intent_archetype : "",
2166
+ target: typeof row.model === "string" ? row.model : "",
2167
+ createdAt: typeof row.created_at === "string" ? row.created_at : "",
2168
+ tokensIn: typeof row.tokens_in === "number" ? row.tokens_in : 0,
2169
+ tokensOut: typeof row.tokens_out === "number" ? row.tokens_out : 0,
2170
+ estimatedCostUsd: typeof row.cost_usd_actual === "number" ? row.cost_usd_actual : 0
2171
+ };
2172
+ }
2173
+ var INPUT_RATIO_YELLOW = 0.65;
2174
+ var INPUT_RATIO_RED = 0.85;
2175
+ var CACHE_HEALTH_MIN_TOKENS = 1e3;
2176
+ var CACHE_RATIO_GREEN = 0.5;
2177
+ var CACHE_RATIO_YELLOW = 0.1;
2178
+ var FALLBACK_REASONS = /* @__PURE__ */ new Set([
2179
+ "rate_limit",
2180
+ "provider_auth_failed",
2181
+ "provider_error",
2182
+ "cliff",
2183
+ "cost_cap",
2184
+ "contract_violation"
2185
+ ]);
2186
+ function asString(v) {
2187
+ return typeof v === "string" && v.length > 0 ? v : void 0;
2188
+ }
2189
+ function asNumber(v) {
2190
+ return typeof v === "number" && Number.isFinite(v) ? v : void 0;
2191
+ }
2192
+ function asNumberOrZero(v) {
2193
+ return typeof v === "number" && Number.isFinite(v) ? v : 0;
2194
+ }
2195
+ function asStringArray(v) {
2196
+ if (!Array.isArray(v)) return [];
2197
+ const out = [];
2198
+ for (const e of v) {
2199
+ if (typeof e === "string") out.push(e);
2200
+ }
2201
+ return out;
2202
+ }
2203
+ function asFallbackReason(v) {
2204
+ if (typeof v !== "string") return void 0;
2205
+ const candidate = v;
2206
+ if (candidate && FALLBACK_REASONS.has(candidate)) return candidate;
2207
+ return "provider_error";
2208
+ }
2209
+ function rowToAdvisory(raw) {
2210
+ if (!raw || typeof raw !== "object") return void 0;
2211
+ const r = raw;
2212
+ const level = r.level;
2213
+ const code = r.code;
2214
+ const message = r.message;
2215
+ if (level !== "info" && level !== "warn" && level !== "critical" || typeof code !== "string" || typeof message !== "string") {
2216
+ return void 0;
2217
+ }
2218
+ const out = { level, code, message };
2219
+ const suggestion = asString(r.suggestion);
2220
+ if (suggestion) out.suggestion = suggestion;
2221
+ const docsUrl = asString(r.docs_url ?? r.docsUrl);
2222
+ if (docsUrl) out.docsUrl = docsUrl;
2223
+ const adapter = toAdapter(r.suggested_adaptation ?? r.suggestedAdaptation);
2224
+ if (adapter) out.suggestedAdaptation = adapter;
2225
+ return out;
2226
+ }
2227
+ var SECTION_KINDS = /* @__PURE__ */ new Set([
2228
+ "role_intro",
2229
+ "tool_call_contract",
2230
+ "narration_contract",
2231
+ "discipline_contract",
2232
+ "user_turn",
2233
+ "reference",
2234
+ "arbitrary"
2235
+ ]);
2236
+ function summarizeSectionRewrite(kind, rule) {
2237
+ if (kind === "tool_call_contract" && rule === "sequential-tool-cliff-below-floor") {
2238
+ return "Sequential tool pattern applied (model cliff cleared at compile time).";
2239
+ }
2240
+ if (kind === "narration_contract" && rule === "narration-drift-anthropic") {
2241
+ return "Narration tightened for Anthropic dialect (terse-log shape preserved).";
2242
+ }
2243
+ if (kind === "narration_contract" && rule === "narration-thinking-leak-deepseek") {
2244
+ return "Thinking-block suppression applied (DeepSeek V4 internal reasoning kept off-wire).";
2245
+ }
2246
+ if (kind === "discipline_contract" && rule === "discipline-gates-v1") {
2247
+ return "Discipline gates applied (surface-scaffolding: evidence-before-reasoning, one-extra-signal, source-labeling).";
2248
+ }
2249
+ return `Translator applied rule "${rule}" to ${kind} section.`;
2250
+ }
2251
+ function rowToSectionRewrite(raw) {
2252
+ if (!raw || typeof raw !== "object") return void 0;
2253
+ const r = raw;
2254
+ const sectionId = r.sectionId ?? r.section_id;
2255
+ if (typeof sectionId !== "string" || sectionId.length === 0) return void 0;
2256
+ const kind = r.kind;
2257
+ if (typeof kind !== "string" || !SECTION_KINDS.has(kind)) {
2258
+ return void 0;
2259
+ }
2260
+ const rule = r.rule;
2261
+ if (typeof rule !== "string" || rule.length === 0) return void 0;
2262
+ return {
2263
+ sectionId,
2264
+ kind,
2265
+ rule,
2266
+ summary: summarizeSectionRewrite(kind, rule)
2267
+ };
2268
+ }
2269
+ function toAdapter(raw) {
2270
+ if (!raw || typeof raw !== "object") return void 0;
2271
+ const a = raw;
2272
+ if (a.parameter === "toolOrchestration" && a.value === "sequential" && typeof a.consequence === "string") {
2273
+ return {
2274
+ parameter: "toolOrchestration",
2275
+ value: "sequential",
2276
+ consequence: a.consequence
2277
+ };
2278
+ }
2279
+ return void 0;
2280
+ }
2281
+ function computeHealth(args) {
2282
+ const {
2283
+ tokensIn,
2284
+ tokensOut,
2285
+ historyCacheableTokens,
2286
+ inputCacheHitRatio,
2287
+ fellOverFrom,
2288
+ target
2289
+ } = args;
2290
+ const total = tokensIn + tokensOut;
2291
+ const ratio = total > 0 ? tokensIn / total : 0;
2292
+ let inputRatioStatus;
2293
+ if (ratio > INPUT_RATIO_RED) inputRatioStatus = "red";
2294
+ else if (ratio > INPUT_RATIO_YELLOW) inputRatioStatus = "yellow";
2295
+ else inputRatioStatus = "green";
2296
+ let cacheStatus;
2297
+ if (historyCacheableTokens <= CACHE_HEALTH_MIN_TOKENS) {
2298
+ cacheStatus = "na";
2299
+ } else if (inputCacheHitRatio >= CACHE_RATIO_GREEN) {
2300
+ cacheStatus = "green";
2301
+ } else if (inputCacheHitRatio >= CACHE_RATIO_YELLOW) {
2302
+ cacheStatus = "yellow";
2303
+ } else {
2304
+ cacheStatus = "red";
2305
+ }
2306
+ const fallbackStatus = fellOverFrom !== void 0 && fellOverFrom !== target ? "red" : "green";
2307
+ return { inputRatioStatus, cacheStatus, fallbackStatus };
2308
+ }
2309
+ function rowToDetail(row) {
2310
+ const summary = rowToSummary(row);
2311
+ const tokensIn = summary.tokensIn;
2312
+ const tokensOut = summary.tokensOut;
2313
+ const cacheReadInputTokens = asNumberOrZero(row.cache_read_input_tokens);
2314
+ const cacheCreationInputTokens = asNumberOrZero(
2315
+ row.cache_creation_input_tokens
2316
+ );
2317
+ const historyCacheableTokens = asNumberOrZero(row.history_cacheable_tokens);
2318
+ const inputCacheHitRatio = tokensIn > 0 ? cacheReadInputTokens / tokensIn : 0;
2319
+ const fellOverFrom = asString(row.fell_over_from);
2320
+ const fallbackReasonRaw = row.fallback_reason;
2321
+ const fallbackReason = fellOverFrom ? asFallbackReason(fallbackReasonRaw) : void 0;
2322
+ const requestedModel = asString(row.requested_model) ?? fellOverFrom;
2323
+ const advisoriesRaw = Array.isArray(row.advisories) ? row.advisories : [];
2324
+ const advisories = [];
2325
+ for (const a of advisoriesRaw) {
2326
+ const rec = rowToAdvisory(a);
2327
+ if (rec) advisories.push(rec);
2328
+ }
2329
+ const health = computeHealth({
2330
+ tokensIn,
2331
+ tokensOut,
2332
+ cacheReadInputTokens,
2333
+ historyCacheableTokens,
2334
+ inputCacheHitRatio,
2335
+ fellOverFrom,
2336
+ target: summary.target
2337
+ });
2338
+ const sectionRewritesRaw = Array.isArray(row.section_rewrites_applied) ? row.section_rewrites_applied : [];
2339
+ const sectionRewritesApplied = [];
2340
+ for (const e of sectionRewritesRaw) {
2341
+ const rw = rowToSectionRewrite(e);
2342
+ if (rw) sectionRewritesApplied.push(rw);
2343
+ }
2344
+ const detail = {
2345
+ ...summary,
2346
+ mutationsApplied: asStringArray(row.mutations_applied),
2347
+ advisories,
2348
+ rawRequest: asString(row.prompt_preview),
2349
+ rawResponse: asString(row.response_preview),
2350
+ requestedModel,
2351
+ finishReason: asString(row.finish_reason),
2352
+ ttftMs: asNumber(row.ttft_ms),
2353
+ totalMs: asNumber(row.total_ms) ?? asNumber(row.latency_ms),
2354
+ toolsCount: asNumber(row.tools_count),
2355
+ historyDepth: asNumber(row.history_depth),
2356
+ systemPromptChars: asNumber(row.system_prompt_chars),
2357
+ cacheReadInputTokens,
2358
+ cacheCreationInputTokens,
2359
+ historyCacheableTokens,
2360
+ inputCacheHitRatio,
2361
+ fellOverFrom,
2362
+ fallbackReason,
2363
+ sectionRewritesApplied,
2364
+ health
2365
+ };
2366
+ return detail;
2367
+ }
2368
+ function createProxyHandler(config) {
2369
+ const {
2370
+ installToken,
2371
+ extensionId,
2372
+ brainEndpoint,
2373
+ brainJwt,
2374
+ brainAnonKey,
2375
+ appId,
2376
+ scrub,
2377
+ fetch: fetchImpl
2378
+ } = config;
2379
+ const doFetch = fetchImpl ?? ((...args) => globalThis.fetch(...args));
2380
+ const base = brainEndpoint.replace(/\/+$/, "");
2381
+ return async function proxy(req) {
2382
+ const authFail = checkAuth(req, { installToken, extensionId });
2383
+ if (authFail) return authFail;
2384
+ const url = new URL(req.url);
2385
+ const traceId = url.searchParams.get("traceId");
2386
+ const limit = parseLimit(url.searchParams.get("limit"));
2387
+ const qs = new URLSearchParams();
2388
+ qs.set("app_id", `eq.${appId}`);
2389
+ if (traceId) {
2390
+ qs.set("handle", `eq.${traceId}`);
2391
+ } else {
2392
+ qs.set("order", "created_at.desc");
2393
+ qs.set("limit", String(limit));
2394
+ }
2395
+ const brainUrl = `${base}/rest/v1/compile_outcomes?${qs.toString()}`;
2396
+ let brainRes;
2397
+ try {
2398
+ brainRes = await doFetch(brainUrl, {
2399
+ method: "GET",
2400
+ headers: {
2401
+ // Authorization carries the scoped JWT — drives RLS via app_id claim.
2402
+ Authorization: `Bearer ${brainJwt}`,
2403
+ // apikey MUST be one of the project's known keys (anon or
2404
+ // service_role). Supabase rejects any other JWT here, even when
2405
+ // HS256-signed with the same secret. Pre-alpha.24 this was set to
2406
+ // brainJwt and silently 401'd against real Supabase. See L-117.
2407
+ apikey: brainAnonKey,
2408
+ Accept: "application/json"
2409
+ }
2410
+ });
2411
+ } catch {
2412
+ return jsonError2(502, "brain_unavailable");
2413
+ }
2414
+ if (brainRes.status === 401 || brainRes.status === 403) {
2415
+ return jsonError2(500, "brain_auth_misconfig");
2416
+ }
2417
+ if (brainRes.status >= 500) {
2418
+ return jsonError2(502, "brain_unavailable");
2419
+ }
2420
+ if (!brainRes.ok) {
2421
+ return jsonError2(400, "bad_request");
2422
+ }
2423
+ let rows;
2424
+ try {
2425
+ rows = await brainRes.json();
2426
+ } catch {
2427
+ return jsonError2(502, "brain_unavailable");
2428
+ }
2429
+ if (!Array.isArray(rows)) {
2430
+ return jsonError2(502, "brain_unavailable");
2431
+ }
2432
+ const scrubbed = rows.map(
2433
+ (row) => applyScrub(row, scrub)
2434
+ );
2435
+ if (traceId) {
2436
+ const first = scrubbed[0];
2437
+ if (!first) return jsonError2(404, "not_found");
2438
+ const detail = rowToDetail(first);
2439
+ const counterfactuals = computeCounterfactuals({
2440
+ servedModel: detail.target,
2441
+ servedCostUsd: detail.estimatedCostUsd,
2442
+ archetype: detail.archetype,
2443
+ tokensIn: detail.tokensIn,
2444
+ tokensOut: detail.tokensOut,
2445
+ cacheReadInputTokens: detail.cacheReadInputTokens
2446
+ });
2447
+ detail.counterfactuals = counterfactuals;
2448
+ if (detail.estimatedCostUsd > 0) {
2449
+ const projected = await computeProjectedDailyCost({
2450
+ appId: detail.appId,
2451
+ archetype: detail.archetype,
2452
+ servedCostUsd: detail.estimatedCostUsd,
2453
+ brainEndpoint: base,
2454
+ brainJwt,
2455
+ brainAnonKey,
2456
+ fetch: doFetch
2457
+ });
2458
+ if (projected !== void 0) {
2459
+ detail.projectedDailyCostUsd = projected;
2460
+ }
2461
+ }
2462
+ return jsonResponse(200, detail);
2463
+ }
2464
+ return jsonResponse(200, { traces: scrubbed.map(rowToSummary) });
2465
+ };
2466
+ }
2467
+
2468
+ // src/glassbox-routes/stream.ts
2469
+ var SSE_HEADERS = {
2470
+ "Content-Type": "text/event-stream",
2471
+ "Cache-Control": "no-cache, no-transform",
2472
+ Connection: "keep-alive",
2473
+ "X-Accel-Buffering": "no"
2474
+ };
2475
+ function applyScrub2(event, scrub) {
2476
+ if (!scrub) return event;
2477
+ try {
2478
+ const out = scrub(event);
2479
+ if (out && typeof out === "object" && typeof out.kind === "string" && typeof out.at === "number") {
2480
+ return out;
2481
+ }
2482
+ return event;
2483
+ } catch {
2484
+ return event;
2485
+ }
2486
+ }
2487
+ function sseFrame(eventName, data) {
2488
+ const safeName = eventName.replace(/[\r\n]/g, "");
2489
+ return `event: ${safeName}
2490
+ data: ${JSON.stringify(data)}
2491
+
2492
+ `;
2493
+ }
2494
+ function createStreamHandler(config, subscribe2, subscribeApp2) {
2495
+ const { installToken, extensionId, appId, scrub } = config;
2496
+ return async function stream(req) {
2497
+ const authFail = checkAuth(req, { installToken, extensionId });
2498
+ if (authFail) return authFail;
2499
+ const url = new URL(req.url);
2500
+ const traceId = url.searchParams.get("traceId");
2501
+ const source = traceId ? subscribe2(traceId) : subscribeApp2({ appId });
2502
+ const encoder = new TextEncoder();
2503
+ let sourceReader;
2504
+ let cancelled = false;
2505
+ const body = new ReadableStream({
2506
+ async start(controller) {
2507
+ controller.enqueue(encoder.encode(sseFrame("ready", {})));
2508
+ sourceReader = source.getReader();
2509
+ const signal = req.signal;
2510
+ if (signal) {
2511
+ if (signal.aborted) {
2512
+ cancelled = true;
2513
+ await sourceReader.cancel();
2514
+ try {
2515
+ controller.close();
2516
+ } catch {
2517
+ }
2518
+ return;
2519
+ }
2520
+ signal.addEventListener(
2521
+ "abort",
2522
+ () => {
2523
+ cancelled = true;
2524
+ sourceReader?.cancel().catch(() => {
2525
+ });
2526
+ try {
2527
+ controller.close();
2528
+ } catch {
2529
+ }
2530
+ },
2531
+ { once: true }
2532
+ );
2533
+ }
2534
+ try {
2535
+ while (!cancelled) {
2536
+ const { value, done } = await sourceReader.read();
2537
+ if (done) break;
2538
+ const scrubbed = applyScrub2(value, scrub);
2539
+ controller.enqueue(
2540
+ encoder.encode(sseFrame(scrubbed.kind, scrubbed))
2541
+ );
2542
+ }
2543
+ } catch {
2544
+ } finally {
2545
+ try {
2546
+ controller.close();
2547
+ } catch {
2548
+ }
2549
+ try {
2550
+ sourceReader?.releaseLock();
2551
+ } catch {
2552
+ }
2553
+ }
2554
+ },
2555
+ cancel() {
2556
+ cancelled = true;
2557
+ sourceReader?.cancel().catch(() => {
2558
+ });
2559
+ }
2560
+ });
2561
+ return new Response(body, { status: 200, headers: SSE_HEADERS });
2562
+ };
2563
+ }
2564
+
2565
+ // src/glassbox/types.ts
2566
+ var GLASSBOX_STREAM_TTL_MS = 6e4;
2567
+
2568
+ // src/glassbox/pubsub-memory.ts
2569
+ var MemoryPubSub = class {
2570
+ subscribers = /* @__PURE__ */ new Map();
2571
+ async publish(channelKey, event) {
2572
+ const subs = this.subscribers.get(channelKey);
2573
+ if (!subs || subs.size === 0) return;
2574
+ for (const sub of subs) {
2575
+ if (sub.closed) continue;
2576
+ try {
2577
+ sub.controller.enqueue(event);
2578
+ } catch {
2579
+ sub.closed = true;
2580
+ continue;
2581
+ }
2582
+ this.refreshTtl(channelKey, sub);
2583
+ }
2584
+ }
2585
+ subscribe(channelKey) {
2586
+ const self = this;
2587
+ let sub;
2588
+ return new ReadableStream({
2589
+ start(controller) {
2590
+ sub = {
2591
+ controller,
2592
+ ttlTimer: setTimeout(() => {
2593
+ self.closeSubscriber(channelKey, sub);
2594
+ }, GLASSBOX_STREAM_TTL_MS),
2595
+ closed: false
2596
+ };
2597
+ let set = self.subscribers.get(channelKey);
2598
+ if (!set) {
2599
+ set = /* @__PURE__ */ new Set();
2600
+ self.subscribers.set(channelKey, set);
2601
+ }
2602
+ set.add(sub);
2603
+ },
2604
+ cancel() {
2605
+ if (sub) self.removeSubscriber(channelKey, sub);
2606
+ }
2607
+ });
2608
+ }
2609
+ /**
2610
+ * Refresh the rolling TTL for a subscriber after an event lands. Replaces
2611
+ * the existing timer with a fresh 60s one.
2612
+ */
2613
+ refreshTtl(channelKey, sub) {
2614
+ clearTimeout(sub.ttlTimer);
2615
+ sub.ttlTimer = setTimeout(() => {
2616
+ this.closeSubscriber(channelKey, sub);
2617
+ }, GLASSBOX_STREAM_TTL_MS);
2618
+ }
2619
+ /**
2620
+ * Close the subscriber's stream cleanly and remove from the fan-out set.
2621
+ * Idempotent — safe to call multiple times.
2622
+ */
2623
+ closeSubscriber(channelKey, sub) {
2624
+ if (sub.closed) return;
2625
+ sub.closed = true;
2626
+ clearTimeout(sub.ttlTimer);
2627
+ try {
2628
+ sub.controller.close();
2629
+ } catch {
2630
+ }
2631
+ this.removeSubscriber(channelKey, sub);
2632
+ }
2633
+ removeSubscriber(channelKey, sub) {
2634
+ clearTimeout(sub.ttlTimer);
2635
+ const set = this.subscribers.get(channelKey);
2636
+ if (!set) return;
2637
+ set.delete(sub);
2638
+ if (set.size === 0) this.subscribers.delete(channelKey);
2639
+ }
2640
+ /**
2641
+ * Test-only reset. Tears down all subscribers, clears all state. Calling
2642
+ * outside of tests is harmless but cancels every active stream.
2643
+ */
2644
+ _reset() {
2645
+ for (const [, set] of this.subscribers) {
2646
+ for (const sub of set) {
2647
+ this.closeSubscriber("", sub);
2648
+ }
2649
+ }
2650
+ this.subscribers.clear();
2651
+ }
2652
+ };
2653
+
2654
+ // src/glassbox/pubsub-upstash.ts
2655
+ var UpstashPubSub = class {
2656
+ url;
2657
+ token;
2658
+ fetchImpl;
2659
+ blockMs;
2660
+ maxLen;
2661
+ constructor(cfg) {
2662
+ this.url = cfg.url.replace(/\/$/, "");
2663
+ this.token = cfg.token;
2664
+ this.fetchImpl = cfg.fetchImpl ?? globalThis.fetch.bind(globalThis);
2665
+ this.blockMs = cfg.blockMs ?? 100;
2666
+ this.maxLen = cfg.maxLen ?? 100;
2667
+ }
2668
+ async publish(channelKey, event) {
2669
+ const key = channelKey;
2670
+ const payload = JSON.stringify(event);
2671
+ await this.cmd([
2672
+ "XADD",
2673
+ key,
2674
+ "MAXLEN",
2675
+ "~",
2676
+ String(this.maxLen),
2677
+ "*",
2678
+ "event",
2679
+ payload
2680
+ ]);
2681
+ await this.cmd(["EXPIRE", key, String(Math.ceil(GLASSBOX_STREAM_TTL_MS / 1e3))]);
2682
+ }
2683
+ subscribe(channelKey) {
2684
+ const key = channelKey;
2685
+ const self = this;
2686
+ let cursor = "$";
2687
+ let cancelled = false;
2688
+ let ttlDeadline = Date.now() + GLASSBOX_STREAM_TTL_MS;
2689
+ return new ReadableStream({
2690
+ async start(controller) {
2691
+ try {
2692
+ while (!cancelled && Date.now() < ttlDeadline) {
2693
+ const resp = await self.cmd([
2694
+ "XREAD",
2695
+ "BLOCK",
2696
+ String(self.blockMs),
2697
+ "STREAMS",
2698
+ key,
2699
+ cursor
2700
+ ]);
2701
+ if (cancelled) break;
2702
+ const parsed = parseXReadResult(resp.result);
2703
+ if (parsed.entries.length === 0) {
2704
+ continue;
2705
+ }
2706
+ for (const entry of parsed.entries) {
2707
+ const evt = decodeEvent(entry.fields);
2708
+ if (evt) {
2709
+ try {
2710
+ controller.enqueue(evt);
2711
+ } catch {
2712
+ cancelled = true;
2713
+ break;
2714
+ }
2715
+ }
2716
+ cursor = entry.id;
2717
+ }
2718
+ ttlDeadline = Date.now() + GLASSBOX_STREAM_TTL_MS;
2719
+ }
2720
+ } catch (err) {
2721
+ if (!cancelled) {
2722
+ try {
2723
+ controller.error(err);
2724
+ } catch {
2725
+ }
2726
+ return;
2727
+ }
2728
+ }
2729
+ try {
2730
+ controller.close();
2731
+ } catch {
2732
+ }
2733
+ },
2734
+ cancel() {
2735
+ cancelled = true;
2736
+ }
2737
+ });
2738
+ }
2739
+ async cmd(args) {
2740
+ const res = await this.fetchImpl(this.url, {
2741
+ method: "POST",
2742
+ headers: {
2743
+ Authorization: `Bearer ${this.token}`,
2744
+ "Content-Type": "application/json"
2745
+ },
2746
+ body: JSON.stringify(args)
2747
+ });
2748
+ if (!res.ok) {
2749
+ throw new Error(`Upstash ${args[0]} failed: HTTP ${res.status}`);
2750
+ }
2751
+ const json = await res.json();
2752
+ if (json.error) {
2753
+ throw new Error(`Upstash ${args[0]} failed: ${json.error}`);
2754
+ }
2755
+ return json;
2756
+ }
2757
+ };
2758
+ function traceChannel(traceId) {
2759
+ return `glassbox:trace:${traceId}`;
2760
+ }
2761
+ function appChannel(appId) {
2762
+ return `glassbox:app:${appId}`;
2763
+ }
2764
+ function decodeEvent(fields) {
2765
+ const raw = fields["event"];
2766
+ if (!raw) return void 0;
2767
+ try {
2768
+ const parsed = JSON.parse(raw);
2769
+ if (typeof parsed.kind === "string" && typeof parsed.at === "number") {
2770
+ return parsed;
2771
+ }
2772
+ return void 0;
2773
+ } catch {
2774
+ return void 0;
2775
+ }
2776
+ }
2777
+ function parseXReadResult(raw) {
2778
+ if (!Array.isArray(raw)) return { entries: [] };
2779
+ const entries = [];
2780
+ for (const stream of raw) {
2781
+ if (!Array.isArray(stream) || stream.length < 2) continue;
2782
+ const streamEntries = stream[1];
2783
+ if (!Array.isArray(streamEntries)) continue;
2784
+ for (const entry of streamEntries) {
2785
+ if (!Array.isArray(entry) || entry.length < 2) continue;
2786
+ const id = String(entry[0]);
2787
+ const flat = entry[1];
2788
+ if (!Array.isArray(flat)) continue;
2789
+ const fields = {};
2790
+ for (let i = 0; i < flat.length; i += 2) {
2791
+ const k = flat[i];
2792
+ const v = flat[i + 1];
2793
+ if (typeof k === "string") fields[k] = String(v ?? "");
2794
+ }
2795
+ entries.push({ id, fields });
2796
+ }
2797
+ }
2798
+ return { entries };
2799
+ }
2800
+
2801
+ // src/glassbox/emit.ts
2802
+ var activePubSub;
2803
+ function getPubSub() {
2804
+ if (activePubSub) return activePubSub;
2805
+ const url = readEnv("UPSTASH_REDIS_URL");
2806
+ const token = readEnv("UPSTASH_REDIS_TOKEN");
2807
+ if (url && token) {
2808
+ activePubSub = new UpstashPubSub({ url, token });
2809
+ } else {
2810
+ activePubSub = new MemoryPubSub();
2811
+ }
2812
+ return activePubSub;
2813
+ }
2814
+ function readEnv(key) {
2815
+ try {
2816
+ if (typeof process !== "undefined" && process.env) {
2817
+ const v = process.env[key];
2818
+ return v && v.trim() !== "" ? v : void 0;
2819
+ }
2820
+ } catch {
2821
+ }
2822
+ return void 0;
2823
+ }
2824
+
2825
+ // src/glassbox/subscribe.ts
2826
+ function emptyStream() {
2827
+ return new ReadableStream({
2828
+ start(controller) {
2829
+ controller.close();
2830
+ }
2831
+ });
2832
+ }
2833
+ function subscribe(traceId) {
2834
+ if (!traceId) return emptyStream();
2835
+ return getPubSub().subscribe(traceChannel(traceId));
2836
+ }
2837
+ function subscribeApp({
2838
+ appId
2839
+ }) {
2840
+ if (!appId) return emptyStream();
2841
+ return getPubSub().subscribe(appChannel(appId));
2842
+ }
2843
+
2844
+ // src/glassbox-routes/index.ts
2845
+ function requireString(name, value) {
2846
+ if (typeof value !== "string" || value.length === 0) {
2847
+ throw new Error(`createGlassboxRoutes: ${name} is required`);
2848
+ }
2849
+ return value;
2850
+ }
2851
+ function createGlassboxRoutes(config) {
2852
+ const installToken = requireString("installToken", config.installToken);
2853
+ const extensionId = requireString("extensionId", config.extensionId);
2854
+ const brainEndpoint = requireString("brainEndpoint", config.brainEndpoint);
2855
+ const brainJwt = requireString("brainJwt", config.brainJwt);
2856
+ const brainAnonKey = requireString("brainAnonKey", config.brainAnonKey);
2857
+ const appId = requireString("appId", config.appId);
2858
+ const proxy = createProxyHandler({
2859
+ installToken,
2860
+ extensionId,
2861
+ brainEndpoint,
2862
+ brainJwt,
2863
+ brainAnonKey,
2864
+ appId,
2865
+ scrub: config.scrub,
2866
+ fetch: config.fetch
2867
+ });
2868
+ const stream = createStreamHandler(
2869
+ {
2870
+ installToken,
2871
+ extensionId,
2872
+ appId,
2873
+ scrub: config.scrub
2874
+ },
2875
+ config.subscribe ?? subscribe,
2876
+ config.subscribeApp ?? subscribeApp
2877
+ );
2878
+ return { proxy, stream };
2879
+ }
2880
+ // Annotate the CommonJS export names for ESM import in node:
2881
+ 0 && (module.exports = {
2882
+ COUNTERFACTUAL_MAX_RESULTS,
2883
+ COUNTERFACTUAL_MIN_SAVINGS_RATIO,
2884
+ computeCounterfactuals,
2885
+ createGlassboxRoutes,
2886
+ rowToDetail,
2887
+ rowToSummary
2888
+ });