@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.mjs
CHANGED
|
@@ -740,14 +740,250 @@ function setNestedField(obj, path, value) {
|
|
|
740
740
|
cursor[parts[parts.length - 1]] = value;
|
|
741
741
|
}
|
|
742
742
|
|
|
743
|
+
// src/brain-query.ts
|
|
744
|
+
var FRESH_SNAPSHOT = {
|
|
745
|
+
data: null,
|
|
746
|
+
expiresAt: 0,
|
|
747
|
+
refreshing: false,
|
|
748
|
+
warned: false
|
|
749
|
+
};
|
|
750
|
+
var snapshot = { ...FRESH_SNAPSHOT };
|
|
751
|
+
var runtime;
|
|
752
|
+
function configureBrainQuery(rt) {
|
|
753
|
+
runtime = rt;
|
|
754
|
+
snapshot = { ...FRESH_SNAPSHOT };
|
|
755
|
+
}
|
|
756
|
+
function createBrainQueryCache(opts) {
|
|
757
|
+
return () => {
|
|
758
|
+
const rt = runtime;
|
|
759
|
+
if (!rt || !rt.enabledTables.has(opts.table)) {
|
|
760
|
+
return opts.bundledFallback();
|
|
761
|
+
}
|
|
762
|
+
const now = Date.now();
|
|
763
|
+
const stale = snapshot.expiresAt <= now;
|
|
764
|
+
if (stale && !snapshot.refreshing) {
|
|
765
|
+
snapshot.refreshing = true;
|
|
766
|
+
void asyncRefresh(rt);
|
|
767
|
+
}
|
|
768
|
+
if (snapshot.data) {
|
|
769
|
+
const rows = snapshot.data[opts.table];
|
|
770
|
+
if (Array.isArray(rows) && rows.length > 0) {
|
|
771
|
+
try {
|
|
772
|
+
return opts.mapRows(rows);
|
|
773
|
+
} catch {
|
|
774
|
+
return opts.bundledFallback();
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return opts.bundledFallback();
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
var pendingRefresh;
|
|
782
|
+
async function asyncRefresh(rt) {
|
|
783
|
+
const promise = doRefresh(rt);
|
|
784
|
+
pendingRefresh = promise;
|
|
785
|
+
try {
|
|
786
|
+
await promise;
|
|
787
|
+
} finally {
|
|
788
|
+
if (pendingRefresh === promise) pendingRefresh = void 0;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
var DEFAULT_CONFIG_URL = "https://kgauto-dashboard.vercel.app/api/kgauto-v2/config";
|
|
792
|
+
async function doRefresh(rt) {
|
|
793
|
+
const url = rt.configEndpoint ?? DEFAULT_CONFIG_URL;
|
|
794
|
+
try {
|
|
795
|
+
const res = await rt.fetchImpl(url, { method: "GET" });
|
|
796
|
+
if (!res.ok) {
|
|
797
|
+
throw new Error(`brain-query ${res.status}: ${res.statusText}`);
|
|
798
|
+
}
|
|
799
|
+
const body = await res.json();
|
|
800
|
+
if (runtime !== rt) return;
|
|
801
|
+
snapshot = {
|
|
802
|
+
data: body,
|
|
803
|
+
expiresAt: Date.now() + rt.ttlMs,
|
|
804
|
+
refreshing: false,
|
|
805
|
+
warned: snapshot.warned
|
|
806
|
+
};
|
|
807
|
+
} catch (err) {
|
|
808
|
+
if (runtime !== rt) return;
|
|
809
|
+
snapshot.refreshing = false;
|
|
810
|
+
snapshot.expiresAt = Date.now() + rt.ttlMs;
|
|
811
|
+
if (!snapshot.warned) {
|
|
812
|
+
snapshot.warned = true;
|
|
813
|
+
(rt.onError ?? defaultOnError)(err);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function defaultOnError(err) {
|
|
818
|
+
console.warn("[kgauto] brain-query failed (using bundled fallback):", err);
|
|
819
|
+
}
|
|
820
|
+
function isBrainQueryActiveFor(table) {
|
|
821
|
+
return runtime !== void 0 && runtime.enabledTables.has(table);
|
|
822
|
+
}
|
|
823
|
+
async function getPerAxisMetrics(opts) {
|
|
824
|
+
const fetchFn = opts.fetch ?? fetch;
|
|
825
|
+
const endpoint = opts.endpoint ?? runtime?.endpoint;
|
|
826
|
+
if (!endpoint) return null;
|
|
827
|
+
const windowDays = opts.windowDays ?? 30;
|
|
828
|
+
const body = {
|
|
829
|
+
p_app_id: opts.appId,
|
|
830
|
+
p_archetype: opts.archetype,
|
|
831
|
+
p_model: opts.model,
|
|
832
|
+
p_window_days: windowDays,
|
|
833
|
+
p_quality_floor: opts.qualityFloor ?? null
|
|
834
|
+
};
|
|
835
|
+
const headers = {
|
|
836
|
+
Accept: "application/json",
|
|
837
|
+
"Content-Type": "application/json",
|
|
838
|
+
...opts.apiKey ? { Authorization: `Bearer ${opts.apiKey}` } : {}
|
|
839
|
+
};
|
|
840
|
+
try {
|
|
841
|
+
const res = await fetchFn(`${endpoint}/rpc/get_per_axis_metrics`, {
|
|
842
|
+
method: "POST",
|
|
843
|
+
headers,
|
|
844
|
+
body: JSON.stringify(body)
|
|
845
|
+
});
|
|
846
|
+
if (!res.ok) return null;
|
|
847
|
+
const raw = await res.json();
|
|
848
|
+
return mapPerAxisMetrics(raw, opts.appId, opts.archetype, opts.model, windowDays);
|
|
849
|
+
} catch {
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
function mapPerAxisMetrics(raw, fallbackAppId, fallbackArchetype, fallbackModel, fallbackWindowDays) {
|
|
854
|
+
if (raw === null || raw === void 0) return null;
|
|
855
|
+
if (typeof raw !== "object") return null;
|
|
856
|
+
const r = raw;
|
|
857
|
+
if (Array.isArray(raw)) {
|
|
858
|
+
if (raw.length === 0) return null;
|
|
859
|
+
return mapPerAxisMetrics(raw[0], fallbackAppId, fallbackArchetype, fallbackModel, fallbackWindowDays);
|
|
860
|
+
}
|
|
861
|
+
const num = (v) => {
|
|
862
|
+
if (v === null || v === void 0) return null;
|
|
863
|
+
if (typeof v === "number") return Number.isFinite(v) ? v : null;
|
|
864
|
+
if (typeof v === "string") {
|
|
865
|
+
const n = Number(v);
|
|
866
|
+
return Number.isFinite(n) ? n : null;
|
|
867
|
+
}
|
|
868
|
+
return null;
|
|
869
|
+
};
|
|
870
|
+
const int = (v) => {
|
|
871
|
+
const n = num(v);
|
|
872
|
+
return n === null ? 0 : Math.trunc(n);
|
|
873
|
+
};
|
|
874
|
+
const bool = (v) => {
|
|
875
|
+
if (v === null || v === void 0) return null;
|
|
876
|
+
if (typeof v === "boolean") return v;
|
|
877
|
+
return null;
|
|
878
|
+
};
|
|
879
|
+
const str = (v, fallback) => typeof v === "string" ? v : fallback;
|
|
880
|
+
const cost = r.cost_efficiency ?? {};
|
|
881
|
+
const time = r.time_efficiency ?? {};
|
|
882
|
+
const rel = r.reliability ?? {};
|
|
883
|
+
return {
|
|
884
|
+
appId: str(r.app_id, fallbackAppId),
|
|
885
|
+
archetype: str(r.archetype, fallbackArchetype),
|
|
886
|
+
model: str(r.model, fallbackModel),
|
|
887
|
+
windowDays: num(r.window_days) ?? fallbackWindowDays,
|
|
888
|
+
nRows: int(r.n_rows),
|
|
889
|
+
nRowsClean: int(r.n_rows_clean),
|
|
890
|
+
nQualityOutcomes: int(r.n_quality_outcomes),
|
|
891
|
+
magicRate: num(r.magic_rate),
|
|
892
|
+
qualityFloorMet: bool(r.quality_floor_met),
|
|
893
|
+
costEfficiency: {
|
|
894
|
+
avgCostUsd: num(cost.avg_cost_usd),
|
|
895
|
+
avgCostUsdClean: num(cost.avg_cost_usd_clean),
|
|
896
|
+
avgInputTokens: num(cost.avg_input_tokens),
|
|
897
|
+
avgOutputTokens: num(cost.avg_output_tokens),
|
|
898
|
+
inputTokenRatio: num(cost.input_token_ratio)
|
|
899
|
+
},
|
|
900
|
+
timeEfficiency: {
|
|
901
|
+
avgLatencyMs: num(time.avg_latency_ms),
|
|
902
|
+
avgTtftMs: num(time.avg_ttft_ms)
|
|
903
|
+
},
|
|
904
|
+
reliability: {
|
|
905
|
+
successRate: num(rel.success_rate),
|
|
906
|
+
successRateClean: num(rel.success_rate_clean),
|
|
907
|
+
emptyRate: num(rel.empty_rate),
|
|
908
|
+
emptyRateClean: num(rel.empty_rate_clean)
|
|
909
|
+
},
|
|
910
|
+
evidenceFreshnessDays: num(r.evidence_freshness_days)
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
// src/archetype-perf-brain.ts
|
|
915
|
+
function isPerfRow(x) {
|
|
916
|
+
if (!x || typeof x !== "object") return false;
|
|
917
|
+
const r = x;
|
|
918
|
+
return typeof r.model_id === "string" && typeof r.archetype === "string" && typeof r.perf_score === "number";
|
|
919
|
+
}
|
|
920
|
+
function mapRowsToPerfMap(rows) {
|
|
921
|
+
const out = /* @__PURE__ */ new Map();
|
|
922
|
+
for (const row of rows) {
|
|
923
|
+
if (!isPerfRow(row)) continue;
|
|
924
|
+
const existing = out.get(row.model_id) ?? {};
|
|
925
|
+
existing[row.archetype] = row.perf_score;
|
|
926
|
+
out.set(row.model_id, existing);
|
|
927
|
+
}
|
|
928
|
+
return out;
|
|
929
|
+
}
|
|
930
|
+
function mapRowsToNMap(rows) {
|
|
931
|
+
const out = /* @__PURE__ */ new Map();
|
|
932
|
+
for (const row of rows) {
|
|
933
|
+
if (!isPerfRow(row)) continue;
|
|
934
|
+
if (typeof row.n !== "number") continue;
|
|
935
|
+
const existing = out.get(row.model_id) ?? {};
|
|
936
|
+
existing[row.archetype] = row.n;
|
|
937
|
+
out.set(row.model_id, existing);
|
|
938
|
+
}
|
|
939
|
+
return out;
|
|
940
|
+
}
|
|
941
|
+
function bundledArchetypePerf() {
|
|
942
|
+
const out = /* @__PURE__ */ new Map();
|
|
943
|
+
for (const profile of allProfiles()) {
|
|
944
|
+
if (profile.archetypePerf) out.set(profile.id, profile.archetypePerf);
|
|
945
|
+
}
|
|
946
|
+
return out;
|
|
947
|
+
}
|
|
948
|
+
function bundledArchetypePerfN() {
|
|
949
|
+
return /* @__PURE__ */ new Map();
|
|
950
|
+
}
|
|
951
|
+
var loadArchetypePerfFromBrain = createBrainQueryCache({
|
|
952
|
+
table: "kgauto_archetype_perf",
|
|
953
|
+
mapRows: mapRowsToPerfMap,
|
|
954
|
+
bundledFallback: bundledArchetypePerf
|
|
955
|
+
});
|
|
956
|
+
var loadArchetypePerfNFromBrain = createBrainQueryCache(
|
|
957
|
+
{
|
|
958
|
+
table: "kgauto_archetype_perf",
|
|
959
|
+
mapRows: mapRowsToNMap,
|
|
960
|
+
bundledFallback: bundledArchetypePerfN
|
|
961
|
+
}
|
|
962
|
+
);
|
|
963
|
+
var MEASURED_GROUNDING_MIN_N = 10;
|
|
964
|
+
function getArchetypePerfScore(modelId, archetype) {
|
|
965
|
+
const score = loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
|
|
966
|
+
const n = loadArchetypePerfNFromBrain().get(modelId)?.[archetype] ?? 0;
|
|
967
|
+
const grounding = n >= MEASURED_GROUNDING_MIN_N ? "measured" : "judgment";
|
|
968
|
+
return { score, n, grounding };
|
|
969
|
+
}
|
|
970
|
+
|
|
743
971
|
// src/advisor.ts
|
|
744
|
-
|
|
972
|
+
var QUALITY_FLOOR_FOR_RECOMMENDATION = 6;
|
|
973
|
+
var TIER_DOWN_COST_RATIO = 0.5;
|
|
974
|
+
var COST_MISMATCHED_CHOSEN_SCORE_CEILING = 7;
|
|
975
|
+
function runAdvisor(ir, result, profile, policy, phase2) {
|
|
745
976
|
const out = [];
|
|
746
977
|
out.push(...detectCachingOff(ir, profile));
|
|
747
978
|
out.push(...detectSingleChunkSystem(ir, profile));
|
|
748
979
|
out.push(...detectToolBloat(ir, result));
|
|
749
980
|
out.push(...detectHistoryUncached(ir, profile));
|
|
750
981
|
out.push(...detectSingleModelArray(ir, policy));
|
|
982
|
+
if (policy?.posture !== "locked") {
|
|
983
|
+
out.push(...detectCostMismatchedArchetype(ir, profile, phase2));
|
|
984
|
+
out.push(...detectModelStaleEvidence(ir, profile));
|
|
985
|
+
out.push(...detectTierDown(ir, profile, phase2));
|
|
986
|
+
}
|
|
751
987
|
return out;
|
|
752
988
|
}
|
|
753
989
|
function detectCachingOff(ir, profile) {
|
|
@@ -833,6 +1069,91 @@ function detectSingleModelArray(ir, policy) {
|
|
|
833
1069
|
}
|
|
834
1070
|
];
|
|
835
1071
|
}
|
|
1072
|
+
function detectCostMismatchedArchetype(ir, profile, phase2) {
|
|
1073
|
+
if (!phase2 || phase2.fallbackChain.length === 0) return [];
|
|
1074
|
+
if (!phase2.profileResolver) return [];
|
|
1075
|
+
const archetype = ir.intent.archetype;
|
|
1076
|
+
const chosenScore = getArchetypePerfScore(profile.id, archetype);
|
|
1077
|
+
const chosenHasRoomToGrow = chosenScore.grounding === "judgment" || chosenScore.score < COST_MISMATCHED_CHOSEN_SCORE_CEILING;
|
|
1078
|
+
if (!chosenHasRoomToGrow) return [];
|
|
1079
|
+
let bestAlt = null;
|
|
1080
|
+
for (const altId of phase2.fallbackChain) {
|
|
1081
|
+
const altProfile = phase2.profileResolver(altId);
|
|
1082
|
+
if (!altProfile) continue;
|
|
1083
|
+
if (altProfile.id === profile.id) continue;
|
|
1084
|
+
const altScore = getArchetypePerfScore(altProfile.id, archetype);
|
|
1085
|
+
if (altScore.score < QUALITY_FLOOR_FOR_RECOMMENDATION) continue;
|
|
1086
|
+
if (altScore.score < chosenScore.score) continue;
|
|
1087
|
+
if (altProfile.costInputPer1m >= profile.costInputPer1m) continue;
|
|
1088
|
+
if (!bestAlt || altScore.score > bestAlt.score.score || altScore.score === bestAlt.score.score && altProfile.costInputPer1m < bestAlt.profile.costInputPer1m) {
|
|
1089
|
+
bestAlt = { id: altId, profile: altProfile, score: altScore };
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
if (!bestAlt) return [];
|
|
1093
|
+
const tierDownWouldFire = bestAlt.score.grounding === "measured" && bestAlt.profile.costInputPer1m <= profile.costInputPer1m * TIER_DOWN_COST_RATIO;
|
|
1094
|
+
if (tierDownWouldFire) return [];
|
|
1095
|
+
const chosenGrounding = chosenScore.grounding === "judgment" ? `archetypePerf.${archetype}=judgment` : `archetypePerf.${archetype}=${chosenScore.score}`;
|
|
1096
|
+
const altGrounding = bestAlt.score.grounding === "measured" ? `archetypePerf.${archetype}=${bestAlt.score.score}, measured, n=${bestAlt.score.n}` : `archetypePerf.${archetype}=${bestAlt.score.score}, judgment`;
|
|
1097
|
+
return [
|
|
1098
|
+
{
|
|
1099
|
+
level: "warn",
|
|
1100
|
+
code: "cost-mismatched-archetype",
|
|
1101
|
+
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.`,
|
|
1102
|
+
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.`,
|
|
1103
|
+
recommendationType: profile.provider === bestAlt.profile.provider ? "tier-down" : "model-swap",
|
|
1104
|
+
docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
|
|
1105
|
+
}
|
|
1106
|
+
];
|
|
1107
|
+
}
|
|
1108
|
+
function detectModelStaleEvidence(ir, profile) {
|
|
1109
|
+
if (!isBrainQueryActiveFor("kgauto_archetype_perf")) return [];
|
|
1110
|
+
const archetype = ir.intent.archetype;
|
|
1111
|
+
const chosen = getArchetypePerfScore(profile.id, archetype);
|
|
1112
|
+
if (chosen.grounding !== "judgment") return [];
|
|
1113
|
+
return [
|
|
1114
|
+
{
|
|
1115
|
+
level: "info",
|
|
1116
|
+
code: "model-stale-evidence",
|
|
1117
|
+
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.`,
|
|
1118
|
+
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.",
|
|
1119
|
+
recommendationType: "prompt-fix",
|
|
1120
|
+
docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
|
|
1121
|
+
}
|
|
1122
|
+
];
|
|
1123
|
+
}
|
|
1124
|
+
function detectTierDown(ir, profile, phase2) {
|
|
1125
|
+
if (!phase2 || phase2.fallbackChain.length === 0) return [];
|
|
1126
|
+
if (!phase2.profileResolver) return [];
|
|
1127
|
+
const archetype = ir.intent.archetype;
|
|
1128
|
+
const chosenScore = getArchetypePerfScore(profile.id, archetype);
|
|
1129
|
+
const chosenCost = profile.costInputPer1m;
|
|
1130
|
+
let bestAlt = null;
|
|
1131
|
+
for (const altId of phase2.fallbackChain) {
|
|
1132
|
+
const altProfile = phase2.profileResolver(altId);
|
|
1133
|
+
if (!altProfile) continue;
|
|
1134
|
+
if (altProfile.id === profile.id) continue;
|
|
1135
|
+
const altScore = getArchetypePerfScore(altProfile.id, archetype);
|
|
1136
|
+
if (altScore.grounding !== "measured") continue;
|
|
1137
|
+
if (altScore.score < QUALITY_FLOOR_FOR_RECOMMENDATION) continue;
|
|
1138
|
+
if (altScore.score < chosenScore.score) continue;
|
|
1139
|
+
if (altProfile.costInputPer1m > chosenCost * TIER_DOWN_COST_RATIO) continue;
|
|
1140
|
+
if (!bestAlt || altProfile.costInputPer1m < bestAlt.profile.costInputPer1m || altProfile.costInputPer1m === bestAlt.profile.costInputPer1m && altScore.score > bestAlt.score.score) {
|
|
1141
|
+
bestAlt = { id: altId, profile: altProfile, score: altScore };
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
if (!bestAlt) return [];
|
|
1145
|
+
const chosenDesc = chosenScore.grounding === "measured" ? `archetypePerf.${archetype}=${chosenScore.score} (measured, n=${chosenScore.n})` : `archetypePerf.${archetype}=${chosenScore.score} (${chosenScore.grounding})`;
|
|
1146
|
+
return [
|
|
1147
|
+
{
|
|
1148
|
+
level: "warn",
|
|
1149
|
+
code: "tier-down",
|
|
1150
|
+
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.`,
|
|
1151
|
+
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.`,
|
|
1152
|
+
recommendationType: "tier-down",
|
|
1153
|
+
docsUrl: "https://github.com/stue/command-center/blob/main/interfaces/kgauto.md#best-practice-advisories"
|
|
1154
|
+
}
|
|
1155
|
+
];
|
|
1156
|
+
}
|
|
836
1157
|
|
|
837
1158
|
// src/compile.ts
|
|
838
1159
|
var counter = 0;
|
|
@@ -908,6 +1229,13 @@ function compile(ir, opts = {}) {
|
|
|
908
1229
|
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)."
|
|
909
1230
|
});
|
|
910
1231
|
}
|
|
1232
|
+
const phase2ProfileResolver = opts.profileResolver ? (id) => {
|
|
1233
|
+
try {
|
|
1234
|
+
return opts.profileResolver(id);
|
|
1235
|
+
} catch {
|
|
1236
|
+
return void 0;
|
|
1237
|
+
}
|
|
1238
|
+
} : tryGetProfile;
|
|
911
1239
|
const advisories = runAdvisor(
|
|
912
1240
|
ir,
|
|
913
1241
|
{
|
|
@@ -917,7 +1245,11 @@ function compile(ir, opts = {}) {
|
|
|
917
1245
|
diagnostics
|
|
918
1246
|
},
|
|
919
1247
|
profile,
|
|
920
|
-
opts.policy
|
|
1248
|
+
opts.policy,
|
|
1249
|
+
{
|
|
1250
|
+
fallbackChain,
|
|
1251
|
+
profileResolver: phase2ProfileResolver
|
|
1252
|
+
}
|
|
921
1253
|
);
|
|
922
1254
|
return {
|
|
923
1255
|
handle,
|
|
@@ -970,84 +1302,6 @@ function validateFinalFit(ir, profile, tokens) {
|
|
|
970
1302
|
}
|
|
971
1303
|
}
|
|
972
1304
|
|
|
973
|
-
// src/brain-query.ts
|
|
974
|
-
var FRESH_SNAPSHOT = {
|
|
975
|
-
data: null,
|
|
976
|
-
expiresAt: 0,
|
|
977
|
-
refreshing: false,
|
|
978
|
-
warned: false
|
|
979
|
-
};
|
|
980
|
-
var snapshot = { ...FRESH_SNAPSHOT };
|
|
981
|
-
var runtime;
|
|
982
|
-
function configureBrainQuery(rt) {
|
|
983
|
-
runtime = rt;
|
|
984
|
-
snapshot = { ...FRESH_SNAPSHOT };
|
|
985
|
-
}
|
|
986
|
-
function createBrainQueryCache(opts) {
|
|
987
|
-
return () => {
|
|
988
|
-
const rt = runtime;
|
|
989
|
-
if (!rt || !rt.enabledTables.has(opts.table)) {
|
|
990
|
-
return opts.bundledFallback();
|
|
991
|
-
}
|
|
992
|
-
const now = Date.now();
|
|
993
|
-
const stale = snapshot.expiresAt <= now;
|
|
994
|
-
if (stale && !snapshot.refreshing) {
|
|
995
|
-
snapshot.refreshing = true;
|
|
996
|
-
void asyncRefresh(rt);
|
|
997
|
-
}
|
|
998
|
-
if (snapshot.data) {
|
|
999
|
-
const rows = snapshot.data[opts.table];
|
|
1000
|
-
if (Array.isArray(rows) && rows.length > 0) {
|
|
1001
|
-
try {
|
|
1002
|
-
return opts.mapRows(rows);
|
|
1003
|
-
} catch {
|
|
1004
|
-
return opts.bundledFallback();
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
return opts.bundledFallback();
|
|
1009
|
-
};
|
|
1010
|
-
}
|
|
1011
|
-
var pendingRefresh;
|
|
1012
|
-
async function asyncRefresh(rt) {
|
|
1013
|
-
const promise = doRefresh(rt);
|
|
1014
|
-
pendingRefresh = promise;
|
|
1015
|
-
try {
|
|
1016
|
-
await promise;
|
|
1017
|
-
} finally {
|
|
1018
|
-
if (pendingRefresh === promise) pendingRefresh = void 0;
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
var DEFAULT_CONFIG_URL = "https://kgauto-dashboard.vercel.app/api/kgauto-v2/config";
|
|
1022
|
-
async function doRefresh(rt) {
|
|
1023
|
-
const url = rt.configEndpoint ?? DEFAULT_CONFIG_URL;
|
|
1024
|
-
try {
|
|
1025
|
-
const res = await rt.fetchImpl(url, { method: "GET" });
|
|
1026
|
-
if (!res.ok) {
|
|
1027
|
-
throw new Error(`brain-query ${res.status}: ${res.statusText}`);
|
|
1028
|
-
}
|
|
1029
|
-
const body = await res.json();
|
|
1030
|
-
if (runtime !== rt) return;
|
|
1031
|
-
snapshot = {
|
|
1032
|
-
data: body,
|
|
1033
|
-
expiresAt: Date.now() + rt.ttlMs,
|
|
1034
|
-
refreshing: false,
|
|
1035
|
-
warned: snapshot.warned
|
|
1036
|
-
};
|
|
1037
|
-
} catch (err) {
|
|
1038
|
-
if (runtime !== rt) return;
|
|
1039
|
-
snapshot.refreshing = false;
|
|
1040
|
-
snapshot.expiresAt = Date.now() + rt.ttlMs;
|
|
1041
|
-
if (!snapshot.warned) {
|
|
1042
|
-
snapshot.warned = true;
|
|
1043
|
-
(rt.onError ?? defaultOnError)(err);
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
function defaultOnError(err) {
|
|
1048
|
-
console.warn("[kgauto] brain-query failed (using bundled fallback):", err);
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
1305
|
// src/pricing-brain.ts
|
|
1052
1306
|
function isPricingRow(x) {
|
|
1053
1307
|
if (!x || typeof x !== "object") return false;
|
|
@@ -2431,63 +2685,6 @@ function clamp(n) {
|
|
|
2431
2685
|
return Math.max(0, Math.min(1, n));
|
|
2432
2686
|
}
|
|
2433
2687
|
|
|
2434
|
-
// src/archetype-perf-brain.ts
|
|
2435
|
-
function isPerfRow(x) {
|
|
2436
|
-
if (!x || typeof x !== "object") return false;
|
|
2437
|
-
const r = x;
|
|
2438
|
-
return typeof r.model_id === "string" && typeof r.archetype === "string" && typeof r.perf_score === "number";
|
|
2439
|
-
}
|
|
2440
|
-
function mapRowsToPerfMap(rows) {
|
|
2441
|
-
const out = /* @__PURE__ */ new Map();
|
|
2442
|
-
for (const row of rows) {
|
|
2443
|
-
if (!isPerfRow(row)) continue;
|
|
2444
|
-
const existing = out.get(row.model_id) ?? {};
|
|
2445
|
-
existing[row.archetype] = row.perf_score;
|
|
2446
|
-
out.set(row.model_id, existing);
|
|
2447
|
-
}
|
|
2448
|
-
return out;
|
|
2449
|
-
}
|
|
2450
|
-
function mapRowsToNMap(rows) {
|
|
2451
|
-
const out = /* @__PURE__ */ new Map();
|
|
2452
|
-
for (const row of rows) {
|
|
2453
|
-
if (!isPerfRow(row)) continue;
|
|
2454
|
-
if (typeof row.n !== "number") continue;
|
|
2455
|
-
const existing = out.get(row.model_id) ?? {};
|
|
2456
|
-
existing[row.archetype] = row.n;
|
|
2457
|
-
out.set(row.model_id, existing);
|
|
2458
|
-
}
|
|
2459
|
-
return out;
|
|
2460
|
-
}
|
|
2461
|
-
function bundledArchetypePerf() {
|
|
2462
|
-
const out = /* @__PURE__ */ new Map();
|
|
2463
|
-
for (const profile of allProfiles()) {
|
|
2464
|
-
if (profile.archetypePerf) out.set(profile.id, profile.archetypePerf);
|
|
2465
|
-
}
|
|
2466
|
-
return out;
|
|
2467
|
-
}
|
|
2468
|
-
function bundledArchetypePerfN() {
|
|
2469
|
-
return /* @__PURE__ */ new Map();
|
|
2470
|
-
}
|
|
2471
|
-
var loadArchetypePerfFromBrain = createBrainQueryCache({
|
|
2472
|
-
table: "kgauto_archetype_perf",
|
|
2473
|
-
mapRows: mapRowsToPerfMap,
|
|
2474
|
-
bundledFallback: bundledArchetypePerf
|
|
2475
|
-
});
|
|
2476
|
-
var loadArchetypePerfNFromBrain = createBrainQueryCache(
|
|
2477
|
-
{
|
|
2478
|
-
table: "kgauto_archetype_perf",
|
|
2479
|
-
mapRows: mapRowsToNMap,
|
|
2480
|
-
bundledFallback: bundledArchetypePerfN
|
|
2481
|
-
}
|
|
2482
|
-
);
|
|
2483
|
-
var MEASURED_GROUNDING_MIN_N = 10;
|
|
2484
|
-
function getArchetypePerfScore(modelId, archetype) {
|
|
2485
|
-
const score = loadArchetypePerfFromBrain().get(modelId)?.[archetype] ?? 5;
|
|
2486
|
-
const n = loadArchetypePerfNFromBrain().get(modelId)?.[archetype] ?? 0;
|
|
2487
|
-
const grounding = n >= MEASURED_GROUNDING_MIN_N ? "measured" : "judgment";
|
|
2488
|
-
return { score, n, grounding };
|
|
2489
|
-
}
|
|
2490
|
-
|
|
2491
2688
|
// src/models-brain.ts
|
|
2492
2689
|
function isModelRow(x) {
|
|
2493
2690
|
if (!x || typeof x !== "object") return false;
|
|
@@ -2637,6 +2834,7 @@ export {
|
|
|
2637
2834
|
getArchetypePerfScore,
|
|
2638
2835
|
getDefaultFallbackChain,
|
|
2639
2836
|
getDefaultFallbackChainWithGrounding,
|
|
2837
|
+
getPerAxisMetrics,
|
|
2640
2838
|
getProfile,
|
|
2641
2839
|
getReachabilityDiagnostic,
|
|
2642
2840
|
getSequentialStarterChain,
|
|
@@ -2645,6 +2843,7 @@ export {
|
|
|
2645
2843
|
getStarterChainWithGrounding,
|
|
2646
2844
|
hashShape,
|
|
2647
2845
|
isArchetype,
|
|
2846
|
+
isBrainQueryActiveFor,
|
|
2648
2847
|
isModelReachable,
|
|
2649
2848
|
isProviderReachable,
|
|
2650
2849
|
learningKey,
|
|
@@ -796,5 +796,42 @@ interface ChainWithGrounding {
|
|
|
796
796
|
/** Ordered: position 0 = primary, rising index = fallback positions. */
|
|
797
797
|
entries: ChainEntry[];
|
|
798
798
|
}
|
|
799
|
+
/** alpha.23 (s78 Phase 3): per-axis metrics returned by the brain RPC. */
|
|
800
|
+
interface PerAxisMetrics {
|
|
801
|
+
appId: string;
|
|
802
|
+
archetype: string;
|
|
803
|
+
model: string;
|
|
804
|
+
windowDays: number;
|
|
805
|
+
/** Total brain rows for this tuple in the window. */
|
|
806
|
+
nRows: number;
|
|
807
|
+
/** Subset of nRows with zero advisories fired — the "clean signal" comparator. */
|
|
808
|
+
nRowsClean: number;
|
|
809
|
+
/** Count of compile_outcome_quality entries joining to this tuple's outcomes. */
|
|
810
|
+
nQualityOutcomes: number;
|
|
811
|
+
/** Approve rate from quality outcomes. null when nQualityOutcomes === 0. */
|
|
812
|
+
magicRate: number | null;
|
|
813
|
+
/** Whether magicRate >= consumer-declared qualityFloor. null when no floor declared OR no outcomes. */
|
|
814
|
+
qualityFloorMet: boolean | null;
|
|
815
|
+
costEfficiency: {
|
|
816
|
+
avgCostUsd: number | null;
|
|
817
|
+
avgCostUsdClean: number | null;
|
|
818
|
+
avgInputTokens: number | null;
|
|
819
|
+
avgOutputTokens: number | null;
|
|
820
|
+
inputTokenRatio: number | null;
|
|
821
|
+
};
|
|
822
|
+
timeEfficiency: {
|
|
823
|
+
avgLatencyMs: number | null;
|
|
824
|
+
avgTtftMs: number | null;
|
|
825
|
+
};
|
|
826
|
+
reliability: {
|
|
827
|
+
successRate: number | null;
|
|
828
|
+
successRateClean: number | null;
|
|
829
|
+
emptyRate: number | null;
|
|
830
|
+
emptyRateClean: number | null;
|
|
831
|
+
};
|
|
832
|
+
evidenceFreshnessDays: number | null;
|
|
833
|
+
}
|
|
834
|
+
/** Per-axis metrics keyed by model — used for chain-comparison views. */
|
|
835
|
+
type PerAxisMetricsByModel = Record<string, PerAxisMetrics>;
|
|
799
836
|
|
|
800
|
-
export { type ApiKeys as A, type BestPracticeAdvisory as B, type CompilePolicy as C, type FallbackReason as F, type Grounding as G, type HistoryCachePolicy as H, type IntentDeclaration as I, type Message as M, type NormalizedResponse as N, type OutcomeResult as O, type ProviderOverrides as P, type RecordInput as R, type ToolCall as T, type CompiledRequest as a, type PromptIR as b, type CallOptions as c, type CallResult as d, type RecordOutcomeInput as e, type OracleScore as f, type CompileResult as g, type
|
|
837
|
+
export { type ApiKeys as A, type BestPracticeAdvisory as B, type CompilePolicy as C, type FallbackReason as F, type Grounding as G, type HistoryCachePolicy as H, type IntentDeclaration as I, type Message as M, type NormalizedResponse as N, type OutcomeResult as O, type ProviderOverrides as P, type RecordInput as R, type ToolCall as T, type CompiledRequest as a, type PromptIR as b, type CallOptions as c, type CallResult as d, type RecordOutcomeInput as e, type OracleScore as f, type CompileResult as g, type PerAxisMetrics as h, type Provider as i, type ChainEntry as j, type CallAttempt as k, CallError as l, type ChainWithGrounding as m, type Constraints as n, type MutationApplied as o, type NormalizedTokens as p, type OutcomeKind as q, type PerAxisMetricsByModel as r, type PromptSection as s, type ToolDefinition as t };
|
|
@@ -796,5 +796,42 @@ interface ChainWithGrounding {
|
|
|
796
796
|
/** Ordered: position 0 = primary, rising index = fallback positions. */
|
|
797
797
|
entries: ChainEntry[];
|
|
798
798
|
}
|
|
799
|
+
/** alpha.23 (s78 Phase 3): per-axis metrics returned by the brain RPC. */
|
|
800
|
+
interface PerAxisMetrics {
|
|
801
|
+
appId: string;
|
|
802
|
+
archetype: string;
|
|
803
|
+
model: string;
|
|
804
|
+
windowDays: number;
|
|
805
|
+
/** Total brain rows for this tuple in the window. */
|
|
806
|
+
nRows: number;
|
|
807
|
+
/** Subset of nRows with zero advisories fired — the "clean signal" comparator. */
|
|
808
|
+
nRowsClean: number;
|
|
809
|
+
/** Count of compile_outcome_quality entries joining to this tuple's outcomes. */
|
|
810
|
+
nQualityOutcomes: number;
|
|
811
|
+
/** Approve rate from quality outcomes. null when nQualityOutcomes === 0. */
|
|
812
|
+
magicRate: number | null;
|
|
813
|
+
/** Whether magicRate >= consumer-declared qualityFloor. null when no floor declared OR no outcomes. */
|
|
814
|
+
qualityFloorMet: boolean | null;
|
|
815
|
+
costEfficiency: {
|
|
816
|
+
avgCostUsd: number | null;
|
|
817
|
+
avgCostUsdClean: number | null;
|
|
818
|
+
avgInputTokens: number | null;
|
|
819
|
+
avgOutputTokens: number | null;
|
|
820
|
+
inputTokenRatio: number | null;
|
|
821
|
+
};
|
|
822
|
+
timeEfficiency: {
|
|
823
|
+
avgLatencyMs: number | null;
|
|
824
|
+
avgTtftMs: number | null;
|
|
825
|
+
};
|
|
826
|
+
reliability: {
|
|
827
|
+
successRate: number | null;
|
|
828
|
+
successRateClean: number | null;
|
|
829
|
+
emptyRate: number | null;
|
|
830
|
+
emptyRateClean: number | null;
|
|
831
|
+
};
|
|
832
|
+
evidenceFreshnessDays: number | null;
|
|
833
|
+
}
|
|
834
|
+
/** Per-axis metrics keyed by model — used for chain-comparison views. */
|
|
835
|
+
type PerAxisMetricsByModel = Record<string, PerAxisMetrics>;
|
|
799
836
|
|
|
800
|
-
export { type ApiKeys as A, type BestPracticeAdvisory as B, type CompilePolicy as C, type FallbackReason as F, type Grounding as G, type HistoryCachePolicy as H, type IntentDeclaration as I, type Message as M, type NormalizedResponse as N, type OutcomeResult as O, type ProviderOverrides as P, type RecordInput as R, type ToolCall as T, type CompiledRequest as a, type PromptIR as b, type CallOptions as c, type CallResult as d, type RecordOutcomeInput as e, type OracleScore as f, type CompileResult as g, type
|
|
837
|
+
export { type ApiKeys as A, type BestPracticeAdvisory as B, type CompilePolicy as C, type FallbackReason as F, type Grounding as G, type HistoryCachePolicy as H, type IntentDeclaration as I, type Message as M, type NormalizedResponse as N, type OutcomeResult as O, type ProviderOverrides as P, type RecordInput as R, type ToolCall as T, type CompiledRequest as a, type PromptIR as b, type CallOptions as c, type CallResult as d, type RecordOutcomeInput as e, type OracleScore as f, type CompileResult as g, type PerAxisMetrics as h, type Provider as i, type ChainEntry as j, type CallAttempt as k, CallError as l, type ChainWithGrounding as m, type Constraints as n, type MutationApplied as o, type NormalizedTokens as p, type OutcomeKind as q, type PerAxisMetricsByModel as r, type PromptSection as s, type ToolDefinition as t };
|
package/dist/profiles.d.mts
CHANGED
package/dist/profiles.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { o as MutationApplied, B as BestPracticeAdvisory, F as FallbackReason, k as CallAttempt } from './ir-B9zqlwjH.mjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Glass-Box observability types (alpha.17).
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { o as MutationApplied, B as BestPracticeAdvisory, F as FallbackReason, k as CallAttempt } from './ir-B_XX2LAO.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Glass-Box observability types (alpha.17).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@warmdrift/kgauto-compiler",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.23",
|
|
4
4
|
"description": "Prompt compiler + central learning brain for multi-model AI apps. Swap models without rewriting prompts.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|