@warmdrift/kgauto-compiler 2.0.0-alpha.6 → 2.0.0-alpha.60

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 (50) hide show
  1. package/README.md +95 -0
  2. package/dist/brain-proxy.d.mts +113 -0
  3. package/dist/brain-proxy.d.ts +113 -0
  4. package/dist/brain-proxy.js +189 -0
  5. package/dist/brain-proxy.mjs +6 -0
  6. package/dist/chunk-IIMPJZNH.mjs +694 -0
  7. package/dist/chunk-IUWFML6Z.mjs +165 -0
  8. package/dist/chunk-NBO4R5PC.mjs +313 -0
  9. package/dist/chunk-P3TOAEG4.mjs +56 -0
  10. package/dist/chunk-QKXTMVCT.mjs +1470 -0
  11. package/dist/chunk-RO22VFIF.mjs +29 -0
  12. package/dist/chunk-RQ3BUUB6.mjs +190 -0
  13. package/dist/glassbox/index.d.mts +59 -0
  14. package/dist/glassbox/index.d.ts +59 -0
  15. package/dist/glassbox/index.js +312 -0
  16. package/dist/glassbox/index.mjs +12 -0
  17. package/dist/glassbox-routes/format.d.mts +24 -0
  18. package/dist/glassbox-routes/format.d.ts +24 -0
  19. package/dist/glassbox-routes/format.js +86 -0
  20. package/dist/glassbox-routes/format.mjs +18 -0
  21. package/dist/glassbox-routes/index.d.mts +191 -0
  22. package/dist/glassbox-routes/index.d.ts +191 -0
  23. package/dist/glassbox-routes/index.js +2714 -0
  24. package/dist/glassbox-routes/index.mjs +663 -0
  25. package/dist/glassbox-routes/react/index.d.mts +74 -0
  26. package/dist/glassbox-routes/react/index.d.ts +74 -0
  27. package/dist/glassbox-routes/react/index.js +819 -0
  28. package/dist/glassbox-routes/react/index.mjs +754 -0
  29. package/dist/index.d.mts +2175 -17
  30. package/dist/index.d.ts +2175 -17
  31. package/dist/index.js +7248 -1597
  32. package/dist/index.mjs +3428 -164
  33. package/dist/ir-B2h0GAEL.d.ts +1378 -0
  34. package/dist/ir-BC4uDL98.d.mts +1378 -0
  35. package/dist/key-health.d.mts +131 -0
  36. package/dist/key-health.d.ts +131 -0
  37. package/dist/key-health.js +215 -0
  38. package/dist/key-health.mjs +6 -0
  39. package/dist/profiles.d.mts +292 -2
  40. package/dist/profiles.d.ts +292 -2
  41. package/dist/profiles.js +1081 -16
  42. package/dist/profiles.mjs +9 -1
  43. package/dist/types-B-pzBJKf.d.mts +149 -0
  44. package/dist/types-CKuu7Clz.d.ts +149 -0
  45. package/dist/types-CRy90cjp.d.mts +131 -0
  46. package/dist/types-CsB2YASc.d.ts +131 -0
  47. package/package.json +52 -6
  48. package/dist/chunk-MBEI5UOM.mjs +0 -409
  49. package/dist/profiles-CQnLkQ7b.d.ts +0 -611
  50. package/dist/profiles-zm6diETo.d.mts +0 -611
package/dist/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ import {
2
+ createBrainForwardRoutes
3
+ } from "./chunk-IUWFML6Z.mjs";
1
4
  import {
2
5
  ALL_ARCHETYPES,
3
6
  DIALECT_VERSION,
@@ -9,13 +12,445 @@ import {
9
12
  isArchetype,
10
13
  learningKey
11
14
  } from "./chunk-5TI6PNSK.mjs";
15
+ import {
16
+ LIBRARY_VERSION,
17
+ createKeyHealthRoute
18
+ } from "./chunk-RQ3BUUB6.mjs";
19
+ import {
20
+ ABSOLUTE_FLOOR,
21
+ ARCHETYPE_FLOOR_DEFAULT,
22
+ PROVIDER_ENV_KEYS,
23
+ configureBrainQuery,
24
+ createBrainQueryCache,
25
+ ensureCrossProviderTail,
26
+ getAllStarterChains,
27
+ getAllStarterChainsWithGrounding,
28
+ getDefaultFallbackChain,
29
+ getDefaultFallbackChainWithGrounding,
30
+ getModelCompatibility,
31
+ getPerAxisMetrics,
32
+ getReachabilityDiagnostic,
33
+ getSequentialStarterChain,
34
+ getSequentialStarterChainWithGrounding,
35
+ getStarterChain,
36
+ getStarterChainWithGrounding,
37
+ isBrainQueryActiveFor,
38
+ isModelReachable,
39
+ isProviderReachable,
40
+ loadChainsFromBrain,
41
+ readBrainReadEnv,
42
+ resolveProviderKey
43
+ } from "./chunk-IIMPJZNH.mjs";
12
44
  import {
13
45
  ALIASES,
46
+ LATENCY_TIER_MS,
47
+ _setProfileBrainHook,
14
48
  allProfiles,
49
+ allProfilesRaw,
15
50
  getProfile,
51
+ latencyTierOf,
16
52
  profilesByProvider,
17
53
  tryGetProfile
18
- } from "./chunk-MBEI5UOM.mjs";
54
+ } from "./chunk-QKXTMVCT.mjs";
55
+ import {
56
+ emitAdvisoryFired,
57
+ emitCompileDone,
58
+ emitCompileStart,
59
+ emitExecuteAttempt,
60
+ emitExecuteSuccess,
61
+ emitFallbackWalked
62
+ } from "./chunk-NBO4R5PC.mjs";
63
+
64
+ // src/models-brain.ts
65
+ function isModelRow(x) {
66
+ if (!x || typeof x !== "object") return false;
67
+ const r = x;
68
+ return typeof r.model_id === "string" && typeof r.provider === "string";
69
+ }
70
+ function isAliasRow(x) {
71
+ if (!x || typeof x !== "object") return false;
72
+ const r = x;
73
+ return typeof r.alias_id === "string" && typeof r.canonical_id === "string";
74
+ }
75
+ function rowToProfile(row) {
76
+ try {
77
+ if (row.cliffs !== void 0 && row.cliffs !== null && !Array.isArray(row.cliffs)) {
78
+ return null;
79
+ }
80
+ if (row.recovery !== void 0 && row.recovery !== null && !Array.isArray(row.recovery)) {
81
+ return null;
82
+ }
83
+ if (row.lowering !== void 0 && row.lowering !== null && (typeof row.lowering !== "object" || Array.isArray(row.lowering))) {
84
+ return null;
85
+ }
86
+ if (row.archetype_conventions !== void 0 && row.archetype_conventions !== null && !Array.isArray(row.archetype_conventions)) {
87
+ return null;
88
+ }
89
+ return {
90
+ id: row.model_id,
91
+ provider: row.provider,
92
+ status: row.status ?? "current",
93
+ maxContextTokens: row.max_context_tokens ?? 0,
94
+ maxOutputTokens: row.max_output_tokens ?? 0,
95
+ maxTools: row.max_tools ?? 0,
96
+ parallelToolCalls: row.parallel_tool_calls ?? false,
97
+ structuredOutput: row.structured_output ?? "none",
98
+ systemPromptMode: row.system_prompt_mode ?? "inline",
99
+ streaming: row.streaming ?? true,
100
+ cliffs: row.cliffs ?? [],
101
+ costInputPer1m: row.cost_input_per_1m ?? 0,
102
+ costOutputPer1m: row.cost_output_per_1m ?? 0,
103
+ lowering: row.lowering ?? { system: { mode: "inline" }, cache: { strategy: "unsupported" } },
104
+ recovery: row.recovery ?? [],
105
+ strengths: row.strengths ?? [],
106
+ weaknesses: row.weaknesses ?? [],
107
+ notes: row.notes ?? void 0,
108
+ verifiedAgainstDocs: row.verified_against_docs ?? void 0,
109
+ archetypePerf: row.archetype_perf ?? void 0,
110
+ // alpha.41 — family-resolution fields. `family` may be null pre-
111
+ // migration 024; runtime falls back to deriveFamilyFromModelId at
112
+ // resolution time (see family-resolution.ts, G1).
113
+ family: row.family ?? void 0,
114
+ versionAdded: row.version_added ?? void 0,
115
+ active: row.active ?? void 0,
116
+ // alpha.49 — executable-knowledge fields (migration 028). Invalid
117
+ // latency_tier → undefined (latencyTierOf derives from tags; not
118
+ // safety-critical). archetype_conventions already array-validated above.
119
+ latencyTier: normalizeLatencyTier(row.latency_tier),
120
+ archetypeConventions: row.archetype_conventions ?? void 0
121
+ };
122
+ } catch {
123
+ return null;
124
+ }
125
+ }
126
+ var VALID_LATENCY_TIERS = /* @__PURE__ */ new Set(["fast", "medium", "slow"]);
127
+ function normalizeLatencyTier(v) {
128
+ return typeof v === "string" && VALID_LATENCY_TIERS.has(v) ? v : void 0;
129
+ }
130
+ function profileToRow(profile, opts = {}) {
131
+ const row = {
132
+ model_id: profile.id,
133
+ provider: profile.provider,
134
+ status: profile.status,
135
+ max_context_tokens: profile.maxContextTokens,
136
+ max_output_tokens: profile.maxOutputTokens,
137
+ max_tools: profile.maxTools,
138
+ parallel_tool_calls: profile.parallelToolCalls,
139
+ structured_output: profile.structuredOutput,
140
+ system_prompt_mode: profile.systemPromptMode,
141
+ streaming: profile.streaming,
142
+ cliffs: profile.cliffs,
143
+ cost_input_per_1m: profile.costInputPer1m,
144
+ cost_output_per_1m: profile.costOutputPer1m,
145
+ lowering: profile.lowering,
146
+ recovery: profile.recovery,
147
+ strengths: profile.strengths,
148
+ weaknesses: profile.weaknesses,
149
+ notes: profile.notes ?? null,
150
+ archetype_perf: profile.archetypePerf ?? null,
151
+ active: opts.active ?? profile.active ?? true,
152
+ // alpha.41 — round-trip family + version_added when present on profile.
153
+ // version_added is operator-controlled via opts; profile-side value is
154
+ // used only when opts didn't override.
155
+ family: profile.family ?? null,
156
+ // alpha.49 — round-trip the executable-knowledge fields (migration 028)
157
+ // so a reseed from bundled profiles makes the latency lever + schema
158
+ // conventions live warm. Closes the silently-dropped-field gap.
159
+ latency_tier: profile.latencyTier ?? null,
160
+ archetype_conventions: profile.archetypeConventions ?? null
161
+ };
162
+ if (opts.verifiedAgainstDocs !== void 0) {
163
+ row.verified_against_docs = opts.verifiedAgainstDocs;
164
+ } else if (profile.verifiedAgainstDocs !== void 0) {
165
+ const v = profile.verifiedAgainstDocs;
166
+ row.verified_against_docs = /^\d{4}-\d{2}-\d{2}/.test(v) ? v : null;
167
+ }
168
+ if (opts.versionAdded !== void 0) row.version_added = opts.versionAdded;
169
+ else if (profile.versionAdded !== void 0) row.version_added = profile.versionAdded;
170
+ if (opts.versionRemoved !== void 0) row.version_removed = opts.versionRemoved;
171
+ return row;
172
+ }
173
+ function mapRowsToModels(rows) {
174
+ const out = /* @__PURE__ */ new Map();
175
+ for (const row of rows) {
176
+ if (!isModelRow(row)) continue;
177
+ const profile = rowToProfile(row);
178
+ if (profile) out.set(profile.id, profile);
179
+ }
180
+ return out;
181
+ }
182
+ function mapRowsToAliases(rows) {
183
+ const out = {};
184
+ for (const row of rows) {
185
+ if (!isAliasRow(row)) continue;
186
+ out[row.alias_id] = row.canonical_id;
187
+ }
188
+ return out;
189
+ }
190
+ function bundledModels() {
191
+ return new Map(allProfilesRaw().map((p) => [p.id, p]));
192
+ }
193
+ function bundledAliases() {
194
+ return { ...ALIASES };
195
+ }
196
+ var loadModelsFromBrain = createBrainQueryCache({
197
+ table: "kgauto_models",
198
+ mapRows: mapRowsToModels,
199
+ bundledFallback: bundledModels
200
+ });
201
+ var loadAliasesFromBrain = createBrainQueryCache({
202
+ table: "kgauto_aliases",
203
+ mapRows: mapRowsToAliases,
204
+ bundledFallback: bundledAliases
205
+ });
206
+ _setProfileBrainHook({
207
+ getProfile: (canonical) => loadModelsFromBrain().get(canonical),
208
+ resolveAlias: (id) => loadAliasesFromBrain()[id]
209
+ });
210
+
211
+ // src/exclusion-findings-brain.ts
212
+ function isValidVerdict(v) {
213
+ return v === "recommend-probe" || v === "unblock" || v === "stay-excluded" || v === "inconclusive";
214
+ }
215
+ function isValidConfidence(v) {
216
+ return v === "high" || v === "medium" || v === "low";
217
+ }
218
+ function isRawFindingRow(x) {
219
+ if (!x || typeof x !== "object") return false;
220
+ const r = x;
221
+ return typeof r.intent_archetype === "string" && typeof r.excluded_model === "string" && typeof r.excluded_provider === "string" && typeof r.verdict === "string" && typeof r.message === "string" && typeof r.suggestion === "string" && typeof r.confidence === "string";
222
+ }
223
+ function mapRowsToFindings(rows) {
224
+ const out = [];
225
+ for (const row of rows) {
226
+ if (!isRawFindingRow(row)) continue;
227
+ if (!isValidVerdict(row.verdict)) continue;
228
+ if (!isValidConfidence(row.confidence)) continue;
229
+ let savings = null;
230
+ if (typeof row.estimated_savings_usd_30d === "number") {
231
+ savings = Number.isFinite(row.estimated_savings_usd_30d) ? row.estimated_savings_usd_30d : null;
232
+ } else if (typeof row.estimated_savings_usd_30d === "string") {
233
+ const n = Number(row.estimated_savings_usd_30d);
234
+ savings = Number.isFinite(n) ? n : null;
235
+ }
236
+ out.push({
237
+ archetype: row.intent_archetype,
238
+ excludedModel: row.excluded_model,
239
+ excludedProvider: row.excluded_provider,
240
+ verdict: row.verdict,
241
+ estimatedSavingsUsd30d: savings,
242
+ message: row.message,
243
+ suggestion: row.suggestion,
244
+ confidence: row.confidence,
245
+ evidence: row.evidence && typeof row.evidence === "object" ? row.evidence : void 0
246
+ });
247
+ }
248
+ return out;
249
+ }
250
+ var snapshots = /* @__PURE__ */ new Map();
251
+ var runtime;
252
+ var warnedOnce = false;
253
+ var DEFAULT_FINDINGS_ENDPOINT = "https://kgauto-dashboard.vercel.app/api/kgauto-v2/findings/exclusions";
254
+ function configureExclusionFindingsBrain(rt) {
255
+ runtime = rt;
256
+ snapshots.clear();
257
+ warnedOnce = false;
258
+ }
259
+ function isExclusionFindingsBrainActive() {
260
+ return runtime !== void 0;
261
+ }
262
+ function getStaleExclusionFindings(opts) {
263
+ const rt = runtime;
264
+ if (!rt) return [];
265
+ const appId = opts.appId;
266
+ if (!appId) return [];
267
+ let snap = snapshots.get(appId);
268
+ if (!snap) {
269
+ snap = { data: [], expiresAt: 0, refreshing: false };
270
+ snapshots.set(appId, snap);
271
+ }
272
+ const now = Date.now();
273
+ const stale = snap.expiresAt <= now;
274
+ if (stale && !snap.refreshing) {
275
+ snap.refreshing = true;
276
+ void asyncRefresh(rt, appId);
277
+ }
278
+ if (opts.archetype) {
279
+ return snap.data.filter((f) => f.archetype === opts.archetype);
280
+ }
281
+ return snap.data;
282
+ }
283
+ var pendingRefreshes = /* @__PURE__ */ new Map();
284
+ async function asyncRefresh(rt, appId) {
285
+ const promise = doRefresh(rt, appId);
286
+ pendingRefreshes.set(appId, promise);
287
+ try {
288
+ await promise;
289
+ } finally {
290
+ if (pendingRefreshes.get(appId) === promise) {
291
+ pendingRefreshes.delete(appId);
292
+ }
293
+ }
294
+ }
295
+ async function doRefresh(rt, appId) {
296
+ const url = `${rt.endpoint}?app_id=${encodeURIComponent(appId)}`;
297
+ let snap = snapshots.get(appId);
298
+ if (!snap) {
299
+ snap = { data: [], expiresAt: 0, refreshing: false };
300
+ snapshots.set(appId, snap);
301
+ }
302
+ try {
303
+ const res = await rt.fetchImpl(url, { method: "GET" });
304
+ if (!res.ok) {
305
+ throw new Error(`findings ${res.status}: ${res.statusText}`);
306
+ }
307
+ const body = await res.json();
308
+ if (runtime !== rt) return;
309
+ const rows = Array.isArray(body) ? mapRowsToFindings(body) : [];
310
+ snap.data = rows;
311
+ snap.expiresAt = Date.now() + rt.ttlMs;
312
+ snap.refreshing = false;
313
+ } catch (err) {
314
+ if (runtime !== rt) return;
315
+ snap.refreshing = false;
316
+ snap.expiresAt = Date.now() + rt.ttlMs;
317
+ if (!warnedOnce) {
318
+ warnedOnce = true;
319
+ (rt.onError ?? defaultOnError)(err);
320
+ }
321
+ }
322
+ }
323
+ function defaultOnError(err) {
324
+ console.warn(
325
+ "[kgauto] exclusion-findings fetch failed (using empty fallback):",
326
+ err
327
+ );
328
+ }
329
+
330
+ // src/family-resolution.ts
331
+ var FamilyResolutionError = class extends Error {
332
+ family;
333
+ cause;
334
+ constructor(family, cause) {
335
+ super(
336
+ `Family "${family}" did not resolve to any current+active model. ${cause}. Pass a literal model id in ir.models, or call getRecommendedPrimary({ family, fallback }) at IR-construction time.`
337
+ );
338
+ this.name = "FamilyResolutionError";
339
+ this.family = family;
340
+ this.cause = cause;
341
+ }
342
+ };
343
+ function deriveFamilyFromModelId(modelId) {
344
+ if (typeof modelId !== "string" || modelId.length === 0) return null;
345
+ if (modelId.startsWith("claude-opus-")) return "claude-opus";
346
+ if (modelId.startsWith("claude-sonnet-")) return "claude-sonnet";
347
+ if (modelId.startsWith("claude-haiku-")) return "claude-haiku";
348
+ if (modelId.startsWith("gemini-") && modelId.includes("flash-lite")) {
349
+ return "gemini-flash-lite";
350
+ }
351
+ if (modelId.startsWith("gemini-") && modelId.includes("flash")) {
352
+ return "gemini-flash";
353
+ }
354
+ if (modelId.startsWith("gemini-") && modelId.includes("pro")) {
355
+ return "gemini-pro";
356
+ }
357
+ if (modelId.startsWith("deepseek-") && modelId.includes("pro")) {
358
+ return "deepseek-reasoner";
359
+ }
360
+ if (modelId.startsWith("deepseek-")) return "deepseek-chat";
361
+ if (modelId.startsWith("gpt-")) return "openai-gpt";
362
+ return null;
363
+ }
364
+ function familyOf(profile) {
365
+ return profile.family ?? deriveFamilyFromModelId(profile.id);
366
+ }
367
+ function archetypePerfFor(profile, archetype) {
368
+ if (!archetype) return 0;
369
+ const score = profile.archetypePerf?.[archetype];
370
+ return typeof score === "number" ? score : 5;
371
+ }
372
+ function selectCandidates(registry, family, archetype, appId) {
373
+ const out = [];
374
+ const excluded = /* @__PURE__ */ new Set();
375
+ if (appId && archetype) {
376
+ const findings = getStaleExclusionFindings({ appId, archetype });
377
+ for (const f of findings) {
378
+ if (f.verdict === "stay-excluded") excluded.add(f.excludedModel);
379
+ }
380
+ }
381
+ for (const profile of registry.values()) {
382
+ if (familyOf(profile) !== family) continue;
383
+ if (profile.status !== "current") continue;
384
+ if (profile.active === false) continue;
385
+ if (archetype) {
386
+ if (archetypePerfFor(profile, archetype) < ARCHETYPE_FLOOR_DEFAULT) continue;
387
+ }
388
+ if (excluded.has(profile.id)) continue;
389
+ out.push(profile);
390
+ }
391
+ return out;
392
+ }
393
+ function sortCandidates(candidates, archetype) {
394
+ const sorted = [...candidates];
395
+ sorted.sort((a, b) => {
396
+ const perfA = archetypePerfFor(a, archetype);
397
+ const perfB = archetypePerfFor(b, archetype);
398
+ if (perfA !== perfB) return perfB - perfA;
399
+ const vA = a.versionAdded ?? "";
400
+ const vB = b.versionAdded ?? "";
401
+ if (vA !== vB) return vA < vB ? 1 : -1;
402
+ return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
403
+ });
404
+ return sorted;
405
+ }
406
+ function getRecommendedPrimary(opts) {
407
+ if (opts.posture === "locked") return opts.fallback;
408
+ const registry = loadModelsFromBrain();
409
+ const candidates = selectCandidates(
410
+ registry,
411
+ opts.family,
412
+ opts.archetype,
413
+ opts.appId
414
+ );
415
+ if (candidates.length === 0) return opts.fallback;
416
+ const sorted = sortCandidates(candidates, opts.archetype);
417
+ return sorted[0]?.id ?? opts.fallback;
418
+ }
419
+ function resolveFamilyEntry(family, ctx) {
420
+ if (typeof family !== "string" || family.length === 0) {
421
+ throw new FamilyResolutionError(
422
+ String(family),
423
+ "Family tag must be a non-empty string"
424
+ );
425
+ }
426
+ const registry = loadModelsFromBrain();
427
+ const candidates = selectCandidates(
428
+ registry,
429
+ family,
430
+ ctx.archetype,
431
+ ctx.appId
432
+ );
433
+ if (candidates.length === 0) {
434
+ let anyInFamily = false;
435
+ for (const profile of registry.values()) {
436
+ if (familyOf(profile) === family) {
437
+ anyInFamily = true;
438
+ break;
439
+ }
440
+ }
441
+ const cause = anyInFamily ? `Family has registered models but none are current+active${ctx.archetype ? ` at archetype-perf >= ${ARCHETYPE_FLOOR_DEFAULT} for "${ctx.archetype}"` : ""}${ctx.appId ? ` and not excluded for app "${ctx.appId}"` : ""}` : `No models in brain registry carry family="${family}". Check the taxonomy table in family-resolution.ts or migration 024`;
442
+ throw new FamilyResolutionError(family, cause);
443
+ }
444
+ const sorted = sortCandidates(candidates, ctx.archetype);
445
+ const winner = sorted[0];
446
+ if (!winner) {
447
+ throw new FamilyResolutionError(
448
+ family,
449
+ "Candidates non-empty but sort returned undefined \u2014 internal invariant violation"
450
+ );
451
+ }
452
+ return winner.id;
453
+ }
19
454
 
20
455
  // src/tokenizer.ts
21
456
  var tokenizerImpl = defaultCharBasedCounter;
@@ -120,38 +555,96 @@ function passToolRelevance(ir, opts = {}) {
120
555
  ]
121
556
  };
122
557
  }
