@warmdrift/kgauto-compiler 2.0.0-alpha.21 → 2.0.0-alpha.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/glassbox/index.d.mts +3 -3
- package/dist/glassbox/index.d.ts +3 -3
- package/dist/glassbox-routes/index.d.mts +2 -2
- package/dist/glassbox-routes/index.d.ts +2 -2
- package/dist/index.d.mts +77 -4
- package/dist/index.d.ts +77 -4
- package/dist/index.js +338 -137
- package/dist/index.mjs +336 -137
- package/dist/{ir-CruZBtpK.d.mts → ir-B9zqlwjH.d.mts} +38 -1
- package/dist/{ir-Wr5lc8Mi.d.ts → ir-B_XX2LAO.d.ts} +38 -1
- package/dist/profiles.d.mts +1 -1
- package/dist/profiles.d.ts +1 -1
- package/dist/{types-zk238uNL.d.mts → types-D9WndxeD.d.mts} +1 -1
- package/dist/{types-BiZKJU41.d.ts → types-DiWBWvxg.d.ts} +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -43,6 +43,7 @@ __export(index_exports, {
|
|
|
43
43
|
getArchetypePerfScore: () => getArchetypePerfScore,
|
|
44
44
|
getDefaultFallbackChain: () => getDefaultFallbackChain,
|
|
45
45
|
getDefaultFallbackChainWithGrounding: () => getDefaultFallbackChainWithGrounding,
|
|
46
|
+
getPerAxisMetrics: () => getPerAxisMetrics,
|
|
46
47
|
getProfile: () => getProfile,
|
|
47
48
|
getReachabilityDiagnostic: () => getReachabilityDiagnostic,
|
|
48
49
|
getSequentialStarterChain: () => getSequentialStarterChain,
|
|
@@ -51,6 +52,7 @@ __export(index_exports, {
|
|
|
51
52
|
getStarterChainWithGrounding: () => getStarterChainWithGrounding,
|
|
52
53
|
hashShape: () => hashShape,
|
|
53
54
|
isArchetype: () => isArchetype,
|
|
55
|
+
isBrainQueryActiveFor: () => isBrainQueryActiveFor,
|
|
54
56
|
isModelReachable: () => isModelReachable,
|
|
55
57
|
isProviderReachable: () => isProviderReachable,
|
|
56
58
|
learningKey: () => learningKey,
|
|
@@ -1944,14 +1946,250 @@ function profilesByProvider(provider) {
|
|
|
1944
1946
|
return PROFILES_RAW.filter((p) => p.provider === provider);
|
|
1945
1947
|
}
|
|
1946
1948
|
|
|
1949
|
+
// src/brain-query.ts
|
|
1950
|
+
var FRESH_SNAPSHOT = {
|
|
1951
|
+
data: null,
|
|
1952
|
+
expiresAt: 0,
|
|
1953
|
+
refreshing: false,
|
|
1954
|
+
warned: false
|
|
1955
|
+
};
|
|
1956
|
+
var snapshot = { ...FRESH_SNAPSHOT };
|
|
1957
|
+
var runtime;
|
|
1958
|
+
function configureBrainQuery(rt) {
|
|
1959
|
+
runtime = rt;
|
|
1960
|
+
snapshot = { ...FRESH_SNAPSHOT };
|
|
1961
|
+
}
|
|
1962
|
+
function createBrainQueryCache(opts) {
|
|
1963
|
+
return () => {
|
|
1964
|
+
const rt = runtime;
|
|
1965
|
+
if (!rt || !rt.enabledTables.has(opts.table)) {
|
|
1966
|
+
return opts.bundledFallback();
|
|
1967
|
+
}
|
|
1968
|
+
const now = Date.now();
|
|
1969
|
+
const stale = snapshot.expiresAt <= now;
|
|
1970
|
+
if (stale && !snapshot.refreshing) {
|
|
1971
|
+
snapshot.refreshing = true;
|
|
1972
|
+
void asyncRefresh(rt);
|
|
1973
|
+
}
|
|
1974
|
+
if (snapshot.data) {
|
|
1975
|
+
const rows = snapshot.data[opts.table];
|
|
1976
|
+
if (Array.isArray(rows) && rows.length > 0) {
|
|
1977
|
+
try {
|
|
1978
|
+
return opts.mapRows(rows);
|
|
1979
|
+
} catch {
|
|
1980
|
+
return opts.bundledFallback();
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
return opts.bundledFallback();
|
|
1985
|
+
};
|
|
1986
|
+
}
|
|
1987
|
+
var pendingRefresh;
|
|
1988
|
+
async function asyncRefresh(rt) {
|
|
1989
|
+
const promise = doRefresh(rt);
|
|
1990
|
+
pendingRefresh = promise;
|
|
1991
|
+
try {
|
|
1992
|
+
await promise;
|
|
1993
|
+
} finally {
|
|
1994
|
+
if (pendingRefresh === promise) pendingRefresh = void 0;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
var DEFAULT_CONFIG_URL = "https://kgauto-dashboard.vercel.app/api/kgauto-v2/config";
|
|
1998
|
+
async function doRefresh(rt) {
|
|
1999
|
+
const url = rt.configEndpoint ?? DEFAULT_CONFIG_URL;
|
|
2000
|
+
try {
|
|
2001
|
+
const res = await rt.fetchImpl(url, { method: "GET" });
|
|
2002
|
+
if (!res.ok) {
|
|
2003
|
+
throw new Error(`brain-query ${res.status}: ${res.statusText}`);
|
|
2004
|
+
}
|
|
2005
|
+
const body = await res.json();
|
|
2006
|
+
if (runtime !== rt) return;
|
|
2007
|
+
snapshot = {
|
|
2008
|
+
data: body,
|
|
2009
|
+
expiresAt: Date.now() + rt.ttlMs,
|
|
2010
|
+
refreshing: false,
|
|
2011
|
+
warned: snapshot.warned
|
|
2012
|
+
};
|
|
2013
|
+
} catch (err) {
|
|
2014
|
+
if (runtime !== rt) return;
|
|
2015
|
+
snapshot.refreshing = false;
|
|
2016
|
+
snapshot.expiresAt = Date.now() + rt.ttlMs;
|
|
2017
|
+
if (!snapshot.warned) {
|
|
2018
|
+
snapshot.warned = true;
|
|
2019
|
+
(rt.onError ?? defaultOnError)(err);
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
function defaultOnError(err) {
|
|
2024
|
+
console.warn("[kgauto] brain-query failed (using bundled fallback):", err);
|
|
2025
|
+
}
|
|
2026
|
+
function isBrainQueryActiveFor(table) {
|
|
2027
|
+
return runtime !== void 0 && runtime.enabledTables.has(table);
|
|
2028
|
+
}
|
|
2029
|
+
async function getPerAxisMetrics(opts) {
|
|
2030
|
+
const fetchFn = opts.fetch ?? fetch;
|
|
2031
|
+
const endpoint = opts.endpoint ?? runtime?.endpoint;
|
|
2032
|
+
if (!endpoint) return null;
|
|
2033
|
+
const windowDays = opts.windowDays ?? 30;
|
|
2034
|
+
const body = {
|
|
2035
|
+
p_app_id: opts.appId,
|
|
2036
|
+
p_archetype: opts.archetype,
|
|
2037
|
+
p_model: opts.model,
|
|
2038
|
+
p_window_days: windowDays,
|
|
2039
|
+
p_quality_floor: opts.qualityFloor ?? null
|
|
2040
|
+
};
|
|
2041
|
+
const headers = {
|
|
2042
|
+
Accept: "application/json",
|
|
2043
|
+
"Content-Type": "application/json",
|
|
2044
|
+
...opts.apiKey ? { Authorization: `Bearer ${opts.apiKey}` } : {}
|
|
2045
|
+
};
|
|
2046
|
+
try {
|
|
2047
|
+
const res = await fetchFn(`${endpoint}/rpc/get_per_axis_metrics`, {
|
|
2048
|
+
method: "POST",
|
|
2049
|
+
headers,
|
|
2050
|
+
body: JSON.stringify(body)
|
|
2051
|
+
});
|
|
2052
|
+
if (!res.ok) return null;
|
|
2053
|
+
const raw = await res.json();
|
|
2054
|
+
return mapPerAxisMetrics(raw, opts.appId, opts.archetype, opts.model, windowDays);
|
|
2055
|
+
} catch {
|
|
2056
|
+
return null;
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
function mapPerAxisMetrics(raw, fallbackAppId, fallbackArchetype, fallbackModel, fallbackWindowDays) {
|
|
2060
|
+
if (raw === null || raw === void 0) return null;
|
|
2061
|
+
if (typeof raw !== "object") return null;
|
|
2062
|
+
const r = raw;
|
|
2063
|
+
if (Array.isArray(raw)) {
|
|
2064
|
+
if (raw.length === 0) return null;
|
|
2065
|
+
return mapPerAxisMetrics(raw[0], fallbackAppId, fallbackArchetype, fallbackModel, fallbackWindowDays);
|
|
2066
|
+
}
|
|
2067
|
+
const num = (v) => {
|
|
2068
|
+
if (v === null || v === void 0) return null;
|
|
2069
|
+
if (typeof v === "number") return Number.isFinite(v) ? v : null;
|
|
2070
|
+
if (typeof v === "string") {
|
|
2071
|
+
const n = Number(v);
|
|
2072
|
+
return Number.isFinite(n) ? n : null;
|
|
2073
|
+
}
|
|
2074
|
+
return null;
|
|
2075
|
+
};
|
|
2076
|
+
const int = (v) => {
|
|
2077
|
+
const n = num(v);
|
|
2078
|
+
return n === null ? 0 : Math.trunc(n);
|
|
2079
|
+
};
|
|
2080
|
+
const bool = (v) => {
|
|
2081
|
+
if (v === null || v === void 0) return null;
|
|
2082
|
+
if (typeof v === "boolean") return v;
|
|
2083
|
+
return null;
|
|
2084
|
+
};
|
|
2085
|
+
const str = (v, fallback) => typeof v === "string" ? v : fallback;
|
|
2086
|
+
const cost = r.cost_efficiency ?? {};
|
|
2087
|
+
const time = r.time_efficiency ?? {};
|
|
2088
|
+
const rel = r.reliability ?? {};
|
|
2089
|
+
return {
|
|
2090
|
+
appId: str(r.app_id, fallbackAppId),
|
|
2091
|
+
archetype: str(r.archetype, fallbackArchetype),
|
|
2092
|
+
model: str(r.model, fallbackModel),
|
|
2093
|
+
windowDays: num(r.window_days) ?? fallbackWindowDays,
|
|
2094
|
+
nRows: int(r.n_rows),
|
|
2095
|
+
nRowsClean: int(r.n_rows_clean),
|
|
2096
|
+
nQualityOutcomes: int(r.n_quality_outcomes),
|
|
2097
|
+
magicRate: num(r.magic_rate),
|
|
2098
|
+
qualityFloorMet: bool(r.quality_floor_met),
|
|
2099
|
+
costEfficiency: {
|
|
2100
|
+
avgCostUsd: num(cost.avg_cost_usd),
|
|
2101
|
+
avgCostUsdClean: num(cost.avg_cost_usd_clean),
|
|
2102
|
+
avgInputTokens: num(cost.avg_input_tokens),
|
|
2103
|
+
avgOutputTokens: num(cost.avg_output_tokens),
|
|
2104
|
+
inputTokenRatio: num(cost.input_token_ratio)
|
|
2105
|
+
},
|
|
2106
|
+
timeEfficiency: {
|
|
2107
|
+
avgLatencyMs: num(time.avg_latency_ms),
|
|
2108
|
+
avgTtftMs: num(time.avg_ttft_ms)
|
|
2109
|
+
},
|
|
2110
|
+
reliability: {
|
|
2111
|
+
successRate: num(rel.success_rate),
|
|
2112
|
+
successRateClean: num(rel.success_rate_clean),
|
|
2113
|
+
emptyRate: num(rel.empty_rate),
|
|
2114
|
+
emptyRateClean: num(rel.empty_rate_clean)
|
|
2115
|
+
},
|
|
2116
|
+
evidenceFreshnessDays: num(r.evidence_freshness_days)
|
|
2117
|
+
};
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
// src/archetype-perf-brain.ts
|
|
2121
|
+
function isPerfRow(x) {
|
|
2122
|
+
if (!x || typeof x !== "object") return false;
|
|
2123
|
+
const r = x;
|
|
2124
|
+
return typeof r.model_id === "string" && typeof r.archetype === "string" && typeof r.perf_score === "number";
|
|
2125
|
+
}
|
|
2126
|
+
function mapRowsToPerfMap(rows) {
|
|
2127
|
+
const out = /* @__PURE__ */ new Map();
|
|
2128
|
+
for (const row of rows) {
|
|
2129
|
+
if (!isPerfRow(row)) continue;
|
|
2130
|
+
const existing = out.get(row.model_id) ?? {};
|
|
2131
|
+
existing[row.archetype] = row.perf_score;
|
|
2132
|
+
out.set(row.model_id, existing);
|
|
2133
|
+
}
|
|
2134
|
+
return out;
|
|
2135
|
+
}
|
|
2136
|
+
function mapRowsToNMap(rows) {
|
|
2137
|
+
const out = /* @__PURE__ */ new Map();
|
|
2138
|
+
for (const row of rows) {
|
|
2139
|
+
if (!isPerfRow(row)) continue;
|
|
2140
|
+
if (typeof row.n !== "number") continue;
|
|
2141
|
+
const existing = out.get(row.model_id) ?? {};
|
|
2142
|
+
existing[row.archetype] = row.n;
|
|
2143
|
+
out.set(row.model_id, existing);
|
|
2144
|
+
}
|
|
2145
|
+
return out;
|
|
2146
|
+
}
|
|
2147
|
+
function bundledArchetypePerf() {
|
|
2148
|
+
const out = /* @__PURE__ */ new Map();
|
|
2149
|
+
for (const profile of allProfiles()) {
|
|
2150
|
+
if (profile.archetypePerf) out.set(profile.id, profile.archetypePerf);
|
|
2151
|
+
}
|
|
2152
|
+
return out;
|
|
2153
|
+
}
|
|
2154
|
+
function bundledArchetypePerfN() {
|
|
2155
|
+
return /* @__PURE__ */ new Map();
|
|
2156
|
+
}
|
|
2157
|
+
var loadArchetypePerfFromBrain = createBrainQueryCache({
|
|
2158
|
+
table: "kgauto_archetype_perf",
|
|
2159
|
+
mapRows: mapRowsToPerfMap,
|
|
2160
|
+
bundledFallback: bundledArchetypePerf
|
|
2161
|
+
});
|
|
2162
|
+
var loadArchetypePerfNFromBrain = createBrainQueryCache(
|
|
2163
|
+
{
|
|
2164
|
+
table: "kgauto_archetype_perf",
|
|
2165
|
+
mapRows: mapRowsToNMap,
|
|
2166
|
+
bundledFallback: bundledArchetypePerfN
|
|
2167
|
+
}
|
|
2168
|
+
);
|
|
2169
|
+
var MEASURED_GROUNDING_MIN_N = 10;
|
|
2170
|
+
function getArchetypePerfScore(modelId, archetype) {
|
|
2171
|
+
const score = loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
|
|
2172
|
+
const n = loadArchetypePerfNFromBrain().get(modelId)?.[archetype] ?? 0;
|
|
2173
|
+
const grounding = n >= MEASURED_GROUNDING_MIN_N ? "measured" : "judgment";
|
|
2174
|
+
return { score, n, grounding };
|
|
2175
|
+
}
|
|
2176
|
+
|
|
1947
2177
|
// src/advisor.ts
|
|
1948
|
-
|
|
2178
|
+
var QUALITY_FLOOR_FOR_RECOMMENDATION = 6;
|
|
2179
|
+
var TIER_DOWN_COST_RATIO = 0.5;
|
|
2180
|
+
var COST_MISMATCHED_CHOSEN_SCORE_CEILING = 7;
|
|
2181
|
+
function runAdvisor(ir, result, profile, policy, phase2) {
|
|
1949
2182
|
const out = [];
|
|
1950
2183
|
out.push(...detectCachingOff(ir, profile));
|
|
1951
2184
|
out.push(...detectSingleChunkSystem(ir, profile));
|
|
1952
2185
|
out.push(...detectToolBloat(ir, result));
|
|
1953
2186
|
out.push(...detectHistoryUncached(ir, profile));
|
|
1954
2187
|
out.push(...detectSingleModelArray(ir, policy));
|
|
2188
|
+
if (policy?.posture !== "locked") {
|
|
2189
|
+
out.push(...detectCostMismatchedArchetype(ir, profile, phase2));
|
|
2190
|
+
out.push(...detectModelStaleEvidence(ir, profile));
|
|
2191
|
+
out.push(...detectTierDown(ir, profile, phase2));
|
|
2192
|
+
}
|
|
1955
2193
|
return out;
|
|
1956
2194
|
}
|
|
1957
2195
|
function detectCachingOff(ir, profile) {
|
|
@@ -2037,6 +2275,91 @@ function detectSingleModelArray(ir, policy) {
|
|
|
2037
2275
|
}
|
|
2038
2276
|
];
|
|
2039
2277
|
}
|
|
2278
|
+
function detectCostMismatchedArchetype(ir, profile, phase2) {
|
|
2279
|
+
if (!phase2 || phase2.fallbackChain.length === 0) return [];
|
|
2280
|
+
if (!phase2.profileResolver) return [];
|
|
2281
|
+
const archetype = ir.intent.archetype;
|
|
2282
|
+
const chosenScore = getArchetypePerfScore(profile.id, archetype);
|
|
2283
|
+
const chosenHasRoomToGrow = chosenScore.grounding === "judgment" || chosenScore.score < COST_MISMATCHED_CHOSEN_SCORE_CEILING;
|
|
2284
|
+
if (!chosenHasRoomToGrow) return [];
|
|
2285
|
+
let bestAlt = null;
|
|
2286
|
+
for (const altId of phase2.fallbackChain) {
|
|
2287
|
+
const altProfile = phase2.profileResolver(altId);
|
|
2288
|
+
if (!altProfile) continue;
|
|
2289
|
+
if (altProfile.id === profile.id) continue;
|
|
2290
|
+
const altScore = getArchetypePerfScore(altProfile.id, archetype);
|
|
2291
|
+
if (altScore.score < QUALITY_FLOOR_FOR_RECOMMENDATION) continue;
|
|
2292
|
+
if (altScore.score < chosenScore.score) continue;
|
|
2293
|
+
if (altProfile.costInputPer1m >= profile.costInputPer1m) continue;
|
|
2294
|
+
if (!bestAlt || altScore.score > bestAlt.score.score || altScore.score === bestAlt.score.score && altProfile.costInputPer1m < bestAlt.profile.costInputPer1m) {
|
|
2295
|
+
bestAlt = { id: altId, profile: altProfile, score: altScore };
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
if (!bestAlt) return [];
|
|
2299
|
+
const tierDownWouldFire = bestAlt.score.grounding === "measured" && bestAlt.profile.costInputPer1m <= profile.costInputPer1m * TIER_DOWN_COST_RATIO;
|
|
2300
|
+
if (tierDownWouldFire) return [];
|
|
2301
|
+
const chosenGrounding = chosenScore.grounding === "judgment" ? `archetypePerf.${archetype}=judgment` : `archetypePerf.${archetype}=${chosenScore.score}`;
|
|
2302
|
+
const altGrounding = bestAlt.score.grounding === "measured" ? `archetypePerf.${archetype}=${bestAlt.score.score}, measured, n=${bestAlt.score.n}` : `archetypePerf.${archetype}=${bestAlt.score.score}, judgment`;
|
|
2303
|
+
return [
|
|
2304
|
+
{
|
|
2305
|
+
level: "warn",
|
|
2306
|
+
code: "cost-mismatched-archetype",
|
|
2307
|
+
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.`,
|
|
2308
|
+
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.`,
|
|
2309
|
+
recommendationType: profile.provider === bestAlt.profile.provider ? "tier-down" : "model-swap",
|
|
2310
|
+
docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
|
|
2311
|
+
}
|
|
2312
|
+
];
|
|
2313
|
+
}
|
|
2314
|
+
function detectModelStaleEvidence(ir, profile) {
|
|
2315
|
+
if (!isBrainQueryActiveFor("kgauto_archetype_perf")) return [];
|
|
2316
|
+
const archetype = ir.intent.archetype;
|
|
2317
|
+
const chosen = getArchetypePerfScore(profile.id, archetype);
|
|
2318
|
+
if (chosen.grounding !== "judgment") return [];
|
|
2319
|
+
return [
|
|
2320
|
+
{
|
|
2321
|
+
level: "info",
|
|
2322
|
+
code: "model-stale-evidence",
|
|
2323
|
+
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.`,
|
|
2324
|
+
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.",
|
|
2325
|
+
recommendationType: "prompt-fix",
|
|
2326
|
+
docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
|
|
2327
|
+
}
|
|
2328
|
+
];
|
|
2329
|
+
}
|
|
2330
|
+
function detectTierDown(ir, profile, phase2) {
|
|
2331
|
+
if (!phase2 || phase2.fallbackChain.length === 0) return [];
|
|
2332
|
+
if (!phase2.profileResolver) return [];
|
|
2333
|
+
const archetype = ir.intent.archetype;
|
|
2334
|
+
const chosenScore = getArchetypePerfScore(profile.id, archetype);
|
|
2335
|
+
const chosenCost = profile.costInputPer1m;
|
|
2336
|
+
let bestAlt = null;
|
|
2337
|
+
for (const altId of phase2.fallbackChain) {
|
|
2338
|
+
const altProfile = phase2.profileResolver(altId);
|
|
2339
|
+
if (!altProfile) continue;
|
|
2340
|
+
if (altProfile.id === profile.id) continue;
|
|
2341
|
+
const altScore = getArchetypePerfScore(altProfile.id, archetype);
|
|
2342
|
+
if (altScore.grounding !== "measured") continue;
|
|
2343
|
+
if (altScore.score < QUALITY_FLOOR_FOR_RECOMMENDATION) continue;
|
|
2344
|
+
if (altScore.score < chosenScore.score) continue;
|
|
2345
|
+
if (altProfile.costInputPer1m > chosenCost * TIER_DOWN_COST_RATIO) continue;
|
|
2346
|
+
if (!bestAlt || altProfile.costInputPer1m < bestAlt.profile.costInputPer1m || altProfile.costInputPer1m === bestAlt.profile.costInputPer1m && altScore.score > bestAlt.score.score) {
|
|
2347
|
+
bestAlt = { id: altId, profile: altProfile, score: altScore };
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
if (!bestAlt) return [];
|
|
2351
|
+
const chosenDesc = chosenScore.grounding === "measured" ? `archetypePerf.${archetype}=${chosenScore.score} (measured, n=${chosenScore.n})` : `archetypePerf.${archetype}=${chosenScore.score} (${chosenScore.grounding})`;
|
|
2352
|
+
return [
|
|
2353
|
+
{
|
|
2354
|
+
level: "warn",
|
|
2355
|
+
code: "tier-down",
|
|
2356
|
+
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.`,
|
|
2357
|
+
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.`,
|
|
2358
|
+
recommendationType: "tier-down",
|
|
2359
|
+
docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
|
|
2360
|
+
}
|
|
2361
|
+
];
|
|
2362
|
+
}
|
|
2040
2363
|
|
|
2041
2364
|
// src/compile.ts
|
|
2042
2365
|
var counter = 0;
|
|
@@ -2112,6 +2435,13 @@ function compile(ir, opts = {}) {
|
|
|
2112
2435
|
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)."
|
|
2113
2436
|
});
|
|
2114
2437
|
}
|
|
2438
|
+
const phase2ProfileResolver = opts.profileResolver ? (id) => {
|
|
2439
|
+
try {
|
|
2440
|
+
return opts.profileResolver(id);
|
|
2441
|
+
} catch {
|
|
2442
|
+
return void 0;
|
|
2443
|
+
}
|
|
2444
|
+
} : tryGetProfile;
|
|
2115
2445
|
const advisories = runAdvisor(
|
|
2116
2446
|
ir,
|
|
2117
2447
|
{
|
|
@@ -2121,7 +2451,11 @@ function compile(ir, opts = {}) {
|
|
|
2121
2451
|
diagnostics
|
|
2122
2452
|
},
|
|
2123
2453
|
profile,
|
|
2124
|
-
opts.policy
|
|
2454
|
+
opts.policy,
|
|
2455
|
+
{
|
|
2456
|
+
fallbackChain,
|
|
2457
|
+
profileResolver: phase2ProfileResolver
|
|
2458
|
+
}
|
|
2125
2459
|
);
|
|
2126
2460
|
return {
|
|
2127
2461
|
handle,
|
|
@@ -2174,84 +2508,6 @@ function validateFinalFit(ir, profile, tokens) {
|
|
|
2174
2508
|
}
|
|
2175
2509
|
}
|
|
2176
2510
|
|
|
2177
|
-
// src/brain-query.ts
|
|
2178
|
-
var FRESH_SNAPSHOT = {
|
|
2179
|
-
data: null,
|
|
2180
|
-
expiresAt: 0,
|
|
2181
|
-
refreshing: false,
|
|
2182
|
-
warned: false
|
|
2183
|
-
};
|
|
2184
|
-
var snapshot = { ...FRESH_SNAPSHOT };
|
|
2185
|
-
var runtime;
|
|
2186
|
-
function configureBrainQuery(rt) {
|
|
2187
|
-
runtime = rt;
|
|
2188
|
-
snapshot = { ...FRESH_SNAPSHOT };
|
|
2189
|
-
}
|
|
2190
|
-
function createBrainQueryCache(opts) {
|
|
2191
|
-
return () => {
|
|
2192
|
-
const rt = runtime;
|
|
2193
|
-
if (!rt || !rt.enabledTables.has(opts.table)) {
|
|
2194
|
-
return opts.bundledFallback();
|
|
2195
|
-
}
|
|
2196
|
-
const now = Date.now();
|
|
2197
|
-
const stale = snapshot.expiresAt <= now;
|
|
2198
|
-
if (stale && !snapshot.refreshing) {
|
|
2199
|
-
snapshot.refreshing = true;
|
|
2200
|
-
void asyncRefresh(rt);
|
|
2201
|
-
}
|
|
2202
|
-
if (snapshot.data) {
|
|
2203
|
-
const rows = snapshot.data[opts.table];
|
|
2204
|
-
if (Array.isArray(rows) && rows.length > 0) {
|
|
2205
|
-
try {
|
|
2206
|
-
return opts.mapRows(rows);
|
|
2207
|
-
} catch {
|
|
2208
|
-
return opts.bundledFallback();
|
|
2209
|
-
}
|
|
2210
|
-
}
|
|
2211
|
-
}
|
|
2212
|
-
return opts.bundledFallback();
|
|
2213
|
-
};
|
|
2214
|
-
}
|
|
2215
|
-
var pendingRefresh;
|
|
2216
|
-
async function asyncRefresh(rt) {
|
|
2217
|
-
const promise = doRefresh(rt);
|
|
2218
|
-
pendingRefresh = promise;
|
|
2219
|
-
try {
|
|
2220
|
-
await promise;
|
|
2221
|
-
} finally {
|
|
2222
|
-
if (pendingRefresh === promise) pendingRefresh = void 0;
|
|
2223
|
-
}
|
|
2224
|
-
}
|
|
2225
|
-
var DEFAULT_CONFIG_URL = "https://kgauto-dashboard.vercel.app/api/kgauto-v2/config";
|
|
2226
|
-
async function doRefresh(rt) {
|
|
2227
|
-
const url = rt.configEndpoint ?? DEFAULT_CONFIG_URL;
|
|
2228
|
-
try {
|
|
2229
|
-
const res = await rt.fetchImpl(url, { method: "GET" });
|
|
2230
|
-
if (!res.ok) {
|
|
2231
|
-
throw new Error(`brain-query ${res.status}: ${res.statusText}`);
|
|
2232
|
-
}
|
|
2233
|
-
const body = await res.json();
|
|
2234
|
-
if (runtime !== rt) return;
|
|
2235
|
-
snapshot = {
|
|
2236
|
-
data: body,
|
|
2237
|
-
expiresAt: Date.now() + rt.ttlMs,
|
|
2238
|
-
refreshing: false,
|
|
2239
|
-
warned: snapshot.warned
|
|
2240
|
-
};
|
|
2241
|
-
} catch (err) {
|
|
2242
|
-
if (runtime !== rt) return;
|
|
2243
|
-
snapshot.refreshing = false;
|
|
2244
|
-
snapshot.expiresAt = Date.now() + rt.ttlMs;
|
|
2245
|
-
if (!snapshot.warned) {
|
|
2246
|
-
snapshot.warned = true;
|
|
2247
|
-
(rt.onError ?? defaultOnError)(err);
|
|
2248
|
-
}
|
|
2249
|
-
}
|
|
2250
|
-
}
|
|
2251
|
-
function defaultOnError(err) {
|
|
2252
|
-
console.warn("[kgauto] brain-query failed (using bundled fallback):", err);
|
|
2253
|
-
}
|
|
2254
|
-
|
|
2255
2511
|
// src/pricing-brain.ts
|
|
2256
2512
|
function isPricingRow(x) {
|
|
2257
2513
|
if (!x || typeof x !== "object") return false;
|
|
@@ -3923,63 +4179,6 @@ function clamp(n) {
|
|
|
3923
4179
|
return Math.max(0, Math.min(1, n));
|
|
3924
4180
|
}
|
|
3925
4181
|
|
|
3926
|
-
// src/archetype-perf-brain.ts
|
|
3927
|
-
function isPerfRow(x) {
|
|
3928
|
-
if (!x || typeof x !== "object") return false;
|
|
3929
|
-
const r = x;
|
|
3930
|
-
return typeof r.model_id === "string" && typeof r.archetype === "string" && typeof r.perf_score === "number";
|
|
3931
|
-
}
|
|
3932
|
-
function mapRowsToPerfMap(rows) {
|
|
3933
|
-
const out = /* @__PURE__ */ new Map();
|
|
3934
|
-
for (const row of rows) {
|
|
3935
|
-
if (!isPerfRow(row)) continue;
|
|
3936
|
-
const existing = out.get(row.model_id) ?? {};
|
|
3937
|
-
existing[row.archetype] = row.perf_score;
|
|
3938
|
-
out.set(row.model_id, existing);
|
|
3939
|
-
}
|
|
3940
|
-
return out;
|
|
3941
|
-
}
|
|
3942
|
-
function mapRowsToNMap(rows) {
|
|
3943
|
-
const out = /* @__PURE__ */ new Map();
|
|
3944
|
-
for (const row of rows) {
|
|
3945
|
-
if (!isPerfRow(row)) continue;
|
|
3946
|
-
if (typeof row.n !== "number") continue;
|
|
3947
|
-
const existing = out.get(row.model_id) ?? {};
|
|
3948
|
-
existing[row.archetype] = row.n;
|
|
3949
|
-
out.set(row.model_id, existing);
|
|
3950
|
-
}
|
|
3951
|
-
return out;
|
|
3952
|
-
}
|
|
3953
|
-
function bundledArchetypePerf() {
|
|
3954
|
-
const out = /* @__PURE__ */ new Map();
|
|
3955
|
-
for (const profile of allProfiles()) {
|
|
3956
|
-
if (profile.archetypePerf) out.set(profile.id, profile.archetypePerf);
|
|
3957
|
-
}
|
|
3958
|
-
return out;
|
|
3959
|
-
}
|
|
3960
|
-
function bundledArchetypePerfN() {
|
|
3961
|
-
return /* @__PURE__ */ new Map();
|
|
3962
|
-
}
|
|
3963
|
-
var loadArchetypePerfFromBrain = createBrainQueryCache({
|
|
3964
|
-
table: "kgauto_archetype_perf",
|
|
3965
|
-
mapRows: mapRowsToPerfMap,
|
|
3966
|
-
bundledFallback: bundledArchetypePerf
|
|
3967
|
-
});
|
|
3968
|
-
var loadArchetypePerfNFromBrain = createBrainQueryCache(
|
|
3969
|
-
{
|
|
3970
|
-
table: "kgauto_archetype_perf",
|
|
3971
|
-
mapRows: mapRowsToNMap,
|
|
3972
|
-
bundledFallback: bundledArchetypePerfN
|
|
3973
|
-
}
|
|
3974
|
-
);
|
|
3975
|
-
var MEASURED_GROUNDING_MIN_N = 10;
|
|
3976
|
-
function getArchetypePerfScore(modelId, archetype) {
|
|
3977
|
-
const score = loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
|
|
3978
|
-
const n = loadArchetypePerfNFromBrain().get(modelId)?.[archetype] ?? 0;
|
|
3979
|
-
const grounding = n >= MEASURED_GROUNDING_MIN_N ? "measured" : "judgment";
|
|
3980
|
-
return { score, n, grounding };
|
|
3981
|
-
}
|
|
3982
|
-
|
|
3983
4182
|
// src/models-brain.ts
|
|
3984
4183
|
function isModelRow(x) {
|
|
3985
4184
|
if (!x || typeof x !== "object") return false;
|
|
@@ -4130,6 +4329,7 @@ function compile2(ir, opts) {
|
|
|
4130
4329
|
getArchetypePerfScore,
|
|
4131
4330
|
getDefaultFallbackChain,
|
|
4132
4331
|
getDefaultFallbackChainWithGrounding,
|
|
4332
|
+
getPerAxisMetrics,
|
|
4133
4333
|
getProfile,
|
|
4134
4334
|
getReachabilityDiagnostic,
|
|
4135
4335
|
getSequentialStarterChain,
|
|
@@ -4138,6 +4338,7 @@ function compile2(ir, opts) {
|
|
|
4138
4338
|
getStarterChainWithGrounding,
|
|
4139
4339
|
hashShape,
|
|
4140
4340
|
isArchetype,
|
|
4341
|
+
isBrainQueryActiveFor,
|
|
4141
4342
|
isModelReachable,
|
|
4142
4343
|
isProviderReachable,
|
|
4143
4344
|
learningKey,
|