558
+ function totalHistoryTokens(history) {
559
+ let total = 0;
560
+ for (const m of history) {
561
+ if (typeof m.content === "string") total += countTokens(m.content);
562
+ }
563
+ return total;
564
+ }
123
565
  function passCompressHistory(ir, opts = {}) {
124
566
  const history = ir.history;
125
- if (!history || history.length === 0) return { value: ir, mutations: [] };
567
+ if (!history || history.length === 0) {
568
+ return { value: ir, mutations: [], historyTokensTotal: 0 };
569
+ }
126
570
  const keepRecent = opts.keepRecent ?? 4;
127
571
  const summarizeOlderThan = opts.summarizeOlderThan ?? 8;
128
- if (history.length <= summarizeOlderThan) return { value: ir, mutations: [] };
129
- const cutIndex = history.length - keepRecent;
130
- const old = history.slice(0, cutIndex);
131
- const recent = history.slice(cutIndex);
132
- const userTurns = old.filter((m) => m.role === "user");
133
- const firstUserLine = userTurns[0]?.content.split("\n")[0]?.slice(0, 200) ?? "";
134
- const summary = {
135
- role: "system",
136
- content: `[Earlier conversation: ${old.length} turns omitted. First user message: "${firstUserLine}"]`
137
- };
138
- return {
139
- value: { ...ir, history: [summary, ...recent] },
140
- mutations: [
141
- {
142
- id: `compress-history-${old.length}`,
143
- source: "static_pass",
144
- passName: "compress_history",
145
- description: `Compressed ${old.length} old turns into 1 summary line (kept ${keepRecent} recent)`
572
+ const summarizeAboveTokens = opts.summarizeAboveTokens;
573
+ const historyTokensTotal = totalHistoryTokens(history);
574
+ const countThresholdHit = history.length > summarizeOlderThan;
575
+ const tokenThresholdHit = summarizeAboveTokens !== void 0 && historyTokensTotal > summarizeAboveTokens;
576
+ if (!countThresholdHit && !tokenThresholdHit) {
577
+ return { value: ir, mutations: [], historyTokensTotal };
578
+ }
579
+ if (history.length > keepRecent) {
580
+ const cutIndex = history.length - keepRecent;
581
+ const old = history.slice(0, cutIndex);
582
+ const recent = history.slice(cutIndex);
583
+ const userTurns = old.filter((m) => m.role === "user");
584
+ const firstUserLine = userTurns[0]?.content.split("\n")[0]?.slice(0, 200) ?? "";
585
+ const oldTokens = totalHistoryTokens(old);
586
+ const trigger = tokenThresholdHit && !countThresholdHit ? "tokens" : "count";
587
+ const summary = {
588
+ role: "system",
589
+ content: `[Earlier conversation: ${old.length} turns omitted (~${oldTokens} tokens). First user message: "${firstUserLine}"]`
590
+ };
591
+ return {
592
+ value: { ...ir, history: [summary, ...recent] },
593
+ mutations: [
594
+ {
595
+ id: `compress-history-${old.length}`,
596
+ source: "static_pass",
597
+ passName: "compress_history",
598
+ description: trigger === "tokens" ? `Compressed ${old.length} old turns (~${oldTokens} tokens) into 1 summary \u2014 token threshold ${summarizeAboveTokens} exceeded (kept ${keepRecent} recent)` : `Compressed ${old.length} old turns into 1 summary (kept ${keepRecent} recent)`
599
+ }
600
+ ],
601
+ historyTokensTotal
602
+ };
603
+ }
604
+ if (tokenThresholdHit) {
605
+ let fattestIdx = -1;
606
+ let fattestTokens = 0;
607
+ for (let i = 0; i < history.length; i++) {
608
+ const m = history[i];
609
+ if (!m || typeof m.content !== "string") continue;
610
+ const t = countTokens(m.content);
611
+ if (t > fattestTokens) {
612
+ fattestTokens = t;
613
+ fattestIdx = i;
146
614
  }
147
- ]
148
- };
615
+ }
616
+ const FAT_DOMINANCE_FLOOR = 0.3;
617
+ const fattest = fattestIdx >= 0 ? history[fattestIdx] : void 0;
618
+ if (fattest && historyTokensTotal > 0 && fattestTokens / historyTokensTotal >= FAT_DOMINANCE_FLOOR) {
619
+ const firstLine = fattest.content.split("\n")[0]?.slice(0, 200) ?? "";
620
+ const newContent = `[Earlier ${fattest.role} message content omitted: ~${fattestTokens} tokens. Preview: "${firstLine}"]`;
621
+ const newHistory = history.slice();
622
+ newHistory[fattestIdx] = { ...fattest, content: newContent };
623
+ return {
624
+ value: { ...ir, history: newHistory },
625
+ mutations: [
626
+ {
627
+ id: `compress-fat-message-${fattestIdx}`,
628
+ source: "static_pass",
629
+ passName: "compress_history",
630
+ description: `Replaced fat ${fattest.role} message #${fattestIdx} content (~${fattestTokens} of ${historyTokensTotal} tokens, ${Math.round(fattestTokens / historyTokensTotal * 100)}% of history) with summary stub \u2014 token threshold ${summarizeAboveTokens} exceeded (history.length ${history.length} <= keepRecent ${keepRecent}, slice not possible)`
631
+ }
632
+ ],
633
+ historyTokensTotal
634
+ };
635
+ }
636
+ }
637
+ return { value: ir, mutations: [], historyTokensTotal };
149
638
  }
150
639
  function passApplyCliffs(ir, profile, estimatedInputTokens) {
151
640
  const mutations = [];
152
641
  const hints = { qualityWarning: [] };
153
642
  let nextIR = ir;
643
+ const sequentialMode = nextIR.constraints?.toolOrchestration === "sequential";
154
644
  for (const cliff of profile.cliffs) {
645
+ if (sequentialMode && cliff.reason.includes("L-040")) {
646
+ continue;
647
+ }
155
648
  let triggered = false;
156
649
  switch (cliff.metric) {
157
650
  case "input_tokens":
@@ -243,6 +736,19 @@ function passApplyCliffs(ir, profile, estimatedInputTokens) {
243
736
  }
244
737
  return { value: { ir: nextIR, loweringHints: hints }, mutations };
245
738
  }
739
+ var LATENCY_OVERAGE_WEIGHT = 0.6;
740
+ var LATENCY_PENALTY_CAP = 1;
741
+ var QUALITY_GATE_PENALTY = 4;
742
+ function effectiveConventions(profile) {
743
+ const own = profile.archetypeConventions ?? [];
744
+ if (own.length > 0) return own;
745
+ const family = profile.family ?? deriveFamilyFromModelId(profile.id);
746
+ if (!family) return [];
747
+ const repId = familyRepId(family);
748
+ if (!repId || repId === profile.id) return [];
749
+ const rep = tryGetProfile(repId);
750
+ return rep?.archetypeConventions ?? [];
751
+ }
246
752
  function passScoreTargets(ir, opts) {
247
753
  const constraints = ir.constraints ?? {};
248
754
  const policy = opts.policy ?? {};
@@ -250,7 +756,8 @@ function passScoreTargets(ir, opts) {
250
756
  const preferredSet = new Set(policy.preferredModels ?? []);
251
757
  const scores = [];
252
758
  const policyMutations = [];
253
- for (const modelId of ir.models) {
759
+ const modelIds = ir.models.filter((m) => typeof m === "string");
760
+ for (const modelId of modelIds) {
254
761
  let profile;
255
762
  try {
256
763
  profile = opts.profilesById(modelId);
@@ -294,10 +801,26 @@ function passScoreTargets(ir, opts) {
294
801
  }
295
802
  const baseQuality = profile.strengths.includes("reasoning") ? 0.85 : profile.strengths.includes("quality") ? 0.8 : 0.6;
296
803
  const qualityScore = Math.max(0, baseQuality - qualityPenalty);
297
- const callerOrderBoost = (ir.models.length - ir.models.indexOf(modelId)) * 0.1;
804
+ const callerOrderBoost = (modelIds.length - modelIds.indexOf(modelId)) * 0.1;
298
805
  const costPenalty = estimatedCostUsd * 5;
299
806
  const preferredBoost = preferredSet.has(modelId) ? 0.5 : 0;
300
- const rank = qualityScore + callerOrderBoost - costPenalty - reasons.length * 10 + preferredBoost;
807
+ let latencyPenalty = 0;
808
+ const maxLatencyMs = constraints.maxLatencyMs;
809
+ if (typeof maxLatencyMs === "number" && maxLatencyMs > 0) {
810
+ const tierMs = LATENCY_TIER_MS[latencyTierOf(profile)];
811
+ if (tierMs > maxLatencyMs) {
812
+ const overage = (tierMs - maxLatencyMs) / maxLatencyMs;
813
+ latencyPenalty = Math.min(LATENCY_OVERAGE_WEIGHT * overage, LATENCY_PENALTY_CAP);
814
+ }
815
+ }
816
+ let qualityGatePenalty = 0;
817
+ if (constraints.structuredOutput) {
818
+ const schemaWeak = effectiveConventions(profile).some(
819
+ (c) => c.archetype === ir.intent.archetype && c.structuredOutputHint === "avoid"
820
+ );
821
+ if (schemaWeak) qualityGatePenalty = QUALITY_GATE_PENALTY;
822
+ }
823
+ const rank = qualityScore + callerOrderBoost - costPenalty - reasons.length * 10 + preferredBoost - latencyPenalty - qualityGatePenalty;
301
824
  scores.push({
302
825
  modelId,
303
826
  estimatedCostUsd,
@@ -330,6 +853,23 @@ function passScoreTargets(ir, opts) {
330
853
  description: `Model ${modelId} rank boosted by CompilePolicy.preferredModels`
331
854
  });
332
855
  }
856
+ if (latencyPenalty > 0) {
857
+ const tier = latencyTierOf(profile);
858
+ policyMutations.push({
859
+ id: `latency-downrank-${modelId}`,
860
+ source: "latency_guard",
861
+ passName: "score_targets",
862
+ description: `Model ${modelId} rank down ${latencyPenalty.toFixed(2)} \u2014 latency tier '${tier}' (~${LATENCY_TIER_MS[tier]}ms) exceeds constraints.maxLatencyMs (${maxLatencyMs}ms)`
863
+ });
864
+ }
865
+ if (qualityGatePenalty > 0) {
866
+ policyMutations.push({
867
+ id: `quality-gate-structured-${modelId}`,
868
+ source: "quality_gate",
869
+ passName: "score_targets",
870
+ description: `Model ${modelId} gated below the quality floor for archetype '${ir.intent.archetype}' \u2014 declared structuredOutput + kgauto convention flags it schema-weak for this contract (structuredOutputHint:'avoid'). Down-ranked out of leadership; retained as graceful fallback only.`
871
+ });
872
+ }
333
873
  }
334
874
  return { value: scores, mutations: policyMutations };
335
875
  }
@@ -356,6 +896,175 @@ function simpleHash(s) {
356
896
  }
357
897
  return (h >>> 0).toString(36);
358
898
  }
899
+ function resolveConventionsForProfile(profile) {
900
+ const own = profile.archetypeConventions ?? [];
901
+ const family = profile.family ?? deriveFamilyFromModelId(profile.id);
902
+ if (!family) return own;
903
+ return own;
904
+ }
905
+ function applyArchetypeConvention(promptText, archetype, family) {
906
+ if (typeof promptText !== "string" || promptText.length === 0) return promptText;
907
+ const repId = familyRepId(family);
908
+ if (!repId) return promptText;
909
+ const profile = tryGetProfile(repId);
910
+ if (!profile || !profile.archetypeConventions) return promptText;
911
+ const conventions = profile.archetypeConventions.filter((c) => c.archetype === archetype);
912
+ if (conventions.length === 0) return promptText;
913
+ let next = promptText;
914
+ for (const c of conventions) {
915
+ if (c.promptPrefix) {
916
+ if (!next.startsWith(c.promptPrefix)) {
917
+ next = c.promptPrefix + next;
918
+ }
919
+ }
920
+ if (c.promptSuffix) {
921
+ if (!next.endsWith(c.promptSuffix)) {
922
+ next = next + c.promptSuffix;
923
+ }
924
+ }
925
+ }
926
+ return next;
927
+ }
928
+ function familyRepId(family) {
929
+ switch (family) {
930
+ case "deepseek-reasoner":
931
+ return "deepseek-v4-pro";
932
+ case "deepseek-chat":
933
+ return "deepseek-v4-flash";
934
+ default:
935
+ return void 0;
936
+ }
937
+ }
938
+ function passApplyConventions(ir, profile) {
939
+ const mutations = [];
940
+ const cliffWarnings = [];
941
+ const structuredOutputHints = [];
942
+ const own = profile.archetypeConventions ?? [];
943
+ let effective = own;
944
+ if (effective.length === 0) {
945
+ const family2 = profile.family ?? deriveFamilyFromModelId(profile.id);
946
+ if (family2) {
947
+ const repId = familyRepId(family2);
948
+ if (repId && repId !== profile.id) {
949
+ const repProfile = tryGetProfile(repId);
950
+ if (repProfile && repProfile.archetypeConventions) {
951
+ effective = repProfile.archetypeConventions;
952
+ }
953
+ }
954
+ }
955
+ }
956
+ if (effective.length === 0) {
957
+ return {
958
+ value: { ir, cliffWarnings, structuredOutputHints },
959
+ mutations
960
+ };
961
+ }
962
+ const archetype = ir.intent.archetype;
963
+ const matching = effective.filter((c) => c.archetype === archetype);
964
+ if (matching.length === 0) {
965
+ return {
966
+ value: { ir, cliffWarnings, structuredOutputHints },
967
+ mutations
968
+ };
969
+ }
970
+ const family = profile.family ?? deriveFamilyFromModelId(profile.id) ?? "unknown";
971
+ let nextIR = ir;
972
+ for (const convention of matching) {
973
+ let touched = false;
974
+ if (convention.promptPrefix) {
975
+ const sections = nextIR.sections ?? [];
976
+ const alreadyPresent = sections.some(
977
+ (s) => s.text.includes(convention.promptPrefix)
978
+ );
979
+ if (!alreadyPresent) {
980
+ const prefixSection = {
981
+ id: `convention-prefix-${family}-${archetype}`,
982
+ text: convention.promptPrefix
983
+ };
984
+ nextIR = { ...nextIR, sections: [prefixSection, ...sections] };
985
+ touched = true;
986
+ }
987
+ }
988
+ if (convention.promptSuffix) {
989
+ const history = nextIR.history ?? [];
990
+ let lastUserIdx = -1;
991
+ for (let i = history.length - 1; i >= 0; i--) {
992
+ if (history[i]?.role === "user") {
993
+ lastUserIdx = i;
994
+ break;
995
+ }
996
+ }
997
+ if (lastUserIdx >= 0) {
998
+ const target = history[lastUserIdx];
999
+ if (!target.content.endsWith(convention.promptSuffix)) {
1000
+ const newHistory = history.slice();
1001
+ newHistory[lastUserIdx] = {
1002
+ ...target,
1003
+ content: target.content + convention.promptSuffix
1004
+ };
1005
+ nextIR = { ...nextIR, history: newHistory };
1006
+ touched = true;
1007
+ }
1008
+ } else if (nextIR.currentTurn && nextIR.currentTurn.role === "user") {
1009
+ const target = nextIR.currentTurn;
1010
+ if (!target.content.endsWith(convention.promptSuffix)) {
1011
+ nextIR = {
1012
+ ...nextIR,
1013
+ currentTurn: {
1014
+ ...target,
1015
+ content: target.content + convention.promptSuffix
1016
+ }
1017
+ };
1018
+ touched = true;
1019
+ }
1020
+ } else {
1021
+ nextIR = {
1022
+ ...nextIR,
1023
+ currentTurn: {
1024
+ role: "user",
1025
+ content: convention.promptSuffix.trim()
1026
+ }
1027
+ };
1028
+ touched = true;
1029
+ }
1030
+ }
1031
+ if (convention.structuredOutputHint) {
1032
+ const wantsStructured = !!nextIR.constraints?.structuredOutput;
1033
+ const shouldSurface = convention.structuredOutputHint === "avoid" && wantsStructured || convention.structuredOutputHint === "enforce" && !wantsStructured;
1034
+ if (shouldSurface) {
1035
+ structuredOutputHints.push({
1036
+ archetype,
1037
+ hint: convention.structuredOutputHint,
1038
+ reason: convention.reason
1039
+ });
1040
+ if (convention.cliffWarning) {
1041
+ cliffWarnings.push(`${profile.id}: ${convention.cliffWarning}`);
1042
+ }
1043
+ touched = true;
1044
+ }
1045
+ }
1046
+ if (convention.cliffWarning && !convention.structuredOutputHint) {
1047
+ const toolCount = nextIR.tools?.length ?? 0;
1048
+ const thresholdOk = convention.whenToolCountAtLeast === void 0 || toolCount >= convention.whenToolCountAtLeast;
1049
+ if (thresholdOk) {
1050
+ cliffWarnings.push(`${profile.id}: ${convention.cliffWarning}`);
1051
+ touched = true;
1052
+ }
1053
+ }
1054
+ if (touched) {
1055
+ mutations.push({
1056
+ id: `apply-convention-${family}-${archetype}`,
1057
+ source: "archetype_convention",
1058
+ passName: "apply_conventions",
1059
+ description: `${profile.id}: applied ${family}-family convention for archetype=${archetype} \u2014 ${convention.reason}`
1060
+ });
1061
+ }
1062
+ }
1063
+ return {
1064
+ value: { ir: nextIR, cliffWarnings, structuredOutputHints },
1065
+ mutations
1066
+ };
1067
+ }
359
1068
 
360
1069
  // src/lower.ts
361
1070
  function lower(ir, profile, hints = {}) {
@@ -383,6 +1092,7 @@ function lowerAnthropic(ir, profile, hints) {
383
1092
  const historyCacheableTokens = markIndex >= 0 ? sumHistoryTokens(history, markIndex) : 0;
384
1093
  const totalCacheableTokens = cacheableTokens + historyCacheableTokens;
385
1094
  const cacheSavings = totalCacheableTokens / 1e6 * profile.costInputPer1m * (1 - (profile.lowering.cache.discount ?? 0.1));
1095
+ const toolChoice = hints.wireOverrides?.parallelToolCalls === false && tools && tools.length > 0 ? { type: "auto", disable_parallel_tool_use: true } : void 0;
386
1096
  return {
387
1097
  request: {
388
1098
  provider: "anthropic",
@@ -390,7 +1100,12 @@ function lowerAnthropic(ir, profile, hints) {
390
1100
  system: systemBlocks,
391
1101
  messages,
392
1102
  tools,
393
- max_tokens: hints.forceTerseOutput ? 200 : Math.min(profile.maxOutputTokens, 4096)
1103
+ // alpha.8: trust profile.maxOutputTokens. The historical Math.min(_, 4096)
1104
+ // floor surprised every consumer once (PB-Cairn contract-gaps brief, Gap 3).
1105
+ // Profile is the single source of truth; consumers wanting a tighter
1106
+ // budget can pass providerOverrides.anthropic.max_tokens explicitly.
1107
+ max_tokens: hints.forceTerseOutput ? 200 : profile.maxOutputTokens,
1108
+ tool_choice: toolChoice
394
1109
  },
395
1110
  diagnostics: {
396
1111
  cacheableTokens,
@@ -584,6 +1299,7 @@ function lowerOpenAI(ir, profile, hints) {
584
1299
  const history = (ir.history ?? []).filter((m) => m.role !== "system");
585
1300
  const histMarkIndex = resolveHistoryMarkIndex(history.length, ir.historyCachePolicy);
586
1301
  const historyCacheableTokens = histMarkIndex >= 0 ? sumHistoryTokens(history, histMarkIndex) : 0;
1302
+ const openaiParallelToolCalls = hints.wireOverrides?.parallelToolCalls === false && ir.tools && ir.tools.length > 0 ? false : void 0;
587
1303
  return {
588
1304
  request: {
589
1305
  provider: "openai",
@@ -591,7 +1307,8 @@ function lowerOpenAI(ir, profile, hints) {
591
1307
  messages,
592
1308
  tools: ir.tools && ir.tools.length > 0 ? toOpenAITools(ir.tools) : void 0,
593
1309
  response_format: ir.constraints?.structuredOutput ? { type: "json_object" } : void 0,
594
- reasoning_effort: hints.forceTerseOutput ? "low" : void 0
1310
+ reasoning_effort: hints.forceTerseOutput ? "low" : void 0,
1311
+ parallel_tool_calls: openaiParallelToolCalls
595
1312
  },
596
1313
  diagnostics: {
597
1314
  cacheableTokens: 0,
@@ -668,101 +1385,897 @@ function setNestedField(obj, path, value) {
668
1385
  cursor[parts[parts.length - 1]] = value;
669
1386
  }
670
1387
 
671
- // src/advisor.ts
672
- function runAdvisor(ir, result, profile) {
673
- const out = [];
674
- out.push(...detectCachingOff(ir, profile));
675
- out.push(...detectSingleChunkSystem(ir, profile));
676
- out.push(...detectToolBloat(ir, result));
677
- out.push(...detectHistoryUncached(ir, profile));
1388
+ // src/archetype-perf-brain.ts
1389
+ function isPerfRow(x) {
1390
+ if (!x || typeof x !== "object") return false;
1391
+ const r = x;
1392
+ return typeof r.model_id === "string" && typeof r.archetype === "string" && typeof r.perf_score === "number";
1393
+ }
1394
+ function mapRowsToPerfMap(rows) {
1395
+ const out = /* @__PURE__ */ new Map();
1396
+ for (const row of rows) {
1397
+ if (!isPerfRow(row)) continue;
1398
+ const existing = out.get(row.model_id) ?? {};
1399
+ existing[row.archetype] = row.perf_score;
1400
+ out.set(row.model_id, existing);
1401
+ }
678
1402
  return out;
679
1403
  }
680
- function detectCachingOff(ir, profile) {
681
- if (profile.provider !== "anthropic") return [];
682
- const totalChars = ir.sections.reduce((s, sec) => s + sec.text.length, 0);
683
- if (totalChars < 2e3) return [];
684
- const anyCacheable = ir.sections.some((s) => s.cacheable === true);
685
- if (anyCacheable) return [];
686
- return [
687
- {
688
- level: "warn",
689
- code: "caching-off-on-claude",
690
- message: `System prompt is ${totalChars} chars on Anthropic but no PromptSection has cacheable=true. Anthropic prompt caching cuts cached-prefix input cost by ~90% on subsequent calls; without it, every turn re-pays full price for the static system context.`,
691
- suggestion: "Mark stable system sections (role, persona, tool policy) with `cacheable: true`. The lowering pass concatenates cacheable sections into a single cache-controlled block before the dynamic ones.",
692
- docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
693
- }
694
- ];
1404
+ function mapRowsToNMap(rows) {
1405
+ const out = /* @__PURE__ */ new Map();
1406
+ for (const row of rows) {
1407
+ if (!isPerfRow(row)) continue;
1408
+ if (typeof row.n !== "number") continue;
1409
+ const existing = out.get(row.model_id) ?? {};
1410
+ existing[row.archetype] = row.n;
1411
+ out.set(row.model_id, existing);
1412
+ }
1413
+ return out;
695
1414
  }
696
- function detectSingleChunkSystem(ir, profile) {
697
- if (profile.provider !== "anthropic") return [];
698
- if (ir.sections.length !== 1) return [];
699
- const only = ir.sections[0];
700
- if (!only || only.text.length <= 1e3) return [];
701
- return [
702
- {
703
- level: "info",
704
- code: "single-chunk-system",
705
- message: `System prompt is a single ${only.text.length}-char chunk. Splitting into NamedChunks (static role/persona vs dynamic context) gives the lowering pass a finer cache-marker boundary \u2014 only the static portion needs to be byte-stable for the cache to hit.`,
706
- suggestion: "Refactor the system builder to return an array of `PromptSection` shaped { id, text, cacheable?: boolean }. Static chunks (role, persona, tool policy) get `cacheable: true`; dynamic ones (current context, today's date) don't.",
707
- docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
708
- }
709
- ];
1415
+ function bundledArchetypePerf() {
1416
+ const out = /* @__PURE__ */ new Map();
1417
+ for (const profile of allProfiles()) {
1418
+ if (profile.archetypePerf) out.set(profile.id, profile.archetypePerf);
1419
+ }
1420
+ return out;
710
1421
  }
711
- function detectToolBloat(ir, result) {
712
- const SHORT_OUTPUT = /* @__PURE__ */ new Set([
713
- "classify",
714
- "extract",
715
- "summarize",
716
- "transform",
717
- "critique"
718
- ]);
719
- if (!ir.tools || ir.tools.length === 0) return [];
720
- const toolsKept = result.diagnostics.toolsKept;
721
- if (toolsKept <= 10) return [];
722
- if (!SHORT_OUTPUT.has(ir.intent.archetype)) return [];
723
- return [
724
- {
725
- level: "warn",
726
- code: "tool-bloat",
727
- message: `${toolsKept} tools kept after the relevance pass for archetype="${ir.intent.archetype}" (consumer declared ${ir.tools.length}). This archetype is short-output and rarely needs more than 3 tools; each tool definition eats ~350 tokens of context budget.`,
728
- suggestion: "Tighten `relevanceByIntent: { [archetype]: 0..1 }` per ToolDefinition. Tools below `toolRelevanceThreshold` (default 0.2) get dropped. Without `relevanceByIntent`, every tool defaults to neutral (0.5) and stays.",
729
- docsUrl: "https://github.com/stue/kgauto/blob/main/v2/README.md#tools"
730
- }
731
- ];
1422
+ function bundledArchetypePerfN() {
1423
+ return /* @__PURE__ */ new Map();
732
1424
  }
733
- function detectHistoryUncached(ir, profile) {
734
- if (profile.provider !== "anthropic") return [];
735
- if (!ir.history || ir.history.length < 2) return [];
736
- if (ir.historyCachePolicy && ir.historyCachePolicy.strategy !== "none") {
737
- return [];
1425
+ var loadArchetypePerfFromBrain = createBrainQueryCache({
1426
+ table: "kgauto_archetype_perf",
1427
+ mapRows: mapRowsToPerfMap,
1428
+ bundledFallback: bundledArchetypePerf
1429
+ });
1430
+ var loadArchetypePerfNFromBrain = createBrainQueryCache(
1431
+ {
1432
+ table: "kgauto_archetype_perf",
1433
+ mapRows: mapRowsToNMap,
1434
+ bundledFallback: bundledArchetypePerfN
738
1435
  }
739
- return [
740
- {
741
- level: "warn",
742
- code: "history-uncached-on-claude",
743
- message: `${ir.history.length} history messages on Anthropic with no historyCachePolicy. Every turn re-pays for the full conversation context; with caching, subsequent turns hit the cache at ~10% the input cost.`,
744
- suggestion: "Set `historyCachePolicy: { strategy: 'all-but-latest' }` on this IR. The lowering pass marks the message immediately preceding currentTurn with cache_control; subsequent turns whose history prefix matches byte-for-byte hit the cache.",
745
- docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
746
- }
747
- ];
1436
+ );
1437
+ var MEASURED_GROUNDING_MIN_N = 10;
1438
+ function getArchetypePerfScore(modelId, archetype) {
1439
+ const score = loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
1440
+ const n = loadArchetypePerfNFromBrain().get(modelId)?.[archetype] ?? 0;
1441
+ const grounding = n >= MEASURED_GROUNDING_MIN_N ? "measured" : "judgment";
1442
+ return { score, n, grounding };
748
1443
  }
749
1444
 
750
- // src/compile.ts
751
- var counter = 0;
752
- function makeHandle() {
753
- counter = (counter + 1) % 1e6;
1445
+ // src/promote-ready-brain.ts
1446
+ function isRawPromoteReadyRow(x) {
1447
+ if (!x || typeof x !== "object") return false;
1448
+ const r = x;
1449
+ return typeof r.intent_archetype === "string" && typeof r.family === "string" && typeof r.candidate_model === "string" && typeof r.current_model === "string" && typeof r.detected_at === "string";
1450
+ }
1451
+ function coerceNumber(v) {
1452
+ if (typeof v === "number") return Number.isFinite(v) ? v : null;
1453
+ if (typeof v === "string") {
1454
+ const n = Number(v);
1455
+ return Number.isFinite(n) ? n : null;
1456
+ }
1457
+ return null;
1458
+ }
1459
+ function mapRowsToFindings2(rows) {
1460
+ const out = [];
1461
+ for (const row of rows) {
1462
+ if (!isRawPromoteReadyRow(row)) continue;
1463
+ const sampleN = coerceNumber(row.sample_n);
1464
+ const passRate = coerceNumber(row.judge_pass_rate);
1465
+ const avgScore = coerceNumber(row.judge_avg_score);
1466
+ if (sampleN === null || passRate === null || avgScore === null) continue;
1467
+ out.push({
1468
+ archetype: row.intent_archetype,
1469
+ family: row.family,
1470
+ candidateModel: row.candidate_model,
1471
+ currentModel: row.current_model,
1472
+ sampleN,
1473
+ judgePassRate: passRate,
1474
+ judgeAvgScore: avgScore,
1475
+ costDeltaPct: coerceNumber(row.cost_delta_pct),
1476
+ detectedAt: row.detected_at
1477
+ });
1478
+ }
1479
+ return out;
1480
+ }
1481
+ var snapshots2 = /* @__PURE__ */ new Map();
1482
+ var runtime2;
1483
+ var warnedOnce2 = false;
1484
+ function isPromoteReadyBrainActive() {
1485
+ return runtime2 !== void 0;
1486
+ }
1487
+ function loadPromoteReadyFindings(opts) {
1488
+ const rt = runtime2;
1489
+ if (!rt) return [];
1490
+ const appId = opts.appId;
1491
+ if (!appId) return [];
1492
+ let snap = snapshots2.get(appId);
1493
+ if (!snap) {
1494
+ snap = { data: [], expiresAt: 0, refreshing: false };
1495
+ snapshots2.set(appId, snap);
1496
+ }
1497
+ const now = Date.now();
1498
+ const stale = snap.expiresAt <= now;
1499
+ if (stale && !snap.refreshing) {
1500
+ snap.refreshing = true;
1501
+ void asyncRefresh2(rt, appId);
1502
+ }
1503
+ let rows = snap.data;
1504
+ if (opts.archetype) {
1505
+ rows = rows.filter((f) => f.archetype === opts.archetype);
1506
+ }
1507
+ if (opts.family) {
1508
+ rows = rows.filter((f) => f.family === opts.family);
1509
+ }
1510
+ return rows;
1511
+ }
1512
+ var pendingRefreshes2 = /* @__PURE__ */ new Map();
1513
+ async function asyncRefresh2(rt, appId) {
1514
+ const promise = doRefresh2(rt, appId);
1515
+ pendingRefreshes2.set(appId, promise);
1516
+ try {
1517
+ await promise;
1518
+ } finally {
1519
+ if (pendingRefreshes2.get(appId) === promise) {
1520
+ pendingRefreshes2.delete(appId);
1521
+ }
1522
+ }
1523
+ }
1524
+ async function doRefresh2(rt, appId) {
1525
+ const url = `${rt.endpoint}?app_id=${encodeURIComponent(appId)}`;
1526
+ let snap = snapshots2.get(appId);
1527
+ if (!snap) {
1528
+ snap = { data: [], expiresAt: 0, refreshing: false };
1529
+ snapshots2.set(appId, snap);
1530
+ }
1531
+ try {
1532
+ const res = await rt.fetchImpl(url, { method: "GET" });
1533
+ if (!res.ok) {
1534
+ throw new Error(`promote-ready ${res.status}: ${res.statusText}`);
1535
+ }
1536
+ const body = await res.json();
1537
+ if (runtime2 !== rt) return;
1538
+ const rows = Array.isArray(body) ? mapRowsToFindings2(body) : [];
1539
+ snap.data = rows;
1540
+ snap.expiresAt = Date.now() + rt.ttlMs;
1541
+ snap.refreshing = false;
1542
+ } catch (err) {
1543
+ if (runtime2 !== rt) return;
1544
+ snap.refreshing = false;
1545
+ snap.expiresAt = Date.now() + rt.ttlMs;
1546
+ if (!warnedOnce2) {
1547
+ warnedOnce2 = true;
1548
+ (rt.onError ?? defaultOnError2)(err);
1549
+ }
1550
+ }
1551
+ }
1552
+ function defaultOnError2(err) {
1553
+ console.warn(
1554
+ "[kgauto] promote-ready fetch failed (using empty fallback):",
1555
+ err
1556
+ );
1557
+ }
1558
+ function resolveFetchImpl(injected) {
1559
+ return injected ?? ((...args) => globalThis.fetch(...args));
1560
+ }
1561
+ function normalizeEndpoint(endpoint) {
1562
+ return endpoint.replace(/\/+$/, "");
1563
+ }
1564
+ async function markPromoteReadyHandled(opts) {
1565
+ const {
1566
+ appId,
1567
+ archetype,
1568
+ family,
1569
+ resolution,
1570
+ resolutionNote,
1571
+ brainEndpoint,
1572
+ brainJwt,
1573
+ brainAnonKey,
1574
+ fetch: injectedFetch
1575
+ } = opts;
1576
+ if (!appId) return { ok: false, reason: "app_id_required" };
1577
+ if (!archetype) return { ok: false, reason: "archetype_required" };
1578
+ if (!family) return { ok: false, reason: "family_required" };
1579
+ if (resolution !== "promoted" && resolution !== "declined" && resolution !== "still-evaluating") {
1580
+ return { ok: false, reason: "resolution_invalid" };
1581
+ }
1582
+ const doFetch = resolveFetchImpl(injectedFetch);
1583
+ const base = normalizeEndpoint(brainEndpoint);
1584
+ const url = `${base}/rest/v1/promote_ready_findings?app_id=eq.${encodeURIComponent(appId)}&intent_archetype=eq.${encodeURIComponent(archetype)}&family=eq.${encodeURIComponent(family)}&resolved_at=is.null`;
1585
+ const patchBody = {
1586
+ resolved_at: (/* @__PURE__ */ new Date()).toISOString(),
1587
+ resolution
1588
+ };
1589
+ if (resolutionNote !== void 0) {
1590
+ patchBody.resolution_note = resolutionNote;
1591
+ }
1592
+ let res;
1593
+ try {
1594
+ res = await doFetch(url, {
1595
+ method: "PATCH",
1596
+ headers: {
1597
+ Authorization: `Bearer ${brainJwt}`,
1598
+ apikey: brainAnonKey,
1599
+ "Content-Type": "application/json",
1600
+ Accept: "application/json",
1601
+ Prefer: "return=minimal"
1602
+ },
1603
+ body: JSON.stringify(patchBody)
1604
+ });
1605
+ } catch (err) {
1606
+ const msg = err instanceof Error ? err.message : String(err);
1607
+ return { ok: false, reason: `network_error:${msg}` };
1608
+ }
1609
+ if (res.status === 401 || res.status === 403) {
1610
+ return { ok: false, reason: "brain_auth_misconfig" };
1611
+ }
1612
+ if (res.status >= 500) {
1613
+ return { ok: false, reason: "brain_unavailable" };
1614
+ }
1615
+ if (!res.ok) {
1616
+ return { ok: false, reason: `patch_failed:${res.status}` };
1617
+ }
1618
+ return { ok: true };
1619
+ }
1620
+
1621
+ // src/advisor-rules/promote-ready.ts
1622
+ var PROMOTE_READY_THRESHOLDS = {
1623
+ minPassRate: 0.8,
1624
+ minAvgScore: 4
1625
+ };
1626
+ function shouldFirePromoteReady(finding, resolvedPrimary) {
1627
+ if (finding.currentModel !== resolvedPrimary) return false;
1628
+ if (finding.judgePassRate < PROMOTE_READY_THRESHOLDS.minPassRate) return false;
1629
+ if (finding.judgeAvgScore < PROMOTE_READY_THRESHOLDS.minAvgScore) return false;
1630
+ return true;
1631
+ }
1632
+ function deriveFamilyLocal(modelId) {
1633
+ if (modelId.startsWith("claude-opus-")) return "claude-opus";
1634
+ if (modelId.startsWith("claude-sonnet-")) return "claude-sonnet";
1635
+ if (modelId.startsWith("claude-haiku-")) return "claude-haiku";
1636
+ if (/^gemini-.*-flash-lite/.test(modelId)) return "gemini-flash-lite";
1637
+ if (/^gemini-.*-flash/.test(modelId)) return "gemini-flash";
1638
+ if (/^gemini-.*-pro/.test(modelId)) return "gemini-pro";
1639
+ if (/^deepseek-.*-pro/.test(modelId)) return "deepseek-reasoner";
1640
+ if (modelId.startsWith("deepseek-")) return "deepseek-chat";
1641
+ if (modelId.startsWith("gpt-")) return "openai-gpt";
1642
+ return null;
1643
+ }
1644
+ function advisorRulePromoteReady(ctx) {
1645
+ if (!isPromoteReadyBrainActive()) return [];
1646
+ if (!ctx.appId) return [];
1647
+ if (!ctx.resolvedPrimary) return [];
1648
+ const family = deriveFamilyLocal(ctx.resolvedPrimary);
1649
+ if (!family) return [];
1650
+ const findings = loadPromoteReadyFindings({
1651
+ appId: ctx.appId,
1652
+ archetype: ctx.archetype,
1653
+ family
1654
+ });
1655
+ if (findings.length === 0) return [];
1656
+ const qualifying = findings.filter(
1657
+ (f) => shouldFirePromoteReady(f, ctx.resolvedPrimary)
1658
+ );
1659
+ if (qualifying.length === 0) return [];
1660
+ qualifying.sort((a, b) => {
1661
+ if (a.judgeAvgScore !== b.judgeAvgScore) {
1662
+ return b.judgeAvgScore - a.judgeAvgScore;
1663
+ }
1664
+ return b.judgePassRate - a.judgePassRate;
1665
+ });
1666
+ const top = qualifying[0];
1667
+ const pctPass = Math.round(top.judgePassRate * 100);
1668
+ const score = top.judgeAvgScore.toFixed(2);
1669
+ let costClause = "";
1670
+ if (top.costDeltaPct !== null) {
1671
+ const sign = top.costDeltaPct < 0 ? "cheaper" : "more expensive";
1672
+ const magnitude = Math.abs(top.costDeltaPct * 100).toFixed(1);
1673
+ costClause = `, cost ${magnitude}% ${sign}`;
1674
+ }
1675
+ const message = `Probe found ${top.candidateModel} produces equivalent-or-better outputs vs ${top.currentModel} on ${top.sampleN} recent ${top.archetype} prompts (pass rate ${pctPass}%, avg score ${score}/5${costClause}). Consider promoting via markPromoteReadyHandled.`;
1676
+ return [
1677
+ {
1678
+ level: "info",
1679
+ code: "promote-ready",
1680
+ message,
1681
+ suggestion: `Migrate ${top.archetype} traffic from ${top.currentModel} to ${top.candidateModel}, then call markPromoteReadyHandled({ appId, archetype: '${top.archetype}', family: '${top.family}', resolution: 'promoted' }) to silence this advisory.`,
1682
+ // alpha.36 architectural field — not a no-ai-needed case.
1683
+ recommendedArchitecture: void 0,
1684
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
1685
+ }
1686
+ ];
1687
+ }
1688
+
1689
+ // src/advisor-rules/consumer-on-stale-model.ts
1690
+ function isStaleStatus(v) {
1691
+ return v === "legacy" || v === "deprecated";
1692
+ }
1693
+ function asString(v) {
1694
+ return typeof v === "string" && v.length > 0 ? v : void 0;
1695
+ }
1696
+ function mapRowsToFindings3(rows) {
1697
+ const out = [];
1698
+ for (const raw of rows) {
1699
+ if (!raw || typeof raw !== "object") continue;
1700
+ const r = raw;
1701
+ const archetype = asString(r.intent_archetype) ?? asString(r.applies_to_archetype);
1702
+ const staleModel = asString(r.stale_model) ?? asString(r.applies_to_model);
1703
+ const staleProvider = asString(r.stale_provider);
1704
+ const recommendedModel = asString(r.recommended_model);
1705
+ const family = asString(r.family);
1706
+ const message = asString(r.message);
1707
+ if (!archetype || !staleModel || !recommendedModel || !family || !message) {
1708
+ continue;
1709
+ }
1710
+ if (!isStaleStatus(r.stale_status)) continue;
1711
+ const row = {
1712
+ archetype,
1713
+ staleModel,
1714
+ staleProvider: staleProvider ?? "unknown",
1715
+ staleStatus: r.stale_status,
1716
+ recommendedModel,
1717
+ family,
1718
+ message
1719
+ };
1720
+ const suggestion = asString(r.suggestion);
1721
+ if (suggestion) row.suggestion = suggestion;
1722
+ if (typeof r.observation_count === "number" && Number.isFinite(r.observation_count)) {
1723
+ row.observationCount = r.observation_count;
1724
+ }
1725
+ out.push(row);
1726
+ }
1727
+ return out;
1728
+ }
1729
+ var snapshots3 = /* @__PURE__ */ new Map();
1730
+ var runtime3;
1731
+ var warnedOnce3 = false;
1732
+ var pendingRefreshes3 = /* @__PURE__ */ new Map();
1733
+ function isStaleModelFindingsBrainActive() {
1734
+ return runtime3 !== void 0;
1735
+ }
1736
+ function getStaleModelFindings(opts) {
1737
+ const rt = runtime3;
1738
+ if (!rt) return [];
1739
+ const appId = opts.appId;
1740
+ if (!appId) return [];
1741
+ let snap = snapshots3.get(appId);
1742
+ if (!snap) {
1743
+ snap = { data: [], expiresAt: 0, refreshing: false };
1744
+ snapshots3.set(appId, snap);
1745
+ }
1746
+ const now = Date.now();
1747
+ const stale = snap.expiresAt <= now;
1748
+ if (stale && !snap.refreshing) {
1749
+ snap.refreshing = true;
1750
+ void asyncRefresh3(rt, appId);
1751
+ }
1752
+ if (opts.archetype) {
1753
+ return snap.data.filter((f) => f.archetype === opts.archetype);
1754
+ }
1755
+ return snap.data;
1756
+ }
1757
+ async function asyncRefresh3(rt, appId) {
1758
+ const promise = doRefresh3(rt, appId);
1759
+ pendingRefreshes3.set(appId, promise);
1760
+ try {
1761
+ await promise;
1762
+ } finally {
1763
+ if (pendingRefreshes3.get(appId) === promise) {
1764
+ pendingRefreshes3.delete(appId);
1765
+ }
1766
+ }
1767
+ }
1768
+ async function doRefresh3(rt, appId) {
1769
+ const url = `${rt.endpoint}?app_id=${encodeURIComponent(appId)}`;
1770
+ let snap = snapshots3.get(appId);
1771
+ if (!snap) {
1772
+ snap = { data: [], expiresAt: 0, refreshing: false };
1773
+ snapshots3.set(appId, snap);
1774
+ }
1775
+ try {
1776
+ const res = await rt.fetchImpl(url, { method: "GET" });
1777
+ if (!res.ok) {
1778
+ throw new Error(`stale-model findings ${res.status}: ${res.statusText}`);
1779
+ }
1780
+ const body = await res.json();
1781
+ if (runtime3 !== rt) return;
1782
+ const rows = Array.isArray(body) ? mapRowsToFindings3(body) : [];
1783
+ snap.data = rows;
1784
+ snap.expiresAt = Date.now() + rt.ttlMs;
1785
+ snap.refreshing = false;
1786
+ } catch (err) {
1787
+ if (runtime3 !== rt) return;
1788
+ snap.refreshing = false;
1789
+ snap.expiresAt = Date.now() + rt.ttlMs;
1790
+ if (!warnedOnce3) {
1791
+ warnedOnce3 = true;
1792
+ (rt.onError ?? defaultOnError3)(err);
1793
+ }
1794
+ }
1795
+ }
1796
+ function defaultOnError3(err) {
1797
+ console.warn(
1798
+ "[kgauto] stale-model findings fetch failed (using empty fallback):",
1799
+ err
1800
+ );
1801
+ }
1802
+ var CONSUMER_ON_STALE_MODEL_RULE_CODE = "consumer-on-stale-model";
1803
+ function advisorRuleConsumerOnStaleModel(ir) {
1804
+ if (!isStaleModelFindingsBrainActive()) return [];
1805
+ if (!ir.appId) return [];
1806
+ const findings = getStaleModelFindings({
1807
+ appId: ir.appId,
1808
+ archetype: ir.intent.archetype
1809
+ });
1810
+ if (findings.length === 0) return [];
1811
+ const ranked = [...findings].sort((a, b) => {
1812
+ if (a.staleStatus !== b.staleStatus) {
1813
+ return a.staleStatus === "deprecated" ? -1 : 1;
1814
+ }
1815
+ return a.staleModel.localeCompare(b.staleModel);
1816
+ });
1817
+ const top = ranked[0];
1818
+ const extraCount = findings.length - 1;
1819
+ const extraNote = extraCount > 0 ? ` (+ ${extraCount} more stale model${extraCount === 1 ? "" : "s"} for this archetype)` : "";
1820
+ return [
1821
+ {
1822
+ level: "warn",
1823
+ code: CONSUMER_ON_STALE_MODEL_RULE_CODE,
1824
+ message: `${top.message}${extraNote}`,
1825
+ suggestion: top.suggestion ?? `Migrate ${top.staleModel} \u2192 ${top.recommendedModel} for archetype "${top.archetype}". The newer model is the current latest in the "${top.family}" family; the stale one is ${top.staleStatus}.`,
1826
+ recommendationType: "model-swap",
1827
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
1828
+ }
1829
+ ];
1830
+ }
1831
+
1832
+ // src/archetype-fits.ts
1833
+ var ARCHETYPE_FAMILY_FITS = Object.freeze([
1834
+ {
1835
+ archetype: "plan",
1836
+ betterFitFamily: "deepseek-reasoner",
1837
+ reason: "Plan archetype is reasoning-shaped (multi-step chains, hypothesis-and-check, sub-goal decomposition) \u2014 exactly where reasoner-family models excel. Sonnet/Opus produce plans but at higher cost; reasoners produce equivalent-or-better plans at 7-17x lower cost at current promo pricing (deepseek-v4-pro $0.435/$0.87 per 1M promo through 2026-05-31 vs sonnet $3/$15).",
1838
+ costGuidance: "substantially cheaper at current pricing (deepseek-v4-pro promo: ~7-17x cheaper than sonnet)"
1839
+ },
1840
+ {
1841
+ archetype: "critique",
1842
+ betterFitFamily: "deepseek-reasoner",
1843
+ reason: "Critique archetype rewards epistemic humility and explicit reasoning \u2014 reasoner-family default behavior. Sonnet/Opus over-confident on critique tasks; reasoners surface uncertainty productively.",
1844
+ costGuidance: "comparable or cheaper at current pricing"
1845
+ }
1846
+ ]);
1847
+ function findBetterFit(archetype, currentFamily) {
1848
+ for (const fit of ARCHETYPE_FAMILY_FITS) {
1849
+ if (fit.archetype !== archetype) continue;
1850
+ if (fit.betterFitFamily === currentFamily) return null;
1851
+ return fit;
1852
+ }
1853
+ return null;
1854
+ }
1855
+
1856
+ // src/advisor-rules/cross-family-fit.ts
1857
+ function familyHasCurrentActiveModel(family) {
1858
+ for (const profile of allProfiles()) {
1859
+ const profileFamily = profile.family ?? deriveFamilyFromModelId(profile.id);
1860
+ if (profileFamily !== family) continue;
1861
+ if (profile.status !== "current") continue;
1862
+ if (profile.active === false) continue;
1863
+ return true;
1864
+ }
1865
+ return false;
1866
+ }
1867
+ function listCandidatesInFamily(family) {
1868
+ const candidates = [];
1869
+ for (const profile of allProfiles()) {
1870
+ const profileFamily = profile.family ?? deriveFamilyFromModelId(profile.id);
1871
+ if (profileFamily !== family) continue;
1872
+ if (profile.status !== "current") continue;
1873
+ if (profile.active === false) continue;
1874
+ candidates.push(profile.id);
1875
+ if (candidates.length >= 3) break;
1876
+ }
1877
+ return candidates;
1878
+ }
1879
+ function advisorRuleCrossFamilyFit(ctx) {
1880
+ if (!ctx.resolvedPrimary) return [];
1881
+ const currentFamily = deriveFamilyFromModelId(ctx.resolvedPrimary);
1882
+ if (!currentFamily) return [];
1883
+ const fit = findBetterFit(ctx.archetype, currentFamily);
1884
+ if (!fit) return [];
1885
+ if (!familyHasCurrentActiveModel(fit.betterFitFamily)) return [];
1886
+ const candidates = listCandidatesInFamily(fit.betterFitFamily);
1887
+ if (candidates.length === 0) return [];
1888
+ const candidateStr = candidates.join(", ");
1889
+ const message = `Your ${currentFamily} call on ${ctx.archetype} could shift to ${fit.betterFitFamily} \u2014 typically better quality + ${fit.costGuidance}. Suggested candidates: ${candidateStr}.`;
1890
+ return [
1891
+ {
1892
+ level: "info",
1893
+ code: "cross-family-fit-candidate",
1894
+ ownership: "consumer-actionable",
1895
+ message,
1896
+ suggestion: `Swap the model literal in \`ir.models\` to one of: ${candidateStr}. Or call \`getRecommendedPrimary({ family: '${fit.betterFitFamily}', archetype: '${ctx.archetype}', fallback: { id: '${candidates[0]}', reason: 'cross-family-fit-recommendation' } })\` to let kgauto resolve to the current+active family member.`,
1897
+ recommendationType: "model-swap",
1898
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
1899
+ }
1900
+ ];
1901
+ }
1902
+
1903
+ // src/advisor.ts
1904
+ var QUALITY_FLOOR_FOR_RECOMMENDATION = 6;
1905
+ var TIER_DOWN_COST_RATIO = 0.5;
1906
+ var COST_MISMATCHED_CHOSEN_SCORE_CEILING = 7;
1907
+ var PRODUCER_OWNED_RULE_CODES = Object.freeze(
1908
+ /* @__PURE__ */ new Set(["model-stale-evidence", "promote-ready"])
1909
+ );
1910
+ function deriveOwnership(code, selfDeclared) {
1911
+ if (selfDeclared) return selfDeclared;
1912
+ return PRODUCER_OWNED_RULE_CODES.has(code) ? "producer-owned" : "consumer-actionable";
1913
+ }
1914
+ function runAdvisor(ir, result, profile, policy, phase2) {
1915
+ const out = [];
1916
+ out.push(...detectCachingOff(ir, profile));
1917
+ out.push(...detectSingleChunkSystem(ir, profile));
1918
+ out.push(...detectToolBloat(ir, result));
1919
+ out.push(...detectHistoryUncached(ir, profile));
1920
+ out.push(...detectSingleModelArray(ir, policy));
1921
+ if (policy?.posture !== "locked") {
1922
+ out.push(...detectCostMismatchedArchetype(ir, profile, phase2));
1923
+ out.push(...detectModelStaleEvidence(ir, profile));
1924
+ out.push(...detectTierDown(ir, profile, phase2));
1925
+ }
1926
+ if (!translatorClearedToolCallCliff(phase2)) {
1927
+ out.push(...detectArchetypePerfFloorBreach(ir, profile));
1928
+ }
1929
+ if (policy?.posture !== "locked") {
1930
+ out.push(...detectStaleExclusionCandidate(ir));
1931
+ }
1932
+ if (policy?.posture !== "locked" && ir.appId) {
1933
+ out.push(
1934
+ ...advisorRulePromoteReady({
1935
+ appId: ir.appId,
1936
+ archetype: ir.intent.archetype,
1937
+ resolvedPrimary: profile.id
1938
+ })
1939
+ );
1940
+ out.push(...advisorRuleConsumerOnStaleModel(ir));
1941
+ }
1942
+ if (policy?.posture !== "locked") {
1943
+ out.push(
1944
+ ...advisorRuleCrossFamilyFit({
1945
+ archetype: ir.intent.archetype,
1946
+ resolvedPrimary: profile.id
1947
+ })
1948
+ );
1949
+ }
1950
+ return out;
1951
+ }
1952
+ function translatorClearedToolCallCliff(phase2) {
1953
+ const rewrites = phase2?.sectionRewritesApplied;
1954
+ if (!rewrites || rewrites.length === 0) return false;
1955
+ for (const rw of rewrites) {
1956
+ if (rw.kind === "tool_call_contract") return true;
1957
+ }
1958
+ return false;
1959
+ }
1960
+ function detectCachingOff(ir, profile) {
1961
+ if (profile.provider !== "anthropic") return [];
1962
+ const totalChars = ir.sections.reduce((s, sec) => s + sec.text.length, 0);
1963
+ if (totalChars < 2e3) return [];
1964
+ const anyCacheable = ir.sections.some((s) => s.cacheable === true);
1965
+ if (anyCacheable) return [];
1966
+ return [
1967
+ {
1968
+ level: "warn",
1969
+ code: "caching-off-on-claude",
1970
+ message: `System prompt is ${totalChars} chars on Anthropic but no PromptSection has cacheable=true. Anthropic prompt caching cuts cached-prefix input cost by ~90% on subsequent calls; without it, every turn re-pays full price for the static system context.`,
1971
+ suggestion: "Mark stable system sections (role, persona, tool policy) with `cacheable: true`. The lowering pass concatenates cacheable sections into a single cache-controlled block before the dynamic ones.",
1972
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
1973
+ }
1974
+ ];
1975
+ }
1976
+ function detectSingleChunkSystem(ir, profile) {
1977
+ if (profile.provider !== "anthropic") return [];
1978
+ if (ir.sections.length !== 1) return [];
1979
+ const only = ir.sections[0];
1980
+ if (!only || only.text.length <= 1e3) return [];
1981
+ return [
1982
+ {
1983
+ level: "info",
1984
+ code: "single-chunk-system",
1985
+ message: `System prompt is a single ${only.text.length}-char chunk. Splitting into NamedChunks (static role/persona vs dynamic context) gives the lowering pass a finer cache-marker boundary \u2014 only the static portion needs to be byte-stable for the cache to hit.`,
1986
+ suggestion: "Refactor the system builder to return an array of `PromptSection` shaped { id, text, cacheable?: boolean }. Static chunks (role, persona, tool policy) get `cacheable: true`; dynamic ones (current context, today's date) don't.",
1987
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
1988
+ }
1989
+ ];
1990
+ }
1991
+ function detectToolBloat(ir, result) {
1992
+ const SHORT_OUTPUT = /* @__PURE__ */ new Set([
1993
+ "classify",
1994
+ "extract",
1995
+ "summarize",
1996
+ "transform",
1997
+ "critique"
1998
+ ]);
1999
+ if (!ir.tools || ir.tools.length === 0) return [];
2000
+ const toolsKept = result.diagnostics.toolsKept;
2001
+ if (toolsKept <= 10) return [];
2002
+ if (!SHORT_OUTPUT.has(ir.intent.archetype)) return [];
2003
+ return [
2004
+ {
2005
+ level: "warn",
2006
+ code: "tool-bloat",
2007
+ message: `${toolsKept} tools kept after the relevance pass for archetype="${ir.intent.archetype}" (consumer declared ${ir.tools.length}). This archetype is short-output and rarely needs more than 3 tools; each tool definition eats ~350 tokens of context budget.`,
2008
+ suggestion: "Tighten `relevanceByIntent: { [archetype]: 0..1 }` per ToolDefinition. Tools below `toolRelevanceThreshold` (default 0.2) get dropped. Without `relevanceByIntent`, every tool defaults to neutral (0.5) and stays.",
2009
+ docsUrl: "https://github.com/stue/kgauto/blob/main/v2/README.md#tools"
2010
+ }
2011
+ ];
2012
+ }
2013
+ function detectHistoryUncached(ir, profile) {
2014
+ if (profile.provider !== "anthropic") return [];
2015
+ if (!ir.history || ir.history.length < 2) return [];
2016
+ if (ir.historyCachePolicy && ir.historyCachePolicy.strategy !== "none") {
2017
+ return [];
2018
+ }
2019
+ return [
2020
+ {
2021
+ level: "warn",
2022
+ code: "history-uncached-on-claude",
2023
+ message: `${ir.history.length} history messages on Anthropic with no historyCachePolicy. Every turn re-pays for the full conversation context; with caching, subsequent turns hit the cache at ~10% the input cost.`,
2024
+ suggestion: "Set `historyCachePolicy: { strategy: 'all-but-latest' }` on this IR. The lowering pass marks the message immediately preceding currentTurn with cache_control; subsequent turns whose history prefix matches byte-for-byte hit the cache.",
2025
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
2026
+ }
2027
+ ];
2028
+ }
2029
+ function detectSingleModelArray(ir, policy) {
2030
+ if (ir.models.length !== 1) return [];
2031
+ if (policy?.posture === "locked") return [];
2032
+ const only = ir.models[0];
2033
+ return [
2034
+ {
2035
+ level: "warn",
2036
+ code: "single-model-array",
2037
+ message: `\`ir.models\` has length 1 (only "${only}") and posture is not 'locked'. A single-model chain has no safety net \u2014 the first 429 / 5xx / cliff hits the user as a failure. Master plan \xA71.2 closes the reliability gap with a 2-step minimum.`,
2038
+ suggestion: "Use `getDefaultFallbackChain({ archetype: ir.intent.archetype, primary: '" + only + "', posture: 'preferred' })` for a user-anchored chain, or `getDefaultFallbackChain({ archetype, posture: 'open' })` for library-picked. If single-model is intentional (compliance/brand promise), set `policy.posture = 'locked'` to silence this rule.",
2039
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#single-model-array"
2040
+ }
2041
+ ];
2042
+ }
2043
+ function detectCostMismatchedArchetype(ir, profile, phase2) {
2044
+ if (!phase2 || phase2.fallbackChain.length === 0) return [];
2045
+ if (!phase2.profileResolver) return [];
2046
+ const archetype = ir.intent.archetype;
2047
+ const chosenScore = getArchetypePerfScore(profile.id, archetype);
2048
+ const chosenHasRoomToGrow = chosenScore.grounding === "judgment" || chosenScore.score < COST_MISMATCHED_CHOSEN_SCORE_CEILING;
2049
+ if (!chosenHasRoomToGrow) return [];
2050
+ let bestAlt = null;
2051
+ for (const altId of phase2.fallbackChain) {
2052
+ const altProfile = phase2.profileResolver(altId);
2053
+ if (!altProfile) continue;
2054
+ if (altProfile.id === profile.id) continue;
2055
+ const altScore = getArchetypePerfScore(altProfile.id, archetype);
2056
+ if (altScore.score < QUALITY_FLOOR_FOR_RECOMMENDATION) continue;
2057
+ if (altScore.score < chosenScore.score) continue;
2058
+ if (altProfile.costInputPer1m >= profile.costInputPer1m) continue;
2059
+ if (!bestAlt || altScore.score > bestAlt.score.score || altScore.score === bestAlt.score.score && altProfile.costInputPer1m < bestAlt.profile.costInputPer1m) {
2060
+ bestAlt = { id: altId, profile: altProfile, score: altScore };
2061
+ }
2062
+ }
2063
+ if (!bestAlt) return [];
2064
+ const tierDownWouldFire = bestAlt.score.grounding === "measured" && bestAlt.profile.costInputPer1m <= profile.costInputPer1m * TIER_DOWN_COST_RATIO;
2065
+ if (tierDownWouldFire) return [];
2066
+ const chosenGrounding = chosenScore.grounding === "judgment" ? `archetypePerf.${archetype}=judgment` : `archetypePerf.${archetype}=${chosenScore.score}`;
2067
+ const altGrounding = bestAlt.score.grounding === "measured" ? `archetypePerf.${archetype}=${bestAlt.score.score}, measured, n=${bestAlt.score.n}` : `archetypePerf.${archetype}=${bestAlt.score.score}, judgment`;
2068
+ return [
2069
+ {
2070
+ level: "warn",
2071
+ code: "cost-mismatched-archetype",
2072
+ message: `Cost-mismatched-archetype: target=${profile.id} (${chosenGrounding}) selected for ${archetype}. Alternative ${bestAlt.id} (${altGrounding}) is cheaper ($${bestAlt.profile.costInputPer1m}/$${bestAlt.profile.costOutputPer1m} vs $${profile.costInputPer1m}/$${profile.costOutputPer1m} per 1M) at equal-or-better quality.`,
2073
+ suggestion: `Consider declaring \`${bestAlt.id}\` as the primary model for this archetype, or relax to posture='open' to let kgauto select among the chain. If the chosen model is required for compliance/brand reasons, set \`policy.posture = 'locked'\` to silence this rule.`,
2074
+ recommendationType: profile.provider === bestAlt.profile.provider ? "tier-down" : "model-swap",
2075
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
2076
+ }
2077
+ ];
2078
+ }
2079
+ function detectModelStaleEvidence(ir, profile) {
2080
+ if (!isBrainQueryActiveFor("kgauto_archetype_perf")) return [];
2081
+ const archetype = ir.intent.archetype;
2082
+ const chosen = getArchetypePerfScore(profile.id, archetype);
2083
+ if (chosen.grounding !== "judgment") return [];
2084
+ return [
2085
+ {
2086
+ level: "info",
2087
+ code: "model-stale-evidence",
2088
+ message: `Model-stale-evidence: target=${profile.id} archetype=${archetype} is judgment-grounded (n=${chosen.n}) despite brain-query mode being active. Measurement substrate is wired but the brain hasn't accumulated >=10 outcomes for this (model, archetype) tuple yet \u2014 routing decisions remain pre-measured for this slot.`,
2089
+ suggestion: "Verify that `record()` is being called on every call() outcome with the appropriate `actualModel` and `mutationsApplied` fields. Once the brain accumulates n>=10 rows on this tuple, the score promotes from judgment to measured automatically (5-min SWR cache). No code change required from your side \u2014 this is the substrate signaling the gap.",
2090
+ recommendationType: "prompt-fix",
2091
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
2092
+ }
2093
+ ];
2094
+ }
2095
+ function detectTierDown(ir, profile, phase2) {
2096
+ if (!phase2 || phase2.fallbackChain.length === 0) return [];
2097
+ if (!phase2.profileResolver) return [];
2098
+ const archetype = ir.intent.archetype;
2099
+ const chosenScore = getArchetypePerfScore(profile.id, archetype);
2100
+ const chosenCost = profile.costInputPer1m;
2101
+ let bestAlt = null;
2102
+ for (const altId of phase2.fallbackChain) {
2103
+ const altProfile = phase2.profileResolver(altId);
2104
+ if (!altProfile) continue;
2105
+ if (altProfile.id === profile.id) continue;
2106
+ const altScore = getArchetypePerfScore(altProfile.id, archetype);
2107
+ if (altScore.grounding !== "measured") continue;
2108
+ if (altScore.score < QUALITY_FLOOR_FOR_RECOMMENDATION) continue;
2109
+ if (altScore.score < chosenScore.score) continue;
2110
+ if (altProfile.costInputPer1m > chosenCost * TIER_DOWN_COST_RATIO) continue;
2111
+ if (!bestAlt || altProfile.costInputPer1m < bestAlt.profile.costInputPer1m || altProfile.costInputPer1m === bestAlt.profile.costInputPer1m && altScore.score > bestAlt.score.score) {
2112
+ bestAlt = { id: altId, profile: altProfile, score: altScore };
2113
+ }
2114
+ }
2115
+ if (!bestAlt) return [];
2116
+ const chosenDesc = chosenScore.grounding === "measured" ? `archetypePerf.${archetype}=${chosenScore.score} (measured, n=${chosenScore.n})` : `archetypePerf.${archetype}=${chosenScore.score} (${chosenScore.grounding})`;
2117
+ return [
2118
+ {
2119
+ level: "warn",
2120
+ code: "tier-down",
2121
+ message: `Tier-down: target=${profile.id} (${chosenDesc}) selected for ${archetype}. Brain shows ${bestAlt.id} delivers equal-or-better quality (archetypePerf.${archetype}=${bestAlt.score.score}, measured, n=${bestAlt.score.n}) at $${bestAlt.profile.costInputPer1m}/$${bestAlt.profile.costOutputPer1m} per 1M vs $${profile.costInputPer1m}/$${profile.costOutputPer1m} \u2014 a measured tier-down opportunity.`,
2122
+ suggestion: `Move \`${bestAlt.id}\` to primary for this archetype. The brain has n=${bestAlt.score.n} measured outcomes backing the recommendation; this is data, not opinion. If posture='locked' is required (compliance/brand promise), set it explicitly to silence this rule.`,
2123
+ recommendationType: "tier-down",
2124
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
2125
+ }
2126
+ ];
2127
+ }
2128
+ function detectArchetypePerfFloorBreach(ir, profile) {
2129
+ const compat = getModelCompatibility(profile.id, {
2130
+ archetype: ir.intent.archetype,
2131
+ toolOrchestration: ir.constraints?.toolOrchestration
2132
+ });
2133
+ if (compat.status === "compatible") return [];
2134
+ if (compat.status === "requires-adapter") {
2135
+ return [
2136
+ {
2137
+ level: "warn",
2138
+ code: "archetype-perf-floor-breach",
2139
+ message: `${profile.id} sits below the archetype floor for ${ir.intent.archetype} (score ${compat.archetypePerf}/10, floor ${6}). A known adapter would lift it: ${compat.adapter.parameter}=${compat.adapter.value}. ${compat.adapter.consequence}`,
2140
+ suggestion: `Pass \`ir.constraints.${compat.adapter.parameter} = '${compat.adapter.value}'\` for this call, OR pick a model whose archetypePerf for ${ir.intent.archetype} already clears the floor (call \`getModelCompatibility(modelId, { archetype: '${ir.intent.archetype}' })\` to check). Estimated post-adapter score: ${compat.archetypePerfWithAdapter}/10.`,
2141
+ recommendationType: "prompt-fix",
2142
+ suggestedAdaptation: compat.adapter,
2143
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
2144
+ }
2145
+ ];
2146
+ }
2147
+ return [
2148
+ {
2149
+ level: "critical",
2150
+ code: "archetype-perf-floor-breach",
2151
+ message: `${profile.id} sits below the archetype floor for ${ir.intent.archetype} (score ${compat.archetypePerf}/10, floor ${6}) and no known adapter would lift it. ${compat.reason}`,
2152
+ suggestion: `Swap to a model whose archetypePerf for ${ir.intent.archetype} clears the floor. Use \`getModelCompatibility(candidateId, { archetype: '${ir.intent.archetype}' })\` to vet candidates, or \`getDefaultFallbackChain({ archetype: '${ir.intent.archetype}', posture: 'open' })\` for a library-picked chain that respects the floor by construction.`,
2153
+ recommendationType: "model-swap",
2154
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
2155
+ }
2156
+ ];
2157
+ }
2158
+ function detectStaleExclusionCandidate(ir) {
2159
+ if (!isExclusionFindingsBrainActive()) return [];
2160
+ if (!ir.appId) return [];
2161
+ const findings = getStaleExclusionFindings({
2162
+ appId: ir.appId,
2163
+ archetype: ir.intent.archetype
2164
+ });
2165
+ if (findings.length === 0) return [];
2166
+ const ranked = [...findings].sort((a, b) => {
2167
+ const sa = a.estimatedSavingsUsd30d ?? -Infinity;
2168
+ const sb = b.estimatedSavingsUsd30d ?? -Infinity;
2169
+ if (sa !== sb) return sb - sa;
2170
+ return confidenceRank(b.confidence) - confidenceRank(a.confidence);
2171
+ });
2172
+ const top = ranked[0];
2173
+ const extraCount = findings.length - 1;
2174
+ const extraNote = extraCount > 0 ? ` (+ ${extraCount} more excluded model${extraCount === 1 ? "" : "s"} for this archetype)` : "";
2175
+ return [
2176
+ {
2177
+ level: "info",
2178
+ code: "stale-exclusion-candidate",
2179
+ message: `${top.message}${extraNote}`,
2180
+ suggestion: top.suggestion,
2181
+ recommendationType: "tier-down",
2182
+ docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
2183
+ }
2184
+ ];
2185
+ }
2186
+ function confidenceRank(c) {
2187
+ if (c === "high") return 3;
2188
+ if (c === "medium") return 2;
2189
+ return 1;
2190
+ }
2191
+
2192
+ // src/translator.ts
2193
+ var TRANSLATOR_FLOOR = ARCHETYPE_FLOOR_DEFAULT;
2194
+ var RULE_SEQUENTIAL_TOOL_CLIFF = "sequential-tool-cliff-below-floor";
2195
+ var RULE_NARRATION_DRIFT_ANTHROPIC = "narration-drift-anthropic";
2196
+ var RULE_NARRATION_THINKING_LEAK_DEEPSEEK = "narration-thinking-leak-deepseek";
2197
+ var SEQUENTIAL_TOOL_PREAMBLE = "IMPORTANT: Use one tool call per response. Wait for the tool result before deciding the next tool. Do NOT batch tool calls in parallel.";
2198
+ var NARRATION_DRIFT_ANTHROPIC_PREAMBLE = "Output ONLY the requested content. Do not narrate your thought process. Each line \u2264 12 words.";
2199
+ var NARRATION_THINKING_LEAK_DEEPSEEK_PREAMBLE = "Reasoning is internal. Output ONLY the requested content; do not emit <thinking> blocks or internal monologue as user-facing text.";
2200
+ function matchRule(kind, profile, archetype) {
2201
+ if (kind === "tool_call_contract") {
2202
+ if (!profile.archetypePerf) return null;
2203
+ const archetypeScore = profile.archetypePerf[archetype];
2204
+ if (typeof archetypeScore !== "number" || archetypeScore >= TRANSLATOR_FLOOR) {
2205
+ return null;
2206
+ }
2207
+ return {
2208
+ id: RULE_SEQUENTIAL_TOOL_CLIFF,
2209
+ preamble: SEQUENTIAL_TOOL_PREAMBLE,
2210
+ wireOverrides: { parallelToolCalls: false }
2211
+ };
2212
+ }
2213
+ if (kind === "narration_contract") {
2214
+ if (profile.provider === "anthropic") {
2215
+ return {
2216
+ id: RULE_NARRATION_DRIFT_ANTHROPIC,
2217
+ preamble: NARRATION_DRIFT_ANTHROPIC_PREAMBLE
2218
+ };
2219
+ }
2220
+ if (profile.provider === "deepseek") {
2221
+ return {
2222
+ id: RULE_NARRATION_THINKING_LEAK_DEEPSEEK,
2223
+ preamble: NARRATION_THINKING_LEAK_DEEPSEEK_PREAMBLE
2224
+ };
2225
+ }
2226
+ return null;
2227
+ }
2228
+ return null;
2229
+ }
2230
+ function applySectionRewrites(args) {
2231
+ const { ir, profile, archetype } = args;
2232
+ if (!Array.isArray(ir.sections) || ir.sections.length === 0) {
2233
+ return { rewrittenIR: ir, rewrites: [] };
2234
+ }
2235
+ const rewrites = [];
2236
+ const newSections = ir.sections.map((section) => {
2237
+ if (!section.kind || section.kind === "arbitrary") return section;
2238
+ const rule = matchRule(section.kind, profile, archetype);
2239
+ if (!rule) return section;
2240
+ const originalText = section.text;
2241
+ const transformedText = `${rule.preamble}
2242
+
2243
+ ${originalText}`;
2244
+ rewrites.push({
2245
+ sectionId: section.id,
2246
+ kind: section.kind,
2247
+ rule: rule.id,
2248
+ originalText,
2249
+ transformedText,
2250
+ ...rule.wireOverrides ? { wireOverrides: rule.wireOverrides } : {}
2251
+ });
2252
+ return { ...section, text: transformedText };
2253
+ });
2254
+ if (rewrites.length === 0) {
2255
+ return { rewrittenIR: ir, rewrites: [] };
2256
+ }
2257
+ const rewrittenIR = { ...ir, sections: newSections };
2258
+ return { rewrittenIR, rewrites };
2259
+ }
2260
+
2261
+ // src/compile.ts
2262
+ var counter = 0;
2263
+ function makeHandle() {
2264
+ counter = (counter + 1) % 1e6;
754
2265
  return `c${Date.now().toString(36)}-${counter.toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
755
2266
  }
756
2267
  function compile(ir, opts = {}) {
757
2268
  const resolver = opts.profileResolver ?? getProfile;
758
2269
  validateIR(ir);
2270
+ ir = resolveModelEntries(ir);
759
2271
  const sliced = passSlice(ir);
760
2272
  const deduped = passDedupe(sliced.value);
761
2273
  const toolFiltered = passToolRelevance(deduped.value, {
762
2274
  threshold: opts.toolRelevanceThreshold
763
2275
  });
764
2276
  const compressed = passCompressHistory(toolFiltered.value, {
765
- summarizeOlderThan: opts.compressHistoryAfter
2277
+ summarizeOlderThan: opts.compressHistoryAfter,
2278
+ summarizeAboveTokens: opts.compressHistoryAboveTokens
766
2279
  });
767
2280
  let workingIR = compressed.value;
768
2281
  const accumulatedMutations = [
@@ -789,14 +2302,44 @@ function compile(ir, opts = {}) {
789
2302
  const cliffs = passApplyCliffs(workingIR, profile, inputTokens);
790
2303
  workingIR = cliffs.value.ir;
791
2304
  accumulatedMutations.push(...cliffs.mutations);
2305
+ const conventions = passApplyConventions(workingIR, profile);
2306
+ workingIR = conventions.value.ir;
2307
+ accumulatedMutations.push(...conventions.mutations);
2308
+ const translated = applySectionRewrites({
2309
+ ir: workingIR,
2310
+ profile,
2311
+ archetype: ir.intent.archetype
2312
+ });
2313
+ workingIR = translated.rewrittenIR;
2314
+ const sectionRewritesApplied = translated.rewrites;
2315
+ let wireOverrides;
2316
+ for (const rw of sectionRewritesApplied) {
2317
+ if (!rw.wireOverrides) continue;
2318
+ if (!wireOverrides) wireOverrides = {};
2319
+ if (rw.wireOverrides.parallelToolCalls !== void 0) {
2320
+ wireOverrides.parallelToolCalls = rw.wireOverrides.parallelToolCalls;
2321
+ }
2322
+ }
2323
+ for (const rw of sectionRewritesApplied) {
2324
+ accumulatedMutations.push({
2325
+ id: `translator:${rw.rule}:${rw.sectionId}`,
2326
+ source: "translator",
2327
+ passName: "translator",
2328
+ description: `Rewrote section "${rw.sectionId}" (kind=${rw.kind}) via rule "${rw.rule}".`
2329
+ });
2330
+ }
792
2331
  const lowered = lower(workingIR, profile, {
793
2332
  forceThinkingZero: cliffs.value.loweringHints.forceThinkingZero,
794
- forceTerseOutput: cliffs.value.loweringHints.forceTerseOutput
2333
+ forceTerseOutput: cliffs.value.loweringHints.forceTerseOutput,
2334
+ wireOverrides
795
2335
  });
796
2336
  validateFinalFit(workingIR, profile, inputTokens);
797
2337
  const handle = makeHandle();
798
2338
  const finalShape = computeShape(workingIR, inputTokens);
799
2339
  const _learningKey = learningKey(ir.intent.archetype, profile.id, finalShape);
2340
+ const historyCacheMarkIndex = computeHistoryCacheMarkIndex(workingIR);
2341
+ const systemMessages = buildSystemMessages(workingIR, profile.provider);
2342
+ const systemCacheMarkIndex = lastCacheableSystemIndex(systemMessages);
800
2343
  const diagnostics = {
801
2344
  sectionsKept: workingIR.sections.length,
802
2345
  sectionsDropped: ir.sections.length - workingIR.sections.length,
@@ -806,9 +2349,39 @@ function compile(ir, opts = {}) {
806
2349
  historyDropped: (ir.history?.length ?? 0) - (workingIR.history?.length ?? 0),
807
2350
  cacheableTokens: lowered.diagnostics.cacheableTokens,
808
2351
  estimatedCacheSavingsUsd: lowered.diagnostics.estimatedCacheSavingsUsd,
809
- historyCacheableTokens: lowered.diagnostics.historyCacheableTokens
2352
+ historyCacheableTokens: lowered.diagnostics.historyCacheableTokens,
2353
+ historyTokensTotal: compressed.historyTokensTotal,
2354
+ // alpha.20 E3: mirror the consumer's declared mode for Glass-Box +
2355
+ // brain observability. Undefined when not declared (pre-alpha.20).
2356
+ toolOrchestration: ir.constraints?.toolOrchestration,
2357
+ // alpha.33 — see top-of-block comment.
2358
+ historyCacheMarkIndex,
2359
+ systemCacheMarkIndex,
2360
+ // alpha.43 — cliff-style warnings emitted by passApplyConventions.
2361
+ // Merge convention-pass cliffWarnings with any cliff-guard quality
2362
+ // warnings the cliff pass surfaced (the same shape — informational
2363
+ // text the consumer can route on without changing behavior).
2364
+ cliffWarnings: [
2365
+ ...cliffs.value.loweringHints.qualityWarning ?? [],
2366
+ ...conventions.value.cliffWarnings
2367
+ ]
810
2368
  };
811
- const advisories = runAdvisor(
2369
+ if (ir.intent.archetype === "hunt" && ir.constraints?.toolOrchestration === "sequential") {
2370
+ accumulatedMutations.push({
2371
+ id: "sequential-mode-chain-selected",
2372
+ source: "tool_orchestration",
2373
+ passName: "compile",
2374
+ description: "ir.constraints.toolOrchestration='sequential' selected the DeepSeek-tier-0 hunt chain overlay (L-040 parallel-tool cliff doesn't apply at single-step granularity)."
2375
+ });
2376
+ }
2377
+ const phase2ProfileResolver = opts.profileResolver ? (id) => {
2378
+ try {
2379
+ return opts.profileResolver(id);
2380
+ } catch {
2381
+ return void 0;
2382
+ }
2383
+ } : tryGetProfile;
2384
+ const rawAdvisories = runAdvisor(
812
2385
  ir,
813
2386
  {
814
2387
  target: profile.id,
@@ -816,8 +2389,24 @@ function compile(ir, opts = {}) {
816
2389
  tokensIn: inputTokens,
817
2390
  diagnostics
818
2391
  },
819
- profile
2392
+ profile,
2393
+ opts.policy,
2394
+ {
2395
+ fallbackChain,
2396
+ profileResolver: phase2ProfileResolver,
2397
+ // alpha.29 — feed translator rewrites to the advisor so the
2398
+ // `archetype-perf-floor-breach` rule can suppress when the translator
2399
+ // already cleared the cliff for the same archetype. Without this,
2400
+ // both the rewrite AND the advisory fire — noisy, and the advisory
2401
+ // would mislead consumers into thinking the cliff is unaddressed.
2402
+ sectionRewritesApplied
2403
+ }
820
2404
  );
2405
+ const advisories = rawAdvisories.map((a) => ({
2406
+ ...a,
2407
+ kgautoRequestId: handle,
2408
+ ownership: deriveOwnership(a.code, a.ownership)
2409
+ }));
821
2410
  return {
822
2411
  handle,
823
2412
  target: profile.id,
@@ -828,9 +2417,47 @@ function compile(ir, opts = {}) {
828
2417
  mutationsApplied: accumulatedMutations,
829
2418
  fallbackChain,
830
2419
  advisories,
831
- diagnostics
2420
+ diagnostics,
2421
+ sectionRewritesApplied,
2422
+ wireOverrides,
2423
+ systemMessages
832
2424
  };
833
2425
  }
2426
+ function computeHistoryCacheMarkIndex(ir) {
2427
+ const policy = ir.historyCachePolicy;
2428
+ if (!policy || policy.strategy === "none") return void 0;
2429
+ const historyLen = ir.history?.length ?? 0;
2430
+ if (historyLen === 0) return void 0;
2431
+ if (policy.strategy === "all-but-latest") {
2432
+ return historyLen - 1;
2433
+ }
2434
+ if (policy.strategy === "fixed-suffix") {
2435
+ const idx = historyLen - 1 - policy.suffix;
2436
+ if (idx < 0) return void 0;
2437
+ return idx;
2438
+ }
2439
+ return void 0;
2440
+ }
2441
+ function buildSystemMessages(ir, provider) {
2442
+ const sections = ir.sections;
2443
+ if (!sections || sections.length === 0) return [];
2444
+ return sections.map((s) => {
2445
+ const base = { role: "system", content: s.text };
2446
+ if (provider === "anthropic" && s.cacheable) {
2447
+ base.providerOptions = { anthropic: { cacheControl: { type: "ephemeral" } } };
2448
+ }
2449
+ return base;
2450
+ });
2451
+ }
2452
+ function lastCacheableSystemIndex(systemMessages) {
2453
+ for (let i = systemMessages.length - 1; i >= 0; i--) {
2454
+ const entry = systemMessages[i];
2455
+ if (entry && entry.providerOptions?.anthropic?.cacheControl) {
2456
+ return i;
2457
+ }
2458
+ }
2459
+ return void 0;
2460
+ }
834
2461
  function validateIR(ir) {
835
2462
  if (!ir.appId) throw new Error("compile(): ir.appId is required");
836
2463
  if (!ir.intent || !ir.intent.archetype) {
@@ -843,6 +2470,29 @@ function validateIR(ir) {
843
2470
  throw new Error("compile(): ir.sections must be an array");
844
2471
  }
845
2472
  }
2473
+ function resolveModelEntries(ir) {
2474
+ const out = [];
2475
+ const seen = /* @__PURE__ */ new Set();
2476
+ for (const entry of ir.models) {
2477
+ let id;
2478
+ if (typeof entry === "string") {
2479
+ id = entry;
2480
+ } else if (entry && typeof entry === "object" && "family" in entry) {
2481
+ id = resolveFamilyEntry(entry.family, {
2482
+ archetype: ir.intent.archetype,
2483
+ appId: ir.appId
2484
+ });
2485
+ } else {
2486
+ throw new Error(
2487
+ `compile(): ir.models entry must be a string or { family: string }; got ${JSON.stringify(entry)}`
2488
+ );
2489
+ }
2490
+ if (seen.has(id)) continue;
2491
+ seen.add(id);
2492
+ out.push(id);
2493
+ }
2494
+ return { ...ir, models: out };
2495
+ }
846
2496
  function pickTarget(ir, scores) {
847
2497
  if (ir.constraints?.forceModel) {
848
2498
  const forced = scores.find((s) => s.modelId === ir.constraints.forceModel);
@@ -869,14 +2519,112 @@ function validateFinalFit(ir, profile, tokens) {
869
2519
  }
870
2520
  }
871
2521
 
2522
+ // src/pricing-brain.ts
2523
+ function isPricingRow(x) {
2524
+ if (!x || typeof x !== "object") return false;
2525
+ const r = x;
2526
+ return typeof r.model_id === "string" && typeof r.cost_input_per_1m === "number" && typeof r.cost_output_per_1m === "number" && typeof r.valid_from === "string";
2527
+ }
2528
+ function mapRowsToPricing(rows) {
2529
+ const out = [];
2530
+ for (const row of rows) {
2531
+ if (!isPricingRow(row)) continue;
2532
+ out.push({
2533
+ modelId: row.model_id,
2534
+ costInputPer1m: row.cost_input_per_1m,
2535
+ costOutputPer1m: row.cost_output_per_1m,
2536
+ cacheInputPer1m: row.cache_input_per_1m ?? void 0,
2537
+ cacheCreationPer1m: row.cache_creation_per_1m ?? void 0,
2538
+ validFrom: Date.parse(row.valid_from),
2539
+ validUntil: row.valid_until == null ? void 0 : Date.parse(row.valid_until),
2540
+ source: row.source ?? void 0
2541
+ });
2542
+ }
2543
+ return out;
2544
+ }
2545
+ function bundledPricing() {
2546
+ const out = [];
2547
+ for (const profile of allProfiles()) {
2548
+ out.push({
2549
+ modelId: profile.id,
2550
+ costInputPer1m: profile.costInputPer1m,
2551
+ costOutputPer1m: profile.costOutputPer1m,
2552
+ cacheInputPer1m: profile.lowering.cache.discount !== void 0 && profile.lowering.cache.discount > 0 ? profile.costInputPer1m * profile.lowering.cache.discount : void 0,
2553
+ validFrom: 0,
2554
+ validUntil: void 0,
2555
+ source: "profile_seed"
2556
+ });
2557
+ }
2558
+ return out;
2559
+ }
2560
+ var loadPricingFromBrain = createBrainQueryCache({
2561
+ table: "kgauto_pricing",
2562
+ mapRows: mapRowsToPricing,
2563
+ bundledFallback: bundledPricing
2564
+ });
2565
+ function resolvePricingAt(modelId, at = /* @__PURE__ */ new Date()) {
2566
+ const ts = at.getTime();
2567
+ const all = loadPricingFromBrain();
2568
+ let best;
2569
+ for (const row of all) {
2570
+ if (row.modelId !== modelId) continue;
2571
+ if (row.validFrom > ts) continue;
2572
+ if (row.validUntil !== void 0 && row.validUntil <= ts) continue;
2573
+ if (!best || row.validFrom > best.validFrom) best = row;
2574
+ }
2575
+ return best;
2576
+ }
2577
+
872
2578
  // src/brain.ts
2579
+ var DEFAULT_BRAIN_WRITE_TIMEOUT_MS = 1e4;
2580
+ function brainWriteFetch(config) {
2581
+ const base = config.fetchImpl ?? fetch;
2582
+ const timeoutMs = config.timeoutMs ?? DEFAULT_BRAIN_WRITE_TIMEOUT_MS;
2583
+ if (timeoutMs <= 0 || typeof AbortSignal === "undefined" || typeof AbortSignal.timeout !== "function") {
2584
+ return base;
2585
+ }
2586
+ return (input, init) => base(input, { ...init, signal: AbortSignal.timeout(timeoutMs) });
2587
+ }
873
2588
  var activeConfig;
874
2589
  function configureBrain(config) {
875
2590
  const endpoint = config.endpoint.replace(/\/outcomes\/?$/, "");
876
2591
  activeConfig = { ...config, endpoint };
2592
+ const bq = config.brainQuery ?? {};
2593
+ const enabledTables = /* @__PURE__ */ new Set();
2594
+ if (bq.chains !== false) enabledTables.add("kgauto_chains");
2595
+ if (bq.perf !== false) enabledTables.add("kgauto_archetype_perf");
2596
+ if (bq.pricing !== false) enabledTables.add("kgauto_pricing");
2597
+ if (bq.models !== false) {
2598
+ enabledTables.add("kgauto_models");
2599
+ enabledTables.add("kgauto_aliases");
2600
+ }
2601
+ if (enabledTables.size === 0) {
2602
+ configureBrainQuery(void 0);
2603
+ } else {
2604
+ configureBrainQuery({
2605
+ endpoint,
2606
+ configEndpoint: bq.configEndpoint,
2607
+ ttlMs: bq.cacheTtlMs ?? 3e5,
2608
+ fetchImpl: config.fetchImpl ?? fetch,
2609
+ enabledTables,
2610
+ onError: config.onError
2611
+ });
2612
+ }
2613
+ if (bq.findingsExclusions !== false) {
2614
+ configureExclusionFindingsBrain({
2615
+ endpoint: bq.findingsEndpoint ?? DEFAULT_FINDINGS_ENDPOINT,
2616
+ ttlMs: bq.cacheTtlMs ?? 3e5,
2617
+ fetchImpl: config.fetchImpl ?? fetch,
2618
+ onError: config.onError
2619
+ });
2620
+ } else {
2621
+ configureExclusionFindingsBrain(void 0);
2622
+ }
877
2623
  }
878
2624
  function clearBrain() {
879
2625
  activeConfig = void 0;
2626
+ configureBrainQuery(void 0);
2627
+ configureExclusionFindingsBrain(void 0);
880
2628
  }
881
2629
  var compileRegistry = /* @__PURE__ */ new Map();
882
2630
  var REGISTRY_MAX_ENTRIES = 1e4;
@@ -903,6 +2651,9 @@ function registerCompile(appId, archetype, ir, result) {
903
2651
  tokens
904
2652
  );
905
2653
  const shapeKey = `${shape.contextBucket}-${shape.toolCountBucket}-${shape.historyDepth}-${shape.outputMode}`;
2654
+ const toolsCount = result.diagnostics.toolsKept;
2655
+ const historyDepth = Array.isArray(ir.history) ? ir.history.length : 0;
2656
+ const systemPromptChars = estimateSystemPromptChars(ir.sections);
906
2657
  compileRegistry.set(result.handle, {
907
2658
  appId,
908
2659
  archetype,
@@ -912,35 +2663,124 @@ function registerCompile(appId, archetype, ir, result) {
912
2663
  learningKey: learningKey(archetype, result.target, shape),
913
2664
  estimatedTokensIn: tokens,
914
2665
  mutationsApplied: result.mutationsApplied.map((m) => m.id),
2666
+ // alpha.30: cache the in-memory advisories so record() can auto-persist
2667
+ // to `compile_outcome_advisories` without consumer-side threading.
2668
+ advisoriesFromCompile: result.advisories ?? [],
915
2669
  startedAt: Date.now(),
916
- historyCacheableTokens: result.diagnostics.historyCacheableTokens
2670
+ historyCacheableTokens: result.diagnostics.historyCacheableTokens,
2671
+ historyTokensTotal: result.diagnostics.historyTokensTotal,
2672
+ // alpha.20 E3: capture consumer's declared mode for the brain payload.
2673
+ toolOrchestration: result.diagnostics.toolOrchestration,
2674
+ // alpha.28: shape fields for Glass-Box renderer.
2675
+ toolsCount,
2676
+ historyDepth,
2677
+ systemPromptChars,
2678
+ // alpha.29: translator activity — persisted on the brain row so
2679
+ // cross-app aggregates can answer "Sonnet narration rule fired N times,
2680
+ // outcome quality lifted to M."
2681
+ sectionRewritesApplied: result.sectionRewritesApplied,
2682
+ // alpha.57: cache the declared effort tier for record()'s auto-enrich.
2683
+ effortFromCompile: ir.constraints?.effort
917
2684
  });
918
2685
  }
2686
+ function estimateSystemPromptChars(sections) {
2687
+ if (!Array.isArray(sections) || sections.length === 0) return void 0;
2688
+ let total = 0;
2689
+ for (const s of sections) {
2690
+ if (s && typeof s === "object") {
2691
+ const content = s.content;
2692
+ if (typeof content === "string") total += content.length;
2693
+ }
2694
+ }
2695
+ return total > 0 ? total : void 0;
2696
+ }
2697
+ function armCompileConsumeTracking(handle) {
2698
+ const reg = compileRegistry.get(handle);
2699
+ if (!reg) return;
2700
+ reg.consumeTracked = true;
2701
+ reg.systemConsumed = false;
2702
+ reg.modelConsumed = false;
2703
+ reg.rawConsumed = false;
2704
+ }
2705
+ function markCompileConsumed(handle, field) {
2706
+ const reg = compileRegistry.get(handle);
2707
+ if (!reg || !reg.consumeTracked) return;
2708
+ if (field === "system") reg.systemConsumed = true;
2709
+ else if (field === "model") reg.modelConsumed = true;
2710
+ else reg.rawConsumed = true;
2711
+ }
2712
+ function maybeWarnDiscardedCompile(reg, handle) {
2713
+ if (!reg.consumeTracked) return;
2714
+ if (reg.systemConsumed || reg.modelConsumed || reg.rawConsumed) return;
2715
+ const isProd = typeof process !== "undefined" && process.env?.NODE_ENV === "production";
2716
+ const enabled = activeConfig?.warnOnDiscardedCompile ?? !isProd;
2717
+ if (!enabled) return;
2718
+ console.warn(
2719
+ `[kgauto] compile-then-discard: compileForAISDKv6() output for handle ${handle} (archetype=${reg.archetype}, model=${reg.model}) was recorded but neither .system, .model, nor .raw was read before record(). The brain row's mutations_applied / estimated_tokens_in / system_prompt_chars describe a compile that did NOT shape the served call. Pass compiled.system + compiled.model into your generateText/streamText (or use call()). Silence via configureBrain({ warnOnDiscardedCompile: false }).`
2720
+ );
2721
+ }
919
2722
  async function record(input) {
920
2723
  const reg = compileRegistry.get(input.handle);
921
- if (reg) compileRegistry.delete(input.handle);
2724
+ if (reg) {
2725
+ maybeWarnDiscardedCompile(reg, input.handle);
2726
+ compileRegistry.delete(input.handle);
2727
+ }
922
2728
  if (!activeConfig) {
923
2729
  return;
924
2730
  }
925
2731
  const payload = buildPayload(input, reg);
926
2732
  const config = activeConfig;
927
- const fetchFn = config.fetchImpl ?? fetch;
2733
+ const fetchFn = brainWriteFetch(config);
928
2734
  const send = async () => {
2735
+ let outcomeId;
929
2736
  try {
930
2737
  const res = await fetchFn(`${config.endpoint}/outcomes`, {
931
2738
  method: "POST",
932
2739
  headers: {
933
2740
  "Content-Type": "application/json",
2741
+ // alpha.20: request the inserted row back so we can JOIN advisories
2742
+ // to it via outcome_id. PostgREST returns the row when
2743
+ // `Prefer: return=representation` is set; proxies that pass the
2744
+ // header through (the recommended `const row = { ...body }` shape
2745
+ // from OutcomePayload's forward-compat rule) will surface
2746
+ // the row id. Proxies that don't (legacy / hand-rolled shapes)
2747
+ // simply produce no parseable id → secondary advisory POST is
2748
+ // skipped silently. Best-effort — primary outcome row is the
2749
+ // load-bearing write.
2750
+ Prefer: "return=representation",
934
2751
  ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
935
2752
  },
936
2753
  body: JSON.stringify(payload)
937
2754
  });
938
2755
  if (!res.ok) {
939
2756
  const text = await res.text().catch(() => "<no body>");
940
- throw new Error(`brain ${res.status}: ${text}`);
2757
+ throw new Error(describeBrainWriteFailure(res.status, "outcomes", text));
2758
+ }
2759
+ outcomeId = await tryExtractOutcomeId(res);
2760
+ } catch (err) {
2761
+ (config.onError ?? defaultOnError4)(err);
2762
+ return;
2763
+ }
2764
+ const advisories = input.advisories ?? reg?.advisoriesFromCompile;
2765
+ if (!advisories || advisories.length === 0) return;
2766
+ if (outcomeId === void 0) return;
2767
+ try {
2768
+ const advisoryPayload = advisories.map((a) => buildAdvisoryRow(outcomeId, a));
2769
+ const res = await fetchFn(`${config.endpoint}/compile_outcome_advisories`, {
2770
+ method: "POST",
2771
+ headers: {
2772
+ "Content-Type": "application/json",
2773
+ Prefer: "return=minimal",
2774
+ ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
2775
+ },
2776
+ body: JSON.stringify(advisoryPayload)
2777
+ });
2778
+ if (!res.ok) {
2779
+ const text = await res.text().catch(() => "<no body>");
2780
+ throw new Error(`brain advisories ${res.status}: ${text}`);
941
2781
  }
942
2782
  } catch (err) {
943
- (config.onError ?? defaultOnError)(err);
2783
+ (config.onError ?? defaultOnError4)(err);
944
2784
  }
945
2785
  };
946
2786
  if (config.sync) {
@@ -949,9 +2789,14 @@ async function record(input) {
949
2789
  void send();
950
2790
  }
951
2791
  }
952
- function defaultOnError(err) {
2792
+ function defaultOnError4(err) {
953
2793
  console.warn("[kgauto] brain record failed:", err);
954
2794
  }
2795
+ function describeBrainWriteFailure(status, route, body) {
2796
+ const base = `brain ${route} ${status}: ${body}`;
2797
+ if (status !== 404 && status !== 405) return base;
2798
+ return `${base} \u2014 likely cause: the brain proxy at your KGAUTO_V2_BRAIN_URL is missing the /${route} forward route. Self-proxying consumers must implement ALL THREE sibling routes (/outcomes, /probe_outcomes, /compile_outcome_quality) \u2014 see interfaces/kgauto.md "Consumer adoption checklist" route-completeness (L-142).`;
2799
+ }
955
2800
  function buildPayload(input, reg) {
956
2801
  const compileTarget = reg?.model;
957
2802
  const actual = input.actualModel ?? compileTarget;
@@ -959,6 +2804,8 @@ function buildPayload(input, reg) {
959
2804
  const mutationsApplied = input.mutationsApplied ?? reg?.mutationsApplied ?? [];
960
2805
  const costModel = actual;
961
2806
  const costUsdActual = costModel ? computeCostUsd(costModel, input.tokensIn, input.tokensOut) : void 0;
2807
+ const fellOverFrom = input.fellOverFrom ?? requested;
2808
+ const fallbackReason = fellOverFrom ? input.fallbackReason : void 0;
962
2809
  return {
963
2810
  handle: input.handle,
964
2811
  app_id: reg?.appId,
@@ -987,17 +2834,183 @@ function buildPayload(input, reg) {
987
2834
  cache_creation_input_tokens: input.cacheCreationInputTokens,
988
2835
  cost_usd_actual: costUsdActual,
989
2836
  ttft_ms: input.ttftMs,
990
- history_cacheable_tokens: reg?.historyCacheableTokens
2837
+ history_cacheable_tokens: reg?.historyCacheableTokens,
2838
+ // alpha.57 (data-first): input wins over compile-declared; undefined
2839
+ // end-to-end -> key absent from the JSON body entirely (safe against
2840
+ // brains that haven't applied migration 033 yet).
2841
+ effort: input.effort ?? reg?.effortFromCompile,
2842
+ history_tokens_at_compile: reg?.historyTokensTotal,
2843
+ // alpha.20 E3: mirror consumer's declared tool-orchestration mode so
2844
+ // the brain can measure per-mode model perf separately (DeepSeek in
2845
+ // sequential vs parallel mode is two different stories — L-040).
2846
+ // Null when consumer hadn't adopted the constraint yet.
2847
+ tool_orchestration: reg?.toolOrchestration ?? null,
2848
+ // alpha.28 — Glass-Box renderer substrate (migration 018). All optional;
2849
+ // omitted-undefined PostgREST inserts store NULL → renderer renders "—".
2850
+ finish_reason: input.finishReason,
2851
+ total_ms: input.totalMs ?? input.latencyMs,
2852
+ tools_count: input.toolsCount ?? reg?.toolsCount,
2853
+ history_depth: input.historyDepth ?? reg?.historyDepth,
2854
+ system_prompt_chars: input.systemPromptChars ?? reg?.systemPromptChars,
2855
+ fell_over_from: fellOverFrom,
2856
+ fallback_reason: fallbackReason,
2857
+ // alpha.29 — translator activity (migration 019). Send NULL when no
2858
+ // rewrites fired so the brain's "did the translator do anything?"
2859
+ // queries can use `IS NOT NULL` cleanly.
2860
+ section_rewrites_applied: reg?.sectionRewritesApplied && reg.sectionRewritesApplied.length > 0 ? reg.sectionRewritesApplied : null
991
2861
  };
992
2862
  }
993
2863
  function computeCostUsd(modelId, tokensIn, tokensOut) {
994
2864
  if (tokensIn === 0 && tokensOut === 0) return void 0;
2865
+ const brainRow = resolvePricingAt(modelId);
2866
+ if (brainRow && (brainRow.costInputPer1m > 0 || brainRow.costOutputPer1m > 0)) {
2867
+ const inUsd2 = tokensIn / 1e6 * brainRow.costInputPer1m;
2868
+ const outUsd2 = tokensOut / 1e6 * brainRow.costOutputPer1m;
2869
+ return Math.round((inUsd2 + outUsd2) * 1e6) / 1e6;
2870
+ }
995
2871
  const profile = tryGetProfile(modelId);
996
2872
  if (!profile) return void 0;
997
2873
  const inUsd = tokensIn / 1e6 * profile.costInputPer1m;
998
2874
  const outUsd = tokensOut / 1e6 * profile.costOutputPer1m;
999
2875
  return Math.round((inUsd + outUsd) * 1e6) / 1e6;
1000
2876
  }
2877
+ async function tryExtractOutcomeId(res) {
2878
+ try {
2879
+ const ct = res.headers?.get?.("content-type") ?? "";
2880
+ if (ct && !ct.includes("application/json")) return void 0;
2881
+ if (typeof res.json !== "function") return void 0;
2882
+ const body = await res.json();
2883
+ if (Array.isArray(body) && body.length > 0) {
2884
+ const first = body[0];
2885
+ const id = first?.id;
2886
+ if (typeof id === "number") return id;
2887
+ } else if (body && typeof body === "object") {
2888
+ const id = body.id;
2889
+ if (typeof id === "number") return id;
2890
+ }
2891
+ return void 0;
2892
+ } catch {
2893
+ return void 0;
2894
+ }
2895
+ }
2896
+ function buildAdvisoryRow(outcomeId, a) {
2897
+ return {
2898
+ outcome_id: outcomeId,
2899
+ code: a.code,
2900
+ level: a.level,
2901
+ message: a.message,
2902
+ recommendation_type: a.recommendationType ?? null,
2903
+ suggestion: a.suggestion ?? null,
2904
+ docs_url: a.docsUrl ?? null
2905
+ };
2906
+ }
2907
+ async function recordOutcome(input) {
2908
+ if (!activeConfig) {
2909
+ return { ok: false, reason: "brain_not_configured" };
2910
+ }
2911
+ const config = activeConfig;
2912
+ const fetchFn = brainWriteFetch(config);
2913
+ const payload = {
2914
+ outcome_id: input.outcomeId,
2915
+ outcome: input.outcome,
2916
+ rating: input.rating ?? null,
2917
+ reason: input.reason ?? null,
2918
+ observed_confidence: input.observedConfidence ?? null
2919
+ };
2920
+ const send = async () => {
2921
+ try {
2922
+ const res = await fetchFn(`${config.endpoint}/compile_outcome_quality`, {
2923
+ method: "POST",
2924
+ headers: {
2925
+ "Content-Type": "application/json",
2926
+ ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
2927
+ },
2928
+ body: JSON.stringify(payload)
2929
+ });
2930
+ if (!res.ok) {
2931
+ const text = await res.text().catch(() => "<no body>");
2932
+ const err = new Error(
2933
+ describeBrainWriteFailure(res.status, "compile_outcome_quality", text)
2934
+ );
2935
+ (config.onError ?? defaultOnError4)(err);
2936
+ return { ok: false, reason: "persistence_failed" };
2937
+ }
2938
+ return { ok: true };
2939
+ } catch (err) {
2940
+ (config.onError ?? defaultOnError4)(err);
2941
+ return { ok: false, reason: "persistence_failed" };
2942
+ }
2943
+ };
2944
+ if (config.sync) {
2945
+ return send();
2946
+ }
2947
+ void send();
2948
+ return { ok: true };
2949
+ }
2950
+ function isBrainSync() {
2951
+ return activeConfig?.sync === true;
2952
+ }
2953
+ function buildShadowProbeRow(input) {
2954
+ return {
2955
+ app_id: input.appId,
2956
+ intent_archetype: input.archetype,
2957
+ family: input.family,
2958
+ candidate_model: input.candidateModel,
2959
+ current_model: input.currentModel,
2960
+ prompt_hash: input.promptHash,
2961
+ current_response: input.currentResponsePreview ?? null,
2962
+ candidate_response: input.candidateResponsePreview ?? null,
2963
+ judge_verdict: null,
2964
+ judge_score: null,
2965
+ tokens_current_in: input.tokensCurrentIn ?? null,
2966
+ tokens_current_out: input.tokensCurrentOut ?? null,
2967
+ tokens_candidate_in: input.tokensCandidateIn ?? null,
2968
+ tokens_candidate_out: input.tokensCandidateOut ?? null,
2969
+ latency_current_ms: input.latencyCurrentMs ?? null,
2970
+ latency_candidate_ms: input.latencyCandidateMs ?? null,
2971
+ // Full IR was replayed (not a truncated preview), so fidelity is 1.0 — the
2972
+ // prompt-fidelity guard never fires on these rows.
2973
+ prompt_fidelity: 1,
2974
+ replay_source: "inline-full-ir",
2975
+ // alpha — migration 029. 'completed' is the default completed-probe shape;
2976
+ // diagnostic rows (aborted/skipped/errored) pass the explicit class.
2977
+ outcome: input.outcome ?? "completed",
2978
+ // alpha.53 — migration 032. Populated only on candidate_error rows.
2979
+ // Self-proxying consumers must whitelist `error_class` (and `outcome`,
2980
+ // per the alpha.51 PB finding) or diagnostic rows degrade silently.
2981
+ error_class: input.errorClass ?? null
2982
+ };
2983
+ }
2984
+ async function recordShadowProbe(input) {
2985
+ if (!activeConfig) return;
2986
+ const config = activeConfig;
2987
+ const fetchFn = brainWriteFetch(config);
2988
+ const row = buildShadowProbeRow(input);
2989
+ const send = async () => {
2990
+ try {
2991
+ const res = await fetchFn(`${config.endpoint}/probe_outcomes`, {
2992
+ method: "POST",
2993
+ headers: {
2994
+ "Content-Type": "application/json",
2995
+ Prefer: "return=minimal",
2996
+ ...config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}
2997
+ },
2998
+ body: JSON.stringify(row)
2999
+ });
3000
+ if (!res.ok) {
3001
+ const text = await res.text().catch(() => "<no body>");
3002
+ throw new Error(describeBrainWriteFailure(res.status, "probe_outcomes", text));
3003
+ }
3004
+ } catch (err) {
3005
+ (config.onError ?? defaultOnError4)(err);
3006
+ }
3007
+ };
3008
+ if (config.sync) {
3009
+ await send();
3010
+ } else {
3011
+ void send();
3012
+ }
3013
+ }
1001
3014
 
1002
3015
  // src/ir.ts
1003
3016
  var CallError = class extends Error {
@@ -1011,10 +3024,297 @@ var CallError = class extends Error {
1011
3024
  this.lastStatus = lastStatus;
1012
3025
  this.lastErrorCode = lastErrorCode;
1013
3026
  }
1014
- };
3027
+ };
3028
+
3029
+ // src/streaming.ts
3030
+ var ANTHROPIC_URL = "https://api.anthropic.com/v1/messages";
3031
+ async function streamAnthropic(request, apiKey, opts) {
3032
+ const { provider: _provider, ...body } = request;
3033
+ const fetchFn = opts.fetchImpl ?? fetch;
3034
+ let res;
3035
+ try {
3036
+ res = await fetchFn(ANTHROPIC_URL, {
3037
+ method: "POST",
3038
+ headers: {
3039
+ "x-api-key": apiKey,
3040
+ "anthropic-version": "2023-06-01",
3041
+ "content-type": "application/json"
3042
+ },
3043
+ body: JSON.stringify({ ...body, stream: true })
3044
+ });
3045
+ } catch (err) {
3046
+ return retryableError(0, "network_error", String(err), null);
3047
+ }
3048
+ if (!res.ok) {
3049
+ const errBody = await res.json().catch(() => ({}));
3050
+ return classifyHttpError(res.status, errBody);
3051
+ }
3052
+ let text = "";
3053
+ let inputTokens = 0;
3054
+ let outputTokens = 0;
3055
+ let cacheReadTokens;
3056
+ let cacheCreatedTokens;
3057
+ let stopReason;
3058
+ const toolBlocks = /* @__PURE__ */ new Map();
3059
+ try {
3060
+ await parseSSEStream(res, (event) => {
3061
+ if (!event.data) return;
3062
+ let payload;
3063
+ try {
3064
+ payload = JSON.parse(event.data);
3065
+ } catch {
3066
+ return;
3067
+ }
3068
+ const type = payload.type;
3069
+ if (type === "message_start") {
3070
+ const msg = payload.message;
3071
+ const usage = msg?.usage;
3072
+ if (usage) {
3073
+ inputTokens = usage.input_tokens ?? 0;
3074
+ outputTokens = usage.output_tokens ?? 0;
3075
+ if (typeof usage.cache_read_input_tokens === "number")
3076
+ cacheReadTokens = usage.cache_read_input_tokens;
3077
+ if (typeof usage.cache_creation_input_tokens === "number")
3078
+ cacheCreatedTokens = usage.cache_creation_input_tokens;
3079
+ }
3080
+ return;
3081
+ }
3082
+ if (type === "content_block_start") {
3083
+ const p = payload;
3084
+ const idx = p.index ?? 0;
3085
+ const block = p.content_block;
3086
+ if (block?.type === "tool_use" && block.id && block.name) {
3087
+ toolBlocks.set(idx, { id: block.id, name: block.name, argsJson: "" });
3088
+ }
3089
+ return;
3090
+ }
3091
+ if (type === "content_block_delta") {
3092
+ const p = payload;
3093
+ const delta = p.delta;
3094
+ if (!delta) return;
3095
+ if (delta.type === "text_delta" && typeof delta.text === "string") {
3096
+ text += delta.text;
3097
+ opts.onChunk(delta.text);
3098
+ } else if (delta.type === "input_json_delta" && typeof delta.partial_json === "string") {
3099
+ const idx = p.index ?? 0;
3100
+ const tool = toolBlocks.get(idx);
3101
+ if (tool) tool.argsJson += delta.partial_json;
3102
+ }
3103
+ return;
3104
+ }
3105
+ if (type === "message_delta") {
3106
+ const p = payload;
3107
+ if (p.delta?.stop_reason) stopReason = p.delta.stop_reason;
3108
+ if (typeof p.usage?.output_tokens === "number") outputTokens = p.usage.output_tokens;
3109
+ return;
3110
+ }
3111
+ });
3112
+ } catch (err) {
3113
+ return retryableError(0, "stream_interrupted", String(err), null);
3114
+ }
3115
+ const toolCalls = Array.from(toolBlocks.values()).map((b) => ({
3116
+ id: b.id,
3117
+ name: b.name,
3118
+ args: tryParseJson(b.argsJson) ?? {}
3119
+ }));
3120
+ const tokens = {
3121
+ input: inputTokens,
3122
+ output: outputTokens,
3123
+ total: inputTokens + outputTokens,
3124
+ cached: cacheReadTokens,
3125
+ cacheCreated: cacheCreatedTokens
3126
+ };
3127
+ const response = {
3128
+ text,
3129
+ structuredOutput: null,
3130
+ toolCalls,
3131
+ tokens,
3132
+ finishReason: stopReason,
3133
+ raw: { streamed: true, provider: "anthropic" }
3134
+ };
3135
+ return { ok: true, status: res.status, response };
3136
+ }
3137
+ async function streamOpenAILike(url, request, apiKey, providerLabel, opts) {
3138
+ const { provider: _provider, ...body } = request;
3139
+ const fetchFn = opts.fetchImpl ?? fetch;
3140
+ const reqBody = {
3141
+ ...body,
3142
+ stream: true,
3143
+ stream_options: {
3144
+ ...body.stream_options ?? {},
3145
+ include_usage: true
3146
+ }
3147
+ };
3148
+ let res;
3149
+ try {
3150
+ res = await fetchFn(url, {
3151
+ method: "POST",
3152
+ headers: {
3153
+ authorization: `Bearer ${apiKey}`,
3154
+ "content-type": "application/json"
3155
+ },
3156
+ body: JSON.stringify(reqBody)
3157
+ });
3158
+ } catch (err) {
3159
+ return retryableError(0, "network_error", String(err), null);
3160
+ }
3161
+ if (!res.ok) {
3162
+ const errBody = await res.json().catch(() => ({}));
3163
+ return classifyHttpError(res.status, errBody);
3164
+ }
3165
+ let text = "";
3166
+ let finishReason;
3167
+ let inputTokens = 0;
3168
+ let outputTokens = 0;
3169
+ let totalTokens;
3170
+ let cachedTokens;
3171
+ const toolBuffers = /* @__PURE__ */ new Map();
3172
+ try {
3173
+ await parseSSEStream(res, (event) => {
3174
+ if (!event.data) return;
3175
+ if (event.data === "[DONE]") return;
3176
+ let payload;
3177
+ try {
3178
+ payload = JSON.parse(event.data);
3179
+ } catch {
3180
+ return;
3181
+ }
3182
+ const choices = payload.choices;
3183
+ if (choices && choices.length > 0) {
3184
+ const choice = choices[0];
3185
+ const deltaContent = choice.delta?.content;
3186
+ if (typeof deltaContent === "string" && deltaContent.length > 0) {
3187
+ text += deltaContent;
3188
+ opts.onChunk(deltaContent);
3189
+ }
3190
+ const toolDeltas = choice.delta?.tool_calls;
3191
+ if (toolDeltas) {
3192
+ for (const tcDelta of toolDeltas) {
3193
+ const idx = tcDelta.index ?? 0;
3194
+ let buf = toolBuffers.get(idx);
3195
+ if (!buf) {
3196
+ buf = { id: tcDelta.id ?? `tc-${idx}`, name: "", argsJson: "" };
3197
+ toolBuffers.set(idx, buf);
3198
+ }
3199
+ if (tcDelta.id) buf.id = tcDelta.id;
3200
+ if (tcDelta.function?.name) buf.name += tcDelta.function.name;
3201
+ if (tcDelta.function?.arguments) buf.argsJson += tcDelta.function.arguments;
3202
+ }
3203
+ }
3204
+ if (choice.finish_reason) finishReason = choice.finish_reason;
3205
+ }
3206
+ const usage = payload.usage;
3207
+ if (usage) {
3208
+ if (typeof usage.prompt_tokens === "number") inputTokens = usage.prompt_tokens;
3209
+ if (typeof usage.completion_tokens === "number") outputTokens = usage.completion_tokens;
3210
+ if (typeof usage.total_tokens === "number") totalTokens = usage.total_tokens;
3211
+ const details = usage.prompt_tokens_details;
3212
+ if (typeof details?.cached_tokens === "number") cachedTokens = details.cached_tokens;
3213
+ }
3214
+ });
3215
+ } catch (err) {
3216
+ return retryableError(0, "stream_interrupted", String(err), null);
3217
+ }
3218
+ const toolCalls = Array.from(toolBuffers.values()).filter((b) => b.name.length > 0).map((b) => ({
3219
+ id: b.id,
3220
+ name: b.name,
3221
+ args: tryParseJson(b.argsJson) ?? {}
3222
+ }));
3223
+ const tokens = {
3224
+ input: inputTokens,
3225
+ output: outputTokens,
3226
+ total: totalTokens ?? inputTokens + outputTokens,
3227
+ cached: cachedTokens
3228
+ };
3229
+ const response = {
3230
+ text,
3231
+ structuredOutput: null,
3232
+ toolCalls,
3233
+ tokens,
3234
+ finishReason,
3235
+ raw: { streamed: true, provider: providerLabel }
3236
+ };
3237
+ return { ok: true, status: res.status, response };
3238
+ }
3239
+ async function parseSSEStream(response, handler) {
3240
+ const body = response.body;
3241
+ if (!body) throw new Error("Response has no body for SSE parse");
3242
+ const reader = body.getReader();
3243
+ const decoder = new TextDecoder("utf-8");
3244
+ let buffer = "";
3245
+ for (; ; ) {
3246
+ const { value, done } = await reader.read();
3247
+ if (done) break;
3248
+ buffer += decoder.decode(value, { stream: true });
3249
+ let sep;
3250
+ while (sep = buffer.indexOf("\n\n"), sep !== -1) {
3251
+ const block = buffer.slice(0, sep);
3252
+ buffer = buffer.slice(sep + 2);
3253
+ const event = parseSSEBlock(block);
3254
+ if (event) handler(event);
3255
+ }
3256
+ }
3257
+ if (buffer.length > 0) {
3258
+ const event = parseSSEBlock(buffer);
3259
+ if (event) handler(event);
3260
+ }
3261
+ }
3262
+ function parseSSEBlock(block) {
3263
+ const lines = block.split(/\r?\n/);
3264
+ let eventName;
3265
+ const dataLines = [];
3266
+ for (const line of lines) {
3267
+ if (line.startsWith(":")) continue;
3268
+ if (line.startsWith("event:")) {
3269
+ eventName = line.slice(6).trim();
3270
+ } else if (line.startsWith("data:")) {
3271
+ dataLines.push(line.slice(5).trim());
3272
+ }
3273
+ }
3274
+ if (dataLines.length === 0) return void 0;
3275
+ return { event: eventName, data: dataLines.join("\n") };
3276
+ }
3277
+ function tryParseJson(s) {
3278
+ if (typeof s !== "string" || s.length === 0) return void 0;
3279
+ try {
3280
+ const parsed = JSON.parse(s);
3281
+ return typeof parsed === "object" && parsed !== null ? parsed : void 0;
3282
+ } catch {
3283
+ return void 0;
3284
+ }
3285
+ }
3286
+ function classifyHttpError(status, body) {
3287
+ const message = extractErrorMessage(body) ?? `HTTP ${status}`;
3288
+ if (status === 429)
3289
+ return { ok: false, status, errorType: "retryable", errorCode: "rate_limit", message, raw: body };
3290
+ if (status === 408)
3291
+ return { ok: false, status, errorType: "retryable", errorCode: "timeout", message, raw: body };
3292
+ if (status >= 500)
3293
+ return { ok: false, status, errorType: "retryable", errorCode: "server_error", message, raw: body };
3294
+ if (status === 404)
3295
+ return { ok: false, status, errorType: "retryable", errorCode: "model_not_found", message, raw: body };
3296
+ if (status === 401 || status === 403)
3297
+ return { ok: false, status, errorType: "terminal", errorCode: "auth", message, raw: body };
3298
+ if (status === 400)
3299
+ return { ok: false, status, errorType: "terminal", errorCode: "invalid_request", message, raw: body };
3300
+ return { ok: false, status, errorType: "terminal", errorCode: "unknown", message, raw: body };
3301
+ }
3302
+ function extractErrorMessage(body) {
3303
+ if (!body || typeof body !== "object") return void 0;
3304
+ const b = body;
3305
+ if (b.error && typeof b.error === "object") {
3306
+ const e = b.error;
3307
+ if (typeof e.message === "string") return e.message;
3308
+ }
3309
+ if (typeof b.message === "string") return b.message;
3310
+ return void 0;
3311
+ }
3312
+ function retryableError(status, code, message, raw) {
3313
+ return { ok: false, status, errorType: "retryable", errorCode: code, message, raw };
3314
+ }
1015
3315
 
1016
3316
  // src/execute.ts
1017
- var ANTHROPIC_URL = "https://api.anthropic.com/v1/messages";
3317
+ var ANTHROPIC_URL2 = "https://api.anthropic.com/v1/messages";
1018
3318
  var OPENAI_URL = "https://api.openai.com/v1/chat/completions";
1019
3319
  var DEEPSEEK_URL = "https://api.deepseek.com/chat/completions";
1020
3320
  async function execute(request, opts = {}) {
@@ -1035,16 +3335,22 @@ async function execute(request, opts = {}) {
1035
3335
  }
1036
3336
  }
1037
3337
  async function executeAnthropic(request, opts) {
1038
- const apiKey = opts.apiKeys?.anthropic ?? process.env.ANTHROPIC_API_KEY;
3338
+ const apiKey = resolveProviderKey("anthropic", { apiKeys: opts.apiKeys });
1039
3339
  if (!apiKey) {
1040
3340
  return terminalError(401, "auth", "ANTHROPIC_API_KEY missing");
1041
3341
  }
3342
+ if (opts.onChunk) {
3343
+ return streamAnthropic(request, apiKey, {
3344
+ onChunk: opts.onChunk,
3345
+ fetchImpl: opts.fetchImpl
3346
+ });
3347
+ }
1042
3348
  const { provider: _provider, ...body } = request;
1043
3349
  const fetchFn = opts.fetchImpl ?? fetch;
1044
3350
  let res;
1045
3351
  let json;
1046
3352
  try {
1047
- res = await fetchFn(ANTHROPIC_URL, {
3353
+ res = await fetchFn(ANTHROPIC_URL2, {
1048
3354
  method: "POST",
1049
3355
  headers: {
1050
3356
  "x-api-key": apiKey,
@@ -1055,9 +3361,9 @@ async function executeAnthropic(request, opts) {
1055
3361
  });
1056
3362
  json = await res.json().catch(() => ({}));
1057
3363
  } catch (err) {
1058
- return retryableError(0, "network_error", String(err), null);
3364
+ return retryableError2(0, "network_error", String(err), null);
1059
3365
  }
1060
- if (!res.ok) return classifyHttpError(res.status, json);
3366
+ if (!res.ok) return classifyHttpError2(res.status, json);
1061
3367
  return { ok: true, status: res.status, response: normalizeAnthropic(json) };
1062
3368
  }
1063
3369
  function normalizeAnthropic(raw) {
@@ -1074,7 +3380,7 @@ function normalizeAnthropic(raw) {
1074
3380
  return { text, structuredOutput: null, toolCalls, tokens, finishReason: r.stop_reason, raw };
1075
3381
  }
1076
3382
  async function executeGoogle(request, opts) {
1077
- const apiKey = opts.apiKeys?.google ?? process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY;
3383
+ const apiKey = resolveProviderKey("google", { apiKeys: opts.apiKeys });
1078
3384
  if (!apiKey) {
1079
3385
  return terminalError(401, "auth", "GOOGLE_API_KEY/GEMINI_API_KEY missing");
1080
3386
  }
@@ -1091,9 +3397,9 @@ async function executeGoogle(request, opts) {
1091
3397
  });
1092
3398
  json = await res.json().catch(() => ({}));
1093
3399
  } catch (err) {
1094
- return retryableError(0, "network_error", String(err), null);
3400
+ return retryableError2(0, "network_error", String(err), null);
1095
3401
  }
1096
- if (!res.ok) return classifyHttpError(res.status, json);
3402
+ if (!res.ok) return classifyHttpError2(res.status, json);
1097
3403
  return { ok: true, status: res.status, response: normalizeGoogle(json) };
1098
3404
  }
1099
3405
  function normalizeGoogle(raw) {
@@ -1116,10 +3422,16 @@ function normalizeGoogle(raw) {
1116
3422
  return { text, structuredOutput: null, toolCalls, tokens, finishReason: candidate?.finishReason, raw };
1117
3423
  }
1118
3424
  async function executeOpenAI(request, opts) {
1119
- const apiKey = opts.apiKeys?.openai ?? process.env.OPENAI_API_KEY;
3425
+ const apiKey = resolveProviderKey("openai", { apiKeys: opts.apiKeys });
1120
3426
  if (!apiKey) {
1121
3427
  return terminalError(401, "auth", "OPENAI_API_KEY missing");
1122
3428
  }
3429
+ if (opts.onChunk) {
3430
+ return streamOpenAILike(OPENAI_URL, request, apiKey, "openai", {
3431
+ onChunk: opts.onChunk,
3432
+ fetchImpl: opts.fetchImpl
3433
+ });
3434
+ }
1123
3435
  const { provider: _provider, ...body } = request;
1124
3436
  const fetchFn = opts.fetchImpl ?? fetch;
1125
3437
  let res;
@@ -1132,16 +3444,22 @@ async function executeOpenAI(request, opts) {
1132
3444
  });
1133
3445
  json = await res.json().catch(() => ({}));
1134
3446
  } catch (err) {
1135
- return retryableError(0, "network_error", String(err), null);
3447
+ return retryableError2(0, "network_error", String(err), null);
1136
3448
  }
1137
- if (!res.ok) return classifyHttpError(res.status, json);
3449
+ if (!res.ok) return classifyHttpError2(res.status, json);
1138
3450
  return { ok: true, status: res.status, response: normalizeOpenAILike(json) };
1139
3451
  }
1140
3452
  async function executeDeepSeek(request, opts) {
1141
- const apiKey = opts.apiKeys?.deepseek ?? process.env.DEEPSEEK_API_KEY;
3453
+ const apiKey = resolveProviderKey("deepseek", { apiKeys: opts.apiKeys });
1142
3454
  if (!apiKey) {
1143
3455
  return terminalError(401, "auth", "DEEPSEEK_API_KEY missing");
1144
3456
  }
3457
+ if (opts.onChunk) {
3458
+ return streamOpenAILike(DEEPSEEK_URL, request, apiKey, "deepseek", {
3459
+ onChunk: opts.onChunk,
3460
+ fetchImpl: opts.fetchImpl
3461
+ });
3462
+ }
1145
3463
  const { provider: _provider, ...body } = request;
1146
3464
  const fetchFn = opts.fetchImpl ?? fetch;
1147
3465
  let res;
@@ -1154,9 +3472,9 @@ async function executeDeepSeek(request, opts) {
1154
3472
  });
1155
3473
  json = await res.json().catch(() => ({}));
1156
3474
  } catch (err) {
1157
- return retryableError(0, "network_error", String(err), null);
3475
+ return retryableError2(0, "network_error", String(err), null);
1158
3476
  }
1159
- if (!res.ok) return classifyHttpError(res.status, json);
3477
+ if (!res.ok) return classifyHttpError2(res.status, json);
1160
3478
  return { ok: true, status: res.status, response: normalizeOpenAILike(json) };
1161
3479
  }
1162
3480
  function normalizeOpenAILike(raw) {
@@ -1166,7 +3484,7 @@ function normalizeOpenAILike(raw) {
1166
3484
  const toolCalls = (choice?.message?.tool_calls ?? []).filter((tc) => tc.function?.name).map((tc, i) => ({
1167
3485
  id: tc.id ?? `tc-${i}`,
1168
3486
  name: tc.function.name,
1169
- args: tryParseJson(tc.function?.arguments) ?? {}
3487
+ args: tryParseJson2(tc.function?.arguments) ?? {}
1170
3488
  }));
1171
3489
  const u = r.usage ?? {};
1172
3490
  const tokens = {
@@ -1183,8 +3501,8 @@ function applyOverrides(request, overrides) {
1183
3501
  if (!layer) return request;
1184
3502
  return { ...request, ...layer };
1185
3503
  }
1186
- function classifyHttpError(status, body) {
1187
- const message = extractErrorMessage(body) ?? `HTTP ${status}`;
3504
+ function classifyHttpError2(status, body) {
3505
+ const message = extractErrorMessage2(body) ?? `HTTP ${status}`;
1188
3506
  if (status === 429) {
1189
3507
  return { ok: false, status, errorType: "retryable", errorCode: "rate_limit", message, raw: body };
1190
3508
  }
@@ -1205,7 +3523,7 @@ function classifyHttpError(status, body) {
1205
3523
  }
1206
3524
  return { ok: false, status, errorType: "terminal", errorCode: "unknown", message, raw: body };
1207
3525
  }
1208
- function extractErrorMessage(body) {
3526
+ function extractErrorMessage2(body) {
1209
3527
  if (!body || typeof body !== "object") return void 0;
1210
3528
  const b = body;
1211
3529
  if (b.error && typeof b.error === "object") {
@@ -1218,10 +3536,10 @@ function extractErrorMessage(body) {
1218
3536
  function terminalError(status, code, message) {
1219
3537
  return { ok: false, status, errorType: "terminal", errorCode: code, message, raw: null };
1220
3538
  }
1221
- function retryableError(status, code, message, raw) {
3539
+ function retryableError2(status, code, message, raw) {
1222
3540
  return { ok: false, status, errorType: "retryable", errorCode: code, message, raw };
1223
3541
  }
1224
- function tryParseJson(s) {
3542
+ function tryParseJson2(s) {
1225
3543
  if (typeof s !== "string" || s.length === 0) return void 0;
1226
3544
  try {
1227
3545
  const parsed = JSON.parse(s);
@@ -1233,15 +3551,148 @@ function tryParseJson(s) {
1233
3551
 
1234
3552
  // src/call.ts
1235
3553
  async function call(ir, opts = {}) {
3554
+ const traceId = generateTraceId();
3555
+ safeEmit(
3556
+ () => emitCompileStart(traceId, ir.appId, {
3557
+ appId: ir.appId,
3558
+ archetype: ir.intent.archetype,
3559
+ // alpha.41: ir.models is ChainModelEntry[] (string | { family }).
3560
+ // Glass-Box renderer carries string ids only; surface family entries
3561
+ // as their family tag prefixed so the panel still shows them in the
3562
+ // pre-resolution log.
3563
+ models: ir.models.map(
3564
+ (m) => typeof m === "string" ? m : `family:${m.family}`
3565
+ )
3566
+ })
3567
+ );
1236
3568
  const initial = compileAndRegister(ir, opts);
3569
+ safeEmit(
3570
+ () => emitCompileDone(traceId, ir.appId, {
3571
+ target: initial.target,
3572
+ provider: initial.provider,
3573
+ fallbackChain: initial.fallbackChain,
3574
+ tokensIn: initial.tokensIn,
3575
+ estimatedCostUsd: initial.estimatedCostUsd,
3576
+ mutationsApplied: initial.mutationsApplied,
3577
+ advisories: initial.advisories
3578
+ })
3579
+ );
3580
+ for (const adv of initial.advisories) {
3581
+ safeEmit(
3582
+ () => emitAdvisoryFired(traceId, ir.appId, { code: adv.code, message: adv.message })
3583
+ );
3584
+ }
1237
3585
  const start = Date.now();
1238
3586
  const attempts = [];
1239
- const targetsToTry = [initial.target, ...initial.fallbackChain];
3587
+ const rawTargets = [initial.target, ...initial.fallbackChain];
3588
+ let unreachableFiltered;
3589
+ let targetsToTry;
3590
+ if (opts.noAutoFilter) {
3591
+ targetsToTry = rawTargets;
3592
+ } else {
3593
+ const dropped = [];
3594
+ targetsToTry = [];
3595
+ for (const t of rawTargets) {
3596
+ if (isModelReachable(t, { apiKeys: opts.apiKeys })) {
3597
+ targetsToTry.push(t);
3598
+ } else {
3599
+ dropped.push(t);
3600
+ }
3601
+ }
3602
+ unreachableFiltered = dropped;
3603
+ if (targetsToTry.length === 0) {
3604
+ const latencyMs2 = Date.now() - start;
3605
+ await record({
3606
+ handle: initial.handle,
3607
+ tokensIn: 0,
3608
+ tokensOut: 0,
3609
+ latencyMs: latencyMs2,
3610
+ success: false,
3611
+ errorType: "no_reachable_models",
3612
+ promptPreview: extractPromptPreview(ir)
3613
+ });
3614
+ const noReachableAttempts = dropped.map((m) => ({
3615
+ model: m,
3616
+ status: "terminal",
3617
+ errorCode: "unreachable_provider",
3618
+ message: `No API key for ${m}'s provider \u2014 set one of PROVIDER_ENV_KEYS or pass apiKeys`
3619
+ }));
3620
+ throw new CallError(
3621
+ `call(): no reachable models in chain. Filtered: [${dropped.join(", ")}]. Add a key for one provider, or pass apiKeys.`,
3622
+ noReachableAttempts,
3623
+ void 0,
3624
+ "no_reachable_models"
3625
+ );
3626
+ }
3627
+ const archetypeName = ir.intent?.archetype;
3628
+ if (archetypeName) {
3629
+ const ensured = ensureCrossProviderTail({
3630
+ chain: targetsToTry,
3631
+ archetype: archetypeName,
3632
+ apiKeys: opts.apiKeys
3633
+ });
3634
+ if (ensured.appended) {
3635
+ targetsToTry = ensured.chain;
3636
+ }
3637
+ }
3638
+ }
3639
+ let policyBlockedFiltered;
3640
+ if (opts.policy?.blockedModels && opts.policy.blockedModels.length > 0) {
3641
+ const blocked = new Set(opts.policy.blockedModels);
3642
+ const filtered = [];
3643
+ const dropped = [];
3644
+ for (const t of targetsToTry) {
3645
+ if (blocked.has(t)) {
3646
+ dropped.push(t);
3647
+ } else {
3648
+ filtered.push(t);
3649
+ }
3650
+ }
3651
+ if (dropped.length > 0) {
3652
+ policyBlockedFiltered = dropped;
3653
+ targetsToTry = filtered;
3654
+ }
3655
+ if (targetsToTry.length === 0) {
3656
+ const latencyMs2 = Date.now() - start;
3657
+ await record({
3658
+ handle: initial.handle,
3659
+ tokensIn: 0,
3660
+ tokensOut: 0,
3661
+ latencyMs: latencyMs2,
3662
+ success: false,
3663
+ errorType: "all_blocked_by_policy",
3664
+ promptPreview: extractPromptPreview(ir)
3665
+ });
3666
+ const blockedAttempts = dropped.map((m) => ({
3667
+ model: m,
3668
+ status: "terminal",
3669
+ errorCode: "blocked_by_policy",
3670
+ message: `Skipped \u2014 model ${m} is in CompilePolicy.blockedModels`
3671
+ }));
3672
+ throw new CallError(
3673
+ `call(): all chain targets blocked by CompilePolicy.blockedModels: [${dropped.join(", ")}]`,
3674
+ blockedAttempts,
3675
+ void 0,
3676
+ "all_blocked_by_policy"
3677
+ );
3678
+ }
3679
+ }
1240
3680
  let activeCompile = initial;
1241
3681
  let lastErr;
3682
+ const failedProviders = /* @__PURE__ */ new Set();
1242
3683
  for (let i = 0; i < targetsToTry.length; i++) {
1243
3684
  const targetModel = targetsToTry[i];
1244
- if (i > 0) {
3685
+ const targetProfile = tryGetProfile(targetModel);
3686
+ if (targetProfile && failedProviders.has(targetProfile.provider) && !opts.noFallback) {
3687
+ attempts.push({
3688
+ model: targetModel,
3689
+ status: "terminal",
3690
+ errorCode: "auth_inferred",
3691
+ message: `Skipped \u2014 provider ${targetProfile.provider} returned 401/403 earlier in this call; same key inferred to fail`
3692
+ });
3693
+ continue;
3694
+ }
3695
+ if (targetModel !== initial.target) {
1245
3696
  try {
1246
3697
  activeCompile = compileAndRegister(
1247
3698
  {
@@ -1261,49 +3712,117 @@ async function call(ir, opts = {}) {
1261
3712
  continue;
1262
3713
  }
1263
3714
  }
3715
+ safeEmit(
3716
+ () => emitExecuteAttempt(traceId, ir.appId, { model: targetModel, attemptIndex: i })
3717
+ );
3718
+ const targetSupportsStreaming = targetProfile?.streaming === true;
3719
+ const streamingOnChunk = opts.onChunk && !opts.noStream && targetSupportsStreaming ? opts.onChunk : void 0;
1264
3720
  const exec = await execute(activeCompile.request, {
1265
3721
  apiKeys: opts.apiKeys,
1266
3722
  fetchImpl: opts.fetchImpl,
1267
- providerOverrides: opts.providerOverrides
3723
+ providerOverrides: opts.providerOverrides,
3724
+ onChunk: streamingOnChunk
1268
3725
  });
1269
- if (exec.ok) {
3726
+ const validated = exec.ok ? validateStructuredContract(exec, ir) : exec;
3727
+ if (validated.ok) {
1270
3728
  attempts.push({ model: targetModel, status: "success" });
1271
3729
  const latencyMs2 = Date.now() - start;
1272
- const responseWithStructured = withStructuredOutput(exec.response, ir);
3730
+ safeEmit(
3731
+ () => emitExecuteSuccess(traceId, ir.appId, {
3732
+ model: targetModel,
3733
+ tokensIn: validated.response.tokens.input,
3734
+ tokensOut: validated.response.tokens.output,
3735
+ latencyMs: latencyMs2
3736
+ })
3737
+ );
3738
+ const fellOver = targetModel !== initial.target;
3739
+ const fallbackReason = fellOver ? normalizeFallbackReason(attempts) : void 0;
1273
3740
  await record({
1274
3741
  handle: initial.handle,
1275
- tokensIn: responseWithStructured.tokens.input,
1276
- tokensOut: responseWithStructured.tokens.output,
3742
+ tokensIn: validated.response.tokens.input,
3743
+ tokensOut: validated.response.tokens.output,
1277
3744
  latencyMs: latencyMs2,
1278
3745
  success: true,
1279
- emptyResponse: responseWithStructured.tokens.output === 0,
1280
- toolsCalled: responseWithStructured.toolCalls.map((tc) => tc.name),
3746
+ emptyResponse: validated.response.tokens.output === 0,
3747
+ toolsCalled: validated.response.toolCalls.map((tc) => tc.name),
1281
3748
  actualModel: targetModel !== initial.target ? targetModel : void 0,
1282
3749
  mutationsApplied: targetModel !== initial.target ? activeCompile.mutationsApplied.map((m) => m.id) : void 0,
1283
3750
  promptPreview: extractPromptPreview(ir),
1284
- responsePreview: responseWithStructured.text.slice(0, 200),
1285
- cacheReadInputTokens: responseWithStructured.tokens.cached,
1286
- cacheCreationInputTokens: responseWithStructured.tokens.cacheCreated
3751
+ responsePreview: validated.response.text.slice(0, 200),
3752
+ cacheReadInputTokens: validated.response.tokens.cached,
3753
+ cacheCreationInputTokens: validated.response.tokens.cacheCreated,
3754
+ // alpha.28 — Glass-Box renderer substrate (migration 018). call()
3755
+ // owns the lifecycle so it has direct visibility into finishReason
3756
+ // (from the normalized provider response), totalMs (mirrors latencyMs
3757
+ // for non-streaming; future streaming variant may diverge), and the
3758
+ // fell-over-from / fallback-reason pair (already computed above for
3759
+ // the CallResult return shape).
3760
+ finishReason: validated.response.finishReason,
3761
+ totalMs: latencyMs2,
3762
+ fellOverFrom: fellOver ? initial.target : void 0,
3763
+ fallbackReason
1287
3764
  });
3765
+ if (fellOver) {
3766
+ const firstFailed = attempts.find((a) => a.status !== "success");
3767
+ if (firstFailed) {
3768
+ safeEmit(
3769
+ () => emitFallbackWalked(traceId, ir.appId, {
3770
+ from: initial.target,
3771
+ to: targetModel,
3772
+ reason: fallbackReason ?? "unknown",
3773
+ attempt: firstFailed
3774
+ })
3775
+ );
3776
+ }
3777
+ }
3778
+ if (opts.shadowProbe) {
3779
+ const probe = runShadowProbe({
3780
+ ir,
3781
+ opts,
3782
+ servedModel: targetModel,
3783
+ servedResponse: validated.response,
3784
+ servedLatencyMs: latencyMs2
3785
+ });
3786
+ if (isBrainSync()) {
3787
+ await probe;
3788
+ } else {
3789
+ void probe;
3790
+ }
3791
+ }
1288
3792
  return {
1289
3793
  handle: initial.handle,
1290
3794
  actualModel: targetModel,
1291
3795
  requestedModel: initial.target,
1292
3796
  provider: activeCompile.provider,
1293
- response: responseWithStructured,
3797
+ response: validated.response,
1294
3798
  latencyMs: latencyMs2,
1295
3799
  mutationsApplied: activeCompile.mutationsApplied,
1296
- attempts
3800
+ attempts,
3801
+ servedBy: targetModel,
3802
+ fellOverFrom: fellOver ? initial.target : void 0,
3803
+ fallbackReason,
3804
+ unreachableFiltered,
3805
+ policyBlockedFiltered,
3806
+ traceId,
3807
+ // alpha.44: surface the served compile's advisories so call()
3808
+ // consumers (agent paths) can logAdvisories() like compile()
3809
+ // consumers (chat/intake) do. activeCompile, not initial — parity
3810
+ // with mutationsApplied above on fallback. Closes IC's side-finding.
3811
+ advisories: activeCompile.advisories
1297
3812
  };
1298
3813
  }
1299
3814
  attempts.push({
1300
3815
  model: targetModel,
1301
- status: exec.errorType,
1302
- errorCode: exec.errorCode,
1303
- message: exec.message
3816
+ status: validated.errorType,
3817
+ errorCode: validated.errorCode,
3818
+ message: validated.message
1304
3819
  });
1305
- lastErr = exec;
1306
- if (exec.errorType === "terminal" || opts.noFallback) {
3820
+ lastErr = validated;
3821
+ if (validated.errorType === "terminal" || opts.noFallback) {
3822
+ if (validated.errorCode === "auth" && !opts.noFallback && activeCompile.provider) {
3823
+ failedProviders.add(activeCompile.provider);
3824
+ continue;
3825
+ }
1307
3826
  break;
1308
3827
  }
1309
3828
  }
@@ -1317,8 +3836,10 @@ async function call(ir, opts = {}) {
1317
3836
  errorType: lastErr?.errorCode,
1318
3837
  promptPreview: extractPromptPreview(ir)
1319
3838
  });
3839
+ const filteredNote = unreachableFiltered && unreachableFiltered.length > 0 ? ` (also auto-filtered: [${unreachableFiltered.join(", ")}] \u2014 no API key)` : "";
3840
+ const blockedNote = policyBlockedFiltered && policyBlockedFiltered.length > 0 ? ` (also policy-blocked: [${policyBlockedFiltered.join(", ")}])` : "";
1320
3841
  throw new CallError(
1321
- `call(): all attempts failed${lastErr ? ` \u2014 ${lastErr.errorCode}: ${lastErr.message}` : ""}`,
3842
+ `call(): all attempts failed${lastErr ? ` \u2014 ${lastErr.errorCode}: ${lastErr.message}` : ""}${filteredNote}${blockedNote}`,
1322
3843
  attempts,
1323
3844
  lastErr?.status,
1324
3845
  lastErr?.errorCode
@@ -1340,20 +3861,371 @@ function extractPromptPreview(ir) {
1340
3861
  if (lastHist) return lastHist.slice(0, 200);
1341
3862
  return void 0;
1342
3863
  }
1343
- function withStructuredOutput(response, ir) {
1344
- if (!ir.constraints?.structuredOutput) return response;
1345
- if (!response.text) return response;
3864
+ function shouldSampleProbe(sampleRate, rng = Math.random) {
3865
+ if (!(sampleRate > 0)) return false;
3866
+ if (sampleRate >= 1) return true;
3867
+ return rng() < sampleRate;
3868
+ }
3869
+ function normalizeProbeCandidates(candidates) {
3870
+ const arr = Array.isArray(candidates) ? candidates : [candidates];
3871
+ return [...new Set(arr.filter((c) => typeof c === "string" && c.length > 0))];
3872
+ }
3873
+ function hashForProbe(s) {
3874
+ let h = 0;
3875
+ for (let i = 0; i < s.length; i++) h = (h << 5) - h + s.charCodeAt(i) | 0;
3876
+ return `h${(h >>> 0).toString(16)}`;
3877
+ }
3878
+ var DEFAULT_PROBE_MAX_LATENCY_MS = 15e3;
3879
+ function isSlowTierCandidate(candidate) {
3880
+ const profile = tryGetProfile(candidate);
3881
+ if (!profile) return false;
3882
+ return latencyTierOf(profile) === "slow";
3883
+ }
3884
+ async function runProbeCandidates(args) {
3885
+ try {
3886
+ if (!shouldSampleProbe(args.cfg.sampleRate ?? 0.05)) return;
3887
+ const candidates = normalizeProbeCandidates(args.cfg.candidates);
3888
+ const promptHash = hashForProbe(args.ir.currentTurn?.content ?? "");
3889
+ const budgetMs = args.cfg.maxLatencyMs ?? DEFAULT_PROBE_MAX_LATENCY_MS;
3890
+ const deadline = args.sync ? Date.now() + budgetMs : Number.POSITIVE_INFINITY;
3891
+ const skipSlow = args.sync && (args.cfg.skipSlowTierInSync ?? true);
3892
+ const recordDiagnostic = async (candidate, outcome, detail) => {
3893
+ await recordShadowProbe({
3894
+ appId: args.ir.appId,
3895
+ archetype: args.ir.intent.archetype,
3896
+ family: deriveFamilyFromModelId(candidate) ?? "unknown",
3897
+ candidateModel: candidate,
3898
+ currentModel: args.servedModel,
3899
+ promptHash,
3900
+ // Served leg only — the candidate never produced a comparable response.
3901
+ currentResponsePreview: args.servedResponse.text.slice(0, 2e3),
3902
+ tokensCurrentIn: args.servedResponse.tokens.input,
3903
+ tokensCurrentOut: args.servedResponse.tokens.output,
3904
+ latencyCurrentMs: args.servedLatencyMs,
3905
+ outcome,
3906
+ ...detail?.errorClass ? { errorClass: detail.errorClass } : {},
3907
+ ...detail?.latencyCandidateMs !== void 0 ? { latencyCandidateMs: detail.latencyCandidateMs } : {}
3908
+ });
3909
+ };
3910
+ for (const candidate of candidates) {
3911
+ if (candidate === args.servedModel) continue;
3912
+ if (deriveFamilyFromModelId(candidate) === "claude-opus") continue;
3913
+ if (!isModelReachable(candidate, { apiKeys: args.exec.apiKeys })) continue;
3914
+ if (skipSlow && isSlowTierCandidate(candidate)) {
3915
+ try {
3916
+ await recordDiagnostic(candidate, "skipped_slow_tier_sync");
3917
+ } catch {
3918
+ }
3919
+ continue;
3920
+ }
3921
+ if (args.sync && Date.now() >= deadline) {
3922
+ try {
3923
+ await recordDiagnostic(candidate, "aborted_latency_budget");
3924
+ } catch {
3925
+ }
3926
+ break;
3927
+ }
3928
+ try {
3929
+ const candCompile = compileAndRegister(
3930
+ {
3931
+ ...args.ir,
3932
+ models: args.ir.models.includes(candidate) ? args.ir.models : [candidate, ...args.ir.models],
3933
+ constraints: { ...args.ir.constraints ?? {}, forceModel: candidate }
3934
+ },
3935
+ args.exec
3936
+ );
3937
+ const candStart = Date.now();
3938
+ let timer;
3939
+ const execPromise = execute(candCompile.request, {
3940
+ apiKeys: args.exec.apiKeys,
3941
+ fetchImpl: args.exec.fetchImpl,
3942
+ providerOverrides: args.exec.providerOverrides
3943
+ }).then((exec2) => ({ kind: "exec", exec: exec2 })).catch(() => ({ kind: "failed" }));
3944
+ let raced;
3945
+ if (args.sync) {
3946
+ const remaining = Math.max(0, deadline - Date.now());
3947
+ const timeoutPromise = new Promise((resolve) => {
3948
+ timer = setTimeout(() => resolve({ kind: "timeout" }), remaining);
3949
+ });
3950
+ raced = await Promise.race([execPromise, timeoutPromise]);
3951
+ if (timer) clearTimeout(timer);
3952
+ } else {
3953
+ raced = await execPromise;
3954
+ }
3955
+ if (raced.kind === "timeout") {
3956
+ try {
3957
+ await recordDiagnostic(candidate, "aborted_latency_budget");
3958
+ } catch {
3959
+ }
3960
+ break;
3961
+ }
3962
+ if (raced.kind === "failed") {
3963
+ try {
3964
+ await recordDiagnostic(candidate, "candidate_error", {
3965
+ errorClass: "execute_rejected",
3966
+ latencyCandidateMs: Date.now() - candStart
3967
+ });
3968
+ } catch {
3969
+ }
3970
+ continue;
3971
+ }
3972
+ const exec = raced.exec;
3973
+ const candidateLatencyMs = Date.now() - candStart;
3974
+ if (!exec.ok) {
3975
+ try {
3976
+ await recordDiagnostic(candidate, "candidate_error", {
3977
+ errorClass: exec.errorCode,
3978
+ latencyCandidateMs: candidateLatencyMs
3979
+ });
3980
+ } catch {
3981
+ }
3982
+ continue;
3983
+ }
3984
+ await recordShadowProbe({
3985
+ appId: args.ir.appId,
3986
+ archetype: args.ir.intent.archetype,
3987
+ family: deriveFamilyFromModelId(candidate) ?? candCompile.provider,
3988
+ candidateModel: candidate,
3989
+ currentModel: args.servedModel,
3990
+ promptHash,
3991
+ currentResponsePreview: args.servedResponse.text.slice(0, 2e3),
3992
+ candidateResponsePreview: exec.response.text.slice(0, 2e3),
3993
+ tokensCurrentIn: args.servedResponse.tokens.input,
3994
+ tokensCurrentOut: args.servedResponse.tokens.output,
3995
+ tokensCandidateIn: exec.response.tokens.input,
3996
+ tokensCandidateOut: exec.response.tokens.output,
3997
+ // alpha.46 — speed is the third swap-decision axis. Served latency
3998
+ // mirrors the user's actual wait; candidate latency is measured only
3999
+ // here and stored nowhere else (irrecoverable if not captured now).
4000
+ latencyCurrentMs: args.servedLatencyMs,
4001
+ latencyCandidateMs: candidateLatencyMs,
4002
+ outcome: "completed"
4003
+ });
4004
+ } catch {
4005
+ }
4006
+ }
4007
+ } catch {
4008
+ }
4009
+ }
4010
+ async function runShadowProbe(args) {
4011
+ const cfg = args.opts.shadowProbe;
4012
+ if (!cfg) return;
4013
+ await runProbeCandidates({
4014
+ ir: args.ir,
4015
+ cfg,
4016
+ servedModel: args.servedModel,
4017
+ servedResponse: args.servedResponse,
4018
+ servedLatencyMs: args.servedLatencyMs,
4019
+ // CallOptions is a structural superset of ProbeExecOpts.
4020
+ exec: args.opts,
4021
+ // alpha — the call()-inline path is on the user's critical path exactly when
4022
+ // BrainConfig.sync is set (call() awaits the probe before returning). That's
4023
+ // the only case where the latency budget + slow-tier skip must fire.
4024
+ sync: isBrainSync()
4025
+ });
4026
+ }
4027
+ async function probeShadow(ir, opts) {
4028
+ try {
4029
+ const servedResponse = {
4030
+ text: opts.served.responseText,
4031
+ structuredOutput: null,
4032
+ toolCalls: [],
4033
+ tokens: {
4034
+ input: opts.served.tokensIn,
4035
+ output: opts.served.tokensOut,
4036
+ total: opts.served.tokensIn + opts.served.tokensOut
4037
+ },
4038
+ raw: null
4039
+ };
4040
+ await runProbeCandidates({
4041
+ ir,
4042
+ cfg: {
4043
+ candidates: opts.candidates,
4044
+ sampleRate: opts.sampleRate,
4045
+ judge: opts.judge ?? "off"
4046
+ },
4047
+ servedModel: opts.served.model,
4048
+ servedResponse,
4049
+ servedLatencyMs: opts.served.latencyMs,
4050
+ exec: {
4051
+ policy: opts.policy,
4052
+ toolRelevanceThreshold: opts.toolRelevanceThreshold,
4053
+ compressHistoryAfter: opts.compressHistoryAfter,
4054
+ apiKeys: opts.apiKeys,
4055
+ fetchImpl: opts.fetchImpl,
4056
+ providerOverrides: opts.providerOverrides
4057
+ },
4058
+ // alpha — probeShadow() is ALWAYS off the user-facing critical path: the
4059
+ // consumer fires it from `onFinish` wrapped in `after()`/`waitUntil()`,
4060
+ // after the response is flushed. So the sync-mode latency budget +
4061
+ // slow-tier skip are no-ops here — slow candidates run to completion (this
4062
+ // is in fact the right place to evaluate a slow reasoner). The
4063
+ // maxLatencyMs/skipSlowTierInSync knobs aren't even exposed on
4064
+ // ProbeShadowOptions for that reason.
4065
+ sync: false
4066
+ });
4067
+ } catch {
4068
+ }
4069
+ }
4070
+ function validateStructuredContract(exec, ir) {
4071
+ if (!ir.constraints?.structuredOutput) {
4072
+ return { ok: true, response: exec.response };
4073
+ }
4074
+ const finish = (exec.response.finishReason ?? "").toLowerCase();
4075
+ if (finish === "max_tokens" || finish === "length") {
4076
+ return {
4077
+ ok: false,
4078
+ status: exec.status,
4079
+ errorType: "retryable",
4080
+ errorCode: "max_tokens_on_structured_output",
4081
+ message: `Provider returned finishReason="${exec.response.finishReason}" on a structured-output call \u2014 output truncated mid-token, JSON cannot be valid`,
4082
+ raw: exec.response.raw
4083
+ };
4084
+ }
4085
+ if (!exec.response.text) {
4086
+ return { ok: true, response: exec.response };
4087
+ }
1346
4088
  try {
1347
- const parsed = JSON.parse(response.text);
1348
- return { ...response, structuredOutput: parsed };
4089
+ const parsed = JSON.parse(exec.response.text);
4090
+ return { ok: true, response: { ...exec.response, structuredOutput: parsed } };
1349
4091
  } catch (err) {
1350
4092
  return {
1351
- ...response,
1352
- structuredOutput: null,
1353
- parseError: err instanceof Error ? err.message : String(err)
4093
+ ok: false,
4094
+ status: exec.status,
4095
+ errorType: "retryable",
4096
+ errorCode: "structured_output_parse_failed",
4097
+ message: err instanceof Error ? err.message : String(err),
4098
+ raw: exec.response.raw
1354
4099
  };
1355
4100
  }
1356
4101
  }
4102
+ function normalizeFallbackReason(attempts) {
4103
+ const first = attempts.find((a) => a.status !== "success");
4104
+ if (!first) return void 0;
4105
+ const code = first.errorCode ?? "";
4106
+ if (code === "rate_limit_429" || code === "rate_limit") return "rate_limit";
4107
+ if (code === "max_tokens_on_structured_output" || code === "structured_output_parse_failed") {
4108
+ return "cliff";
4109
+ }
4110
+ if (code === "cost_cap_exceeded") return "cost_cap";
4111
+ if (code === "auth" || code === "auth_inferred") return "provider_auth_failed";
4112
+ return "provider_error";
4113
+ }
4114
+ function generateTraceId() {
4115
+ try {
4116
+ const g = globalThis;
4117
+ if (g.crypto && typeof g.crypto.randomUUID === "function") {
4118
+ return g.crypto.randomUUID();
4119
+ }
4120
+ } catch {
4121
+ }
4122
+ const hex = (n) => Math.floor(Math.random() * Math.pow(16, n)).toString(16).padStart(n, "0");
4123
+ return `${hex(8)}-${hex(4)}-${hex(4)}-${hex(4)}-${hex(12)}`;
4124
+ }
4125
+ function safeEmit(fn) {
4126
+ try {
4127
+ fn();
4128
+ } catch {
4129
+ }
4130
+ }
4131
+
4132
+ // src/streamtext-helpers.ts
4133
+ function attachCacheControlToStreamTextInput(result, convertedMessages) {
4134
+ const messages = convertedMessages.map((m) => ({ ...m }));
4135
+ const markIdx = result.diagnostics.historyCacheMarkIndex;
4136
+ if (result.provider === "anthropic" && typeof markIdx === "number" && markIdx >= 0 && markIdx < messages.length) {
4137
+ const target = messages[markIdx];
4138
+ if (target) {
4139
+ messages[markIdx] = {
4140
+ role: target.role,
4141
+ content: target.content,
4142
+ providerOptions: {
4143
+ ...target.providerOptions ?? {},
4144
+ anthropic: {
4145
+ ...target.providerOptions?.anthropic ?? {},
4146
+ cacheControl: { type: "ephemeral" }
4147
+ }
4148
+ }
4149
+ };
4150
+ }
4151
+ }
4152
+ const systemMessages = result.systemMessages;
4153
+ const hasStructuredSystemCacheControl = result.provider === "anthropic" && systemMessages.some(
4154
+ (m) => m.providerOptions?.anthropic?.cacheControl !== void 0
4155
+ );
4156
+ const system = hasStructuredSystemCacheControl ? systemMessages : systemMessages.map((m) => m.content).join("\n\n");
4157
+ return { system, messages };
4158
+ }
4159
+
4160
+ // src/compile-aisdk.ts
4161
+ function compileForAISDKv6(ir, opts) {
4162
+ const result = compile(ir, opts);
4163
+ registerCompile(ir.appId, ir.intent.archetype, ir, result);
4164
+ armCompileConsumeTracking(result.handle);
4165
+ const systemMessages = result.systemMessages;
4166
+ const hasStructuredSystemCacheControl = result.provider === "anthropic" && systemMessages.some(
4167
+ (m) => m.providerOptions?.anthropic?.cacheControl !== void 0
4168
+ );
4169
+ const systemValue = hasStructuredSystemCacheControl ? systemMessages : systemMessages.map((m) => m.content).join("\n\n");
4170
+ const keptToolNames = extractKeptToolNames(result);
4171
+ const providerOptions = result.provider === "anthropic" && result.diagnostics.cacheableTokens > 0 ? { anthropic: { cacheControl: { type: "ephemeral" } } } : void 0;
4172
+ const handle = result.handle;
4173
+ const diagnostics = {
4174
+ ...result.diagnostics,
4175
+ targetProvider: result.provider,
4176
+ estimatedCostUsd: result.estimatedCostUsd,
4177
+ fallbackChain: result.fallbackChain
4178
+ };
4179
+ return {
4180
+ handle,
4181
+ // Consume-tracking getters: reading any of these flags the compile as
4182
+ // "used", silencing the discard guard at record() time.
4183
+ get model() {
4184
+ markCompileConsumed(handle, "model");
4185
+ return result.target;
4186
+ },
4187
+ get system() {
4188
+ markCompileConsumed(handle, "system");
4189
+ return systemValue;
4190
+ },
4191
+ get raw() {
4192
+ markCompileConsumed(handle, "raw");
4193
+ return result;
4194
+ },
4195
+ keptToolNames,
4196
+ ...providerOptions ? { providerOptions } : {},
4197
+ // Provider-gated (alpha.55, IC finding): the marker is an Anthropic
4198
+ // concept — Gemini/OpenAI/DeepSeek cache prefixes implicitly, and a
4199
+ // consumer attaching Anthropic providerOptions at this index on a
4200
+ // non-Anthropic call would ship a no-op-at-best marker. The ungated value
4201
+ // stays on diagnostics.historyCacheMarkIndex for observability.
4202
+ ...result.provider === "anthropic" && result.diagnostics.historyCacheMarkIndex !== void 0 ? { historyCacheMarkIndex: result.diagnostics.historyCacheMarkIndex } : {},
4203
+ mutationsApplied: result.mutationsApplied.map((m) => m.id),
4204
+ advisories: result.advisories ?? [],
4205
+ diagnostics,
4206
+ ir
4207
+ };
4208
+ }
4209
+ function extractKeptToolNames(result) {
4210
+ const names = /* @__PURE__ */ new Set();
4211
+ const tools = result.request.tools;
4212
+ if (!Array.isArray(tools)) return names;
4213
+ if (result.provider === "google") {
4214
+ for (const td of tools) {
4215
+ for (const fn of td.functionDeclarations ?? []) if (fn.name) names.add(fn.name);
4216
+ }
4217
+ } else if (result.provider === "openai") {
4218
+ for (const t of tools) {
4219
+ const n = t.function?.name;
4220
+ if (n) names.add(n);
4221
+ }
4222
+ } else {
4223
+ for (const t of tools) {
4224
+ if (t.name) names.add(t.name);
4225
+ }
4226
+ }
4227
+ return names;
4228
+ }
1357
4229
 
1358
4230
  // src/oracle.ts
1359
4231
  var DEFAULT_DIMENSIONS = ["correctness", "completeness", "conciseness", "format"];
@@ -1443,6 +4315,338 @@ function clamp(n) {
1443
4315
  return Math.max(0, Math.min(1, n));
1444
4316
  }
1445
4317
 
4318
+ // src/advisories-api.ts
4319
+ var SEVERITY_SET = /* @__PURE__ */ new Set(["info", "warn", "critical"]);
4320
+ var STATUS_SET = /* @__PURE__ */ new Set(["open", "snoozed", "resolved"]);
4321
+ var RESOLUTION_SOURCE_SET = /* @__PURE__ */ new Set([
4322
+ "auto",
4323
+ "consumer-marked",
4324
+ "declined"
4325
+ ]);
4326
+ function asString2(v) {
4327
+ return typeof v === "string" && v.length > 0 ? v : void 0;
4328
+ }
4329
+ function asSeverity(v) {
4330
+ if (typeof v === "string" && SEVERITY_SET.has(v)) {
4331
+ return v;
4332
+ }
4333
+ return "info";
4334
+ }
4335
+ function asStatus(v) {
4336
+ if (typeof v === "string" && STATUS_SET.has(v)) {
4337
+ return v;
4338
+ }
4339
+ return "open";
4340
+ }
4341
+ function asResolutionSource(v) {
4342
+ if (typeof v === "string" && RESOLUTION_SOURCE_SET.has(v)) {
4343
+ return v;
4344
+ }
4345
+ return void 0;
4346
+ }
4347
+ function rowToAdvisory(row) {
4348
+ const archetype = asString2(row.applies_to_archetype);
4349
+ const model = asString2(row.applies_to_model);
4350
+ const docsLink = asString2(row.docs_url);
4351
+ const suggestion = asString2(row.suggestion);
4352
+ let suggestedFix = null;
4353
+ if (docsLink || suggestion) {
4354
+ suggestedFix = { type: "manual" };
4355
+ if (docsLink) suggestedFix.docsLink = docsLink;
4356
+ if (suggestion) suggestedFix.before = suggestion;
4357
+ }
4358
+ const out = {
4359
+ id: typeof row.id === "string" ? row.id : "",
4360
+ rule: typeof row.rule === "string" ? row.rule : "",
4361
+ severity: asSeverity(row.severity),
4362
+ openedAt: typeof row.opened_at === "string" ? row.opened_at : "",
4363
+ lastObservedAt: typeof row.last_observed_at === "string" ? row.last_observed_at : "",
4364
+ observationCount: typeof row.observation_count === "number" ? row.observation_count : 0,
4365
+ appliesTo: {
4366
+ ...archetype ? { archetype } : {},
4367
+ ...model ? { model } : {}
4368
+ },
4369
+ message: typeof row.message === "string" ? row.message : "",
4370
+ suggestedFix,
4371
+ autoApplicable: false,
4372
+ // reserved — alpha.30+
4373
+ status: asStatus(row.status)
4374
+ };
4375
+ const resolvedAt = asString2(row.resolved_at);
4376
+ if (resolvedAt) out.resolvedAt = resolvedAt;
4377
+ const resolutionSource = asResolutionSource(row.resolution_source);
4378
+ if (resolutionSource) out.resolutionSource = resolutionSource;
4379
+ const resolutionNote = asString2(row.resolution_note);
4380
+ if (resolutionNote) out.resolutionNote = resolutionNote;
4381
+ return out;
4382
+ }
4383
+ function resolveFetch(injected) {
4384
+ return injected ?? ((...args) => globalThis.fetch(...args));
4385
+ }
4386
+ function normalizeEndpoint2(endpoint) {
4387
+ return endpoint.replace(/\/+$/, "");
4388
+ }
4389
+ async function getActionableAdvisories(opts) {
4390
+ const {
4391
+ appId,
4392
+ severity,
4393
+ status,
4394
+ brainEndpoint,
4395
+ brainJwt,
4396
+ brainAnonKey,
4397
+ fetch: injectedFetch
4398
+ } = opts;
4399
+ if (!appId) {
4400
+ throw new Error("getActionableAdvisories: appId is required");
4401
+ }
4402
+ const doFetch = resolveFetch(injectedFetch);
4403
+ const base = normalizeEndpoint2(brainEndpoint);
4404
+ const qs = new URLSearchParams();
4405
+ qs.set("app_id", `eq.${appId}`);
4406
+ if (severity) qs.set("severity", `eq.${severity}`);
4407
+ const effectiveStatus = status ?? "open";
4408
+ if (effectiveStatus !== "all") {
4409
+ qs.set("status", `eq.${effectiveStatus}`);
4410
+ }
4411
+ qs.set("order", "last_observed_at.desc");
4412
+ const url = `${base}/rest/v1/actionable_advisories_v?${qs.toString()}`;
4413
+ let res;
4414
+ try {
4415
+ res = await doFetch(url, {
4416
+ method: "GET",
4417
+ headers: {
4418
+ Authorization: `Bearer ${brainJwt}`,
4419
+ apikey: brainAnonKey,
4420
+ Accept: "application/json"
4421
+ }
4422
+ });
4423
+ } catch (err) {
4424
+ const msg = err instanceof Error ? err.message : String(err);
4425
+ throw new Error(`getActionableAdvisories: network error: ${msg}`);
4426
+ }
4427
+ if (res.status === 401 || res.status === 403) {
4428
+ throw new Error("getActionableAdvisories: brain auth misconfig");
4429
+ }
4430
+ if (res.status >= 500) {
4431
+ throw new Error(`getActionableAdvisories: brain unavailable (${res.status})`);
4432
+ }
4433
+ if (!res.ok) {
4434
+ throw new Error(`getActionableAdvisories: bad request (${res.status})`);
4435
+ }
4436
+ let rows;
4437
+ try {
4438
+ rows = await res.json();
4439
+ } catch {
4440
+ throw new Error("getActionableAdvisories: malformed brain response");
4441
+ }
4442
+ if (!Array.isArray(rows)) {
4443
+ throw new Error("getActionableAdvisories: expected array from brain");
4444
+ }
4445
+ const out = [];
4446
+ for (const raw of rows) {
4447
+ if (raw && typeof raw === "object") {
4448
+ out.push(rowToAdvisory(raw));
4449
+ }
4450
+ }
4451
+ return out;
4452
+ }
4453
+ async function markAdvisoryResolved(opts) {
4454
+ const {
4455
+ id,
4456
+ resolutionNote,
4457
+ brainEndpoint,
4458
+ brainJwt,
4459
+ brainAnonKey,
4460
+ fetch: injectedFetch
4461
+ } = opts;
4462
+ if (!id) {
4463
+ return { ok: false, reason: "id_required" };
4464
+ }
4465
+ const doFetch = resolveFetch(injectedFetch);
4466
+ const base = normalizeEndpoint2(brainEndpoint);
4467
+ const lookupUrl = `${base}/rest/v1/actionable_advisories_v?id=eq.${encodeURIComponent(id)}&select=app_id,rule`;
4468
+ let lookupRes;
4469
+ try {
4470
+ lookupRes = await doFetch(lookupUrl, {
4471
+ method: "GET",
4472
+ headers: {
4473
+ Authorization: `Bearer ${brainJwt}`,
4474
+ apikey: brainAnonKey,
4475
+ Accept: "application/json"
4476
+ }
4477
+ });
4478
+ } catch (err) {
4479
+ const msg = err instanceof Error ? err.message : String(err);
4480
+ return { ok: false, reason: `network_error:${msg}` };
4481
+ }
4482
+ if (lookupRes.status === 401 || lookupRes.status === 403) {
4483
+ return { ok: false, reason: "brain_auth_misconfig" };
4484
+ }
4485
+ if (lookupRes.status >= 500) {
4486
+ return { ok: false, reason: "brain_unavailable" };
4487
+ }
4488
+ if (!lookupRes.ok) {
4489
+ return { ok: false, reason: `brain_lookup_failed:${lookupRes.status}` };
4490
+ }
4491
+ let lookupRows;
4492
+ try {
4493
+ lookupRows = await lookupRes.json();
4494
+ } catch {
4495
+ return { ok: false, reason: "brain_lookup_malformed" };
4496
+ }
4497
+ if (!Array.isArray(lookupRows) || lookupRows.length === 0) {
4498
+ return { ok: false, reason: "advisory_not_found" };
4499
+ }
4500
+ const tuple = lookupRows[0];
4501
+ const appId = typeof tuple.app_id === "string" ? tuple.app_id : "";
4502
+ const code = typeof tuple.rule === "string" ? tuple.rule : "";
4503
+ if (!appId || !code) {
4504
+ return { ok: false, reason: "advisory_tuple_invalid" };
4505
+ }
4506
+ const outcomesUrl = `${base}/rest/v1/compile_outcomes?app_id=eq.${encodeURIComponent(appId)}&select=id`;
4507
+ let outcomesRes;
4508
+ try {
4509
+ outcomesRes = await doFetch(outcomesUrl, {
4510
+ method: "GET",
4511
+ headers: {
4512
+ Authorization: `Bearer ${brainJwt}`,
4513
+ apikey: brainAnonKey,
4514
+ Accept: "application/json"
4515
+ }
4516
+ });
4517
+ } catch (err) {
4518
+ const msg = err instanceof Error ? err.message : String(err);
4519
+ return { ok: false, reason: `network_error:${msg}` };
4520
+ }
4521
+ if (outcomesRes.status === 401 || outcomesRes.status === 403) {
4522
+ return { ok: false, reason: "brain_auth_misconfig" };
4523
+ }
4524
+ if (outcomesRes.status >= 500) {
4525
+ return { ok: false, reason: "brain_unavailable" };
4526
+ }
4527
+ if (!outcomesRes.ok) {
4528
+ return { ok: false, reason: `brain_lookup_failed:${outcomesRes.status}` };
4529
+ }
4530
+ let outcomeRows;
4531
+ try {
4532
+ outcomeRows = await outcomesRes.json();
4533
+ } catch {
4534
+ return { ok: false, reason: "brain_lookup_malformed" };
4535
+ }
4536
+ if (!Array.isArray(outcomeRows)) {
4537
+ return { ok: false, reason: "brain_lookup_malformed" };
4538
+ }
4539
+ const outcomeIds = [];
4540
+ for (const row of outcomeRows) {
4541
+ if (row && typeof row === "object") {
4542
+ const idVal = row.id;
4543
+ if (typeof idVal === "number" && Number.isFinite(idVal)) {
4544
+ outcomeIds.push(idVal);
4545
+ }
4546
+ }
4547
+ }
4548
+ if (outcomeIds.length === 0) {
4549
+ return { ok: true };
4550
+ }
4551
+ const inList = outcomeIds.join(",");
4552
+ const patchUrl = `${base}/rest/v1/compile_outcome_advisories?outcome_id=in.(${inList})&code=eq.${encodeURIComponent(code)}&resolved_at=is.null`;
4553
+ const patchBody = {
4554
+ resolved_at: (/* @__PURE__ */ new Date()).toISOString(),
4555
+ resolution_source: "consumer-marked"
4556
+ };
4557
+ if (resolutionNote !== void 0) {
4558
+ patchBody.resolution_note = resolutionNote;
4559
+ }
4560
+ let patchRes;
4561
+ try {
4562
+ patchRes = await doFetch(patchUrl, {
4563
+ method: "PATCH",
4564
+ headers: {
4565
+ Authorization: `Bearer ${brainJwt}`,
4566
+ apikey: brainAnonKey,
4567
+ "Content-Type": "application/json",
4568
+ Accept: "application/json",
4569
+ // PostgREST default is no return; we don't need the row back.
4570
+ Prefer: "return=minimal"
4571
+ },
4572
+ body: JSON.stringify(patchBody)
4573
+ });
4574
+ } catch (err) {
4575
+ const msg = err instanceof Error ? err.message : String(err);
4576
+ return { ok: false, reason: `network_error:${msg}` };
4577
+ }
4578
+ if (patchRes.status === 401 || patchRes.status === 403) {
4579
+ return { ok: false, reason: "brain_auth_misconfig" };
4580
+ }
4581
+ if (patchRes.status >= 500) {
4582
+ return { ok: false, reason: "brain_unavailable" };
4583
+ }
4584
+ if (!patchRes.ok) {
4585
+ return { ok: false, reason: `patch_failed:${patchRes.status}` };
4586
+ }
4587
+ return { ok: true };
4588
+ }
4589
+ async function markExclusionFindingHandled(opts) {
4590
+ const {
4591
+ appId,
4592
+ archetype,
4593
+ excludedModel,
4594
+ resolution,
4595
+ resolutionNote,
4596
+ brainEndpoint,
4597
+ brainJwt,
4598
+ brainAnonKey,
4599
+ fetch: injectedFetch
4600
+ } = opts;
4601
+ if (!appId) return { ok: false, reason: "app_id_required" };
4602
+ if (!archetype) return { ok: false, reason: "archetype_required" };
4603
+ if (!excludedModel) {
4604
+ return { ok: false, reason: "excluded_model_required" };
4605
+ }
4606
+ if (resolution !== "consumer-marked" && resolution !== "declined" && resolution !== "probed-unblock" && resolution !== "probed-stay-excluded") {
4607
+ return { ok: false, reason: "resolution_invalid" };
4608
+ }
4609
+ const doFetch = resolveFetch(injectedFetch);
4610
+ const base = normalizeEndpoint2(brainEndpoint);
4611
+ const url = `${base}/rest/v1/exclusion_findings?app_id=eq.${encodeURIComponent(appId)}&intent_archetype=eq.${encodeURIComponent(archetype)}&excluded_model=eq.${encodeURIComponent(excludedModel)}&resolved_at=is.null`;
4612
+ const patchBody = {
4613
+ resolved_at: (/* @__PURE__ */ new Date()).toISOString(),
4614
+ resolution_source: resolution
4615
+ };
4616
+ if (resolutionNote !== void 0) {
4617
+ patchBody.resolution_note = resolutionNote;
4618
+ }
4619
+ let res;
4620
+ try {
4621
+ res = await doFetch(url, {
4622
+ method: "PATCH",
4623
+ headers: {
4624
+ Authorization: `Bearer ${brainJwt}`,
4625
+ apikey: brainAnonKey,
4626
+ "Content-Type": "application/json",
4627
+ Accept: "application/json",
4628
+ // PATCH may match zero rows when already resolved — return=minimal
4629
+ // keeps the response body empty so the success path is uniform.
4630
+ Prefer: "return=minimal"
4631
+ },
4632
+ body: JSON.stringify(patchBody)
4633
+ });
4634
+ } catch (err) {
4635
+ const msg = err instanceof Error ? err.message : String(err);
4636
+ return { ok: false, reason: `network_error:${msg}` };
4637
+ }
4638
+ if (res.status === 401 || res.status === 403) {
4639
+ return { ok: false, reason: "brain_auth_misconfig" };
4640
+ }
4641
+ if (res.status >= 500) {
4642
+ return { ok: false, reason: "brain_unavailable" };
4643
+ }
4644
+ if (!res.ok) {
4645
+ return { ok: false, reason: `patch_failed:${res.status}` };
4646
+ }
4647
+ return { ok: true };
4648
+ }
4649
+
1446
4650
  // src/index.ts
1447
4651
  function compile2(ir, opts) {
1448
4652
  const result = compile(ir, opts);
@@ -1450,29 +4654,89 @@ function compile2(ir, opts) {
1450
4654
  return result;
1451
4655
  }
1452
4656
  export {
4657
+ ABSOLUTE_FLOOR,
1453
4658
  ALIASES,
1454
4659
  ALL_ARCHETYPES,
4660
+ ARCHETYPE_FAMILY_FITS,
4661
+ ARCHETYPE_FLOOR_DEFAULT,
1455
4662
  CallError,
4663
+ DEFAULT_FINDINGS_ENDPOINT,
1456
4664
  DIALECT_VERSION,
4665
+ FamilyResolutionError,
1457
4666
  INTENT_ARCHETYPES,
4667
+ LATENCY_TIER_MS,
4668
+ LIBRARY_VERSION,
4669
+ MEASURED_GROUNDING_MIN_N,
4670
+ PRODUCER_OWNED_RULE_CODES,
4671
+ PROVIDER_ENV_KEYS,
4672
+ RULE_SEQUENTIAL_TOOL_CLIFF,
4673
+ TRANSLATOR_FLOOR,
1458
4674
  allProfiles,
4675
+ applyArchetypeConvention,
4676
+ applySectionRewrites,
4677
+ attachCacheControlToStreamTextInput,
1459
4678
  bucketContext,
1460
4679
  bucketHistory,
1461
4680
  bucketToolCount,
1462
4681
  buildLLMJudge,
4682
+ buildShadowProbeRow,
1463
4683
  call,
1464
4684
  clearBrain,
1465
4685
  compile2 as compile,
4686
+ compileForAISDKv6,
1466
4687
  configureBrain,
1467
4688
  countTokens,
4689
+ createBrainForwardRoutes,
4690
+ createKeyHealthRoute,
4691
+ deriveFamilyFromModelId,
4692
+ deriveOwnership,
1468
4693
  execute,
4694
+ findBetterFit,
4695
+ getActionableAdvisories,
4696
+ getAllStarterChains,
4697
+ getAllStarterChainsWithGrounding,
4698
+ getArchetypePerfScore,
4699
+ getDefaultFallbackChain,
4700
+ getDefaultFallbackChainWithGrounding,
4701
+ getModelCompatibility,
4702
+ getPerAxisMetrics,
1469
4703
  getProfile,
4704
+ getReachabilityDiagnostic,
4705
+ getRecommendedPrimary,
4706
+ getSequentialStarterChain,
4707
+ getSequentialStarterChainWithGrounding,
4708
+ getStaleExclusionFindings,
4709
+ getStarterChain,
4710
+ getStarterChainWithGrounding,
1470
4711
  hashShape,
1471
4712
  isArchetype,
4713
+ isBrainQueryActiveFor,
4714
+ isBrainSync,
4715
+ isExclusionFindingsBrainActive,
4716
+ isModelReachable,
4717
+ isProviderReachable,
4718
+ latencyTierOf,
1472
4719
  learningKey,
4720
+ loadAliasesFromBrain,
4721
+ loadArchetypePerfFromBrain,
4722
+ loadArchetypePerfNFromBrain,
4723
+ loadChainsFromBrain,
4724
+ loadModelsFromBrain,
4725
+ loadPricingFromBrain,
4726
+ markAdvisoryResolved,
4727
+ markExclusionFindingHandled,
4728
+ markPromoteReadyHandled,
4729
+ probeShadow,
4730
+ profileToRow,
1473
4731
  profilesByProvider,
4732
+ readBrainReadEnv,
1474
4733
  record,
4734
+ recordOutcome,
4735
+ recordShadowProbe,
1475
4736
  resetTokenizer,
4737
+ resolveConventionsForProfile,
4738
+ resolvePricingAt,
4739
+ resolveProviderKey,
1476
4740
  runAdvisor,
1477
4741
  setTokenizer,
1478
4742
  tryGetProfile